From: Pasi Kallinen Date: Sun, 9 Feb 2020 11:44:04 +0000 (+0200) Subject: Unify some exported lua script params X-Git-Tag: NetHack-3.7.0_WIP-2020-02-14~34 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ced327fdf5cfb63f7c12d3a3085f753c53541ca7;p=nethack Unify some exported lua script params --- diff --git a/src/nhlsel.c b/src/nhlsel.c index 6fa291d17..fdca6ae5d 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -13,6 +13,8 @@ static int FDECL(l_selection_setpoint, (lua_State *)); static int FDECL(l_selection_not, (lua_State *)); static int FDECL(l_selection_filter_percent, (lua_State *)); static int FDECL(l_selection_rndcoord, (lua_State *)); +static boolean FDECL(params_sel_2coords, (lua_State *, struct selectionvar *, + schar *, schar *, schar *, schar *)); static int FDECL(l_selection_line, (lua_State *)); static int FDECL(l_selection_randline, (lua_State *)); static int FDECL(l_selection_rect, (lua_State *)); @@ -312,6 +314,39 @@ lua_State *L; return 2; } +/* function(selection, x1,y1, x2,y2) */ +/* selection:function(x1,y1, x2,y2) */ +static boolean +params_sel_2coords(L, sel, x1,y1, x2,y2) +lua_State *L; +struct selectionvar *sel; +schar *x1, *y1, *x2, *y2; +{ + int argc = lua_gettop(L); + + if (argc == 4) { + (void) l_selection_new(L); + *x1 = (schar) luaL_checkinteger(L, 1); + *y1 = (schar) luaL_checkinteger(L, 2); + *x2 = (schar) luaL_checkinteger(L, 3); + *y2 = (schar) luaL_checkinteger(L, 4); + sel = l_selection_check(L, 5); + lua_remove(L, 1); + lua_remove(L, 1); + lua_remove(L, 1); + lua_remove(L, 1); + return TRUE; + } else if (argc == 5) { + sel = l_selection_check(L, 1); + *x1 = (schar) luaL_checkinteger(L, 2); + *y1 = (schar) luaL_checkinteger(L, 3); + *x2 = (schar) luaL_checkinteger(L, 4); + *y2 = (schar) luaL_checkinteger(L, 5); + return TRUE; + } + return FALSE; +} + /* local s = selection.line(sel, x1,y1, x2,y2); */ /* local s = selection.line(x1,y1, x2,y2); */ /* s:line(x1,y1, x2,y2); */ @@ -319,30 +354,14 @@ static int l_selection_line(L) lua_State *L; { - int argc = lua_gettop(L); struct selectionvar *sel; schar x1; schar y1; schar x2; schar y2; - if (argc == 4) { - (void) l_selection_new(L); - x1 = (schar) luaL_checkinteger(L, 1); - y1 = (schar) luaL_checkinteger(L, 2); - x2 = (schar) luaL_checkinteger(L, 3); - y2 = (schar) luaL_checkinteger(L, 4); - sel = l_selection_check(L, 5); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - } else { - sel = l_selection_check(L, 1); - x1 = (schar) luaL_checkinteger(L, 2); - y1 = (schar) luaL_checkinteger(L, 3); - x2 = (schar) luaL_checkinteger(L, 4); - y2 = (schar) luaL_checkinteger(L, 5); + if (!params_sel_2coords(L, sel, &x1, &y1, &x2, &y2)) { + nhl_error(L, "selection.line: illegal arguments"); } get_location_coord(&x1, &y1, ANY_LOC, g.coder ? g.coder->croom : NULL, SP_COORD_PACK(x1,y1)); @@ -358,30 +377,14 @@ static int l_selection_rect(L) lua_State *L; { - int argc = lua_gettop(L); struct selectionvar *sel; schar x1; schar y1; schar x2; schar y2; - if (argc == 4) { - (void) l_selection_new(L); - x1 = (schar) luaL_checkinteger(L, 1); - y1 = (schar) luaL_checkinteger(L, 2); - x2 = (schar) luaL_checkinteger(L, 3); - y2 = (schar) luaL_checkinteger(L, 4); - sel = l_selection_check(L, 5); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - } else { - sel = l_selection_check(L, 1); - x1 = (schar) luaL_checkinteger(L, 2); - y1 = (schar) luaL_checkinteger(L, 3); - x2 = (schar) luaL_checkinteger(L, 4); - y2 = (schar) luaL_checkinteger(L, 5); + if (!params_sel_2coords(L, sel, &x1, &y1, &x2, &y2)) { + nhl_error(L, "selection.rect: illegal arguments"); } get_location_coord(&x1, &y1, ANY_LOC, g.coder ? g.coder->croom : NULL, SP_COORD_PACK(x1,y1)); @@ -411,23 +414,8 @@ lua_State *L; schar x2; schar y2; - if (argc == 4) { - (void) l_selection_new(L); - x1 = (schar) luaL_checkinteger(L, 1); - y1 = (schar) luaL_checkinteger(L, 2); - x2 = (schar) luaL_checkinteger(L, 3); - y2 = (schar) luaL_checkinteger(L, 4); - sel = l_selection_check(L, 5); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - lua_remove(L, 1); - } else { - sel = l_selection_check(L, 1); - x1 = (schar) luaL_checkinteger(L, 2); - y1 = (schar) luaL_checkinteger(L, 3); - x2 = (schar) luaL_checkinteger(L, 4); - y2 = (schar) luaL_checkinteger(L, 5); + if (!params_sel_2coords(L, sel, &x1, &y1, &x2, &y2)) { + nhl_error(L, "selection.fillrect: illegal arguments"); } get_location_coord(&x1, &y1, ANY_LOC, g.coder ? g.coder->croom : NULL, SP_COORD_PACK(x1,y1));