home · contact · privacy
Server: Check against setting map object id of 0 in config file reading.
[plomrogue] / src / server / map.c
index 9b0f29422b8564d3476b4f9223560413aa912d81..95d7e0ff4e96f875e17845e8a608c60a05307fbd 100644 (file)
@@ -1,7 +1,8 @@
 /* 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_uint8.h" /* struct yx_uint8 */
 #include "rrand.h" /* rrand() */
@@ -14,7 +15,7 @@ extern void init_map()
     char * f_name = "init_map()";
     uint32_t size = world.map.size.x * world.map.size.y;
     world.map.cells = try_malloc(size, f_name);
-    uint8_t y, x;
+    uint16_t y, x;
     for (y = 0; y < world.map.size.y; y++)
     {
         for (x = 0;
@@ -24,7 +25,9 @@ extern void init_map()
     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;
-    while (1)
+    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;
@@ -55,8 +58,7 @@ extern void init_map()
 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]);
     }