/* map_objects.c */
#include "map_objects.h"
-#include <stdlib.h> /* for malloc(), calloc(), free(), atoi() */
+#include <stdlib.h> /* for free(), atoi() */
+#include <stdint.h> /* for uint8_t */
#include <stdio.h> /* for FILE typedef */
-#include <string.h> /* for strchr(), strlen(), memcpy() */
-#include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */
-#include "misc.h" /* for textfile_sizes(), find_passable_pos() */
+#include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
+#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() */
+#include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */
-/* 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.
- */
-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)
+extern void init_map_object_defs(struct World * world, char * filename)
{
- if (* first)
- {
- struct MapObj * * z = start;
- map_obj = malloc(size);
- * z = map_obj;
- * first = 0;
- }
- else
+ 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 = " ";
+ char line[linemax + 1];
+ while (try_fgets(line, linemax + 1, file, world, f_name))
{
- map_obj->next = malloc(size);
- map_obj = map_obj->next;
+ 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;
}
- 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;
+ try_fclose(file, world, f_name);
}
-static void build_map_objects_monsterdata(struct MapObjDef * map_obj_def,
- void * start)
+extern void free_map_object_defs(struct MapObjDef * mod_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;
+ if (NULL == mod_start)
+ {
+ return;
+ }
+ free_map_object_defs(mod_start->next);
+ free(mod_start->name);
+ free(mod_start);
}
-static uint8_t write_map_objects_monsterdata(void * start, FILE * file)
+extern void write_map_objects(struct World * world, FILE * file)
{
- struct Monster * m = (struct Monster *) start;
- return write_uint8(m->hitpoints, file);
+ 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)
+ {
+ 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;
+ }
}
-static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
+extern void read_map_objects(struct World * world, FILE * file, char * line,
+ int linemax)
{
- struct Monster * m = (struct Monster *) start;
- return read_uint8(file, &m->hitpoints);
+ 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))
+ {
+ mo = malloc(sizeof(struct MapObj));
+ mo->next = NULL;
+ mo->id = atoi(strtok(line, delim));
+ if (mo->id > world->map_obj_count)
+ {
+ 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;
+ }
+ world->last_map_obj = mo;
}
-extern void init_map_object_defs(struct World * world, char * filename)
+extern void add_map_object(struct World * world, uint8_t type)
{
- 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;
- while (fgets(defline, linemax, file))
+ 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)
{
- mod.next = 0;
- mod.id = atoi(defline);
- line_p = strchr(defline, ' ') + 1;
- mod.m_or_i = * line_p;
- mod.mapchar = * (line_p + 2);
- if ('i' == mod.m_or_i)
- {
- line_p = line_p + 5;
- }
- else
+ 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)
{
- md.hitpoints_start = atoi (line_p + 4);
- line_p = strchr (line_p + 4, ' ') + 1;
+ if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
+ {
+ clear = 0;
+ break;
+ }
}
- mod.desc = calloc (strlen (line_p), sizeof(char));
- memcpy (mod.desc, line_p, strlen(line_p) - 1);
- if ('i' == mod.m_or_i)
+ if (1 == clear)
{
- 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;
+ mo->pos = pos;
+ break;
}
}
- free(defline);
- fclose(file);
-};
+ mo->next = NULL;
+ if (NULL == world->last_map_obj)
+ {
+ world->map_objs = mo;
+ }
+ else
+ {
+ world->last_map_obj->next = mo;
+ }
+ world->last_map_obj = mo;
+}
-extern uint8_t write_map_objects(struct World * world, void * start,
- FILE * file)
+extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
{
- struct MapObj * map_obj;
- struct MapObjDef * mod;
- uint8_t fail = 0;
- for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
+ uint8_t i;
+ for (i = 0; i < n; i++)
{
- fail = fail | write_uint8(map_obj->type, file);
- fail = fail | write_uint16_bigendian(map_obj->pos.y + 1, file);
- fail = fail | write_uint16_bigendian(map_obj->pos.x + 1, file);
- mod = get_map_obj_def(world, map_obj->type);
- if ('m' == mod->m_or_i)
- {
- fail = fail | write_map_objects_monsterdata(map_obj, file);
- }
+ add_map_object(world, type);
}
- return (fail | write_uint16_bigendian(0, file));
}
-extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
+extern void free_map_objects(struct MapObj * mo_start)
{
- struct MapObj * map_obj;
- struct MapObjDef * mod;
- size_t size;
- uint8_t type;
- char first = 1;
- long pos;
- uint16_t read_uint16 = 0;
- uint8_t fail = 0;
- while (1)
+ if (NULL == mo_start)
{
- pos = ftell(file);
- fail = fail | read_uint16_bigendian(file, &read_uint16);
- if (0 == read_uint16)
- {
- break;
- }
- fseek(file, pos, SEEK_SET);
- fail = fail | read_uint8(file, &type);
- 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);
- map_obj->type = type;
- fail = fail | read_uint16_bigendian(file, &map_obj->pos.y);
- fail = fail | read_uint16_bigendian(file, &map_obj->pos.x);
- map_obj->pos.y--;
- map_obj->pos.x--;
- if ('m' == mod->m_or_i)
- {
- fail = fail | read_map_objects_monsterdata(map_obj, file);
- }
- }
- if (!first)
- {
- map_obj->next = 0;
+ return;
}
- return fail;
+ free_map_objects(mo_start->next);
+ free(mo_start);
}
-extern void * build_map_objects(struct World * world, void * start, char def_id,
- unsigned char n)
+extern struct MapObj * get_player(struct World * world)
{
- unsigned char 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++)
+ struct MapObj * ptr = world->map_objs;
+ while (1)
{
- mo = get_next_map_obj(start, &first, size, mo);
- mo->pos = find_passable_pos(world->map);
- if ('i' == mod->m_or_i)
+ if (NULL == ptr)
{
- build_map_objects_itemdata(mod, mo);
+ return ptr;
}
- else
+ if (0 == ptr->id)
{
- build_map_objects_monsterdata(mod, mo);
+ return ptr;
}
-
- }
- if (!first)
- {
- mo->next = 0;
+ ptr = ptr->next;
}
- return &mo->next;
}
-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;
}