2001-01-25 10:37:55 +01:00
|
|
|
|
/* vi: set ts=2:
|
|
|
|
|
*
|
2001-04-14 13:39:14 +02:00
|
|
|
|
*
|
2003-07-29 11:48:03 +02:00
|
|
|
|
* Eressea PB(E)M host Copyright (C) 1998-2003
|
2001-01-25 10:37:55 +01:00
|
|
|
|
* Christian Schlittchen (corwin@amber.kn-bremen.de)
|
|
|
|
|
* Katja Zedel (katze@felidae.kn-bremen.de)
|
|
|
|
|
* Henning Peters (faroul@beyond.kn-bremen.de)
|
|
|
|
|
* Enno Rehling (enno@eressea-pbem.de)
|
|
|
|
|
* Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
|
|
|
|
|
*
|
|
|
|
|
* This program may not be used, modified or distributed without
|
|
|
|
|
* prior permission by the authors of Eressea.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "attrib.h"
|
|
|
|
|
|
- Neue Messages fertig
Messages werden jetzt in einem anderen Meta-Format (message* of
message_type*) gespeichert, das man in beliebige Formate (CR oder NR)
rendern kann. crmessage.c und nrmessage.c sind die render-engines dafür.
Die Messagetypen werden in res/{de,en}/messages.xml gesammelt, ultimativ
kann das aber durchaus eine einzelne Datei sein. Die ist derzeit nicht
wirklich xml (Umlaute drin, keine Definitionsdatei), aber gut lesbar.
- make_message
Diese Funktion ersetzt new_message, und ist etwas einfacher in der Syntax:
make_message("dumb_mistake", "unit region command", u, r, cmd) erzeugt
eine neue Nachricht, die dann einfach mit add_message wie bisher an die
Nachrichtenliste gehängt werden kann.
TODO: Messages könnte man durchaus reference-counten, und in mehrere Listen
einfügen, solang sie a) mehrfachverwendet (Kampf!) und b) vom Betrachter
unabhängig sind. Das spart einigen Speicher.
- CR Version erhöht.
Weil die MESSAGETYPES Blocks anders sind als früher
- OFFENSIVE_DELAY
Verbietet Einheiten, deren Partei eine Reigon niht bewachen, den
Angriff in der Region, wenn sie sich in der Runde zuvor bewegt haben.
Status der letzten Runde wird in neuem Attribut at_moved gespeichert.
- SHORT_ATTACKS
ein define, das angibt ob Kämpfen grundsätzlich keine lange Aktion ist.
- XML Parser
xml.[hc] enthält einen XML-Parser, dem man ein plugin mit callbacks
übergibt, die nach dem Parsen eines tokens aufgerufen werden.
2001-04-12 19:21:57 +02:00
|
|
|
|
#include "log.h"
|
|
|
|
|
|
2001-01-25 10:37:55 +01:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2004-05-26 08:42:58 +02:00
|
|
|
|
#define MAXATHASH 61
|
2001-01-25 10:37:55 +01:00
|
|
|
|
attrib_type * at_hash[MAXATHASH];
|
|
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
|
__at_hashkey(const char* s)
|
|
|
|
|
{
|
|
|
|
|
int key = 0;
|
|
|
|
|
int i = strlen(s);
|
|
|
|
|
|
|
|
|
|
while (i) {
|
|
|
|
|
--i;
|
|
|
|
|
key = ((key & 1) << 31) ^ (key >> 1) ^ s[i];
|
|
|
|
|
}
|
|
|
|
|
return key & 0x7fffffff;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
at_register(attrib_type * at)
|
|
|
|
|
{
|
2004-03-19 00:48:40 +01:00
|
|
|
|
attrib_type * find;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
|
2004-03-19 00:48:40 +01:00
|
|
|
|
if (at->read==NULL) {
|
|
|
|
|
log_warning(("registering non-persistent attribute %s.\n", at->name));
|
|
|
|
|
}
|
|
|
|
|
at->hashkey = __at_hashkey(at->name);
|
|
|
|
|
find = at_hash[at->hashkey % MAXATHASH];
|
|
|
|
|
while (find && at->hashkey!=find->hashkey) find = find->nexthash;
|
|
|
|
|
if (find && find==at) {
|
|
|
|
|
log_warning(("attribute '%s' was registered more than once\n", at->name));
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
assert(!find || !"hashkey is already in use");
|
|
|
|
|
}
|
|
|
|
|
at->nexthash = at_hash[at->hashkey % MAXATHASH];
|
|
|
|
|
at_hash[at->hashkey % MAXATHASH] = at;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static attrib_type *
|
|
|
|
|
at_find(unsigned int hk)
|
|
|
|
|
{
|
|
|
|
|
const char* translate[3][2] = {
|
|
|
|
|
{ "zielregion", "targetregion" }, /* remapping: fr<66>her zielregion, heute targetregion */
|
|
|
|
|
{ "verzaubert", "curse" }, /* remapping: fr<66>her verzaubert, jetzt curse */
|
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
attrib_type * find = at_hash[hk % MAXATHASH];
|
|
|
|
|
while (find && hk!=find->hashkey) find = find->nexthash;
|
|
|
|
|
if (!find) {
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (translate[i][0]) {
|
|
|
|
|
if (__at_hashkey(translate[i][0])==hk)
|
|
|
|
|
return at_find(__at_hashkey(translate[i][1]));
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return find;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrib *
|
2001-12-10 01:13:39 +01:00
|
|
|
|
a_select(attrib * a, const void * data, boolean(*compare)(const attrib *, const void *))
|
2001-01-25 10:37:55 +01:00
|
|
|
|
{
|
2001-02-14 08:44:57 +01:00
|
|
|
|
while (a && !compare(a, data)) a = a->next;
|
|
|
|
|
return a;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrib *
|
2004-01-19 21:37:39 +01:00
|
|
|
|
a_find(attrib * a, const attrib_type * at)
|
2001-02-14 08:44:57 +01:00
|
|
|
|
{
|
|
|
|
|
while (a && a->type!=at) a = a->next;
|
|
|
|
|
return a;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-29 11:48:03 +02:00
|
|
|
|
const attrib *
|
|
|
|
|
a_findc(const attrib * a, const attrib_type * at)
|
|
|
|
|
{
|
|
|
|
|
while (a && a->type!=at) a = a->next;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-25 10:37:55 +01:00
|
|
|
|
attrib *
|
2004-01-23 01:11:54 +01:00
|
|
|
|
a_add(attrib ** pa, attrib * a)
|
|
|
|
|
{
|
2001-01-25 10:37:55 +01:00
|
|
|
|
attrib ** find = pa;
|
|
|
|
|
assert(a->next==NULL && a->nexttype==NULL);
|
|
|
|
|
while (*find && (*find)->type!=a->type) find = &(*find)->next;
|
|
|
|
|
if (a->type->flags & ATF_PRESERVE) {
|
|
|
|
|
while (*find) find = &(*find)->nexttype;
|
|
|
|
|
}
|
|
|
|
|
if (a->type->flags & ATF_UNIQUE && *find) {
|
|
|
|
|
if ((*find)->type == a->type) {
|
2004-03-06 19:09:35 +01:00
|
|
|
|
log_error(("duplicate attribute: %s\n", a->type->name));
|
2001-01-25 10:37:55 +01:00
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (*find) {
|
|
|
|
|
attrib ** last = find;
|
|
|
|
|
while (*last) last = &(*last)->nexttype;
|
|
|
|
|
*last = a;
|
|
|
|
|
while (*find && (*find)->type==a->type) find = &(*find)->next;
|
|
|
|
|
}
|
|
|
|
|
a->next = *find;
|
|
|
|
|
*find = a;
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_free(attrib * a) {
|
|
|
|
|
const attrib_type * at = a->type;
|
|
|
|
|
if (at->finalize) at->finalize(a);
|
|
|
|
|
free(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
a_unlink(attrib ** p, attrib * a) {
|
|
|
|
|
attrib ** pa = p;
|
|
|
|
|
while (*pa && *pa!=a) pa = &(*pa)->next;
|
|
|
|
|
if (*pa) {
|
|
|
|
|
attrib ** pnt;
|
|
|
|
|
for (pnt=p;*pnt;pnt=&(*pnt)->next)
|
|
|
|
|
if ((*pnt)->nexttype == a) {
|
|
|
|
|
(*pnt)->nexttype = a->nexttype;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*pa = (*pa)->next;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
a_remove(attrib ** p, attrib * a) {
|
|
|
|
|
int ok;
|
|
|
|
|
ok = a_unlink(p, a);
|
|
|
|
|
if( ok ) a_free(a);
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_removeall(attrib **p, const attrib_type * at)
|
|
|
|
|
{
|
|
|
|
|
attrib *find = *p, *findnext;
|
2001-09-05 21:40:40 +02:00
|
|
|
|
if (find && find->type != at){
|
|
|
|
|
find = a_find(find, at);
|
|
|
|
|
}
|
2001-01-25 10:37:55 +01:00
|
|
|
|
while(find && find->type == at) {
|
|
|
|
|
findnext = find->next;
|
|
|
|
|
a_remove(p, find);
|
|
|
|
|
find = findnext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attrib *
|
|
|
|
|
a_new(const attrib_type * at) {
|
|
|
|
|
attrib * a = calloc(1, sizeof(attrib));
|
|
|
|
|
a->type = at;
|
|
|
|
|
if (at->initialize) at->initialize(a);
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
a_readdefault(attrib * a, FILE * f)
|
|
|
|
|
{
|
|
|
|
|
assert(sizeof(int)==sizeof(a->data));
|
2001-01-27 19:15:52 +01:00
|
|
|
|
fscanf(f, "%d", &a->data.i);
|
2002-04-07 02:44:01 +02:00
|
|
|
|
return AT_READ_OK;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_writedefault(const attrib * a, FILE * f)
|
|
|
|
|
{
|
|
|
|
|
fprintf(f, "%d ", a->data.i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
read_quoted(FILE * f, char *c, size_t size)
|
|
|
|
|
{
|
|
|
|
|
char * s = c;
|
|
|
|
|
do {
|
|
|
|
|
*s = (char) fgetc(f);
|
|
|
|
|
} while (*s!='"');
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
*s = (char) fgetc(f);
|
|
|
|
|
if (*s=='"') break;
|
|
|
|
|
if (s<c+size) ++s;
|
|
|
|
|
}
|
|
|
|
|
*s = 0;
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
a_readstring(attrib * a, FILE * f)
|
|
|
|
|
{
|
|
|
|
|
char zText[4096];
|
|
|
|
|
read_quoted(f, zText, sizeof(zText));
|
|
|
|
|
a->data.v = strdup(zText);
|
2002-04-07 02:44:01 +02:00
|
|
|
|
return AT_READ_OK;
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_writestring(const attrib * a, FILE * f)
|
|
|
|
|
{
|
|
|
|
|
assert(a->data.v);
|
|
|
|
|
fprintf(f, "\"%s\" ", (char*)a->data.v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_finalizestring(attrib * a)
|
|
|
|
|
{
|
|
|
|
|
free(a->data.v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
a_age(attrib ** p)
|
|
|
|
|
{
|
|
|
|
|
attrib ** ap = p;
|
|
|
|
|
/* Attribute altern, und die Entfernung (age()==0) eines Attributs
|
|
|
|
|
* hat Einflu<EFBFBD> auf den Besitzer */
|
|
|
|
|
while(*ap) {
|
|
|
|
|
attrib * a = *ap;
|
2002-04-07 11:58:22 +02:00
|
|
|
|
if (a->type->age && a->type->age(a)==0) a_remove(p, a);
|
2001-01-25 10:37:55 +01:00
|
|
|
|
else ap=&a->next;
|
|
|
|
|
}
|
|
|
|
|
return (*p!=NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_read(FILE * f, attrib ** attribs)
|
|
|
|
|
{
|
|
|
|
|
int key;
|
|
|
|
|
char zText[128];
|
|
|
|
|
strcpy(zText, "unknown");
|
|
|
|
|
|
|
|
|
|
key = -1;
|
|
|
|
|
fscanf(f, "%s", zText);
|
2001-01-27 19:15:52 +01:00
|
|
|
|
if (!strcmp(zText, "end")) return;
|
|
|
|
|
/* { fgets(zText, sizeof(zText), f); ENNO: was ist das? "always ends with \n" ? */
|
|
|
|
|
/* key=-1; }*/
|
2001-01-25 10:37:55 +01:00
|
|
|
|
else key = __at_hashkey(zText);
|
|
|
|
|
|
|
|
|
|
while(key!=-1) {
|
|
|
|
|
attrib_type * at = at_find(key);
|
|
|
|
|
if (!at) {
|
2001-01-27 19:15:52 +01:00
|
|
|
|
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
|
2001-01-25 10:37:55 +01:00
|
|
|
|
assert(at || !"attribute not registered");
|
|
|
|
|
}
|
|
|
|
|
if (at->read) {
|
2002-03-24 11:22:27 +01:00
|
|
|
|
attrib * na = a_new(at);
|
2002-04-07 02:44:01 +02:00
|
|
|
|
int i = at->read(na, f);
|
|
|
|
|
switch (i) {
|
|
|
|
|
case AT_READ_OK:
|
2001-01-25 10:37:55 +01:00
|
|
|
|
a_add(attribs, na);
|
2002-04-07 02:44:01 +02:00
|
|
|
|
break;
|
|
|
|
|
case AT_READ_FAIL:
|
2001-01-25 10:37:55 +01:00
|
|
|
|
a_free(na);
|
2002-04-07 02:44:01 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(!"invalid return value");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-01-25 10:37:55 +01:00
|
|
|
|
} else {
|
|
|
|
|
assert(!"fehler: keine laderoutine f<>r attribut");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fscanf(f, " %s", zText);
|
|
|
|
|
if (!strcmp(zText, "end")) break;
|
|
|
|
|
key = __at_hashkey(zText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
a_write(FILE * f, const attrib * attribs)
|
|
|
|
|
{
|
|
|
|
|
const attrib * na = attribs;
|
|
|
|
|
|
|
|
|
|
while(na) {
|
|
|
|
|
if (na->type->write) {
|
|
|
|
|
assert(na->type->hashkey || !"attribute not registered");
|
|
|
|
|
fprintf(f, "%s ", na->type->name);
|
|
|
|
|
na->type->write(na, f);
|
|
|
|
|
putc(' ', f);
|
|
|
|
|
}
|
|
|
|
|
na = na->next;
|
|
|
|
|
}
|
2001-01-27 19:15:52 +01:00
|
|
|
|
fprintf(f, "end");
|
2001-01-25 10:37:55 +01:00
|
|
|
|
}
|