refactor the resurrection code. There is more work to be done here, too much duplicaton between battle and volcano.

This commit is contained in:
Enno Rehling 2018-02-15 21:05:11 +01:00
parent a9375200e4
commit ad86e69e6b
3 changed files with 53 additions and 33 deletions

View File

@ -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;

View File

@ -12,11 +12,16 @@ without prior permission by the authors of Eressea.
#ifndef H_KRNL_ITEMS
#define H_KRNL_ITEMS
#include <stdbool.h>
struct unit;
#ifdef __cplusplus
extern "C" {
#endif
extern void register_itemfunctions(void);
void register_itemfunctions(void);
#ifdef __cplusplus
}

View File

@ -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];