Example #1
0
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);
        }
    }
}
Example #2
0
static void json_item(cJSON *json, item_type *itype) {
    cJSON *child;
    const char *flags[] = {
        "herb", "cursed", "nodrop", "big", "animal", "vehicle", 0
    };
    if (json->type != cJSON_Object) {
        log_error("ship %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_Number:
            if (strcmp(child->string, "weight") == 0) {
                itype->weight = child->valueint;
                break;
            }
            if (strcmp(child->string, "capacity") == 0) {
                itype->capacity = child->valueint;
                break;
            }
            log_error("item %s contains unknown attribute %s", json->string, child->string);
            break;
        case cJSON_Array:
            if (strcmp(child->string, "flags") == 0) {
                itype->flags = json_flags(child, flags);
                break;
            }
            log_error("item %s contains unknown attribute %s", json->string, child->string);
        case cJSON_Object:
        default:
            log_error("item %s contains unknown attribute %s", json->string, child->string);
        }
    }
}
Example #3
0
static void json_building(cJSON *json, building_type *bt) {
    cJSON *child;
    const char *flags[] = {
        "nodestroy", "nobuild", "unique", "decay", "dynamic", "magic", "oneperturn", "namechange", "fort", 0
    };
    if (json->type != cJSON_Object) {
        log_error("building %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_Array:
            if (strcmp(child->string, "construction") == 0) {
                json_construction(child, &bt->construction);
            }
            else if (strcmp(child->string, "maintenance") == 0) {
                json_maintenance(child, &bt->maintenance);
            }
            else if (strcmp(child->string, "flags") == 0) {
                json_flags(child, flags);
            }
            break;
        case cJSON_Object:
            if (strcmp(child->string, "construction") == 0) {
                json_construction(child, &bt->construction);
            }
            else if (strcmp(child->string, "maintenance") == 0) {
                json_maintenance(child, &bt->maintenance);
            }
            break;
        case cJSON_String:
            if (strcmp(child->string, "name") == 0) {
                bt->name = (const char *(*)(const struct building_type *,
                    const struct building *, int))get_function(child->valuestring);
                break;
            }
            log_error("building %s contains unknown attribute %s", json->string, child->string);
            break;
        default:
            log_error("building %s contains unknown attribute %s", json->string, child->string);
        }
    }
}
Example #4
0
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_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 {
                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);
        }
    }
}
Example #5
0
static void json_race(cJSON *json, race *rc) {
    cJSON *child;
    const char *flags[] = {
        "npc", "killpeasants", "scarepeasants",
        "nosteal", "moverandom", "cannotmove",
        "learn", "fly", "swim", "walk", "nolearn",
        "noteach", "horse", "desert",
        "illusionary", "absorbpeasants", "noheal",
        "noweapons", "shapeshift", "", "undead", "dragon",
        "coastal", "", "cansail", 0
    };
    const char *ecflags[] = {
        "", "keepitem", "giveperson",
        "giveunit", "getitem", 0
    };
    if (json->type != cJSON_Object) {
        log_error("race %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_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_Array:
            if (strcmp(child->string, "flags") == 0) {
                rc->flags = json_flags(child, flags);
                rc->ec_flags = json_flags(child, ecflags);
            }
            break;
        }
    }
}
Example #6
0
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);
        }
    }
}