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:30 +02:00
parent 7238622621
commit e5ee580961
3 changed files with 22 additions and 5 deletions

View 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 *);

View 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 */

View File

@@ -1082,6 +1082,7 @@ freedynamicdata(void)
free_waterlevel();
free_dungeons();
free_CapMons();
free_rect();
/* some pointers in iflags */
if (iflags.wc_font_map)