diff --git a/src/kernel/equipment.test.c b/src/kernel/equipment.test.c index edc7f92e1..66872af0e 100644 --- a/src/kernel/equipment.test.c +++ b/src/kernel/equipment.test.c @@ -17,14 +17,13 @@ void test_equipment(CuTest * tc) equipment * eq; unit * u; const item_type * it_horses; - const char * names[] = {"horse", "horse_p"}; spell *sp; sc_mage * mage; test_cleanup(); test_create_race("human"); enable_skill(SK_MAGIC, true); - it_horses = test_create_itemtype(names); + it_horses = test_create_itemtype("horse"); CuAssertPtrNotNull(tc, it_horses); sp = create_spell("testspell", 0); CuAssertPtrNotNull(tc, sp); diff --git a/src/kernel/item.c b/src/kernel/item.c index ec307bbf2..1a2bbf879 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -147,6 +147,20 @@ const char *resourcename(const resource_type * rtype, int flags) return "none"; } +static int num_resources; + +static void rt_register(resource_type * rtype) +{ + char buffer[64]; + const char * name = rtype->_name[0]; + size_t len = strlen(name); + + assert(len_name[i] = _strdup(names[i]); - if (appearances) - rtype->_appearance[i] = _strdup(appearances[i]); - else - rtype->_appearance[i] = NULL; - } - } -#ifndef NDEBUG - else { - /* TODO: check that this is the same type */ - } -#endif - rtype->flags |= flags; - return rtype; -} - void it_register(item_type * itype) { char buffer[64]; @@ -230,9 +218,13 @@ item_type *it_get_or_create(resource_type *rtype) { item_type * itype; assert(rtype); itype = it_find(rtype->_name[0]); - assert(!itype); - itype = (item_type *)calloc(sizeof(item_type), 1); + assert(!itype || !itype->rtype || itype->rtype == rtype); + if (!itype) { + itype = (item_type *)calloc(sizeof(item_type), 1); + } itype->rtype = rtype; + rtype->uchange = res_changeitem; + rtype->itype = itype; rtype->flags |= RTF_ITEM; it_register(itype); return itype; @@ -348,18 +340,11 @@ potion_type *new_potiontype(item_type * itype, int level) return ptype; } -static int num_resources; - -void rt_register(resource_type * rtype) -{ - char buffer[64]; - const char * name = rtype->_name[0]; - size_t len = strlen(name); - - assert(lenrtype); + itype->rtype->_appearance[0] = _strdup(appearance); + itype->rtype->_appearance[1] = appearance ? + strcat(strcpy((char *)malloc(strlen((char *)appearance) + 3), (char *)appearance), "_p") : 0; } const resource_type *item2resource(const item_type * itype) @@ -964,70 +949,37 @@ static void init_oldpotions(void) } static const char *names[] = { - "money", "money_p", - "person", "person_p", - "permaura", "permaura_p", - "hp", "hp_p", - "peasant", "peasant_p", - "aura", "aura_p", - "unit", "unit_p" + "money", "person", "permaura", "hp", "peasant", "aura", "unit", }; void init_resources(void) { resource_type *rtype; - item_type *itype; - rtype = rt_find(names[8]); - if (!rtype) { - rtype = new_resourcetype(names + 8, NULL, RTF_NONE); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_PEASANT]); rtype->uchange = res_changepeasants; // R_SILVER - rtype = rt_find(names[0]); - if (!rtype) { - rtype = new_resourcetype(&names[0], NULL, RTF_ITEM | RTF_POOLED); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_SILVER]); + rtype->flags |= RTF_ITEM | RTF_POOLED; rtype->uchange = res_changeitem; - itype = rtype->itype; - if (!itype) { - itype = new_itemtype(rtype, ITF_NONE, 1 /*weight */ , 0); - } - itype->give = give_money; + rtype->itype = it_get_or_create(rtype); + rtype->itype->give = give_money; // R_PERMAURA - rtype = rt_find(names[4]); - if (!rtype) { - rtype = new_resourcetype(&names[4], NULL, RTF_NONE); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_PERMAURA]); rtype->uchange = res_changepermaura; // R_LIFE - rtype = rt_find(names[6]); - if (!rtype) { - rtype = new_resourcetype(&names[6], NULL, RTF_NONE); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_LIFE]); rtype->uchange = res_changehp; // R_AURA - rtype = rt_find(names[10]); - if (!rtype) { - rtype = new_resourcetype(&names[10], NULL, RTF_NONE); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_AURA]); rtype->uchange = res_changeaura; // R_UNIT - rtype = rt_find(names[12]); - if (!rtype) { - rtype = new_resourcetype(&names[12], NULL, RTF_NONE); - rt_register(rtype); - } + rtype = rt_get_or_create(resourcenames[R_UNIT]); rtype->uchange = res_changeperson; /* alte typen registrieren: */ @@ -1250,7 +1202,8 @@ void test_clear_resources(void) ++num_resources; for (i=0; i!=MAXLOCALES; ++i) { - cb_clear(inames+i); + cb_clear(inames + i); + cb_clear(rnames + i); } } #endif diff --git a/src/kernel/item.h b/src/kernel/item.h index a80da6c2e..1466d02fb 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -225,12 +225,12 @@ extern "C" { int *deaths); } weapon_type; - void rt_register(resource_type * it); resource_type *rt_find(const char *name); item_type *it_find(const char *name); - extern void it_register(item_type * it); - extern void wt_register(weapon_type * wt); + void it_set_appearance(item_type *itype, const char *appearance); + void it_register(item_type * it); + void wt_register(weapon_type * wt); extern const item_type *resource2item(const resource_type * rtype); extern const resource_type *item2resource(const item_type * i); @@ -254,7 +254,7 @@ extern "C" { /* creation */ resource_type *rt_get_or_create(const char *name); - resource_type *new_resourcetype(const char **names, const char **appearances, int flags); +// resource_type *new_resourcetype(const char **names, const char **appearances, int flags); item_type *it_get_or_create(resource_type *rtype); extern item_type *new_itemtype(resource_type * rtype, int iflags, int weight, int capacity); diff --git a/src/kernel/item.test.c b/src/kernel/item.test.c index 626090eae..3613777c8 100644 --- a/src/kernel/item.test.c +++ b/src/kernel/item.test.c @@ -51,20 +51,14 @@ void test_change_person(CuTest * tc) void test_resource_type(CuTest * tc) { struct item_type *itype; - const char *names[2] = { 0 , 0 }; test_cleanup(); CuAssertPtrEquals(tc, 0, rt_find("herpderp")); - names[0] = names[1] = "herpderp"; - test_create_itemtype(names); - - names[0] = names[1] = "herp"; - itype = test_create_itemtype(names); - - names[0] = names[1] = "herpes"; - test_create_itemtype(names); + test_create_itemtype("herpderp"); + test_create_itemtype("herpes"); + itype = test_create_itemtype("herp"); CuAssertPtrEquals(tc, itype->rtype, rt_find("herp")); } diff --git a/src/kernel/move.test.c b/src/kernel/move.test.c index 6b4eb015b..5e1d0ecae 100644 --- a/src/kernel/move.test.c +++ b/src/kernel/move.test.c @@ -18,14 +18,13 @@ static void test_ship_not_allowed_in_coast(CuTest * tc) ship * sh; terrain_type *ttype, *otype; ship_type *stype; - const char * names[] = { "derp", "derp_p" }; test_cleanup(); test_create_world(); ttype = test_create_terrain("glacier", LAND_REGION|ARCTIC_REGION|WALK_INTO|SAIL_INTO); otype = test_create_terrain("ocean", SEA_REGION|SAIL_INTO); - stype = test_create_shiptype(names); + stype = test_create_shiptype("derp"); stype->coasts = (const struct terrain_type **)calloc(2, sizeof(const struct terrain_type *)); r1 = test_create_region(0, 0, ttype); diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 4cc8ba358..01c42a67e 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -105,22 +105,6 @@ static xmlChar *xml_cleanup_string(xmlChar * str) return str; } -static const resource_type *rt_findorcreate(const char *name) -{ - resource_type *rtype = rt_find(name); - if (rtype == NULL) { - const char *names[2]; - char *namep = strcat(strcpy((char *)malloc(strlen(name) + 3), name), "_p"); - /* we'll make a placeholder */ - names[0] = name; - names[1] = namep; - rtype = new_resourcetype(names, NULL, RTF_NONE); - rt_register(rtype); - free(namep); - } - return rtype; -} - static void xml_readrequirements(xmlNodePtr * nodeTab, int nodeNr, requirement ** reqArray) { @@ -141,7 +125,7 @@ xml_readrequirements(xmlNodePtr * nodeTab, int nodeNr, requirement ** reqArray) radd->recycle = xml_fvalue(node, "recycle", 0.5); propValue = xmlGetProp(node, BAD_CAST "type"); - radd->rtype = rt_findorcreate((const char *)propValue); + radd->rtype = rt_get_or_create((const char *)propValue); xmlFree(propValue); ++radd; @@ -978,10 +962,9 @@ static int parse_resources(xmlDocPtr doc) for (i = 0; i != nodes->nodeNr; ++i) { xmlNodePtr node = nodes->nodeTab[i]; xmlChar *propValue, *name, *appearance; - const char *names[2], *appearances[2]; - char *namep = NULL, *appearancep = NULL; resource_type *rtype; - unsigned int flags = RTF_NONE; + item_type *itype; + unsigned int flags = RTF_ITEM; xmlXPathObjectPtr result; int k; @@ -994,44 +977,15 @@ static int parse_resources(xmlDocPtr doc) appearance = xmlGetProp(node, BAD_CAST "appearance"); assert(name != NULL); - if (appearance != NULL) { - appearancep = - strcat(strcpy((char *)malloc(strlen((char *)appearance) + 3), - (char *)appearance), "_p"); + rtype = rt_get_or_create((const char *)name); + rtype->flags |= flags; + itype = rtype->itype ? rtype->itype : it_get_or_create(rtype); + if (appearance) { + it_set_appearance(itype, (const char *)appearance); } - rtype = rt_find((const char *)name); - if (rtype != NULL) { - /* dependency from another item, was created earlier */ - rtype->flags |= flags; - if (appearance) { - rtype->_appearance[0] = _strdup((const char *)appearance); - rtype->_appearance[1] = appearancep; - free(appearancep); - } - } else { - namep = - strcat(strcpy((char *)malloc(strlen((char *)name) + 3), (char *)name), - "_p"); - names[0] = (const char *)name; - names[1] = namep; - if (appearance) { - appearances[0] = (const char *)appearance; - appearances[1] = appearancep; - rtype = new_resourcetype((const char **)names, (const char **)appearances, flags); - rt_register(rtype); - free(appearancep); - } else { - rtype = new_resourcetype(names, NULL, flags); - rt_register(rtype); - } - free(namep); - } - - if (name) - xmlFree(name); - if (appearance) - xmlFree(appearance); + if (name) xmlFree(name); + if (appearance) xmlFree(appearance); name = xmlGetProp(node, BAD_CAST "material"); if (name) { diff --git a/src/market.test.c b/src/market.test.c index 71f9bcf24..3670dd4f3 100644 --- a/src/market.test.c +++ b/src/market.test.c @@ -26,7 +26,6 @@ static void test_market_curse(CuTest * tc) unit *u; faction *f; int x, y; - const char *names[4] = { "herb", "herbs", "balm", "balms" }; const terrain_type *terrain; item_type *htype, *ltype; luxury_type *lux; @@ -36,11 +35,11 @@ static void test_market_curse(CuTest * tc) test_cleanup(); test_create_world(); - htype = test_create_itemtype(names); + htype = test_create_itemtype("herb"); htype->flags |= ITF_HERB; htype->rtype->flags |= (RTF_ITEM | RTF_POOLED); - ltype = test_create_itemtype(names + 2); + ltype = test_create_itemtype("balm"); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED); lux = new_luxurytype(ltype, 0); diff --git a/src/tests.c b/src/tests.c index 0ed746388..00e8db540 100644 --- a/src/tests.c +++ b/src/tests.c @@ -93,10 +93,10 @@ ship * test_create_ship(region * r, const ship_type * stype) return s; } -ship_type * test_create_shiptype(const char ** names) +ship_type * test_create_shiptype(const char * name) { - ship_type * stype = st_get_or_create(names[0]); - locale_setstring(default_locale, names[0], names[0]); + ship_type * stype = st_get_or_create(name); + locale_setstring(default_locale, name, name); return stype; } @@ -119,13 +119,12 @@ building_type * test_create_buildingtype(const char * name) return btype; } -item_type * test_create_itemtype(const char ** names) { +item_type * test_create_itemtype(const char * name) { resource_type * rtype; item_type * itype; - rtype = new_resourcetype(names, NULL, RTF_ITEM); - rt_register(rtype); - itype = new_itemtype(rtype, ITF_ANIMAL | ITF_BIG, 5000, 7000); + rtype = rt_get_or_create(name); + itype = it_get_or_create(rtype); return itype; } @@ -142,14 +141,18 @@ void test_create_world(void) terrain_type *t_plain, *t_ocean; region *island[2]; int i; - const char * names[] = { "horse", "horse_p", "boat", "boat_p", "iron", "iron_p", "stone", "stone_p" }; + item_type * itype; get_or_create_locale("de"); init_resources(); - test_create_itemtype(names+0); - test_create_itemtype(names+4); - test_create_itemtype(names + 6); + itype = test_create_itemtype("horse"); + itype->flags |= ITF_BIG | ITF_ANIMAL; + itype->weight = 5000; + itype->capacity = 7000; + + test_create_itemtype("iron"); + test_create_itemtype("stone"); t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); t_plain->size = 1000; @@ -173,6 +176,6 @@ void test_create_world(void) test_create_race("human"); test_create_buildingtype("castle"); - test_create_shiptype(names+2); + test_create_shiptype("boat"); } diff --git a/src/tests.h b/src/tests.h index d7b54e75e..9cbcbcac1 100644 --- a/src/tests.h +++ b/src/tests.h @@ -15,8 +15,8 @@ extern "C" { void test_create_world(void); struct building * test_create_building(struct region * r, const struct building_type * btype); struct ship * test_create_ship(struct region * r, const struct ship_type * stype); - struct item_type * test_create_itemtype(const char ** names); - struct ship_type *test_create_shiptype(const char **names); + struct item_type * test_create_itemtype(const char * name); + struct ship_type *test_create_shiptype(const char * name); struct building_type *test_create_buildingtype(const char *name); int RunAllTests(void); diff --git a/src/tests.test.c b/src/tests.test.c index e0fb05990..d18775c56 100644 --- a/src/tests.test.c +++ b/src/tests.test.c @@ -10,21 +10,27 @@ #include static void test_resources(CuTest *tc) { - const char *names[] = { "horse", "horse_p" }; resource_type *rtype; test_cleanup(); - CuAssertPtrEquals(tc, 0, rt_find("horse")); - rtype = new_resourcetype(names, 0, 0); - CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); - rt_register(rtype); - CuAssertPtrEquals(tc, (void *)rtype, (void *)rt_find("horse")); - CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_HORSE)); + CuAssertPtrNotNull(tc, rt_find("hp")); + CuAssertPtrEquals(tc, rt_find("hp"), (void *)get_resourcetype(R_LIFE)); + CuAssertPtrNotNull(tc, rt_find("peasant")); + CuAssertPtrEquals(tc, rt_find("peasant"), (void *)get_resourcetype(R_PEASANT)); + CuAssertPtrNotNull(tc, rt_find("aura")); + CuAssertPtrEquals(tc, rt_find("aura"), (void *)get_resourcetype(R_AURA)); + CuAssertPtrNotNull(tc, rt_find("permaura")); + CuAssertPtrEquals(tc, rt_find("permaura"), (void *)get_resourcetype(R_PERMAURA)); + CuAssertPtrNotNull(tc, rt_find("unit")); + CuAssertPtrEquals(tc, rt_find("unit"), (void *)get_resourcetype(R_UNIT)); + + CuAssertPtrEquals(tc, 0, rt_find("stone")); + rtype = rt_get_or_create("stone"); + CuAssertPtrEquals(tc, (void *)rtype, (void *)rt_find("stone")); + CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_STONE)); test_cleanup(); - CuAssertPtrEquals(tc, 0, rt_find("horse")); - rtype = new_resourcetype(names, 0, 0); - CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); - rt_register(rtype); - CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_HORSE)); + CuAssertPtrEquals(tc, 0, rt_find("stone")); + rtype = rt_get_or_create("stone"); + CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_STONE)); } static void test_recreate_world(CuTest * tc)