From f0ce33304bfb60c955dc514f5112c5f2d49aeafd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 26 Nov 2005 00:08:47 +0000 Subject: [PATCH] kingdoms: handling lack of luxuries --- src/common/kernel/region.c | 28 ++++++++++++++++++---------- src/common/kernel/region.h | 1 + src/mapper/map_modify.c | 19 +++++++++---------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/common/kernel/region.c b/src/common/kernel/region.c index 1775f9658..ebae5c232 100644 --- a/src/common/kernel/region.c +++ b/src/common/kernel/region.c @@ -51,8 +51,17 @@ extern int dice_rand(const char *s); -static int g_maxluxuries = 0; - +int +get_maxluxuries() +{ + static int maxluxuries = -1; + if (maxluxuries==-1) { + const luxury_type * ltype; + maxluxuries = 0; + for (ltype = luxurytypes;ltype;ltype=ltype->next) ++maxluxuries; + } + return maxluxuries; +} const short delta_x[MAXDIRECTIONS] = { -1, 0, 1, 1, 0, -1 @@ -904,7 +913,7 @@ terraform_region(region * r, const terrain_type * terrain) const luxury_type * type; int value; } *trash =NULL, *nb = NULL; - const luxury_type * ltype; + const luxury_type * ltype = NULL; direction_t d; int mnr = 0; @@ -935,13 +944,12 @@ terraform_region(region * r, const terrain_type * terrain) } } if (!nb) { - int i; - if (g_maxluxuries==0) { - for (ltype = luxurytypes;ltype;ltype=ltype->next) ++g_maxluxuries; - } - i = rand() % g_maxluxuries; - ltype = luxurytypes; - while (i--) ltype=ltype->next; + int i = get_maxluxuries(); + if (i>0) { + i = rand() % i; + ltype = luxurytypes; + while (i--) ltype=ltype->next; + } } else { int i = rand() % mnr; struct surround * srd = nb; diff --git a/src/common/kernel/region.h b/src/common/kernel/region.h index 0a2ce3a52..f01793956 100644 --- a/src/common/kernel/region.h +++ b/src/common/kernel/region.h @@ -182,6 +182,7 @@ void deathcounts(struct region * r, int delta); void chaoscounts(struct region * r, int delta); void setluxuries(struct region * r, const struct luxury_type * sale); +extern int get_maxluxuries(); short rroad(const struct region * r, direction_t d); void rsetroad(struct region * r, direction_t d, short value); diff --git a/src/mapper/map_modify.c b/src/mapper/map_modify.c index ddd0592df..88c82270a 100644 --- a/src/mapper/map_modify.c +++ b/src/mapper/map_modify.c @@ -86,7 +86,6 @@ terrain_create(int climate) } static terrain_t newblock[BLOCKSIZE][BLOCKSIZE]; -static int g_maxluxuries; void block_create(short x1, short y1, int size, char chaotisch, int special, const terrain_type * terrain) @@ -173,16 +172,16 @@ block_create(short x1, short y1, int size, char chaotisch, int special, const te * Landstriche werden benannt und bevoelkert, und die produkte * p1 und p2 des Kontinentes werden gesetzt. */ region *r; - int i, i1, i2; + int i, i1 = -1, i2 = -1; const luxury_type *ltype, *p1 = NULL, *p2=NULL; - if (g_maxluxuries==0) { - for (ltype = luxurytypes;ltype;ltype=ltype->next) ++g_maxluxuries; - } - i1 = (item_t)(rand() % g_maxluxuries); - do { - i2 = (item_t)(rand() % g_maxluxuries); - } - while (i2 == i1); + int maxlux = get_maxluxuries(); + if (maxlux>0) { + i1 = (item_t)(rand() % maxlux); + do { + i2 = (item_t)(rand() % maxlux); + } + while (i2 == i1); + } ltype = luxurytypes; for (i=0;!p1 || !p2;++i) { if (i==i1) p1=ltype;