2014-08-27 21:09:39 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "vortex.h"
|
|
|
|
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <kernel/attrib.h>
|
2018-09-29 13:21:46 +02:00
|
|
|
#include <kernel/gamedata.h>
|
2014-08-27 21:09:39 +02:00
|
|
|
#include <util/language.h>
|
|
|
|
#include <util/log.h>
|
2017-12-28 18:29:40 +01:00
|
|
|
#include <util/strings.h>
|
2014-08-27 21:09:39 +02:00
|
|
|
#include <util/umlaut.h>
|
|
|
|
#include <util/variant.h>
|
|
|
|
|
|
|
|
#include <storage.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2014-08-29 07:47:47 +02:00
|
|
|
#include <stdlib.h>
|
2016-11-26 16:21:41 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-08-27 21:09:39 +02:00
|
|
|
|
|
|
|
typedef struct dir_lookup {
|
|
|
|
char *name;
|
|
|
|
const char *oldname;
|
|
|
|
struct dir_lookup *next;
|
|
|
|
} dir_lookup;
|
|
|
|
|
|
|
|
static dir_lookup *dir_name_lookup;
|
|
|
|
|
2017-08-25 08:52:15 +02:00
|
|
|
void free_special_directions(void)
|
|
|
|
{
|
|
|
|
while (dir_name_lookup) {
|
|
|
|
dir_lookup *dl = dir_name_lookup;
|
|
|
|
dir_name_lookup = dl->next;
|
|
|
|
free(dl->name);
|
|
|
|
free(dl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:19:07 +02:00
|
|
|
void register_special_direction(struct locale *lang, const char *name)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
2015-01-08 20:55:29 +01:00
|
|
|
const char *token = locale_string(lang, name, false);
|
2014-08-27 21:09:39 +02:00
|
|
|
|
2014-09-29 21:19:07 +02:00
|
|
|
if (token) {
|
2014-08-27 21:09:39 +02:00
|
|
|
void **tokens = get_translations(lang, UT_SPECDIR);
|
2014-09-29 21:19:07 +02:00
|
|
|
variant var;
|
2017-12-28 18:29:40 +01:00
|
|
|
char *str = str_strdup(name);
|
2014-09-29 21:19:07 +02:00
|
|
|
|
|
|
|
var.v = str;
|
2016-09-06 20:57:07 +02:00
|
|
|
addtoken((struct tnode **)tokens, token, var);
|
2014-09-29 21:19:07 +02:00
|
|
|
|
|
|
|
if (lang == locales) {
|
|
|
|
dir_lookup *dl = malloc(sizeof(dir_lookup));
|
2018-11-17 22:01:15 +01:00
|
|
|
assert(dl);
|
2014-09-29 21:19:07 +02:00
|
|
|
dl->name = str;
|
|
|
|
dl->oldname = token;
|
|
|
|
dl->next = dir_name_lookup;
|
|
|
|
dir_name_lookup = dl;
|
2014-08-27 21:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-29 21:19:07 +02:00
|
|
|
else {
|
2015-01-08 20:55:29 +01:00
|
|
|
log_debug("no translation for spec_direction '%s' in locale '%s'\n", name, locale_name(lang));
|
2014-09-29 21:19:07 +02:00
|
|
|
}
|
2014-08-27 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************/
|
|
|
|
/* at_direction */
|
|
|
|
/********************/
|
2018-02-09 21:20:43 +01:00
|
|
|
static void a_initdirection(variant * var)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
2018-02-09 21:20:43 +01:00
|
|
|
var->v = calloc(1, sizeof(spec_direction));
|
2014-08-27 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static void a_freedirection(variant *var)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
2018-02-09 21:20:43 +01:00
|
|
|
spec_direction *d = (spec_direction *)(var->v);
|
2014-12-30 23:34:24 +01:00
|
|
|
free(d->desc);
|
2014-12-31 21:14:21 +01:00
|
|
|
free(d->keyword);
|
2014-12-30 23:34:24 +01:00
|
|
|
free(d);
|
2014-08-27 21:09:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:18:44 +01:00
|
|
|
static int a_agedirection(attrib * a, void *owner)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
|
|
|
spec_direction *d = (spec_direction *)(a->data.v);
|
2017-01-10 16:31:05 +01:00
|
|
|
(void)owner;
|
2014-08-27 21:09:39 +02:00
|
|
|
--d->duration;
|
|
|
|
return (d->duration > 0) ? AT_AGE_KEEP : AT_AGE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static int a_readdirection(variant *var, void *owner, struct gamedata *data)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
2016-02-13 13:42:02 +01:00
|
|
|
struct storage *store = data->store;
|
2018-02-09 21:20:43 +01:00
|
|
|
spec_direction *d = (spec_direction *)(var->v);
|
2019-01-24 14:25:58 +01:00
|
|
|
char lbuf[32]; /* Flawfinder: ignore */
|
2014-08-27 21:09:39 +02:00
|
|
|
|
2017-01-10 16:31:05 +01:00
|
|
|
(void)owner;
|
2014-08-27 21:09:39 +02:00
|
|
|
READ_INT(store, &d->x);
|
|
|
|
READ_INT(store, &d->y);
|
|
|
|
READ_INT(store, &d->duration);
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_TOK(store, lbuf, sizeof(lbuf));
|
2017-12-28 18:29:40 +01:00
|
|
|
d->desc = str_strdup(lbuf);
|
2014-12-12 22:52:23 +01:00
|
|
|
READ_TOK(store, lbuf, sizeof(lbuf));
|
2017-12-28 18:29:40 +01:00
|
|
|
d->keyword = str_strdup(lbuf);
|
2014-08-27 21:09:39 +02:00
|
|
|
d->active = true;
|
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-02-09 21:20:43 +01:00
|
|
|
a_writedirection(const variant *var, const void *owner, struct storage *store)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
2018-02-09 21:20:43 +01:00
|
|
|
spec_direction *d = (spec_direction *)(var->v);
|
2014-08-27 21:09:39 +02:00
|
|
|
|
2017-01-10 16:31:05 +01:00
|
|
|
(void)owner;
|
2014-08-27 21:09:39 +02:00
|
|
|
WRITE_INT(store, d->x);
|
|
|
|
WRITE_INT(store, d->y);
|
|
|
|
WRITE_INT(store, d->duration);
|
|
|
|
WRITE_TOK(store, d->desc);
|
|
|
|
WRITE_TOK(store, d->keyword);
|
|
|
|
}
|
|
|
|
attrib_type at_direction = {
|
|
|
|
"direction",
|
|
|
|
a_initdirection,
|
|
|
|
a_freedirection,
|
|
|
|
a_agedirection,
|
|
|
|
a_writedirection,
|
|
|
|
a_readdirection
|
|
|
|
};
|
|
|
|
|
2014-08-28 19:50:38 +02:00
|
|
|
region *find_special_direction(const region * r, const char *token)
|
2014-08-27 21:09:39 +02:00
|
|
|
{
|
|
|
|
attrib *a;
|
|
|
|
|
2019-01-24 14:25:58 +01:00
|
|
|
if (*token == '\0')
|
2014-08-27 21:09:39 +02:00
|
|
|
return NULL;
|
|
|
|
for (a = a_find(r->attribs, &at_direction); a && a->type == &at_direction;
|
|
|
|
a = a->next) {
|
2018-12-02 05:04:49 +01:00
|
|
|
spec_direction *d = (spec_direction *)(a->data.v);
|
2014-08-27 21:09:39 +02:00
|
|
|
|
2014-08-28 19:50:38 +02:00
|
|
|
if (d->active && strcmp(token, d->keyword) == 0) {
|
|
|
|
return findregion(d->x, d->y);
|
2014-08-27 21:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
attrib *create_special_direction(region * r, region * rt, int duration,
|
|
|
|
const char *desc, const char *keyword, bool active)
|
|
|
|
{
|
|
|
|
attrib *a = a_add(&r->attribs, a_new(&at_direction));
|
|
|
|
spec_direction *d = (spec_direction *)(a->data.v);
|
|
|
|
|
|
|
|
d->active = active;
|
|
|
|
d->x = rt->x;
|
|
|
|
d->y = rt->y;
|
|
|
|
d->duration = duration;
|
2017-12-28 18:29:40 +01:00
|
|
|
d->desc = str_strdup(desc);
|
|
|
|
d->keyword = str_strdup(keyword);
|
2014-08-27 21:09:39 +02:00
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
spec_direction *special_direction(const region * from, const region * to)
|
|
|
|
{
|
2016-02-01 17:31:03 +01:00
|
|
|
const attrib *a = a_find(from->attribs, &at_direction);
|
2014-08-27 21:09:39 +02: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;
|
|
|
|
a = a->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|