add a terrain_changed function for use with static terrain variables.

This commit is contained in:
Enno Rehling 2017-11-06 20:29:26 +01:00
parent c597bebb8b
commit 7196f799e6
3 changed files with 28 additions and 7 deletions

View file

@ -57,6 +57,17 @@ const char *terraindata[MAXTERRAINS] = {
}; };
static terrain_type *registered_terrains; 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) void free_terrains(void)
{ {
@ -76,6 +87,7 @@ void free_terrains(void)
} }
free(t); free(t);
} }
++terrain_changes;
} }
const terrain_type *terrains(void) 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 * 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) {
++terrain_changes;
terrain = (terrain_type *)calloc(sizeof(terrain_type), 1); terrain = (terrain_type *)calloc(sizeof(terrain_type), 1);
if (terrain) { if (terrain) {
terrain->_name = strdup(name); terrain->_name = strdup(name);

View file

@ -18,6 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef TERRAIN_H #ifndef TERRAIN_H
#define TERRAIN_H #define TERRAIN_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -68,12 +69,13 @@ extern "C" {
struct terrain_type *next; struct terrain_type *next;
} terrain_type; } terrain_type;
extern terrain_type *get_or_create_terrain(const char *name); terrain_type *get_or_create_terrain(const char *name);
extern const terrain_type *terrains(void); const terrain_type *terrains(void);
extern const terrain_type *get_terrain(const char *name); const terrain_type *get_terrain(const char *name);
extern const char *terrain_name(const struct region *r); 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); void free_terrains(void);
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -246,9 +246,15 @@ static void calculate_emigration(region * r)
int maxp = region_maxworkers(r); int maxp = region_maxworkers(r);
int rp = rpeasants(r); int rp = rpeasants(r);
int max_immigrants = MAX_IMMIGRATION(maxp - rp); 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) if (terrain_changed(&terrain_cache)) {
|| r->terrain == newterrain(T_VOLCANO_SMOKING)) { t_volcano = get_terrain("volcano");
t_smoking = get_terrain("activevolcano");
}
if (r->terrain == t_volcano || r->terrain == t_smoking) {
max_immigrants = max_immigrants / 10; max_immigrants = max_immigrants / 10;
} }