X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fconfigfile.c;h=14464396dc7e93ff6af93396d8a0faee0e2f78bf;hb=639a12151eeb7ce0d18de330b4bb8d9d8094c4d3;hp=026f658346b6308270c71682a623b3a1427620ce;hpb=a3c8dd9de99c7c77ad8218c3767abd4475c3dab6;p=plomrogue diff --git a/src/server/configfile.c b/src/server/configfile.c index 026f658..1446439 100644 --- a/src/server/configfile.c +++ b/src/server/configfile.c @@ -5,17 +5,18 @@ #include /* uint8_t */ #include /* atoi(), free() */ #include /* strcmp() */ -#include "../common/parse_file.h" /* EDIT_STARTED, set_val(), test_for_int(), - * err_line(), parse_file(),token_from_line(), - * finalize_by_readyflag() +#include "../common/parse_file.h" /* EDIT_STARTED, parsetest_int(),parse_file(), + * parsetest_too_many_values(),parse_id_uniq() + * parse_unknown_arg(), parse_init_entry(), + * parse_and_reduce_to_readyflag(),parse_val() */ #include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag(), CLEANUP_MAP_OBJ_DEFS, * CLEANUP_MAP_OBJ_ACTS */ -#include "map_object_actions.h" /* struct MapObjAct */ -#include "map_objects.h" /* struct MapObj, struct MapObjDef */ +#include "map_object_actions.h" /* MapObjAct */ +#include "map_objects.h" /* MapObj, MapObjDef, struct MapObjDef */ #include "world.h" /* world global */ @@ -25,15 +26,19 @@ */ enum flag { + HEIGHT_SET = 0x02, + WIDTH_SET = 0x04, NAME_SET = 0x02, EFFORT_SET = 0x04, CORPSE_ID_SET = 0x04, SYMBOL_SET = 0x08, LIFEPOINTS_SET = 0x10, CONSUMABLE_SET = 0x20, + START_N_SET = 0x40, READY_ACT = NAME_SET | EFFORT_SET, READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET - | CONSUMABLE_SET + | CONSUMABLE_SET | START_N_SET, + READY_MAP = HEIGHT_SET | WIDTH_SET }; @@ -66,11 +71,16 @@ static void tokens_into_entries(char * token0, char * token1); * reading. Check that "token1" id of new entry has not already been used in DB * starting at "entry_cmp". */ -static uint8_t new_entry(char * token0, char * token1, char * comparand, +static uint8_t start_entry(char * token0, char * token1, char * comparand, uint8_t * flags, size_t size, struct EntryHead ** entry, struct EntryHead * entry_cmp); +/* Start reading map definition if "token0" matches "comparand". Set "map_flags" + * to EDIT_STARTED to mark beginning of map definition reading. + */ +static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags); + /* Write DB entry pointed to by "entry" to its appropriate location. */ static void write_if_entry(struct EntryHead ** entry, struct EntryHead *** entry_p_p_p); @@ -80,16 +90,25 @@ static void write_if_entry(struct EntryHead ** entry, */ static void test_corpse_ids(); -/* Try to read tokens as members for the entry currently edited, which must be - * either "mod" or "moa". What member of which of the two is set depends on - * which of "object_flags" and "action_flags" has EDIT_STARTED set and on the - * key name in "token0". Return 1 if interpretation succeeds, else 0. +/* set_members() helper specifically for editing world.map members. */ +static uint8_t set_map_members(char * token0,char * token1,uint8_t * map_flags); + +/* If "token0" matches "comparand", set world.enemy_fov to "token1". */ +static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1); + +/* If "token0" matches "comparand", set world.player_type to int in "token1". */ +static uint8_t set_player_type(char * token0, char * comparand, char * token1); + +/* Try to read tokens as members for the definition currently edited, which may + * be "mod" or "moa" or that of world.map. What member of which of the three is + * set depends on which of the flags has EDIT_STARTED set and on the key name in + * "token0". Return 1 if interpretation succeeds, else 0. * * Note that MapObjAct entries' .name also determines their .func. */ static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, - uint8_t * action_flags, struct MapObjDef * mod, - struct MapObjAct * moa); + uint8_t * action_flags, uint8_t * map_flags, + struct MapObjDef * mod, struct MapObjAct * moa); /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct * .func from .name for set_members(). @@ -103,55 +122,82 @@ static void tokens_into_entries(char * token0, char * token1) { char * str_act = "ACTION"; char * str_obj = "OBJECT"; + char * str_map = "MAP_TYPE"; + char * str_player = "PLAYER_TYPE"; + char * str_enemyfov = "ENEMY_FOV"; static struct MapObjAct ** moa_p_p = &world.map_obj_acts; static struct MapObjDef ** mod_p_p = &world.map_obj_defs; static uint8_t action_flags = READY_ACT; static uint8_t object_flags = READY_OBJ; + static uint8_t map_flags = READY_MAP; static struct EntryHead * moa = NULL; static struct EntryHead * mod = NULL; - if (!token0 || !strcmp(token0,str_act) || !strcmp(token0,str_obj)) + if (!token0 || !strcmp(token0, str_act) || !strcmp(token0, str_obj) + || !strcmp(token0, str_map) || !strcmp(token0, str_player) + || !strcmp(token0, str_enemyfov)) { - finalize_by_readyflag(&action_flags, READY_ACT); - finalize_by_readyflag(&object_flags, READY_OBJ); + parse_and_reduce_to_readyflag(&action_flags, READY_ACT); + parse_and_reduce_to_readyflag(&object_flags, READY_OBJ); + parse_and_reduce_to_readyflag(&map_flags, READY_MAP); write_if_entry(&moa, (struct EntryHead ***) &moa_p_p); write_if_entry(&mod, (struct EntryHead ***) &mod_p_p); } - err_line(token0 && NULL != token_from_line(NULL), "Too many values."); - if ( token0 - && !( new_entry(token0, token1, str_act, &action_flags, - sizeof(struct MapObjAct), (struct EntryHead**) &moa, - (struct EntryHead *) world.map_obj_acts) - || new_entry(token0, token1, str_obj, &object_flags, - sizeof(struct MapObjDef), (struct EntryHead**) &mod, - (struct EntryHead *) world.map_obj_defs) - || set_members(token0, token1, &object_flags, &action_flags, - (struct MapObjDef *) mod, (struct MapObjAct *) moa))) + if (token0) { - err_line(1, "Unknown argument."); + parsetest_too_many_values(); + if (start_entry(token0, token1, str_act, &action_flags, + sizeof(struct MapObjAct), (struct EntryHead**) &moa, + (struct EntryHead *) world.map_obj_acts)) + { + err_line(0 == atoi(token1), "Value must not be 0."); + } + else if (!( start_entry(token0, token1, str_obj, &object_flags, + sizeof(struct MapObjDef), + (struct EntryHead**) &mod, + (struct EntryHead *) world.map_obj_defs) + || start_map(token0, str_map, &map_flags) + || set_player_type(token0, str_player, token1) + || set_enemy_fov(token0, str_enemyfov, token1) + || set_members(token0, token1, &object_flags, &action_flags, + &map_flags, (struct MapObjDef *)mod, + (struct MapObjAct *) moa))) + { + parse_unknown_arg(); + } } } -static uint8_t new_entry(char * token0, char * token1, char * comparand, - uint8_t * flags, size_t size, - struct EntryHead ** entry,struct EntryHead * entry_cmp) +static uint8_t start_entry(char * token0, char * token1, char * comparand, + uint8_t * flags, size_t size, + struct EntryHead ** entry, + struct EntryHead * entry_cmp) { - char * f_name = "new_entry()"; - if (!strcmp(token0, comparand)) + if (strcmp(token0, comparand)) { - char * err_uniq = "Declaration of ID already used."; - * flags = EDIT_STARTED; - * entry = try_malloc(size, f_name); - test_for_int(token1, '8'); - (*entry)-> id = atoi(token1); - for (; NULL != entry_cmp; entry_cmp = entry_cmp->next) - { - err_line((*entry)->id == entry_cmp->id, err_uniq); - } - return 1; + return 0; } - return 0; + *entry = (struct EntryHead *) parse_init_entry(flags, size); + parsetest_int(token1, '8'); + (*entry)-> id = atoi(token1); + for (; NULL != entry_cmp; entry_cmp = entry_cmp->next) + { + parse_id_uniq((*entry)->id == entry_cmp->id); + } + return 1; +} + + + +static uint8_t start_map(char * token0, char * comparand, uint8_t * map_flags) +{ + if (strcmp(token0, comparand)) + { + return 0; + } + *map_flags = EDIT_STARTED; + return 1; } @@ -199,13 +245,57 @@ static void test_corpse_ids() +static uint8_t set_map_members(char * token0, char * token1,uint8_t * map_flags) +{ + if ( parse_val(token0, token1, "HEIGHT", map_flags, + HEIGHT_SET, 'i', (char *) &world.map.size.y) + || parse_val(token0, token1, "WIDTH", map_flags, + WIDTH_SET, 'i', (char *) &world.map.size.x)) + { + int test = atoi(token1) > 256 || atoi(token1) < 1; + err_line(test, "Value must be >= 1 and <= 256."); + return 1; + } + return 0; +} + + + +static uint8_t set_enemy_fov(char * token0, char * comparand, char * token1) +{ + if (strcmp(token0, comparand)) + { + return 0; + } + parsetest_int(token1, '8'); + int test = atoi(token1) > 1; + err_line(test, "Value must be 0 or 1."); + world.enemy_fov = atoi(token1); + return 1; +} + + + +static uint8_t set_player_type(char * token0, char * comparand, char * token1) +{ + if (strcmp(token0, comparand)) + { + return 0; + } + parsetest_int(token1, '8'); + world.player_type = atoi(token1); + return 1; +} + + + static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, - uint8_t * action_flags, struct MapObjDef * mod, - struct MapObjAct * moa) + uint8_t * action_flags, uint8_t * map_flags, + struct MapObjDef * mod, struct MapObjAct * moa) { if ( *action_flags & EDIT_STARTED - && set_val(token0, token1, "NAME", action_flags, - NAME_SET, 's', (char *) &moa->name)) + && parse_val(token0, token1, "NAME", action_flags, + NAME_SET, 's', (char *) &moa->name)) { if (!( try_func_name(moa, "move", actor_move) || try_func_name(moa, "pick_up", actor_pick) @@ -217,18 +307,21 @@ static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, *action_flags = *action_flags | NAME_SET; return 1; } - else if ( set_val(token0, token1, "NAME", object_flags, - NAME_SET, 's', (char *) &mod->name) - || set_val(token0, token1, "SYMBOL", object_flags, - SYMBOL_SET, 'c', (char *) &mod->char_on_map) - || set_val(token0, token1, "EFFORT", action_flags, - EFFORT_SET, '8', (char *) &moa->effort) - || set_val(token0, token1, "LIFEPOINTS", object_flags, - LIFEPOINTS_SET, '8', (char *) &mod->lifepoints) - || set_val(token0, token1, "CONSUMABLE", object_flags, - CONSUMABLE_SET, '8', (char *) &mod->consumable) - || set_val(token0, token1, "CORPSE_ID", object_flags, - CORPSE_ID_SET, '8', (char *) &mod->corpse_id)) + else if ( set_map_members(token0, token1, map_flags) + || parse_val(token0, token1, "NAME", object_flags, + NAME_SET, 's', (char *) &mod->name) + || parse_val(token0, token1, "SYMBOL", object_flags, + SYMBOL_SET, 'c', (char *) &mod->char_on_map) + || parse_val(token0, token1, "EFFORT", action_flags, + EFFORT_SET, '8', (char *) &moa->effort) + || parse_val(token0, token1, "START_NUMBER", object_flags, + START_N_SET, '8', (char *) &mod->start_n) + || parse_val(token0, token1, "LIFEPOINTS", object_flags, + LIFEPOINTS_SET, '8', (char *) &mod->lifepoints) + || parse_val(token0, token1, "CONSUMABLE", object_flags, + CONSUMABLE_SET, '8', (char *) &mod->consumable) + || parse_val(token0, token1, "CORPSE_ID", object_flags, + CORPSE_ID_SET, '8', (char *) &mod->corpse_id)) { return 1; } @@ -253,6 +346,18 @@ static uint8_t try_func_name(struct MapObjAct * moa, char * name, extern void read_config_file() { parse_file(world.path_config, tokens_into_entries); + exit_err(!world.map.size.y, "Map not defined in config file."); + uint8_t player_type_is_valid = 0; + struct MapObjDef * mod; + for (mod = world.map_obj_defs; NULL != mod; mod = mod->next) + { + if (world.player_type == mod->id) + { + player_type_is_valid = 1; + break; + } + } + exit_err(!player_type_is_valid, "No valid map object type set for player."); set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS); test_corpse_ids(); }