cleaning up terrains, reading race flags from a JSON array

This commit is contained in:
Enno Rehling 2014-06-16 22:43:40 -07:00
parent 33928568cf
commit 5c78a3883e
4 changed files with 40 additions and 37 deletions

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<terrains> <terrains>
<!-- defaults: build="yes" walk="yes" sail="yes" fly="yes" shallow="yes" swim="no" forest="no" sea="no" land="yes" forbidden="no" arctic="no" cavalry="no" size="0" --> <!-- defaults: build="yes" walk="yes" sail="yes" fly="yes" swim="no" forest="no" sea="no" land="yes" forbidden="no" arctic="no" cavalry="no" size="0" -->
<terrain name="ocean" size="100" shallow="no" walk="no" swim="yes" land="no" sea="yes" /> <terrain name="ocean" size="100" walk="no" swim="yes" land="no" sea="yes" />
<terrain name="plain" size="4000" road="50" shallow="no" forest="yes" cavalry="yes" seed="3"> <terrain name="plain" size="4000" road="50" forest="yes" cavalry="yes" seed="3">
<herb name="h0" /> <herb name="h0" />
<herb name="h4" /> <herb name="h4" />
<resource name="iron" chance="0.1" level="2d4-1" base="5d8" div="2d20+10" /> <resource name="iron" chance="0.1" level="2d4-1" base="5d8" div="2d20+10" />

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<terrains> <terrains>
<!-- defaults: walk="yes" sail="yes" fly="yes" shallow="yes" swim="no" forest="no" sea="no" land="yes" forbidden="no" arctic="no" cavalry="no" size="0" --> <!-- defaults: walk="yes" sail="yes" fly="yes" swim="no" forest="no" sea="no" land="yes" forbidden="no" arctic="no" cavalry="no" size="0" -->
<terrain name="ocean" size="100" shallow="no" walk="no" swim="yes" land="no" sea="yes" /> <terrain name="ocean" size="100" walk="no" swim="yes" land="no" sea="yes" />
<terrain name="plain" size="4000" road="50" shallow="no" forest="yes" cavalry="yes" seed="3"> <terrain name="plain" size="4000" road="50" forest="yes" cavalry="yes" seed="3">
<resource name="iron" chance="0.1" level="2d4-1" base="5d8" div="2d20+10" /> <resource name="iron" chance="0.1" level="2d4-1" base="5d8" div="2d20+10" />
<resource name="stone" chance="0.15" level="1d4" base="5d8" div="2d30+20" /> <resource name="stone" chance="0.15" level="1d4" base="5d8" div="2d30+20" />
<resource name="laen" chance="0.01" level="1d4" base="1d4" div="2d20+50" /> <resource name="laen" chance="0.01" level="1d4" base="1d4" div="2d20+50" />

View File

@ -52,6 +52,23 @@ without prior permission by the authors of Eressea.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int json_flags(cJSON *json, const char *flags[]) {
cJSON *entry;
int result = 0;
assert(json->type==cJSON_Array);
for (entry=json->child;entry;entry=entry->next) {
if (entry->type == cJSON_String) {
int i;
for (i = 0; flags[i]; ++i) {
if (strcmp(flags[i], entry->valuestring)==0) {
result |= (1<<i);
}
}
}
}
return result;
}
void json_construction(cJSON *json, construction **consp) { void json_construction(cJSON *json, construction **consp) {
cJSON *child; cJSON *child;
if (json->type!=cJSON_Object) { if (json->type!=cJSON_Object) {
@ -89,20 +106,10 @@ void json_terrain(cJSON *json, terrain_type *ter) {
switch(child->type) { switch(child->type) {
case cJSON_Array: case cJSON_Array:
if (strcmp(child->string, "flags")==0) { if (strcmp(child->string, "flags")==0) {
cJSON *entry;
const char * flags[] = { const char * flags[] = {
"land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0 "land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0
}; };
for (entry=child->child;entry;entry=entry->next) { ter->flags = json_flags(child, flags);
if (entry->type == cJSON_String) {
int i;
for (i = 0; flags[i]; ++i) {
if (strcmp(flags[i], entry->valuestring)==0) {
ter->flags |= (1<<i);
}
}
}
}
} else { } else {
log_error_n("terrain %s contains unknown attribute %s", json->string, child->string); log_error_n("terrain %s contains unknown attribute %s", json->string, child->string);
} }
@ -162,6 +169,15 @@ void json_ship(cJSON *json, ship_type *st) {
void json_race(cJSON *json, race *rc) { void json_race(cJSON *json, race *rc) {
cJSON *child; cJSON *child;
const char *flags[] = {
"playerrace", "killpeasants", "scarepeasants",
"cansteal", "moverandom", "cannotmove",
"learn", "fly", "swim", "walk", "nolearn",
"noteach", "horse", "desert",
"illusionary", "absorbpeasants", "noheal",
"noweapons", "shapeshift", "", "undead", "dragon",
"coastal", "", "cansail", 0
};
if (json->type!=cJSON_Object) { if (json->type!=cJSON_Object) {
log_error_n("race %s is not a json object: %d", json->string, json->type); log_error_n("race %s is not a json object: %d", json->string, json->type);
return; return;
@ -206,28 +222,13 @@ void json_race(cJSON *json, race *rc) {
} }
// TODO: studyspeed (orcs only) // TODO: studyspeed (orcs only)
break; break;
case cJSON_True: { case cJSON_Array:
const char *flags[] = { if (strcmp(child->string, "flags")==0) {
"playerrace", "killpeasants", "scarepeasants", rc->flags = json_flags(child, flags);
"cansteal", "moverandom", "cannotmove",
"learn", "fly", "swim", "walk", "nolearn",
"noteach", "horse", "desert",
"illusionary", "absorbpeasants", "noheal",
"noweapons", "shapeshift", "", "undead", "dragon",
"coastal", "", "cansail", 0
};
int i;
for(i=0;flags[i];++i) {
const char * flag = flags[i];
if (*flag && strcmp(child->string, flag)==0) {
rc->flags |= (1<<i);
break;
}
} }
break; break;
} }
} }
}
} }
void json_terrains(cJSON *json) { void json_terrains(cJSON *json) {

View File

@ -18,7 +18,7 @@ static void check_flag(CuTest *tc, const char *name, int flag) {
char data[1024]; char data[1024];
const struct race *rc; const struct race *rc;
cJSON *json; cJSON *json;
sprintf(data, "{\"races\" : { \"orc\": { \"%s\" : true }}}", name); sprintf(data, "{\"races\" : { \"orc\": { \"flags\" : [ \"%s\"] }}}", name);
json = cJSON_Parse(data); json = cJSON_Parse(data);
free_races(); free_races();
@ -52,7 +52,8 @@ static void test_races(CuTest * tc)
"\"weight\" : 3," "\"weight\" : 3,"
"\"capacity\" : 4," "\"capacity\" : 4,"
"\"hp\" : 5," "\"hp\" : 5,"
"\"ac\" : 6" "\"ac\" : 6,"
"\"flags\" : [ \"playerrace\", \"walk\", \"undead\" ]"
"}}}"; "}}}";
cJSON *json = cJSON_Parse(data); cJSON *json = cJSON_Parse(data);
const struct race *rc; const struct race *rc;
@ -66,6 +67,7 @@ static void test_races(CuTest * tc)
CuAssertPtrNotNull(tc, races); CuAssertPtrNotNull(tc, races);
rc = rc_find("orc"); rc = rc_find("orc");
CuAssertPtrNotNull(tc, rc); CuAssertPtrNotNull(tc, rc);
CuAssertIntEquals(tc, RCF_PLAYERRACE|RCF_WALK|RCF_UNDEAD, rc->flags);
CuAssertStrEquals(tc, "1d4", rc->def_damage); CuAssertStrEquals(tc, "1d4", rc->def_damage);
CuAssertDblEquals(tc, 1.0, rc->magres, 0.0); CuAssertDblEquals(tc, 1.0, rc->magres, 0.0);
CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0); CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0);