home · contact · privacy
Strongly overhauled keybinding managemment. Window-specific keybindings and a window...
[plomrogue] / src / map_objects.c
index 6ed8c331e4374a4a286fdbb70a314627055c4669..8d08ae04db1832e3222799df73278984aff731fb 100644 (file)
@@ -1,20 +1,24 @@
 /* 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() */
 
 
 
 /* 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,
+static struct MapObj * get_next_map_obj(struct World * world,
+                                        void * start, char * first,
                                         size_t size, struct MapObj * map_obj);
 
 
@@ -29,19 +33,21 @@ static uint8_t read_map_objects_monsterdata( void * start, FILE * file);
 
 
 
-static struct MapObj * get_next_map_obj(void * start, char * first,
+static struct MapObj * get_next_map_obj(struct World * world,
+                                        void * start, char * first,
                                         size_t size, struct MapObj * map_obj)
 {
+    char * f_name = "get_next_map_obj()";
     if (* first)
     {
         struct MapObj * * z = start;
-        map_obj = malloc(size);
+        map_obj = try_malloc(size, world, f_name);
         * z = map_obj;
         * first = 0;
     }
     else
     {
-        map_obj->next = malloc(size);
+        map_obj->next = try_malloc(size, world, f_name);
         map_obj = map_obj->next;
     }
     return map_obj;
@@ -87,54 +93,79 @@ static uint8_t read_map_objects_monsterdata (void * start, FILE * file)
 
 extern void init_map_object_defs(struct World * world, char * filename)
 {
-    world->item_def    = 0;
-    world->monster_def = 0;
-    FILE * file = fopen(filename, "r");
-    uint16_t linemax;
-    textfile_sizes (file, &linemax, NULL);
+    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  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);
+    char defline[linemax + 1];
     char * line_p;
-    while (fgets(defline, linemax, file))
+    char * delim = " ";
+    while (fgets(defline, linemax + 1, file))
     {
         mod.next    = 0;
-        mod.id      = atoi(defline);
-        line_p      = strchr(defline, ' ') + 1;
-        mod.m_or_i  = * line_p;
-        mod.mapchar = * (line_p + 2);
+        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 = line_p + 5;
+            line_p = strtok(NULL, delim);
         }
         else
         {
-            md.hitpoints_start = atoi   (line_p + 4);
-            line_p             = strchr (line_p + 4, ' ') + 1;
+            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));
-        memcpy (mod.desc, line_p, strlen(line_p) - 1);
+        mod.desc = try_calloc(strlen(line_p), sizeof(char), world, f_name);
+        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));
+            * p_p_id       = try_malloc(sizeof(struct ItemDef), world, f_name);
             * * 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     = try_malloc(sizeof(struct MonsterDef), world, f_name);
             * * p_p_md     = md;
             p_p_md         = (struct MonsterDef * *) * p_p_md;
         }
     }
-    free(defline);
-    fclose(file);
-};
+    try_fclose(file, world, f_name);
+}
+
+
+
+extern void free_item_defs(struct ItemDef * id_start)
+{
+    if (0 != id_start->map_obj_def.next)
+    {
+        free_item_defs((struct ItemDef *) id_start->map_obj_def.next);
+    }
+    free(id_start->map_obj_def.desc);
+    free(id_start);
+}
+
+
+
+
+extern void free_monster_defs(struct MonsterDef * md_start)
+{
+    if (0 != md_start->map_obj_def.next)
+    {
+        free_monster_defs((struct MonsterDef *) md_start->map_obj_def.next);
+    }
+    free(md_start->map_obj_def.desc);
+    free(md_start);
+}
 
 
 
@@ -143,25 +174,31 @@ extern uint8_t write_map_objects(struct World * world, void * start,
 {
     struct MapObj * map_obj;
     struct MapObjDef * mod;
-    uint8_t err = 0;
     for (map_obj = start; map_obj != 0; map_obj = map_obj->next)
     {
-        err = err | write_uint8(map_obj->type, file);
-        err = err | write_uint16_bigendian(map_obj->pos.y + 1, file);
-        err = err | write_uint16_bigendian(map_obj->pos.x + 1, file);
+        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)
         {
-            err = err | write_map_objects_monsterdata(map_obj, file);
+            if (write_map_objects_monsterdata(map_obj, file))
+            {
+                return 1;
+            }
         }
     }
-    return (err | write_uint16_bigendian(0, file));
+    return write_uint16_bigendian(0, file);
 }
 
 
 
 extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
 {
+    char * err = "Trouble in read_map_objects() with get_next_map_obj().";
     struct MapObj * map_obj;
     struct MapObjDef * mod;
     size_t size;
@@ -169,17 +206,22 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
     char first = 1;
     long pos;
     uint16_t read_uint16 = 0;
-    uint8_t err = 0;
     while (1)
     {
         pos = ftell(file);
-        err = err | read_uint16_bigendian(file, &read_uint16);
+        if (read_uint16_bigendian(file, &read_uint16))
+        {
+            return 1;
+        }
         if (0 == read_uint16)
         {
             break;
         }
         fseek(file, pos, SEEK_SET);
-        err = err | read_uint8(file, &type);
+        if (read_uint8(file, &type))
+        {
+            return 1;
+        }
         mod = get_map_obj_def(world, type);
         if ('m' == mod->m_or_i)
         {
@@ -189,22 +231,29 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
         {
             size = sizeof(struct Item);
         }
-        map_obj = get_next_map_obj(start, &first, size, map_obj);
+        map_obj = get_next_map_obj(world, start, &first, size, map_obj);
+        exit_err(NULL == map_obj, world, err);
         map_obj->type = type;
-        err = err | read_uint16_bigendian(file, &map_obj->pos.y);
-        err = err | read_uint16_bigendian(file, &map_obj->pos.x);
+        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)
         {
-            err = err | read_map_objects_monsterdata(map_obj, file);
+            if (read_map_objects_monsterdata(map_obj, file))
+            {
+                return 1;
+            }
         }
     }
     if (!first)
     {
         map_obj->next = 0;
     }
-    return err;
+    return 0;
 }
 
 
@@ -212,6 +261,7 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file)
 extern void * build_map_objects(struct World * world, void * start, char def_id,
                                 uint8_t n)
 {
+    char * err = "Trouble in build_map_objects() with get_next_map_obj().";
     uint8_t i;
     struct MapObj * mo;
     char first = 1;
@@ -227,7 +277,8 @@ extern void * build_map_objects(struct World * world, void * start, char def_id,
     }
     for (i = 0; i < n; i++)
     {
-        mo = get_next_map_obj(start, &first, size, mo);
+        mo = get_next_map_obj(world, start, &first, size, mo);
+        exit_err(NULL == mo, world, err);
         mo->pos = find_passable_pos(world->map);
         if ('i' == mod->m_or_i)
         {
@@ -248,7 +299,31 @@ extern void * build_map_objects(struct World * world, void * start, char def_id,
 
 
 
-extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id)
+extern void free_items(struct Item * item)
+{
+    if (0 == item)
+    {
+        return;
+    }
+    free_items((struct Item *) item->map_obj.next);
+    free(item);
+}
+
+
+
+extern void free_monsters(struct Monster * monster)
+{
+    if (0 == monster)
+    {
+        return;
+    }
+    free_monsters((struct Monster *) monster->map_obj.next);
+    free(monster);
+}
+
+
+
+extern struct MapObjDef * get_map_obj_def(struct World * world, char def_id)
 {
     struct MapObjDef * d = NULL;
     for (d = (struct MapObjDef *) world->monster_def;