diff --git a/scripts/eressea/embassy.lua b/scripts/eressea/embassy.lua index 704581c6f..93ff66d7c 100644 --- a/scripts/eressea/embassy.lua +++ b/scripts/eressea/embassy.lua @@ -5,6 +5,20 @@ if not config.embassy then return nil end local embassy = {} local home = nil +-- global exports (use item) +function use_seashell(u, amount) +-- Muschelplateau... + local visit = u.faction:get_key('mupL') + if visit and u.region~= home then + local turns = get_turn() - visit + local msg = message.create('msg_event') + msg:set_string("string", u.name .. "(" .. itoa36(u.id) .. ") erzählt den Bewohnern von " .. u.region.name .. " von Muschelplateau, das die Partei " .. u.faction.name .. " vor " .. turns .. " Wochen besucht hat." ) + msg:send_region(u.region) + return 0 + end + return -4 +end + function embassy.init() home = get_region(165,30) if home==nil then @@ -20,7 +34,7 @@ function embassy.update() eressea.log.debug("updating embassies in " .. tostring(home)) local u for u in home.units do - if not u.faction:get_key('mupL') then + if u.faction:get_key('mupL')==0 then if (u.faction:add_item('seashell', 1)>0) then eressea.log.debug("new seashell for " .. tostring(u.faction)) u.faction:set_key('mupL', get_turn()) diff --git a/src/attributes/dict.c b/src/attributes/dict.c index 6d4b3ed3c..ad66e3676 100644 --- a/src/attributes/dict.c +++ b/src/attributes/dict.c @@ -42,6 +42,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +typedef enum { + TNONE = 0, TINTEGER = 1 +} dict_type; + typedef struct dict_data { dict_type type; char *name; @@ -62,94 +66,38 @@ static int dict_read(attrib * a, void *owner, gamedata *data) storage *store = data->store; char name[NAMESIZE]; dict_data *dd = (dict_data *)a->data.v; - int result, n; - float flt; + int n; READ_STR(store, name, sizeof(name)); dd->name = strdup(name); READ_INT(store, &n); dd->type = (dict_type)n; - switch (dd->type) { - case TINTEGER: - READ_INT(store, &dd->data.i); - break; - case TREAL: - READ_FLT(store, &flt); - if ((int)flt == flt) { - dd->type = TINTEGER; - dd->data.i = (int)flt; - } - else { - dd->data.real = flt; - } - break; - case TSTRING: - READ_STR(store, name, sizeof(name)); - dd->data.str = strdup(name); - break; - case TBUILDING: - result = - read_reference(&dd->data.b, data, read_building_reference, - resolve_building); - if (result == 0 && !dd->data.b) { - return AT_READ_FAIL; - } - break; - case TUNIT: - result = - read_reference(&dd->data.u, data, read_unit_reference, resolve_unit); - if (result == 0 && !dd->data.u) { - return AT_READ_FAIL; - } - break; - case TFACTION: - result = - read_reference(&dd->data.f, data, read_faction_reference, - resolve_faction); - if (result == 0 && !dd->data.f) { - return AT_READ_FAIL; - } - break; - case TREGION: - result = - read_reference(&dd->data.r, data, read_region_reference, - RESOLVE_REGION(data->version)); - if (result == 0 && !dd->data.r) { - return AT_READ_FAIL; - } - break; - case TSHIP: - /* return read_ship_reference(&data->data.sh, store); */ - assert(!"not implemented"); - break; - case TNONE: - break; - default: + if (dd->type != TINTEGER) { + log_error("read dict, invalid type %d", n); return AT_READ_FAIL; } + READ_INT(store, &dd->data.i); return AT_READ_DEPR; } static void dict_init(attrib * a) { - dict_data *data; + dict_data *dd; a->data.v = malloc(sizeof(dict_data)); - data = (dict_data *)a->data.v; - data->type = TNONE; + dd = (dict_data *)a->data.v; + dd->type = TNONE; } static void dict_done(attrib * a) { - dict_data *data = (dict_data *)a->data.v; - if (data->type == TSTRING) - free(data->data.str); - free(data->name); + dict_data *dd = (dict_data *)a->data.v; + free(dd->name); free(a->data.v); } static void dict_upgrade(attrib **alist, attrib *abegin) { int n = 0, *keys = 0; - int i = 0, val[4]; + int i = 0, val[8]; attrib *a, *ak = a_find(*alist, &at_keys); if (ak) { keys = (int *)ak->data.v; @@ -162,7 +110,9 @@ static void dict_upgrade(attrib **alist, attrib *abegin) { } else { if (strcmp(dd->name, "embassy_muschel")==0) { - val[i++] = atoi36("mupL"); + val[i * 2] = atoi36("mupL"); + val[i * 2 + 1] = dd->data.i; + ++i; } else { log_error("dict conversion, bad entry %s", dd->name); @@ -170,14 +120,14 @@ static void dict_upgrade(attrib **alist, attrib *abegin) { } if (i == 4) { keys = realloc(keys, sizeof(int) * (n + i + 1)); - memcpy(keys + n + 1, val, sizeof(int)*i); + memcpy(keys + n + 1, val, sizeof(val)); n += i; i = 0; } } if (i > 0) { - keys = realloc(keys, sizeof(int) * (n + i + 1)); - memcpy(keys + n + 1, val, sizeof(int)*i); + keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1)); + memcpy(keys + n*2 + 1, val, sizeof(int)*i*2); if (!ak) { ak = a_add(alist, a_new(&at_keys)); } @@ -190,3 +140,11 @@ attrib_type at_dict = { "object", dict_init, dict_done, NULL, NULL, dict_read, dict_upgrade }; + +void dict_set(attrib * a, const char * name, int value) +{ + dict_data *dd = (dict_data *)a->data.v; + dd->name = strdup(name); + dd->type = TINTEGER; + dd->data.i = value; +} diff --git a/src/attributes/dict.h b/src/attributes/dict.h index 7b028fa00..cc700bb9f 100644 --- a/src/attributes/dict.h +++ b/src/attributes/dict.h @@ -13,18 +13,16 @@ #ifndef H_ATTRIBUTE_OBJECT #define H_ATTRIBUTE_OBJECT -#include +struct attrib_type; +struct attrib; #ifdef __cplusplus extern "C" { #endif - typedef enum { - TNONE = 0, TINTEGER = 1, TREAL = 2, TSTRING = 3, - TUNIT = 10, TFACTION = 11, TREGION = 12, TBUILDING = 13, TSHIP = 14 - } dict_type; + extern struct attrib_type at_dict; // DEPRECATED: at_dict has been replaced with at_keys - extern struct attrib_type at_dict; + void dict_set(struct attrib * a, const char * name, int value); #ifdef __cplusplus } diff --git a/src/attributes/key.c b/src/attributes/key.c index 84bbf7c04..904df32be 100644 --- a/src/attributes/key.c +++ b/src/attributes/key.c @@ -30,8 +30,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static void a_writekeys(const attrib *a, const void *o, storage *store) { int i, *keys = (int *)a->data.v; - for (i = 0; i <= keys[0]; ++i) { - WRITE_INT(store, keys[i]); + 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]); } } @@ -42,10 +44,16 @@ static int a_readkeys(attrib * a, void *owner, gamedata *data) { if (i == 0) { return AT_READ_FAIL; } - a->data.v = p = malloc(sizeof(int)*(i + 1)); + a->data.v = p = malloc(sizeof(int)*(i*2 + 1)); *p++ = i; while (i--) { READ_INT(data->store, p++); + if (data->version >= KEYVAL_VERSION) { + READ_INT(data->store, p++); + } + else { + *p++ = 1; + } } return AT_READ_OK; } @@ -71,24 +79,25 @@ attrib_type at_keys = { static void a_upgradekeys(attrib **alist, attrib *abegin) { int n = 0, *keys = 0; - int i = 0, val[4]; + int i = 0, val[8]; attrib *a, *ak = a_find(*alist, &at_keys); if (ak) { keys = (int *)ak->data.v; if (keys) n = keys[0]; } for (a = abegin; a && a->type == abegin->type; a = a->next) { - val[i++] = a->data.i; - if (i == 4) { - keys = realloc(keys, sizeof(int) * (n + i + 1)); - memcpy(keys + n + 1, val, sizeof(int)*i); + 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)); n += i; i = 0; } } if (i > 0) { - keys = realloc(keys, sizeof(int) * (n + i + 1)); - memcpy(keys + n + 1, val, sizeof(int)*i); + keys = realloc(keys, sizeof(int) * (2 * (n + i) + 1)); + memcpy(keys + 2 * n + 1, val, sizeof(int)*i*2); if (!ak) { ak = a_add(alist, a_new(&at_keys)); } @@ -107,9 +116,9 @@ attrib_type at_key = { a_upgradekeys }; -void key_set(attrib ** alist, int key) +void key_set(attrib ** alist, int key, int val) { - int *keys, n = 1; + int *keys, n = 0; attrib *a; assert(key != 0); a = a_find(*alist, &at_keys); @@ -118,12 +127,13 @@ void key_set(attrib ** alist, int key) } keys = (int *)a->data.v; if (keys) { - n = keys[0] + 1; + n = keys[0]; } - keys = realloc(keys, sizeof(int) *(n + 1)); - // TODO: does insertion sort pay off here? - keys[0] = n; - keys[n] = key; + 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; a->data.v = keys; } @@ -135,29 +145,31 @@ void key_unset(attrib ** alist, int key) if (a) { int i, *keys = (int *)a->data.v; if (keys) { - for (i = 1; i <= keys[0]; ++i) { - if (keys[i] == key) { - keys[i] = keys[keys[0]]; + 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)); keys[0]--; + break; } } } } } -bool key_get(attrib *alist, int key) { +int key_get(attrib *alist, int key) { attrib *a; assert(key != 0); a = a_find(alist, &at_keys); if (a) { int i, *keys = (int *)a->data.v; if (keys) { - for (i = 1; i <= keys[0]; ++i) { - if (keys[i] == key) { - return true; + for (i = 0; i != keys[0]; ++i) { + if (keys[i*2+1] == key) { + return keys[i * 2 + 2]; } } } } - return false; + return 0; } diff --git a/src/attributes/key.h b/src/attributes/key.h index d2cea1ff0..81511d248 100644 --- a/src/attributes/key.h +++ b/src/attributes/key.h @@ -26,12 +26,12 @@ extern "C" { #endif struct attrib; struct attrib_type; - extern struct attrib_type at_key; + extern struct attrib_type at_key; // DEPRECATED: at_key has been replaced with at_keys extern struct attrib_type at_keys; - void key_set(struct attrib **alist, int key); + void key_set(struct attrib **alist, int key, int value); void key_unset(struct attrib **alist, int key); - bool key_get(struct attrib *alist, int key); + int key_get(struct attrib *alist, int key); #ifdef __cplusplus } diff --git a/src/attributes/key.test.c b/src/attributes/key.test.c index 2b7632917..b816e1d86 100644 --- a/src/attributes/key.test.c +++ b/src/attributes/key.test.c @@ -1,22 +1,24 @@ #include #include "key.h" +#include "dict.h" #include +#include #include #include static void test_get_set_keys(CuTest *tc) { attrib *a = 0; - key_set(&a, 42); - key_set(&a, 43); - key_set(&a, 44); - CuAssertTrue(tc, key_get(a, 42)); - CuAssertTrue(tc, key_get(a, 43)); - CuAssertTrue(tc, key_get(a, 44)); + key_set(&a, 42, 1); + key_set(&a, 43, 2); + key_set(&a, 44, 3); + CuAssertIntEquals(tc, 1, key_get(a, 42)); + CuAssertIntEquals(tc, 2, key_get(a, 43)); + CuAssertIntEquals(tc, 3, key_get(a, 44)); key_unset(&a, 42); - CuAssertTrue(tc, !key_get(a, 42)); - CuAssertTrue(tc, key_get(a, 43)); - CuAssertTrue(tc, key_get(a, 44)); + CuAssertIntEquals(tc, 0, key_get(a, 42)); + CuAssertIntEquals(tc, 2, key_get(a, 43)); + CuAssertIntEquals(tc, 3, key_get(a, 44)); a_removeall(&a, NULL); } @@ -26,7 +28,7 @@ static attrib *key_set_orig(attrib **alist, int key) { return a; } -static void test_upgrade(CuTest *tc) { +static void test_upgrade_key(CuTest *tc) { attrib *alist = 0; key_set_orig(&alist, 40); key_set_orig(&alist, 41); @@ -35,18 +37,31 @@ static void test_upgrade(CuTest *tc) { key_set_orig(&alist, 44); CuAssertPtrNotNull(tc, alist->type->upgrade); alist->type->upgrade(&alist, alist); - CuAssertTrue(tc, key_get(alist, 40)); - CuAssertTrue(tc, key_get(alist, 41)); - CuAssertTrue(tc, key_get(alist, 42)); - CuAssertTrue(tc, key_get(alist, 43)); - CuAssertTrue(tc, key_get(alist, 44)); + CuAssertIntEquals(tc, 1, key_get(alist, 40)); + CuAssertIntEquals(tc, 1, key_get(alist, 41)); + CuAssertIntEquals(tc, 1, key_get(alist, 42)); + CuAssertIntEquals(tc, 1, key_get(alist, 43)); + CuAssertIntEquals(tc, 1, key_get(alist, 44)); a_removeall(&alist, NULL); } +static void test_upgrade_dict(CuTest *tc) { + attrib *a; + + a = a_new(&at_dict); + + dict_set(a, "embassy_muschel", 42); + CuAssertPtrNotNull(tc, a->type->upgrade); + a->type->upgrade(&a, a); + CuAssertIntEquals(tc, 42, key_get(a, atoi36("mupL"))); + a_removeall(&a, NULL); +} + CuSuite *get_key_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_get_set_keys); - SUITE_ADD_TEST(suite, test_upgrade); + SUITE_ADD_TEST(suite, test_upgrade_key); + SUITE_ADD_TEST(suite, test_upgrade_dict); return suite; } diff --git a/src/bind_faction.c b/src/bind_faction.c index 749bb65f6..371481fcb 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -248,7 +248,7 @@ static int tolua_faction_getkey(lua_State * L) const char *name = tolua_tostring(L, 2, 0); int flag = atoi36(name); - lua_pushboolean(L, key_get(self->attribs, flag)); + lua_pushinteger(L, key_get(self->attribs, flag)); return 1; } @@ -256,11 +256,11 @@ static int tolua_faction_setkey(lua_State * L) { faction *self = (faction *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); - int value = tolua_toboolean(L, 3, 0); + int value = (int)tolua_tonumber(L, 3, 0); int flag = atoi36(name); if (value) { - key_set(&self->attribs, flag); + key_set(&self->attribs, flag, value); } else { key_unset(&self->attribs, flag); diff --git a/src/bind_region.c b/src/bind_region.c index d9956c8b6..51a5bedd4 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -544,11 +544,11 @@ static int tolua_region_setkey(lua_State * L) { region *self = (region *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); - int value = tolua_toboolean(L, 3, 0); + int value = (int)tolua_tonumber(L, 3, 0); int flag = atoi36(name); if (value) { - key_set(&self->attribs, flag); + key_set(&self->attribs, flag, value); } else { key_unset(&self->attribs, flag); diff --git a/src/bind_unit.c b/src/bind_unit.c index c0939b5cf..a9d413bf0 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -786,7 +786,7 @@ static int tolua_unit_set_flag(lua_State * L) int value = (int)tolua_tonumber(L, 3, 0); int flag = atoi36(name); if (value) { - key_set(&self->attribs, flag); + key_set(&self->attribs, flag, value); } else { key_unset(&self->attribs, flag); diff --git a/src/bindings.c b/src/bindings.c index 21f2aad58..531b68618 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -206,10 +206,10 @@ static int tolua_translate(lua_State * L) static int tolua_setkey(lua_State * L) { const char *name = tolua_tostring(L, 1, 0); - int value = tolua_toboolean(L, 2, 0); + int value = (int)tolua_tonumber(L, 3, 0); int flag = atoi36(name); if (value) { - key_set(&global.attribs, flag); + key_set(&global.attribs, flag, value); } else { key_unset(&global.attribs, flag); diff --git a/src/kernel/group.test.c b/src/kernel/group.test.c index 78ee6b24f..e95458114 100644 --- a/src/kernel/group.test.c +++ b/src/kernel/group.test.c @@ -84,7 +84,7 @@ static void test_group_readwrite(CuTest * tc) f = test_create_faction(0); new_group(f, "NW", 42); g = new_group(f, "Egoisten", 43); - key_set(&g->attribs, 44); + key_set(&g->attribs, 44, 44); al = ally_add(&g->allies, f); al->status = HELP_GIVE; write_groups(&store, f); @@ -108,7 +108,7 @@ static void test_group_readwrite(CuTest * tc) CuAssertStrEquals(tc, "Egoisten", f->groups->next->name); CuAssertPtrEquals(tc, 0, f->groups->allies); g = f->groups->next; - CuAssertTrue(tc, key_get(g->attribs, 44)); + CuAssertIntEquals(tc, 44, key_get(g->attribs, 44)); CuAssertPtrNotNull(tc, g->allies); CuAssertPtrEquals(tc, 0, g->allies->next); CuAssertPtrEquals(tc, f, g->allies->faction); diff --git a/src/kernel/item.c b/src/kernel/item.c index 34a0e1875..9327700cd 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -939,7 +939,7 @@ struct order *ord) use_pooled(user, itype->rtype, GET_SLACK | GET_RESERVE | GET_POOLED_SLACK, user->number); - key_set(&f->attribs, atoi36("mbst")); + key_set(&f->attribs, atoi36("mbst"), turn); set_level(user, SK_MAGIC, 3); ADDMSG(&user->faction->msgs, msg_message("use_item", diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index c05ce4455..70bcb2e65 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -211,8 +211,8 @@ static void test_readwrite_attrib(CuTest *tc) { attrib *a = NULL; test_setup(); - key_set(&a, 41); - key_set(&a, 42); + key_set(&a, 41, 42); + key_set(&a, 42, 43); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_attribs(data.store, a, NULL); @@ -223,8 +223,8 @@ static void test_readwrite_attrib(CuTest *tc) { read_attribs(&data, &a, NULL); mstream_done(&data.strm); gamedata_done(&data); - CuAssertTrue(tc, key_get(a, 41)); - CuAssertTrue(tc, key_get(a, 42)); + CuAssertIntEquals(tc, 42, key_get(a, 41)); + CuAssertIntEquals(tc, 43, key_get(a, 42)); a_removeall(&a, NULL); test_cleanup(); diff --git a/src/util/gamedata.h b/src/util/gamedata.h index bb1632838..75a750452 100644 --- a/src/util/gamedata.h +++ b/src/util/gamedata.h @@ -32,10 +32,11 @@ #define NOCRYPT_VERSION 352 /* passwords are plaintext again */ #define ATHASH_VERSION 353 /* attribute-type hash, not name */ #define NOWATCH_VERSION 354 /* plane->watchers is gone */ +#define KEYVAL_VERSION 355 /* at_keys has values */ /* unfinished: */ #define CRYPT_VERSION 400 /* passwords are encrypted */ -#define RELEASE_VERSION NOWATCH_VERSION /* current datafile */ +#define RELEASE_VERSION KEYVAL_VERSION /* current datafile */ #define MIN_VERSION INTPAK_VERSION /* minimal datafile we support */ #define MAX_VERSION RELEASE_VERSION /* change this if we can need to read the future datafile, and we can do so */