From cd1ac671dae035da349e97983f9d65d5c0d7b2a9 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 20 Jul 2013 01:35:14 +0200 Subject: [PATCH] Moved initialization of map object definitions from defs file into map_objects library. --- src/main.c | 42 ++---------------------------------------- src/map_objects.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/map_objects.h | 1 + src/misc.h | 2 +- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/main.c b/src/main.c index ce01ee5..b65108b 100644 --- a/src/main.c +++ b/src/main.c @@ -3,8 +3,6 @@ #include #include #include -#include -#include #include "windows.h" #include "draw_wins.h" #include "keybindings.h" @@ -40,46 +38,10 @@ int main (int argc, char *argv[]) { world.player = &player; world.monster = 0; world.item = 0; - 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); + init_map_object_defs(&world, "defs"); // 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); diff --git a/src/map_objects.c b/src/map_objects.c index 731d830..6aeb13b 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -1,12 +1,54 @@ #include "map_objects.h" #include #include +#include #include "readwrite.h" #include "misc.h" #include "main.h" static struct MapObj * get_next_map_obj (void *, char *, size_t, struct MapObj *); +extern void init_map_object_defs (struct World * world, char * filename) { +// Initialize map object definitions from file at path "filename". + world->item_def = 0; + world->monster_def = 0; + FILE * file = fopen(filename, "r"); + uint16_t linemax; + textfile_sizes (file, &linemax, NULL); + 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; + char m_or_i; + 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); }; + extern void readwrite_map_objects_dummy (void * dummy, FILE * file) { // Dummy function for calls of (write|read)_map_objects on map objects without specific attributes. ; } diff --git a/src/map_objects.h b/src/map_objects.h index 9ef7f3b..abf77eb 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -35,6 +35,7 @@ struct MonsterDef { struct MapObjDef map_obj_def; unsigned char hitpoints_start; }; +extern void init_map_object_defs (struct World *, char *); extern void readwrite_map_objects_dummy (void *, FILE *); extern void write_map_objects_monsterdata (void *, FILE *); extern void write_map_objects (void * start, FILE *, void (*) (void *, FILE *) ); diff --git a/src/misc.h b/src/misc.h index 68db822..5d90d15 100644 --- a/src/misc.h +++ b/src/misc.h @@ -2,8 +2,8 @@ #define MISC_H #include -#include "yx_uint16.h" #include +#include "yx_uint16.h" struct World; struct WinMeta; -- 2.30.2