server/src/kernel/connection.c

684 lines
17 KiB
C
Raw Normal View History

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 "connection.h"
#include "region.h"
#include "save.h"
#include "terrain.h"
#include "unit.h"
#include "version.h"
#include <util/attrib.h>
#include <util/language.h>
#include <util/log.h>
#include <quicklist.h>
2010-08-08 10:06:34 +02:00
#include <util/rng.h>
#include <util/storage.h>
/* libc includes */
#include <assert.h>
#include <limits.h>
#include <string.h>
unsigned int nextborder = 0;
#define BORDER_MAXHASH 8191
2011-03-07 08:02:35 +01:00
connection *borders[BORDER_MAXHASH];
border_type *bordertypes;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
void (*border_convert_cb) (struct connection * con, struct attrib * attr) = 0;
void free_borders(void)
2010-08-08 10:06:34 +02:00
{
int i;
2011-03-07 08:02:35 +01:00
for (i = 0; i != BORDER_MAXHASH; ++i) {
2010-08-08 10:06:34 +02:00
while (borders[i]) {
2011-03-07 08:02:35 +01:00
connection *b = borders[i];
2010-08-08 10:06:34 +02:00
borders[i] = b->nexthash;
while (b) {
2011-03-07 08:02:35 +01:00
connection *bf = b;
2010-08-08 10:06:34 +02:00
b = b->next;
2011-03-07 08:02:35 +01:00
assert(b == NULL || b->nexthash == NULL);
2010-08-08 10:06:34 +02:00
if (bf->type->destroy) {
bf->type->destroy(bf);
}
free(bf);
}
}
}
}
2011-03-07 08:02:35 +01:00
connection *find_border(unsigned int id)
2010-08-08 10:06:34 +02:00
{
int key;
2011-03-07 08:02:35 +01:00
for (key = 0; key != BORDER_MAXHASH; key++) {
connection *bhash;
for (bhash = borders[key]; bhash != NULL; bhash = bhash->nexthash) {
connection *b;
for (b = bhash; b; b = b->next) {
if (b->id == id)
return b;
2010-08-08 10:06:34 +02:00
}
}
}
return NULL;
}
2011-03-07 08:02:35 +01:00
int resolve_borderid(variant id, void *addr)
2010-08-08 10:06:34 +02:00
{
int result = 0;
2011-03-07 08:02:35 +01:00
connection *b = NULL;
if (id.i != 0) {
2010-08-08 10:06:34 +02:00
b = find_border(id.i);
2011-03-07 08:02:35 +01:00
if (b == NULL) {
2010-08-08 10:06:34 +02:00
result = -1;
}
}
2011-03-07 08:02:35 +01:00
*(connection **) addr = b;
2010-08-08 10:06:34 +02:00
return result;
}
2011-03-07 08:02:35 +01:00
static connection **get_borders_i(const region * r1, const region * r2)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
connection **bp;
2010-08-08 10:06:34 +02:00
int key = reg_hashkey(r1);
int k2 = reg_hashkey(r2);
key = MIN(k2, key) % BORDER_MAXHASH;
bp = &borders[key];
while (*bp) {
2011-03-07 08:02:35 +01:00
connection *b = *bp;
if ((b->from == r1 && b->to == r2) || (b->from == r2 && b->to == r1))
break;
2010-08-08 10:06:34 +02:00
bp = &b->nexthash;
}
return bp;
}
2011-03-07 08:02:35 +01:00
connection *get_borders(const region * r1, const region * r2)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
connection **bp = get_borders_i(r1, r2);
2010-08-08 10:06:34 +02:00
return *bp;
}
2011-03-07 08:02:35 +01:00
connection *new_border(border_type * type, region * from, region * to)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
connection *b = calloc(1, sizeof(struct connection));
2010-08-08 10:06:34 +02:00
if (from && to) {
2011-03-07 08:02:35 +01:00
connection **bp = get_borders_i(from, to);
while (*bp)
bp = &(*bp)->next;
2010-08-08 10:06:34 +02:00
*bp = b;
}
b->type = type;
b->from = from;
b->to = to;
b->id = ++nextborder;
2011-03-07 08:02:35 +01:00
if (type->init)
type->init(b);
2010-08-08 10:06:34 +02:00
return b;
}
2011-03-07 08:02:35 +01:00
void erase_border(connection * b)
2010-08-08 10:06:34 +02:00
{
if (b->from && b->to) {
2011-03-07 08:02:35 +01:00
connection **bp = get_borders_i(b->from, b->to);
assert(*bp != NULL || !"error: connection is not registered");
if (*bp == b) {
2010-08-08 10:06:34 +02:00
/* it is the first in the list, so it is in the nexthash list */
if (b->next) {
*bp = b->next;
(*bp)->nexthash = b->nexthash;
} else {
*bp = b->nexthash;
}
} else {
while (*bp && *bp != b) {
bp = &(*bp)->next;
}
2011-03-07 08:02:35 +01:00
assert(*bp == b || !"error: connection is not registered");
2010-08-08 10:06:34 +02:00
*bp = b->next;
}
}
if (b->type->destroy) {
b->type->destroy(b);
}
free(b);
}
2011-03-07 08:02:35 +01:00
void register_bordertype(border_type * type)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
border_type **btp = &bordertypes;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (*btp && *btp != type)
btp = &(*btp)->next;
if (*btp)
return;
2010-08-08 10:06:34 +02:00
*btp = type;
}
2011-03-07 08:02:35 +01:00
border_type *find_bordertype(const char *name)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
border_type *bt = bordertypes;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
while (bt && strcmp(bt->__name, name))
bt = bt->next;
2010-08-08 10:06:34 +02:00
return bt;
}
2011-03-07 08:02:35 +01:00
void b_read(connection * b, storage * store)
2010-08-08 10:06:34 +02:00
{
int result = 0;
switch (b->type->datatype) {
case VAR_NONE:
case VAR_INT:
b->data.i = store->r_int(store);
break;
case VAR_SHORTA:
b->data.sa[0] = (short)store->r_int(store);
b->data.sa[1] = (short)store->r_int(store);
break;
case VAR_VOIDPTR:
default:
assert(!"invalid variant type in connection");
result = 0;
}
2011-03-07 08:02:35 +01:00
assert(result >= 0 || "EOF encountered?");
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
void b_write(const connection * b, storage * store)
2010-08-08 10:06:34 +02:00
{
switch (b->type->datatype) {
case VAR_NONE:
case VAR_INT:
store->w_int(store, b->data.i);
break;
case VAR_SHORTA:
store->w_int(store, b->data.sa[0]);
store->w_int(store, b->data.sa[1]);
break;
case VAR_VOIDPTR:
default:
assert(!"invalid variant type in connection");
}
}
2011-03-07 08:02:35 +01:00
boolean b_transparent(const connection * b, const struct faction *f)
{
unused(b);
unused(f);
return true;
}
boolean b_opaque(const connection * b, const struct faction * f)
{
unused(b);
unused(f);
return false;
}
boolean b_blockall(const connection * b, const unit * u, const region * r)
{
unused(u);
unused(r);
unused(b);
return true;
}
boolean b_blocknone(const connection * b, const unit * u, const region * r)
{
unused(u);
unused(r);
unused(b);
return false;
}
boolean b_rvisible(const connection * b, const region * r)
{
return (boolean) (b->to == r || b->from == r);
}
boolean b_fvisible(const connection * b, const struct faction * f,
const region * r)
{
unused(r);
unused(f);
unused(b);
return true;
}
boolean b_uvisible(const connection * b, const unit * u)
{
unused(u);
unused(b);
return true;
}
boolean b_rinvisible(const connection * b, const region * r)
{
unused(r);
unused(b);
return false;
}
boolean b_finvisible(const connection * b, const struct faction * f,
const region * r)
{
unused(r);
unused(f);
unused(b);
return false;
}
boolean b_uinvisible(const connection * b, const unit * u)
{
unused(u);
unused(b);
return false;
}
2010-08-08 10:06:34 +02:00
/**************************************/
/* at_countdown - legacy, do not use */
/**************************************/
attrib_type at_countdown = {
"countdown",
DEFAULT_INIT,
DEFAULT_FINALIZE,
NULL,
NULL,
a_readint
};
2011-03-07 08:02:35 +01:00
void age_borders(void)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
quicklist *deleted = NULL, *ql;
2010-08-08 10:06:34 +02:00
int i;
2011-03-07 08:02:35 +01:00
for (i = 0; i != BORDER_MAXHASH; ++i) {
connection *bhash = borders[i];
for (; bhash; bhash = bhash->nexthash) {
connection *b = bhash;
for (; b; b = b->next) {
2010-08-08 10:06:34 +02:00
if (b->type->age) {
2011-03-07 08:02:35 +01:00
if (b->type->age(b) == AT_AGE_REMOVE) {
ql_push(&deleted, b);
2010-08-08 10:06:34 +02:00
}
}
}
}
}
2011-03-07 08:02:35 +01:00
for (ql = deleted, i = 0; ql; ql_advance(&ql, &i, 1)) {
connection *b = (connection *) ql_get(ql, i);
2010-08-08 10:06:34 +02:00
erase_border(b);
}
ql_free(deleted);
2010-08-08 10:06:34 +02:00
}
/********
* implementation of a couple of borders. this shouldn't really be in here, so
* let's keep it separate from the more general stuff above
********/
#include "faction.h"
2011-03-07 08:02:35 +01:00
static const char *b_namewall(const connection * b, const region * r,
const struct faction *f, int gflags)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
const char *bname = "wall";
2010-08-08 10:06:34 +02:00
unused(f);
unused(r);
unused(b);
2011-03-07 08:02:35 +01:00
if (gflags & GF_ARTICLE)
bname = "a_wall";
if (gflags & GF_PURE)
return bname;
2010-08-08 10:06:34 +02:00
return LOC(f->locale, mkname("border", bname));
}
border_type bt_wall = {
"wall", VAR_INT,
b_opaque,
2011-03-07 08:02:35 +01:00
NULL, /* init */
NULL, /* destroy */
b_read, /* read */
b_write, /* write */
b_blockall, /* block */
b_namewall, /* name */
b_rvisible, /* rvisible */
b_fvisible, /* fvisible */
b_uvisible, /* uvisible */
2010-08-08 10:06:34 +02:00
};
border_type bt_noway = {
"noway", VAR_INT,
b_transparent,
2011-03-07 08:02:35 +01:00
NULL, /* init */
NULL, /* destroy */
b_read, /* read */
b_write, /* write */
b_blockall, /* block */
NULL, /* name */
b_rinvisible, /* rvisible */
b_finvisible, /* fvisible */
b_uinvisible, /* uvisible */
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
static const char *b_namefogwall(const connection * b, const region * r,
const struct faction *f, int gflags)
2010-08-08 10:06:34 +02:00
{
unused(f);
unused(b);
unused(r);
2011-03-07 08:02:35 +01:00
if (gflags & GF_PURE)
return "fogwall";
if (gflags & GF_ARTICLE)
return LOC(f->locale, mkname("border", "a_fogwall"));
2010-08-08 10:06:34 +02:00
return LOC(f->locale, mkname("border", "fogwall"));
}
static boolean
b_blockfogwall(const connection * b, const unit * u, const region * r)
{
unused(b);
unused(r);
2011-03-07 08:02:35 +01:00
if (!u)
return true;
return (boolean) (effskill(u, SK_PERCEPTION) > 4); /* Das ist die alte Nebelwand */
2010-08-08 10:06:34 +02:00
}
/** Legacy type used in old Eressea games, no longer in use. */
border_type bt_fogwall = {
"fogwall", VAR_INT,
2011-03-07 08:02:35 +01:00
b_transparent, /* transparent */
NULL, /* init */
NULL, /* destroy */
b_read, /* read */
b_write, /* write */
b_blockfogwall, /* block */
b_namefogwall, /* name */
b_rvisible, /* rvisible */
b_fvisible, /* fvisible */
b_uvisible, /* uvisible */
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
static const char *b_nameillusionwall(const connection * b, const region * r,
const struct faction *f, int gflags)
2010-08-08 10:06:34 +02:00
{
int fno = b->data.i;
unused(b);
unused(r);
2011-03-07 08:02:35 +01:00
if (gflags & GF_PURE)
return (f && fno == f->no) ? "illusionwall" : "wall";
2010-08-08 10:06:34 +02:00
if (gflags & GF_ARTICLE) {
2011-03-07 08:02:35 +01:00
return LOC(f->locale, mkname("border", (f
&& fno == f->subscription) ? "an_illusionwall" : "a_wall"));
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
return LOC(f->locale, mkname("border", (f
&& fno == f->no) ? "illusionwall" : "wall"));
2010-08-08 10:06:34 +02:00
}
border_type bt_illusionwall = {
"illusionwall", VAR_INT,
b_opaque,
2011-03-07 08:02:35 +01:00
NULL, /* init */
NULL, /* destroy */
b_read, /* read */
b_write, /* write */
b_blocknone, /* block */
b_nameillusionwall, /* name */
b_rvisible, /* rvisible */
b_fvisible, /* fvisible */
b_uvisible, /* uvisible */
2010-08-08 10:06:34 +02:00
};
/***
* special quest door
***/
2011-03-07 08:02:35 +01:00
boolean b_blockquestportal(const connection * b, const unit * u,
const region * r)
{
if (b->data.i > 0)
return true;
2010-08-08 10:06:34 +02:00
return false;
}
2011-03-07 08:02:35 +01:00
static const char *b_namequestportal(const connection * b, const region * r,
const struct faction *f, int gflags)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
const char *bname;
2010-08-08 10:06:34 +02:00
int lock = b->data.i;
unused(b);
unused(r);
if (gflags & GF_ARTICLE) {
if (lock > 0) {
bname = "a_gate_locked";
} else {
bname = "a_gate_open";
}
} else {
if (lock > 0) {
bname = "gate_locked";
} else {
bname = "gate_open";
}
}
2011-03-07 08:02:35 +01:00
if (gflags & GF_PURE)
return bname;
2010-08-08 10:06:34 +02:00
return LOC(f->locale, mkname("border", bname));
}
border_type bt_questportal = {
"questportal", VAR_INT,
b_opaque,
2011-03-07 08:02:35 +01:00
NULL, /* init */
NULL, /* destroy */
b_read, /* read */
b_write, /* write */
b_blockquestportal, /* block */
b_namequestportal, /* name */
b_rvisible, /* rvisible */
b_fvisible, /* fvisible */
b_uvisible, /* uvisible */
2010-08-08 10:06:34 +02:00
};
/***
* roads. meant to replace the old at_road or r->road attribute
***/
2011-03-07 08:02:35 +01:00
static const char *b_nameroad(const connection * b, const region * r,
const struct faction *f, int gflags)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
region *r2 = (r == b->to) ? b->from : b->to;
int local = (r == b->from) ? b->data.sa[0] : b->data.sa[1];
2010-08-08 10:06:34 +02:00
static char buffer[64];
unused(f);
2011-03-07 08:02:35 +01:00
if (gflags & GF_PURE)
return "road";
2010-08-08 10:06:34 +02:00
if (gflags & GF_ARTICLE) {
2011-03-07 08:02:35 +01:00
if (!(gflags & GF_DETAILED))
return LOC(f->locale, mkname("border", "a_road"));
else if (r->terrain->max_road <= local) {
int remote = (r2 == b->from) ? b->data.sa[0] : b->data.sa[1];
if (r2->terrain->max_road <= remote) {
2010-08-08 10:06:34 +02:00
return LOC(f->locale, mkname("border", "a_road"));
} else {
return LOC(f->locale, mkname("border", "an_incomplete_road"));
}
} else {
2011-03-07 08:02:35 +01:00
int percent = MAX(1, 100 * local / r->terrain->max_road);
2010-08-08 10:06:34 +02:00
if (local) {
2012-05-29 21:17:25 +02:00
slprintf(buffer, sizeof(buffer), LOC(f->locale, mkname("border",
2011-03-07 08:02:35 +01:00
"a_road_percent")), percent);
2010-08-08 10:06:34 +02:00
} else {
return LOC(f->locale, mkname("border", "a_road_connection"));
}
}
2011-03-07 08:02:35 +01:00
} else if (gflags & GF_PLURAL)
return LOC(f->locale, mkname("border", "roads"));
else
return LOC(f->locale, mkname("border", "road"));
2010-08-08 10:06:34 +02:00
return buffer;
}
2011-03-07 08:02:35 +01:00
static void b_readroad(connection * b, storage * store)
2010-08-08 10:06:34 +02:00
{
b->data.sa[0] = (short)store->r_int(store);
b->data.sa[1] = (short)store->r_int(store);
}
2011-03-07 08:02:35 +01:00
static void b_writeroad(const connection * b, storage * store)
2010-08-08 10:06:34 +02:00
{
store->w_int(store, b->data.sa[0]);
store->w_int(store, b->data.sa[1]);
}
2011-03-07 08:02:35 +01:00
static boolean b_validroad(const connection * b)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
if (b->data.sa[0] == SHRT_MAX)
return false;
2010-08-08 10:06:34 +02:00
return true;
}
2011-03-07 08:02:35 +01:00
static boolean b_rvisibleroad(const connection * b, const region * r)
2010-08-08 10:06:34 +02:00
{
int x = b->data.i;
2011-03-07 08:02:35 +01:00
x = (r == b->from) ? b->data.sa[0] : b->data.sa[1];
if (x == 0) {
2010-08-08 10:06:34 +02:00
return false;
}
2011-03-07 08:02:35 +01:00
if (b->to != r && b->from != r) {
2010-08-08 10:06:34 +02:00
return false;
}
return true;
}
border_type bt_road = {
"road", VAR_INT,
b_transparent,
2011-03-07 08:02:35 +01:00
NULL, /* init */
NULL, /* destroy */
b_readroad, /* read */
b_writeroad, /* write */
b_blocknone, /* block */
b_nameroad, /* name */
b_rvisibleroad, /* rvisible */
b_finvisible, /* fvisible */
b_uinvisible, /* uvisible */
b_validroad /* valid */
2010-08-08 10:06:34 +02:00
};
2011-03-07 08:02:35 +01:00
void write_borders(struct storage *store)
2010-08-08 10:06:34 +02:00
{
int i;
2011-03-07 08:02:35 +01:00
for (i = 0; i != BORDER_MAXHASH; ++i) {
connection *bhash;
for (bhash = borders[i]; bhash; bhash = bhash->nexthash) {
connection *b;
for (b = bhash; b != NULL; b = b->next) {
if (b->type->valid && !b->type->valid(b))
continue;
2010-08-08 10:06:34 +02:00
store->w_tok(store, b->type->__name);
store->w_int(store, b->id);
store->w_int(store, b->from->uid);
store->w_int(store, b->to->uid);
2011-03-07 08:02:35 +01:00
if (b->type->write)
b->type->write(b, store);
2010-08-08 10:06:34 +02:00
store->w_brk(store);
}
}
}
store->w_tok(store, "end");
}
2011-03-07 08:02:35 +01:00
int read_borders(struct storage *store)
2010-08-08 10:06:34 +02:00
{
for (;;) {
unsigned int bid = 0;
char zText[32];
2011-03-07 08:02:35 +01:00
connection *b;
region *from, *to;
border_type *type;
2010-08-08 10:06:34 +02:00
store->r_tok_buf(store, zText, sizeof(zText));
2011-03-07 08:02:35 +01:00
if (!strcmp(zText, "end"))
break;
2010-08-08 10:06:34 +02:00
bid = store->r_int(store);
2011-03-07 08:02:35 +01:00
if (store->version < UIDHASH_VERSION) {
2010-08-08 10:06:34 +02:00
short fx, fy, tx, ty;
fx = (short)store->r_int(store);
fy = (short)store->r_int(store);
tx = (short)store->r_int(store);
ty = (short)store->r_int(store);
from = findregion(fx, fy);
to = findregion(tx, ty);
} else {
unsigned int fid = (unsigned int)store->r_int(store);
unsigned int tid = (unsigned int)store->r_int(store);
from = findregionbyid(fid);
to = findregionbyid(tid);
}
type = find_bordertype(zText);
2011-03-07 08:02:35 +01:00
if (type == NULL) {
log_error("[read_borders] unknown connection type %s in %s\n", zText, regionname(from, NULL));
2010-08-08 10:06:34 +02:00
assert(type || !"connection type not registered");
}
2011-03-07 08:02:35 +01:00
if (to == from && type && from) {
2010-08-08 10:06:34 +02:00
direction_t dir = (direction_t) (rng_int() % MAXDIRECTIONS);
2011-03-07 08:02:35 +01:00
region *r = rconnect(from, dir);
log_error("[read_borders] invalid %s in %s\n", type->__name, regionname(from, NULL));
2011-03-07 08:02:35 +01:00
if (r != NULL)
to = r;
2010-08-08 10:06:34 +02:00
}
b = new_border(type, from, to);
2011-03-07 08:02:35 +01:00
nextborder--; /* new_border erh<72>ht den Wert */
2010-08-08 10:06:34 +02:00
b->id = bid;
2011-03-07 08:02:35 +01:00
assert(bid <= nextborder);
if (type->read)
type->read(b, store);
if (store->version < NOBORDERATTRIBS_VERSION) {
attrib *a = NULL;
2010-08-08 10:06:34 +02:00
int result = a_read(store, &a, b);
2011-03-07 08:02:35 +01:00
if (border_convert_cb)
border_convert_cb(b, a);
2010-08-08 10:06:34 +02:00
while (a) {
a_remove(&a, a);
}
2011-03-07 08:02:35 +01:00
if (result < 0)
return result;
2010-08-08 10:06:34 +02:00
}
if ((type->read && !type->write) || !to || !from) {
log_warning("erase invalid border '%s' between '%s' and '%s'\n", type->__name, regionname(from, 0), regionname(to, 0));
2010-08-08 10:06:34 +02:00
erase_border(b);
}
}
return 0;
}