forked from github/server
cleaning up terrains, reading race flags from a JSON array
This commit is contained in:
parent
33928568cf
commit
5c78a3883e
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<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" -->
|
||||
<terrain name="ocean" size="100" shallow="no" walk="no" swim="yes" land="no" sea="yes" />
|
||||
<terrain name="plain" size="4000" road="50" shallow="no" forest="yes" cavalry="yes" seed="3">
|
||||
<!-- 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" walk="no" swim="yes" land="no" sea="yes" />
|
||||
<terrain name="plain" size="4000" road="50" forest="yes" cavalry="yes" seed="3">
|
||||
<herb name="h0" />
|
||||
<herb name="h4" />
|
||||
<resource name="iron" chance="0.1" level="2d4-1" base="5d8" div="2d20+10" />
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<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" -->
|
||||
<terrain name="ocean" size="100" shallow="no" walk="no" swim="yes" land="no" sea="yes" />
|
||||
<terrain name="plain" size="4000" road="50" shallow="no" forest="yes" cavalry="yes" seed="3">
|
||||
<!-- 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" walk="no" swim="yes" land="no" sea="yes" />
|
||||
<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="stone" chance="0.15" level="1d4" base="5d8" div="2d30+20" />
|
||||
<resource name="laen" chance="0.01" level="1d4" base="1d4" div="2d20+50" />
|
||||
|
|
|
@ -52,6 +52,23 @@ without prior permission by the authors of Eressea.
|
|||
#include <stdlib.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) {
|
||||
cJSON *child;
|
||||
if (json->type!=cJSON_Object) {
|
||||
|
@ -89,20 +106,10 @@ void json_terrain(cJSON *json, terrain_type *ter) {
|
|||
switch(child->type) {
|
||||
case cJSON_Array:
|
||||
if (strcmp(child->string, "flags")==0) {
|
||||
cJSON *entry;
|
||||
const char * flags[] = {
|
||||
"land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0
|
||||
};
|
||||
for (entry=child->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) {
|
||||
ter->flags |= (1<<i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ter->flags = json_flags(child, flags);
|
||||
} else {
|
||||
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) {
|
||||
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) {
|
||||
log_error_n("race %s is not a json object: %d", json->string, json->type);
|
||||
return;
|
||||
|
@ -206,27 +222,12 @@ void json_race(cJSON *json, race *rc) {
|
|||
}
|
||||
// TODO: studyspeed (orcs only)
|
||||
break;
|
||||
case cJSON_True: {
|
||||
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
|
||||
};
|
||||
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;
|
||||
}
|
||||
case cJSON_Array:
|
||||
if (strcmp(child->string, "flags")==0) {
|
||||
rc->flags = json_flags(child, flags);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ static void check_flag(CuTest *tc, const char *name, int flag) {
|
|||
char data[1024];
|
||||
const struct race *rc;
|
||||
cJSON *json;
|
||||
sprintf(data, "{\"races\" : { \"orc\": { \"%s\" : true }}}", name);
|
||||
sprintf(data, "{\"races\" : { \"orc\": { \"flags\" : [ \"%s\"] }}}", name);
|
||||
|
||||
json = cJSON_Parse(data);
|
||||
free_races();
|
||||
|
@ -52,7 +52,8 @@ static void test_races(CuTest * tc)
|
|||
"\"weight\" : 3,"
|
||||
"\"capacity\" : 4,"
|
||||
"\"hp\" : 5,"
|
||||
"\"ac\" : 6"
|
||||
"\"ac\" : 6,"
|
||||
"\"flags\" : [ \"playerrace\", \"walk\", \"undead\" ]"
|
||||
"}}}";
|
||||
cJSON *json = cJSON_Parse(data);
|
||||
const struct race *rc;
|
||||
|
@ -66,6 +67,7 @@ static void test_races(CuTest * tc)
|
|||
CuAssertPtrNotNull(tc, races);
|
||||
rc = rc_find("orc");
|
||||
CuAssertPtrNotNull(tc, rc);
|
||||
CuAssertIntEquals(tc, RCF_PLAYERRACE|RCF_WALK|RCF_UNDEAD, rc->flags);
|
||||
CuAssertStrEquals(tc, "1d4", rc->def_damage);
|
||||
CuAssertDblEquals(tc, 1.0, rc->magres, 0.0);
|
||||
CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0);
|
||||
|
|
Loading…
Reference in New Issue