Allocate rects dynamically

... 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.
This commit is contained in:
Pasi Kallinen
2022-01-05 17:35:33 +02:00
parent 7238622621
commit e5ee580961
3 changed files with 22 additions and 5 deletions
+1
View File
@@ -2171,6 +2171,7 @@ extern boolean create_particular(void);
/* ### rect.c ### */ /* ### rect.c ### */
extern void init_rect(void); extern void init_rect(void);
extern void free_rect(void);
extern NhRect *get_rect(NhRect *); extern NhRect *get_rect(NhRect *);
extern NhRect *rnd_rect(void); extern NhRect *rnd_rect(void);
extern void remove_rect(NhRect *); extern void remove_rect(NhRect *);
+20 -5
View File
@@ -13,11 +13,11 @@ static boolean intersect(NhRect *, NhRect *, NhRect *);
* need for room generation. * need for room generation.
*/ */
#define MAXRECT 50
#define XLIM 4 #define XLIM 4
#define YLIM 3 #define YLIM 3
static NhRect rect[MAXRECT + 1]; static NhRect *rect = (NhRect *) 0;
static int n_rects = 0;
static int rect_cnt; static int rect_cnt;
/* /*
@@ -28,12 +28,28 @@ static int rect_cnt;
void void
init_rect(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_cnt = 1;
rect[0].lx = rect[0].ly = 0; rect[0].lx = rect[0].ly = 0;
rect[0].hx = COLNO - 1; rect[0].hx = COLNO - 1;
rect[0].hy = ROWNO - 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. * Search Index of one precise NhRect.
* *
@@ -133,9 +149,8 @@ remove_rect(NhRect* r)
void void
add_rect(NhRect* r) add_rect(NhRect* r)
{ {
if (rect_cnt >= MAXRECT) { if (rect_cnt >= n_rects) {
if (wizard) impossible("n_rects may be too small.");
pline("MAXRECT may be too small.");
return; return;
} }
/* Check that this NhRect is not included in another one */ /* Check that this NhRect is not included in another one */
+1
View File
@@ -1082,6 +1082,7 @@ freedynamicdata(void)
free_waterlevel(); free_waterlevel();
free_dungeons(); free_dungeons();
free_CapMons(); free_CapMons();
free_rect();
/* some pointers in iflags */ /* some pointers in iflags */
if (iflags.wc_font_map) if (iflags.wc_font_map)