Skip to content

Commit

Permalink
Weapons::getMaxWeaponDamage >> get_max_weapon_damage in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Sep 9, 2023
1 parent 7f3c4fc commit 63e19ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
26 changes: 25 additions & 1 deletion beats-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
19 changes: 5 additions & 14 deletions src/items/weapons/weapons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,16 +49,6 @@ int32_t Weapons::getMaxMeleeDamage(int32_t attackSkill, int32_t attackValue) {
return static_cast<int32_t>(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<int32_t>(attack_value_base + ((attack_factor * attack_value) * (attack_skill + 4) / 28));
}

return static_cast<int32_t>(std::round((0.09 * attack_factor * attack_skill * attack_value) + attack_value_base));
}

void Weapon::configureWeapon(const ItemType &it) {
id = it.id;
}
Expand Down Expand Up @@ -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<int32_t>(Weapons::getMaxWeaponDamage(player, playerSkill, totalAttack, attackFactor, minDamage, true) * player->getVocation()->meleeDamageMultiplier * damageModifier / 100);
int32_t maxDamage = static_cast<int32_t>(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
Expand All @@ -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;
Expand Down Expand Up @@ -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<int32_t>(maxValue * player->getVocation()->meleeDamageMultiplier));
}

Expand All @@ -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<int32_t>(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, minValue, true) * player->getVocation()->meleeDamageMultiplier);
int32_t maxValue = static_cast<int32_t>(get_max_weapon_damage(attackSkill, attackValue, attackFactor, minValue, true) * player->getVocation()->meleeDamageMultiplier);

if (maxDamage) {
return -maxValue;
Expand Down
1 change: 0 additions & 1 deletion src/items/weapons/weapons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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<int32_t>(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier);
int32_t maxDamage = static_cast<int32_t>(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier);
if (it.abilities && it.abilities->elementType != COMBAT_NONE) {
maxDamage += static_cast<int32_t>(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue - weapon->getAttack() + it.abilities->elementDamage, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier);
maxDamage += static_cast<int32_t>(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->distDamageMultiplier);
}
msg.add<uint16_t>(maxDamage);
msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL);
Expand All @@ -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<int32_t>(Weapons::getMaxWeaponDamage(player, attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier);
int32_t maxDamage = static_cast<int32_t>(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier);
if (it.abilities && it.abilities->elementType != COMBAT_NONE) {
maxDamage += static_cast<int32_t>(Weapons::getMaxWeaponDamage(player, attackSkill, it.abilities->elementDamage, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier);
maxDamage += static_cast<int32_t>(get_max_weapon_damage(attackSkill, attackValue, attackFactor, attackValueBase, true) * player->getVocation()->meleeDamageMultiplier);
}
msg.add<uint16_t>(maxDamage);
msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL);
Expand All @@ -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<uint16_t>(maxDamage);
msg.addByte(CIPBIA_ELEMENTAL_PHYSICAL);
msg.addByte(0);
Expand Down

0 comments on commit 63e19ed

Please sign in to comment.