From 10e78b1455147442a4334a3f6c845545ba2c0244 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 22:15:21 +0100 Subject: [PATCH] remove code for dict and lua .objects properties. rewrite muschelplateau code to use keys. --- scripts/eressea/embassy.lua | 18 +--- src/CMakeLists.txt | 1 - src/attributes/dict.c | 179 +++++++++------------------------- src/attributes/dict.h | 7 -- src/attributes/key.c | 2 +- src/bind_dict.c | 185 ------------------------------------ src/bind_dict.h | 26 ----- src/bind_faction.c | 40 ++++++-- src/bind_region.c | 13 +-- src/bind_unit.c | 8 -- src/bindings.c | 2 - 11 files changed, 79 insertions(+), 402 deletions(-) delete mode 100644 src/bind_dict.c delete mode 100644 src/bind_dict.h diff --git a/scripts/eressea/embassy.lua b/scripts/eressea/embassy.lua index 82bce62c2..704581c6f 100644 --- a/scripts/eressea/embassy.lua +++ b/scripts/eressea/embassy.lua @@ -1,19 +1,5 @@ -- Muschelplateau --- global exports (use item) -function use_seashell(u, amount) --- Muschelplateau... - local visit = u.faction.objects:get("embassy_muschel") - 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 - if not config.embassy then return nil end local embassy = {} @@ -34,10 +20,10 @@ function embassy.update() eressea.log.debug("updating embassies in " .. tostring(home)) local u for u in home.units do - if u.faction.objects:get('embassy_muschel')==nil then + if not u.faction:get_key('mupL') then if (u.faction:add_item('seashell', 1)>0) then eressea.log.debug("new seashell for " .. tostring(u.faction)) - u.faction.objects:set('embassy_muschel', get_turn()) + u.faction:set_key('mupL', get_turn()) end end end diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a65cb8912..b4d3614e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,7 +148,6 @@ set(SERVER_SRC bind_locale.c bind_eressea.c bind_faction.c - bind_dict.c bind_order.c bindings.c bind_message.c diff --git a/src/attributes/dict.c b/src/attributes/dict.c index a3f034145..6d4b3ed3c 100644 --- a/src/attributes/dict.c +++ b/src/attributes/dict.c @@ -19,6 +19,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include "dict.h" +#include "key.h" /* kernel includes */ #include @@ -29,6 +30,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* util includes */ #include +#include +#include #include #include @@ -54,46 +57,6 @@ typedef struct dict_data { } data; } dict_data; -static void -dict_write(const attrib * a, const void *owner, struct storage *store) -{ - const dict_data *data = (dict_data *)a->data.v; - int type = (int)data->type; - WRITE_TOK(store, data->name); - WRITE_INT(store, type); - switch (data->type) { - case TINTEGER: - WRITE_INT(store, data->data.i); - break; - case TREAL: - WRITE_FLT(store, (float)data->data.real); - break; - case TSTRING: - WRITE_STR(store, data->data.str); - break; - case TUNIT: - write_unit_reference(data->data.u, store); - break; - case TFACTION: - write_faction_reference(data->data.f, store); - break; - case TBUILDING: - write_building_reference(data->data.b, store); - break; - case TSHIP: - /* write_ship_reference(data->data.sh, store); */ - assert(!"not implemented"); - break; - case TREGION: - write_region_reference(data->data.r, store); - break; - case TNONE: - break; - default: - assert(!"illegal type in object-attribute"); - } -} - static int dict_read(attrib * a, void *owner, gamedata *data) { storage *store = data->store; @@ -164,7 +127,7 @@ static int dict_read(attrib * a, void *owner, gamedata *data) default: return AT_READ_FAIL; } - return AT_READ_OK; + return AT_READ_DEPR; } static void dict_init(attrib * a) @@ -184,100 +147,46 @@ static void dict_done(attrib * a) free(a->data.v); } +static void dict_upgrade(attrib **alist, attrib *abegin) { + int n = 0, *keys = 0; + int i = 0, val[4]; + 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) { + dict_data *dd = (dict_data *)a->data.v; + if (dd->type != TINTEGER) { + log_error("dict conversion, bad type %d for %s", dd->type, dd->name); + } + else { + if (strcmp(dd->name, "embassy_muschel")==0) { + val[i++] = atoi36("mupL"); + } + else { + log_error("dict conversion, bad entry %s", dd->name); + } + } + if (i == 4) { + keys = realloc(keys, sizeof(int) * (n + i + 1)); + memcpy(keys + n + 1, val, sizeof(int)*i); + n += i; + i = 0; + } + } + if (i > 0) { + keys = realloc(keys, sizeof(int) * (n + i + 1)); + memcpy(keys + n + 1, val, sizeof(int)*i); + if (!ak) { + ak = a_add(alist, a_new(&at_keys)); + } + } + ak->data.v = keys; + keys[0] = n + i; +} + attrib_type at_dict = { "object", dict_init, dict_done, NULL, - dict_write, dict_read + NULL, dict_read, dict_upgrade }; - -const char *dict_name(const attrib * a) -{ - dict_data *data = (dict_data *)a->data.v; - return data->name; -} - -struct attrib *dict_create(const char *name, dict_type type, variant value) -{ - attrib *a = a_new(&at_dict); - dict_data *data = (dict_data *)a->data.v; - data->name = strdup(name); - - dict_set(a, type, value); - return a; -} - -void dict_set(attrib * a, dict_type type, variant value) -{ - dict_data *data = (dict_data *)a->data.v; - - if (data->type == TSTRING) - free(data->data.str); - data->type = type; - switch (type) { - case TSTRING: - data->data.str = value.v ? strdup(value.v) : NULL; - break; - case TINTEGER: - data->data.i = value.i; - break; - case TREAL: - data->data.real = value.f; - break; - case TREGION: - data->data.r = (region *)value.v; - break; - case TBUILDING: - data->data.b = (building *)value.v; - break; - case TFACTION: - data->data.f = (faction *)value.v; - break; - case TUNIT: - data->data.u = (unit *)value.v; - break; - case TSHIP: - data->data.sh = (ship *)value.v; - break; - case TNONE: - break; - default: - assert(!"invalid object-type"); - break; - } -} - -void dict_get(const struct attrib *a, dict_type * type, variant * value) -{ - dict_data *data = (dict_data *)a->data.v; - *type = data->type; - switch (data->type) { - case TSTRING: - value->v = data->data.str; - break; - case TINTEGER: - value->i = data->data.i; - break; - case TREAL: - value->f = (float)data->data.real; - break; - case TREGION: - value->v = data->data.r; - break; - case TBUILDING: - value->v = data->data.b; - break; - case TFACTION: - value->v = data->data.f; - break; - case TUNIT: - value->v = data->data.u; - break; - case TSHIP: - value->v = data->data.sh; - break; - case TNONE: - break; - default: - assert(!"invalid object-type"); - break; - } -} diff --git a/src/attributes/dict.h b/src/attributes/dict.h index 5b282779e..7b028fa00 100644 --- a/src/attributes/dict.h +++ b/src/attributes/dict.h @@ -26,13 +26,6 @@ extern "C" { extern struct attrib_type at_dict; - struct attrib *dict_create(const char *name, dict_type type, - variant value); - void dict_get(const struct attrib *a, dict_type * type, - variant * value); - void dict_set(struct attrib *a, dict_type type, variant value); - const char *dict_name(const struct attrib *a); - #ifdef __cplusplus } #endif diff --git a/src/attributes/key.c b/src/attributes/key.c index 0b252e0c0..ef4b79ad5 100644 --- a/src/attributes/key.c +++ b/src/attributes/key.c @@ -66,7 +66,7 @@ attrib_type at_keys = { NULL }; -void a_upgradekeys(attrib **alist, attrib *abegin) { +static void a_upgradekeys(attrib **alist, attrib *abegin) { int n = 0, *keys = 0; int i = 0, val[4]; attrib *a, *ak = a_find(*alist, &at_keys); diff --git a/src/bind_dict.c b/src/bind_dict.c deleted file mode 100644 index 40f73690f..000000000 --- a/src/bind_dict.c +++ /dev/null @@ -1,185 +0,0 @@ -/* -+-------------------+ -| | Enno Rehling -| Eressea PBEM host | Christian Schlittchen -| (c) 1998 - 2008 | Katja Zedel -| | Henning Peters -+-------------------+ - -This program may not be used, modified or distributed -without prior permission by the authors of Eressea. -*/ - -#include -#include "bind_dict.h" - -#include -#include -#include -#include - -#include - -#include -#include - -#include -#include - -#include -#include - -static int tolua_dict_get(lua_State * L) -{ - dict self = (dict)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); - attrib *a = a_find(*self, &at_dict); - - for (; a && a->type == &at_dict; a = a->next) { - const char *obj_name = dict_name(a); - if (obj_name && name && strcmp(obj_name, name) == 0) { - variant val; - dict_type type; - - dict_get(a, &type, &val); - switch (type) { - case TNONE: - lua_pushnil(L); - break; - case TINTEGER: - lua_pushinteger(L, val.i); - break; - case TREAL: - lua_pushnumber(L, (lua_Number)val.f); - break; - case TREGION: - tolua_pushusertype(L, val.v, TOLUA_CAST "region"); - break; - case TBUILDING: - tolua_pushusertype(L, val.v, TOLUA_CAST "building"); - break; - case TUNIT: - tolua_pushusertype(L, val.v, TOLUA_CAST "unit"); - break; - case TSHIP: - tolua_pushusertype(L, val.v, TOLUA_CAST "ship"); - break; - case TSTRING: - tolua_pushstring(L, (const char *)val.v); - break; - default: - assert(!"not implemented"); - } - return 1; - } - } - lua_pushnil(L); - return 1; -} - -static int tolua_dict_set_number(lua_State * L) -{ - dict self = (dict)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); - lua_Number value = tolua_tonumber(L, 3, 0); - attrib *a = a_find(*self, &at_dict); - variant val; - - val.f = (float)value; - - for (; a && a->type == &at_dict; a = a->next) { - if (strcmp(dict_name(a), name) == 0) { - dict_set(a, TREAL, val); - return 0; - } - } - - a = a_add(self, dict_create(name, TREAL, val)); - return 0; -} - -static int tolua_dict_set_string(lua_State * L) -{ - dict self = (dict)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); - const char *value = tolua_tostring(L, 3, 0); - attrib *a = a_find(*self, &at_dict); - variant val; - - val.v = strdup(value); - - for (; a && a->type == &at_dict; a = a->next) { - if (strcmp(dict_name(a), name) == 0) { - dict_set(a, TSTRING, val); - return 0; - } - } - - a = a_add(self, dict_create(name, TSTRING, val)); - return 0; -} - -static int tolua_dict_set_usertype(lua_State * L, int type) -{ - dict self = (dict)tolua_tousertype(L, 1, 0); - const char *name = tolua_tostring(L, 2, 0); - unit *value = tolua_tousertype(L, 3, 0); - attrib *a = a_find(*self, &at_dict); - variant val; - - val.v = value; - - for (; a && a->type == &at_dict; a = a->next) { - if (strcmp(dict_name(a), name) == 0) { - dict_set(a, type, val); - return 0; - } - } - - a = a_add(self, dict_create(name, type, val)); - return 0; -} - -static int tolua_dict_set(lua_State * L) -{ - tolua_Error tolua_err; - if (tolua_isnumber(L, 3, 0, &tolua_err)) { - return tolua_dict_set_number(L); - } - else if (tolua_isusertype(L, 3, TOLUA_CAST "unit", 0, &tolua_err)) { - return tolua_dict_set_usertype(L, TUNIT); - } - else if (tolua_isusertype(L, 3, TOLUA_CAST "faction", 0, &tolua_err)) { - return tolua_dict_set_usertype(L, TFACTION); - } - else if (tolua_isusertype(L, 3, TOLUA_CAST "ship", 0, &tolua_err)) { - return tolua_dict_set_usertype(L, TSHIP); - } - else if (tolua_isusertype(L, 3, TOLUA_CAST "building", 0, &tolua_err)) { - return tolua_dict_set_usertype(L, TBUILDING); - } - else if (tolua_isusertype(L, 3, TOLUA_CAST "region", 0, &tolua_err)) { - return tolua_dict_set_usertype(L, TREGION); - } - return tolua_dict_set_string(L); -} - -void tolua_dict_open(lua_State * L) -{ - /* register user types */ - tolua_usertype(L, USERTYPE_DICT); - - tolua_module(L, NULL, 0); - tolua_beginmodule(L, NULL); - { - tolua_cclass(L, USERTYPE_DICT, USERTYPE_DICT, - TOLUA_CAST "", NULL); - tolua_beginmodule(L, USERTYPE_DICT); - { - tolua_function(L, TOLUA_CAST "get", tolua_dict_get); - tolua_function(L, TOLUA_CAST "set", tolua_dict_set); - } - tolua_endmodule(L); - } - tolua_endmodule(L); -} diff --git a/src/bind_dict.h b/src/bind_dict.h deleted file mode 100644 index 83ca1efbf..000000000 --- a/src/bind_dict.h +++ /dev/null @@ -1,26 +0,0 @@ -/* -+-------------------+ -| | Enno Rehling -| Eressea PBEM host | Christian Schlittchen -| (c) 1998 - 2008 | Katja Zedel -| | Henning Peters -+-------------------+ - -This program may not be used, modified or distributed -without prior permission by the authors of Eressea. -*/ - -#ifdef __cplusplus -extern "C" { -#endif - -#define USERTYPE_DICT ((char *)"dict") - - struct lua_State; - void tolua_dict_open(struct lua_State *L); - - typedef struct attrib **dict; - -#ifdef __cplusplus -} -#endif diff --git a/src/bind_faction.c b/src/bind_faction.c index f5c393de6..749bb65f6 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -13,7 +13,6 @@ without prior permission by the authors of Eressea. #include #include "bind_faction.h" #include "bind_unit.h" -#include "bind_dict.h" #include "bindings.h" #include "helpers.h" @@ -27,7 +26,9 @@ without prior permission by the authors of Eressea. #include #include #include +#include +#include #include #include #include @@ -241,6 +242,32 @@ static int tolua_faction_addnotice(lua_State * L) return 0; } +static int tolua_faction_getkey(lua_State * L) +{ + faction *self = (faction *)tolua_tousertype(L, 1, 0); + const char *name = tolua_tostring(L, 2, 0); + int flag = atoi36(name); + + lua_pushboolean(L, key_get(self->attribs, flag)); + return 1; +} + +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 flag = atoi36(name); + + if (value) { + key_set(&self->attribs, flag); + } + else { + key_unset(&self->attribs, flag); + } + return 0; +} + static int tolua_faction_count_msg_type(lua_State *L) { faction *self = (faction *)tolua_tousertype(L, 1, 0); const char *str = tolua_tostring(L, 2, 0); @@ -258,13 +285,6 @@ static int tolua_faction_count_msg_type(lua_State *L) { return 1; } -static int tolua_faction_get_objects(lua_State * L) -{ - faction *self = (faction *)tolua_tousertype(L, 1, 0); - tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT); - return 1; -} - static int tolua_faction_get_policy(lua_State * L) { faction *self = (faction *)tolua_tousertype(L, 1, 0); @@ -618,8 +638,8 @@ void tolua_faction_open(lua_State * L) /* tech debt hack, siehe https://paper.dropbox.com/doc/Weihnachten-2015-5tOx5r1xsgGDBpb0gILrv#:h=Probleme-mit-Tests-(Nachtrag-0 */ tolua_function(L, TOLUA_CAST "count_msg_type", tolua_faction_count_msg_type); - tolua_variable(L, TOLUA_CAST "objects", tolua_faction_get_objects, - NULL); + tolua_function(L, TOLUA_CAST "get_key", tolua_faction_getkey); + tolua_function(L, TOLUA_CAST "set_key", tolua_faction_setkey); } tolua_endmodule(L); } diff --git a/src/bind_region.c b/src/bind_region.c index 2e328dc96..d9956c8b6 100644 --- a/src/bind_region.c +++ b/src/bind_region.c @@ -14,7 +14,6 @@ without prior permission by the authors of Eressea. #include "bind_region.h" #include "bind_unit.h" #include "bind_ship.h" -#include "bind_dict.h" #include "bind_building.h" #include "chaos.h" @@ -36,10 +35,11 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include +#include + #include #include @@ -434,13 +434,6 @@ static int tolua_region_set_resource(lua_State * L) return 0; } -static int tolua_region_get_objects(lua_State * L) -{ - region *self = (region *)tolua_tousertype(L, 1, 0); - tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT); - return 1; -} - static int tolua_region_destroy(lua_State * L) { region *self = (region *)tolua_tousertype(L, 1, 0); @@ -733,8 +726,6 @@ void tolua_region_open(lua_State * L) tolua_function(L, TOLUA_CAST "get_key", tolua_region_getkey); tolua_function(L, TOLUA_CAST "set_key", tolua_region_setkey); - - tolua_variable(L, TOLUA_CAST "objects", tolua_region_get_objects, 0); } tolua_endmodule(L); diff --git a/src/bind_unit.c b/src/bind_unit.c index 1983e0f62..c0939b5cf 100755 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -13,7 +13,6 @@ without prior permission by the authors of Eressea. #include #include "bind_unit.h" -#include "bind_dict.h" #include "alchemy.h" #include "bindings.h" #include "move.h" @@ -67,12 +66,6 @@ static int tolua_bufunit(lua_State * L) { return 1; } -static int tolua_unit_get_objects(lua_State * L) -{ - unit *self = (unit *)tolua_tousertype(L, 1, 0); - tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT); - return 1; -} int tolua_unitlist_nextf(lua_State * L) { @@ -1013,7 +1006,6 @@ void tolua_unit_open(lua_State * L) tolua_unit_set_race); tolua_variable(L, TOLUA_CAST "hp_max", &tolua_unit_get_hpmax, 0); - tolua_variable(L, TOLUA_CAST "objects", &tolua_unit_get_objects, 0); tolua_function(L, TOLUA_CAST "show", &tolua_bufunit); } tolua_endmodule(L); diff --git a/src/bindings.c b/src/bindings.c index 1515c74e0..2e4ed3dc6 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -15,7 +15,6 @@ without prior permission by the authors of Eressea. #include "bind_unit.h" #include "bind_storage.h" #include "bind_building.h" -#include "bind_dict.h" #include "bind_message.h" #include "bind_building.h" #include "bind_faction.h" @@ -1143,7 +1142,6 @@ lua_State *lua_init(const dictionary *inifile) { tolua_unit_open(L); tolua_message_open(L); tolua_order_open(L); - tolua_dict_open(L); #ifdef USE_CURSES tolua_gmtool_open(L); #endif