home · contact · privacy
Server: Limit iterations for map generation and object start placement.
[plomrogue] / src / server / map.c
index 2969b6dd4f9d551c40ebd3e7e7879989f0fea635..95d7e0ff4e96f875e17845e8a608c60a05307fbd 100644 (file)
@@ -1,9 +1,10 @@
 /* src/server/map.c */
 
 #include "map.h"
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
+#include "../common/rexit.h" /* exit_err() */
 #include "../common/try_malloc.h" /* try_malloc() */
-#include "../common/yx_uint16.h" /* struct yx_uint16 */
+#include "../common/yx_uint8.h" /* struct yx_uint8 */
 #include "rrand.h" /* rrand() */
 #include "world.h" /* global world */
 
@@ -21,13 +22,16 @@ extern void init_map()
              x < world.map.size.x;
              world.map.cells[(y * world.map.size.x) + x] = '~', x++);
     }
-    world.map.cells[size / 2 + (world.map.size.x / 2)] = '.';
-    uint32_t curpos;
-    while (1)
+    uint8_t add_half_width = !(world.map.size.y % 2) * (world.map.size.x / 2);
+    world.map.cells[(size / 2) + add_half_width] = '.';
+    uint16_t curpos;
+    char * err = "Map generation reached iteration limit. Change map size?";
+    uint32_t i;
+    for (i = 0; ; i++, exit_err(256 * UINT16_MAX == i, err))
     {
         y = rrand() % world.map.size.y;
         x = rrand() % world.map.size.x;
-        curpos = y * world.map.size.x + x;
+        curpos = (y * world.map.size.x) + x;
         if ('~' == world.map.cells[curpos]
             && (   (   curpos >= world.map.size.x
                     && '.' == world.map.cells[curpos - world.map.size.x])
@@ -39,8 +43,8 @@ extern void init_map()
                     && (curpos+1) % world.map.size.x != 0
                     && '.' == world.map.cells[curpos+1])))
         {
-            if (  y == 0 || y == world.map.size.y - 1 || x == 0
-                || x == world.map.size.x - 1)
+            if (   y == 0 || y == world.map.size.y - 1
+                || x == 0 || x == world.map.size.x - 1)
             {
                 break;
             }
@@ -51,13 +55,12 @@ extern void init_map()
 
 
 
-extern uint8_t is_passable(struct yx_uint16 pos)
+extern uint8_t is_passable(struct yx_uint8 pos)
 {
     uint8_t passable = 0;
-    if (   0 <= pos.x && pos.x < world.map.size.x
-        && 0 <= pos.y && pos.y < world.map.size.y)
+    if (pos.x < world.map.size.x && pos.y < world.map.size.y)
     {
-        passable = (('.' == world.map.cells[pos.y * world.map.size.x + pos.x]));
+        passable = ('.' == world.map.cells[(pos.y * world.map.size.x) + pos.x]);
     }
     return passable;
 }