home · contact · privacy
Unify Map struct common to server and client in src/common/map.h.
[plomrogue] / src / server / map_objects.c
index ae3fbd1884146759b09f54ed8f6216573fc35a1e..975c7487fe98906bf4d588c7b4350d02d0da6a13 100644 (file)
@@ -2,11 +2,9 @@
 
 #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(), memset() */
-#include "../common/err_try_fgets.h" /* err_try_fgets() */
+#include <stdint.h> /* uint8_t, uint16_t, UINT16_MAX */
+#include <stdlib.h> /* free() */
+#include <string.h> /* memset(), strlen() */
 #include "../common/rexit.h" /* exit_err() */
 #include "../common/try_malloc.h" /* try_malloc() */
 #include "../common/yx_uint8.h" /* yx_uint8 struct */
 #include "world.h" /* global world */
 #include "yx_uint8.h" /* yx_uint8_cmp() */
 
-#include "io.h" /* struct EntrySkeleton */
+
 
 /* Return pointer to map object of "id" in chain starting at "ptr". */
 static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id);
 
-/* Return random passable (as by is_passable()) position on world.map. */
-static struct yx_uint8 find_passable_pos();
-
-/* Add object of "type" to map on random position. Don't place actor on actor.*/
+/* Add object of "type" to map on passable position. Don't put actor on actor.*/
 static void add_map_object(uint8_t type);
 
 
@@ -47,19 +42,6 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id)
 
 
 
-static struct yx_uint8 find_passable_pos()
-{
-    struct yx_uint8 pos;
-    for (pos.y = pos.x = 0; 0 == is_passable(pos);)
-    {
-        pos.y = rrand() % world.map.size.y;
-        pos.x = rrand() % world.map.size.x;
-    }
-    return pos;
-}
-
-
-
 static void add_map_object(uint8_t type)
 {
     char * f_name = "add_map_object()";
@@ -69,9 +51,17 @@ static void add_map_object(uint8_t type)
     mo->id         = world.map_obj_count++;
     mo->type       = mod->id;
     mo->lifepoints = mod->lifepoints;
+    char * err = "Space to put map object on too hard to find. Map too small?";
+    uint16_t i = 0;
     while (1)
     {
-        struct yx_uint8 pos = find_passable_pos(world.map);
+        struct yx_uint8 pos;
+        for (pos.y = pos.x = 0; 0 == is_passable(pos); i++)
+        {
+            exit_err(UINT16_MAX == i, err);
+            pos.y = rrand() % world.map.size.y;
+            pos.x = rrand() % world.map.size.x;
+        }
         struct MapObj * mo_ptr;
         uint8_t clear = 1;
         for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)
@@ -95,27 +85,6 @@ static void add_map_object(uint8_t type)
 
 
 
-extern void read_map_object_def(char * line, uint32_t linemax, char * context,
-                                struct EntrySkeleton * entry, FILE * file)
-{
-    char * f_name = "init_map_object_defs()";
-    struct MapObjDef * mod = (struct MapObjDef *) entry;
-    err_try_fgets(line, linemax, file, context, "0nfi8");
-    mod->corpse_id = atoi(line);
-    err_try_fgets(line, linemax, file, context, "0nfs");
-    mod->char_on_map = line[0];
-    err_try_fgets(line, linemax, file, context, "0nfi8");
-    mod->lifepoints = atoi(line);
-    err_try_fgets(line, linemax, file, context, "0nf");
-    line[strlen(line) - 1] = '\0';
-    mod->name = try_malloc(strlen(line) + 1, f_name);
-    memcpy(mod->name, line, strlen(line) + 1);
-    err_try_fgets(line, linemax, file, context, "0nfi8");
-    mod->consumable = atoi(line);
-}
-
-
-
 extern void free_map_object_defs(struct MapObjDef * mod_start)
 {
     if (NULL == mod_start)
@@ -148,6 +117,7 @@ extern void free_map_objects(struct MapObj * mo_start)
     }
     free_map_objects(mo_start->owns);
     free_map_objects(mo_start->next);
+    free(mo_start->fov_map);
     free(mo_start);
     if (mo_start == world.map_objs)  /* So add_map_objects()' NULL-delimited  */
     {                                /* map object iteration loop does not    */