From ad86e69e6b11b94cdff7cbcfc2ff7bf706de8710 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 15 Feb 2018 21:05:11 +0100 Subject: [PATCH] refactor the resurrection code. There is more work to be done here, too much duplicaton between battle and volcano. --- src/battle.c | 30 ++++++++++++++++++++---------- src/items.h | 7 ++++++- src/volcano.c | 49 +++++++++++++++++++++++++++---------------------- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/battle.c b/src/battle.c index 58e8cf9fc..e82ba71a5 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1130,6 +1130,21 @@ int calculate_armor(troop dt, const weapon_type *dwtype, const weapon_type *awty return ar; } +static bool resurrect_troop(troop dt) +{ + fighter *df = dt.fighter; + unit *du = df->unit; + if (oldpotiontype[P_HEAL] && !fval(&df->person[dt.index], FL_HEALING_USED)) { + if (i_get(du->items, oldpotiontype[P_HEAL]) > 0) { + fset(&df->person[dt.index], FL_HEALING_USED); + i_change(&du->items, oldpotiontype[P_HEAL], -1); + df->person[dt.index].hp = u_race(du)->hitpoints * 5; /* give the person a buffer */ + return true; + } + } + return false; +} + bool terminate(troop dt, troop at, int type, const char *damage, bool missile) { @@ -1301,16 +1316,11 @@ terminate(troop dt, troop at, int type, const char *damage, bool missile) } /* healing potions can avert a killing blow */ - if (oldpotiontype[P_HEAL] && !fval(&df->person[dt.index], FL_HEALING_USED)) { - if (i_get(du->items, oldpotiontype[P_HEAL]) > 0) { - message *m = msg_message("potionsave", "unit", du); - battle_message_faction(b, du->faction, m); - msg_release(m); - i_change(&du->items, oldpotiontype[P_HEAL], -1); - fset(&df->person[dt.index], FL_HEALING_USED); - df->person[dt.index].hp = u_race(du)->hitpoints * 5; /* give the person a buffer */ - return false; - } + if (resurrect_troop(dt)) { + message *m = msg_message("potionsave", "unit", du); + battle_message_faction(b, du->faction, m); + msg_release(m); + return false; } ++at.fighter->kills; diff --git a/src/items.h b/src/items.h index 2b839563d..f37444b74 100644 --- a/src/items.h +++ b/src/items.h @@ -12,11 +12,16 @@ without prior permission by the authors of Eressea. #ifndef H_KRNL_ITEMS #define H_KRNL_ITEMS + +#include + +struct unit; + #ifdef __cplusplus extern "C" { #endif - extern void register_itemfunctions(void); + void register_itemfunctions(void); #ifdef __cplusplus } diff --git a/src/volcano.c b/src/volcano.c index df70a864f..a1b2240e2 100644 --- a/src/volcano.c +++ b/src/volcano.c @@ -74,12 +74,31 @@ static int nb_armor(const unit * u, int index) return av; } +static bool resurrect_unit(unit *u) { + if (oldpotiontype[P_HEAL]) { + bool heiltrank = false; + if (get_effect(u, oldpotiontype[P_HEAL]) > 0) { + change_effect(u, oldpotiontype[P_HEAL], -1); + heiltrank = true; + } + else if (i_get(u->items, oldpotiontype[P_HEAL]) > 0) { + i_change(&u->items, oldpotiontype[P_HEAL], -1); + change_effect(u, oldpotiontype[P_HEAL], 3); + heiltrank = true; + } + if (heiltrank && chance(0.50)) { + return true; + } + } + return false; +} + static int damage_unit(unit * u, const char *dam, bool physical, bool magic) { int *hp, hpstack[20]; int h; - int i, dead = 0, hp_rem = 0, heiltrank; + int i, dead = 0, hp_rem = 0; assert(u->number); if (fval(u_race(u), RCF_ILLUSIONARY)) { @@ -118,33 +137,19 @@ damage_unit(unit * u, const char *dam, bool physical, bool magic) /* Auswirkungen */ for (i = 0; i < u->number; i++) { if (hp[i] <= 0) { - heiltrank = 0; - /* Sieben Leben */ if (u_race(u) == get_race(RC_CAT) && (chance(1.0 / 7))) { hp[i] = u->hp / u->number; hp_rem += hp[i]; - continue; } - - /* Heiltrank */ - if (oldpotiontype[P_HEAL]) { - if (get_effect(u, oldpotiontype[P_HEAL]) > 0) { - change_effect(u, oldpotiontype[P_HEAL], -1); - heiltrank = 1; - } - else if (i_get(u->items, oldpotiontype[P_HEAL]) > 0) { - i_change(&u->items, oldpotiontype[P_HEAL], -1); - change_effect(u, oldpotiontype[P_HEAL], 3); - heiltrank = 1; - } - if (heiltrank && (chance(0.50))) { - hp[i] = u->hp / u->number; - hp_rem += hp[i]; - continue; - } + else if (resurrect_unit(u)) { + /* Heiltrank */ + hp[i] = u->hp / u->number; + hp_rem += hp[i]; + } + else { + ++dead; } - dead++; } else { hp_rem += hp[i];