From 7196f799e6a5eb80e3644b20ac29b7128c29027a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Nov 2017 20:29:26 +0100 Subject: [PATCH] add a terrain_changed function for use with static terrain variables. --- src/kernel/terrain.c | 13 +++++++++++++ src/kernel/terrain.h | 12 +++++++----- src/laws.c | 10 ++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/kernel/terrain.c b/src/kernel/terrain.c index 5a70a21e9..4d3f10c74 100644 --- a/src/kernel/terrain.c +++ b/src/kernel/terrain.c @@ -57,6 +57,17 @@ const char *terraindata[MAXTERRAINS] = { }; static terrain_type *registered_terrains; +static int terrain_changes = 1; + +bool terrain_changed(int *cache) { + assert(cache); + if (*cache != terrain_changes) { + *cache = terrain_changes; + return true; + } + return false; +} + void free_terrains(void) { @@ -76,6 +87,7 @@ void free_terrains(void) } free(t); } + ++terrain_changes; } const terrain_type *terrains(void) @@ -110,6 +122,7 @@ const terrain_type *get_terrain(const char *name) { terrain_type * get_or_create_terrain(const char *name) { terrain_type *terrain = terrain_find_i(name); if (!terrain) { + ++terrain_changes; terrain = (terrain_type *)calloc(sizeof(terrain_type), 1); if (terrain) { terrain->_name = strdup(name); diff --git a/src/kernel/terrain.h b/src/kernel/terrain.h index 3d1491867..26c509f75 100644 --- a/src/kernel/terrain.h +++ b/src/kernel/terrain.h @@ -18,6 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifndef TERRAIN_H #define TERRAIN_H + #ifdef __cplusplus extern "C" { #endif @@ -68,12 +69,13 @@ extern "C" { struct terrain_type *next; } terrain_type; - extern terrain_type *get_or_create_terrain(const char *name); - extern const terrain_type *terrains(void); - extern const terrain_type *get_terrain(const char *name); - extern const char *terrain_name(const struct region *r); + terrain_type *get_or_create_terrain(const char *name); + const terrain_type *terrains(void); + const terrain_type *get_terrain(const char *name); + const char *terrain_name(const struct region *r); + bool terrain_changed(int *cache); - extern void init_terrains(void); + void init_terrains(void); void free_terrains(void); #ifdef __cplusplus diff --git a/src/laws.c b/src/laws.c index 1f3633d14..0d3f97f9d 100644 --- a/src/laws.c +++ b/src/laws.c @@ -246,9 +246,15 @@ static void calculate_emigration(region * r) int maxp = region_maxworkers(r); int rp = rpeasants(r); int max_immigrants = MAX_IMMIGRATION(maxp - rp); + static int terrain_cache; + static const terrain_type *t_volcano; + static const terrain_type *t_smoking; - if (r->terrain == newterrain(T_VOLCANO) - || r->terrain == newterrain(T_VOLCANO_SMOKING)) { + if (terrain_changed(&terrain_cache)) { + t_volcano = get_terrain("volcano"); + t_smoking = get_terrain("activevolcano"); + } + if (r->terrain == t_volcano || r->terrain == t_smoking) { max_immigrants = max_immigrants / 10; }