forked from github/server
json config files, beginning with races
This commit is contained in:
parent
f43d8a3297
commit
dceff481d7
|
@ -15,6 +15,7 @@ pool_test.c
|
||||||
reports_test.c
|
reports_test.c
|
||||||
spellbook_test.c
|
spellbook_test.c
|
||||||
curse_test.c
|
curse_test.c
|
||||||
|
jsonconf_test.c
|
||||||
)
|
)
|
||||||
|
|
||||||
SET(_FILES
|
SET(_FILES
|
||||||
|
@ -56,6 +57,7 @@ teleport.c
|
||||||
terrain.c
|
terrain.c
|
||||||
unit.c
|
unit.c
|
||||||
xmlreader.c
|
xmlreader.c
|
||||||
|
jsonconf.c
|
||||||
)
|
)
|
||||||
|
|
||||||
FOREACH(_FILE ${_FILES})
|
FOREACH(_FILE ${_FILES})
|
||||||
|
|
|
@ -154,6 +154,20 @@ void bt_register(building_type * type)
|
||||||
ql_push(&buildingtypes, (void *)type);
|
ql_push(&buildingtypes, (void *)type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
building_type *bt_get_or_create(const char *name)
|
||||||
|
{
|
||||||
|
if (name != NULL) {
|
||||||
|
building_type *btype = bt_find(name);
|
||||||
|
if (btype == NULL) {
|
||||||
|
btype = calloc(sizeof(building_type), 1);
|
||||||
|
btype->_name = _strdup(name);
|
||||||
|
bt_register(btype);
|
||||||
|
}
|
||||||
|
return btype;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int buildingcapacity(const building * b)
|
int buildingcapacity(const building * b)
|
||||||
{
|
{
|
||||||
if (b->type->capacity >= 0) {
|
if (b->type->capacity >= 0) {
|
||||||
|
|
|
@ -73,10 +73,11 @@ extern "C" {
|
||||||
|
|
||||||
extern struct quicklist *buildingtypes;
|
extern struct quicklist *buildingtypes;
|
||||||
|
|
||||||
extern building_type *bt_find(const char *name);
|
building_type *bt_find(const char *name);
|
||||||
extern void register_buildings(void);
|
building_type *bt_get_or_create(const char *name);
|
||||||
extern void bt_register(struct building_type *type);
|
void register_buildings(void);
|
||||||
extern int bt_effsize(const struct building_type *btype,
|
void bt_register(struct building_type *type);
|
||||||
|
int bt_effsize(const struct building_type *btype,
|
||||||
const struct building *b, int bsize);
|
const struct building *b, int bsize);
|
||||||
|
|
||||||
/* buildingt => building_type
|
/* buildingt => building_type
|
||||||
|
|
|
@ -23,6 +23,7 @@ void test_equipment(CuTest * tc)
|
||||||
sc_mage * mage;
|
sc_mage * mage;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
|
test_create_race("human");
|
||||||
skill_enabled[SK_MAGIC] = 1;
|
skill_enabled[SK_MAGIC] = 1;
|
||||||
it_horses = test_create_itemtype(names);
|
it_horses = test_create_itemtype(names);
|
||||||
CuAssertPtrNotNull(tc, it_horses);
|
CuAssertPtrNotNull(tc, it_horses);
|
||||||
|
|
|
@ -0,0 +1,147 @@
|
||||||
|
/* vi: set ts=2:
|
||||||
|
+-------------------+
|
||||||
|
| | Enno Rehling <enno@eressea.de>
|
||||||
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
|
| (c) 1998 - 2004 | Katja Zedel <katze@felidae.kn-bremen.de>
|
||||||
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
||||||
|
+-------------------+
|
||||||
|
|
||||||
|
This program may not be used, modified or distributed
|
||||||
|
without prior permission by the authors of Eressea.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <platform.h>
|
||||||
|
#include <kernel/config.h>
|
||||||
|
#include "jsonconf.h"
|
||||||
|
|
||||||
|
/* kernel includes */
|
||||||
|
#include "building.h"
|
||||||
|
#include "equipment.h"
|
||||||
|
#include "item.h"
|
||||||
|
#include "message.h"
|
||||||
|
#include "race.h"
|
||||||
|
#include "region.h"
|
||||||
|
#include "resources.h"
|
||||||
|
#include "ship.h"
|
||||||
|
#include "terrain.h"
|
||||||
|
#include "skill.h"
|
||||||
|
#include "spell.h"
|
||||||
|
#include "spellbook.h"
|
||||||
|
#include "calendar.h"
|
||||||
|
|
||||||
|
/* util includes */
|
||||||
|
#include <util/attrib.h>
|
||||||
|
#include <util/bsdstring.h>
|
||||||
|
#include <util/crmessage.h>
|
||||||
|
#include <util/functions.h>
|
||||||
|
#include <util/language.h>
|
||||||
|
#include <util/log.h>
|
||||||
|
#include <util/message.h>
|
||||||
|
#include <util/nrmessage.h>
|
||||||
|
#include <util/xml.h>
|
||||||
|
|
||||||
|
/* external libraries */
|
||||||
|
#include <cJSON.h>
|
||||||
|
|
||||||
|
/* libc includes */
|
||||||
|
#include <assert.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void json_race(cJSON *json, race *rc) {
|
||||||
|
cJSON *child;
|
||||||
|
if (json->type!=cJSON_Object) {
|
||||||
|
log_error("race %s is not a json object: %d\n", json->string, json->type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (child=json->child;child;child=child->next) {
|
||||||
|
switch(child->type) {
|
||||||
|
case cJSON_String:
|
||||||
|
if (strcmp(child->string, "damage")==0) {
|
||||||
|
rc->def_damage = _strdup(child->valuestring);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case cJSON_Number:
|
||||||
|
if (strcmp(child->string, "magres")==0) {
|
||||||
|
rc->magres = (float)child->valuedouble;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "maxaura")==0) {
|
||||||
|
rc->maxaura = (float)child->valuedouble;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "regaura")==0) {
|
||||||
|
rc->regaura = (float)child->valuedouble;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "speed")==0) {
|
||||||
|
rc->speed = (float)child->valuedouble;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "recruitcost")==0) {
|
||||||
|
rc->recruitcost = child->valueint;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "maintenance")==0) {
|
||||||
|
rc->maintenance = child->valueint;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "weight")==0) {
|
||||||
|
rc->weight = child->valueint;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "capacity")==0) {
|
||||||
|
rc->capacity = child->valueint;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "hp")==0) {
|
||||||
|
rc->hitpoints = child->valueint;
|
||||||
|
}
|
||||||
|
else if (strcmp(child->string, "ac")==0) {
|
||||||
|
rc->armor = child->valueint;
|
||||||
|
}
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void json_races(cJSON *json) {
|
||||||
|
cJSON *child;
|
||||||
|
if (json->type!=cJSON_Object) {
|
||||||
|
log_error("races is not a json object: %d\n", json->type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (child=json->child;child;child=child->next) {
|
||||||
|
race * rc = rc_find(child->string);
|
||||||
|
if (!rc) {
|
||||||
|
rc = rc_add(rc_new((const char *)child->string));
|
||||||
|
json_race(child, rc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void json_config(cJSON *json) {
|
||||||
|
cJSON *child;
|
||||||
|
if (json->type!=cJSON_Object) {
|
||||||
|
log_error("config is not a json object: %d\n", json->type);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
child = cJSON_GetObjectItem(json, "races");
|
||||||
|
if (child && child->type==cJSON_Object) {
|
||||||
|
json_races(child);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* vi: set ts=2:
|
||||||
|
+-------------------+
|
||||||
|
| | Enno Rehling <enno@eressea.de>
|
||||||
|
| Eressea PBEM host | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
|
| (c) 1998 - 2007 | Katja Zedel <katze@felidae.kn-bremen.de>
|
||||||
|
| | Henning Peters <faroul@beyond.kn-bremen.de>
|
||||||
|
+-------------------+
|
||||||
|
|
||||||
|
This program may not be used, modified or distributed
|
||||||
|
without prior permission by the authors of Eressea.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef H_JSONCONF_H
|
||||||
|
#define H_JSONCONF_H
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct cJSON;
|
||||||
|
void json_config(struct cJSON *str);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
|
@ -0,0 +1,78 @@
|
||||||
|
#include <platform.h>
|
||||||
|
#include "types.h"
|
||||||
|
#include "jsonconf.h"
|
||||||
|
#include "race.h"
|
||||||
|
#include <CuTest.h>
|
||||||
|
#include <cJSON.h>
|
||||||
|
#include <tests.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static void test_flag(CuTest *tc, const char *name, int flag) {
|
||||||
|
char data[1024];
|
||||||
|
const struct race *rc;
|
||||||
|
cJSON *json;
|
||||||
|
sprintf(data, "{\"races\" : { \"orc\": { \"%s\" : true }}}", name);
|
||||||
|
|
||||||
|
json = cJSON_Parse(data);
|
||||||
|
free_races();
|
||||||
|
json_config(json);
|
||||||
|
rc = rc_find("orc");
|
||||||
|
CuAssertPtrNotNull(tc, rc);
|
||||||
|
CuAssertIntEquals(tc, flag, rc->flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_flags(CuTest *tc) {
|
||||||
|
test_flag(tc, "playerrace", RCF_PLAYERRACE);
|
||||||
|
test_flag(tc, "scarepeasants", RCF_SCAREPEASANTS);
|
||||||
|
test_flag(tc, "cansteal", RCF_CANSTEAL);
|
||||||
|
test_flag(tc, "noheal", RCF_NOHEAL);
|
||||||
|
test_flag(tc, "undead", RCF_UNDEAD);
|
||||||
|
test_flag(tc, "dragon", RCF_DRAGON);
|
||||||
|
test_flag(tc, "fly", RCF_FLY);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_races(CuTest * tc)
|
||||||
|
{
|
||||||
|
const char * data = "{\"races\": { \"orc\" : { "
|
||||||
|
"\"damage\" : \"1d4\","
|
||||||
|
"\"magres\" : 1.0,"
|
||||||
|
"\"maxaura\" : 2.0,"
|
||||||
|
"\"regaura\" : 3.0,"
|
||||||
|
"\"speed\" : 4.0,"
|
||||||
|
"\"recruitcost\" : 1,"
|
||||||
|
"\"maintenance\" : 2,"
|
||||||
|
"\"weight\" : 3,"
|
||||||
|
"\"capacity\" : 4,"
|
||||||
|
"\"hp\" : 5,"
|
||||||
|
"\"ac\" : 6"
|
||||||
|
"}}}";
|
||||||
|
cJSON *json = cJSON_Parse(data);
|
||||||
|
const struct race *rc;
|
||||||
|
test_cleanup();
|
||||||
|
|
||||||
|
CuAssertPtrEquals(tc, 0, races);
|
||||||
|
json_config(json);
|
||||||
|
|
||||||
|
CuAssertPtrNotNull(tc, races);
|
||||||
|
rc = rc_find("orc");
|
||||||
|
CuAssertPtrNotNull(tc, rc);
|
||||||
|
CuAssertStrEquals(tc, "1d4", rc->def_damage);
|
||||||
|
CuAssertDblEquals(tc, 1.0, rc->magres, 0.0);
|
||||||
|
CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0);
|
||||||
|
CuAssertDblEquals(tc, 3.0, rc->regaura, 0.0);
|
||||||
|
CuAssertDblEquals(tc, 4.0, rc->speed, 0.0);
|
||||||
|
CuAssertIntEquals(tc, 1, rc->recruitcost);
|
||||||
|
CuAssertIntEquals(tc, 2, rc->maintenance);
|
||||||
|
CuAssertIntEquals(tc, 3, rc->weight);
|
||||||
|
CuAssertIntEquals(tc, 4, rc->capacity);
|
||||||
|
CuAssertIntEquals(tc, 5, rc->hitpoints);
|
||||||
|
CuAssertIntEquals(tc, 6, rc->armor);
|
||||||
|
}
|
||||||
|
|
||||||
|
CuSuite *get_jsonconf_suite(void)
|
||||||
|
{
|
||||||
|
CuSuite *suite = CuSuiteNew();
|
||||||
|
SUITE_ADD_TEST(suite, test_races);
|
||||||
|
SUITE_ADD_TEST(suite, test_flags);
|
||||||
|
return suite;
|
||||||
|
}
|
|
@ -25,7 +25,8 @@ void test_updatespells(CuTest * tc)
|
||||||
spellbook *book = 0;
|
spellbook *book = 0;
|
||||||
|
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
|
test_create_race("human");
|
||||||
|
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
sp = create_spell("testspell", 0);
|
sp = create_spell("testspell", 0);
|
||||||
CuAssertPtrNotNull(tc, sp);
|
CuAssertPtrNotNull(tc, sp);
|
||||||
|
|
|
@ -98,6 +98,14 @@ void racelist_insert(struct race_list **rl, const struct race *r)
|
||||||
*rl = rl2;
|
*rl = rl2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_races(void) {
|
||||||
|
while (races) {
|
||||||
|
race * rc = races->next;
|
||||||
|
free(races);
|
||||||
|
races =rc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
race *rc_new(const char *zName)
|
race *rc_new(const char *zName)
|
||||||
{
|
{
|
||||||
char zBuffer[80];
|
char zBuffer[80];
|
||||||
|
|
|
@ -115,6 +115,7 @@ extern "C" {
|
||||||
extern struct race *rc_new(const char *zName);
|
extern struct race *rc_new(const char *zName);
|
||||||
extern int rc_specialdamage(const race *, const race *,
|
extern int rc_specialdamage(const race *, const race *,
|
||||||
const struct weapon_type *);
|
const struct weapon_type *);
|
||||||
|
void free_races(void);
|
||||||
|
|
||||||
/* Flags */
|
/* Flags */
|
||||||
#define RCF_PLAYERRACE (1<<0) /* can be played by a player. */
|
#define RCF_PLAYERRACE (1<<0) /* can be played by a player. */
|
||||||
|
|
|
@ -51,20 +51,6 @@ without prior permission by the authors of Eressea.
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static building_type *bt_get_or_create(const char *name)
|
|
||||||
{
|
|
||||||
if (name != NULL) {
|
|
||||||
building_type *btype = bt_find(name);
|
|
||||||
if (btype == NULL) {
|
|
||||||
btype = calloc(sizeof(building_type), 1);
|
|
||||||
btype->_name = _strdup(name);
|
|
||||||
bt_register(btype);
|
|
||||||
}
|
|
||||||
return btype;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void xml_readtext(xmlNodePtr node, struct locale **lang, xmlChar ** text)
|
static void xml_readtext(xmlNodePtr node, struct locale **lang, xmlChar ** text)
|
||||||
{
|
{
|
||||||
xmlChar *propValue = xmlGetProp(node, BAD_CAST "locale");
|
xmlChar *propValue = xmlGetProp(node, BAD_CAST "locale");
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#include <util/log.h>
|
#include <util/log.h>
|
||||||
|
|
||||||
CuSuite *get_tests_suite(void);
|
CuSuite *get_tests_suite(void);
|
||||||
|
CuSuite *get_jsonconf_suite(void);
|
||||||
CuSuite *get_json_suite(void);
|
CuSuite *get_json_suite(void);
|
||||||
CuSuite *get_economy_suite(void);
|
CuSuite *get_economy_suite(void);
|
||||||
CuSuite *get_laws_suite(void);
|
CuSuite *get_laws_suite(void);
|
||||||
|
@ -36,6 +37,7 @@ int RunAllTests(void)
|
||||||
/* self-test */
|
/* self-test */
|
||||||
CuSuiteAddSuite(suite, get_tests_suite());
|
CuSuiteAddSuite(suite, get_tests_suite());
|
||||||
CuSuiteAddSuite(suite, get_json_suite());
|
CuSuiteAddSuite(suite, get_json_suite());
|
||||||
|
CuSuiteAddSuite(suite, get_jsonconf_suite());
|
||||||
/* util */
|
/* util */
|
||||||
CuSuiteAddSuite(suite, get_base36_suite());
|
CuSuiteAddSuite(suite, get_base36_suite());
|
||||||
CuSuiteAddSuite(suite, get_bsdstring_suite());
|
CuSuiteAddSuite(suite, get_bsdstring_suite());
|
||||||
|
|
|
@ -63,6 +63,7 @@ void test_cleanup(void)
|
||||||
default_locale = 0;
|
default_locale = 0;
|
||||||
free_locales();
|
free_locales();
|
||||||
free_spells();
|
free_spells();
|
||||||
|
free_races();
|
||||||
free_spellbooks();
|
free_spellbooks();
|
||||||
free_gamedata();
|
free_gamedata();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue