From c5b561a819b971151d824e0aa9622f6fc074ff28 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 9 Apr 2014 05:05:47 +0200 Subject: [PATCH] Server: Make map geometry definable in config file. --- README | 45 +++++++++++++++++------ TODO | 2 - confserver/world | 6 +++ src/server/configfile.c | 81 +++++++++++++++++++++++++++++++++++------ src/server/init.c | 14 +------ src/server/init.h | 3 -- src/server/main.c | 9 ++--- 7 files changed, 114 insertions(+), 46 deletions(-) diff --git a/README b/README index bd4c0eb..a944cea 100644 --- a/README +++ b/README @@ -79,9 +79,30 @@ option (like "-s100") to start the recording at the respective turn number. Hacking / server internals and configuration -------------------------------------------- -The ./confserver/world file defines the map object types and the actions -available to them. Each object type and action is defined by a multi-line block -wherein each line sets one attribute of the object type or action. +The ./confserver/world file defines the map object types, actions available to +them and the map itself. Each definition consists of a multi-line block wherein +each line sets one attribute of the object type, action or the map. + +Here's a typical map definition block: + +MAP_TYPE 0 +HEIGHT 64 +WIDTH 64 +DIST_ORTHOGONAL 5 +DIST_DIAGONAL 7 + +A line of "MAP_TYPE" followed by a non-empty token starts the map definition +block. In the future, the second token may differentiate different map types, +but as of right now, only one is available and the value is not interpreted. +The numbers after "HEIGHT" and "WIDTH" give the map's vertical and horizontal +extensions in cells. They must be >= 1 and <= 256. The numbers after +"DIST_ORTHOGONAL" and "DIST_DIAGONAL" define the diagonal movement penalty as +the second value divided by the first. The above values define a penalty ratio +of 7/5 or 1.4, i.e. it takes 40% longer in turns to move diagonally than +orthogonally. Set both to the same value to eliminate the diagonal movement +penalty. A negative penalty to realize weird geometry is possible by setting +"DIST_DIAGONAL" to a lower value than "DIST_ORTHOGONAL". Both values, however, +must be >= 1 and <= 255. Here's a typical action definition block: @@ -118,14 +139,16 @@ yet: or if they are inanimate, but are otherwise crushed). Note that the "CORPSE_ID" must match the id of an object type defined in the file (before or after, it may even be the same). -All this definition block members must be present within a block, but only -"ACTION" / "OBJECT" must be positioned at the respective blocks' first line, -the others may appear in whatever order and even multiple times. If a block is -finished, however, it cannot be re-defined by starting a new block wit the same -object type or action id. Tokens in this config file are separated by -whitespace. Single quotes can be put around string values that are to include -whitespace by themslves. Note that all numbers must be decimal representations -of unsigned 8 bit integers, i.e. >= 0 and < 256 and sans preceding "+". +All these definition block members must be present within their blocks, but only +"ACTION" / "OBJECT" / "MAP_TYPE" must be positioned at their respective blocks' +first line; the others may appear in whatever order and even multiple times. If +an object or action definition block is finished, however, it cannot be +re-defined by starting a new block with the same object type or action id. + +Tokens in this config file are separated by whitespace. Single quotes can be +put around string values that are to include whitespace by themslves. Note that +all numbers must be decimal representations of unsigned 8 bit integers, i.e. +>= 0 and < 256 and sans preceding "+". All source files are thoroughly documented to explain more details of plomrogue's internals. The ./roguelike-server executable can be run with a -v diff --git a/TODO b/TODO index 17d1364..e80f6d7 100644 --- a/TODO +++ b/TODO @@ -15,8 +15,6 @@ SERVER: - is it actually useful to define map object action ids in the config file? -- make shape of map (and diagonal movement penalty) configurable in config file - - for game continuation, replace re-playing of whole record files with loading game state snapshots / save files diff --git a/confserver/world b/confserver/world index 01d475a..84e747d 100644 --- a/confserver/world +++ b/confserver/world @@ -1,3 +1,9 @@ +MAP_TYPE 0 +HEIGHT 64 +WIDTH 64 +DIST_ORTHOGONAL 5 +DIST_DIAGONAL 7 + ACTION 1 NAME wait EFFORT 1 diff --git a/src/server/configfile.c b/src/server/configfile.c index a48c494..1eb5165 100644 --- a/src/server/configfile.c +++ b/src/server/configfile.c @@ -26,6 +26,10 @@ */ enum flag { + HEIGHT_SET = 0x02, + WIDTH_SET = 0x04, + ORTH_SET = 0x08, + DIAG_SET = 0x10, NAME_SET = 0x02, EFFORT_SET = 0x04, CORPSE_ID_SET = 0x04, @@ -34,7 +38,8 @@ enum flag CONSUMABLE_SET = 0x20, READY_ACT = NAME_SET | EFFORT_SET, READY_OBJ = NAME_SET | CORPSE_ID_SET | SYMBOL_SET | LIFEPOINTS_SET - | CONSUMABLE_SET + | CONSUMABLE_SET, + READY_MAP = HEIGHT_SET | WIDTH_SET | ORTH_SET | DIAG_SET }; @@ -72,6 +77,11 @@ static uint8_t start_entry(char * token0, char * token1, char * comparand, 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); @@ -81,16 +91,19 @@ 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. +/* 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); + +/* set_members() helper specifically for editing world.map members. */ +static uint8_t set_map_members(char * token0,char * token1,uint8_t * map_flags); /* If "name" fits "moa"->name, set "moa"->func to "func". (Derives MapObjAct * .func from .name for set_members(). @@ -104,16 +117,20 @@ static void tokens_into_entries(char * token0, char * token1) { char * str_act = "ACTION"; char * str_obj = "OBJECT"; + char * str_map = "MAP_TYPE"; 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)) { 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); } @@ -126,8 +143,10 @@ static void tokens_into_entries(char * token0, char * token1) || 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_members(token0, token1, &object_flags, &action_flags, - (struct MapObjDef *)mod,(struct MapObjAct *) moa))) + &map_flags, (struct MapObjDef *)mod, + (struct MapObjAct *) moa))) { parse_unknown_arg(); } @@ -157,6 +176,18 @@ static uint8_t start_entry(char * token0, char * token1, char * comparand, +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; +} + + + static void write_if_entry(struct EntryHead ** entry, struct EntryHead *** entry_p_p_p) { @@ -200,9 +231,33 @@ 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; + } + else if ( parse_val(token0, token1, "DIST_ORTHOGONAL", map_flags, + ORTH_SET, '8', (char *) &world.map.dist_orthogonal) + || parse_val(token0, token1, "DIST_DIAGONAL", map_flags, + DIAG_SET, '8', (char *) &world.map.dist_diagonal)) + { + err_line(0 == atoi(token1), "Value must not be zero."); + return 1; + } + return 0; +} + + + 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 && parse_val(token0, token1, "NAME", action_flags, @@ -218,7 +273,8 @@ static uint8_t set_members(char * token0, char * token1, uint8_t * object_flags, *action_flags = *action_flags | NAME_SET; return 1; } - else if ( parse_val(token0, token1, "NAME", object_flags, + 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) @@ -254,6 +310,7 @@ 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."); set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS | CLEANUP_MAP_OBJECT_DEFS); test_corpse_ids(); } diff --git a/src/server/init.c b/src/server/init.c index fec2fce..9f2a4e9 100644 --- a/src/server/init.c +++ b/src/server/init.c @@ -1,6 +1,6 @@ /* src/server/init.c */ -#define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup() */ +#define _POSIX_C_SOURCE 2 /* getopt(), optarg */ #include "init.h" #include /* global errno, EEXIST */ #include /* NULL */ @@ -18,7 +18,6 @@ #include "../common/rexit.h" /* exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag() */ -#include "configfile.h" /* read_config_file() */ #include "map.h" /* init_map() */ #include "map_objects.h" /* free_map_objects(), add_map_objects() */ #include "rrand.h" /* rrand() */ @@ -53,17 +52,6 @@ extern void obey_argv(int argc, char * argv[]) -extern void init_map_and_map_objects_configs() -{ - world.map.size.x = 64; - world.map.size.y = 64; - world.map.dist_orthogonal = 5; - world.map.dist_diagonal = 7; - read_config_file(); -} - - - extern void setup_server_io() { char * f_name = "setup_server_io()"; diff --git a/src/server/init.h b/src/server/init.h index a906273..0be0dfd 100644 --- a/src/server/init.h +++ b/src/server/init.h @@ -13,9 +13,6 @@ /* Parses command line arguments -v and -s into server configuration. */ extern void obey_argv(int argc, char * argv[]); -/* Set map geometry and, from config files, map object (action) definitions. */ -extern void init_map_and_map_objects_configs(); - /* Start server in file and out file, latter with server process test string. */ extern void setup_server_io(); diff --git a/src/server/main.c b/src/server/main.c index 642a6fe..366fb17 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -4,9 +4,8 @@ #include /* exit() */ #include "../common/rexit.h" /* exit_err, set_cleanup_func() */ #include "cleanup.h" /* set_cleanup_flag(), cleanup() */ -#include "init.h" /* run_game(), obey_argv(), obey_argv(), setup_server_io(), - * init_map_and_map_object_configs() - */ +#include "configfile.h" /* read_config_file() */ +#include "init.h" /* run_game(), obey_argv(), obey_argv(), setup_server_io() */ #include "world.h" /* struct World */ @@ -41,8 +40,8 @@ int main(int argc, char ** argv) world.path_record = "record"; world.tmp_suffix = "_tmp"; - /* Init map, map object configurations and server i/o files. */ - init_map_and_map_objects_configs(); + /* Init config file and server i/o files. */ + read_config_file(); setup_server_io(); /* Enter play or replay mode loops, then leave properly. */ -- 2.30.2