diff --git a/cJSON b/cJSON index 22a4fc9be..8df81fb49 160000 --- a/cJSON +++ b/cJSON @@ -1 +1 @@ -Subproject commit 22a4fc9be31f0426e622f5bc9ebd7a1550845001 +Subproject commit 8df81fb497cc48b089a57fcdc3a9933540ebc7c9 diff --git a/scripts/tests/e2/undead.lua b/scripts/tests/e2/undead.lua index 28d2c20c1..f7586ee4b 100644 --- a/scripts/tests/e2/undead.lua +++ b/scripts/tests/e2/undead.lua @@ -31,9 +31,15 @@ function test_undead_reserve_other() u1.name = 'Xolgrim' process_orders() - -- Intermittent Failure: expected 0 but was 2 + if 0 ~= u1:get_item("log") then + -- try to catch that intermittent bug: + print(u1:show()) + end assert_equal(0, u1:get_item("log")) - + if 2 ~= u2:get_item("log") then + -- try to catch that intermittent bug: + print(u2:show()) + end assert_equal(2, u2:get_item("log")) end diff --git a/src/json.test.c b/src/json.test.c index 30d4e072e..ec814eec6 100644 --- a/src/json.test.c +++ b/src/json.test.c @@ -58,6 +58,7 @@ static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, r int err; region *r; cJSON *json, *attr, *result, *regs; + size_t sz; r = test_create_region(0, 0, terrain); @@ -65,7 +66,8 @@ static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, r err = json_export(&out, EXPORT_REGIONS); CuAssertIntEquals(tc, 0, err); out.api->rewind(out.handle); - out.api->read(out.handle, buf, sizeof(buf)); + sz = out.api->read(out.handle, buf, sizeof(buf)); + buf[sz] = '\0'; mstream_done(&out); json = cJSON_Parse(buf); diff --git a/src/kernel/ally.c b/src/kernel/ally.c index 1999b34b3..5ecf728bc 100644 --- a/src/kernel/ally.c +++ b/src/kernel/ally.c @@ -28,6 +28,7 @@ typedef struct allies { static void block_insert(allies *al, struct faction *f, int status) { int i = al->num++; + assert(status > 0); al->status[i] = status; al->factions[i] = f; /* TODO: heapify */ @@ -46,7 +47,7 @@ static int block_search(allies *al, const struct faction *f) { int allies_walk(struct allies *all, cb_allies_walk callback, void *udata) { - allies *al; + allies *al; for (al = all; al; al = al->next) { int i; for (i = 0; i != al->num; ++i) { @@ -94,13 +95,17 @@ void ally_set(allies **p_al, struct faction *f, int status) return; } if (al->num < BLOCKSIZE) { - block_insert(al, f, status); + if (status > 0) { + block_insert(al, f, status); + } return; } p_al = &al->next; } - *p_al = calloc(1, sizeof(allies)); - block_insert(*p_al, f, status); + if (status > 0) { + *p_al = calloc(1, sizeof(allies)); + block_insert(*p_al, f, status); + } } void write_allies(gamedata * data, const allies *alist) @@ -112,6 +117,7 @@ void write_allies(gamedata * data, const allies *alist) const faction * f = al->factions[i]; if (f && f->_alive) { write_faction_reference(f, data->store); + assert(al->status[i] > 0); WRITE_INT(data->store, al->status[i]); } } @@ -132,7 +138,10 @@ void read_allies(gamedata * data, allies **p_al) f = findfaction(aid); if (!f) f = faction_create(aid); READ_INT(data->store, &status); - ally_set(p_al, f, status); + /* NB: some data files have allies with status=0 */ + if (status > 0) { + ally_set(p_al, f, status); + } } } diff --git a/src/kernel/ally.test.c b/src/kernel/ally.test.c index 248eb5831..1b42a0f1f 100644 --- a/src/kernel/ally.test.c +++ b/src/kernel/ally.test.c @@ -1,6 +1,7 @@ #include #include "types.h" #include "ally.h" +#include "faction.h" #include #include @@ -41,11 +42,36 @@ static void test_allies(CuTest *tc) { test_teardown(); } +static void test_allies_set(CuTest *tc) { + struct faction *f1, *f2; + struct allies * al = NULL; + + test_setup(); + f1 = test_create_faction(NULL); + f2 = test_create_faction(NULL); + + CuAssertPtrEquals(tc, NULL, al); + ally_set(&al, f1, HELP_ALL); + CuAssertPtrNotNull(tc, al); + ally_set(&al, f1, DONT_HELP); + CuAssertPtrEquals(tc, NULL, f1->allies); + ally_set(&al, f1, DONT_HELP); + CuAssertPtrEquals(tc, NULL, al); + + ally_set(&al, f1, HELP_ALL); + ally_set(&al, f2, DONT_HELP); + ally_set(&al, f1, DONT_HELP); + CuAssertPtrEquals(tc, NULL, al); + + test_teardown(); +} + CuSuite *get_ally_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_allies); SUITE_ADD_TEST(suite, test_allies_clone); + SUITE_ADD_TEST(suite, test_allies_set); return suite; } diff --git a/src/races/dragons.c b/src/races/dragons.c index ec16f78dc..5ab877c40 100644 --- a/src/races/dragons.c +++ b/src/races/dragons.c @@ -27,26 +27,26 @@ static int age_chance(int a, int b, int p) { return (r < 0) ? 0 : r; } -#define DRAGONAGE 27 -#define WYRMAGE 68 +#define DRAGONAGE 27 +#define WYRMAGE 68 + +static void evolve_dragon(unit * u, const struct race *rc) { + scale_number(u, 1); + u_setrace(u, rc); + u->irace = NULL; + u->hp = unit_max_hp(u); +} void age_firedragon(unit * u) { if (u->number > 0 && rng_int() % 100 < age_chance(u->age, DRAGONAGE, 1)) { - double q = (double)u->hp / (double)(unit_max_hp(u) * u->number); - u_setrace(u, get_race(RC_DRAGON)); - u->irace = NULL; - scale_number(u, 1); - u->hp = (int)(unit_max_hp(u) * u->number * q); + evolve_dragon(u, get_race(RC_DRAGON)); } } void age_dragon(unit * u) { if (u->number > 0 && rng_int() % 100 < age_chance(u->age, WYRMAGE, 1)) { - double q = (double)u->hp / (double)(unit_max_hp(u) * u->number); - u_setrace(u, get_race(RC_WYRM)); - u->irace = NULL; - u->hp = (int)(unit_max_hp(u) * u->number * q); + evolve_dragon(u, get_race(RC_WYRM)); } } diff --git a/src/report.c b/src/report.c index 7ea25c8d6..669412cdd 100644 --- a/src/report.c +++ b/src/report.c @@ -1509,7 +1509,7 @@ static int show_allies_cb(struct allies *all, faction *af, int status, void *uda if ((mode & HELP_ALL) == HELP_ALL) { sbs_strcat(sbp, LOC(f->locale, parameters[P_ANY])); } - else { + else if (mode > 0) { int h, hh = 0; for (h = 1; h <= HELP_TRAVEL; h *= 2) { int p = MAXPARAMS; @@ -2177,8 +2177,8 @@ report_plaintext(const char *filename, report_context * ctx, if (wants_stats && r->seen.mode >= seen_travel) { if (r->land || r->seen.mode >= seen_unit) { - newline(out); statistics(out, r, f); + newline(out); } } diff --git a/src/util/base36.c b/src/util/base36.c index f77823a0b..33701f0f6 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -88,7 +88,7 @@ const char *itoab_r(int i, int base, char *s, size_t len) } } else { - log_error("static buffer exhauset, itoab(%d, %d)", i, base); + log_error("static buffer exhausted, itoab(%d, %d)", i, base); assert(i == 0 || !"itoab: static buffer exhausted"); } }