replace damage_unit with somethign that doesn't malloc

This commit is contained in:
Enno Rehling 2021-03-11 20:15:25 +01:00
parent e2a567e9c9
commit cd8cd3c45d
3 changed files with 40 additions and 72 deletions

View File

@ -75,85 +75,32 @@ static bool resurrect_unit(unit *u) {
return false; 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 hp = u->hp / u->number;
int h; int remain = u->hp % u->number;
int healings; int ac, i, dead = 0, total = 0;
int i, dead = 0, hp_rem = 0;
assert(u->number); for (i = 0; i != u->number; ++i) {
if (fval(u_race(u), RCF_ILLUSIONARY)) { int damage = dice_rand(dice);
return 0; if (damage > 0) {
} if (i == 0 || ac > 0) {
ac = nb_armor(u, i);
assert(u->number <= u->hp); damage -= ac;
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];
} }
else if (healings > 0) { if (damage > 0) {
--healings; int h = hp + ((i < remain) ? 1 : 0);
if (resurrect_unit(u)) { if (damage >= h) {
/* Heiltrank benutzen */
hp[i] = u->hp / u->number;
hp_rem += hp[i];
}
else {
++dead; ++dead;
} }
else {
total += (h - damage);
}
} }
else {
++dead;
}
}
else {
hp_rem += hp[i];
} }
} }
scale_number(u, u->number - dead); scale_number(u, u->number - dead);
u->hp = hp_rem; u->hp = total;
if (hp != hpstack) {
free(hp);
}
return dead; return dead;
} }
@ -212,7 +159,7 @@ volcano_destruction(region * volcano, region * r, const char *damage)
for (up = &r->units; *up;) { for (up = &r->units; *up;) {
unit *u = *up; unit *u = *up;
if (u->number) { if (u->number) {
int dead = damage_unit(u, damage, true, false); int dead = volcano_damage(u, damage);
/* TODO create undead */ /* TODO create undead */
if (dead) { if (dead) {
ADDMSG(&u->faction->msgs, msg_message("volcano_dead", ADDMSG(&u->faction->msgs, msg_message("volcano_dead",

View File

@ -8,7 +8,7 @@ extern "C" {
#endif #endif
struct region; struct region;
int volcano_damage(struct unit* u, const char* dice);
void volcano_outbreak(struct region * r, struct region *rn); void volcano_outbreak(struct region * r, struct region *rn);
void volcano_update(void); void volcano_update(void);
bool volcano_module(void); bool volcano_module(void);

View File

@ -35,6 +35,26 @@ static void test_volcano_update(CuTest *tc) {
test_teardown(); 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) { static void test_volcano_outbreak(CuTest *tc) {
region *r, *rn; region *r, *rn;
unit *u1, *u2; unit *u1, *u2;
@ -89,6 +109,7 @@ CuSuite *get_volcano_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_volcano_update); SUITE_ADD_TEST(suite, test_volcano_update);
SUITE_ADD_TEST(suite, test_volcano_damage);
SUITE_ADD_TEST(suite, test_volcano_outbreak); SUITE_ADD_TEST(suite, test_volcano_outbreak);
return suite; return suite;
} }