newterrain was unfit for unit testing.

bad test: insects cannot trade in deserts without castle.
This commit is contained in:
Enno Rehling 2018-03-04 20:03:13 +01:00
parent f997ff7e59
commit db253ea6a1
4 changed files with 28 additions and 9 deletions

View File

@ -254,6 +254,7 @@ static void test_trade_insect(CuTest *tc) {
CuAssertPtrEquals(tc, r, u->region); CuAssertPtrEquals(tc, r, u->region);
CuAssertPtrEquals(tc, (void *)it_luxury, (void *)r_luxury(u->region)); CuAssertPtrEquals(tc, (void *)it_luxury, (void *)r_luxury(u->region));
produce(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, 1, get_item(u, it_luxury));
CuAssertIntEquals(tc, 5, get_item(u, it_silver)); CuAssertIntEquals(tc, 5, get_item(u, it_silver));

View File

@ -1202,12 +1202,17 @@ void terraform_region(region * r, const terrain_type * terrain)
} }
if (oldterrain == NULL || terrain->size != oldterrain->size) { if (oldterrain == NULL || terrain->size != oldterrain->size) {
static int changed;
static const terrain_type *t_plain;
int horses = 0, trees = 0; int horses = 0, trees = 0;
if (terrain_changed(&changed)) {
t_plain = get_terrain(terrainnames[T_PLAIN]);
}
if (terrain->size>0) { if (terrain->size>0) {
horses = rng_int() % (terrain->size / 50); horses = rng_int() % (terrain->size / 50);
trees = terrain->size * (30 + rng_int() % 40) / 1000; trees = terrain->size * (30 + rng_int() % 40) / 1000;
} }
if (terrain == newterrain(T_PLAIN)) { if (t_plain && terrain == t_plain) {
rsethorses(r, horses); rsethorses(r, horses);
if (chance(0.4)) { if (chance(0.4)) {
rsettrees(r, 2, trees); rsettrees(r, 2, trees);

View File

@ -38,7 +38,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static const char *terrainnames[MAXTERRAINS] = { const char *terrainnames[MAXTERRAINS] = {
"ocean", "ocean",
"plain", "plain",
"swamp", "swamp",
@ -114,10 +114,14 @@ static terrain_type *terrain_find_i(const char *name)
return terrain; return terrain;
} }
const terrain_type *get_terrain(const char *name) { static terrain_type *get_terrain_i(const char *name) {
return terrain_find_i(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 * get_or_create_terrain(const char *name) {
terrain_type *terrain = terrain_find_i(name); terrain_type *terrain = terrain_find_i(name);
if (!terrain) { if (!terrain) {
@ -127,7 +131,7 @@ terrain_type * get_or_create_terrain(const char *name) {
terrain->_name = str_strdup(name); terrain->_name = str_strdup(name);
terrain->next = registered_terrains; terrain->next = registered_terrains;
registered_terrains = terrain; registered_terrains = terrain;
if (strcmp("plain", name) == 0) { if (strcmp(terrainnames[T_PLAIN], name) == 0) {
/* TODO: this is awful, it belongs in config */ /* TODO: this is awful, it belongs in config */
terrain->name = &plain_name; terrain->name = &plain_name;
} }
@ -136,19 +140,26 @@ terrain_type * get_or_create_terrain(const char *name) {
return terrain; return terrain;
} }
static const terrain_type *newterrains[MAXTERRAINS]; static terrain_type *newterrains[MAXTERRAINS];
const struct terrain_type *newterrain(terrain_t t) const struct terrain_type *newterrain(terrain_t t)
{ {
static int changed;
const struct terrain_type *result; const struct terrain_type *result;
if (t == NOTERRAIN) { if (t == NOTERRAIN) {
return NULL; return NULL;
} }
assert(t >= 0); assert(t >= 0);
assert(t < MAXTERRAINS); assert(t < MAXTERRAINS);
result = newterrains[t]; if (terrain_changed(&changed)) {
memset(newterrains, 0, sizeof(newterrains));
result = NULL;
}
else {
result = newterrains[t];
}
if (!result) { if (!result) {
result = newterrains[t] = get_terrain(terrainnames[t]); result = newterrains[t] = get_terrain_i(terrainnames[t]);
} }
if (!result) { if (!result) {
log_error("no such terrain: %s.", terrainnames[t]); log_error("no such terrain: %s.", terrainnames[t]);
@ -194,11 +205,11 @@ void init_terrains(void)
{ {
terrain_t t; terrain_t t;
for (t = 0; t != MAXTERRAINS; ++t) { for (t = 0; t != MAXTERRAINS; ++t) {
const terrain_type *newterrain = newterrains[t]; terrain_type *newterrain = newterrains[t];
if (newterrain != NULL) if (newterrain != NULL)
continue; continue;
if (terrainnames[t] != NULL) { if (terrainnames[t] != NULL) {
newterrain = get_terrain(terrainnames[t]); newterrain = get_terrain_i(terrainnames[t]);
if (newterrain != NULL) { if (newterrain != NULL) {
newterrains[t] = newterrain; newterrains[t] = newterrain;
} }

View File

@ -36,6 +36,8 @@ extern "C" {
#define SWIM_INTO (1<<8) /* man darf hierhin schwimmen */ #define SWIM_INTO (1<<8) /* man darf hierhin schwimmen */
#define WALK_INTO (1<<9) /* man darf hierhin laufen */ #define WALK_INTO (1<<9) /* man darf hierhin laufen */
extern const char *terrainnames[];
typedef struct production_rule { typedef struct production_rule {
char *name; char *name;
const struct resource_type *rtype; const struct resource_type *rtype;