From cd8cd3c45d7fd2b4fb8a848c783004035a421d52 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 11 Mar 2021 20:15:25 +0100 Subject: [PATCH] replace damage_unit with somethign that doesn't malloc --- src/volcano.c | 89 ++++++++++------------------------------------ src/volcano.h | 2 +- src/volcano.test.c | 21 +++++++++++ 3 files changed, 40 insertions(+), 72 deletions(-) diff --git a/src/volcano.c b/src/volcano.c index 1c9d9a320..5ce69767c 100644 --- a/src/volcano.c +++ b/src/volcano.c @@ -75,85 +75,32 @@ static bool resurrect_unit(unit *u) { return false; } -static int damage_unit(unit * u, const char *dam, bool physical, bool magic) +int volcano_damage(unit* u, const char* dice) { - int *hp, hpstack[20]; - int h; - int healings; - int i, dead = 0, hp_rem = 0; + int hp = u->hp / u->number; + int remain = u->hp % u->number; + int ac, i, dead = 0, total = 0; - assert(u->number); - if (fval(u_race(u), RCF_ILLUSIONARY)) { - return 0; - } - - assert(u->number <= u->hp); - h = u->hp / u->number; - /* HP verteilen */ - if (u->number < 20) { - hp = hpstack; - } - else { - hp = malloc(u->number * sizeof(int)); - } - for (i = 0; i < u->number; i++) - hp[i] = h; - h = u->hp - (u->number * h); - for (i = 0; i < h; i++) - hp[i]++; - - /* Schaden */ - for (i = 0; i < u->number; i++) { - int damage = dice_rand(dam); - if (magic) { - variant magres = magic_resistance(u); - int save = magres.sa[0] / magres.sa[1]; - damage -= damage * save; - } - if (physical) { - damage -= nb_armor(u, i); - } - hp[i] -= damage; - } - - /* does this unit have any healing potions or effects? */ - healings = i_get(u->items, oldpotiontype[P_HEAL]) * 4; - healings += get_effect(u, oldpotiontype[P_HEAL]); - /* Auswirkungen */ - for (i = 0; i < u->number; i++) { - if (hp[i] <= 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]; + for (i = 0; i != u->number; ++i) { + int damage = dice_rand(dice); + if (damage > 0) { + if (i == 0 || ac > 0) { + ac = nb_armor(u, i); + damage -= ac; } - else if (healings > 0) { - --healings; - if (resurrect_unit(u)) { - /* Heiltrank benutzen */ - hp[i] = u->hp / u->number; - hp_rem += hp[i]; - } - else { + if (damage > 0) { + int h = hp + ((i < remain) ? 1 : 0); + if (damage >= h) { ++dead; } + else { + total += (h - damage); + } } - else { - ++dead; - } - } - else { - hp_rem += hp[i]; } } - scale_number(u, u->number - dead); - u->hp = hp_rem; - - if (hp != hpstack) { - free(hp); - } - + u->hp = total; return dead; } @@ -212,7 +159,7 @@ volcano_destruction(region * volcano, region * r, const char *damage) for (up = &r->units; *up;) { unit *u = *up; if (u->number) { - int dead = damage_unit(u, damage, true, false); + int dead = volcano_damage(u, damage); /* TODO create undead */ if (dead) { ADDMSG(&u->faction->msgs, msg_message("volcano_dead", diff --git a/src/volcano.h b/src/volcano.h index 0ec72523e..3c5d82f4b 100644 --- a/src/volcano.h +++ b/src/volcano.h @@ -8,7 +8,7 @@ extern "C" { #endif struct region; - + int volcano_damage(struct unit* u, const char* dice); void volcano_outbreak(struct region * r, struct region *rn); void volcano_update(void); bool volcano_module(void); diff --git a/src/volcano.test.c b/src/volcano.test.c index e2425bb19..aa4edffbf 100644 --- a/src/volcano.test.c +++ b/src/volcano.test.c @@ -35,6 +35,26 @@ static void test_volcano_update(CuTest *tc) { test_teardown(); } +static void test_volcano_damage(CuTest* tc) { + unit* u; + + test_setup(); + u = test_create_unit(test_create_faction(), test_create_plain(0, 0)); + scale_number(u, 100); + u->hp = u->number * 20; + CuAssertIntEquals(tc, 0, volcano_damage(u, "10")); + CuAssertIntEquals(tc, u->number * 10, u->hp); + CuAssertIntEquals(tc, 0, volcano_damage(u, "d9")); + + /* 10 people have 11 HP, the rest has 10 and dies */ + u->hp = 1010; + CuAssertIntEquals(tc, 90, volcano_damage(u, "10")); + CuAssertIntEquals(tc, 10, u->number); + CuAssertIntEquals(tc, 10, u->hp); + + test_teardown(); +} + static void test_volcano_outbreak(CuTest *tc) { region *r, *rn; unit *u1, *u2; @@ -89,6 +109,7 @@ CuSuite *get_volcano_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_volcano_update); + SUITE_ADD_TEST(suite, test_volcano_damage); SUITE_ADD_TEST(suite, test_volcano_outbreak); return suite; }