X-Git-Url: https://plomlompom.com/repos/index.html?a=blobdiff_plain;f=src%2Fmap_objects.c;h=93f46841447aec667f1ddd0322268a0a330c0d6d;hb=2c2521789dc5c8bb77eb36362d4244606f878420;hp=15072217a381bf29de23d121a895a6c93a6fa40f;hpb=b97faa0009abc699ab7bff7bcc3221b8b3104e12;p=plomrogue
diff --git a/src/map_objects.c b/src/map_objects.c
index 1507221..93f4684 100644
--- a/src/map_objects.c
+++ b/src/map_objects.c
@@ -1,349 +1,198 @@
/* map_objects.c */
#include "map_objects.h"
-#include /* for malloc(), calloc(), free(), atoi() */
+#include /* for free(), atoi() */
#include /* for uint8_t */
#include /* for FILE typedef */
#include /* for strchr(), strlen(), memcpy(), strtok() */
-#include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */
-#include "misc.h" /* for textfile_sizes(), find_passable_pos() */
+#include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
+ * [read/write]_uint[8/16/23][_bigendian]()
+ */
+#include "misc.h" /* for try_malloc(), try_calloc(), find_passable_pos() */
#include "main.h" /* for World struct */
#include "rexit.h" /* for err_exit() */
-
-
-
-/* Return pointer to newly allocated map object struct of size "size". If first
- * in map object chain ("first" pointing to !0), point "start" to it.
- *
- * Returns NULL instead of MapObj pointer if malloc() failed.
- */
-static struct MapObj * get_next_map_obj(void * start, char * first,
- size_t size, struct MapObj * map_obj);
-
-
-
-/* Map-object-type-specific helpers to (build|write|read)_map_objects(). */
-static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
- void * start);
-static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
- void * start);
-static uint8_t write_map_objects_monsterdata(void * start, FILE * file);
-static uint8_t read_map_objects_monsterdata( void * start, FILE * file);
-
-
-
-static struct MapObj * get_next_map_obj(void * start, char * first,
- size_t size, struct MapObj * map_obj)
-{
- if (* first)
- {
- struct MapObj * * z = start;
- map_obj = malloc(size);
- * z = map_obj;
- * first = 0;
- }
- else
- {
- map_obj->next = malloc(size);
- map_obj = map_obj->next;
- }
- return map_obj;
-}
-
-
-
-static void build_map_objects_itemdata(struct MapObjDef * map_obj_def,
- void * start)
-{
- struct Item * i = (struct Item *) start;
- i->map_obj.type = map_obj_def->id;
-}
-
-
-
-static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
- void * start)
-{
- struct Monster * m = (struct Monster *) start;
- m->map_obj.type = map_obj_def->id;
- struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
- m->hitpoints = md->hitpoints_start;
-}
-
-
-
-static uint8_t write_map_objects_monsterdata(void * start, FILE * file)
-{
- struct Monster * m = (struct Monster *) start;
- return write_uint8(m->hitpoints, file);
-}
-
-
-
-static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
-{
- struct Monster * m = (struct Monster *) start;
- return read_uint8(file, &m->hitpoints);
-}
+#include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
extern void init_map_object_defs(struct World * world, char * filename)
{
- char * err_o = "Trouble in init_map_object_defs() with fopen().";
- char * err_s = "Trouble in init_map_object_defs() with textfile_sizes().";
- char * err_m = "Trouble in init_map_object_defs() with malloc()/calloc().";
- char * err_c = "Trouble in init_map_object_defs() with fclose().";
-
- FILE * file = fopen(filename, "r");
- exit_err(NULL == file, world, err_o);
- uint16_t linemax;
- exit_err(textfile_sizes(file, &linemax, NULL), world, err_s);
-
- struct MapObjDef mod;
- struct ItemDef id;
- struct MonsterDef md;
- world->item_def = 0;
- world->monster_def = 0;
- struct ItemDef * * p_p_id = &world->item_def;
- struct MonsterDef * * p_p_md = &world->monster_def;
- char * defline = malloc(linemax);
- exit_err(NULL == defline, world, err_m);
- char * line_p;
+ char * f_name = "init_map_object_defs()";
+ FILE * file = try_fopen(filename, "r", world, f_name);
+ uint16_t linemax = get_linemax(file, world, f_name);
+ struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
char * delim = " ";
- while (fgets(defline, linemax, file))
+ char line[linemax + 1];
+ while (try_fgets(line, linemax + 1, file, world, f_name))
{
- mod.next = 0;
- mod.id = atoi(strtok(defline, delim));
- mod.m_or_i = * strtok(NULL, delim);
- mod.mapchar = * strtok(NULL, delim);
- if ('i' == mod.m_or_i)
- {
- line_p = strtok(NULL, delim);
- }
- else
- {
- md.corpse_id = atoi(strtok(NULL, delim));
- md.hitpoints_start = atoi(strtok(NULL, delim));
- line_p = strtok(NULL, delim);
- }
- mod.desc = calloc(strlen(line_p), sizeof(char));
- exit_err(NULL == mod.desc, world, err_m);
- memcpy(mod.desc, line_p, strlen(line_p) - 1);
- if ('i' == mod.m_or_i)
- {
- id.map_obj_def = mod;
- * p_p_id = malloc(sizeof(struct ItemDef));
- exit_err(NULL == p_p_id, world, err_m);
- * * 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));
- exit_err(NULL == p_p_md, world, err_m);
- * * p_p_md = md;
- p_p_md = (struct MonsterDef * *) * p_p_md;
- }
+ struct MapObjDef * mod;
+ mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
+ mod->next = NULL;
+ mod->id = atoi(strtok(line, delim));
+ mod->corpse_id = atoi(strtok(NULL, delim));
+ mod->char_on_map = * strtok(NULL, delim);
+ mod->lifepoints = atoi(strtok(NULL, delim));
+ char * name = strtok(NULL, "\n");
+ mod->name = try_malloc(strlen(name) + 1, world, f_name);
+ memcpy(mod->name, name, strlen(name) + 1);
+ * last_mod_ptr_ptr = mod;
+ last_mod_ptr_ptr = &mod->next;
}
-
- free(defline);
- exit_err(fclose(file), world, err_c);
+ try_fclose(file, world, f_name);
}
-extern void free_item_defs(struct ItemDef * id_start)
+extern void free_map_object_defs(struct MapObjDef * mod_start)
{
- if (0 != id_start->map_obj_def.next)
+ if (NULL == mod_start)
{
- free_item_defs((struct ItemDef *) id_start->map_obj_def.next);
+ return;
}
- free(id_start->map_obj_def.desc);
- free(id_start);
+ free_map_object_defs(mod_start->next);
+ free(mod_start->name);
+ free(mod_start);
}
-
-extern void free_monster_defs(struct MonsterDef * md_start)
+extern void write_map_objects(struct World * world, FILE * file)
{
- if (0 != md_start->map_obj_def.next)
+ char * f_name = "write_map_objects()";
+ struct MapObj * mo = world->map_objs;
+ uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
+ char line[size];
+ while (NULL != mo)
{
- free_monster_defs((struct MonsterDef *) md_start->map_obj_def.next);
+ sprintf(line, "%d %d %d %d %d\n",
+ mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
+ try_fwrite(line, strlen(line), 1, file, world, f_name);
+ mo = mo->next;
}
- free(md_start->map_obj_def.desc);
- free(md_start);
}
-extern uint8_t write_map_objects(struct World * world, void * start,
- FILE * file)
+extern void read_map_objects(struct World * world, FILE * file, char * line,
+ int linemax)
{
- struct MapObj * map_obj;
- struct MapObjDef * mod;
- for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
+ char * f_name = "read_map_objects()";
+ struct MapObj ** mo_ptr_ptr = &world->map_objs;
+ char * delim = " ";
+ struct MapObj * mo;
+ while (try_fgets(line, linemax + 1, file, world, f_name))
{
- if ( write_uint8(map_obj->type, file)
- || write_uint16_bigendian(map_obj->pos.y + 1, file)
- || write_uint16_bigendian(map_obj->pos.x + 1, file))
- {
- return 1;
- }
- mod = get_map_obj_def(world, map_obj->type);
- if ('m' == mod->m_or_i)
+ mo = malloc(sizeof(struct MapObj));
+ mo->next = NULL;
+ mo->id = atoi(strtok(line, delim));
+ if (mo->id > world->map_obj_count)
{
- if (write_map_objects_monsterdata(map_obj, file))
- {
- return 1;
- }
+ world->map_obj_count = mo->id;
}
+ mo->type = atoi(strtok(NULL, delim));
+ mo->lifepoints = atoi(strtok(NULL, delim));
+ mo->pos.y = atoi(strtok(NULL, delim));
+ mo->pos.x = atoi(strtok(NULL, delim));
+ * mo_ptr_ptr = mo;
+ mo_ptr_ptr = &mo->next;
}
- return write_uint16_bigendian(0, file);
+ world->last_map_obj = mo;
}
-extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
+extern void add_map_object(struct World * world, uint8_t type)
{
- char * err = "Trouble in read_map_objects() with get_next_map_obj().";
- struct MapObj * map_obj;
- struct MapObjDef * mod;
- size_t size;
- uint8_t type;
- char first = 1;
- long pos;
- uint16_t read_uint16 = 0;
+ char * f_name = "add_map_object";
+ struct MapObjDef * mod = get_map_object_def(world, type);
+ struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
+ mo->id = world->map_obj_count;
+ world->map_obj_count++;
+ mo->type = mod->id;
+ mo->lifepoints = mod->lifepoints;
while (1)
{
- pos = ftell(file);
- if (read_uint16_bigendian(file, &read_uint16))
+ struct yx_uint16 pos = find_passable_pos(world->map);
+ struct MapObj * mo_ptr;
+ uint8_t clear = 1;
+ for (mo_ptr = world->map_objs;
+ mo_ptr != NULL;
+ mo_ptr = mo_ptr->next)
{
- return 1;
+ if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
+ {
+ clear = 0;
+ break;
+ }
}
- if (0 == read_uint16)
+ if (1 == clear)
{
+ mo->pos = pos;
break;
}
- fseek(file, pos, SEEK_SET);
- if (read_uint8(file, &type))
- {
- return 1;
- }
- mod = get_map_obj_def(world, type);
- if ('m' == mod->m_or_i)
- {
- size = sizeof(struct Monster);
- }
- else
- {
- size = sizeof(struct Item);
- }
- map_obj = get_next_map_obj(start, &first, size, map_obj);
- exit_err(NULL == map_obj, world, err);
- map_obj->type = type;
- if ( read_uint16_bigendian(file, &map_obj->pos.y)
- || read_uint16_bigendian(file, &map_obj->pos.x))
- {
- return 1;
- }
- map_obj->pos.y--;
- map_obj->pos.x--;
- if ('m' == mod->m_or_i)
- {
- if (read_map_objects_monsterdata(map_obj, file))
- {
- return 1;
- }
- }
}
- if (!first)
+ mo->next = NULL;
+ if (NULL == world->last_map_obj)
{
- map_obj->next = 0;
+ world->map_objs = mo;
}
- return 0;
+ else
+ {
+ world->last_map_obj->next = mo;
+ }
+ world->last_map_obj = mo;
}
-extern void * build_map_objects(struct World * world, void * start, char def_id,
- uint8_t n)
+extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
{
- char * err = "Trouble in build_map_objects() with get_next_map_obj().";
uint8_t i;
- struct MapObj * mo;
- char first = 1;
- struct MapObjDef * mod = get_map_obj_def(world, def_id);
- size_t size = 0;
- if ('i' == mod->m_or_i)
- {
- size = sizeof(struct Item);
- }
- else
- {
- size = sizeof(struct Monster);
- }
for (i = 0; i < n; i++)
{
- mo = get_next_map_obj(start, &first, size, mo);
- exit_err(NULL == mo, world, err);
- mo->pos = find_passable_pos(world->map);
- if ('i' == mod->m_or_i)
- {
- build_map_objects_itemdata(mod, mo);
- }
- else
- {
- build_map_objects_monsterdata(mod, mo);
- }
-
- }
- if (!first)
- {
- mo->next = 0;
+ add_map_object(world, type);
}
- return &mo->next;
}
-extern void free_items(struct Item * item)
+extern void free_map_objects(struct MapObj * mo_start)
{
- if (0 == item)
+ if (NULL == mo_start)
{
return;
}
- free_items((struct Item *) item->map_obj.next);
- free(item);
+ free_map_objects(mo_start->next);
+ free(mo_start);
}
-extern void free_monsters(struct Monster * monster)
+extern struct MapObj * get_player(struct World * world)
{
- if (0 == monster)
+ struct MapObj * ptr = world->map_objs;
+ while (1)
{
- return;
+ if (NULL == ptr)
+ {
+ return ptr;
+ }
+ if (0 == ptr->id)
+ {
+ return ptr;
+ }
+ ptr = ptr->next;
}
- free_monsters((struct Monster *) monster->map_obj.next);
- free(monster);
}
-extern struct MapObjDef * get_map_obj_def(struct World * world, char def_id)
+extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
{
- struct MapObjDef * d = NULL;
- for (d = (struct MapObjDef *) world->monster_def;
- d->id != def_id && 0 != d->next;
- d = d->next);
- if (d->id != def_id)
+ struct MapObjDef * mod = w->map_obj_defs;
+ while (id != mod->id)
{
- for (d = (struct MapObjDef *) world->item_def;
- d->id != def_id && 0 != d->next;
- d = d->next);
+ mod = mod->next;
}
- return d;
+ return mod;
}