]> granicus.if.org Git - nethack/commitdiff
Allocate rects dynamically
authorPasi Kallinen <paxed@alt.org>
Wed, 5 Jan 2022 15:35:30 +0000 (17:35 +0200)
committerPasi Kallinen <paxed@alt.org>
Wed, 5 Jan 2022 15:35:33 +0000 (17:35 +0200)
... instead of hard-coding them to 50. New allocated value is
(COLNO*ROWNO)/30, which is slightly higher (56), and that formula
seems to work for hypothetical larger maps too.

include/extern.h
src/rect.c
src/save.c

index 7c6bac9c180adeffb5038f6ccd381695f256fcb1..de02946358dccd6d8c9ae74249719a8e29218493 100644 (file)
@@ -2171,6 +2171,7 @@ extern boolean create_particular(void);
 /* ### rect.c ### */
 
 extern void init_rect(void);
+extern void free_rect(void);
 extern NhRect *get_rect(NhRect *);
 extern NhRect *rnd_rect(void);
 extern void remove_rect(NhRect *);
index 26d7a1167f4aa98ad024e39dc9cf0593c7c91c83..5aaf4a688b6487c6cf3fbd0b41cf7a8e758769e6 100644 (file)
@@ -13,11 +13,11 @@ static boolean intersect(NhRect *, NhRect *, NhRect *);
  * need for room generation.
  */
 
-#define MAXRECT 50
 #define XLIM 4
 #define YLIM 3
 
-static NhRect rect[MAXRECT + 1];
+static NhRect *rect = (NhRect *) 0;
+static int n_rects = 0;
 static int rect_cnt;
 
 /*
@@ -28,12 +28,28 @@ static int rect_cnt;
 void
 init_rect(void)
 {
+    if (!rect) {
+        n_rects = (COLNO * ROWNO) / 30;
+        rect = (NhRect *) alloc(sizeof(NhRect) * n_rects);
+        if (!rect)
+            panic("Could not alloc rect");
+    }
+
     rect_cnt = 1;
     rect[0].lx = rect[0].ly = 0;
     rect[0].hx = COLNO - 1;
     rect[0].hy = ROWNO - 1;
 }
 
+void
+free_rect(void)
+{
+    if (rect)
+        free(rect);
+    n_rects = rect_cnt = 0;
+}
+
+
 /*
  * Search Index of one precise NhRect.
  *
@@ -133,9 +149,8 @@ remove_rect(NhRect* r)
 void
 add_rect(NhRect* r)
 {
-    if (rect_cnt >= MAXRECT) {
-        if (wizard)
-            pline("MAXRECT may be too small.");
+    if (rect_cnt >= n_rects) {
+        impossible("n_rects may be too small.");
         return;
     }
     /* Check that this NhRect is not included in another one */
index 3fa32a431c84616b6dbe950db716ebcf1ec40d7b..27106c9087984a3abdbd29e42d4358b77480dcf5 100644 (file)
@@ -1082,6 +1082,7 @@ freedynamicdata(void)
     free_waterlevel();
     free_dungeons();
     free_CapMons();
+    free_rect();
 
     /* some pointers in iflags */
     if (iflags.wc_font_map)