From 6e8e16309989bbfc7251ca77e7c805de5e8c5d11 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 3 Mar 2017 09:56:27 +0100 Subject: [PATCH] make item allow/deny XML easier to read, fix code --- res/core/weapons/greatbow.xml | 2 +- res/core/weapons/rep_crossbow.xml | 2 +- res/e3a/armor/plate.xml | 2 +- res/e3a/armor/scale.xml | 2 +- res/e3a/armor/towershield.xml | 2 +- res/e3a/races.xml | 6 +++--- res/e3a/weapons.xml | 4 ++-- res/e3a/weapons/greatbow.xml | 2 +- res/e3a/weapons/halberd.xml | 2 +- res/e3a/weapons/mallornlance.xml | 2 +- res/e3a/weapons/rustyhalberd.xml | 2 +- res/races/goblin-3.xml | 2 +- scripts/tests/e3/items.lua | 4 ++-- src/kernel/race.c | 2 +- src/kernel/race.test.c | 2 +- src/kernel/xmlreader.c | 26 +++++++++++++++++++++++--- 16 files changed, 42 insertions(+), 22 deletions(-) diff --git a/res/core/weapons/greatbow.xml b/res/core/weapons/greatbow.xml index 10b270e55..14734a129 100644 --- a/res/core/weapons/greatbow.xml +++ b/res/core/weapons/greatbow.xml @@ -1,6 +1,6 @@ - + diff --git a/res/core/weapons/rep_crossbow.xml b/res/core/weapons/rep_crossbow.xml index 66e3f9fc2..d6f869046 100644 --- a/res/core/weapons/rep_crossbow.xml +++ b/res/core/weapons/rep_crossbow.xml @@ -1,6 +1,6 @@ - + diff --git a/res/e3a/armor/plate.xml b/res/e3a/armor/plate.xml index 29cabbf1a..9aeafc3c1 100644 --- a/res/e3a/armor/plate.xml +++ b/res/e3a/armor/plate.xml @@ -1,6 +1,6 @@ - + diff --git a/res/e3a/armor/scale.xml b/res/e3a/armor/scale.xml index 7039b4008..691ce5ba7 100644 --- a/res/e3a/armor/scale.xml +++ b/res/e3a/armor/scale.xml @@ -1,6 +1,6 @@ - + diff --git a/res/e3a/armor/towershield.xml b/res/e3a/armor/towershield.xml index dcd2858cf..78e113e2a 100644 --- a/res/e3a/armor/towershield.xml +++ b/res/e3a/armor/towershield.xml @@ -1,6 +1,6 @@ - + diff --git a/res/e3a/races.xml b/res/e3a/races.xml index fc16645c6..8b0cfae6d 100644 --- a/res/e3a/races.xml +++ b/res/e3a/races.xml @@ -75,7 +75,7 @@ regaura="1.0" recruitcost="100" maintenance="10" weight="1000" capacity="540" speed="1.0" hp="20" damage="1d5" unarmedattack="-2" unarmeddefense="-2" playerrace="yes" walk="yes" giveperson="yes" - giveunit="yes" getitem="yes" equipment="yes" items="8"> + giveunit="yes" getitem="yes" equipment="yes"> @@ -124,7 +124,7 @@ recruitcost="200" maintenance="10" weight="1000" capacity="540" speed="1.0" hp="20" damage="1d5" unarmedattack="-2" unarmeddefense="-2" playerrace="yes" walk="yes" giveperson="yes" - giveunit="yes" getitem="yes" equipment="yes" items="2"> + giveunit="yes" getitem="yes" equipment="yes" > @@ -168,7 +168,7 @@ recruitcost="240" maintenance="10" weight="1000" capacity="540" speed="1.0" hp="20" damage="1d5" unarmedattack="-2" unarmeddefense="-2" playerrace="yes" walk="yes" giveperson="yes" - giveunit="yes" getitem="yes" equipment="yes" items="1"> + giveunit="yes" getitem="yes" equipment="yes"> diff --git a/res/e3a/weapons.xml b/res/e3a/weapons.xml index 052266a02..96a987b28 100644 --- a/res/e3a/weapons.xml +++ b/res/e3a/weapons.xml @@ -1,9 +1,7 @@ - - @@ -13,6 +11,8 @@ + + diff --git a/res/e3a/weapons/greatbow.xml b/res/e3a/weapons/greatbow.xml index 7f08427c8..b488a011c 100644 --- a/res/e3a/weapons/greatbow.xml +++ b/res/e3a/weapons/greatbow.xml @@ -4,7 +4,7 @@ * has lower damage --> - + diff --git a/res/e3a/weapons/halberd.xml b/res/e3a/weapons/halberd.xml index 2c11b62fc..c07cbfd4d 100644 --- a/res/e3a/weapons/halberd.xml +++ b/res/e3a/weapons/halberd.xml @@ -3,7 +3,7 @@ 1. you cannt use this with cavalry --> - + diff --git a/res/e3a/weapons/mallornlance.xml b/res/e3a/weapons/mallornlance.xml index 1fcad08c8..d2aacaea7 100644 --- a/res/e3a/weapons/mallornlance.xml +++ b/res/e3a/weapons/mallornlance.xml @@ -1,6 +1,6 @@ - + diff --git a/res/e3a/weapons/rustyhalberd.xml b/res/e3a/weapons/rustyhalberd.xml index ed25d5db7..32c0d4497 100644 --- a/res/e3a/weapons/rustyhalberd.xml +++ b/res/e3a/weapons/rustyhalberd.xml @@ -3,7 +3,7 @@ 1. you cannot use this with cavalry --> - + diff --git a/res/races/goblin-3.xml b/res/races/goblin-3.xml index 8deb33f5e..a65d4c99a 100644 --- a/res/races/goblin-3.xml +++ b/res/races/goblin-3.xml @@ -3,7 +3,7 @@ recruitcost="60" maintenance="6" weight="600" capacity="440" speed="1.0" hp="16" damage="1d5" unarmedattack="-2" unarmeddefense="0" playerrace="yes" walk="yes" giveperson="yes" giveunit="yes" -getitem="yes" equipment="yes" healing="2.0" items="4"> +getitem="yes" equipment="yes" healing="2.0"> diff --git a/scripts/tests/e3/items.lua b/scripts/tests/e3/items.lua index 5594eed67..349e12b2a 100644 --- a/scripts/tests/e3/items.lua +++ b/scripts/tests/e3/items.lua @@ -22,8 +22,8 @@ function test_goblins() local restricted = { "towershield", "rep_crossbow", "plate", "lance", - "mllornlance", "greatbow", "greataxe", "axe", "scale", - "plate", "rustyhalberd", "halberd", "greatsword" + "mallornlance", "greatbow", "greataxe", "axe", "scale", + "plate", "halberd", "greatsword", "rustyhalberd" } for k, v in ipairs(restricted) do ud:add_item(v, 1) diff --git a/src/kernel/race.c b/src/kernel/race.c index efe38dcfe..8a8cc906e 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -328,7 +328,7 @@ bool rc_changed(int *cache) { bool rc_can_use(const struct race *rc, const struct item_type *itype) { if (itype->mask_allow) { - return (rc->mask_item==0 || (itype->mask_allow & rc->mask_item) != 0); + return (itype->mask_allow & rc->mask_item) != 0; } if (itype->mask_deny) { return (itype->mask_deny & rc->mask_item) == 0; diff --git a/src/kernel/race.test.c b/src/kernel/race.test.c index b292a825e..2db76a6fd 100644 --- a/src/kernel/race.test.c +++ b/src/kernel/race.test.c @@ -146,7 +146,7 @@ static void test_rc_can_use(CuTest *tc) { /* we are not special */ rc->mask_item = 0; - CuAssertTrue(tc, rc_can_use(rc, itype)); + CuAssertTrue(tc, ! rc_can_use(rc, itype)); test_cleanup(); } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 3a6ba6d81..208a586a7 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -757,6 +757,27 @@ static weapon_type *xml_readweapon(xmlXPathContextPtr xpath, item_type * itype) return wtype; } +static int race_mask = 1; + +static void mask_races(xmlNodePtr node, const char *key, int *maskp) { + xmlChar *propValue = xmlGetProp(node, BAD_CAST key); + char *tok; + int mask = 0; + assert(maskp); + tok = strtok((char *)propValue, " ,"); + while (tok) { + race * rc = rc_get_or_create(tok); + if (!rc->mask_item) { + rc->mask_item = race_mask; + race_mask = race_mask << 1; + } + mask |= rc->mask_item; + tok = strtok(NULL, " ,"); + } + *maskp = mask; + xmlFree(propValue); +} + static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype) { xmlNodePtr node = xpath->node; @@ -782,8 +803,8 @@ static item_type *xml_readitem(xmlXPathContextPtr xpath, resource_type * rtype) itype = rtype->itype ? rtype->itype : it_get_or_create(rtype); itype->weight = xml_ivalue(node, "weight", 0); itype->capacity = xml_ivalue(node, "capacity", 0); - itype->mask_allow = xml_ivalue(node, "allow", 0); - itype->mask_deny = xml_ivalue(node, "deny", 0); + mask_races(node, "allow", &itype->mask_allow); + mask_races(node, "deny", &itype->mask_deny); itype->flags |= flags; /* reading item/construction */ @@ -1577,7 +1598,6 @@ static int parse_races(xmlDocPtr doc) rc->speed = (float)xml_fvalue(node, "speed", rc->speed); rc->hitpoints = xml_ivalue(node, "hp", rc->hitpoints); rc->armor = (char)xml_ivalue(node, "ac", rc->armor); - rc->mask_item = (char)xml_ivalue(node, "items", rc->mask_item); study_speed_base = xml_ivalue(node, "studyspeed", 0); rc->at_default = (char)xml_ivalue(node, "unarmedattack", -2);