home · contact · privacy
Fixed bug where record unlinking in remake_world() *always* unlinked *any* previously...
[plomrogue] / src / server / map_objects.c
index 5b5bd975cbfe9d382c4313c36be51edc5221d222..36fb9b79829d9667f2f984f361fe77d6a62d72e7 100644 (file)
@@ -1,10 +1,11 @@
 /* src/server/map_objects.c */
 
 #include "map_objects.h"
+#include <stddef.h> /* NULL */
 #include <stdio.h> /* FILE typedef */
 #include <stdint.h> /* uint8_t, uint16_t */
 #include <stdlib.h> /* free(), atoi() */
-#include <string.h> /* strlen(), memcpy(), strtok() */
+#include <string.h> /* strlen(), memcpy(), strtok(), memset() */
 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
                                   * textfile_sizes()
                                   */
@@ -48,7 +49,7 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
 
 
 
-static struct yx_uint16 find_passable_pos() // struct Map * map)
+static struct yx_uint16 find_passable_pos()
 {
     struct yx_uint16 pos;
     for (pos.y = pos.x = 0; 0 == is_passable(pos);)
@@ -66,6 +67,7 @@ static void add_map_object(uint8_t type)
     char * f_name = "add_map_object()";
     struct MapObjDef * mod = get_map_object_def(type);
     struct MapObj *    mo  = try_malloc(sizeof(struct MapObj), f_name);
+    memset(mo, 0, sizeof(struct MapObj));
     mo->id         = world.map_obj_count++;
     mo->type       = mod->id;
     mo->lifepoints = mod->lifepoints;
@@ -88,11 +90,6 @@ static void add_map_object(uint8_t type)
             break;
         }
     }
-    mo->progress = 0;
-    mo->command  = 0;
-    mo->arg      = 0;
-    mo->owns     = NULL;
-    mo->next     = NULL;
     struct MapObj ** mo_ptr_ptr = &world.map_objs;
     for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next);
     * mo_ptr_ptr = mo;
@@ -137,7 +134,6 @@ extern void free_map_object_defs(struct MapObjDef * mod_start)
     free_map_object_defs(mod_start->next);
     free(mod_start->name);
     free(mod_start);
-    mod_start = NULL;
 }
 
 
@@ -162,10 +158,10 @@ extern void free_map_objects(struct MapObj * mo_start)
     free_map_objects(mo_start->owns);
     free_map_objects(mo_start->next);
     free(mo_start);
-    if (mo_start == world.map_objs)
-    {
-        world.map_objs = NULL;
-    }
+    if (mo_start == world.map_objs)  /* So add_map_objects()' NULL-delimited  */
+    {                                /* map object iteration loop does not    */
+        world.map_objs = NULL;       /* iterate over freed memory when called */
+    }                                /* the 1st time after world re-seeding.  */
 }