2010-08-08 10:06:34 +02:00
|
|
|
|
/*
|
|
|
|
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
|
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
#include <platform.h>
|
|
|
|
|
#include <kernel/config.h>
|
|
|
|
|
#include "region.h"
|
|
|
|
|
|
|
|
|
|
/* kernel includes */
|
|
|
|
|
#include "alliance.h"
|
|
|
|
|
#include "building.h"
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
#include "curse.h"
|
|
|
|
|
#include "equipment.h"
|
|
|
|
|
#include "faction.h"
|
|
|
|
|
#include "item.h"
|
|
|
|
|
#include "message.h"
|
|
|
|
|
#include "plane.h"
|
|
|
|
|
#include "region.h"
|
|
|
|
|
#include "resources.h"
|
|
|
|
|
#include "save.h"
|
|
|
|
|
#include "ship.h"
|
|
|
|
|
#include "terrain.h"
|
|
|
|
|
#include "terrainid.h"
|
|
|
|
|
#include "unit.h"
|
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
|
|
/* util includes */
|
|
|
|
|
#include <util/attrib.h>
|
|
|
|
|
#include <util/goodies.h>
|
|
|
|
|
#include <util/lists.h>
|
|
|
|
|
#include <util/log.h>
|
|
|
|
|
#include <util/resolve.h>
|
|
|
|
|
#include <util/umlaut.h>
|
|
|
|
|
#include <util/language.h>
|
|
|
|
|
#include <util/rand.h>
|
|
|
|
|
#include <util/rng.h>
|
|
|
|
|
#include <util/storage.h>
|
|
|
|
|
|
|
|
|
|
#include <modules/autoseed.h>
|
|
|
|
|
|
|
|
|
|
/* libc includes */
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
extern int dice_rand(const char *s);
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *regions;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2012-05-16 00:04:23 +02:00
|
|
|
|
int get_maxluxuries(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
static int maxluxuries = -1;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (maxluxuries == -1) {
|
|
|
|
|
const luxury_type *ltype;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
maxluxuries = 0;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (ltype = luxurytypes; ltype; ltype = ltype->next)
|
|
|
|
|
++maxluxuries;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return maxluxuries;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
|
|
|
|
const int delta_x[MAXDIRECTIONS] = {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
-1, 0, 1, 1, 0, -1
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const int delta_y[MAXDIRECTIONS] = {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
1, 1, 0, -1, -1, 0
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static const direction_t back[MAXDIRECTIONS] = {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
D_SOUTHEAST,
|
|
|
|
|
D_SOUTHWEST,
|
|
|
|
|
D_WEST,
|
|
|
|
|
D_NORTHWEST,
|
|
|
|
|
D_NORTHEAST,
|
|
|
|
|
D_EAST,
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
direction_t dir_invert(direction_t dir)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
switch (dir) {
|
|
|
|
|
case D_PAUSE:
|
|
|
|
|
case D_SPECIAL:
|
|
|
|
|
return dir;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dir >= 0 && dir < MAXDIRECTIONS)
|
|
|
|
|
return back[dir];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
assert(!"illegal direction");
|
|
|
|
|
return NODIRECTION;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *write_regionname(const region * r, const faction * f, char *buffer,
|
|
|
|
|
size_t size)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
char *buf = (char *)buffer;
|
|
|
|
|
const struct locale *lang = f ? f->locale : 0;
|
|
|
|
|
if (r == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
strcpy(buf, "(null)");
|
|
|
|
|
} else {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
plane *pl = rplane(r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int nx = r->x, ny = r->y;
|
|
|
|
|
pnormalize(&nx, &ny, pl);
|
|
|
|
|
adjust_coordinates(f, &nx, &ny, pl, r);
|
|
|
|
|
snprintf(buf, size, "%s (%d,%d)", rname(r, lang), nx, ny);
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
buf[size - 1] = 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *regionname(const region * r, const faction * f)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
static char buf[NAMESIZE];
|
|
|
|
|
return write_regionname(r, f, buf, sizeof(buf));
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int deathcount(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
attrib *a = a_find(r->attribs, &at_deathcount);
|
|
|
|
|
if (!a)
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return a->data.i;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int chaoscount(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
attrib *a = a_find(r->attribs, &at_chaoscount);
|
|
|
|
|
if (!a)
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return a->data.i;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void deathcounts(region * r, int fallen)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
attrib *a;
|
|
|
|
|
static const curse_type *ctype = NULL;
|
|
|
|
|
|
|
|
|
|
if (fallen == 0)
|
|
|
|
|
return;
|
|
|
|
|
if (!ctype)
|
|
|
|
|
ctype = ct_find("holyground");
|
|
|
|
|
if (ctype && curse_active(get_curse(r->attribs, ctype)))
|
|
|
|
|
return;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
a = a_find(r->attribs, &at_deathcount);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (!a)
|
|
|
|
|
a = a_add(&r->attribs, a_new(&at_deathcount));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
a->data.i += fallen;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (a->data.i <= 0)
|
|
|
|
|
a_remove(&r->attribs, a);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void chaoscounts(region * r, int fallen)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
attrib *a;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (fallen == 0)
|
|
|
|
|
return;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
a = a_find(r->attribs, &at_chaoscount);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (!a)
|
|
|
|
|
a = a_add(&r->attribs, a_new(&at_chaoscount));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
a->data.i += fallen;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (a->data.i <= 0)
|
|
|
|
|
a_remove(&r->attribs, a);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
|
/* at_direction */
|
|
|
|
|
/********************/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static void a_initdirection(attrib * a)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
a->data.v = calloc(1, sizeof(spec_direction));
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static void a_freedirection(attrib * a)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
free(a->data.v);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static int a_agedirection(attrib * a)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
spec_direction *d = (spec_direction *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
--d->duration;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return (d->duration > 0) ? AT_AGE_KEEP : AT_AGE_REMOVE;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct dir_lookup {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
char *name;
|
|
|
|
|
const char *oldname;
|
|
|
|
|
struct dir_lookup *next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
} dir_lookup;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static dir_lookup *dir_name_lookup;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void register_special_direction(const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct locale *lang;
|
|
|
|
|
char *str = strdup(name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (lang = locales; lang; lang = nextlocale(lang)) {
|
2012-05-16 00:04:23 +02:00
|
|
|
|
void **tokens = get_translations(lang, UT_SPECDIR);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *token = LOC(lang, name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
if (token) {
|
|
|
|
|
variant var;
|
|
|
|
|
|
|
|
|
|
var.v = str;
|
|
|
|
|
addtoken(tokens, token, var);
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (lang == default_locale) {
|
|
|
|
|
dir_lookup *dl = malloc(sizeof(dir_lookup));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dl->name = str;
|
|
|
|
|
dl->oldname = token;
|
|
|
|
|
dl->next = dir_name_lookup;
|
|
|
|
|
dir_name_lookup = dl;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log_error(("no translation for spec_direction '%s' in locale '%s'\n",
|
2011-03-07 08:02:35 +01:00
|
|
|
|
name, locale_name(lang)));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static int a_readdirection(attrib * a, void *owner, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
spec_direction *d = (spec_direction *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
d->x = (short)store->r_int(store);
|
|
|
|
|
d->y = (short)store->r_int(store);
|
|
|
|
|
d->duration = store->r_int(store);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (store->version < UNICODE_VERSION) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
char lbuf[16];
|
2011-03-07 08:02:35 +01:00
|
|
|
|
dir_lookup *dl = dir_name_lookup;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
store->r_tok_buf(store, NULL, 0);
|
|
|
|
|
store->r_tok_buf(store, lbuf, sizeof(lbuf));
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
cstring_i(lbuf);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (; dl; dl = dl->next) {
|
|
|
|
|
if (strcmp(lbuf, dl->oldname) == 0) {
|
|
|
|
|
d->keyword = strdup(dl->name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
sprintf(lbuf, "%s_desc", d->keyword);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
d->desc = strdup(dl->name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dl == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
log_error(("unknown spec_direction '%s'\n", lbuf));
|
|
|
|
|
assert(!"not implemented");
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} else {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
d->desc = store->r_tok(store);
|
|
|
|
|
d->keyword = store->r_tok(store);
|
|
|
|
|
}
|
|
|
|
|
d->active = true;
|
|
|
|
|
return AT_READ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2011-03-07 08:02:35 +01:00
|
|
|
|
a_writedirection(const attrib * a, const void *owner, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
spec_direction *d = (spec_direction *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
store->w_int(store, d->x);
|
|
|
|
|
store->w_int(store, d->y);
|
|
|
|
|
store->w_int(store, d->duration);
|
|
|
|
|
store->w_tok(store, d->desc);
|
|
|
|
|
store->w_tok(store, d->keyword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrib_type at_direction = {
|
|
|
|
|
"direction",
|
|
|
|
|
a_initdirection,
|
|
|
|
|
a_freedirection,
|
|
|
|
|
a_agedirection,
|
|
|
|
|
a_writedirection,
|
|
|
|
|
a_readdirection
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *find_special_direction(const region * r, const char *token,
|
|
|
|
|
const struct locale *lang)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
attrib *a;
|
|
|
|
|
spec_direction *d;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (strlen(token) == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
for (a = a_find(r->attribs, &at_direction); a && a->type == &at_direction;
|
|
|
|
|
a = a->next) {
|
|
|
|
|
d = (spec_direction *) (a->data.v);
|
|
|
|
|
|
|
|
|
|
if (d->active) {
|
2012-05-16 00:04:23 +02:00
|
|
|
|
void **tokens = get_translations(lang, UT_SPECDIR);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
variant var;
|
2012-05-16 00:04:23 +02:00
|
|
|
|
if (findtoken(*tokens, token, &var) == E_TOK_SUCCESS) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (strcmp((const char *)var.v, d->keyword) == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return findregion(d->x, d->y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
attrib *create_special_direction(region * r, region * rt, int duration,
|
|
|
|
|
const char *desc, const char *keyword)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
attrib *a = a_add(&r->attribs, a_new(&at_direction));
|
2011-03-07 08:02:35 +01:00
|
|
|
|
spec_direction *d = (spec_direction *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
d->active = false;
|
|
|
|
|
d->x = rt->x;
|
|
|
|
|
d->y = rt->y;
|
|
|
|
|
d->duration = duration;
|
|
|
|
|
d->desc = strdup(desc);
|
|
|
|
|
d->keyword = strdup(keyword);
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Moveblock wird zur Zeit nicht <20>ber Attribute, sondern ein Bitfeld
|
|
|
|
|
r->moveblock gemacht. Sollte umgestellt werden, wenn kompliziertere
|
|
|
|
|
Dinge gefragt werden. */
|
|
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
|
/* at_moveblock */
|
|
|
|
|
/********************/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void a_initmoveblock(attrib * a)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
a->data.v = calloc(1, sizeof(moveblock));
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int a_readmoveblock(attrib * a, void *owner, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
moveblock *m = (moveblock *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
i = store->r_int(store);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
m->dir = (direction_t) i;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return AT_READ_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-03-07 08:02:35 +01:00
|
|
|
|
a_writemoveblock(const attrib * a, const void *owner, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
moveblock *m = (moveblock *) (a->data.v);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
store->w_int(store, (int)m->dir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrib_type at_moveblock = {
|
|
|
|
|
"moveblock", a_initmoveblock, NULL, NULL, a_writemoveblock, a_readmoveblock
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define coor_hashkey(x, y) (unsigned int)((x<<16) + y)
|
|
|
|
|
#define RMAXHASH MAXREGIONS
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static region *regionhash[RMAXHASH];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
static int dummy_data;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static region *dummy_ptr = (region *) & dummy_data; /* a funny hack */
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
typedef struct uidhashentry {
|
|
|
|
|
unsigned int uid;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
} uidhashentry;
|
|
|
|
|
static uidhashentry uidhash[MAXREGIONS];
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct region *findregionbyid(unsigned int uid)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int key = uid % MAXREGIONS;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (uidhash[key].uid != 0 && uidhash[key].uid != uid)
|
|
|
|
|
++key;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return uidhash[key].r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define DELMARKER dummy_ptr
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static void unhash_uid(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int key = r->uid % MAXREGIONS;
|
|
|
|
|
assert(r->uid);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (uidhash[key].uid != 0 && uidhash[key].uid != r->uid)
|
|
|
|
|
++key;
|
|
|
|
|
assert(uidhash[key].r == r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
uidhash[key].r = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static void hash_uid(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
unsigned int uid = r->uid;
|
|
|
|
|
for (;;) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (uid != 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int key = uid % MAXREGIONS;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (uidhash[key].uid != 0 && uidhash[key].uid != uid)
|
|
|
|
|
++key;
|
|
|
|
|
if (uidhash[key].uid == 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
uidhash[key].uid = uid;
|
|
|
|
|
uidhash[key].r = r;
|
|
|
|
|
break;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
}
|
|
|
|
|
assert(uidhash[key].r != r || !"duplicate registration");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
r->uid = uid = rng_int();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define HASH_STATISTICS 1
|
|
|
|
|
#if HASH_STATISTICS
|
|
|
|
|
static int hash_requests;
|
|
|
|
|
static int hash_misses;
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
boolean pnormalize(int *x, int *y, const plane * pl)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
if (pl) {
|
|
|
|
|
if (x) {
|
|
|
|
|
int width = pl->maxx - pl->minx + 1;
|
|
|
|
|
int nx = *x - pl->minx;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
nx = (nx > 0) ? nx : (width - (-nx) % width);
|
|
|
|
|
*x = nx % width + pl->minx;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
if (y) {
|
|
|
|
|
int height = pl->maxy - pl->miny + 1;
|
|
|
|
|
int ny = *y - pl->miny;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
ny = (ny > 0) ? ny : (height - (-ny) % height);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
*y = ny % height + pl->miny;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return false; /* TBD */
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static region *rfindhash(int x, int y)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2012-05-16 00:04:23 +02:00
|
|
|
|
unsigned int rid = coor_hashkey(x, y);
|
|
|
|
|
int key = HASH1(rid, RMAXHASH), gk = HASH2(rid, RMAXHASH);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#if HASH_STATISTICS
|
|
|
|
|
++hash_requests;
|
|
|
|
|
#endif
|
2012-05-16 00:04:23 +02:00
|
|
|
|
while (regionhash[key] != NULL && (regionhash[key] == DELMARKER
|
|
|
|
|
|| regionhash[key]->x != x || regionhash[key]->y != y)) {
|
|
|
|
|
key = (key + gk) % RMAXHASH;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#if HASH_STATISTICS
|
2012-05-16 00:04:23 +02:00
|
|
|
|
++hash_misses;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#endif
|
|
|
|
|
}
|
2012-05-16 00:04:23 +02:00
|
|
|
|
return regionhash[key];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void rhash(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unsigned int rid = coor_hashkey(r->x, r->y);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int key = HASH1(rid, RMAXHASH), gk = HASH2(rid, RMAXHASH);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (regionhash[key] != NULL && regionhash[key] != DELMARKER
|
|
|
|
|
&& regionhash[key] != r) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
key = (key + gk) % RMAXHASH;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(regionhash[key] != r || !"trying to add the same region twice");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
regionhash[key] = r;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void runhash(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unsigned int rid = coor_hashkey(r->x, r->y);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int key = HASH1(rid, RMAXHASH), gk = HASH2(rid, RMAXHASH);
|
|
|
|
|
|
|
|
|
|
#ifdef FAST_CONNECT
|
|
|
|
|
int d, di;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (d = 0, di = MAXDIRECTIONS / 2; d != MAXDIRECTIONS; ++d, ++di) {
|
|
|
|
|
region *rc = r->connect[d];
|
|
|
|
|
if (rc != NULL) {
|
|
|
|
|
if (di >= MAXDIRECTIONS)
|
|
|
|
|
di -= MAXDIRECTIONS;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rc->connect[di] = NULL;
|
|
|
|
|
r->connect[d] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (regionhash[key] != NULL && regionhash[key] != r) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
key = (key + gk) % RMAXHASH;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(regionhash[key] == r || !"trying to remove a unit that is not hashed");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
regionhash[key] = DELMARKER;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r_connect(const region * r, direction_t dir)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *result;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int x, y;
|
|
|
|
|
#ifdef FAST_CONNECT
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *rmodify = (region *) r;
|
|
|
|
|
assert(dir >= 0 && dir < MAXDIRECTIONS);
|
|
|
|
|
if (r->connect[dir])
|
|
|
|
|
return r->connect[dir];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
#endif
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(dir < MAXDIRECTIONS);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
x = r->x + delta_x[dir];
|
|
|
|
|
y = r->y + delta_y[dir];
|
|
|
|
|
pnormalize(&x, &y, rplane(r));
|
|
|
|
|
result = rfindhash(x, y);
|
|
|
|
|
#ifdef FAST_CONNECT
|
|
|
|
|
if (result) {
|
|
|
|
|
rmodify->connect[dir] = result;
|
|
|
|
|
result->connect[back[dir]] = rmodify;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *findregion(int x, int y)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
return rfindhash(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Contributed by Hubert Mackenberg. Thanks.
|
|
|
|
|
* x und y Abstand zwischen x1 und x2 berechnen
|
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static int koor_distance_orig(int x1, int y1, int x2, int y2)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int dx = x1 - x2;
|
|
|
|
|
int dy = y1 - y2;
|
|
|
|
|
|
|
|
|
|
/* Bei negativem dy am Ursprung spiegeln, das veraendert
|
|
|
|
|
* den Abstand nicht
|
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dy < 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dy = -dy;
|
|
|
|
|
dx = -dx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* dy ist jetzt >=0, fuer dx sind 3 Faelle zu untescheiden
|
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dx >= 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int result = dx + dy;
|
|
|
|
|
return result;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} else if (-dx >= dy) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int result = -dx;
|
|
|
|
|
return result;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} else {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return dy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
koor_distance_wrap_xy(int x1, int y1, int x2, int y2, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
int dx = x1 - x2;
|
|
|
|
|
int dy = y1 - y2;
|
|
|
|
|
int result, dist;
|
|
|
|
|
int mindist = MIN(width, height) >> 1;
|
|
|
|
|
|
|
|
|
|
/* Bei negativem dy am Ursprung spiegeln, das veraendert
|
|
|
|
|
* den Abstand nicht
|
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dy < 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dy = -dy;
|
|
|
|
|
dx = -dx;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dx < 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dx = width + dx;
|
|
|
|
|
}
|
|
|
|
|
/* dx,dy is now pointing northeast */
|
|
|
|
|
result = dx + dy;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (result <= mindist)
|
|
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
dist = (width - dx) + (height - dy); /* southwest */
|
|
|
|
|
if (dist >= 0 && dist < result) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
result = dist;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (result <= mindist)
|
|
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
dist = MAX(dx, height - dy);
|
|
|
|
|
if (dist >= 0 && dist < result) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
result = dist;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (result <= mindist)
|
|
|
|
|
return result;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
dist = MAX(width - dx, dy);
|
|
|
|
|
if (dist >= 0 && dist < result)
|
|
|
|
|
result = dist;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int koor_distance(int x1, int y1, int x2, int y2)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const plane *p1 = findplane(x1, y1);
|
|
|
|
|
const plane *p2 = findplane(x2, y2);
|
|
|
|
|
if (p1 != p2)
|
|
|
|
|
return INT_MAX;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
else {
|
|
|
|
|
int width = plane_width(p1);
|
|
|
|
|
int height = plane_height(p1);
|
|
|
|
|
if (width && height) {
|
|
|
|
|
return koor_distance_wrap_xy(x1, y1, x2, y2, width, height);
|
|
|
|
|
} else {
|
|
|
|
|
return koor_distance_orig(x1, y1, x2, y2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int distance(const region * r1, const region * r2)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
return koor_distance(r1->x, r1->y, r2->x, r2->y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static direction_t
|
2011-03-07 08:02:35 +01:00
|
|
|
|
koor_reldirection(int ax, int ay, int bx, int by, const struct plane *pl)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
direction_t dir;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (dir = 0; dir != MAXDIRECTIONS; ++dir) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int x = ax + delta_x[dir];
|
|
|
|
|
int y = ay + delta_y[dir];
|
|
|
|
|
pnormalize(&x, &y, pl);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (bx == x && by == y)
|
|
|
|
|
return dir;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return NODIRECTION;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
spec_direction *special_direction(const region * from, const region * to)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
const attrib *a = a_findc(from->attribs, &at_direction);
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
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;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
a = a->next;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
direction_t reldirection(const region * from, const region * to)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
plane *pl = rplane(from);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (pl == rplane(to)) {
|
|
|
|
|
direction_t dir = koor_reldirection(from->x, from->y, to->x, to->y, pl);
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (dir == NODIRECTION) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
spec_direction *sd = special_direction(from, to);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (sd != NULL && sd->active)
|
|
|
|
|
return D_SPECIAL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
return NODIRECTION;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_regionlist(region_list * rl)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
while (rl) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region_list *rl2 = rl->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free(rl);
|
|
|
|
|
rl = rl2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void add_regionlist(region_list ** rl, region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region_list *rl2 = (region_list *) malloc(sizeof(region_list));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
rl2->data = r;
|
|
|
|
|
rl2->next = *rl;
|
|
|
|
|
|
|
|
|
|
*rl = rl2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
|
/* at_horseluck */
|
|
|
|
|
/********************/
|
|
|
|
|
attrib_type at_horseluck = {
|
|
|
|
|
"horseluck",
|
|
|
|
|
DEFAULT_INIT,
|
|
|
|
|
DEFAULT_FINALIZE,
|
|
|
|
|
DEFAULT_AGE,
|
|
|
|
|
NO_WRITE,
|
|
|
|
|
NO_READ,
|
|
|
|
|
ATF_UNIQUE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**********************/
|
|
|
|
|
/* at_peasantluck */
|
|
|
|
|
/**********************/
|
|
|
|
|
attrib_type at_peasantluck = {
|
|
|
|
|
"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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*********************/
|
|
|
|
|
/* at_deathcount */
|
|
|
|
|
/*********************/
|
|
|
|
|
attrib_type at_deathcount = {
|
|
|
|
|
"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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*********************/
|
|
|
|
|
/* at_travelunit */
|
|
|
|
|
/*********************/
|
|
|
|
|
attrib_type at_travelunit = {
|
|
|
|
|
"travelunit",
|
|
|
|
|
DEFAULT_INIT,
|
|
|
|
|
DEFAULT_FINALIZE,
|
|
|
|
|
DEFAULT_AGE,
|
|
|
|
|
NO_WRITE,
|
|
|
|
|
NO_READ
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void rsetroad(region * r, direction_t d, short val)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
connection *b;
|
|
|
|
|
region *r2 = rconnect(r, d);
|
|
|
|
|
|
2011-06-04 18:37:02 +02:00
|
|
|
|
if (!r2) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return;
|
2011-06-04 18:37:02 +02:00
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
b = get_borders(r, r2);
|
2011-06-04 18:37:02 +02:00
|
|
|
|
while (b && b->type != &bt_road) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
b = b->next;
|
2011-06-04 18:37:02 +02:00
|
|
|
|
}
|
|
|
|
|
if (!b) {
|
|
|
|
|
if (!val) return;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
b = new_border(&bt_road, r, r2);
|
2011-06-04 18:37:02 +02:00
|
|
|
|
}
|
|
|
|
|
if (r == b->from) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
b->data.sa[0] = val;
|
2011-06-04 18:37:02 +02:00
|
|
|
|
} else {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
b->data.sa[1] = val;
|
2011-06-04 18:37:02 +02:00
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
short rroad(const region * r, direction_t d)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int rval;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
connection *b;
|
|
|
|
|
region *r2 = rconnect(r, d);
|
|
|
|
|
|
|
|
|
|
if (!r2)
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
b = get_borders(r, r2);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (b && b->type != &bt_road)
|
|
|
|
|
b = b->next;
|
|
|
|
|
if (!b)
|
|
|
|
|
return 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rval = b->data.i;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r == b->from)
|
|
|
|
|
return b->data.sa[0];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return b->data.sa[1];
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
boolean r_isforest(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
if (fval(r->terrain, FOREST_REGION)) {
|
|
|
|
|
/* needs to be covered with at leas 48% trees */
|
|
|
|
|
int mincover = (int)(r->terrain->size * 0.48);
|
|
|
|
|
int trees = rtrees(r, 2) + rtrees(r, 1);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return (trees * TREESIZE >= mincover);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int is_coastregion(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
direction_t i;
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (i = 0; i < MAXDIRECTIONS; i++) {
|
|
|
|
|
region *rn = rconnect(r, i);
|
|
|
|
|
if (rn && fval(rn->terrain, SEA_REGION))
|
|
|
|
|
res++;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int rpeasants(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return ((r)->land ? (r)->land->peasants : 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void rsetpeasants(region * r, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
((r)->land ? ((r)->land->peasants =
|
|
|
|
|
(value)) : (assert((value) >= 0), (value)), 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int rmoney(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return ((r)->land ? (r)->land->money : 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void rsethorses(const region * r, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
assert(value >= 0);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r->land)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->land->horses = value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int rhorses(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return r->land ? r->land->horses : 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void rsetmoney(region * r, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
((r)->land ? ((r)->land->money =
|
|
|
|
|
(value)) : (assert((value) >= 0), (value)), 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void r_setdemand(region * r, const luxury_type * ltype, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct demand *d, **dp = &r->land->demands;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (ltype == NULL)
|
|
|
|
|
return;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (*dp && (*dp)->type != ltype)
|
|
|
|
|
dp = &(*dp)->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
d = *dp;
|
|
|
|
|
if (!d) {
|
|
|
|
|
d = *dp = malloc(sizeof(struct demand));
|
|
|
|
|
d->next = NULL;
|
|
|
|
|
d->type = ltype;
|
|
|
|
|
}
|
|
|
|
|
d->value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const item_type *r_luxury(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct demand *dmd;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (r->land) {
|
|
|
|
|
if (!r->land->demands) {
|
|
|
|
|
fix_demand(r);
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (dmd = r->land->demands; dmd; dmd = dmd->next) {
|
|
|
|
|
if (dmd->value == 0)
|
|
|
|
|
return dmd->type->itype;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int r_demand(const region * r, const luxury_type * ltype)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct demand *d = r->land->demands;
|
|
|
|
|
while (d && d->type != ltype)
|
|
|
|
|
d = d->next;
|
|
|
|
|
if (!d)
|
|
|
|
|
return -1;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return d->value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *rname(const region * r, const struct locale *lang)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2010-09-12 04:37:11 +02:00
|
|
|
|
if (r->land && r->land->name) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return r->land->name;
|
|
|
|
|
}
|
|
|
|
|
return LOC(lang, terrain_name(r));
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int rtrees(const region * r, int ageclass)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return ((r)->land ? (r)->land->trees[ageclass] : 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int rsettrees(const region * r, int ageclass, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (!r->land)
|
|
|
|
|
assert(value == 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
else {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(value >= 0);
|
|
|
|
|
return r->land->trees[ageclass] = value;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static region *last;
|
|
|
|
|
|
|
|
|
|
static unsigned int max_index = 0;
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *new_region(int x, int y, struct plane *pl, unsigned int uid)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
region *r;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
|
pnormalize(&x, &y, pl);
|
|
|
|
|
r = rfindhash(x, y);
|
|
|
|
|
|
|
|
|
|
if (r) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
log_error(("duplicate region discovered: %s(%d,%d)\n", regionname(r, NULL),
|
|
|
|
|
x, y));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (r->units)
|
|
|
|
|
log_error(("duplicate region contains units\n"));
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
r = calloc(1, sizeof(region));
|
|
|
|
|
r->x = x;
|
|
|
|
|
r->y = y;
|
|
|
|
|
r->uid = uid;
|
|
|
|
|
r->age = 1;
|
|
|
|
|
r->_plane = pl;
|
|
|
|
|
rhash(r);
|
|
|
|
|
hash_uid(r);
|
|
|
|
|
if (last)
|
|
|
|
|
addlist(&last, r);
|
|
|
|
|
else
|
|
|
|
|
addlist(®ions, r);
|
|
|
|
|
last = r;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(r->next == NULL);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->index = ++max_index;
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static region *deleted_regions;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void remove_region(region ** rlist, region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
while (r->units) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unit *u = r->units;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
i_freeall(&u->items);
|
|
|
|
|
remove_unit(&r->units, u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runhash(r);
|
|
|
|
|
unhash_uid(r);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (*rlist && *rlist != r)
|
|
|
|
|
rlist = &(*rlist)->next;
|
|
|
|
|
assert(*rlist == r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
*rlist = r->next;
|
|
|
|
|
r->next = deleted_regions;
|
|
|
|
|
deleted_regions = r;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static void freeland(land_region * lr)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
while (lr->demands) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct demand *d = lr->demands;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
lr->demands = d->next;
|
|
|
|
|
free(d);
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (lr->name)
|
|
|
|
|
free(lr->name);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free(lr);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void region_setresource(region * r, const resource_type * rtype, int value)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rawmaterial *rm = r->resources;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
while (rm) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (rm->type->rtype == rtype) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rm->amount = value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rm = rm->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
if (!rm) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (rtype == rt_find("money"))
|
|
|
|
|
rsetmoney(r, value);
|
|
|
|
|
else if (rtype == rt_find("peasant"))
|
|
|
|
|
rsetpeasants(r, value);
|
|
|
|
|
else if (rtype == rt_find("horse"))
|
|
|
|
|
rsethorses(r, value);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int region_getresource(const region * r, const resource_type * rtype)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const rawmaterial *rm;
|
|
|
|
|
for (rm = r->resources; rm; rm = rm->next) {
|
|
|
|
|
if (rm->type->rtype == rtype) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return rm->amount;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (rtype == rt_find("money"))
|
|
|
|
|
return rmoney(r);
|
|
|
|
|
if (rtype == rt_find("horse"))
|
|
|
|
|
return rhorses(r);
|
|
|
|
|
if (rtype == rt_find("peasant"))
|
|
|
|
|
return rpeasants(r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_region(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (last == r)
|
|
|
|
|
last = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free(r->display);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r->land)
|
|
|
|
|
freeland(r->land);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
if (r->msgs) {
|
|
|
|
|
free_messagelist(r->msgs);
|
|
|
|
|
r->msgs = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (r->individual_messages) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct individual_message *msg = r->individual_messages;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->individual_messages = msg->next;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (msg->msgs)
|
|
|
|
|
free_messagelist(msg->msgs);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (r->attribs)
|
|
|
|
|
a_remove(&r->attribs, r->attribs);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
while (r->resources) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rawmaterial *res = r->resources;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->resources = res->next;
|
|
|
|
|
free(res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (r->donations) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
donation *don = r->donations;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->donations = don->next;
|
|
|
|
|
free(don);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (r->units) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unit *u = r->units;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->units = u->next;
|
|
|
|
|
uunhash(u);
|
|
|
|
|
free_unit(u);
|
|
|
|
|
free(u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (r->buildings) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
building *b = r->buildings;
|
|
|
|
|
assert(b->region == r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->buildings = b->next;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
bunhash(b); /* must be done here, because remove_building does it, and wasn't called */
|
2010-08-08 10:06:34 +02:00
|
|
|
|
free_building(b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (r->ships) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
ship *s = r->ships;
|
|
|
|
|
assert(s->region == r);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->ships = s->next;
|
|
|
|
|
sunhash(s);
|
|
|
|
|
free_ship(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(r);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void free_regions(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
memset(uidhash, 0, sizeof(uidhash));
|
|
|
|
|
while (deleted_regions) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r = deleted_regions;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
deleted_regions = r->next;
|
|
|
|
|
free_region(r);
|
|
|
|
|
}
|
|
|
|
|
while (regions) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r = regions;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
regions = r->next;
|
|
|
|
|
runhash(r);
|
|
|
|
|
free_region(r);
|
|
|
|
|
}
|
|
|
|
|
max_index = 0;
|
|
|
|
|
last = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** creates a name for a region
|
|
|
|
|
* TODO: Make vowels XML-configurable and allow non-ascii characters again.
|
|
|
|
|
* - that will probably require a wchar_t * string to pick from.
|
|
|
|
|
*/
|
2011-03-07 08:02:35 +01:00
|
|
|
|
static char *makename(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
int s, v, k, e, p = 0, x = 0;
|
|
|
|
|
size_t nk, ne, nv, ns;
|
|
|
|
|
static char name[16];
|
|
|
|
|
const char *kons = "bcdfghklmnprstvwz",
|
|
|
|
|
*start = "bcdgtskpvfr",
|
|
|
|
|
*end = "nlrdst",
|
|
|
|
|
*vowels = "aaaaaaaaaaaeeeeeeeeeeeeiiiiiiiiiiioooooooooooouuuuuuuuuuyy";
|
|
|
|
|
|
|
|
|
|
/* const char * vowels_latin1 = "aaaaaaaaa<61><61>eeeeeeeee<65><65><EFBFBD>iiiiiiiii<69><69>ooooooooo<6F><6F><EFBFBD>uuuuuuuuu<75>yy"; */
|
|
|
|
|
|
|
|
|
|
nk = strlen(kons);
|
|
|
|
|
ne = strlen(end);
|
|
|
|
|
nv = strlen(vowels);
|
|
|
|
|
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] = vowels[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';
|
2011-03-07 08:02:35 +01:00
|
|
|
|
name[0] = (char)toupper(name[0]);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void setluxuries(region * r, const luxury_type * sale)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const luxury_type *ltype;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
assert(r->land);
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r->land->demands)
|
|
|
|
|
freelist(r->land->demands);
|
|
|
|
|
|
|
|
|
|
for (ltype = luxurytypes; ltype; ltype = ltype->next) {
|
|
|
|
|
struct demand *dmd = malloc(sizeof(struct demand));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dmd->type = ltype;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (ltype != sale)
|
|
|
|
|
dmd->value = 1 + rng_int() % 5;
|
|
|
|
|
else
|
|
|
|
|
dmd->value = 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
dmd->next = r->land->demands;
|
|
|
|
|
r->land->demands = dmd;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void terraform_region(region * r, const terrain_type * terrain)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
/* Resourcen, die nicht mehr vorkommen k<>nnen, l<>schen */
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const terrain_type *oldterrain = r->terrain;
|
|
|
|
|
rawmaterial **lrm = &r->resources;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
assert(terrain);
|
|
|
|
|
|
|
|
|
|
while (*lrm) {
|
|
|
|
|
rawmaterial *rm = *lrm;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const resource_type *rtype = NULL;
|
|
|
|
|
|
|
|
|
|
if (terrain->production != NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int i;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (i = 0; terrain->production[i].type; ++i) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (rm->type->rtype == terrain->production[i].type) {
|
|
|
|
|
rtype = rm->type->rtype;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (rtype == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
*lrm = rm->next;
|
|
|
|
|
free(rm);
|
|
|
|
|
} else {
|
|
|
|
|
lrm = &rm->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r->terrain = terrain;
|
|
|
|
|
terraform_resources(r);
|
|
|
|
|
|
|
|
|
|
if (!fval(terrain, LAND_REGION)) {
|
|
|
|
|
region_setinfo(r, NULL);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r->land != NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r->land) {
|
2011-06-04 18:37:02 +02:00
|
|
|
|
int d;
|
|
|
|
|
for (d=0;d!=MAXDIRECTIONS;++d) {
|
|
|
|
|
rsetroad(r, d, 0);
|
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
i_freeall(&r->land->items);
|
|
|
|
|
} else {
|
|
|
|
|
static struct surround {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct surround *next;
|
|
|
|
|
const luxury_type *type;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
int value;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} *trash = NULL, *nb = NULL;
|
|
|
|
|
const luxury_type *ltype = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
direction_t d;
|
|
|
|
|
int mnr = 0;
|
|
|
|
|
|
|
|
|
|
r->land = calloc(1, sizeof(land_region));
|
|
|
|
|
r->land->ownership = NULL;
|
|
|
|
|
region_set_morale(r, MORALE_DEFAULT, -1);
|
|
|
|
|
region_setname(r, makename());
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (d = 0; d != MAXDIRECTIONS; ++d) {
|
|
|
|
|
region *nr = rconnect(r, d);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (nr && nr->land) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct demand *sale = r->land->demands;
|
|
|
|
|
while (sale && sale->value != 0)
|
|
|
|
|
sale = sale->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (sale) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct surround *sr = nb;
|
|
|
|
|
while (sr && sr->type != sale->type)
|
|
|
|
|
sr = sr->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
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;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} else
|
|
|
|
|
sr->value++;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
++mnr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!nb) {
|
|
|
|
|
int i = get_maxluxuries();
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (i > 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
i = rng_int() % i;
|
|
|
|
|
ltype = luxurytypes;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (i--)
|
|
|
|
|
ltype = ltype->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int i = rng_int() % mnr;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct surround *srd = nb;
|
|
|
|
|
while (i > srd->value) {
|
|
|
|
|
i -= srd->value;
|
|
|
|
|
srd = srd->next;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (srd->type)
|
|
|
|
|
setluxuries(r, srd->type);
|
|
|
|
|
while (srd->next != NULL)
|
|
|
|
|
srd = srd->next;
|
|
|
|
|
srd->next = trash;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
trash = nb;
|
|
|
|
|
nb = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fval(terrain, LAND_REGION)) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const item_type *itype = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
char equip_hash[64];
|
|
|
|
|
|
|
|
|
|
/* TODO: put the equipment in struct terrain, faster */
|
|
|
|
|
sprintf(equip_hash, "terrain_%s", terrain->_name);
|
|
|
|
|
equip_items(&r->land->items, get_equipment(equip_hash));
|
|
|
|
|
|
|
|
|
|
if (r->terrain->herbs) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int len = 0;
|
|
|
|
|
while (r->terrain->herbs[len])
|
|
|
|
|
++len;
|
|
|
|
|
if (len)
|
|
|
|
|
itype = r->terrain->herbs[rng_int() % len];
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (itype != NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rsetherbtype(r, itype);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rsetherbs(r, (short)(50 + rng_int() % 31));
|
|
|
|
|
} else {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rsetherbtype(r, NULL);
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (oldterrain == NULL || !fval(oldterrain, LAND_REGION)) {
|
|
|
|
|
if (rng_int() % 100 < 3)
|
|
|
|
|
fset(r, RF_MALLORN);
|
|
|
|
|
else
|
|
|
|
|
freset(r, RF_MALLORN);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (rng_int() % 100 < ENCCHANCE) {
|
|
|
|
|
fset(r, RF_ENCOUNTER);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (oldterrain == NULL || terrain->size != oldterrain->size) {
|
|
|
|
|
if (terrain == newterrain(T_PLAIN)) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rsethorses(r, rng_int() % (terrain->size / 50));
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (rng_int() % 100 < 40) {
|
|
|
|
|
rsettrees(r, 2, terrain->size * (30 + rng_int() % 40) / 1000);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
} else if (chance(0.2)) {
|
|
|
|
|
rsettrees(r, 2, terrain->size * (30 + rng_int() % 40) / 1000);
|
|
|
|
|
} else {
|
|
|
|
|
rsettrees(r, 2, 0);
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rsettrees(r, 1, rtrees(r, 2) / 4);
|
|
|
|
|
rsettrees(r, 0, rtrees(r, 2) / 8);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
if (!fval(r, RF_CHAOTIC)) {
|
|
|
|
|
int peasants;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
peasants = (maxworkingpeasants(r) * (20 + dice_rand("6d10"))) / 100;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
rsetpeasants(r, MAX(100, peasants));
|
2011-03-07 08:02:35 +01:00
|
|
|
|
rsetmoney(r, rpeasants(r) * ((wage(r, NULL, NULL,
|
|
|
|
|
INT_MAX) + 1) + rng_int() % 5));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** ENNO:
|
|
|
|
|
* ich denke, das das hier nicht sein sollte.
|
|
|
|
|
* statt dessen sollte ein attribut an der region sein, das das erledigt,
|
|
|
|
|
* egal ob durch den spell oder anderes angelegt.
|
|
|
|
|
**/
|
|
|
|
|
#include "curse.h"
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int production(const region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
/* mu<6D> rterrain(r) sein, nicht rterrain() wegen rekursion */
|
|
|
|
|
int p = r->terrain->size / MAXPEASANTS_PER_AREA;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (curse_active(get_curse(r->attribs, ct_find("drought"))))
|
|
|
|
|
p /= 2;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int resolve_region_coor(variant id, void *address)
|
|
|
|
|
{
|
|
|
|
|
region *r = findregion(id.sa[0], id.sa[1]);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (r) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
*(region **) address = r;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
*(region **) address = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
int resolve_region_id(variant id, void *address)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region *r = NULL;
|
|
|
|
|
if (id.i != 0) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r = findregionbyid((unsigned int)id.i);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (r == NULL) {
|
|
|
|
|
*(region **) address = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
*(region **) address = r;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
variant read_region_reference(struct storage * store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
variant result;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (store->version < UIDHASH_VERSION) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
result.sa[0] = (short)store->r_int(store);
|
|
|
|
|
result.sa[1] = (short)store->r_int(store);
|
|
|
|
|
} else {
|
|
|
|
|
result.i = store->r_int(store);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void write_region_reference(const region * r, struct storage *store)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
if (r) {
|
|
|
|
|
store->w_int(store, r->uid);
|
|
|
|
|
} else {
|
|
|
|
|
store->w_int(store, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct message_list *r_getmessages(const struct region *r,
|
|
|
|
|
const struct faction *viewer)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct individual_message *imsg = r->individual_messages;
|
|
|
|
|
while (imsg && (imsg)->viewer != viewer)
|
|
|
|
|
imsg = imsg->next;
|
|
|
|
|
if (imsg)
|
|
|
|
|
return imsg->msgs;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct message *r_addmessage(struct region *r, const struct faction *viewer,
|
|
|
|
|
struct message *msg)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
assert(r);
|
|
|
|
|
if (viewer) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct individual_message *imsg;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
imsg = r->individual_messages;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
while (imsg && imsg->viewer != viewer)
|
|
|
|
|
imsg = imsg->next;
|
|
|
|
|
if (imsg == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
return add_message(&r->msgs, msg);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct faction *region_get_owner(const struct region *r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
assert(rule_region_owners());
|
|
|
|
|
if (r->land && r->land->ownership) {
|
|
|
|
|
return r->land->ownership->owner;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
struct alliance *region_get_alliance(const struct region *r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
assert(rule_region_owners());
|
|
|
|
|
if (r->land && r->land->ownership) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
region_owner *own = r->land->ownership;
|
|
|
|
|
return own->owner ? own->owner->alliance : own->alliance;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void region_set_owner(struct region *r, struct faction *owner, int turn)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
assert(rule_region_owners());
|
|
|
|
|
if (r->land) {
|
|
|
|
|
if (!r->land->ownership) {
|
|
|
|
|
r->land->ownership = malloc(sizeof(region_owner));
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(region_get_morale(r) == MORALE_DEFAULT);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->land->ownership->owner = NULL;
|
|
|
|
|
r->land->ownership->alliance = NULL;
|
|
|
|
|
r->land->ownership->flags = 0;
|
|
|
|
|
}
|
|
|
|
|
r->land->ownership->since_turn = turn;
|
|
|
|
|
r->land->ownership->morale_turn = turn;
|
|
|
|
|
assert(r->land->ownership->owner != owner);
|
|
|
|
|
r->land->ownership->owner = owner;
|
|
|
|
|
if (owner) {
|
|
|
|
|
r->land->ownership->alliance = owner->alliance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
faction *update_owners(region * r)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
2011-03-07 08:02:35 +01:00
|
|
|
|
faction *f = NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
assert(rule_region_owners());
|
|
|
|
|
if (r->land) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
building *bowner = largestbuilding(r, &cmp_current_owner, false);
|
|
|
|
|
building *blargest = largestbuilding(r, &cmp_taxes, false);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (blargest) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (!bowner || bowner->size < blargest->size) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
/* region owners update? */
|
2011-03-07 08:02:35 +01:00
|
|
|
|
unit *u = building_owner(blargest);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
f = region_get_owner(r);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (u == NULL) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (f) {
|
|
|
|
|
region_set_owner(r, NULL, turn);
|
|
|
|
|
r->land->ownership->flags |= OWNER_MOURNING;
|
|
|
|
|
f = NULL;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
} else if (u->faction != f) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (!r->land->ownership) {
|
|
|
|
|
/* there has never been a prior owner */
|
|
|
|
|
region_set_morale(r, MORALE_DEFAULT, turn);
|
|
|
|
|
} else {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
alliance *al = region_get_alliance(r);
|
|
|
|
|
if (al && u->faction->alliance == al) {
|
|
|
|
|
int morale = MAX(0, r->land->morale - MORALE_TRANSFER);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
region_set_morale(r, morale, turn);
|
|
|
|
|
} else {
|
|
|
|
|
region_set_morale(r, MORALE_TAKEOVER, turn);
|
|
|
|
|
if (f) {
|
|
|
|
|
r->land->ownership->flags |= OWNER_MOURNING;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
region_set_owner(r, u->faction, turn);
|
|
|
|
|
f = u->faction;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (r->land->ownership && r->land->ownership->owner) {
|
|
|
|
|
r->land->ownership->flags |= OWNER_MOURNING;
|
|
|
|
|
region_set_owner(r, NULL, turn);
|
|
|
|
|
f = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void region_setinfo(struct region *r, const char *info)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
free(r->display);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
r->display = info ? strdup(info) : 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *region_getinfo(const region * r)
|
|
|
|
|
{
|
|
|
|
|
return r->display ? r->display : "";
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
void region_setname(struct region *r, const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
|
{
|
|
|
|
|
if (r->land) {
|
|
|
|
|
free(r->land->name);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
r->land->name = name ? strdup(name) : 0;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
|
const char *region_getname(const region * r)
|
|
|
|
|
{
|
2010-08-08 10:06:34 +02:00
|
|
|
|
if (r->land && r->land->name) {
|
|
|
|
|
return r->land->name;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int region_get_morale(const region * r)
|
|
|
|
|
{
|
|
|
|
|
if (r->land) {
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(r->land->morale >= 0 && r->land->morale <= MORALE_MAX);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
return r->land->morale;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void region_set_morale(region * r, int morale, int turn)
|
|
|
|
|
{
|
|
|
|
|
if (r->land) {
|
|
|
|
|
r->land->morale = (short)morale;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
if (turn >= 0 && r->land->ownership) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
r->land->ownership->morale_turn = turn;
|
|
|
|
|
}
|
2011-03-07 08:02:35 +01:00
|
|
|
|
assert(r->land->morale >= 0 && r->land->morale <= MORALE_MAX);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void get_neighbours(const region * r, region ** list)
|
|
|
|
|
{
|
|
|
|
|
direction_t dir;
|
2011-03-07 08:02:35 +01:00
|
|
|
|
for (dir = 0; dir != MAXDIRECTIONS; ++dir) {
|
2010-08-08 10:06:34 +02:00
|
|
|
|
list[dir] = rconnect(r, dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int owner_change(const region * r)
|
|
|
|
|
{
|
|
|
|
|
if (r->land && r->land->ownership) {
|
|
|
|
|
return r->land->ownership->since_turn;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean is_mourning(const region * r, int in_turn)
|
|
|
|
|
{
|
|
|
|
|
int change = owner_change(r);
|
2011-03-07 08:02:35 +01:00
|
|
|
|
return (change == in_turn - 1
|
|
|
|
|
&& (r->land->ownership->flags & OWNER_MOURNING));
|
2010-08-08 10:06:34 +02:00
|
|
|
|
}
|