rt_register is no longer required, new_resourcetype replaced by rt_get_or_create.

This commit is contained in:
Enno Rehling 2014-06-29 17:02:45 -07:00
parent 95929a99b5
commit 5d32896407
10 changed files with 93 additions and 186 deletions

View file

@ -17,14 +17,13 @@ void test_equipment(CuTest * tc)
equipment * eq; equipment * eq;
unit * u; unit * u;
const item_type * it_horses; const item_type * it_horses;
const char * names[] = {"horse", "horse_p"};
spell *sp; spell *sp;
sc_mage * mage; sc_mage * mage;
test_cleanup(); test_cleanup();
test_create_race("human"); test_create_race("human");
enable_skill(SK_MAGIC, true); enable_skill(SK_MAGIC, true);
it_horses = test_create_itemtype(names); it_horses = test_create_itemtype("horse");
CuAssertPtrNotNull(tc, it_horses); CuAssertPtrNotNull(tc, it_horses);
sp = create_spell("testspell", 0); sp = create_spell("testspell", 0);
CuAssertPtrNotNull(tc, sp); CuAssertPtrNotNull(tc, sp);

View file

@ -147,6 +147,20 @@ const char *resourcename(const resource_type * rtype, int flags)
return "none"; 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<sizeof(buffer) - sizeof(rtype));
len = cb_new_kv(name, len, &rtype, sizeof(rtype), buffer);
cb_insert(&cb_resources, buffer, len);
++num_resources;
}
resource_type *rt_get_or_create(const char *name) { resource_type *rt_get_or_create(const char *name) {
resource_type *rtype = rt_find(name); resource_type *rtype = rt_find(name);
if (!rtype) { if (!rtype) {
@ -159,32 +173,6 @@ resource_type *rt_get_or_create(const char *name) {
return rtype; return rtype;
} }
resource_type *new_resourcetype(const char **names, const char **appearances,
int flags)
{
resource_type *rtype = rt_find(names[0]);
if (rtype == NULL) {
int i;
rtype = (resource_type *)calloc(sizeof(resource_type), 1);
for (i = 0; i != 2; ++i) {
rtype->_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) void it_register(item_type * itype)
{ {
char buffer[64]; char buffer[64];
@ -230,9 +218,13 @@ item_type *it_get_or_create(resource_type *rtype) {
item_type * itype; item_type * itype;
assert(rtype); assert(rtype);
itype = it_find(rtype->_name[0]); itype = it_find(rtype->_name[0]);
assert(!itype); assert(!itype || !itype->rtype || itype->rtype == rtype);
itype = (item_type *)calloc(sizeof(item_type), 1); if (!itype) {
itype = (item_type *)calloc(sizeof(item_type), 1);
}
itype->rtype = rtype; itype->rtype = rtype;
rtype->uchange = res_changeitem;
rtype->itype = itype;
rtype->flags |= RTF_ITEM; rtype->flags |= RTF_ITEM;
it_register(itype); it_register(itype);
return itype; return itype;
@ -348,18 +340,11 @@ potion_type *new_potiontype(item_type * itype, int level)
return ptype; return ptype;
} }
static int num_resources; void it_set_appearance(item_type *itype, const char *appearance) {
assert(itype && itype->rtype);
void rt_register(resource_type * rtype) itype->rtype->_appearance[0] = _strdup(appearance);
{ itype->rtype->_appearance[1] = appearance ?
char buffer[64]; strcat(strcpy((char *)malloc(strlen((char *)appearance) + 3), (char *)appearance), "_p") : 0;
const char * name = rtype->_name[0];
size_t len = strlen(name);
assert(len<sizeof(buffer)-sizeof(rtype));
len = cb_new_kv(name, len, &rtype, sizeof(rtype), buffer);
cb_insert(&cb_resources, buffer, len);
++num_resources;
} }
const resource_type *item2resource(const item_type * itype) const resource_type *item2resource(const item_type * itype)
@ -964,70 +949,37 @@ static void init_oldpotions(void)
} }
static const char *names[] = { static const char *names[] = {
"money", "money_p", "money", "person", "permaura", "hp", "peasant", "aura", "unit",
"person", "person_p",
"permaura", "permaura_p",
"hp", "hp_p",
"peasant", "peasant_p",
"aura", "aura_p",
"unit", "unit_p"
}; };
void init_resources(void) void init_resources(void)
{ {
resource_type *rtype; resource_type *rtype;
item_type *itype;
rtype = rt_find(names[8]); rtype = rt_get_or_create(resourcenames[R_PEASANT]);
if (!rtype) {
rtype = new_resourcetype(names + 8, NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changepeasants; rtype->uchange = res_changepeasants;
// R_SILVER // R_SILVER
rtype = rt_find(names[0]); rtype = rt_get_or_create(resourcenames[R_SILVER]);
if (!rtype) { rtype->flags |= RTF_ITEM | RTF_POOLED;
rtype = new_resourcetype(&names[0], NULL, RTF_ITEM | RTF_POOLED);
rt_register(rtype);
}
rtype->uchange = res_changeitem; rtype->uchange = res_changeitem;
itype = rtype->itype; rtype->itype = it_get_or_create(rtype);
if (!itype) { rtype->itype->give = give_money;
itype = new_itemtype(rtype, ITF_NONE, 1 /*weight */ , 0);
}
itype->give = give_money;
// R_PERMAURA // R_PERMAURA
rtype = rt_find(names[4]); rtype = rt_get_or_create(resourcenames[R_PERMAURA]);
if (!rtype) {
rtype = new_resourcetype(&names[4], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changepermaura; rtype->uchange = res_changepermaura;
// R_LIFE // R_LIFE
rtype = rt_find(names[6]); rtype = rt_get_or_create(resourcenames[R_LIFE]);
if (!rtype) {
rtype = new_resourcetype(&names[6], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changehp; rtype->uchange = res_changehp;
// R_AURA // R_AURA
rtype = rt_find(names[10]); rtype = rt_get_or_create(resourcenames[R_AURA]);
if (!rtype) {
rtype = new_resourcetype(&names[10], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changeaura; rtype->uchange = res_changeaura;
// R_UNIT // R_UNIT
rtype = rt_find(names[12]); rtype = rt_get_or_create(resourcenames[R_UNIT]);
if (!rtype) {
rtype = new_resourcetype(&names[12], NULL, RTF_NONE);
rt_register(rtype);
}
rtype->uchange = res_changeperson; rtype->uchange = res_changeperson;
/* alte typen registrieren: */ /* alte typen registrieren: */
@ -1250,7 +1202,8 @@ void test_clear_resources(void)
++num_resources; ++num_resources;
for (i=0; i!=MAXLOCALES; ++i) { for (i=0; i!=MAXLOCALES; ++i) {
cb_clear(inames+i); cb_clear(inames + i);
cb_clear(rnames + i);
} }
} }
#endif #endif

View file

@ -225,12 +225,12 @@ extern "C" {
int *deaths); int *deaths);
} weapon_type; } weapon_type;
void rt_register(resource_type * it);
resource_type *rt_find(const char *name); resource_type *rt_find(const char *name);
item_type *it_find(const char *name); item_type *it_find(const char *name);
extern void it_register(item_type * it); void it_set_appearance(item_type *itype, const char *appearance);
extern void wt_register(weapon_type * wt); void it_register(item_type * it);
void wt_register(weapon_type * wt);
extern const item_type *resource2item(const resource_type * rtype); extern const item_type *resource2item(const resource_type * rtype);
extern const resource_type *item2resource(const item_type * i); extern const resource_type *item2resource(const item_type * i);
@ -254,7 +254,7 @@ extern "C" {
/* creation */ /* creation */
resource_type *rt_get_or_create(const char *name); 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); item_type *it_get_or_create(resource_type *rtype);
extern item_type *new_itemtype(resource_type * rtype, int iflags, int weight, extern item_type *new_itemtype(resource_type * rtype, int iflags, int weight,
int capacity); int capacity);

View file

@ -51,20 +51,14 @@ void test_change_person(CuTest * tc)
void test_resource_type(CuTest * tc) void test_resource_type(CuTest * tc)
{ {
struct item_type *itype; struct item_type *itype;
const char *names[2] = { 0 , 0 };
test_cleanup(); test_cleanup();
CuAssertPtrEquals(tc, 0, rt_find("herpderp")); CuAssertPtrEquals(tc, 0, rt_find("herpderp"));
names[0] = names[1] = "herpderp"; test_create_itemtype("herpderp");
test_create_itemtype(names); test_create_itemtype("herpes");
itype = test_create_itemtype("herp");
names[0] = names[1] = "herp";
itype = test_create_itemtype(names);
names[0] = names[1] = "herpes";
test_create_itemtype(names);
CuAssertPtrEquals(tc, itype->rtype, rt_find("herp")); CuAssertPtrEquals(tc, itype->rtype, rt_find("herp"));
} }

View file

@ -18,14 +18,13 @@ static void test_ship_not_allowed_in_coast(CuTest * tc)
ship * sh; ship * sh;
terrain_type *ttype, *otype; terrain_type *ttype, *otype;
ship_type *stype; ship_type *stype;
const char * names[] = { "derp", "derp_p" };
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
ttype = test_create_terrain("glacier", LAND_REGION|ARCTIC_REGION|WALK_INTO|SAIL_INTO); ttype = test_create_terrain("glacier", LAND_REGION|ARCTIC_REGION|WALK_INTO|SAIL_INTO);
otype = test_create_terrain("ocean", SEA_REGION|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 *)); stype->coasts = (const struct terrain_type **)calloc(2, sizeof(const struct terrain_type *));
r1 = test_create_region(0, 0, ttype); r1 = test_create_region(0, 0, ttype);

View file

@ -105,22 +105,6 @@ static xmlChar *xml_cleanup_string(xmlChar * str)
return 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 static void
xml_readrequirements(xmlNodePtr * nodeTab, int nodeNr, requirement ** reqArray) 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); radd->recycle = xml_fvalue(node, "recycle", 0.5);
propValue = xmlGetProp(node, BAD_CAST "type"); propValue = xmlGetProp(node, BAD_CAST "type");
radd->rtype = rt_findorcreate((const char *)propValue); radd->rtype = rt_get_or_create((const char *)propValue);
xmlFree(propValue); xmlFree(propValue);
++radd; ++radd;
@ -978,10 +962,9 @@ static int parse_resources(xmlDocPtr doc)
for (i = 0; i != nodes->nodeNr; ++i) { for (i = 0; i != nodes->nodeNr; ++i) {
xmlNodePtr node = nodes->nodeTab[i]; xmlNodePtr node = nodes->nodeTab[i];
xmlChar *propValue, *name, *appearance; xmlChar *propValue, *name, *appearance;
const char *names[2], *appearances[2];
char *namep = NULL, *appearancep = NULL;
resource_type *rtype; resource_type *rtype;
unsigned int flags = RTF_NONE; item_type *itype;
unsigned int flags = RTF_ITEM;
xmlXPathObjectPtr result; xmlXPathObjectPtr result;
int k; int k;
@ -994,44 +977,15 @@ static int parse_resources(xmlDocPtr doc)
appearance = xmlGetProp(node, BAD_CAST "appearance"); appearance = xmlGetProp(node, BAD_CAST "appearance");
assert(name != NULL); assert(name != NULL);
if (appearance != NULL) { rtype = rt_get_or_create((const char *)name);
appearancep = rtype->flags |= flags;
strcat(strcpy((char *)malloc(strlen((char *)appearance) + 3), itype = rtype->itype ? rtype->itype : it_get_or_create(rtype);
(char *)appearance), "_p"); if (appearance) {
it_set_appearance(itype, (const char *)appearance);
} }
rtype = rt_find((const char *)name); if (name) xmlFree(name);
if (rtype != NULL) { if (appearance) xmlFree(appearance);
/* 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);
name = xmlGetProp(node, BAD_CAST "material"); name = xmlGetProp(node, BAD_CAST "material");
if (name) { if (name) {

View file

@ -26,7 +26,6 @@ static void test_market_curse(CuTest * tc)
unit *u; unit *u;
faction *f; faction *f;
int x, y; int x, y;
const char *names[4] = { "herb", "herbs", "balm", "balms" };
const terrain_type *terrain; const terrain_type *terrain;
item_type *htype, *ltype; item_type *htype, *ltype;
luxury_type *lux; luxury_type *lux;
@ -36,11 +35,11 @@ static void test_market_curse(CuTest * tc)
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
htype = test_create_itemtype(names); htype = test_create_itemtype("herb");
htype->flags |= ITF_HERB; htype->flags |= ITF_HERB;
htype->rtype->flags |= (RTF_ITEM | RTF_POOLED); 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); ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED);
lux = new_luxurytype(ltype, 0); lux = new_luxurytype(ltype, 0);

View file

@ -93,10 +93,10 @@ ship * test_create_ship(region * r, const ship_type * stype)
return s; 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]); ship_type * stype = st_get_or_create(name);
locale_setstring(default_locale, names[0], names[0]); locale_setstring(default_locale, name, name);
return stype; return stype;
} }
@ -119,13 +119,12 @@ building_type * test_create_buildingtype(const char * name)
return btype; return btype;
} }
item_type * test_create_itemtype(const char ** names) { item_type * test_create_itemtype(const char * name) {
resource_type * rtype; resource_type * rtype;
item_type * itype; item_type * itype;
rtype = new_resourcetype(names, NULL, RTF_ITEM); rtype = rt_get_or_create(name);
rt_register(rtype); itype = it_get_or_create(rtype);
itype = new_itemtype(rtype, ITF_ANIMAL | ITF_BIG, 5000, 7000);
return itype; return itype;
} }
@ -142,14 +141,18 @@ void test_create_world(void)
terrain_type *t_plain, *t_ocean; terrain_type *t_plain, *t_ocean;
region *island[2]; region *island[2];
int i; 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"); get_or_create_locale("de");
init_resources(); init_resources();
test_create_itemtype(names+0); itype = test_create_itemtype("horse");
test_create_itemtype(names+4); itype->flags |= ITF_BIG | ITF_ANIMAL;
test_create_itemtype(names + 6); 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 = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION);
t_plain->size = 1000; t_plain->size = 1000;
@ -173,6 +176,6 @@ void test_create_world(void)
test_create_race("human"); test_create_race("human");
test_create_buildingtype("castle"); test_create_buildingtype("castle");
test_create_shiptype(names+2); test_create_shiptype("boat");
} }

View file

@ -15,8 +15,8 @@ extern "C" {
void test_create_world(void); void test_create_world(void);
struct building * test_create_building(struct region * r, const struct building_type * btype); 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 ship * test_create_ship(struct region * r, const struct ship_type * stype);
struct item_type * test_create_itemtype(const char ** names); struct item_type * test_create_itemtype(const char * name);
struct ship_type *test_create_shiptype(const char **names); struct ship_type *test_create_shiptype(const char * name);
struct building_type *test_create_buildingtype(const char *name); struct building_type *test_create_buildingtype(const char *name);
int RunAllTests(void); int RunAllTests(void);

View file

@ -10,21 +10,27 @@
#include <tests.h> #include <tests.h>
static void test_resources(CuTest *tc) { static void test_resources(CuTest *tc) {
const char *names[] = { "horse", "horse_p" };
resource_type *rtype; resource_type *rtype;
test_cleanup(); test_cleanup();
CuAssertPtrEquals(tc, 0, rt_find("horse")); CuAssertPtrNotNull(tc, rt_find("hp"));
rtype = new_resourcetype(names, 0, 0); CuAssertPtrEquals(tc, rt_find("hp"), (void *)get_resourcetype(R_LIFE));
CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, rt_find("peasant"));
rt_register(rtype); CuAssertPtrEquals(tc, rt_find("peasant"), (void *)get_resourcetype(R_PEASANT));
CuAssertPtrEquals(tc, (void *)rtype, (void *)rt_find("horse")); CuAssertPtrNotNull(tc, rt_find("aura"));
CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_HORSE)); 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(); test_cleanup();
CuAssertPtrEquals(tc, 0, rt_find("horse")); CuAssertPtrEquals(tc, 0, rt_find("stone"));
rtype = new_resourcetype(names, 0, 0); rtype = rt_get_or_create("stone");
CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_STONE));
rt_register(rtype);
CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_HORSE));
} }
static void test_recreate_world(CuTest * tc) static void test_recreate_world(CuTest * tc)