From bd836b76e10f2e4dbd8d913d4129b94c32026866 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Feb 2017 16:55:51 +0100 Subject: [PATCH 01/21] channel XML race.parameters through a single, tested, function --- src/battle.test.c | 4 ++-- src/economy.c | 5 +---- src/kernel/faction.c | 13 +++++++++---- src/kernel/faction.test.c | 17 +++++++++++++++++ src/kernel/race.c | 14 ++++++++++++++ src/kernel/race.h | 2 ++ src/kernel/race.test.c | 16 ++++++++++++++++ src/kernel/xmlreader.c | 12 +----------- src/market.test.c | 4 ++-- 9 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/battle.test.c b/src/battle.test.c index e8187c772..946d5fcee 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -225,10 +225,10 @@ static void test_natural_armor(CuTest * tc) set_level(u, SK_STAMINA, 2); CuAssertIntEquals(tc, 0, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 0, natural_armor(u)); - set_param(&rc->parameters, "armor.stamina", "1"); + rc_set_param(rc, "armor.stamina", "1"); CuAssertIntEquals(tc, 1, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 2, natural_armor(u)); - set_param(&rc->parameters, "armor.stamina", "2"); + rc_set_param(rc, "armor.stamina", "2"); CuAssertIntEquals(tc, 2, rc_armor_bonus(rc)); CuAssertIntEquals(tc, 1, natural_armor(u)); test_cleanup(); diff --git a/src/economy.c b/src/economy.c index 2d9fe6666..dcbc7431e 100644 --- a/src/economy.c +++ b/src/economy.c @@ -422,10 +422,7 @@ static void expandrecruit(region * r, request * recruitorders) static int recruit_cost(const faction * f, const race * rc) { - if (is_monsters(f) || f->race == rc) { - return rc->recruitcost; - } - else if (valid_race(f, rc)) { + if (is_monsters(f) || valid_race(f, rc)) { return rc->recruitcost; } return -1; diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 28ac2e203..30a623e44 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -558,15 +558,20 @@ void faction_setpassword(faction * f, const char *pwhash) f->_password = strdup(pwhash); } +const race *other_race(const race *rc) { + if (rc->parameters) { + const char *str = get_param(rc->parameters, "other_race"); + return str ? rc_find(str) : NULL; + } + return NULL; +} + bool valid_race(const struct faction *f, const struct race *rc) { if (f->race == rc) return true; else { - const char *str = get_param(f->race->parameters, "other_race"); - if (str) - return rc_find(str) == rc; - return false; + return other_race(f->race) == rc; } } diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 1b1a0236b..c01731964 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -209,6 +209,22 @@ static void test_max_migrants(CuTest *tc) { test_cleanup(); } +static void test_valid_race(CuTest *tc) { + race * rc1, *rc2; + faction *f; + + test_setup(); + rc1 = test_create_race("human"); + rc2 = test_create_race("elf"); + f = test_create_faction(rc1); + CuAssertTrue(tc, valid_race(f, rc1)); + CuAssertTrue(tc, !valid_race(f, rc2)); + rc_set_param(rc1, "other_race", "elf"); + CuAssertTrue(tc, valid_race(f, rc1)); + CuAssertTrue(tc, valid_race(f, rc2)); + test_cleanup(); +} + CuSuite *get_faction_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -222,5 +238,6 @@ CuSuite *get_faction_suite(void) SUITE_ADD_TEST(suite, test_set_origin); SUITE_ADD_TEST(suite, test_set_origin_bug); SUITE_ADD_TEST(suite, test_check_passwd); + SUITE_ADD_TEST(suite, test_valid_race); return suite; } diff --git a/src/kernel/race.c b/src/kernel/race.c index 7f6fe15c2..0e9371e39 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -299,6 +299,20 @@ int rc_migrants_formula(const race *rc) return (rc->flags&RCF_MIGRANTS) ? MIGRANTS_LOG10 : MIGRANTS_NONE; } +void rc_set_param(struct race *rc, const char *key, const char *value) { + if (strcmp(key, "recruit_multi") == 0) { + rc->recruit_multi = atof(value); + } + else if (strcmp(key, "migrants.formula") == 0) { + if (value[0] == '1') { + rc->flags |= RCF_MIGRANTS; + } + } + else { + set_param(&rc->parameters, key, value); + } +} + const char* rc_name(const race * rc, name_t n, char *name, size_t size) { const char * postfix = 0; if (!rc) { diff --git a/src/kernel/race.h b/src/kernel/race.h index 67eea00f4..b500ed6a1 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -184,6 +184,8 @@ extern "C" { const char * rc_name_s(const race *rc, name_t n); const char * rc_name(const race *rc, name_t n, char *name, size_t size); + void rc_set_param(struct race *rc, const char *key, const char *value); + double rc_magres(const struct race *rc); double rc_maxaura(const struct race *rc); int rc_armor_bonus(const struct race *rc); diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index b730676a4..132457cb4 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -84,6 +84,21 @@ static void test_old_race(CuTest *tc) test_cleanup(); } +static void test_rc_set_param(CuTest *tc) { + race *rc; + test_setup(); + rc = test_create_race("human"); + CuAssertPtrEquals(tc, NULL, rc->parameters); + rc_set_param(rc, "hodor", "HODOR"); + CuAssertStrEquals(tc, "HODOR", get_param(rc->parameters, "hodor")); + rc_set_param(rc, "recruit_multi", "0.5"); + CuAssertDblEquals(tc, 0.5, rc->recruit_multi, 0.0); + rc_set_param(rc, "migrants.formula", "1"); + CuAssertIntEquals(tc, RCF_MIGRANTS, rc->flags&RCF_MIGRANTS); + CuAssertIntEquals(tc, MIGRANTS_LOG10, rc_migrants_formula(rc)); + test_cleanup(); +} + CuSuite *get_race_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -92,6 +107,7 @@ CuSuite *get_race_suite(void) SUITE_ADD_TEST(suite, test_rc_name); SUITE_ADD_TEST(suite, test_rc_defaults); SUITE_ADD_TEST(suite, test_rc_find); + SUITE_ADD_TEST(suite, test_rc_set_param); return suite; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index a0172313a..c07e19809 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1734,17 +1734,7 @@ static int parse_races(xmlDocPtr doc) else if (strcmp((const char *)child->name, "param") == 0) { xmlChar *propName = xmlGetProp(child, BAD_CAST "name"); xmlChar *propValue = xmlGetProp(child, BAD_CAST "value"); - if (strcmp((const char *)propName, "recruit_multi")==0) { - rc->recruit_multi = atof((const char *)propValue); - } - else if (strcmp((const char *)propName, "migrants.formula") == 0) { - if (propValue[0] == '1') { - rc->flags |= RCF_MIGRANTS; - } - } - else { - set_param(&rc->parameters, (const char *)propName, (const char *)propValue); - } + rc_set_param(rc, (const char *)propName, (const char *)propValue); xmlFree(propName); xmlFree(propValue); } diff --git a/src/market.test.c b/src/market.test.c index 1e0195a57..b4d5f05f5 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -86,8 +86,8 @@ static void test_rc_trade(CuTest *tc) { rc = test_create_race("human"); CuAssertIntEquals(tc, 1000, rc_luxury_trade(rc)); CuAssertIntEquals(tc, 500, rc_herb_trade(rc)); - set_param(&rc->parameters, "luxury_trade", "100"); - set_param(&rc->parameters, "herb_trade", "50"); + rc_set_param(rc, "luxury_trade", "100"); + rc_set_param(rc, "herb_trade", "50"); CuAssertIntEquals(tc, 100, rc_luxury_trade(rc)); CuAssertIntEquals(tc, 50, rc_herb_trade(rc)); test_cleanup(); From f2ed2c892ab535b2567a39009d3825cbd0a1a0e6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Feb 2017 18:38:53 +0100 Subject: [PATCH 02/21] bugfix: Drachen "scare" attribut muss mit MOD, nicht AND angewendet werden. rng_int() & 400 ist eine bekloppte Rechnung. --- src/kernel/race.c | 10 +++++++++- src/kernel/race.h | 1 + src/monsters.c | 18 +++++++++--------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/kernel/race.c b/src/kernel/race.c index 0e9371e39..4dc347751 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -48,6 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* attrib includes */ +#include #include /* libc includes */ @@ -290,10 +291,17 @@ double rc_maxaura(const race *rc) { return rc->maxaura / 100.0; } -int rc_armor_bonus(const race *rc) { +int rc_armor_bonus(const race *rc) +{ return get_param_int(rc->parameters, "armor.stamina", 0); } +int rc_scare(const struct race *rc) +{ + attrib *a = a_find(rc->attribs, &at_scare); + return a ? a->data.i : 0; +} + int rc_migrants_formula(const race *rc) { return (rc->flags&RCF_MIGRANTS) ? MIGRANTS_LOG10 : MIGRANTS_NONE; diff --git a/src/kernel/race.h b/src/kernel/race.h index b500ed6a1..6a4feb269 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -189,6 +189,7 @@ extern "C" { double rc_magres(const struct race *rc); double rc_maxaura(const struct race *rc); int rc_armor_bonus(const struct race *rc); + int rc_scare(const struct race *rc); #define MIGRANTS_NONE 0 #define MIGRANTS_LOG10 1 diff --git a/src/monsters.c b/src/monsters.c index bba1cbd50..a7678c701 100644 --- a/src/monsters.c +++ b/src/monsters.c @@ -31,7 +31,6 @@ #include "study.h" /* attributes includes */ -#include #include #include @@ -1013,11 +1012,11 @@ static void eaten_by_monster(unit * u) int horse = -1; const resource_type *rhorse = get_resourcetype(R_HORSE); const race *rc = u_race(u); - attrib *a; + int scare; - a = a_find(rc->attribs, &at_scare); - if (a) { - n = rng_int() & a->data.i * u->number; + scare = rc_scare(rc); + if (scare>0) { + n = rng_int() % scare * u->number; } else { n = rng_int() % (u->number / 20 + 1); horse = 0; @@ -1093,10 +1092,11 @@ static void scared_by_monster(unit * u) { int n; const race *rc = u_race(u); - attrib *a; - a = a_find(rc->attribs, &at_scare); - if (a) { - n = rng_int() & a->data.i * u->number; + int scare; + + scare = rc_scare(rc); + if (scare>0) { + n = rng_int() % scare * u->number; } else { n = rng_int() % (u->number / 4 + 1); } From 67414f29ebf6af99a084e315d086bf429cd646cd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Feb 2017 19:26:07 +0100 Subject: [PATCH 03/21] wrap ai.scare and at_scare in rc_set_param --- src/kernel/race.c | 5 +++++ src/kernel/race.test.c | 2 ++ src/kernel/xmlreader.c | 13 ++++++------- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/kernel/race.c b/src/kernel/race.c index 4dc347751..b7c52d169 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -311,6 +311,11 @@ void rc_set_param(struct race *rc, const char *key, const char *value) { if (strcmp(key, "recruit_multi") == 0) { rc->recruit_multi = atof(value); } + else if (strcmp(key, "ai.scare") == 0) { + attrib *a = a_new(&at_scare); + a->data.i = atoi(value); + a_add(&rc->attribs, a); + } else if (strcmp(key, "migrants.formula") == 0) { if (value[0] == '1') { rc->flags |= RCF_MIGRANTS; diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index 132457cb4..857e8696a 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -96,6 +96,8 @@ static void test_rc_set_param(CuTest *tc) { rc_set_param(rc, "migrants.formula", "1"); CuAssertIntEquals(tc, RCF_MIGRANTS, rc->flags&RCF_MIGRANTS); CuAssertIntEquals(tc, MIGRANTS_LOG10, rc_migrants_formula(rc)); + rc_set_param(rc, "ai.scare", "400"); + CuAssertIntEquals(tc, 400, rc_scare(rc)); test_cleanup(); } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index c07e19809..0864df9a6 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1575,13 +1575,12 @@ static int parse_spells(xmlDocPtr doc) static void parse_ai(race * rc, xmlNodePtr node) { - int n; - - n = xml_ivalue(node, "scare", 0); - if (n>0) { - attrib *a = a_new(&at_scare); - a->data.i = n; - a_add(&rc->attribs, a); + xmlChar *propValue; + + propValue = xmlGetProp(node, BAD_CAST "scare"); + if (propValue) { + rc_set_param(rc, "ai.scare", (const char *)propValue); + xmlFree(propValue); } rc->splitsize = xml_ivalue(node, "splitsize", 0); rc->aggression = (float)xml_fvalue(node, "aggression", 0.04); From 492aba543bb79847268d54c7d4247d9a4a8d0a8e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Feb 2017 19:30:09 +0100 Subject: [PATCH 04/21] delete at_scare delete dead code (race.attribs has no at_skillmod) delete race.attribs --- src/attributes/attributes.c | 5 ----- src/attributes/attributes.h | 1 - src/battle.c | 4 ---- src/kernel/race.c | 9 +-------- src/kernel/race.h | 1 - 5 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/attributes/attributes.c b/src/attributes/attributes.c index 8e9b22139..42f5247fe 100644 --- a/src/attributes/attributes.c +++ b/src/attributes/attributes.c @@ -58,10 +58,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include -attrib_type at_scare = { // monster scares peasants - "scare", NULL, NULL, NULL, a_writeint, a_readint -}; - attrib_type at_unitdissolve = { "unitdissolve", NULL, NULL, NULL, a_writechars, a_readchars }; @@ -78,7 +74,6 @@ static int read_ext(attrib * a, void *owner, gamedata *data) void register_attributes(void) { /* Alle speicherbaren Attribute müssen hier registriert werden */ - at_register(&at_scare); at_register(&at_shiptrail); at_register(&at_familiar); at_register(&at_familiarmage); diff --git a/src/attributes/attributes.h b/src/attributes/attributes.h index 91184ad98..4ec3150ec 100644 --- a/src/attributes/attributes.h +++ b/src/attributes/attributes.h @@ -23,7 +23,6 @@ extern "C" { #endif struct attrib_type; - extern struct attrib_type at_scare; extern void register_attributes(void); #ifdef __cplusplus diff --git a/src/battle.c b/src/battle.c index 9316d35dd..04e72d92c 100644 --- a/src/battle.c +++ b/src/battle.c @@ -760,10 +760,6 @@ bool missile) if (is_riding(t) && (wtype == NULL || (fval(wtype, WTF_HORSEBONUS) && !fval(wtype, WTF_MISSILE)))) { skill += CavalryBonus(tu, enemy, BONUS_SKILL); - if (wtype) - skill = - skillmod(u_race(tu)->attribs, tu, tu->region, wtype->skill, skill, - SMF_RIDING); } if (t.index < tf->elvenhorses) { diff --git a/src/kernel/race.c b/src/kernel/race.c index b7c52d169..9a5ce84c2 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -48,7 +48,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include /* attrib includes */ -#include #include /* libc includes */ @@ -298,8 +297,7 @@ int rc_armor_bonus(const race *rc) int rc_scare(const struct race *rc) { - attrib *a = a_find(rc->attribs, &at_scare); - return a ? a->data.i : 0; + return get_param_int(rc->parameters, "ai.scare", 0); } int rc_migrants_formula(const race *rc) @@ -311,11 +309,6 @@ void rc_set_param(struct race *rc, const char *key, const char *value) { if (strcmp(key, "recruit_multi") == 0) { rc->recruit_multi = atof(value); } - else if (strcmp(key, "ai.scare") == 0) { - attrib *a = a_new(&at_scare); - a->data.i = atoi(value); - a_add(&rc->attribs, a); - } else if (strcmp(key, "migrants.formula") == 0) { if (value[0] == '1') { rc->flags |= RCF_MIGRANTS; diff --git a/src/kernel/race.h b/src/kernel/race.h index 6a4feb269..906b56808 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -154,7 +154,6 @@ extern "C" { void(*init_familiar) (struct unit *); const struct race *familiars[MAXMAGIETYP]; - struct attrib *attribs; struct race *next; } race; From de10a8ad65a3fcafccd9e34de3938dc4172a350a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 5 Feb 2017 20:48:44 +0100 Subject: [PATCH 05/21] reduce side-effects in snowglobe tests. should no longer be intermittent. --- clibs | 2 +- cmake | 2 +- scripts/tests/xmas.lua | 12 +++--------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/clibs b/clibs index 27c8b3202..f91ef37f0 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 27c8b3202b52766465743c3324fc0b52c5ba4b11 +Subproject commit f91ef37f08c5244bf616f1836c0aa9caaf36805c diff --git a/cmake b/cmake index d88983c7f..f1fb3943a 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit d88983c7ff4bc3a4884a7c3f74e8190bac5eab23 +Subproject commit f1fb3943ace59994d90d71a891b80033dc2700a2 diff --git a/scripts/tests/xmas.lua b/scripts/tests/xmas.lua index 5477e08ab..459984901 100644 --- a/scripts/tests/xmas.lua +++ b/scripts/tests/xmas.lua @@ -46,23 +46,17 @@ function test_snowglobe() local r2 = region.create(1, 0, "ocean") local f = faction.create("snowglobe2@eressea.de", "human", "de") local u = unit.create(f, r1, 1) - local have = 6 local fail = 0 u:add_item("snowglobe", have) - local xform = { ocean = "glacier", glacier = "glacier", firewall = "volcano", volcano = "mountain", desert = "plain", plain = "plain" } - u:clear_orders() - u:add_order("BENUTZEN 1 Schneekugel Ost") + local xform = { ocean = "glacier", glacier = "glacier", firewall = "volcano", desert = "plain", volcano = "mountain", plain = "plain" } for k, v in pairs(xform) do r2.terrain = k - process_orders() + use_snowglobe(u, 1, "Ost", nil) assert_equal(v, r2.terrain) - if k~=v then - have=have - 1 - else + if k==v then fail = fail + 1 assert_equal(fail, f:count_msg_type('target_region_invalid')) end - assert_equal(have, u:get_item("snowglobe")) end end From 998dcffab2f29f81b3a64c595275ed01ac78a372 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 09:03:08 +0100 Subject: [PATCH 06/21] introduce an rcoption struct. used for pretty rare options that have no property in the race struct. --- clibs | 2 +- cmake | 2 +- src/kernel/faction.c | 10 +------ src/kernel/race.c | 69 +++++++++++++++++++++++++++++++++++++++++++- src/kernel/race.h | 4 +++ 5 files changed, 75 insertions(+), 12 deletions(-) diff --git a/clibs b/clibs index f91ef37f0..27c8b3202 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit f91ef37f08c5244bf616f1836c0aa9caaf36805c +Subproject commit 27c8b3202b52766465743c3324fc0b52c5ba4b11 diff --git a/cmake b/cmake index f1fb3943a..d88983c7f 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit f1fb3943ace59994d90d71a891b80033dc2700a2 +Subproject commit d88983c7ff4bc3a4884a7c3f74e8190bac5eab23 diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 30a623e44..a5373d44d 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -558,20 +558,12 @@ void faction_setpassword(faction * f, const char *pwhash) f->_password = strdup(pwhash); } -const race *other_race(const race *rc) { - if (rc->parameters) { - const char *str = get_param(rc->parameters, "other_race"); - return str ? rc_find(str) : NULL; - } - return NULL; -} - bool valid_race(const struct faction *f, const struct race *rc) { if (f->race == rc) return true; else { - return other_race(f->race) == rc; + return rc_otherrace(f->race) == rc; } } diff --git a/src/kernel/race.c b/src/kernel/race.c index 9a5ce84c2..bc4bdd1e4 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -44,6 +44,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include +#include #include @@ -77,6 +78,59 @@ static const char *racenames[MAXRACES] = { "clone" }; +#define MAXOPTIONS 4 +typedef struct rcoption { + unsigned char key[MAXOPTIONS]; + variant value[MAXOPTIONS]; +} rcoption; + +enum { + RCO_NONE, + RCO_SCARE, + RCO_OTHER +}; + +static void rc_setoption(race *rc, int key, const char *value) { + int i; + variant *v = NULL; + if (!rc->options) { + rc->options = malloc(sizeof(rcoption)); + rc->options->key[0] = key; + rc->options->key[1] = RCO_NONE; + v = rc->options->value; + } else { + for (i=0;!v && i < MAXOPTIONS && rc->options->key[i]!=RCO_NONE;++i) { + if (rc->options->key[i]==key) { + v = rc->options->value+i; + } + } + if (!v) { + assert(ioptions->value+i; + rc->options->key[i] = key; + } + } + assert(v); + if (key == RCO_SCARE) { + v->i = atoi(value); + } + else if (key == RCO_OTHER) { + v->v = rc_get_or_create(value); + } +} + +static variant *rc_getoption(const race *rc, int key) { + if (rc->options) { + int i; + for (i=0;i!=MAXOPTIONS && rc->options->key[i]!=RCO_NONE;++i) { + if (rc->options->key[i]==key) { + return rc->options->value+i; + } + } + } + return NULL; +} + const struct race *findrace(const char *s, const struct locale *lang) { void **tokens = get_translations(lang, UT_RACES); @@ -297,7 +351,14 @@ int rc_armor_bonus(const race *rc) int rc_scare(const struct race *rc) { - return get_param_int(rc->parameters, "ai.scare", 0); + variant *v = rc_getoption(rc, RCO_SCARE); + return v ? v->i : 0; +} + +const race *rc_otherrace(const race *rc) +{ + variant *v = rc_getoption(rc, RCO_OTHER); + return v ? (const race *)v->v : NULL; } int rc_migrants_formula(const race *rc) @@ -314,6 +375,12 @@ void rc_set_param(struct race *rc, const char *key, const char *value) { rc->flags |= RCF_MIGRANTS; } } + else if (strcmp(key, "other_race")==0) { + rc_setoption(rc, RCO_OTHER, value); + } + else if (strcmp(key, "ai.scare")==0) { + rc_setoption(rc, RCO_SCARE, value); + } else { set_param(&rc->parameters, key, value); } diff --git a/src/kernel/race.h b/src/kernel/race.h index 906b56808..f4afb04de 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -47,6 +47,7 @@ extern "C" { struct spell; struct spellref; struct locale; + struct rcoption; extern int num_races; @@ -153,6 +154,8 @@ extern "C" { struct item *(*itemdrop) (const struct race *, int size); void(*init_familiar) (struct unit *); + struct rcoption *options; // rarely used properties + const struct race *familiars[MAXMAGIETYP]; struct race *next; } race; @@ -189,6 +192,7 @@ extern "C" { double rc_maxaura(const struct race *rc); int rc_armor_bonus(const struct race *rc); int rc_scare(const struct race *rc); + const race *rc_otherrace(const race *rc); #define MIGRANTS_NONE 0 #define MIGRANTS_LOG10 1 From 28c951bdfd423aaa381324665369ffaff4fd5549 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 09:46:36 +0100 Subject: [PATCH 07/21] eliminate race.parameters. move all special properties to race.options --- src/kernel/race.c | 71 +++++++++++++++++++++++++++++++++++++++--- src/kernel/race.h | 4 ++- src/kernel/race.test.c | 6 ++-- src/kernel/xmlreader.c | 1 - src/market.c | 16 ---------- src/morale.c | 7 ++--- src/upkeep.c | 18 +++++------ 7 files changed, 84 insertions(+), 39 deletions(-) diff --git a/src/kernel/race.c b/src/kernel/race.c index bc4bdd1e4..8b2632753 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -86,8 +86,12 @@ typedef struct rcoption { enum { RCO_NONE, - RCO_SCARE, - RCO_OTHER + RCO_SCARE, // races that scare and eat peasants + RCO_OTHER, // may recruit from another race + RCO_STAMINA, // every n levels of stamina add +1 RC + RCO_HUNGER, // custom hunger.damage override (char *) + RCO_TRADELUX, + RCO_TRADEHERB, }; static void rc_setoption(race *rc, int key, const char *value) { @@ -114,9 +118,21 @@ static void rc_setoption(race *rc, int key, const char *value) { if (key == RCO_SCARE) { v->i = atoi(value); } + else if (key == RCO_STAMINA) { + v->i = atoi(value); + } else if (key == RCO_OTHER) { v->v = rc_get_or_create(value); } + else if (key == RCO_HUNGER) { + v->v = strdup(value); + } + else if (key == RCO_TRADEHERB) { + v->i = atoi(value); + } + else if (key == RCO_TRADELUX) { + v->i = atoi(value); + } } static variant *rc_getoption(const race *rc, int key) { @@ -238,12 +254,20 @@ void free_races(void) { while (races) { int i; race * rc = races->next; + rcoption * opt = races->options; + if (opt) { + for (i=0;i!=MAXOPTIONS && opt->key[i]!=RCO_NONE;++i) { + if (opt->key[i]==RCO_HUNGER) { + free(opt->value[i].v); + } + } + free(opt); + } for (i = 0; races->attack[i].type!=AT_NONE; ++i) { spellref_free(races->attack[i].data.sp); } spellref_free(races->precombatspell); - free_params(&races->parameters); free(xrefs); xrefs = 0; free(races->_name); @@ -344,9 +368,16 @@ double rc_maxaura(const race *rc) { return rc->maxaura / 100.0; } +const char * rc_hungerdamage(const race *rc) +{ + variant *v = rc_getoption(rc, RCO_HUNGER); + return v ? (const char *)v->v : NULL; +} + int rc_armor_bonus(const race *rc) { - return get_param_int(rc->parameters, "armor.stamina", 0); + variant *v = rc_getoption(rc, RCO_STAMINA); + return v ? v->i : 0; } int rc_scare(const struct race *rc) @@ -355,6 +386,24 @@ int rc_scare(const struct race *rc) return v ? v->i : 0; } +int rc_luxury_trade(const struct race *rc) +{ + if (rc) { + variant *v = rc_getoption(rc, RCO_TRADELUX); + if (v) return v->i; + } + return 1000; +} + +int rc_herb_trade(const struct race *rc) +{ + if (rc) { + variant *v = rc_getoption(rc, RCO_TRADEHERB); + if (v) return v->i; + } + return 500; +} + const race *rc_otherrace(const race *rc) { variant *v = rc_getoption(rc, RCO_OTHER); @@ -381,8 +430,20 @@ void rc_set_param(struct race *rc, const char *key, const char *value) { else if (strcmp(key, "ai.scare")==0) { rc_setoption(rc, RCO_SCARE, value); } + else if (strcmp(key, "hunger.damage")==0) { + rc_setoption(rc, RCO_HUNGER, value); + } + else if (strcmp(key, "armor.stamina")==0) { + rc_setoption(rc, RCO_STAMINA, value); + } + else if (strcmp(key, "luxury_trade")==0) { + rc_setoption(rc, RCO_TRADELUX, value); + } + else if (strcmp(key, "herb_trade")==0) { + rc_setoption(rc, RCO_TRADEHERB, value); + } else { - set_param(&rc->parameters, key, value); + log_error("unknown property for race %s: %s=%s", rc->_name, key, value); } } diff --git a/src/kernel/race.h b/src/kernel/race.h index f4afb04de..6321d4f22 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -138,7 +138,6 @@ extern "C" { int df_default; /* Verteidigungsskill Unbewaffnet (default: -2) */ int at_bonus; /* Ver�ndert den Angriffsskill (default: 0) */ int df_bonus; /* Ver�ndert den Verteidigungskill (default: 0) */ - struct param *parameters; // additional properties, for an example see natural_armor struct spellref *precombatspell; signed char *study_speed; /* study-speed-bonus in points/turn (0=30 Tage) */ int flags; @@ -188,10 +187,13 @@ extern "C" { void rc_set_param(struct race *rc, const char *key, const char *value); + int rc_luxury_trade(const struct race *rc); + int rc_herb_trade(const struct race *rc); double rc_magres(const struct race *rc); double rc_maxaura(const struct race *rc); int rc_armor_bonus(const struct race *rc); int rc_scare(const struct race *rc); + const char * rc_hungerdamage(const race *rc); const race *rc_otherrace(const race *rc); #define MIGRANTS_NONE 0 diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index 857e8696a..8363d8b72 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -88,9 +88,7 @@ static void test_rc_set_param(CuTest *tc) { race *rc; test_setup(); rc = test_create_race("human"); - CuAssertPtrEquals(tc, NULL, rc->parameters); - rc_set_param(rc, "hodor", "HODOR"); - CuAssertStrEquals(tc, "HODOR", get_param(rc->parameters, "hodor")); + CuAssertPtrEquals(tc, NULL, rc->options); rc_set_param(rc, "recruit_multi", "0.5"); CuAssertDblEquals(tc, 0.5, rc->recruit_multi, 0.0); rc_set_param(rc, "migrants.formula", "1"); @@ -98,6 +96,8 @@ static void test_rc_set_param(CuTest *tc) { CuAssertIntEquals(tc, MIGRANTS_LOG10, rc_migrants_formula(rc)); rc_set_param(rc, "ai.scare", "400"); CuAssertIntEquals(tc, 400, rc_scare(rc)); + rc_set_param(rc, "hunger.damage", "1d10+12"); + CuAssertStrEquals(tc, "1d10+12", rc_hungerdamage(rc)); test_cleanup(); } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 0864df9a6..00bb9f848 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -1738,7 +1738,6 @@ static int parse_races(xmlDocPtr doc) xmlFree(propValue); } } - rc->recruit_multi = get_param_flt(rc->parameters, "recruit_multi", 1.0); /* reading eressea/races/race/skill */ xpath->node = node; diff --git a/src/market.c b/src/market.c index 254d3a483..8fd4e3f0d 100644 --- a/src/market.c +++ b/src/market.c @@ -66,22 +66,6 @@ attrib_type at_market = { NULL, NULL, NULL, ATF_UNIQUE }; -int rc_luxury_trade(const struct race *rc) -{ - if (rc) { - return get_param_int(rc->parameters, "luxury_trade", 1000); - } - return 1000; -} - -int rc_herb_trade(const struct race *rc) -{ - if (rc) { - return get_param_int(rc->parameters, "herb_trade", 500); - } - return 500; -} - #define MAX_MARKETS 128 #define MIN_PEASANTS 50 /* if there are at least this many peasants, you will get 1 good */ diff --git a/src/morale.c b/src/morale.c index 1bdf2363d..580af899f 100644 --- a/src/morale.c +++ b/src/morale.c @@ -31,10 +31,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include -static double rc_popularity(const struct race *rc) +static double popularity() { - int pop = get_param_int(rc->parameters, "morale", MORALE_AVERAGE); - return 1.0 / (pop - MORALE_COOLDOWN); /* 10 turns average */ + return 1.0 / (MORALE_AVERAGE - MORALE_COOLDOWN); /* 10 turns average */ } void morale_update(region *r) { @@ -52,7 +51,7 @@ void morale_update(region *r) { if (morale < maxmorale) { if (stability > MORALE_COOLDOWN && r->land->ownership->owner && morale < MORALE_MAX) { - double ch = rc_popularity(r->land->ownership->owner->race); + double ch = popularity(); if (is_cursed(r->attribs, C_GENEROUS, 0)) { ch *= 1.2; /* 20% improvement */ } diff --git a/src/upkeep.c b/src/upkeep.c index c53c321d0..8494d7f25 100644 --- a/src/upkeep.c +++ b/src/upkeep.c @@ -59,7 +59,7 @@ static void help_feed(unit * donor, unit * u, int *need_p) } static const char *hunger_damage(const race *rc) { - const char * damage = rc->parameters ? get_param(rc->parameters, "hunger.damage") : NULL; + const char * damage = rc_hungerdamage(rc); if (!damage) { damage = config_get("hunger.damage"); } @@ -98,11 +98,11 @@ static bool hunger(int number, unit * u) deathcounts(r, dead); } if (hpsub > 0) { - /* Jetzt die Schäden der nicht gestorbenen abziehen. */ + /* Jetzt die Sch�den der nicht gestorbenen abziehen. */ u->hp -= hpsub; - /* Meldung nur, wenn noch keine für Tote generiert. */ + /* Meldung nur, wenn noch keine f�r Tote generiert. */ if (dead == 0) { - /* Durch unzureichende Ernährung wird %s geschwächt */ + /* Durch unzureichende Ern�hrung wird %s geschw�cht */ ADDMSG(&u->faction->msgs, msg_message("malnourish", "unit region", u, r)); } } @@ -125,13 +125,13 @@ void get_food(region * r) return; } /* 1. Versorgung von eigenen Einheiten. Das vorhandene Silber - * wird zunächst so auf die Einheiten aufgeteilt, dass idealerweise - * jede Einheit genug Silber für ihren Unterhalt hat. */ + * wird zun�chst so auf die Einheiten aufgeteilt, dass idealerweise + * jede Einheit genug Silber f�r ihren Unterhalt hat. */ for (u = r->units; u; u = u->next) { int need = lifestyle(u); - /* Erstmal zurücksetzen */ + /* Erstmal zur�cksetzen */ freset(u, UFL_HUNGER); if (u->ship && (u->ship->flags & SF_FISHING)) { @@ -230,7 +230,7 @@ void get_food(region * r) } /* 3. bestimmen, wie viele Bauern gefressen werden. - * bei fehlenden Bauern den Dämon hungern lassen + * bei fehlenden Bauern den D�mon hungern lassen */ for (u = r->units; u; u = u->next) { if (u_race(u) == rc_demon) { @@ -293,7 +293,7 @@ void get_food(region * r) } rsetpeasants(r, peasantfood / 10); - /* 3. Von den überlebenden das Geld abziehen: */ + /* 3. Von den �berlebenden das Geld abziehen: */ for (u = r->units; u; u = u->next) { int need = MIN(get_money(u), lifestyle(u)); change_money(u, -need); From 2267373b2e40409f75f485c1155dd69e9dbf7cba Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 10:44:11 +0100 Subject: [PATCH 08/21] gcc warning --- clibs | 2 +- cmake | 2 +- src/morale.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clibs b/clibs index 27c8b3202..f91ef37f0 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 27c8b3202b52766465743c3324fc0b52c5ba4b11 +Subproject commit f91ef37f08c5244bf616f1836c0aa9caaf36805c diff --git a/cmake b/cmake index d88983c7f..f1fb3943a 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit d88983c7ff4bc3a4884a7c3f74e8190bac5eab23 +Subproject commit f1fb3943ace59994d90d71a891b80033dc2700a2 diff --git a/src/morale.c b/src/morale.c index 580af899f..67108333e 100644 --- a/src/morale.c +++ b/src/morale.c @@ -31,7 +31,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include -static double popularity() +static double popularity(void) { return 1.0 / (MORALE_AVERAGE - MORALE_COOLDOWN); /* 10 turns average */ } From baf3275ebac67b3d01e1040bd644c9f3cb984bd2 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 11:52:07 +0100 Subject: [PATCH 09/21] valgrind: always terminate options --- src/kernel/race.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/kernel/race.c b/src/kernel/race.c index 8b2632753..c57d98a24 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -112,6 +112,9 @@ static void rc_setoption(race *rc, int key, const char *value) { assert(ioptions->value+i; rc->options->key[i] = key; + if (i+1options->key[i+1]=RCO_NONE; + } } } assert(v); From d851554ebe68abc540a45796767fc016a786b0c0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 18:16:34 +0100 Subject: [PATCH 10/21] MSVC doesn't like to do integer->char conversions. --- src/kernel/race.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/kernel/race.c b/src/kernel/race.c index c57d98a24..c7c2e8165 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -94,7 +94,8 @@ enum { RCO_TRADEHERB, }; -static void rc_setoption(race *rc, int key, const char *value) { +static void rc_setoption(race *rc, int k, const char *value) { + unsigned char key = (unsigned char)k; int i; variant *v = NULL; if (!rc->options) { From 8ffa0919366a631e9189ee9a84c562de3b8a5d4f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 6 Feb 2017 20:34:11 +0100 Subject: [PATCH 11/21] do not abort xml parsing when there is no calendar --- src/kernel/xmlreader.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 00bb9f848..cd3331517 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -358,16 +358,13 @@ static int parse_calendar(xmlDocPtr doc) xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathObjectPtr xpathCalendars; xmlNodeSetPtr nsetCalendars; - int c, rv = 0; /* reading eressea/buildings/building */ xpathCalendars = xmlXPathEvalExpression(BAD_CAST "/eressea/calendar", xpath); nsetCalendars = xpathCalendars->nodesetval; months_per_year = 0; - if (nsetCalendars == NULL || nsetCalendars->nodeNr == 0) { - rv = -1; - } - else + if (nsetCalendars != NULL && nsetCalendars->nodeNr != 0) { + int c; for (c = 0; c != nsetCalendars->nodeNr; ++c) { xmlNodePtr calendar = nsetCalendars->nodeTab[c]; xmlXPathObjectPtr xpathWeeks, xpathMonths, xpathSeasons; @@ -477,10 +474,11 @@ static int parse_calendar(xmlDocPtr doc) xmlFree(newyear); newyear = NULL; } + } xmlXPathFreeObject(xpathCalendars); xmlXPathFreeContext(xpath); - return rv; + return 0; } static int parse_ships(xmlDocPtr doc) From 60497da87b74c4228523e63d927c741abafa2d67 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 7 Feb 2017 20:50:07 +0100 Subject: [PATCH 12/21] print error message when missing parameter translation. --- src/kernel/xmlreader.c | 3 +-- src/util/language.c | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index cd3331517..6491daf20 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -359,7 +359,6 @@ static int parse_calendar(xmlDocPtr doc) xmlXPathObjectPtr xpathCalendars; xmlNodeSetPtr nsetCalendars; - /* reading eressea/buildings/building */ xpathCalendars = xmlXPathEvalExpression(BAD_CAST "/eressea/calendar", xpath); nsetCalendars = xpathCalendars->nodesetval; months_per_year = 0; @@ -374,7 +373,7 @@ static int parse_calendar(xmlDocPtr doc) xmlChar *start; start = xmlGetProp(calendar, BAD_CAST "start"); - if (start && config_get("game.start")==NULL) { + if (start && config_get("game.start") == NULL) { config_set("game.start", (const char *)start); xmlFree(start); } diff --git a/src/util/language.c b/src/util/language.c index 300e69103..0de04be71 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -300,6 +300,9 @@ void init_translations(const struct locale *lang, int ut, const char * (*string_ struct critbit_tree ** cb = (struct critbit_tree **)tokens; add_translation(cb, key, i); } + else { + log_error("no translation for %s in locale %s", s, lang->name); + } } } } From f48dd415380b82b509447650bd96221b9a4b37c8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 7 Feb 2017 21:37:38 +0100 Subject: [PATCH 13/21] fix a segfault in free_Races --- src/kernel/config.c | 2 +- src/kernel/race.c | 8 +++++++- src/kernel/race.h | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index ca23a5bc9..b2734d43a 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -203,7 +203,7 @@ param_t findparam(const char *s, const struct locale * lang) void **tokens = get_translations(lang, UT_PARAMS); critbit_tree *cb = (critbit_tree *)*tokens; if (!cb) { - log_warning("no parameters defined in locale %s", locale_name(lang)); + log_error("no parameters defined in locale %s", locale_name(lang)); } else if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { cb_get_kv(match, &i, sizeof(int)); diff --git a/src/kernel/race.c b/src/kernel/race.c index c7c2e8165..a4a11c15d 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -269,7 +269,13 @@ void free_races(void) { free(opt); } for (i = 0; races->attack[i].type!=AT_NONE; ++i) { - spellref_free(races->attack[i].data.sp); + att *at = races->attack + i; + if (at->type == AT_SPELL) { + spellref_free(at->data.sp); + } + else { + free(at->data.dice); + } } spellref_free(races->precombatspell); free(xrefs); diff --git a/src/kernel/race.h b/src/kernel/race.h index 6321d4f22..c58bad701 100644 --- a/src/kernel/race.h +++ b/src/kernel/race.h @@ -105,7 +105,7 @@ extern "C" { typedef struct att { int type; union { - const char *dice; + char *dice; struct spellref *sp; } data; int flags; From b504899b07fbcc48f9c96cec85da3c2e4d066561 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 9 Feb 2017 23:28:50 +0100 Subject: [PATCH 14/21] fix locale initialization --- src/bind_config.c | 2 -- src/bindings.c | 2 +- src/kernel/config.c | 2 +- src/laws.c | 1 + src/util/language.c | 2 +- 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/bind_config.c b/src/bind_config.c index faf543f47..c603800ed 100644 --- a/src/bind_config.c +++ b/src/bind_config.c @@ -22,7 +22,6 @@ void config_reset(void) { default_locale = 0; free_config(); - free_locales(); free_nrmesssages(); free_spells(); free_buildingtypes(); @@ -37,7 +36,6 @@ int config_parse(const char *json) if (conf) { json_config(conf); cJSON_Delete(conf); - init_locales(); return 0; } else { diff --git a/src/bindings.c b/src/bindings.c index 7bf30202a..1515c74e0 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -517,7 +517,7 @@ static void reset_game(void) for (f = factions; f; f = f->next) { f->flags &= FFL_SAVEMASK; } - init_locales(); +// init_locales(); } static int tolua_process_orders(lua_State * L) diff --git a/src/kernel/config.c b/src/kernel/config.c index b2734d43a..ca23a5bc9 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -203,7 +203,7 @@ param_t findparam(const char *s, const struct locale * lang) void **tokens = get_translations(lang, UT_PARAMS); critbit_tree *cb = (critbit_tree *)*tokens; if (!cb) { - log_error("no parameters defined in locale %s", locale_name(lang)); + log_warning("no parameters defined in locale %s", locale_name(lang)); } else if (cb_find_prefix(cb, str, strlen(str), &match, 1, 0)) { cb_get_kv(match, &i, sizeof(int)); diff --git a/src/laws.c b/src/laws.c index 98ff5d9f6..78ca6fba9 100644 --- a/src/laws.c +++ b/src/laws.c @@ -4196,6 +4196,7 @@ void init_processor(void) void processorders(void) { + init_locales(); init_processor(); process(); /*************************************************/ diff --git a/src/util/language.c b/src/util/language.c index 0de04be71..86a6895be 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -301,7 +301,7 @@ void init_translations(const struct locale *lang, int ut, const char * (*string_ add_translation(cb, key, i); } else { - log_error("no translation for %s in locale %s", s, lang->name); + log_warning("no translation for %s in locale %s", s, lang->name); } } } From 2ee0e599b173295f45fabf58124e2063047ce3b1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 17:38:39 +0100 Subject: [PATCH 15/21] initialize game after loading config --- scripts/eressea/xmlconf.lua | 1 + src/bind_eressea.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/scripts/eressea/xmlconf.lua b/scripts/eressea/xmlconf.lua index 9d87f3f3e..584f8d3e2 100644 --- a/scripts/eressea/xmlconf.lua +++ b/scripts/eressea/xmlconf.lua @@ -7,3 +7,4 @@ if config.rules then assert(0 == read_xml(confdir .. rules .. 'config.xml', confdir .. rules .. 'catalog.xml'), "could not load XML data, did you compile with LIBXML2 ?") assert(0 == eressea.config.read(rules .. 'config.json', confdir), "could not read JSON data") end +eressea.game.reset() diff --git a/src/bind_eressea.c b/src/bind_eressea.c index 279ce8f4e..de8751306 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -9,6 +9,8 @@ #include #include +#include + #include #include @@ -16,6 +18,7 @@ void eressea_free_game(void) { free_gamedata(); init_resources(); + init_locales(); } int eressea_read_game(const char * filename) { From 853f63b50129f1d0d7ab0013f7e330c760f69daf Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 20:27:13 +0100 Subject: [PATCH 16/21] delete unitmessage trigger, it is unused --- src/spells.c | 1 - src/triggers/CMakeLists.txt | 1 - src/triggers/triggers.c | 2 - src/triggers/unitmessage.c | 124 ------------------------------------ src/triggers/unitmessage.h | 37 ----------- 5 files changed, 165 deletions(-) delete mode 100644 src/triggers/unitmessage.c delete mode 100644 src/triggers/unitmessage.h diff --git a/src/spells.c b/src/spells.c index 0bb6df5af..e936a46a2 100644 --- a/src/spells.c +++ b/src/spells.c @@ -95,7 +95,6 @@ #include #include #include -#include /* attributes includes */ #include diff --git a/src/triggers/CMakeLists.txt b/src/triggers/CMakeLists.txt index d6670a95b..e123698d6 100644 --- a/src/triggers/CMakeLists.txt +++ b/src/triggers/CMakeLists.txt @@ -11,7 +11,6 @@ killunit.c shock.c timeout.c triggers.c -unitmessage.c ) FOREACH(_FILE ${_FILES}) LIST(APPEND _SOURCES ${PROJECT_NAME}/${_FILE}) diff --git a/src/triggers/triggers.c b/src/triggers/triggers.c index 433201abd..8826fcba2 100644 --- a/src/triggers/triggers.c +++ b/src/triggers/triggers.c @@ -28,7 +28,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include #include -#include #include /* util includes */ @@ -47,7 +46,6 @@ void register_triggers(void) tt_register(&tt_giveitem); tt_register(&tt_killunit); tt_register(&tt_shock); - tt_register(&tt_unitmessage); tt_register(&tt_timeout); tt_register(&tt_clonedied); } diff --git a/src/triggers/unitmessage.c b/src/triggers/unitmessage.c deleted file mode 100644 index 7ba97d9bc..000000000 --- a/src/triggers/unitmessage.c +++ /dev/null @@ -1,124 +0,0 @@ -/* -+-------------------+ Enno Rehling -| Eressea PBEM host | Christian Schlittchen -| (c) 1998 - 2008 | Katja Zedel -+-------------------+ -This program may not be used, modified or distributed -without prior permission by the authors of Eressea. -*/ - -#include -#include "unitmessage.h" - -/* kernel includes */ -#include -#include -#include - -/* util includes */ -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -/* ansi includes */ -#include -#include -#include -#include - -/*** -** give an item to someone -**/ - -typedef struct unitmessage_data { - struct unit *target; - char *string; - int type; - int level; -} unitmessage_data; - -static void unitmessage_init(trigger * t) -{ - t->data.v = calloc(sizeof(unitmessage_data), 1); -} - -static void unitmessage_free(trigger * t) -{ - unitmessage_data *sd = (unitmessage_data *)t->data.v; - free(sd->string); - free(t->data.v); -} - -static int unitmessage_handle(trigger * t, void *data) -{ - /* call an event handler on unitmessage. - * data.v -> ( variant event, int timer ) - */ - unitmessage_data *td = (unitmessage_data *)t->data.v; - if (td->target && td->target->no) { - 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); - } - } - UNUSED_ARG(data); - return 0; -} - -static void unitmessage_write(const trigger * t, struct storage *store) -{ - unitmessage_data *td = (unitmessage_data *)t->data.v; - write_unit_reference(td->target, store); - WRITE_TOK(store, td->string); - WRITE_INT(store, td->type); - WRITE_INT(store, td->level); -} - -static int unitmessage_read(trigger * t, gamedata *data) -{ - unitmessage_data *td = (unitmessage_data *)t->data.v; - char zText[256]; - - int result = read_reference(&td->target, data, read_unit_reference, - resolve_unit); - READ_TOK(data->store, zText, sizeof(zText)); - td->string = strdup(zText); - READ_INT(data->store, &td->type); - READ_INT(data->store, &td->level); - - if (result == 0 && td->target == NULL) { - return AT_READ_FAIL; - } - return AT_READ_OK; -} - -trigger_type tt_unitmessage = { - "unitmessage", - unitmessage_init, - unitmessage_free, - unitmessage_handle, - unitmessage_write, - unitmessage_read -}; - -trigger *trigger_unitmessage(unit * target, const char *string, int type, - int level) -{ - trigger *t = t_new(&tt_unitmessage); - unitmessage_data *td = (unitmessage_data *)t->data.v; - td->target = target; - td->string = strdup(string); - td->type = type; - td->level = level; - return t; -} diff --git a/src/triggers/unitmessage.h b/src/triggers/unitmessage.h deleted file mode 100644 index 5b5b0793b..000000000 --- a/src/triggers/unitmessage.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -Copyright (c) 1998-2015, Enno Rehling -Katja Zedel - -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. -**/ - -#ifndef UNITMESSAGE_H -#define UNITMESSAGE_H -#ifdef __cplusplus -extern "C" { -#endif - - /* all types we use are defined here to reduce dependencies */ - struct trigger_type; - struct trigger; - struct unit; - - extern struct trigger_type tt_unitmessage; - extern struct trigger *trigger_unitmessage(struct unit *target, - const char *string, int type, int level); - -#ifdef __cplusplus -} -#endif -#endif From c897108a2cf29a0f562d44da294d91b2a749db9c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 20:58:34 +0100 Subject: [PATCH 17/21] clibs latest --- clibs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clibs b/clibs index f91ef37f0..27c8b3202 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit f91ef37f08c5244bf616f1836c0aa9caaf36805c +Subproject commit 27c8b3202b52766465743c3324fc0b52c5ba4b11 From 10e78b1455147442a4334a3f6c845545ba2c0244 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 22:15:21 +0100 Subject: [PATCH 18/21] 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 From 40de740a0aa06e6b036ca9fe03b1f0a4fee3dc74 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 22:24:36 +0100 Subject: [PATCH 19/21] remove dict from ships and buildings, too --- src/bind_building.c | 9 --------- src/bind_ship.c | 10 ---------- 2 files changed, 19 deletions(-) diff --git a/src/bind_building.c b/src/bind_building.c index f8cf4e6df..ea689bfb5 100644 --- a/src/bind_building.c +++ b/src/bind_building.c @@ -13,7 +13,6 @@ without prior permission by the authors of Eressea. #include #include "bind_building.h" #include "bind_unit.h" -#include "bind_dict.h" #include #include @@ -41,13 +40,6 @@ int tolua_buildinglist_next(lua_State * L) return 0; /* no more values to return */ } -static int tolua_building_get_objects(lua_State * L) -{ - building *self = (building *)tolua_tousertype(L, 1, 0); - tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT); - return 1; -} - static int tolua_building_set_working(lua_State * L) { building *self = (building *)tolua_tousertype(L, 1, 0); @@ -258,7 +250,6 @@ void tolua_building_open(lua_State * L) tolua_variable(L, TOLUA_CAST "size", tolua_building_get_size, tolua_building_set_size); tolua_function(L, TOLUA_CAST "get_typename", tolua_building_get_typename); - tolua_variable(L, TOLUA_CAST "objects", tolua_building_get_objects, 0); tolua_variable(L, TOLUA_CAST "working", tolua_building_get_working, tolua_building_set_working); } diff --git a/src/bind_ship.c b/src/bind_ship.c index 95006cf5f..515fa8de3 100644 --- a/src/bind_ship.c +++ b/src/bind_ship.c @@ -13,7 +13,6 @@ without prior permission by the authors of Eressea. #include #include "bind_ship.h" #include "bind_unit.h" -#include "bind_dict.h" #include "move.h" @@ -115,13 +114,6 @@ static int tolua_ship_get_units(lua_State * L) return 1; } -static int tolua_ship_get_objects(lua_State * L) -{ - ship *self = (ship *)tolua_tousertype(L, 1, 0); - tolua_pushusertype(L, (void *)&self->attribs, USERTYPE_DICT); - return 1; -} - static int tolua_ship_create(lua_State * L) { region *r = (region *)tolua_tousertype(L, 1, 0); @@ -233,8 +225,6 @@ void tolua_ship_open(lua_State * L) tolua_variable(L, TOLUA_CAST "type", tolua_ship_get_type, 0); tolua_variable(L, TOLUA_CAST "damage", tolua_ship_get_damage, tolua_ship_set_damage); - tolua_variable(L, TOLUA_CAST "objects", tolua_ship_get_objects, 0); - tolua_function(L, TOLUA_CAST "create", tolua_ship_create); } tolua_endmodule(L); From f6fcd392934fbaa90d2edb72421114da09d6f564 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 22:28:57 +0100 Subject: [PATCH 20/21] add a failing test for familiars (disabled). remove superfluous init_locales calls. --- src/bind_config.c | 1 - src/kernel/save.c | 1 - src/laws.c | 1 - src/magic.test.c | 20 ++++++++++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/bind_config.c b/src/bind_config.c index c603800ed..b4aa68d58 100644 --- a/src/bind_config.c +++ b/src/bind_config.c @@ -20,7 +20,6 @@ #include "kernel/terrain.h" void config_reset(void) { - default_locale = 0; free_config(); free_nrmesssages(); free_spells(); diff --git a/src/kernel/save.c b/src/kernel/save.c index 8ec797e68..67ed7e125 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1470,7 +1470,6 @@ int readgame(const char *filename) FILE *F; size_t sz; - init_locales(); log_debug("- reading game data from %s", filename); join_path(datapath(), filename, path, sizeof(path)); diff --git a/src/laws.c b/src/laws.c index 78ca6fba9..98ff5d9f6 100644 --- a/src/laws.c +++ b/src/laws.c @@ -4196,7 +4196,6 @@ void init_processor(void) void processorders(void) { - init_locales(); init_processor(); process(); /*************************************************/ diff --git a/src/magic.test.c b/src/magic.test.c index 4a6078589..07a6d5257 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -2,6 +2,7 @@ #include "magic.h" #include "teleport.h" +#include "give.h" #include #include @@ -448,6 +449,24 @@ static void test_max_spellpoints(CuTest *tc) { test_cleanup(); } +static void test_familiar_mage(CuTest *tc) { + unit *um, *uf, *ut; + test_setup(); + um = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + uf = test_create_unit(um->faction, um->region); + ut = test_create_unit(um->faction, um->region); + set_number(ut, 0); + CuAssertTrue(tc, create_newfamiliar(um, uf)); + CuAssertTrue(tc, is_familiar(uf)); + CuAssertTrue(tc, !is_familiar(um)); + CuAssertPtrEquals(tc, um, get_familiar_mage(uf)); + CuAssertPtrEquals(tc, uf, get_familiar(um)); + + CuAssertPtrEquals(tc, NULL, give_men(1, um, ut, NULL)); + CuAssertPtrEquals(tc, ut, get_familiar_mage(uf)); + test_cleanup(); +} + CuSuite *get_magic_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -465,5 +484,6 @@ CuSuite *get_magic_suite(void) SUITE_ADD_TEST(suite, test_hasspell); SUITE_ADD_TEST(suite, test_magic_resistance); SUITE_ADD_TEST(suite, test_max_spellpoints); + DISABLE_TEST(suite, test_familiar_mage); return suite; } From b56538e0923475992d62e0e875c252f21d67bc53 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 11 Feb 2017 22:37:15 +0100 Subject: [PATCH 21/21] remove test for dicts --- scripts/tests/common.lua | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 670060816..96673bfd3 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -330,17 +330,6 @@ function test_message() return msg end -function test_hashtable() - local f = faction.create("noreply1@eressea.de", "human", "de") - f.objects:set("enno", "smart guy") - f.objects:set("age", 10) - assert(f.objects:get("jesus") == nil) - assert(f.objects:get("enno") == "smart guy") - assert(f.objects:get("age") == 10) - f.objects:set("age", nil) - assert(f.objects:get("age") == nil) -end - function test_events() local fail = 1 local function msg_handler(u, evt)