home · contact · privacy
New animate map objects are never placed on a square with other animated map objects...
[plomrogue] / src / map_objects.c
index e69028122d694d64ea24d5ffd9724541d34ec645..93f46841447aec667f1ddd0322268a0a330c0d6d 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() */
 
 
 
@@ -78,9 +79,10 @@ extern void read_map_objects(struct World * world, FILE * file, char * line,
     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))
     {
-        struct MapObj * mo = malloc(sizeof(struct MapObj));
+        mo = malloc(sizeof(struct MapObj));
         mo->next = NULL;
         mo->id = atoi(strtok(line, delim));
         if (mo->id > world->map_obj_count)
@@ -94,31 +96,62 @@ 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;
 }
 
 
 
-extern struct MapObj ** build_map_objects(struct World * w,
-                                           struct MapObj ** mo_ptr_ptr,
-                                           uint8_t type, uint8_t n)
+extern void add_map_object(struct World * world, uint8_t type)
 {
-    char * f_name = "build_map_objects()";
-    uint8_t i = 0;
-    struct MapObjDef * mod = get_map_object_def(w, type);
-    while (i < n)
+    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)
     {
-        struct MapObj * mo = try_malloc(sizeof(struct MapObj), w, f_name);
-        mo->id = w->map_obj_count;
-        w->map_obj_count++;
-        mo->type = mod->id;
-        mo->next = NULL;
-        mo->lifepoints = mod->lifepoints;
-        mo->pos = find_passable_pos(w->map);
-        i++;
-        * mo_ptr_ptr = mo;
-        mo_ptr_ptr = &mo->next;
+        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;
+        }
+    }
+    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 void add_map_objects(struct World * world, uint8_t type, uint8_t n)
+{
+    uint8_t i;
+    for (i = 0; i < n; i++)
+    {
+        add_map_object(world, type);
     }
-    return mo_ptr_ptr;
 }
 
 
@@ -135,6 +168,25 @@ extern void free_map_objects(struct MapObj * mo_start)
 
 
 
+extern struct MapObj * get_player(struct World * world)
+{
+    struct MapObj * ptr = world->map_objs;
+    while (1)
+    {
+        if (NULL == ptr)
+        {
+            return ptr;
+        }
+        if (0 == ptr->id)
+        {
+            return ptr;
+        }
+        ptr = ptr->next;
+    }
+}
+
+
+
 extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
 {
     struct MapObjDef * mod = w->map_obj_defs;