forked from github/server
replace damage_unit with somethign that doesn't malloc
This commit is contained in:
parent
e2a567e9c9
commit
cd8cd3c45d
|
@ -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",
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue