From f9346f4332c6460b0134077541de28b39df7c1c8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 12 Mar 2021 20:11:45 +0100 Subject: [PATCH] lucky cats can survive a volcano --- src/volcano.c | 8 +++++++- src/volcano.test.c | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/volcano.c b/src/volcano.c index ef603b46c..c8ebc7608 100644 --- a/src/volcano.c +++ b/src/volcano.c @@ -81,6 +81,7 @@ int volcano_damage(unit* u, const char* dice) int remain = u->hp % u->number; int ac, i, dead = 0, total = 0; int healings = 0; + const struct race* rc_cat = get_race(RC_CAT); /* does this unit have any healing potions or effects? */ if (oldpotiontype[P_HEAL]) { @@ -99,7 +100,12 @@ int volcano_damage(unit* u, const char* dice) int h = hp + ((i < remain) ? 1 : 0); if (damage >= h) { - if (healings > 0) { + if (rc_cat && u_race(u) == rc_cat && ((rng_int() % 7) == 0)) { + /* cats have nine lives */ + total += h; + continue; + } + else if (healings > 0) { --healings; if (resurrect_unit(u)) { /* take no damage at all */ diff --git a/src/volcano.test.c b/src/volcano.test.c index 5b772f9d1..4818e59da 100644 --- a/src/volcano.test.c +++ b/src/volcano.test.c @@ -76,6 +76,29 @@ static void test_volcano_damage_armor(CuTest* tc) { test_teardown(); } +static void test_volcano_damage_cats(CuTest* tc) { + unit* u; + struct race* rc_cat; + + test_setup(); + rc_cat = test_create_race("cat"); + u = test_create_unit(test_create_faction_ex(rc_cat, NULL), test_create_plain(0, 0)); + scale_number(u, 100); + + random_source_inject_constants(0.0, 0); /* cats are always lucky */ + u->hp = u->number * 10; + CuAssertIntEquals(tc, 0, volcano_damage(u, "10")); + CuAssertIntEquals(tc, 100, u->number); + CuAssertIntEquals(tc, 10 * u->number, u->hp); + + random_source_inject_constants(0.0, 1); /* cats are never lucky */ + CuAssertIntEquals(tc, 100, volcano_damage(u, "10")); + CuAssertIntEquals(tc, 0, u->number); + CuAssertIntEquals(tc, 0, u->hp); + + test_teardown(); +} + static void test_volcano_damage_healing_potions(CuTest* tc) { unit* u; item_type* itype; @@ -162,6 +185,7 @@ CuSuite *get_volcano_suite(void) SUITE_ADD_TEST(suite, test_volcano_damage); SUITE_ADD_TEST(suite, test_volcano_damage_healing_potions); SUITE_ADD_TEST(suite, test_volcano_damage_armor); + SUITE_ADD_TEST(suite, test_volcano_damage_cats); SUITE_ADD_TEST(suite, test_volcano_outbreak); return suite; }