static void json_maintenance_i(cJSON *json, maintenance *mt) { cJSON *child; for (child = json->child; child; child = child->next) { switch (child->type) { case cJSON_Number: if (strcmp(child->string, "amount") == 0) { mt->number = child->valueint; } else { log_error("maintenance contains unknown attribute %s", child->string); } break; case cJSON_String: if (strcmp(child->string, "type") == 0) { mt->rtype = rt_get_or_create(child->valuestring); } else { log_error("maintenance contains unknown attribute %s", child->string); } break; case cJSON_Array: if (strcmp(child->string, "flags") == 0) { const char * flags[] = { "variable", "required", 0 }; mt->flags = json_flags(child, flags); } else { log_error("maintenance contains unknown array %s", child->string); } break; default: log_error("maintenance contains unknown attribute %s of type %d", child->string, child->type); } } }
static void setup_resources(void) { struct locale *lang; item_type *itype; test_setup(); itype = it_get_or_create(rt_get_or_create("stone")); itype->rtype->flags = RTF_LIMITED | RTF_POOLED; itype->construction = calloc(1, sizeof(construction)); itype->construction->skill = SK_QUARRYING; itype->construction->minskill = 1; rmt_create(itype->rtype); init_resources(); lang = get_or_create_locale("de"); /* CR tags are translated from this */ locale_setstring(lang, "money", "Silber"); locale_setstring(lang, "money_p", "Silber"); locale_setstring(lang, "horse", "Pferd"); locale_setstring(lang, "horse_p", "Pferde"); locale_setstring(lang, "peasant", "Bauer"); locale_setstring(lang, "peasant_p", "Bauern"); locale_setstring(lang, "stone", "Stein"); locale_setstring(lang, "stone_p", "Steine"); locale_setstring(lang, "tree", "Blume"); locale_setstring(lang, "tree_p", "Blumen"); locale_setstring(lang, "sapling", "Schoessling"); locale_setstring(lang, "sapling_p", "Schoesslinge"); locale_setstring(lang, "mallornsapling", "Mallornschoessling"); locale_setstring(lang, "mallornsapling_p", "Mallornschoesslinge"); locale_setstring(lang, "mallorntree", "Mallorn"); locale_setstring(lang, "mallorntree_p", "Mallorn"); }
static void test_all_spy_message(CuTest *tc) { spy_fixture fix; setup_spy(&fix); enable_skill(SK_MAGIC, true); set_level(fix.victim, SK_MINING, 2); set_level(fix.victim, SK_MAGIC, 2); create_mage(fix.victim, M_DRAIG); set_factionstealth(fix.victim, fix.spy->faction); item_type *itype; itype = it_get_or_create(rt_get_or_create("sword")); new_weapontype(itype, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 2); i_change(&fix.victim->items, itype, 1); spy_message(99, fix.spy, fix.victim); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport")); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_mage")); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_skills")); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_faction")); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_items")); test_cleanup(); }
static void test_tax_cmd(CuTest *tc) { order *ord; faction *f; region *r; unit *u; item_type *sword, *silver; request *taxorders = 0; test_cleanup(); config_set("taxing.perlevel", "20"); test_create_world(); f = test_create_faction(NULL); r = findregion(0, 0); assert(r && f); u = test_create_unit(f, r); ord = create_order(K_TAX, f->locale, ""); assert(ord); tax_cmd(u, ord, &taxorders); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error48")); test_clear_messages(u->faction); silver = get_resourcetype(R_SILVER)->itype; sword = it_get_or_create(rt_get_or_create("sword")); new_weapontype(sword, 0, 0.0, NULL, 0, 0, 0, SK_MELEE, 1); i_change(&u->items, sword, 1); set_level(u, SK_MELEE, 1); tax_cmd(u, ord, &taxorders); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_no_tax_skill")); test_clear_messages(u->faction); set_level(u, SK_TAXING, 1); tax_cmd(u, ord, &taxorders); CuAssertPtrEquals(tc, 0, test_find_messagetype(u->faction->msgs, "error_no_tax_skill")); CuAssertPtrNotNull(tc, taxorders); rsetmoney(r, 11); expandtax(r, taxorders); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "income")); /* taxing is in multiples of 10 */ CuAssertIntEquals(tc, 10, i_get(u->items, silver)); test_clear_messages(u->faction); i_change(&u->items, silver, -i_get(u->items, silver)); rsetmoney(r, 1000); taxorders = 0; tax_cmd(u, ord, &taxorders); expandtax(r, taxorders); CuAssertIntEquals(tc, 20, i_get(u->items, silver)); test_clear_messages(u->faction); free_order(ord); test_cleanup(); }
static void json_requirements(cJSON *json, requirement **matp) { cJSON *child; int i; requirement *mat = calloc(sizeof(requirement), 1 + cJSON_GetArraySize(json)); for (i = 0, child = json->child; child; child = child->next, ++i) { mat[i].number = child->valueint; mat[i].rtype = rt_get_or_create(child->string); } *matp = mat; }
static void test_resourcename_with_appearance(CuTest *tc) { item_type *itype; test_cleanup(); itype = it_get_or_create(rt_get_or_create("foo")); assert(itype && itype->rtype); it_set_appearance(itype, "bar"); CuAssertStrEquals(tc, "foo", resourcename(itype->rtype, 0)); CuAssertStrEquals(tc, "foo_p", resourcename(itype->rtype, NMF_PLURAL)); CuAssertStrEquals(tc, "bar", resourcename(itype->rtype, NMF_APPEARANCE)); CuAssertStrEquals(tc, "bar_p", resourcename(itype->rtype, NMF_APPEARANCE | NMF_PLURAL)); test_cleanup(); }
static void setup_give(struct give *env) { struct terrain_type *ter = test_create_terrain("plain", LAND_REGION); race *rc; assert(env->f1); rc = test_create_race(env->f1->race ? env->f1->race->_name : "humon"); rc->ec_flags |= ECF_GIVEPERSON; env->r = test_create_region(0, 0, ter); env->src = test_create_unit(env->f1, env->r); env->itype = it_get_or_create(rt_get_or_create("money")); env->itype->flags |= ITF_HERB; if (env->f2) { ally_set(&env->f2->allies, env->f1, HELP_GIVE); env->dst = test_create_unit(env->f2, env->r); } else { env->dst = NULL; } if (env->lang) { locale_setstring(env->lang, env->itype->rtype->_name, "SILBER"); init_locale(env->lang); env->f1->locale = env->lang; } config_set("rules.give.max_men", "-1"); /* success messages: */ mt_create_va(mt_new("receive_person", NULL), "unit:unit", "target:unit", "amount:int", MT_NEW_END); mt_create_va(mt_new("give_person", NULL), "unit:unit", "target:unit", "amount:int", MT_NEW_END); mt_create_va(mt_new("give_person_peasants", NULL), "unit:unit", "amount:int", MT_NEW_END); mt_create_va(mt_new("give_person_ocean", NULL), "unit:unit", "amount:int", MT_NEW_END); mt_create_va(mt_new("receive", NULL), "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END); mt_create_va(mt_new("give", NULL), "unit:unit", "target:unit", "resource:resource", "amount:int", MT_NEW_END); mt_create_va(mt_new("give_peasants", NULL), "unit:unit", "resource:resource", "amount:int", MT_NEW_END); /* error messages: */ mt_create_error(120); mt_create_error(128); mt_create_error(129); mt_create_error(96); mt_create_error(10); mt_create_feedback("feedback_give_forbidden"); mt_create_feedback("peasants_give_invalid"); mt_create_va(mt_new("too_many_units_in_faction", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END); mt_create_va(mt_new("too_many_units_in_alliance", NULL), "unit:unit", "region:region", "command:order", "allowed:int", MT_NEW_END); mt_create_va(mt_new("feedback_no_contact", NULL), "unit:unit", "region:region", "command:order", "target:unit", MT_NEW_END); mt_create_va(mt_new("giverestriction", NULL), "unit:unit", "region:region", "command:order", "turns:int", MT_NEW_END); mt_create_va(mt_new("error_unit_size", NULL), "unit:unit", "region:region", "command:order", "maxsize:int", MT_NEW_END); mt_create_va(mt_new("nogive_reserved", NULL), "unit:unit", "region:region", "command:order", "resource:resource", "reservation:int", MT_NEW_END); mt_create_va(mt_new("race_notake", NULL), "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END); mt_create_va(mt_new("race_noregroup", NULL), "unit:unit", "region:region", "command:order", "race:race", MT_NEW_END); }
static void test_resources(CuTest *tc) { resource_type *rtype; test_cleanup(); CuAssertPtrNotNull(tc, rt_find("hp")); CuAssertPtrEquals(tc, rt_find("hp"), (void *)get_resourcetype(R_LIFE)); CuAssertPtrNotNull(tc, rt_find("peasant")); CuAssertPtrEquals(tc, rt_find("peasant"), (void *)get_resourcetype(R_PEASANT)); CuAssertPtrNotNull(tc, rt_find("aura")); 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(); CuAssertPtrEquals(tc, 0, rt_find("stone")); rtype = rt_get_or_create("stone"); CuAssertPtrEquals(tc, (void *)rtype, (void *)get_resourcetype(R_STONE)); }
static void json_items(cJSON *json) { cJSON *child; if (json->type != cJSON_Object) { log_error("items is not a json object: %d", json->type); return; } for (child = json->child; child; child = child->next) { resource_type *rtype = rt_get_or_create(child->string); item_type *itype = rtype->itype; if (!itype) { rtype->itype = itype = it_get_or_create(rtype); } json_item(child, itype); } }
static void json_terrain(cJSON *json, terrain_type *ter) { cJSON *child; if (json->type != cJSON_Object) { log_error("terrain %s is not a json object: %d", json->string, json->type); return; } for (child = json->child; child; child = child->next) { switch (child->type) { case cJSON_Object: if (strcmp(child->string, "production") == 0) { cJSON *entry; int size = cJSON_GetArraySize(child); if (size > 0) { int n; ter->production = (terrain_production *)calloc(size + 1, sizeof(terrain_production)); ter->production[size].type = 0; for (n = 0, entry = child->child; entry; entry = entry->next, ++n) { ter->production[n].type = rt_get_or_create(entry->string); if (entry->type != cJSON_Object) { log_error("terrain %s contains invalid production %s", json->string, entry->string); } else { json_terrain_production(entry, ter->production + n); } } } } else { log_error("terrain %s contains unknown attribute %s", json->string, child->string); } break; case cJSON_Array: if (strcmp(child->string, "flags") == 0) { const char * flags[] = { "land", "sea", "forest", "arctic", "cavalry", "forbidden", "sail", "fly", "swim", "walk", 0 }; ter->flags = json_flags(child, flags); } else if (strcmp(child->string, "herbs") == 0) { cJSON *entry; int size = cJSON_GetArraySize(child); if (size > 0) { int n; free(ter->herbs); ter->herbs = malloc(sizeof(const item_type *) * (size + 1)); ter->herbs[size] = 0; for (n = 0, entry = child->child; entry; entry = entry->next) { ter->herbs[n++] = it_get_or_create(rt_get_or_create(entry->valuestring)); } } } else { log_error("terrain %s contains unknown attribute %s", json->string, child->string); } break; case cJSON_Number: if (strcmp(child->string, "size") == 0) { ter->size = child->valueint; } else if (strcmp(child->string, "road") == 0) { ter->max_road = (short)child->valueint; } else if (strcmp(child->string, "seed") == 0) { ter->distribution = (short)child->valueint; } else { log_error("terrain %s contains unknown attribute %s", json->string, child->string); } break; default: log_error("terrain %s contains unknown attribute %s", json->string, child->string); } } }