2001-02-24 13:50:51 +01:00
|
|
|
/* vi: set ts=2:
|
|
|
|
+-------------------+ Christian Schlittchen <corwin@amber.kn-bremen.de>
|
|
|
|
| | Enno Rehling <enno@eressea-pbem.de>
|
|
|
|
| Eressea PBEM host | Katja Zedel <katze@felidae.kn-bremen.de>
|
2003-07-29 11:48:03 +02:00
|
|
|
| (c) 1998 - 2003 | Henning Peters <faroul@beyond.kn-bremen.de>
|
2001-02-24 13:50:51 +01:00
|
|
|
| | Ingo Wilken <Ingo.Wilken@informatik.uni-oldenburg.de>
|
|
|
|
+-------------------+ Stefan Reich <reich@halbling.de>
|
|
|
|
|
2001-04-16 16:34:19 +02:00
|
|
|
This program may not be used, modified or distributed
|
2001-02-24 13:50:51 +01:00
|
|
|
without prior permission by the authors of Eressea.
|
2001-04-16 16:34:19 +02:00
|
|
|
|
2001-02-24 13:50:51 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include "message.h"
|
|
|
|
|
2004-02-21 18:13:24 +01:00
|
|
|
#include "goodies.h"
|
2004-04-18 14:24:33 +02:00
|
|
|
#include "log.h"
|
2004-02-21 18:13:24 +01:00
|
|
|
|
2001-05-20 23:47:56 +02:00
|
|
|
/* libc includes */
|
|
|
|
#include <assert.h>
|
2001-02-24 13:50:51 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2001-04-16 16:34:19 +02:00
|
|
|
const char *
|
- 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
|
|
|
mt_name(const message_type* mtype)
|
|
|
|
{
|
|
|
|
return mtype->name;
|
|
|
|
}
|
|
|
|
|
2001-04-16 16:34:19 +02:00
|
|
|
message_type *
|
2001-02-24 13:50:51 +01:00
|
|
|
mt_new(const char * name, const char * args[])
|
|
|
|
{
|
2004-03-28 22:53:47 +02:00
|
|
|
int i, nparameters = 0;
|
|
|
|
message_type * mtype = (message_type *)malloc(sizeof(message_type));
|
2001-02-24 13:50:51 +01:00
|
|
|
|
2004-04-18 14:24:33 +02:00
|
|
|
assert(name!=NULL);
|
2004-04-19 01:25:16 +02:00
|
|
|
if (name==NULL) {
|
2004-04-18 14:24:33 +02:00
|
|
|
log_error(("Trying to create message_type with name=0x0\n"));
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-03-28 22:53:47 +02:00
|
|
|
if (args!=NULL) for (nparameters=0;args[nparameters];++nparameters);
|
2001-02-24 13:50:51 +01:00
|
|
|
|
2004-03-28 22:53:47 +02:00
|
|
|
mtype->name = strdup(name);
|
|
|
|
mtype->nparameters = nparameters;
|
|
|
|
if (nparameters > 0) {
|
|
|
|
mtype->pnames = (const char**)malloc(sizeof(char*) * nparameters);
|
|
|
|
mtype->types = (const char**)malloc(sizeof(char*) * nparameters);
|
|
|
|
} else {
|
|
|
|
mtype->pnames = NULL;
|
|
|
|
mtype->types = NULL;
|
|
|
|
}
|
|
|
|
if (args!=NULL) for (i=0;args[i];++i) {
|
|
|
|
const char * x = args[i];
|
|
|
|
const char * spos = strchr(x, ':');
|
|
|
|
if (spos==NULL) {
|
|
|
|
mtype->pnames[i] = strdup(x);
|
|
|
|
mtype->types[i] = NULL;
|
2001-09-05 21:40:40 +02:00
|
|
|
} else {
|
2004-08-07 09:42:22 +02:00
|
|
|
char * cp = strncpy((char*)malloc(spos-x+1), x, spos-x);
|
2004-03-28 22:53:47 +02:00
|
|
|
cp[spos-x] = '\0';
|
|
|
|
mtype->pnames[i] = cp;
|
|
|
|
/* optimierung: Typ-Strings zentral verwalten. */
|
|
|
|
mtype->types[i] = strdup(spos+1);
|
2001-09-05 21:40:40 +02:00
|
|
|
}
|
2004-03-28 22:53:47 +02:00
|
|
|
}
|
|
|
|
return mtype;
|
2001-02-24 13:50:51 +01:00
|
|
|
}
|
|
|
|
|
2001-04-16 16:34:19 +02:00
|
|
|
message_type *
|
2001-02-24 13:50:51 +01:00
|
|
|
mt_new_va(const char * name, ...)
|
|
|
|
{
|
|
|
|
const char * args[16];
|
|
|
|
int i = 0;
|
|
|
|
va_list marker;
|
|
|
|
|
|
|
|
va_start(marker, name);
|
|
|
|
for (;;) {
|
|
|
|
const char * c = va_arg(marker, const char*);
|
|
|
|
args[i++] = c;
|
|
|
|
if (c==NULL) break;
|
|
|
|
}
|
|
|
|
va_end(marker);
|
|
|
|
return mt_new(name, args);
|
|
|
|
}
|
|
|
|
|
2004-06-21 18:45:27 +02:00
|
|
|
typedef struct arg_type {
|
|
|
|
struct arg_type * next;
|
|
|
|
const char * name;
|
|
|
|
void (*free)(void*);
|
|
|
|
void* (*copy)(void*);
|
|
|
|
} arg_type;
|
|
|
|
|
|
|
|
arg_type * argtypes = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
register_argtype(const char * name, void(*free_arg)(void*), void*(*copy_arg)(void*))
|
|
|
|
{
|
2004-08-07 09:42:22 +02:00
|
|
|
arg_type * atype = (arg_type *)malloc(sizeof(arg_type));
|
2004-06-21 18:45:27 +02:00
|
|
|
atype->name = name;
|
|
|
|
atype->next = argtypes;
|
|
|
|
atype->free = free_arg;
|
|
|
|
atype->copy = copy_arg;
|
|
|
|
argtypes = atype;
|
|
|
|
}
|
|
|
|
|
|
|
|
static arg_type *
|
|
|
|
find_argtype(const char * name)
|
|
|
|
{
|
|
|
|
arg_type * atype = argtypes;
|
|
|
|
while (atype!=NULL) {
|
|
|
|
if (strcmp(atype->name, name)==0) return atype;
|
|
|
|
atype = atype->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
copy_arg(const char * type, void * data)
|
|
|
|
{
|
|
|
|
arg_type * atype = find_argtype(type);
|
|
|
|
if (atype==NULL) return data;
|
|
|
|
if (atype->copy==NULL) return data;
|
|
|
|
return atype->copy(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_arg(const char * type, void * data)
|
|
|
|
{
|
|
|
|
arg_type * atype = find_argtype(type);
|
|
|
|
if (atype && atype->free) atype->free(data);
|
|
|
|
}
|
|
|
|
|
2001-02-24 13:50:51 +01:00
|
|
|
message *
|
2001-05-20 12:42:15 +02:00
|
|
|
msg_create(const struct message_type * type, void * args[])
|
2001-02-24 13:50:51 +01:00
|
|
|
{
|
2004-04-18 14:24:33 +02:00
|
|
|
int i;
|
|
|
|
message * msg = (message *)malloc(sizeof(message));
|
|
|
|
|
|
|
|
assert(type!=NULL);
|
|
|
|
if (type==NULL) {
|
|
|
|
log_error(("Trying to create message with type=0x0\n"));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
msg->type = type;
|
2004-08-07 09:42:22 +02:00
|
|
|
msg->parameters = (void**)calloc(sizeof(void*), type->nparameters);
|
2004-04-18 14:24:33 +02:00
|
|
|
msg->refcount=1;
|
|
|
|
for (i=0;i!=type->nparameters;++i) {
|
2004-06-21 18:45:27 +02:00
|
|
|
msg->parameters[i] = copy_arg(type->types[i], args[i]);
|
2004-04-18 14:24:33 +02:00
|
|
|
}
|
|
|
|
return msg;
|
2001-02-24 13:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message *
|
2001-05-20 12:42:15 +02:00
|
|
|
msg_create_va(const struct message_type * type, ...)
|
2001-02-24 13:50:51 +01:00
|
|
|
/* sets a messages parameters */
|
|
|
|
{
|
|
|
|
void * args[16];
|
|
|
|
va_list marker;
|
|
|
|
int i;
|
|
|
|
va_start(marker, type);
|
|
|
|
for (i=0;i!=type->nparameters;++i) {
|
|
|
|
args[i] = va_arg(marker, void*);
|
|
|
|
}
|
|
|
|
va_end(marker);
|
2001-05-20 12:42:15 +02:00
|
|
|
return msg_create(type, args);
|
2001-02-24 13:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct messagetype_list {
|
|
|
|
struct messagetype_list * next;
|
2004-02-21 18:13:24 +01:00
|
|
|
const struct message_type * data;
|
2001-02-24 13:50:51 +01:00
|
|
|
} messagetype_list;
|
|
|
|
|
2004-05-26 08:42:58 +02:00
|
|
|
#define MT_MAXHASH 1021
|
2004-02-21 18:13:24 +01:00
|
|
|
static messagetype_list * messagetypes[MT_MAXHASH];
|
|
|
|
|
|
|
|
const message_type *
|
|
|
|
mt_find(const char * name)
|
|
|
|
{
|
|
|
|
const struct message_type * found = NULL;
|
|
|
|
unsigned int hash = hashstring(name) % MT_MAXHASH;
|
|
|
|
messagetype_list * type = messagetypes[hash];
|
|
|
|
while (type) {
|
|
|
|
if (strcmp(type->data->name, name)==0) {
|
|
|
|
if (found==NULL) found = type->data;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
type = type->next;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2001-02-24 13:50:51 +01:00
|
|
|
|
2001-04-16 16:34:19 +02:00
|
|
|
const message_type *
|
2001-02-24 13:50:51 +01:00
|
|
|
mt_register(const message_type * type)
|
|
|
|
{
|
2004-02-21 18:13:24 +01:00
|
|
|
unsigned int hash = hashstring(type->name) % MT_MAXHASH;
|
|
|
|
messagetype_list * mtl = messagetypes[hash];
|
2004-04-18 14:24:33 +02:00
|
|
|
while (mtl && mtl->data!=type) mtl=mtl->next;
|
|
|
|
if (mtl==NULL) {
|
2004-08-07 09:42:22 +02:00
|
|
|
mtl = (messagetype_list*)malloc(sizeof(messagetype_list));
|
2004-04-18 14:24:33 +02:00
|
|
|
mtl->data = type;
|
|
|
|
mtl->next = messagetypes[hash];
|
|
|
|
messagetypes[hash] = mtl;
|
|
|
|
}
|
|
|
|
return type;
|
2001-02-24 13:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-05-20 23:47:56 +02:00
|
|
|
msg_free(message *msg)
|
2001-02-24 13:50:51 +01:00
|
|
|
{
|
2004-06-21 18:45:27 +02:00
|
|
|
int i;
|
2004-04-18 14:24:33 +02:00
|
|
|
assert(msg->refcount==0);
|
2004-06-21 18:45:27 +02:00
|
|
|
for (i=0;i!=msg->type->nparameters;++i) {
|
|
|
|
free_arg(msg->type->types[i], msg->parameters[i]);
|
|
|
|
}
|
2004-04-18 14:24:33 +02:00
|
|
|
free((void*)msg->parameters);
|
|
|
|
free(msg);
|
2001-02-28 23:14:59 +01:00
|
|
|
}
|
2001-05-20 23:47:56 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
msg_release(struct message * msg)
|
|
|
|
{
|
2004-04-18 14:24:33 +02:00
|
|
|
assert(msg->refcount>0);
|
|
|
|
if (--msg->refcount>0) return;
|
|
|
|
msg_free(msg);
|
2001-05-20 23:47:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct message *
|
|
|
|
msg_addref(struct message * msg)
|
|
|
|
{
|
2004-04-18 14:24:33 +02:00
|
|
|
assert(msg->refcount>0);
|
|
|
|
++msg->refcount;
|
|
|
|
return msg;
|
2001-05-20 23:47:56 +02:00
|
|
|
}
|
|
|
|
|