allow settings in json configuration file

move E2 settings from XML to JSON
This commit is contained in:
Enno Rehling 2015-09-10 23:16:17 +02:00
parent 2b46c4c4a9
commit 1969c165d3
4 changed files with 68 additions and 29 deletions

View File

@ -1,5 +1,34 @@
{ {
"include": [ "include": [
"keywords.json" "keywords.json"
] ],
"settings": {
"NewbieImmunity": 8,
"modules.wormholes": 1,
"entertain.base": 0,
"entertain.perlevel": 20,
"nmr.timeout": 5,
"nmr.removenewbie": 0,
"GiveRestriction": 3,
"hunger.long": 1,
"init_spells": 0,
"world.era": 2,
"seed.population.min": 8,
"seed.population.max": 8,
"rules.ship.damage_drift": 0.00,
"rules.reserve.twophase": 1,
"rules.give.max_men": "-1",
"rules.check_overload": 0,
"rules.limit.faction": 2500,
"rules.maxskills.magic": 5,
"rules.guard.base_stop_prob": 0.30,
"rules.guard.skill_stop_prob": 0.05,
"rules.guard.amulet_stop_prob": 0.10,
"rules.guard.guard_number_stop_prob": 0.001,
"rules.guard.castle_stop_prob": 0.05,
"rules.guard.region_type_stop_prob": 0.05,
"rules.economy.repopulate_maximum": 500,
"game.id": 2,
"game.name": "Eressea"
}
} }

View File

@ -90,34 +90,6 @@
<skill name="taxation" enable="true"/> <skill name="taxation" enable="true"/>
<skill name="stamina" enable="true"/> <skill name="stamina" enable="true"/>
<skill name="unarmed" enable="true"/> <skill name="unarmed" enable="true"/>
<param name="NewbieImmunity" value="8"/>
<param name="modules.wormholes" value="1"/>
<param name="entertain.base" value="0"/>
<param name="entertain.perlevel" value="20"/>
<param name="nmr.timeout" value="5"/>
<param name="nmr.removenewbie" value="0"/>
<param name="GiveRestriction" value="3"/>
<param name="hunger.long" value="1"/>
<param name="init_spells" value="0"/>
<param name="world.era" value="2"/>
<param name="seed.population.min" value="8"/>
<param name="seed.population.max" value="8"/>
<param name="rules.ship.damage_drift" value="0.00"/> <!-- percent damage from drifting-->
<param name="rules.reserve.twophase" value="1"/>
<param name="rules.give.max_men" value="-1"/>
<param name="rules.check_overload" value="0"/>
<param name="rules.limit.faction" value="2500"/>
<param name="rules.maxskills.magic" value="5"/>
<param name="rules.guard.base_stop_prob" value="0.30"/>
<param name="rules.guard.skill_stop_prob" value="0.05"/>
<param name="rules.guard.amulet_stop_prob" value="0.10"/>
<param name="rules.guard.guard_number_stop_prob" value="0.001"/>
<param name="rules.guard.castle_stop_prob" value="0.05"/>
<param name="rules.guard.region_type_stop_prob" value="0.05"/>
<param name="rules.economy.repopulate_maximum" value="500"/>
<param name="game.id" value="2"/>
<param name="game.name" value="Eressea"/>
</game> </game>
<strings> <strings>
<string name="mailto"> <string name="mailto">

View File

@ -638,6 +638,24 @@ static void json_keywords(cJSON *json) {
} }
} }
static void json_settings(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Object) {
log_error("settings is not a json object: %d", json->type);
return;
}
for (child = json->child; child; child = child->next) {
if (child->valuestring) {
set_param(&global.parameters, child->string, child->valuestring);
}
else {
char value[32];
_snprintf(value, sizeof(value), "%lf", child->valuedouble);
set_param(&global.parameters, child->string, value);
}
}
}
static void json_races(cJSON *json) { static void json_races(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type != cJSON_Object) { if (json->type != cJSON_Object) {
@ -714,6 +732,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "keywords") == 0) { else if (strcmp(child->string, "keywords") == 0) {
json_keywords(child); json_keywords(child);
} }
else if (strcmp(child->string, "settings") == 0) {
json_settings(child);
}
else if (strcmp(child->string, "skills") == 0) { else if (strcmp(child->string, "skills") == 0) {
json_skills(child); json_skills(child);
} }

View File

@ -57,6 +57,22 @@ static void test_flags(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_settings(CuTest * tc)
{
const char * data = "{\"settings\": { "
"\"string\" : \"1d4\","
"\"integer\" : 14,"
"\"float\" : 0.5 }}";
cJSON *json = cJSON_Parse(data);
test_cleanup();
json_config(json);
CuAssertStrEquals(tc, "1d4", get_param(global.parameters, "string"));
CuAssertIntEquals(tc, 14, get_param_int(global.parameters, "integer", 0));
CuAssertDblEquals(tc, 0.5f, get_param_flt(global.parameters, "float", 0), 0.01);
test_cleanup();
}
static void test_races(CuTest * tc) static void test_races(CuTest * tc)
{ {
const char * data = "{\"races\": { \"orc\" : { " const char * data = "{\"races\": { \"orc\" : { "
@ -510,6 +526,7 @@ CuSuite *get_jsonconf_suite(void)
SUITE_ADD_TEST(suite, test_strings); SUITE_ADD_TEST(suite, test_strings);
SUITE_ADD_TEST(suite, test_spells); SUITE_ADD_TEST(suite, test_spells);
SUITE_ADD_TEST(suite, test_flags); SUITE_ADD_TEST(suite, test_flags);
SUITE_ADD_TEST(suite, test_settings);
SUITE_ADD_TEST(suite, test_infinitive_from_config); SUITE_ADD_TEST(suite, test_infinitive_from_config);
return suite; return suite;
} }