Fix the lua integration parts marked for review

Also, explicitly allow traps of type "random".
This commit is contained in:
Pasi Kallinen
2019-11-23 17:42:14 +02:00
parent 95c1ccd716
commit acf48fa305
2 changed files with 31 additions and 54 deletions
+1
View File
@@ -789,6 +789,7 @@ traceback_handler(L)
lua_State *L; lua_State *L;
{ {
luaL_traceback(L, L, lua_tostring(L, 1), 0); luaL_traceback(L, L, lua_tostring(L, 1), 0);
/* TODO: call impossible() if fuzzing? */
return 1; return 1;
} }
+30 -54
View File
@@ -216,7 +216,6 @@ xchar x1, y1, x2, y2;
int prop; int prop;
{ {
register xchar x, y; register xchar x, y;
/* struct rm *lev; */ /* REVIEW: unreferenced */
x1 = max(x1, 1); x1 = max(x1, 1);
x2 = min(x2, COLNO - 1); x2 = min(x2, COLNO - 1);
@@ -2361,7 +2360,7 @@ get_table_montype(L)
lua_State *L; lua_State *L;
{ {
char *s = get_table_str_opt(L, "id", NULL); char *s = get_table_str_opt(L, "id", NULL);
int ret = NON_PM /*, i */ /* REVIEW: unreferenced */; int ret = NON_PM;
if (s) { if (s) {
ret = find_montype(L, s); ret = find_montype(L, s);
@@ -2892,11 +2891,9 @@ lua_State *L;
long ecoord; long ecoord;
const char *const engrtypes[] = { "dust", "engrave", "burn", "mark", "blood", NULL }; const char *const engrtypes[] = { "dust", "engrave", "burn", "mark", "blood", NULL };
const int engrtypes2i[] = { DUST, ENGRAVE, BURN, MARK, ENGR_BLOOD, 0 }; const int engrtypes2i[] = { DUST, ENGRAVE, BURN, MARK, ENGR_BLOOD, 0 };
xchar x, y; xchar x = -1, y = -1;
int argc = lua_gettop(L); int argc = lua_gettop(L);
x = y = 0; /* FIXME: quiet a warning for else clause below.
should it actually be -1? */
create_des_coder(); create_des_coder();
if (argc == 1) { if (argc == 1) {
@@ -2915,8 +2912,6 @@ lua_State *L;
txt = dupstr(luaL_checkstring(L, 3)); txt = dupstr(luaL_checkstring(L, 3));
} else { } else {
nhl_error(L, "Wrong parameters"); nhl_error(L, "Wrong parameters");
/* FIXME: this clause left etyp uninitialized so initialization
to DUST was added above to quiet a macosx warning */
} }
if (x == -1 && y == -1) if (x == -1 && y == -1)
@@ -3168,7 +3163,7 @@ lua_State *L;
const int stairdirs2i[] = { 0, 1 }; const int stairdirs2i[] = { 0, 1 };
long scoord; long scoord;
int ax = -1, ay = -1; /* FIXME: initializers added, macosx warning */ int ax = -1, ay = -1;
int up; int up;
int ltype = lua_type(L, 1); int ltype = lua_type(L, 1);
@@ -3331,7 +3326,8 @@ const struct {
{ "anti magic", ANTI_MAGIC }, { "anti magic", ANTI_MAGIC },
{ "polymorph", POLY_TRAP }, { "polymorph", POLY_TRAP },
{ "vibrating square", VIBRATING_SQUARE }, { "vibrating square", VIBRATING_SQUARE },
{ 0, 0 } }; { "random", -1 },
{ 0, NO_TRAP } };
int int
get_table_traptype_opt(L, name, defval) get_table_traptype_opt(L, name, defval)
@@ -3352,6 +3348,19 @@ int defval;
return defval; return defval;
} }
int
get_traptype_byname(trapname)
char *trapname;
{
int i;
for (i = 0; trap_types[i].name; i++)
if (!strcmpi(trapname, trap_types[i].name))
return trap_types[i].type;
return NO_TRAP;
}
/* trap({ type = "hole", x = 1, y = 1 }); */ /* trap({ type = "hole", x = 1, y = 1 }); */
/* trap("hole", 3, 4); */ /* trap("hole", 3, 4); */
/* trap("level teleport", {5, 8}); */ /* trap("level teleport", {5, 8}); */
@@ -3363,49 +3372,27 @@ lua_State *L;
{ {
spltrap tmptrap; spltrap tmptrap;
int x, y; int x, y;
/* long tcoord; */ /* REVIEW: unreferenced */
int argc = lua_gettop(L); int argc = lua_gettop(L);
create_des_coder(); create_des_coder();
if (argc == 1 && lua_type(L, 1) == LUA_TSTRING) { if (argc == 1 && lua_type(L, 1) == LUA_TSTRING) {
const char *trapstr = luaL_checkstring(L, 1); const char *trapstr = luaL_checkstring(L, 1);
int i;
tmptrap.type = -1; tmptrap.type = get_traptype_byname(trapstr);
for (i = 0; trap_types[i].name; i++)
if (!strcmpi(trapstr, trap_types[i].name)) {
tmptrap.type = trap_types[i].type;
break;
}
x = y = -1; x = y = -1;
} else if (argc == 2 && lua_type(L, 1) == LUA_TSTRING } else if (argc == 2 && lua_type(L, 1) == LUA_TSTRING
&& lua_type(L, 2) == LUA_TTABLE) { && lua_type(L, 2) == LUA_TTABLE) {
const char *trapstr = luaL_checkstring(L, 1); const char *trapstr = luaL_checkstring(L, 1);
int i;
tmptrap.type = -1; tmptrap.type = get_traptype_byname(trapstr);
for (i = 0; trap_types[i].name; i++)
if (!strcmpi(trapstr, trap_types[i].name)) {
tmptrap.type = trap_types[i].type;
break;
}
get_coord(L, 2, &x, &y); get_coord(L, 2, &x, &y);
} else if (argc == 3) { } else if (argc == 3) {
const char *trapstr = luaL_checkstring(L, 1); const char *trapstr = luaL_checkstring(L, 1);
int i;
tmptrap.type = -1; tmptrap.type = get_traptype_byname(trapstr);
for (i = 0; trap_types[i].name; i++)
if (!strcmpi(trapstr, trap_types[i].name)) {
tmptrap.type = trap_types[i].type;
break;
}
x = luaL_checkinteger(L, 2); x = luaL_checkinteger(L, 2);
y = luaL_checkinteger(L, 3); y = luaL_checkinteger(L, 3);
if (tmptrap.type == -1)
nhl_error(L, "Unknown trap type");
} else { } else {
lcheck_param_table(L); lcheck_param_table(L);
@@ -3420,6 +3407,9 @@ lua_State *L;
} }
} }
if (tmptrap.type == NO_TRAP)
nhl_error(L, "Unknown trap type");
if (x == -1 && y == -1) if (x == -1 && y == -1)
tmptrap.coord = SP_COORD_PACK_RANDOM(0); tmptrap.coord = SP_COORD_PACK_RANDOM(0);
else else
@@ -4328,11 +4318,12 @@ lua_State *L;
{ {
terrain tmpterrain; terrain tmpterrain;
xchar x, y; xchar x, y;
/* char *ter; */ /* REVIEW: unreferenced */
struct selectionvar *sel = NULL; struct selectionvar *sel = NULL;
int argc = lua_gettop(L); int argc = lua_gettop(L);
create_des_coder(); create_des_coder();
tmpterrain.tlit = 0;
tmpterrain.ter = INVALID_TYPE;
if (argc == 1) { if (argc == 1) {
lcheck_param_table(L); lcheck_param_table(L);
@@ -4357,12 +4348,10 @@ lua_State *L;
} else if (argc == 2) { } else if (argc == 2) {
sel = l_selection_check(L, 1); sel = l_selection_check(L, 1);
tmpterrain.ter = check_mapchr(luaL_checkstring(L, 2)); tmpterrain.ter = check_mapchr(luaL_checkstring(L, 2));
tmpterrain.tlit = luaL_optinteger(L, 3, 0); /* FIXME: this can never be here, argc==2 */
} else if (argc == 3) { } else if (argc == 3) {
x = luaL_checkinteger(L, 1); x = luaL_checkinteger(L, 1);
y = luaL_checkinteger(L, 2); y = luaL_checkinteger(L, 2);
tmpterrain.ter = check_mapchr(luaL_checkstring(L, 3)); tmpterrain.ter = check_mapchr(luaL_checkstring(L, 3));
tmpterrain.tlit = 0;
} else { } else {
nhl_error(L, "wrong parameters"); nhl_error(L, "wrong parameters");
} }
@@ -4390,7 +4379,6 @@ lspo_replace_terrain(L)
lua_State *L; lua_State *L;
{ {
replaceterrain rt; replaceterrain rt;
/* char *toter, *fromter; */ /* REVIEW: unreferenced */
xchar totyp, fromtyp; xchar totyp, fromtyp;
create_des_coder(); create_des_coder();
@@ -4571,8 +4559,7 @@ const char *name;
int *x1, *y1, *x2, *y2; int *x1, *y1, *x2, *y2;
boolean optional; boolean optional;
{ {
int arrlen /*, i*/ /* REVIEW: unreferenced */; int arrlen;
/* int retvals[4]; */ /* REVIEW: unreferenced */
lua_getfield(L, 1, name); lua_getfield(L, 1, name);
if (optional && lua_type(L, -1) == LUA_TNIL) { if (optional && lua_type(L, -1) == LUA_TNIL) {
@@ -4904,7 +4891,6 @@ lua_State *L;
const char *const dbopens[] = { "open", "closed", "random", NULL }; const char *const dbopens[] = { "open", "closed", "random", NULL };
const int dbopens2i[] = { 1, 0, -1, -2 }; const int dbopens2i[] = { 1, 0, -1, -2 };
xchar x, y; xchar x, y;
/* int dbopen; */
int mx, my, dir; int mx, my, dir;
int db_open; int db_open;
long dcoord; long dcoord;
@@ -4922,7 +4908,6 @@ lua_State *L;
y = my; y = my;
get_location_coord(&x, &y, DRY | WET | HOT, g.coder->croom, dcoord); get_location_coord(&x, &y, DRY | WET | HOT, g.coder->croom, dcoord);
/* REVIEW: from here down was using dbopen previously */
if (db_open == -1) if (db_open == -1)
db_open = !rn2(2); db_open = !rn2(2);
if (!create_drawbridge(x, y, dir, db_open ? TRUE : FALSE)) if (!create_drawbridge(x, y, dir, db_open ? TRUE : FALSE))
@@ -5088,9 +5073,6 @@ lua_State *L;
int argc = lua_gettop(L); int argc = lua_gettop(L);
boolean freesel = FALSE; boolean freesel = FALSE;
struct selectionvar *sel = (struct selectionvar *) 0; struct selectionvar *sel = (struct selectionvar *) 0;
/* REVIEW: compiler warning,
all assignments conditional
so initializer was added */
create_des_coder(); create_des_coder();
@@ -5123,9 +5105,6 @@ lua_State *L;
int argc = lua_gettop(L); int argc = lua_gettop(L);
boolean freesel = FALSE; boolean freesel = FALSE;
struct selectionvar *sel = (struct selectionvar *) 0; struct selectionvar *sel = (struct selectionvar *) 0;
/* REVIEW: compiler warning,
all assignments conditional
so initializer was added */
create_des_coder(); create_des_coder();
@@ -5192,15 +5171,14 @@ lua_State *L;
/* reset_level is only needed for testing purposes */ /* reset_level is only needed for testing purposes */
int int
lspo_reset_level(L) lspo_reset_level(L)
lua_State *L UNUSED; /* macosx complaint needed UNUSED */ lua_State *L UNUSED;
{ {
boolean wtower = In_W_tower(u.ux, u.uy, &u.uz); boolean wtower = In_W_tower(u.ux, u.uy, &u.uz);
create_des_coder(); create_des_coder();
makemap_prepost(TRUE, wtower); makemap_prepost(TRUE, wtower);
clear_level_structures(); clear_level_structures();
return 0; /* REVIEW: warning, int fn must return value return 0;
so added "return 0; " */
} }
/* map({ x = 10, y = 10, map = [[...]] }); */ /* map({ x = 10, y = 10, map = [[...]] }); */
@@ -5211,9 +5189,7 @@ lspo_map(L)
lua_State *L; lua_State *L;
{ {
mazepart tmpmazepart; mazepart tmpmazepart;
/* xchar halign, valign; */ /* REVIEW: unreferenced */
xchar tmpxstart, tmpystart, tmpxsize, tmpysize; xchar tmpxstart, tmpystart, tmpxsize, tmpysize;
/* unpacked_coord upc; */ /* REVIEW: unreferenced */
/* /*
TODO: allow passing an array of strings as map data TODO: allow passing an array of strings as map data