From 63e19ed66498ef32bcce0e706ae21e1f423d00b4 Mon Sep 17 00:00:00 2001 From: Beats Date: Sat, 9 Sep 2023 14:57:19 -0300 Subject: [PATCH] Weapons::getMaxWeaponDamage >> get_max_weapon_damage in rust --- beats-rust/src/lib.rs | 26 +++++++++++++++++++- src/items/weapons/weapons.cpp | 19 ++++---------- src/items/weapons/weapons.hpp | 1 - src/server/network/protocol/protocolgame.cpp | 11 +++++---- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/beats-rust/src/lib.rs b/beats-rust/src/lib.rs index dd85f58251d..645518b8c04 100644 --- a/beats-rust/src/lib.rs +++ b/beats-rust/src/lib.rs @@ -10,6 +10,16 @@ pub extern "C" fn get_base_attack(level: u32) -> i32 { base_form.floor() as i32 } +#[no_mangle] +pub extern "C" fn get_max_weapon_damage(attack_skill: i32, attack_value: i32, attack_factor: f32, attack_value_base: i32, is_melee: bool) -> i32 { + // Implementação da função + if is_melee { + return (attack_value_base as f32 + ((attack_factor * attack_value as f32) * (attack_skill as f32 + 4.0) / 28.0)) as i32; + } + + return ((0.09 * attack_factor * attack_skill as f32 * attack_value as f32) + attack_value_base as f32).round() as i32; +} + #[cfg(test)] // use: cargo test -- --nocapture / cargo test --package beats --lib -- --nocapture / cargo test --package beats --lib "nome_do_teste_exato" -- --nocapture mod tests { @@ -18,7 +28,21 @@ mod tests { #[test] fn get_attack_base() { let result = get_base_attack(1100); - println!("O valor de resultado é: {}", result); + println!("O valor de get_attack_base é: {}", result); assert_eq!(result, 200); } + + #[test] + fn test_get_max_weapon_damage_melee() { + let damage = get_max_weapon_damage(10, 50, 1.0, 100, true); + println!("O valor de test_get_max_weapon_damage_melee é: {}", damage); + assert_eq!(damage, 125); // Valor exemplo + } + + #[test] + fn test_get_max_weapon_damage_ranged() { + let damage = get_max_weapon_damage(10, 50, 1.0, 100, false); + println!("O valor de test_get_max_weapon_damage_ranged é: {}", damage); + assert_eq!(damage, 145); // Valor exemplo + } } diff --git a/src/items/weapons/weapons.cpp b/src/items/weapons/weapons.cpp index 3aafa3a6854..f808f678515 100644 --- a/src/items/weapons/weapons.cpp +++ b/src/items/weapons/weapons.cpp @@ -16,6 +16,7 @@ extern "C" { int32_t get_base_attack(uint32_t level); +int32_t get_max_weapon_damage(int32_t attack_skill, int32_t attack_value, float attack_factor, int32_t attack_value_base, bool is_melee); } Weapons::Weapons() = default; @@ -48,16 +49,6 @@ int32_t Weapons::getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue) { return static_cast(std::ceil((attackSkill * (attackValue * 0.05)) + (attackValue * 0.5))); } -// Players -int32_t Weapons::getMaxWeaponDamage(const Player* player, const int32_t attack_skill, const int32_t attack_value, const float attack_factor, const int32_t attack_value_base, const bool is_melee) { - - if (is_melee) { - return static_cast(attack_value_base + ((attack_factor * attack_value) * (attack_skill + 4) / 28)); - } - - return static_cast(std::round((0.09 * attack_factor * attack_skill * attack_value) + attack_value_base)); -} - void Weapon::configureWeapon(const ItemType &it) { id = it.id; } @@ -135,7 +126,7 @@ CombatDamage Weapon::getCombatDamage(CombatDamage combat, Player* player, Item* // Calculating damage int32_t minDamage = get_base_attack(player->getLevel()); - int32_t maxDamage = static_cast(Weapons::getMaxWeaponDamage(player, playerSkill, totalAttack, attackFactor, minDamage, true) * player->getVocation()->meleeDamageMultiplier * damageModifier / 100); + int32_t maxDamage = static_cast(get_max_weapon_damage(playerSkill, totalAttack, attackFactor, minDamage, true) * player->getVocation()->meleeDamageMultiplier * damageModifier / 100); int32_t realDamage = normal_random(minDamage, maxDamage); // Setting damage to combat @@ -154,7 +145,7 @@ bool Weapon::useFist(Player* player, Creature* target) { int32_t attackValue = 1; int32_t attackValueBase = get_base_attack(player->getLevel()); - int32_t maxDamage = Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true); + int32_t maxDamage = get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true); CombatParams params; params.combatType = COMBAT_PHYSICALDAMAGE; @@ -475,7 +466,7 @@ int32_t WeaponMelee::getElementDamage(const Player* player, const Creature*, con float attackFactor = player->getAttackFactor(); int32_t minValue = get_base_attack(player->getLevel()); - int32_t maxValue = Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, minValue, true); + int32_t maxValue = get_max_weapon_damage(attackSkill, attackValue, attackFactor, minValue, true); return -normal_random(minValue, static_cast(maxValue * player->getVocation()->meleeDamageMultiplier)); } @@ -490,7 +481,7 @@ int32_t WeaponMelee::getWeaponDamage(const Player* player, const Creature*, cons float attackFactor = player->getAttackFactor(); int32_t minValue = get_base_attack(player->getLevel()); - int32_t maxValue = static_cast(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, minValue, true) * player->getVocation()->meleeDamageMultiplier); + int32_t maxValue = static_cast(get_max_weapon_damage(attackSkill, attackValue, attackFactor, minValue, true) * player->getVocation()->meleeDamageMultiplier); if (maxDamage) { return -maxValue; diff --git a/src/items/weapons/weapons.hpp b/src/items/weapons/weapons.hpp index 1cad404b200..d09700dcfae 100644 --- a/src/items/weapons/weapons.hpp +++ b/src/items/weapons/weapons.hpp @@ -39,7 +39,6 @@ class Weapons final : public Scripts { const Weapon* getWeapon(const Item* item) const; static int32_t getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue); - static int32_t getMaxWeaponDamage(const Player* player, int32_t attackSkill, int32_t attackValue, float attackFactor, int32_t attackValueBase, bool isMelee); static int32_t calculateMeleeWeaponDamage(const int8_t multiplication_factor, const int8_t division_factor, const int32_t attack_value, const int32_t attack_skill, const int32_t attack_value_base, const bool is_balanced_mode); bool registerLuaEvent(Weapon* event); diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 2c12de354d1..2b01cad61fd 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -35,6 +35,7 @@ extern "C" { int32_t get_base_attack(uint32_t level); +int32_t get_max_weapon_damage(int32_t attack_skill, int32_t attack_value, float attack_factor, int32_t attack_value_base, bool is_melee); } /* @@ -3468,9 +3469,9 @@ void ProtocolGame::sendCyclopediaCharacterCombatStats() { int32_t attackSkill = player->getSkillLevel(SKILL_DISTANCE); float attackFactor = player->getAttackFactor(); int32_t attackValueBase = get_base_attack(player->getLevel()); - int32_t maxDamage = static_cast(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier); + int32_t maxDamage = static_cast(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier); if (it.abilities && it.abilities->elementType != COMBAT_NONE) { - maxDamage += static_cast(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue - weapon->getAttack() + it.abilities->elementDamage, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier); + maxDamage += static_cast(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier); } msg.add(maxDamage); msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL); @@ -3485,9 +3486,9 @@ void ProtocolGame::sendCyclopediaCharacterCombatStats() { int32_t attackSkill = player->getWeaponSkill(weapon); float attackFactor = player->getAttackFactor(); int32_t attackValueBase = get_base_attack(player->getLevel()); - int32_t maxDamage = static_cast(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier); + int32_t maxDamage = static_cast(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier); if (it.abilities && it.abilities->elementType != COMBAT_NONE) { - maxDamage += static_cast(Weapons::getMaxWeaponDamage(player, attackSkill, it.abilities->elementDamage, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier); + maxDamage += static_cast(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier); } msg.add(maxDamage); msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL); @@ -3504,7 +3505,7 @@ void ProtocolGame::sendCyclopediaCharacterCombatStats() { int32_t attackValue = 0; int32_t attackValueBase = get_base_attack(player->getLevel()); - int32_t maxDamage = Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true); + int32_t maxDamage = get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true); msg.add(maxDamage); msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL); msg.addByte(0);