From 88ecdd173a1f27188054504fec2a782dc71302e3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 24 Jul 2006 20:35:30 +0000 Subject: [PATCH] http://eressea.upb.de/mantis/view.php?id=959 "Eisberg treibt ab und angelandetes Boot nimmt Schaden" as mentioned in the bug, regions get their neighbor-relations screwed after icebergs drift. in runhash(), I'm removing the relationships so that they are getting rebuilt properly. Issue: 959 --- src/common/kernel/region.c | 883 +++++++++++++++++++------------------ 1 file changed, 448 insertions(+), 435 deletions(-) diff --git a/src/common/kernel/region.c b/src/common/kernel/region.c index c031a04a5..83e48dce6 100644 --- a/src/common/kernel/region.c +++ b/src/common/kernel/region.c @@ -1,7 +1,7 @@ /* vi: set ts=2: * - * - * Eressea PB(E)M host Copyright (C) 1998-2003 + * + * Eressea PB(E)M host Copyright (C) 1998-2003 * Christian Schlittchen (corwin@amber.kn-bremen.de) * Katja Zedel (katze@felidae.kn-bremen.de) * Henning Peters (faroul@beyond.kn-bremen.de) @@ -84,7 +84,7 @@ static const direction_t back[MAXDIRECTIONS] = D_EAST, }; -direction_t +direction_t dir_invert(direction_t dir) { switch (dir) { @@ -101,66 +101,66 @@ dir_invert(direction_t dir) const char * regionname(const region * r, const faction * f) { - static char buf[65]; + static char buf[65]; const struct locale * lang = f ? f->locale : 0; - plane *pl = getplane(r); - if (r==NULL) { - strcpy(buf, "(null)"); - } else if (pl && fval(pl, PFL_NOCOORDS)) { - strncpy(buf, rname(r, lang), 65); - } else { + plane *pl = getplane(r); + if (r==NULL) { + strcpy(buf, "(null)"); + } else if (pl && fval(pl, PFL_NOCOORDS)) { + strncpy(buf, rname(r, lang), 65); + } else { #ifdef HAVE_SNPRINTF - snprintf(buf, 65, "%s (%d,%d)", rname(r, lang), + snprintf(buf, 65, "%s (%d,%d)", rname(r, lang), region_x(r, f), region_y(r, f)); #else - strncpy(buf, rname(r, lang), 50); - buf[50]=0; - sprintf(buf+strlen(buf), " (%d,%d)", region_x(r, f), region_y(r, f)); + strncpy(buf, rname(r, lang), 50); + buf[50]=0; + sprintf(buf+strlen(buf), " (%d,%d)", region_x(r, f), region_y(r, f)); #endif - } - buf[64] = 0; - return buf; + } + buf[64] = 0; + return buf; } int deathcount(const region * r) { - attrib * a = a_find(r->attribs, &at_deathcount); - if (!a) return 0; - return a->data.i; + attrib * a = a_find(r->attribs, &at_deathcount); + if (!a) return 0; + return a->data.i; } int chaoscount(const region * r) { - attrib * a = a_find(r->attribs, &at_chaoscount); - if (!a) return 0; - return a->data.i; + attrib * a = a_find(r->attribs, &at_chaoscount); + if (!a) return 0; + return a->data.i; } void deathcounts (region * r, int fallen) { - attrib * a; + attrib * a; - if (fallen==0) return; - if (is_cursed(r->attribs, C_HOLYGROUND,0)) return; + if (fallen==0) return; + if (is_cursed(r->attribs, C_HOLYGROUND,0)) return; - a = a_find(r->attribs, &at_deathcount); - if (!a) a = a_add(&r->attribs, a_new(&at_deathcount)); - a->data.i += fallen; + a = a_find(r->attribs, &at_deathcount); + if (!a) a = a_add(&r->attribs, a_new(&at_deathcount)); + a->data.i += fallen; - if (a->data.i<=0) a_remove(&r->attribs, a); + if (a->data.i<=0) a_remove(&r->attribs, a); } void chaoscounts(region * r, int fallen) { - attrib * a; + attrib * a; - if (fallen==0) return; + if (fallen==0) return; - a = a_find(r->attribs, &at_chaoscount); - if (!a) a = a_add(&r->attribs, a_new(&at_chaoscount)); - a->data.i += fallen; + a = a_find(r->attribs, &at_chaoscount); + if (!a) a = a_add(&r->attribs, a_new(&at_chaoscount)); + a->data.i += fallen; - if (a->data.i<=0) a_remove(&r->attribs, a); + if (a->data.i<=0) a_remove(&r->attribs, a); } /********************/ @@ -169,49 +169,49 @@ chaoscounts(region * r, int fallen) { void a_initdirection(attrib *a) { - a->data.v = calloc(1, sizeof(spec_direction)); + a->data.v = calloc(1, sizeof(spec_direction)); } int a_agedirection(attrib *a) { - spec_direction *d = (spec_direction *)(a->data.v); + spec_direction *d = (spec_direction *)(a->data.v); - if (d->duration > 0) d->duration--; - else d->duration = 0; + if (d->duration > 0) d->duration--; + else d->duration = 0; - return d->duration; + return d->duration; } int a_readdirection(attrib *a, FILE *f) { - spec_direction *d = (spec_direction *)(a->data.v); - fscanf(f, "%hd %hd %d", &d->x, &d->y, &d->duration); - fscanf(f, "%s ", buf); - d->desc = strdup(cstring(buf)); - fscanf(f, "%s ", buf); - d->keyword = strdup(cstring(buf)); + spec_direction *d = (spec_direction *)(a->data.v); + fscanf(f, "%hd %hd %d", &d->x, &d->y, &d->duration); + fscanf(f, "%s ", buf); + d->desc = strdup(cstring(buf)); + fscanf(f, "%s ", buf); + d->keyword = strdup(cstring(buf)); d->active = true; - return AT_READ_OK; + return AT_READ_OK; } void a_writedirection(const attrib *a, FILE *f) { - spec_direction *d = (spec_direction *)(a->data.v); + spec_direction *d = (spec_direction *)(a->data.v); - fprintf(f, "%d %d %d %s %s ", d->x, d->y, d->duration, - estring(d->desc), estring(d->keyword)); + fprintf(f, "%d %d %d %s %s ", d->x, d->y, d->duration, + estring(d->desc), estring(d->keyword)); } attrib_type at_direction = { - "direction", - a_initdirection, - NULL, - a_agedirection, - a_writedirection, - a_readdirection + "direction", + a_initdirection, + NULL, + a_agedirection, + a_writedirection, + a_readdirection }; @@ -235,7 +235,7 @@ create_special_direction(region *r, region * rt, int duration, /* Moveblock wird zur Zeit nicht über Attribute, sondern ein Bitfeld r->moveblock gemacht. Sollte umgestellt werden, wenn kompliziertere - Dinge gefragt werden. */ + Dinge gefragt werden. */ /********************/ /* at_moveblock */ @@ -243,29 +243,29 @@ create_special_direction(region *r, region * rt, int duration, void a_initmoveblock(attrib *a) { - a->data.v = calloc(1, sizeof(moveblock)); + a->data.v = calloc(1, sizeof(moveblock)); } int a_readmoveblock(attrib *a, FILE *f) { - moveblock *m = (moveblock *)(a->data.v); - int i; + moveblock *m = (moveblock *)(a->data.v); + int i; - fscanf(f, "%d", &i); - m->dir = (direction_t)i; - return AT_READ_OK; + fscanf(f, "%d", &i); + m->dir = (direction_t)i; + return AT_READ_OK; } void a_writemoveblock(const attrib *a, FILE *f) { - moveblock *m = (moveblock *)(a->data.v); - fprintf(f, "%d ", (int)m->dir); + moveblock *m = (moveblock *)(a->data.v); + fprintf(f, "%d ", (int)m->dir); } attrib_type at_moveblock = { - "moveblock", a_initmoveblock, NULL, NULL, a_writemoveblock, a_readmoveblock + "moveblock", a_initmoveblock, NULL, NULL, a_writemoveblock, a_readmoveblock }; @@ -275,100 +275,113 @@ region *regionhash[RMAXHASH]; static region * rfindhash(short x, short y) { - region *old; - for (old = regionhash[coor_hashkey(x, y) % RMAXHASH]; old; old = old->nexthash) - if (old->x == x && old->y == y) - return old; - return 0; + region *old; + for (old = regionhash[coor_hashkey(x, y) % RMAXHASH]; old; old = old->nexthash) + if (old->x == x && old->y == y) + return old; + return 0; } void rhash(region * r) { int key = coor_hashkey(r->x, r->y) % RMAXHASH; - region *old = regionhash[key]; - regionhash[key] = r; - r->nexthash = old; + region *old = regionhash[key]; + regionhash[key] = r; + r->nexthash = old; } void runhash(region * r) { - region **show; - for (show = ®ionhash[coor_hashkey(r->x, r->y) % RMAXHASH]; *show; show = &(*show)->nexthash) { - if ((*show)->x == r->x && (*show)->y == r->y) - break; - } - if (*show) { - assert(*show == r); - *show = (*show)->nexthash; - r->nexthash = 0; - } + region **show; + +#ifdef FAST_CONNECT + int d, di; + for (d=0,di=MAXDIRECTIONS/2;d!=MAXDIRECTIONS;++d,++di) { + region * rc = r->connect[d]; + if (rc!=NULL) { + if (di>=MAXDIRECTIONS) di-=MAXDIRECTIONS; + rc->connect[di] = NULL; + r->connect[d] = NULL; + } + } +#endif + for (show = ®ionhash[coor_hashkey(r->x, r->y) % RMAXHASH]; *show; show = &(*show)->nexthash) { + if ((*show)->x == r->x && (*show)->y == r->y) { + break; + } + } + if (*show) { + assert(*show == r); + *show = (*show)->nexthash; + r->nexthash = 0; + } } region * r_connect(const region * r, direction_t dir) { - static int set = 0; - static region * buffer[MAXDIRECTIONS]; - static const region * last = NULL; + static int set = 0; + static region * buffer[MAXDIRECTIONS]; + static const region * last = NULL; #ifdef FAST_CONNECT - region * rmodify = (region*)r; - assert (dir>=0 && dirconnect[dir]) return r->connect[dir]; + region * rmodify = (region*)r; + assert (dir>=0 && dirconnect[dir]) return r->connect[dir]; #endif - assert(dirx + delta_x[dir], r->y + delta_y[dir]); - set |= (1<x + delta_x[dir], r->y + delta_y[dir]); + set |= (1<connect[dir] = buffer[dir]; + rmodify->connect[dir] = buffer[dir]; #endif - return buffer[dir]; + return buffer[dir]; } region * findregion(short x, short y) { - return rfindhash(x, y); + return rfindhash(x, y); } int koor_distance(int x1, int y1, int x2, int y2) { - /* Contributed by Hubert Mackenberg. Thanks. - * x und y Abstand zwischen x1 und x2 berechnen - */ - int dx = x1 - x2; - int dy = y1 - y2; + /* Contributed by Hubert Mackenberg. Thanks. + * x und y Abstand zwischen x1 und x2 berechnen + */ + int dx = x1 - x2; + int dy = y1 - y2; - /* Bei negativem dy am Ursprung spiegeln, das veraendert - * den Abstand nicht - */ - if ( dy < 0 ) - { - dy = -dy; - dx = -dx; - } + /* Bei negativem dy am Ursprung spiegeln, das veraendert + * den Abstand nicht + */ + if ( dy < 0 ) + { + dy = -dy; + dx = -dx; + } - /* - * dy ist jetzt >=0, fuer dx sind 3 Faelle zu untescheiden - */ - if ( dx >= 0 ) return dx + dy; - else if (-dx >= dy) return -dx; - else return dy; + /* + * dy ist jetzt >=0, fuer dx sind 3 Faelle zu untescheiden + */ + if ( dx >= 0 ) return dx + dy; + else if (-dx >= dy) return -dx; + else return dy; } int distance(const region * r1, const region * r2) { - return koor_distance(r1->x, r1->y, r2->x, r2->y); + return koor_distance(r1->x, r1->y, r2->x, r2->y); } static direction_t @@ -385,7 +398,7 @@ spec_direction * special_direction(const region * from, const region * to) { const attrib *a = a_findc(from->attribs, &at_direction); - + while (a!=NULL && a->type==&at_direction) { spec_direction * sd = (spec_direction *)a->data.v; if (sd->x==to->x && sd->y==to->y) return sd; @@ -409,35 +422,35 @@ reldirection(const region * from, const region * to) void free_regionlist(region_list *rl) { - while (rl) { - region_list * rl2 = rl->next; - free(rl); - rl = rl2; - } + while (rl) { + region_list * rl2 = rl->next; + free(rl); + rl = rl2; + } } void add_regionlist(region_list **rl, region *r) { - region_list *rl2 = (region_list*)malloc(sizeof(region_list)); + region_list *rl2 = (region_list*)malloc(sizeof(region_list)); - rl2->data = r; - rl2->next = *rl; + rl2->data = r; + rl2->next = *rl; - *rl = rl2; + *rl = rl2; } /********************/ /* at_horseluck */ /********************/ attrib_type at_horseluck = { - "horseluck", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - NO_READ, - ATF_UNIQUE + "horseluck", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + NO_READ, + ATF_UNIQUE }; @@ -445,64 +458,64 @@ attrib_type at_horseluck = { /* at_peasantluck */ /**********************/ attrib_type at_peasantluck = { - "peasantluck", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - NO_READ, - ATF_UNIQUE + "peasantluck", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + NO_READ, + ATF_UNIQUE }; /*********************/ /* at_chaoscount */ /*********************/ attrib_type at_chaoscount = { - "chaoscount", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - a_writeint, - a_readint, - ATF_UNIQUE + "chaoscount", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + a_writeint, + a_readint, + ATF_UNIQUE }; /*********************/ /* at_deathcount */ /*********************/ attrib_type at_deathcount = { - "deathcount", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - a_writeint, - a_readint, - ATF_UNIQUE + "deathcount", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + a_writeint, + a_readint, + ATF_UNIQUE }; /*********************/ /* at_woodcount */ /*********************/ attrib_type at_woodcount = { - "woodcount", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - a_readint, - ATF_UNIQUE + "woodcount", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + a_readint, + ATF_UNIQUE }; /*********************/ /* at_travelunit */ /*********************/ attrib_type at_travelunit = { - "travelunit", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - NO_WRITE, - NO_READ + "travelunit", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + NO_WRITE, + NO_READ }; extern int laen_read(attrib * a, FILE * F); @@ -513,58 +526,58 @@ extern int laen_read(attrib * a, FILE * F); /* at_laen */ /***************/ attrib_type at_laen = { - "laen", - DEFAULT_INIT, - DEFAULT_FINALIZE, - DEFAULT_AGE, - LAEN_WRITE, - LAEN_READ, - ATF_UNIQUE + "laen", + DEFAULT_INIT, + DEFAULT_FINALIZE, + DEFAULT_AGE, + LAEN_WRITE, + LAEN_READ, + ATF_UNIQUE }; void rsetlaen(region * r, int val) { - attrib * a = a_find(r->attribs, &at_laen); - if (!a && val>=0) a = a_add(&r->attribs, a_new(&at_laen)); - else if (a && val<0) a_remove(&r->attribs, a); - if (val>=0) a->data.i = val; + attrib * a = a_find(r->attribs, &at_laen); + if (!a && val>=0) a = a_add(&r->attribs, a_new(&at_laen)); + else if (a && val<0) a_remove(&r->attribs, a); + if (val>=0) a->data.i = val; } int rlaen(const region * r) { - attrib * a = a_find(r->attribs, &at_laen); - if (!a) return -1; - return a->data.i; + attrib * a = a_find(r->attribs, &at_laen); + if (!a) return -1; + return a->data.i; } void rsetroad(region * r, direction_t d, short val) { - border * b; - region * r2 = rconnect(r, d); + border * b; + region * r2 = rconnect(r, d); - if (!r2) return; - b = get_borders(r, r2); - while (b && b->type!=&bt_road) b = b->next; - if (!b) b = new_border(&bt_road, r, r2); - if (r==b->from) b->data.sa[0] = val; - else b->data.sa[1] = val; + if (!r2) return; + b = get_borders(r, r2); + while (b && b->type!=&bt_road) b = b->next; + if (!b) b = new_border(&bt_road, r, r2); + if (r==b->from) b->data.sa[0] = val; + else b->data.sa[1] = val; } short rroad(const region * r, direction_t d) { - int rval; - border * b; - region * r2 = rconnect(r, d); + int rval; + border * b; + region * r2 = rconnect(r, d); - if (!r2) return 0; - b = get_borders(r, r2); - while (b && b->type!=&bt_road) b = b->next; - if (!b) return 0; - rval = b->data.i; + if (!r2) return 0; + b = get_borders(r, r2); + while (b && b->type!=&bt_road) b = b->next; + if (!b) return 0; + rval = b->data.i; if (r==b->from) return b->data.sa[0]; return b->data.sa[1]; } @@ -578,107 +591,107 @@ r_isforest(const region * r) int trees = rtrees(r, 2) + rtrees(r, 1); return (trees*TREESIZE >= mincover); } - return false; + return false; } int is_coastregion(region *r) { - direction_t i; - int res = 0; + direction_t i; + int res = 0; - for(i=0;iterrain, SEA_REGION)) res++; - } - return res; + if (rn && fval(rn->terrain, SEA_REGION)) res++; + } + return res; } int rpeasants(const region * r) { - return ((r)->land?(r)->land->peasants:0); + return ((r)->land?(r)->land->peasants:0); } void rsetpeasants(region * r, int value) { - ((r)->land?((r)->land->peasants=(value)):(assert((value)>=0), (value)),0); + ((r)->land?((r)->land->peasants=(value)):(assert((value)>=0), (value)),0); } int rmoney(const region * r) { - return ((r)->land?(r)->land->money:0); + return ((r)->land?(r)->land->money:0); } void rsethorses(const region *r, int value) { - assert(value >= 0); - if(r->land) - r->land->horses = value; + assert(value >= 0); + if(r->land) + r->land->horses = value; } int rhorses(const region *r) { - return r->land?r->land->horses:0; + return r->land?r->land->horses:0; } void rsetmoney(region * r, int value) { - ((r)->land?((r)->land->money=(value)):(assert((value)>=0), (value)),0); + ((r)->land?((r)->land->money=(value)):(assert((value)>=0), (value)),0); } void r_setdemand(region * r, const luxury_type * ltype, int value) { - struct demand * d, ** dp = &r->land->demands; + struct demand * d, ** dp = &r->land->demands; if (ltype==NULL) return; - while (*dp && (*dp)->type != ltype) dp = &(*dp)->next; - d = *dp; - if (!d) { - d = *dp = calloc(sizeof(struct demand), 1); - d->type = ltype; - } - d->value = value; + while (*dp && (*dp)->type != ltype) dp = &(*dp)->next; + d = *dp; + if (!d) { + d = *dp = calloc(sizeof(struct demand), 1); + d->type = ltype; + } + d->value = value; } int r_demand(const region * r, const luxury_type * ltype) { - struct demand * d = r->land->demands; - while (d && d->type != ltype) d = d->next; - if (!d) return -1; - return d->value; + struct demand * d = r->land->demands; + while (d && d->type != ltype) d = d->next; + if (!d) return -1; + return d->value; } const char * rname(const region * r, const struct locale * lang) { - if (r->land) - return r->land->name; - return locale_string(lang, terrain_name(r)); + if (r->land) + return r->land->name; + return locale_string(lang, terrain_name(r)); } int rtrees(const region *r, int ageclass) { - return ((r)->land?(r)->land->trees[ageclass]:0); + return ((r)->land?(r)->land->trees[ageclass]:0); } int rsettrees(const region *r, int ageclass, int value) { - if (!r->land) assert(value==0); - else { - assert(value>=0); - return r->land->trees[ageclass]=value; - } - return 0; + if (!r->land) assert(value==0); + else { + assert(value>=0); + return r->land->trees[ageclass]=value; + } + return 0; } static region *last; @@ -688,41 +701,41 @@ static unsigned int max_index = 0; region * new_region(short x, short y) { - region *r = rfindhash(x, y); + region *r = rfindhash(x, y); - if (r) { - fprintf(stderr, "\ndoppelte regionen entdeckt: %s(%d,%d)\n", regionname(r, NULL), x, y); - if (r->units) - fprintf(stderr, "doppelte region enthält einheiten\n"); - return r; - } - r = calloc(1, sizeof(region)); - r->x = x; - r->y = y; + if (r) { + fprintf(stderr, "\ndoppelte regionen entdeckt: %s(%d,%d)\n", regionname(r, NULL), x, y); + if (r->units) + fprintf(stderr, "doppelte region enthält einheiten\n"); + return r; + } + r = calloc(1, sizeof(region)); + r->x = x; + r->y = y; r->age = 1; - r->planep = findplane(x, y); - set_string(&r->display, ""); - rhash(r); - if (last) - addlist(&last, r); - else - addlist(®ions, r); - last = r; + r->planep = findplane(x, y); + set_string(&r->display, ""); + rhash(r); + if (last) + addlist(&last, r); + else + addlist(®ions, r); + last = r; assert(r->next==NULL); r->index = ++max_index; - return r; + return r; } static void freeland(land_region * lr) { - while (lr->demands) { - struct demand * d = lr->demands; - lr->demands = d->next; - free(d); - } - if (lr->name) free(lr->name); - free(lr); + while (lr->demands) { + struct demand * d = lr->demands; + lr->demands = d->next; + free(d); + } + if (lr->name) free(lr->name); + free(lr); } void @@ -737,7 +750,7 @@ free_region(region * r) free_messagelist(r->msgs); r->msgs = 0; } - + while (r->individual_messages) { struct individual_message * msg = r->individual_messages; r->individual_messages = msg->next; @@ -764,61 +777,61 @@ free_region(region * r) static char * makename(void) { - int s, v, k, e, p = 0, x = 0; - size_t nk, ne, nv, ns; - static char name[16]; - const char *kons = "bcdfghklmnprstvwz", - *end = "nlrdst", - *vokal = "aaaaaaaaaàâeeeeeeeeeéèêiiiiiiiiiíîoooooooooóòôuuuuuuuuuúyy", - *start = "bcdgtskpvfr"; + int s, v, k, e, p = 0, x = 0; + size_t nk, ne, nv, ns; + static char name[16]; + const char *kons = "bcdfghklmnprstvwz", + *end = "nlrdst", + *vokal = "aaaaaaaaaàâeeeeeeeeeéèêiiiiiiiiiíîoooooooooóòôuuuuuuuuuúyy", + *start = "bcdgtskpvfr"; - nk = strlen(kons); - ne = strlen(end); - nv = strlen(vokal); - ns = strlen(start); + nk = strlen(kons); + ne = strlen(end); + nv = strlen(vokal); + ns = strlen(start); - for (s = rng_int() % 3 + 2; s > 0; s--) { - if (x > 0) { - k = rng_int() % (int)nk; - name[p] = kons[k]; - p++; - } else { - k = rng_int() % (int)ns; - name[p] = start[k]; - p++; - } - v = rng_int() % (int)nv; - name[p] = vokal[v]; - p++; - if (rng_int() % 3 == 2 || s == 1) { - e = rng_int() % (int)ne; - name[p] = end[e]; - p++; - x = 1; - } else - x = 0; - } - name[p] = '\0'; - name[0] = (char) toupper(name[0]); - return name; + for (s = rng_int() % 3 + 2; s > 0; s--) { + if (x > 0) { + k = rng_int() % (int)nk; + name[p] = kons[k]; + p++; + } else { + k = rng_int() % (int)ns; + name[p] = start[k]; + p++; + } + v = rng_int() % (int)nv; + name[p] = vokal[v]; + p++; + if (rng_int() % 3 == 2 || s == 1) { + e = rng_int() % (int)ne; + name[p] = end[e]; + p++; + x = 1; + } else + x = 0; + } + name[p] = '\0'; + name[0] = (char) toupper(name[0]); + return name; } -void +void setluxuries(region * r, const luxury_type * sale) { - const luxury_type * ltype; + const luxury_type * ltype; - assert(r->land); + assert(r->land); - if(r->land->demands) freelist(r->land->demands); + if(r->land->demands) freelist(r->land->demands); - for (ltype=luxurytypes; ltype; ltype=ltype->next) { - struct demand * dmd = calloc(sizeof(struct demand), 1); - dmd->type = ltype; - if (ltype!=sale) dmd->value = 1 + rng_int() % 5; - dmd->next = r->land->demands; - r->land->demands = dmd; - } + for (ltype=luxurytypes; ltype; ltype=ltype->next) { + struct demand * dmd = calloc(sizeof(struct demand), 1); + dmd->type = ltype; + if (ltype!=sale) dmd->value = 1 + rng_int() % 5; + dmd->next = r->land->demands; + r->land->demands = dmd; + } } void @@ -830,7 +843,7 @@ terraform(region * r, terrain_t t) void terraform_region(region * r, const terrain_type * terrain) { - /* Resourcen, die nicht mehr vorkommen können, löschen */ + /* Resourcen, die nicht mehr vorkommen können, löschen */ const terrain_type * oldterrain = r->terrain; rawmaterial **lrm = &r->resources; @@ -858,86 +871,86 @@ terraform_region(region * r, const terrain_type * terrain) r->terrain = terrain; terraform_resources(r); - if (!fval(terrain, LAND_REGION)) { - if (r->land!=NULL) { + if (!fval(terrain, LAND_REGION)) { + if (r->land!=NULL) { i_freeall(&r->land->items); - freeland(r->land); - r->land = NULL; - } - rsettrees(r, 0, 0); - rsettrees(r, 1, 0); - rsettrees(r, 2, 0); - rsethorses(r, 0); - rsetpeasants(r, 0); - rsetmoney(r, 0); - freset(r, RF_ENCOUNTER); - freset(r, RF_MALLORN); - /* Beschreibung und Namen löschen */ - return; - } + freeland(r->land); + r->land = NULL; + } + rsettrees(r, 0, 0); + rsettrees(r, 1, 0); + rsettrees(r, 2, 0); + rsethorses(r, 0); + rsetpeasants(r, 0); + rsetmoney(r, 0); + freset(r, RF_ENCOUNTER); + freset(r, RF_MALLORN); + /* Beschreibung und Namen löschen */ + return; + } - if (r->land) { + if (r->land) { i_freeall(&r->land->items); } else { - static struct surround { - struct surround * next; - const luxury_type * type; - int value; - } *trash =NULL, *nb = NULL; - const luxury_type * ltype = NULL; - direction_t d; - int mnr = 0; + static struct surround { + struct surround * next; + const luxury_type * type; + int value; + } *trash =NULL, *nb = NULL; + const luxury_type * ltype = NULL; + direction_t d; + int mnr = 0; - r->land = calloc(1, sizeof(land_region)); - rsetname(r, makename()); - for (d=0;d!=MAXDIRECTIONS;++d) { - region * nr = rconnect(r, d); - if (nr && nr->land) { - struct demand * sale = r->land->demands; - while (sale && sale->value!=0) sale=sale->next; - if (sale) { - struct surround * sr = nb; - while (sr && sr->type!=sale->type) sr=sr->next; - if (!sr) { - if (trash) { - sr = trash; - trash = trash->next; - } else { - sr = calloc(1, sizeof(struct surround)); - } - sr->next = nb; - sr->type = sale->type; - sr->value = 1; - nb = sr; - } else sr->value++; - ++mnr; - } - } - } - if (!nb) { - int i = get_maxluxuries(); + r->land = calloc(1, sizeof(land_region)); + rsetname(r, makename()); + for (d=0;d!=MAXDIRECTIONS;++d) { + region * nr = rconnect(r, d); + if (nr && nr->land) { + struct demand * sale = r->land->demands; + while (sale && sale->value!=0) sale=sale->next; + if (sale) { + struct surround * sr = nb; + while (sr && sr->type!=sale->type) sr=sr->next; + if (!sr) { + if (trash) { + sr = trash; + trash = trash->next; + } else { + sr = calloc(1, sizeof(struct surround)); + } + sr->next = nb; + sr->type = sale->type; + sr->value = 1; + nb = sr; + } else sr->value++; + ++mnr; + } + } + } + if (!nb) { + int i = get_maxluxuries(); if (i>0) { i = rng_int() % i; ltype = luxurytypes; - while (i--) ltype=ltype->next; + while (i--) ltype=ltype->next; } - } else { - int i = rng_int() % mnr; - struct surround * srd = nb; - while (i>srd->value) { - i-=srd->value; - srd=srd->next; - } - if (srd->type) setluxuries(r, srd->type); - while (srd->next!=NULL) srd=srd->next; - srd->next=trash; - trash = nb; - nb = NULL; - } - } - - if (fval(terrain, LAND_REGION)) { - const item_type * itype = NULL; + } else { + int i = rng_int() % mnr; + struct surround * srd = nb; + while (i>srd->value) { + i-=srd->value; + srd=srd->next; + } + if (srd->type) setluxuries(r, srd->type); + while (srd->next!=NULL) srd=srd->next; + srd->next=trash; + trash = nb; + nb = NULL; + } + } + + if (fval(terrain, LAND_REGION)) { + const item_type * itype = NULL; char equip_hash[64]; /* TODO: put the equipment in struct terrain, faster */ @@ -945,17 +958,17 @@ terraform_region(region * r, const terrain_type * terrain) equip_items(&r->land->items, get_equipment(equip_hash)); if (r->terrain->herbs) { - int len=0; - while (r->terrain->herbs[len]) ++len; - if (len) itype = r->terrain->herbs[rng_int()%len]; - } - if (itype!=NULL) { - rsetherbtype(r, itype); - rsetherbs(r, (short)(50+rng_int()%31)); - } - else { - rsetherbtype(r, NULL); - } + int len=0; + while (r->terrain->herbs[len]) ++len; + if (len) itype = r->terrain->herbs[rng_int()%len]; + } + if (itype!=NULL) { + rsetherbtype(r, itype); + rsetherbs(r, (short)(50+rng_int()%31)); + } + else { + rsetherbtype(r, NULL); + } if (oldterrain==NULL || !fval(oldterrain, LAND_REGION)) { if (rng_int() % 100 < 3) fset(r, RF_MALLORN); else freset(r, RF_MALLORN); @@ -963,7 +976,7 @@ terraform_region(region * r, const terrain_type * terrain) fset(r, RF_ENCOUNTER); } } - } + } if (oldterrain==NULL || terrain->size!=oldterrain->size) { if (terrain==newterrain(T_PLAIN)) { @@ -980,12 +993,12 @@ terraform_region(region * r, const terrain_type * terrain) } if (!fval(r, RF_CHAOTIC)) { - int peasants; - peasants = (maxworkingpeasants(r) * (20+dice_rand("6d10")))/100; - rsetpeasants(r, max(100, peasants)); - rsetmoney(r, rpeasants(r) * ((wage(r, NULL, NULL)+1) + rng_int() % 5)); + int peasants; + peasants = (maxworkingpeasants(r) * (20+dice_rand("6d10")))/100; + rsetpeasants(r, max(100, peasants)); + rsetmoney(r, rpeasants(r) * ((wage(r, NULL, NULL)+1) + rng_int() % 5)); } - } + } } /** ENNO: @@ -997,70 +1010,70 @@ terraform_region(region * r, const terrain_type * terrain) int production(const region *r) { - /* muß rterrain(r) sein, nicht rterrain() wegen rekursion */ + /* muß rterrain(r) sein, nicht rterrain() wegen rekursion */ int p = r->terrain->size / MAXPEASANTS_PER_AREA; - if (curse_active(get_curse(r->attribs, ct_find("drought")))) p /= 2; + if (curse_active(get_curse(r->attribs, ct_find("drought")))) p /= 2; - return p; + return p; } int read_region_reference(region ** r, FILE * F) { - variant coor; - fscanf(F, "%hd %hd", &coor.sa[0], &coor.sa[1]); - if (coor.sa[0]==SHRT_MAX) { - *r = NULL; - return AT_READ_FAIL; - } - *r = findregion(coor.sa[0], coor.sa[1]); + variant coor; + fscanf(F, "%hd %hd", &coor.sa[0], &coor.sa[1]); + if (coor.sa[0]==SHRT_MAX) { + *r = NULL; + return AT_READ_FAIL; + } + *r = findregion(coor.sa[0], coor.sa[1]); if (*r==NULL) { ur_add(coor, (void**)r, resolve_region); } - return AT_READ_OK; + return AT_READ_OK; } void write_region_reference(const region * r, FILE * F) { - if (r) fprintf(F, "%d %d ", r->x, r->y); - else fprintf(F, "%d %d ", SHRT_MAX, SHRT_MAX); + if (r) fprintf(F, "%d %d ", r->x, r->y); + else fprintf(F, "%d %d ", SHRT_MAX, SHRT_MAX); } void * resolve_region(variant id) { - return findregion(id.sa[0], id.sa[1]); + return findregion(id.sa[0], id.sa[1]); } struct message_list * r_getmessages(const struct region * r, const struct faction * viewer) { - struct individual_message * imsg = r->individual_messages; - while (imsg && (imsg)->viewer!=viewer) imsg = imsg->next; - if (imsg) return imsg->msgs; - return NULL; + struct individual_message * imsg = r->individual_messages; + while (imsg && (imsg)->viewer!=viewer) imsg = imsg->next; + if (imsg) return imsg->msgs; + return NULL; } -struct message * +struct message * r_addmessage(struct region * r, const struct faction * viewer, struct message * msg) { - struct individual_message * imsg; - assert(r); - imsg = r->individual_messages; - while (imsg && imsg->viewer!=viewer) imsg = imsg->next; - if (imsg==NULL) { - imsg = malloc(sizeof(struct individual_message)); - imsg->next = r->individual_messages; - imsg->msgs = NULL; - r->individual_messages = imsg; - imsg->viewer = viewer; - } - return add_message(&imsg->msgs, msg); + struct individual_message * imsg; + assert(r); + imsg = r->individual_messages; + while (imsg && imsg->viewer!=viewer) imsg = imsg->next; + if (imsg==NULL) { + imsg = malloc(sizeof(struct individual_message)); + imsg->next = r->individual_messages; + imsg->msgs = NULL; + r->individual_messages = imsg; + imsg->viewer = viewer; + } + return add_message(&imsg->msgs, msg); } -struct faction * +struct faction * region_owner(const struct region * r) { #ifdef REGIONOWNERS