home · contact · privacy
Changed the way the end of the map object list is identified.
[plomrogue] / src / map_objects.c
index e825755b86a991b1b823756038d1df0a4181ec89..ed41d93ac62f30ab65c5fd194182782a8d034156 100644 (file)
@@ -11,6 +11,7 @@
 #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() */
 
 
 
@@ -95,7 +96,6 @@ extern void read_map_objects(struct World * world, FILE * file, char * line,
         * mo_ptr_ptr = mo;
         mo_ptr_ptr = &mo->next;
     }
-    world->last_map_obj = mo;
 }
 
 
@@ -109,17 +109,36 @@ extern void add_map_object(struct World * world, uint8_t type)
     world->map_obj_count++;
     mo->type = mod->id;
     mo->lifepoints = mod->lifepoints;
-    mo->pos = find_passable_pos(world->map);
-    mo->next = NULL;
-    if (NULL == world->last_map_obj)
+    while (1)
     {
-        world->map_objs = mo;
+        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)
+        {
+            if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints)
+            {
+                clear = 0;
+                break;
+            }
+        }
+        if (1 == clear)
+        {
+            mo->pos = pos;
+            break;
+        }
     }
-    else
+    mo->next = NULL;
+    struct MapObj ** last_ptr_ptr = &world->map_objs;
+    struct MapObj * mo_ptr;
+    while (NULL != * last_ptr_ptr)
     {
-        world->last_map_obj->next = mo;
+        mo_ptr = * last_ptr_ptr;
+        last_ptr_ptr = & mo_ptr->next;
     }
-    world->last_map_obj = mo;
+    * last_ptr_ptr = mo;
 }