server/src/triggers/unitmessage.c

125 lines
2.9 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/* vi: set ts=2:
+-------------------+ Enno Rehling <enno@eressea.de>
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
| (c) 1998 - 2008 | Katja Zedel <katze@felidae.kn-bremen.de>
+-------------------+
This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include <kernel/config.h>
#include "unitmessage.h"
/* kernel includes */
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/messages.h>
2010-08-08 10:06:34 +02:00
/* util includes */
#include <util/attrib.h>
#include <util/base36.h>
#include <util/event.h>
#include <util/goodies.h>
#include <util/language.h>
#include <util/log.h>
#include <util/resolve.h>
#include <storage.h>
2010-08-08 10:06:34 +02:00
/* ansi includes */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/***
** give an item to someone
**/
typedef struct unitmessage_data {
2011-03-07 08:02:35 +01:00
struct unit *target;
char *string;
2010-08-08 10:06:34 +02:00
int type;
int level;
} unitmessage_data;
2011-03-07 08:02:35 +01:00
static void unitmessage_init(trigger * t)
2010-08-08 10:06:34 +02:00
{
t->data.v = calloc(sizeof(unitmessage_data), 1);
}
2011-03-07 08:02:35 +01:00
static void unitmessage_free(trigger * t)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
unitmessage_data *sd = (unitmessage_data *) t->data.v;
2010-08-08 10:06:34 +02:00
free(sd->string);
free(t->data.v);
}
2011-03-07 08:02:35 +01:00
static int unitmessage_handle(trigger * t, void *data)
2010-08-08 10:06:34 +02:00
{
/* call an event handler on unitmessage.
2011-03-07 08:02:35 +01:00
* data.v -> ( variant event, int timer )
*/
unitmessage_data *td = (unitmessage_data *) t->data.v;
2010-08-08 10:06:34 +02:00
if (td->target && td->target->no) {
2011-03-07 08:02:35 +01:00
struct faction *f = td->target->faction;
const char * str = LOC(f->locale, td->string);
/* bug found in turn 733: sometimes, alps have f*cked up messages */
if (td->string && td->string[0]) {
addmessage(td->target->region, f, str, td->type,
td->level);
}
2010-08-08 10:06:34 +02:00
}
unused_arg(data);
2010-08-08 10:06:34 +02:00
return 0;
}
2011-03-07 08:02:35 +01:00
static void unitmessage_write(const trigger * t, struct storage *store)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
unitmessage_data *td = (unitmessage_data *) t->data.v;
2010-08-08 10:06:34 +02:00
write_unit_reference(td->target, store);
WRITE_TOK(store, td->string);
WRITE_INT(store, td->type);
WRITE_INT(store, td->level);
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
static int unitmessage_read(trigger * t, struct storage *store)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
unitmessage_data *td = (unitmessage_data *) t->data.v;
2010-08-08 10:06:34 +02:00
char zText[256];
2011-03-07 08:02:35 +01:00
int result =
read_reference(&td->target, store, read_unit_reference, resolve_unit);
READ_TOK(store, zText, sizeof(zText));
td->string = _strdup(zText);
READ_INT(store, &td->type);
READ_INT(store, &td->level);
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
if (result == 0 && td->target == NULL) {
2010-08-08 10:06:34 +02:00
return AT_READ_FAIL;
}
return AT_READ_OK;
}
trigger_type tt_unitmessage = {
"unitmessage",
unitmessage_init,
unitmessage_free,
unitmessage_handle,
unitmessage_write,
unitmessage_read
};
2011-03-07 08:02:35 +01:00
trigger *trigger_unitmessage(unit * target, const char *string, int type,
int level)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
trigger *t = t_new(&tt_unitmessage);
unitmessage_data *td = (unitmessage_data *) t->data.v;
2010-08-08 10:06:34 +02:00
td->target = target;
td->string = _strdup(string);
2010-08-08 10:06:34 +02:00
td->type = type;
td->level = level;
return t;
}