Bug 2567: we had alliances with status 0 in the data.

This commit is contained in:
Enno Rehling 2019-03-24 17:18:25 +01:00
parent dd927aebb6
commit ea1bf3d3b9
4 changed files with 43 additions and 8 deletions

View File

@ -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 */
@ -94,13 +95,17 @@ void ally_set(allies **p_al, struct faction *f, int status)
return;
}
if (al->num < BLOCKSIZE) {
if (status > 0) {
block_insert(al, f, status);
}
return;
}
p_al = &al->next;
}
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,8 +138,11 @@ void read_allies(gamedata * data, allies **p_al)
f = findfaction(aid);
if (!f) f = faction_create(aid);
READ_INT(data->store, &status);
/* NB: some data files have allies with status=0 */
if (status > 0) {
ally_set(p_al, f, status);
}
}
}
void allies_free(allies *al)

View File

@ -1,6 +1,7 @@
#include <platform.h>
#include "types.h"
#include "ally.h"
#include "faction.h"
#include <CuTest.h>
#include <tests.h>
@ -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;
}

View File

@ -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);
}
}

View File

@ -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");
}
}