sp_lev.c reformatting

Mostly reformatting but fixes a bug in mapfrag_free(); would matter
if the same map fragment gets freed a second time.
This commit is contained in:
PatR
2021-05-30 01:01:01 -07:00
parent 72866e2252
commit 0fda8504bb
2 changed files with 168 additions and 146 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 sp_lev.h $NHDT-Date: 1599434249 2020/09/06 23:17:29 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.39 $ */
/* NetHack 3.7 sp_lev.h $NHDT-Date: 1622361649 2021/05/30 08:00:49 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.47 $ */
/* Copyright (c) 1989 by Jean-Christophe Collet */
/* NetHack may be freely redistributed. See license for details. */
@@ -196,7 +196,7 @@ struct mapfragment {
};
#define SET_TYPLIT(x, y, ttyp, llit) \
{ \
do { \
if ((x) >= 1 && (y) >= 0 && (x) < COLNO && (y) < ROWNO) { \
if ((ttyp) < MAX_TYPE && levl[(x)][(y)].typ != STAIRS \
&& levl[(x)][(y)].typ != LADDER) \
@@ -210,6 +210,6 @@ struct mapfragment {
levl[(x)][(y)].lit = (llit); \
} \
} \
}
} while (0)
#endif /* SP_LEV_H */
+65 -43
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 sp_lev.c $NHDT-Date: 1605779812 2020/11/19 09:56:52 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.216 $ */
/* NetHack 3.7 sp_lev.c $NHDT-Date: 1622361654 2021/05/30 08:00:54 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.233 $ */
/* Copyright (c) 1989 by Jean-Christophe Collet */
/* NetHack may be freely redistributed. See license for details. */
@@ -189,8 +189,7 @@ static struct monst *invent_carrying_monster = (struct monst *) 0;
* end of no 'g.'
*/
#define TYP_CANNOT_MATCH(typ) \
((typ) == MAX_TYPE || (typ) == INVALID_TYPE)
#define TYP_CANNOT_MATCH(typ) ((typ) == MAX_TYPE || (typ) == INVALID_TYPE)
/* Does typ match with levl[][].typ, considering special types
MATCH_WALL and MAX_TYPE (aka transparency)? */
@@ -207,7 +206,7 @@ match_maptyps(xchar typ, xchar levltyp)
struct mapfragment *
mapfrag_fromstr(char *str)
{
struct mapfragment *mf = (struct mapfragment *) alloc(sizeof(struct mapfragment));
struct mapfragment *mf = (struct mapfragment *) alloc(sizeof *mf);
char *tmps;
@@ -239,7 +238,7 @@ mapfrag_free(struct mapfragment** mf)
if (mf && *mf) {
free((*mf)->data);
free(*mf);
mf = NULL;
*mf = NULL;
}
}
@@ -247,7 +246,8 @@ schar
mapfrag_get(struct mapfragment *mf, int x, int y)
{
if (y < 0 || x < 0 || y > mf->hei - 1 || x > mf->wid - 1)
panic("outside mapfrag (%i,%i), wanted (%i,%i)", mf->wid, mf->hei, x,y);
panic("outside mapfrag (%i,%i), wanted (%i,%i)",
mf->wid, mf->hei, x, y);
return splev_chr2typ(mf->data[y * (mf->wid + 1) + x]);
}
@@ -260,16 +260,18 @@ mapfrag_canmatch(struct mapfragment* mf)
const char *
mapfrag_error(struct mapfragment *mf)
{
if (!mf)
return "mapfragment error";
else if (!mapfrag_canmatch(mf)) {
const char *res = NULL;
if (!mf) {
res = "mapfragment error";
} else if (!mapfrag_canmatch(mf)) {
mapfrag_free(&mf);
return "mapfragment needs to have odd height and width";
} else if (TYP_CANNOT_MATCH(mapfrag_get(mf, (mf->wid/2), (mf->hei/2)))) {
res = "mapfragment needs to have odd height and width";
} else if (TYP_CANNOT_MATCH(mapfrag_get(mf, mf->wid / 2, mf->hei / 2))) {
mapfrag_free(&mf);
return "mapfragment center must be valid terrain";
res = "mapfragment center must be valid terrain";
}
return NULL;
return res;
}
boolean
@@ -279,8 +281,10 @@ mapfrag_match(struct mapfragment* mf, int x, int y)
for (rx = -(mf->wid / 2); rx <= (mf->wid / 2); rx++)
for (ry = -(mf->hei / 2); ry <= (mf->hei / 2); ry++) {
schar mapc = mapfrag_get(mf, rx + (mf->wid / 2) , ry + (mf->hei / 2));
schar mapc = mapfrag_get(mf, rx + (mf->wid / 2),
ry + (mf->hei / 2));
schar levc = isok(x+rx, y+ry) ? levl[x+rx][y+ry].typ : STONE;
if (!match_maptyps(mapc, levc))
return FALSE;
}
@@ -340,16 +344,27 @@ lvlfill_swamp(schar fg, schar bg, schar lit)
for (x = 2; x <= g.x_maze_max; x += 2)
for (y = 0; y <= g.y_maze_max; y += 2) {
int c = 0;
SET_TYPLIT(x, y, fg, lit);
if (levl[x+1][y].typ == bg) c++;
if (levl[x][y+1].typ == bg) c++;
if (levl[x+1][y+1].typ == bg) c++;
if (levl[x + 1][y].typ == bg)
++c;
if (levl[x][y + 1].typ == bg)
++c;
if (levl[x + 1][y + 1].typ == bg)
++c;
if (c == 3) {
switch (rn2(3)) {
case 0: SET_TYPLIT((x+1),y, fg, lit); break;
case 1: SET_TYPLIT(x, (y+1), fg, lit); break;
case 2: SET_TYPLIT((x+1),(y+1), fg, lit); break;
default: break;
case 0:
SET_TYPLIT(x + 1,y, fg, lit);
break;
case 1:
SET_TYPLIT(x, y + 1, fg, lit);
break;
case 2:
SET_TYPLIT(x + 1, y + 1, fg, lit);
break;
default:
break;
}
}
}
@@ -4158,7 +4173,7 @@ lspo_random_corridors(lua_State* L UNUSED)
struct selectionvar *
selection_new(void)
{
struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof(struct selectionvar));
struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof *tmps);
tmps->wid = COLNO;
tmps->hei = ROWNO;
@@ -4185,8 +4200,7 @@ selection_free(struct selectionvar* sel, boolean freesel)
struct selectionvar *
selection_clone(struct selectionvar* sel)
{
struct selectionvar *
tmps = (struct selectionvar *) alloc(sizeof (struct selectionvar));
struct selectionvar *tmps = (struct selectionvar *) alloc(sizeof *tmps);
tmps->wid = sel->wid;
tmps->hei = sel->hei;
@@ -4241,7 +4255,8 @@ selection_filter_mapchar(struct selectionvar* ov, xchar typ, int lit)
for (x = 0; x < ret->wid; x++)
for (y = 0; y < ret->hei; y++)
if (selection_getpoint(x, y, ov) && match_maptyps(typ, levl[x][y].typ)) {
if (selection_getpoint(x, y, ov)
&& match_maptyps(typ, levl[x][y].typ)) {
switch (lit) {
default:
case -2:
@@ -4849,7 +4864,8 @@ l_table_getset_feature_flag(
int val = get_table_boolean_opt(L, name, -2);
if (val != -2) {
if (val == -1) val = rn2(2);
if (val == -1)
val = rn2(2);
if (val)
levl[x][y].flags |= flag;
else
@@ -5224,6 +5240,7 @@ get_table_intarray_entry(lua_State* L, int tableidx, int entrynum)
ret = lua_tointeger(L, -1);
} else {
char buf[BUFSZ];
Sprintf(buf, "Array entry #%i is %s, expected number",
1, luaL_typename(L, -1));
nhl_error(L, buf);
@@ -5814,7 +5831,8 @@ lspo_wallify(lua_State* L)
/* TODO: clamp coord values */
/* TODO: maybe allow wallify({x1,y1}, {x2,y2}) */
/* TODO: is_table_coord(), is_table_area(), get_table_coord(), get_table_area() */
/* TODO: is_table_coord(), is_table_area(),
get_table_coord(), get_table_area() */
create_des_coder();
@@ -5871,6 +5889,7 @@ TODO: g.coder->croom needs to be updated
static const int t_or_b2i[] = { TOP, CENTER, BOTTOM, -1, -1 };
int lr, tb, x = -1, y = -1;
struct mapfragment *mf;
char *tmpstr;
int argc = lua_gettop(L);
boolean has_contents = FALSE;
int tryct = 0;
@@ -5882,12 +5901,11 @@ TODO: g.coder->croom needs to be updated
return 0;
if (argc == 1 && lua_type(L, 1) == LUA_TSTRING) {
char *tmpstr = dupstr(luaL_checkstring(L, 1));
tmpstr = dupstr(luaL_checkstring(L, 1));
lr = tb = CENTER;
mf = mapfrag_fromstr(tmpstr);
free(tmpstr);
} else {
char *tmpstr;
lcheck_param_table(L);
lr = l_or_r2i[get_table_option(L, "halign", "none", left_or_right)];
tb = t_or_b2i[get_table_option(L, "valign", "none", top_or_bot)];
@@ -5912,7 +5930,6 @@ TODO: g.coder->croom needs to be updated
ox = x;
oy = y;
redo_maploc:
g.xsize = mf->wid;
g.ysize = mf->hei;
@@ -5921,7 +5938,8 @@ redo_maploc:
if (ox == -1) {
if (g.coder->croom) {
x = somex(g.coder->croom) - mf->wid;
if (x < 1) x = 1;
if (x < 1)
x = 1;
} else {
x = 1 + rn2(COLNO - 1 - mf->wid);
}
@@ -5930,7 +5948,8 @@ redo_maploc:
if (oy == -1) {
if (g.coder->croom) {
y = somey(g.coder->croom) - mf->hei;
if (y < 1) y = 1;
if (y < 1)
y = 1;
} else {
y = rn2(ROWNO - mf->wid);
}
@@ -6018,21 +6037,26 @@ redo_maploc:
if (g.in_mk_themerooms) {
boolean isokp = TRUE;
for (y = g.ystart - 1; y < min(ROWNO, g.ystart + g.ysize) + 1; y++)
for (x = g.xstart - 1; x < min(COLNO, g.xstart + g.xsize) + 1; x++) {
for (x = g.xstart - 1; x < min(COLNO, g.xstart + g.xsize) + 1;
x++) {
if (!isok(x, y)) {
isokp = FALSE;
} else if (y < g.ystart || y >= (g.ystart + g.ysize)
|| x < g.xstart || x >= (g.xstart + g.xsize)) {
if (levl[x][y].typ != STONE) isokp = FALSE;
if (levl[x][y].roomno != NO_ROOM) isokp = FALSE;
if (levl[x][y].typ != STONE
|| levl[x][y].roomno != NO_ROOM)
isokp = FALSE;
} else {
mptyp = mapfrag_get(mf, (x - g.xstart), (y - g.ystart));
if (mptyp >= MAX_TYPE) continue;
if (levl[x][y].typ != STONE && levl[x][y].typ != mptyp) isokp = FALSE;
if (levl[x][y].roomno != NO_ROOM) isokp = FALSE;
mptyp = mapfrag_get(mf, x - g.xstart, y - g.ystart);
if (mptyp >= MAX_TYPE)
continue;
if ((levl[x][y].typ != STONE
&& levl[x][y].typ != mptyp)
|| levl[x][y].roomno != NO_ROOM)
isokp = FALSE;
}
if (!isokp) {
if ((tryct++ < 100) && ((lr == -1) || (tb == -1)))
if (tryct++ < 100 && (lr == -1 || tb == -1))
goto redo_maploc;
g.themeroom_failed = TRUE;
goto skipmap;
@@ -6087,7 +6111,6 @@ redo_maploc:
}
skipmap:
mapfrag_free(&mf);
if (has_contents && !(g.in_mk_themerooms && g.themeroom_failed)) {
@@ -6114,8 +6137,7 @@ static struct sp_coder *
sp_level_coder_init(void)
{
int tmpi;
struct sp_coder *coder =
(struct sp_coder *) alloc(sizeof (struct sp_coder));
struct sp_coder *coder = (struct sp_coder *) alloc(sizeof *coder);
coder->premapped = FALSE;
coder->solidify = FALSE;