From a9375200e4c02282e321300dabf210fe3f2f2086 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 15 Feb 2018 20:35:38 +0100 Subject: [PATCH] use_potion has a lot of scaffolding that use_healingpotion was duplicating. --- src/alchemy.c | 74 ++++++++++++++++++++++++++++++++------------------- src/battle.c | 1 + src/items.c | 36 ++----------------------- 3 files changed, 50 insertions(+), 61 deletions(-) diff --git a/src/alchemy.c b/src/alchemy.c index b766278c6..bfa371c8e 100644 --- a/src/alchemy.c +++ b/src/alchemy.c @@ -154,8 +154,6 @@ static int begin_potion(unit * u, const item_type * itype, struct order *ord) static void end_potion(unit * u, const item_type * itype, int amount) { - use_pooled(u, itype->rtype, GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, - amount); usetpotionuse(u, itype); ADDMSG(&u->faction->msgs, msg_message("usepotion", @@ -173,12 +171,10 @@ static int potion_water_of_life(unit * u, region *r, int amount) { } /* mallorn is required to make mallorn forests, wood for regular ones */ if (fval(r, RF_MALLORN)) { - wood = use_pooled(u, rt_find("mallorn"), - GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); + wood = use_pooled(u, rt_find("mallorn"), GET_DEFAULT, tree_count * amount); } else { - wood = use_pooled(u, rt_find("log"), - GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, tree_count * amount); + wood = use_pooled(u, rt_find("log"), GET_DEFAULT, tree_count * amount); } if (r->land == 0) wood = 0; @@ -211,13 +207,6 @@ void show_potions(faction *f, int sklevel) } } -static int potion_ointment(unit * u, int amount) { - int maxhp = unit_max_hp(u) * u->number; - u->hp = u->hp + 400 * amount; - if (u->hp > maxhp) u->hp = maxhp; - return amount; -} - static int potion_luck(unit *u, region *r, attrib_type *atype, int amount) { attrib *a = (attrib *)a_find(r->attribs, atype); UNUSED_ARG(u); @@ -239,20 +228,56 @@ static int potion_power(unit *u, int amount) { return amount; } +static int heal(unit * user, int effect) +{ + int req = unit_max_hp(user) * user->number - user->hp; + if (req > 0) { + if (req > effect) req = effect; + effect -= req; + user->hp += req; + } + return effect; +} + +static int potion_ointment(unit * u, int amount) { + int effect = amount * 400; + effect = heal(u, effect); + return amount; +} + +static int potion_healing(struct unit *user, int amount) +{ + int effect = amount * 400; + unit *u = user->region->units; + effect = heal(user, effect); + while (effect > 0 && u != NULL) { + if (u->faction == user->faction) { + effect = heal(u, effect); + } + u = u->next; + } + return amount; +} + + static int do_potion(unit * u, region *r, const item_type * itype, int amount) { + /* TODO: why do some of these take a region argument? */ if (itype == oldpotiontype[P_LIFE]) { return potion_water_of_life(u, r, amount); } - else if (itype == oldpotiontype[P_OINTMENT]) { - return potion_ointment(u, amount); - } else if (itype == oldpotiontype[P_PEOPLE]) { return potion_luck(u, r, &at_peasantluck, amount); } else if (itype == oldpotiontype[P_HORSE]) { return potion_luck(u, r, &at_horseluck, amount); } + else if (itype == oldpotiontype[P_HEAL]) { + return potion_healing(u, amount); + } + else if (itype == oldpotiontype[P_OINTMENT]) { + return potion_ointment(u, amount); + } else if (itype == oldpotiontype[P_MACHT]) { return potion_power(u, amount); } @@ -264,17 +289,12 @@ static int do_potion(unit * u, region *r, const item_type * itype, int amount) int use_potion(unit * u, const item_type * itype, int amount, struct order *ord) { - if (oldpotiontype[P_HEAL] && itype == oldpotiontype[P_HEAL]) { - return EUNUSABLE; - } - else { - int result = begin_potion(u, itype, ord); - if (result) - return result; - amount = do_potion(u, u->region, itype, amount); - end_potion(u, itype, amount); - } - return 0; + int result = begin_potion(u, itype, ord); + if (result) + return result; + amount = do_potion(u, u->region, itype, amount); + end_potion(u, itype, amount); + return amount; } /*****************/ diff --git a/src/battle.c b/src/battle.c index b7482e48a..58e8cf9fc 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1300,6 +1300,7 @@ terminate(troop dt, troop at, int type, const char *damage, bool missile) return false; } + /* 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); diff --git a/src/items.c b/src/items.c index f1940a644..d4ceca78a 100644 --- a/src/items.c +++ b/src/items.c @@ -310,39 +310,6 @@ struct order *ord) return 0; } -static int heal(unit * user, int effect) -{ - int req = unit_max_hp(user) * user->number - user->hp; - if (req > 0) { - if (req > effect) req = effect; - effect -= req; - user->hp += req; - } - return effect; -} - -static int -use_healingpotion(struct unit *user, const struct item_type *itype, - int amount, struct order *ord) -{ - int effect = amount * 400; - unit *u = user->region->units; - effect = heal(user, effect); - while (effect > 0 && u != NULL) { - if (u->faction == user->faction) { - effect = heal(u, effect); - } - u = u->next; - } - use_pooled(user, itype->rtype, GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, - amount); - usetpotionuse(user, itype); - - ADDMSG(&user->faction->msgs, msg_message("usepotion", - "unit potion", user, itype->rtype)); - return 0; -} - /* ------------------------------------------------------------- */ /* Kann auch von Nichtmagier benutzt werden, modifiziert Taktik fuer diese * Runde um -1 - 4 Punkte. */ @@ -412,6 +379,7 @@ static int use_warmthpotion(unit *u, const item_type *itype, void register_itemfunctions(void) { /* have tests: */ + /* TODO: potions should really use use_potion */ register_item_use(use_mistletoe, "use_mistletoe"); register_item_use(use_tacticcrystal, "use_dreameye"); register_item_use(use_studypotion, "use_studypotion"); @@ -423,7 +391,7 @@ void register_itemfunctions(void) register_item_use(use_foolpotion, "use_p7"); register_item_use(use_bloodpotion, "use_peasantblood"); register_item_use(use_potion, "use_ointment"); - register_item_use(use_healingpotion, "use_p14"); + register_item_use(use_potion, "use_p14"); register_item_use(use_warmthpotion, "use_nestwarmth"); /* p2 = P_LIFE = Wasser des Lebens */ register_item_use(use_potion, "use_p2");