From db253ea6a16be7674c3ba9a6bd499e785d8d42ef Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 4 Mar 2018 20:03:13 +0100 Subject: [PATCH] newterrain was unfit for unit testing. bad test: insects cannot trade in deserts without castle. --- src/economy.test.c | 1 + src/kernel/region.c | 7 ++++++- src/kernel/terrain.c | 27 +++++++++++++++++++-------- src/kernel/terrain.h | 2 ++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index 943ac2090..282318191 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -254,6 +254,7 @@ static void test_trade_insect(CuTest *tc) { CuAssertPtrEquals(tc, r, u->region); CuAssertPtrEquals(tc, (void *)it_luxury, (void *)r_luxury(u->region)); produce(u->region); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(u->faction->msgs, "error119")); CuAssertIntEquals(tc, 1, get_item(u, it_luxury)); CuAssertIntEquals(tc, 5, get_item(u, it_silver)); diff --git a/src/kernel/region.c b/src/kernel/region.c index b44ce3ec2..790a8341c 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -1202,12 +1202,17 @@ void terraform_region(region * r, const terrain_type * terrain) } if (oldterrain == NULL || terrain->size != oldterrain->size) { + static int changed; + static const terrain_type *t_plain; int horses = 0, trees = 0; + if (terrain_changed(&changed)) { + t_plain = get_terrain(terrainnames[T_PLAIN]); + } if (terrain->size>0) { horses = rng_int() % (terrain->size / 50); trees = terrain->size * (30 + rng_int() % 40) / 1000; } - if (terrain == newterrain(T_PLAIN)) { + if (t_plain && terrain == t_plain) { rsethorses(r, horses); if (chance(0.4)) { rsettrees(r, 2, trees); diff --git a/src/kernel/terrain.c b/src/kernel/terrain.c index 6a5b4652d..c7e007891 100644 --- a/src/kernel/terrain.c +++ b/src/kernel/terrain.c @@ -38,7 +38,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -static const char *terrainnames[MAXTERRAINS] = { +const char *terrainnames[MAXTERRAINS] = { "ocean", "plain", "swamp", @@ -114,10 +114,14 @@ static terrain_type *terrain_find_i(const char *name) return terrain; } -const terrain_type *get_terrain(const char *name) { +static terrain_type *get_terrain_i(const char *name) { return terrain_find_i(name); } +const terrain_type *get_terrain(const char *name) { + return get_terrain_i(name); +} + terrain_type * get_or_create_terrain(const char *name) { terrain_type *terrain = terrain_find_i(name); if (!terrain) { @@ -127,7 +131,7 @@ terrain_type * get_or_create_terrain(const char *name) { terrain->_name = str_strdup(name); terrain->next = registered_terrains; registered_terrains = terrain; - if (strcmp("plain", name) == 0) { + if (strcmp(terrainnames[T_PLAIN], name) == 0) { /* TODO: this is awful, it belongs in config */ terrain->name = &plain_name; } @@ -136,19 +140,26 @@ terrain_type * get_or_create_terrain(const char *name) { return terrain; } -static const terrain_type *newterrains[MAXTERRAINS]; +static terrain_type *newterrains[MAXTERRAINS]; const struct terrain_type *newterrain(terrain_t t) { + static int changed; const struct terrain_type *result; if (t == NOTERRAIN) { return NULL; } assert(t >= 0); assert(t < MAXTERRAINS); - result = newterrains[t]; + if (terrain_changed(&changed)) { + memset(newterrains, 0, sizeof(newterrains)); + result = NULL; + } + else { + result = newterrains[t]; + } if (!result) { - result = newterrains[t] = get_terrain(terrainnames[t]); + result = newterrains[t] = get_terrain_i(terrainnames[t]); } if (!result) { log_error("no such terrain: %s.", terrainnames[t]); @@ -194,11 +205,11 @@ void init_terrains(void) { terrain_t t; for (t = 0; t != MAXTERRAINS; ++t) { - const terrain_type *newterrain = newterrains[t]; + terrain_type *newterrain = newterrains[t]; if (newterrain != NULL) continue; if (terrainnames[t] != NULL) { - newterrain = get_terrain(terrainnames[t]); + newterrain = get_terrain_i(terrainnames[t]); if (newterrain != NULL) { newterrains[t] = newterrain; } diff --git a/src/kernel/terrain.h b/src/kernel/terrain.h index 26c509f75..d49d04054 100644 --- a/src/kernel/terrain.h +++ b/src/kernel/terrain.h @@ -36,6 +36,8 @@ extern "C" { #define SWIM_INTO (1<<8) /* man darf hierhin schwimmen */ #define WALK_INTO (1<<9) /* man darf hierhin laufen */ + extern const char *terrainnames[]; + typedef struct production_rule { char *name; const struct resource_type *rtype;