From: Christian Heller Date: Fri, 19 Jul 2013 23:21:56 +0000 (+0200) Subject: Read monster / item definitions from file "defs". File-reading repeats some code... X-Git-Tag: tce~1149 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_id%7D%7D?a=commitdiff_plain;h=0f12557b88c73f8629ffafc38dc59ab5bd15e687;p=plomrogue Read monster / item definitions from file "defs". File-reading repeats some code from keybindings.c/init_keybindings() so refactored that into misc.c/textfile_sizes.c. --- diff --git a/defs b/defs new file mode 100644 index 0000000..4f2e61f --- /dev/null +++ b/defs @@ -0,0 +1,5 @@ +0 m a 1 ANT +1 m z 3 ZOMBIE +2 m S 9 SHOGGOTH +3 i # THING +4 i % SKELETON diff --git a/src/keybindings.c b/src/keybindings.c index dae5497..2f27da9 100644 --- a/src/keybindings.c +++ b/src/keybindings.c @@ -10,20 +10,9 @@ void init_keybindings(struct World * world) { // Initialize keybindings from file "keybindings". FILE * file = fopen("keybindings", "r"); - uint16_t lines = 0; - int c = 0; - uint16_t linemax = 0; - uint16_t c_count = 0; - while (EOF != c) { - c_count++; - c = getc(file); - if ('\n' == c) { - if (c_count > linemax) - linemax = c_count + 1; - c_count = 0; - lines++; } } + uint16_t lines, linemax; + textfile_sizes (file, &linemax, &lines); struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); - fseek(file, 0, SEEK_SET); char * command = malloc(linemax); uint16_t commcount = 0; char * cmdptr; diff --git a/src/main.c b/src/main.c index 3d6c0d3..ce01ee5 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "windows.h" #include "draw_wins.h" #include "keybindings.h" @@ -30,7 +32,7 @@ int main (int argc, char *argv[]) { default: exit(EXIT_FAILURE); } } - // Initialize log, player, monsters and items. + // Initialize log, player, monster/item definitions and monsters/items. world.log = calloc(1, sizeof(char)); update_log (&world, " "); struct Player player; @@ -38,37 +40,46 @@ int main (int argc, char *argv[]) { world.player = &player; world.monster = 0; world.item = 0; - struct MonsterDef monster_def_A; - monster_def_A.map_obj_def.id = 1; - monster_def_A.map_obj_def.mapchar = 'a'; - monster_def_A.map_obj_def.desc = "ANT"; - monster_def_A.hitpoints_start = 1; - struct MonsterDef monster_def_B; - monster_def_B.map_obj_def.id = 2; - monster_def_B.map_obj_def.mapchar = 'z'; - monster_def_B.map_obj_def.desc = "ZOMBIE"; - monster_def_B.hitpoints_start = 3; - struct MonsterDef monster_def_C; - monster_def_C.map_obj_def.id = 3; - monster_def_C.map_obj_def.mapchar = 'S'; - monster_def_C.map_obj_def.desc = "SHOGGOTH"; - monster_def_C.hitpoints_start = 9; - world.monster_def = &monster_def_A; - monster_def_A.map_obj_def.next = (struct MapObjDef *) &monster_def_B; - monster_def_B.map_obj_def.next = (struct MapObjDef *) &monster_def_C; - monster_def_C.map_obj_def.next = NULL; - struct ItemDef item_def_A; - item_def_A.map_obj_def.id = 4; - item_def_A.map_obj_def.mapchar = '#'; - struct ItemDef item_def_B; - item_def_B.map_obj_def.id = 5; - item_def_B.map_obj_def.mapchar = '%'; - world.item_def = &item_def_A; - item_def_A.map_obj_def.next = (struct MapObjDef *) &item_def_B; - item_def_B.map_obj_def.next = NULL; + world.item_def = 0; + world.monster_def = 0; + FILE * file = fopen("defs", "r"); + uint16_t linemax; + textfile_sizes (file, &linemax, NULL); + char m_or_i; + struct MapObjDef mod; + struct ItemDef id; + struct MonsterDef md; + struct ItemDef * * p_p_id = &world.item_def; + struct MonsterDef * * p_p_md = &world.monster_def; + char * defline = malloc(linemax); + char * line_p; + while (fgets (defline, linemax, file)) { + mod.next = 0; + mod.id = atoi(defline); + line_p = strchr(defline, ' ') + 1; + m_or_i = * line_p; + mod.mapchar = * (line_p + 2); + if ('i' == m_or_i) + line_p = line_p + 5; + else { + md.hitpoints_start = atoi (line_p + 4); + line_p = strchr (line_p + 4, ' ') + 1; } + mod.desc = calloc (strlen (line_p), sizeof(char)); + memcpy (mod.desc, line_p, strlen(line_p) - 1); + if ('i' == m_or_i) { + id.map_obj_def = mod; + * p_p_id = malloc (sizeof (struct ItemDef)); + * * p_p_id = id; + p_p_id = (struct ItemDef * *) * p_p_id; } + else { + md.map_obj_def = mod; + * p_p_md = malloc (sizeof (struct MonsterDef)); + * * p_p_md = md; + p_p_md = (struct MonsterDef * *) * p_p_md; } } + free(defline); + fclose(file); // For interactive mode, try to load world state from savefile. - FILE * file; if (1 == world.interactive && 0 == access("savefile", F_OK)) { file = fopen("savefile", "r"); world.seed = read_uint32_bigendian(file); @@ -100,15 +111,15 @@ int main (int argc, char *argv[]) { world.map = ↦ if (1 == world.turn) { player.pos = find_passable_pos(&map); - void * foo = build_map_objects (&world, &world.monster, 1, 1 + rrand(0,0) % 27, sizeof(struct Monster), + void * foo = build_map_objects (&world, &world.monster, 0, 1 + rrand(0,0) % 27, sizeof(struct Monster), build_map_objects_monsterdata); - foo = build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 9, sizeof(struct Monster), + foo = build_map_objects (&world, foo, 1, 1 + rrand(0,0) % 9, sizeof(struct Monster), build_map_objects_monsterdata); - build_map_objects (&world, foo, 3, 1 + rrand(0,0) % 3, sizeof(struct Monster), + build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 3, sizeof(struct Monster), build_map_objects_monsterdata); - foo = build_map_objects (&world, &world.item, 4, 1 + rrand(0,0) % 3, sizeof(struct Item), + foo = build_map_objects (&world, &world.item, 3, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); - build_map_objects (&world, foo, 5, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); } + build_map_objects (&world, foo, 4, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); } // Initialize window system and windows. WINDOW * screen = initscr(); diff --git a/src/misc.c b/src/misc.c index 683346f..8d38d59 100644 --- a/src/misc.c +++ b/src/misc.c @@ -10,6 +10,26 @@ #include "main.h" #include "yx_uint16.h" +extern void textfile_sizes (FILE * file, uint16_t * linemax_p, uint16_t * n_lines_p) { +// Learn largest line length (linemax_p) and (n_lines_p if not set to NULL) number of lines. + uint16_t n_lines = 0; + int c = 0; + uint16_t linemax = 0; + uint16_t c_count = 0; + while (EOF != c) { + c_count++; + c = getc(file); + if ('\n' == c) { + if (c_count > linemax) + linemax = c_count + 1; + c_count = 0; + if (n_lines_p) + n_lines++; } } + fseek(file, 0, SEEK_SET); + * linemax_p = linemax; + if (n_lines_p) + * n_lines_p = n_lines; } + extern uint16_t rrand(char use_seed, uint32_t new_seed) { // Pseudo-random number generator (LGC algorithm). Use instead of rand() to ensure portable predictability. static uint32_t seed; diff --git a/src/misc.h b/src/misc.h index 64e10db..68db822 100644 --- a/src/misc.h +++ b/src/misc.h @@ -3,12 +3,14 @@ #include #include "yx_uint16.h" +#include struct World; struct WinMeta; struct Win; struct Map; +extern void textfile_sizes (FILE *, uint16_t *, uint16_t *); extern uint16_t rrand(char, uint32_t); extern void update_log (struct World *, char *); extern uint16_t center_offset (uint16_t, uint16_t, uint16_t);