From 6d8a3efc44961d374c1565b9d490416ced792f4c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Wed, 19 Feb 2025 20:24:34 -0600 Subject: [PATCH 1/3] Refactor: use damage_target() to remove repetitive if-else --- src/game/boe.combat.cpp | 48 ++++++++++++++++++----------------------- src/game/boe.combat.hpp | 3 ++- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/game/boe.combat.cpp b/src/game/boe.combat.cpp index a1b83a20a..4d3d747f6 100644 --- a/src/game/boe.combat.cpp +++ b/src/game/boe.combat.cpp @@ -510,6 +510,7 @@ void char_stand_ready() { univ.current_pc().ap = 0; } +// Melee attack a monster void pc_attack(short who_att,iLiving* target) { short r1,r2; short hit_adj, dam_adj; @@ -586,11 +587,7 @@ void pc_attack(short who_att,iLiving* target) { type = eDamageType::UNDEAD; else if(attacker.race == eRace::DEMON) type = eDamageType::DEMON; - // TODO: Change to damage_target() - if(cCreature* m_target = dynamic_cast(target)) - damage_monst(*m_target, who_att, r2, type,4); - else if(cPlayer* pc_target = dynamic_cast(target)) - damage_pc(*pc_target, r2, type, attacker.race, 4); + damage_target(*target, r2, type, 4, true, who_att, attacker.race); } else { draw_terrain(2); @@ -810,25 +807,19 @@ void pc_attack_weapon(short who_att,iLiving& target,short hit_adj,short dam_adj, short inflicted_weapon_damage = 0; short inflicted_special_damage = 0; short inflicted_bonus_damage = 0; - // TODO: Change these to damage_target() - if(cCreature* monst = dynamic_cast(&target)) { - if(dmg_snd != no_dmg) - inflicted_weapon_damage = damage_monst(*monst, who_att, r2, eDamageType::WEAPON, dmg_snd, false); - if(spec_dam) - inflicted_special_damage = damage_monst(*monst, who_att, spec_dam, eDamageType::SPECIAL, 5, false); - if(bonus_dam) - inflicted_bonus_damage = damage_monst(*monst, who_att, bonus_dam, dmg_tp, 0, false); - if(inflicted_weapon_damage || inflicted_special_damage || inflicted_bonus_damage) + + eRace race = attacker.race; + if(dmg_snd != no_dmg) + inflicted_weapon_damage = damage_target(target, r2, eDamageType::WEAPON, dmg_snd, false, who_att, race); + if(spec_dam) + inflicted_special_damage = damage_target(target, spec_dam, eDamageType::SPECIAL, 5, false, who_att, race); + if(bonus_dam) + inflicted_bonus_damage = damage_target(target, bonus_dam, dmg_tp, 0, false, who_att, race); + + if(inflicted_weapon_damage || inflicted_special_damage || inflicted_bonus_damage){ + if(cCreature* monst = dynamic_cast(&target)) { monst->damaged_msg(inflicted_weapon_damage, inflicted_special_damage + inflicted_bonus_damage); - } else if(cPlayer* who = dynamic_cast(&target)) { - eRace race = attacker.race; - if(dmg_snd != no_dmg) - inflicted_weapon_damage = damage_pc(*who, r2, eDamageType::WEAPON, race, dmg_snd, false); - if(spec_dam) - inflicted_special_damage = damage_pc(*who, spec_dam, eDamageType::SPECIAL, race, dmg_snd, false); - if(bonus_dam) - inflicted_bonus_damage = damage_pc(*who, bonus_dam, dmg_tp, race, dmg_snd, false); - if(inflicted_weapon_damage || inflicted_special_damage || inflicted_bonus_damage) { + } else if(cPlayer* who = dynamic_cast(&target)) { std::string msg = " " + who->name + " takes " + std::to_string(inflicted_weapon_damage); if(inflicted_special_damage + inflicted_bonus_damage) msg += '+' + std::to_string(inflicted_special_damage + inflicted_bonus_damage); @@ -4030,13 +4021,16 @@ bool monst_cast_priest(cCreature *caster,short targ) { return acted; } -void damage_target(short target,short dam,eDamageType type,short sound_type) { - if(target == 6) return; +short damage_target(short target,short dam,eDamageType type,short sound_type, bool do_print, short who_hit, eRace race) { + if(target == 6) return 0; if(target < 6) - damage_pc(univ.party[target],dam,type,eRace::UNKNOWN,sound_type); - else damage_monst(univ.town.monst[target - 100], 7, dam, type,sound_type); + return damage_pc(univ.party[target],dam,type,race,sound_type, do_print); + else return damage_monst(univ.town.monst[target - 100], who_hit, dam, type,sound_type, do_print); } +short damage_target(iLiving& target,short dam,eDamageType type,short sound_type, bool do_print, short who_hit, eRace race) { + return damage_target(univ.get_target_i(target), dam, type, sound_type, do_print, who_hit, race); +} // target = find_fireball_loc(caster->m_loc,1,(caster->attitude == 1) ? 0 : 1,&target_levels); diff --git a/src/game/boe.combat.hpp b/src/game/boe.combat.hpp index fa656658c..a65cd4919 100644 --- a/src/game/boe.combat.hpp +++ b/src/game/boe.combat.hpp @@ -34,7 +34,8 @@ void monst_basic_abil(short m_num, std::pair abil, iLiving* bool monst_breathe(cCreature *caster,location targ_space,uAbility dam_type); bool monst_cast_mage(cCreature *caster,short targ); bool monst_cast_priest(cCreature *caster,short targ); -void damage_target(short target,short dam,eDamageType type,short sound_type = 0); +short damage_target(short target,short dam,eDamageType type,short sound_type = 0, bool do_print = true, short who_hit = 7, eRace race = eRace::UNKNOWN); +short damage_target(iLiving& target,short dam,eDamageType type,short sound_type = 0, bool do_print = true, short who_hit = 7, eRace race = eRace::UNKNOWN); location find_fireball_loc(location where,short radius,short mode,short *m); location closest_pc_loc(location where); short count_levels(location where,short radius); From 87240ccc5c03f6b53b9af173ff47f0dc160d5dd1 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 20 Feb 2025 10:30:29 -0600 Subject: [PATCH 2/3] Don't double-adjust boom text pos. Fix #127 --- src/game/boe.graphics.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/game/boe.graphics.cpp b/src/game/boe.graphics.cpp index b3a3cff98..c98f12418 100644 --- a/src/game/boe.graphics.cpp +++ b/src/game/boe.graphics.cpp @@ -1441,7 +1441,6 @@ void boom_space(location where,short mode,short type,short damage,short sound) { text_rect = dest_rect; text_rect.top += 13; text_rect.height() = 10; - text_rect.offset(x_adj,y_adj); std::string dam_str = std::to_string(damage); style.colour = sf::Color::White; text_rect.offset(-1,-1); From 46f374134e8b591685257b8d1f23b26e06c9860a Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 23 Feb 2025 18:00:59 -0600 Subject: [PATCH 3/3] Exclude more invalid indices from damage_target --- src/game/boe.combat.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/game/boe.combat.cpp b/src/game/boe.combat.cpp index 4d3d747f6..bdd829e75 100644 --- a/src/game/boe.combat.cpp +++ b/src/game/boe.combat.cpp @@ -4022,10 +4022,14 @@ bool monst_cast_priest(cCreature *caster,short targ) { } short damage_target(short target,short dam,eDamageType type,short sound_type, bool do_print, short who_hit, eRace race) { - if(target == 6) return 0; - if(target < 6) - return damage_pc(univ.party[target],dam,type,race,sound_type, do_print); - else return damage_monst(univ.town.monst[target - 100], who_hit, dam, type,sound_type, do_print); + if(target >= 6 && target < 100) return 0; + if(target < 6){ + return damage_pc(univ.party[target], dam, type, race, sound_type, do_print); + }else{ + int monst_idx = target - 100; + if(monst_idx >= univ.town.monst.size()) return 0; + return damage_monst(univ.town.monst[monst_idx], who_hit, dam, type, sound_type, do_print); + } } short damage_target(iLiving& target,short dam,eDamageType type,short sound_type, bool do_print, short who_hit, eRace race) {