2010-08-08 10:06:34 +02:00
|
|
|
/*
|
2015-01-30 22:10:29 +01:00
|
|
|
Copyright (c) 1998-2015, Enno Rehling <enno@eressea.de>
|
2015-01-30 20:37:14 +01:00
|
|
|
Katja Zedel <katze@felidae.kn-bremen.de
|
|
|
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
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 "key.h"
|
|
|
|
|
|
|
|
#include <util/attrib.h>
|
2016-02-13 13:42:02 +01:00
|
|
|
#include <util/gamedata.h>
|
2016-02-09 06:43:19 +01:00
|
|
|
#include <storage.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2016-02-09 13:56:57 +01:00
|
|
|
#include <string.h>
|
2016-02-09 06:43:19 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
static void a_writekeys(const attrib *a, const void *o, storage *store) {
|
|
|
|
int i, *keys = (int *)a->data.v;
|
2017-02-13 18:48:38 +01:00
|
|
|
WRITE_INT(store, keys[0]);
|
|
|
|
for (i = 0; i < keys[0]; ++i) {
|
|
|
|
WRITE_INT(store, keys[i * 2 + 1]);
|
|
|
|
WRITE_INT(store, keys[i * 2 + 2]);
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
static int a_readkeys(attrib * a, void *owner, gamedata *data) {
|
2016-02-09 06:43:19 +01:00
|
|
|
int i, *p = 0;
|
2016-02-13 13:42:02 +01:00
|
|
|
READ_INT(data->store, &i);
|
2017-02-12 19:57:02 +01:00
|
|
|
assert(i < 4096 && i>=0);
|
|
|
|
if (i == 0) {
|
|
|
|
return AT_READ_FAIL;
|
|
|
|
}
|
2017-02-13 18:48:38 +01:00
|
|
|
a->data.v = p = malloc(sizeof(int)*(i*2 + 1));
|
2016-02-09 06:43:19 +01:00
|
|
|
*p++ = i;
|
2016-02-09 07:03:11 +01:00
|
|
|
while (i--) {
|
2016-02-13 13:42:02 +01:00
|
|
|
READ_INT(data->store, p++);
|
2017-02-13 18:48:38 +01:00
|
|
|
if (data->version >= KEYVAL_VERSION) {
|
|
|
|
READ_INT(data->store, p++);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*p++ = 1;
|
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
static int a_readkey(attrib *a, void *owner, struct gamedata *data) {
|
|
|
|
int res = a_readint(a, owner, data);
|
2016-02-09 06:43:19 +01:00
|
|
|
return (res != AT_READ_FAIL) ? AT_READ_DEPR : res;
|
|
|
|
}
|
|
|
|
|
2016-03-10 21:54:53 +01:00
|
|
|
static void a_freekeys(attrib *a) {
|
|
|
|
free(a->data.v);
|
|
|
|
}
|
|
|
|
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib_type at_keys = {
|
|
|
|
"keys",
|
|
|
|
NULL,
|
2016-03-10 21:54:53 +01:00
|
|
|
a_freekeys,
|
2016-02-09 06:43:19 +01:00
|
|
|
NULL,
|
|
|
|
a_writekeys,
|
|
|
|
a_readkeys,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-02-11 22:15:21 +01:00
|
|
|
static void a_upgradekeys(attrib **alist, attrib *abegin) {
|
2016-02-09 06:43:19 +01:00
|
|
|
int n = 0, *keys = 0;
|
2017-02-13 18:48:38 +01:00
|
|
|
int i = 0, val[8];
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib *a, *ak = a_find(*alist, &at_keys);
|
2016-05-02 19:22:57 +02:00
|
|
|
if (ak) {
|
2016-02-09 06:43:19 +01:00
|
|
|
keys = (int *)ak->data.v;
|
2016-05-02 19:22:57 +02:00
|
|
|
if (keys) n = keys[0];
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
for (a = abegin; a && a->type == abegin->type; a = a->next) {
|
2017-02-13 18:48:38 +01:00
|
|
|
val[i * 2] = a->data.i;
|
|
|
|
val[i * 2 + 1] = 1;
|
|
|
|
if (++i == 4) {
|
|
|
|
keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1));
|
|
|
|
memcpy(keys + 2 * n + 1, val, sizeof(val));
|
2016-02-09 06:43:19 +01:00
|
|
|
n += i;
|
2016-02-13 21:38:42 +01:00
|
|
|
i = 0;
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
2017-02-13 18:48:38 +01:00
|
|
|
keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1));
|
|
|
|
memcpy(keys + 2 * n + 1, val, sizeof(int)*i*2);
|
2016-05-02 19:22:57 +02:00
|
|
|
if (!ak) {
|
|
|
|
ak = a_add(alist, a_new(&at_keys));
|
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
2016-05-02 19:22:57 +02:00
|
|
|
ak->data.v = keys;
|
2016-02-09 06:43:19 +01:00
|
|
|
keys[0] = n + i;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
attrib_type at_key = {
|
2015-01-30 20:37:14 +01:00
|
|
|
"key",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
a_writeint,
|
2016-02-09 06:43:19 +01:00
|
|
|
a_readkey,
|
|
|
|
a_upgradekeys
|
2010-08-08 10:06:34 +02:00
|
|
|
};
|
|
|
|
|
2017-02-13 18:48:38 +01:00
|
|
|
void key_set(attrib ** alist, int key, int val)
|
2016-02-09 06:43:19 +01:00
|
|
|
{
|
2017-02-13 18:48:38 +01:00
|
|
|
int *keys, n = 0;
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(*alist, &at_keys);
|
2016-02-09 06:43:19 +01:00
|
|
|
if (!a) {
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_add(alist, a_new(&at_keys));
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
2016-02-09 07:03:11 +01:00
|
|
|
keys = (int *)a->data.v;
|
|
|
|
if (keys) {
|
2017-02-13 18:48:38 +01:00
|
|
|
n = keys[0];
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
2017-02-13 18:48:38 +01:00
|
|
|
keys = realloc(keys, sizeof(int) *(2 * n + 3));
|
|
|
|
// TODO: does insertion sort pay off here? prob. not.
|
|
|
|
keys[0] = n + 1;
|
|
|
|
keys[2 * n + 1] = key;
|
|
|
|
keys[2 * n + 2] = val;
|
2016-02-09 07:03:11 +01:00
|
|
|
a->data.v = keys;
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void key_unset(attrib ** alist, int key)
|
|
|
|
{
|
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(*alist, &at_keys);
|
2016-02-09 06:43:19 +01:00
|
|
|
if (a) {
|
2016-02-09 07:03:11 +01:00
|
|
|
int i, *keys = (int *)a->data.v;
|
|
|
|
if (keys) {
|
2017-02-13 18:48:38 +01:00
|
|
|
int n = keys[0];
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
if (keys[2 * i + 1] == key) {
|
|
|
|
memmove(keys + 2 * i + 1, keys + 2 * n - 1, 2 * sizeof(int));
|
2016-02-09 07:03:11 +01:00
|
|
|
keys[0]--;
|
2017-02-13 18:48:38 +01:00
|
|
|
break;
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:48:38 +01:00
|
|
|
int key_get(attrib *alist, int key) {
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(alist, &at_keys);
|
|
|
|
if (a) {
|
|
|
|
int i, *keys = (int *)a->data.v;
|
|
|
|
if (keys) {
|
2017-02-13 18:48:38 +01:00
|
|
|
for (i = 0; i != keys[0]; ++i) {
|
|
|
|
if (keys[i*2+1] == key) {
|
|
|
|
return keys[i * 2 + 2];
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 18:48:38 +01:00
|
|
|
return 0;
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|