Lunatic: make con.ai accept literal action/move, check for calling from top.

Also, fix con._setgamepalette.

git-svn-id: https://svn.eduke32.com/eduke32@3541 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-03-03 16:06:37 +00:00
parent 596b3e6d6a
commit 0646e41fa4
4 changed files with 27 additions and 12 deletions

View file

@ -89,5 +89,11 @@ function bcheck.quote_idx(qnum, onlyidx)
return cstr return cstr
end end
function bcheck.top_level(funcname)
if (ffiC.g_elCallDepth > 0) then
error("Invalid use of "..funcname..": must be called from top level", 3)
end
end
return bcheck return bcheck

View file

@ -12,6 +12,7 @@ local bit = require("bit")
local io = require("io") local io = require("io")
local math = require("math") local math = require("math")
local geom = require("geom") local geom = require("geom")
local bcheck = require("bcheck")
local con_lang = require("con_lang") local con_lang = require("con_lang")
local byte = require("string").byte local byte = require("string").byte
@ -93,17 +94,19 @@ end
---=== ACTION / MOVE / AI ===--- ---=== ACTION / MOVE / AI ===---
function action(name, ...) function action(name, ...)
bcheck.top_level("action")
action_or_move("action", 5, def.action, name, ...) action_or_move("action", 5, def.action, name, ...)
end end
function move(name, ...) function move(name, ...)
bcheck.top_level("move")
action_or_move("move", 2, def.move, name, ...) action_or_move("move", 2, def.move, name, ...)
end end
-- Get action or move for an 'ai' definition.
local function get_action_or_move(what, val, argi) local function get_action_or_move(what, val, argi)
if (val == nil) then if (val == nil) then
return {} -- will init the struct to all zeros return {} -- ffi.new will init the struct to all zeros
elseif (type(val)=="string") then elseif (type(val)=="string") then
local am = def[what][val] local am = def[what][val]
if (am==nil) then if (am==nil) then
@ -112,13 +115,20 @@ local function get_action_or_move(what, val, argi)
return am return am
elseif (ffi.istype("con_"..what.."_t", val)) then elseif (ffi.istype("con_"..what.."_t", val)) then
return val return val
elseif (type(val)=="number") then
if (val==0 or val==1) then
-- Create an action or move with an ID of 0 or 1 but all other
-- fields cleared.
return ffi.new("con_"..what.."_t", val)
end
end end
-- TODO: literal number actions/moves? error("bad argument #"..argi.." to ai: must be string or (literal) "..what, 3)
error("bad argument #"..argi.." to ai: must be string or "..what, 3)
end end
function ai(name, action, move, flags) function ai(name, action, move, flags)
bcheck.top_level("ai")
if (lastid.ai <= -(2^31)) then if (lastid.ai <= -(2^31)) then
error("Too many AIs defined", 2); error("Too many AIs defined", 2);
end end
@ -143,7 +153,6 @@ end
---=== RUNTIME CON FUNCTIONS ===--- ---=== RUNTIME CON FUNCTIONS ===---
local bcheck = require("bcheck")
local check_sector_idx = bcheck.sector_idx local check_sector_idx = bcheck.sector_idx
local check_tile_idx = bcheck.tile_idx local check_tile_idx = bcheck.tile_idx
local check_sprite_idx = bcheck.sprite_idx local check_sprite_idx = bcheck.sprite_idx
@ -1584,7 +1593,7 @@ function _setaspect(viewingrange, yxaspect)
end end
function _setgamepalette(pli, basepal) function _setgamepalette(pli, basepal)
ffiC.P_SetGamePalette(player[pli], basepal) ffiC.P_SetGamePalette(player[pli], basepal, 2+16)
end end
-- Gamevar persistence in the configuration file -- Gamevar persistence in the configuration file

View file

@ -1360,9 +1360,7 @@ G_._G = G_
local gameactor_internal = gameactor_internal -- included in lunatic.c local gameactor_internal = gameactor_internal -- included in lunatic.c
-- gameactor(tilenum [, strength [, act [, mov [, movflags]]]], actor_func) -- gameactor(tilenum [, strength [, act [, mov [, movflags]]]], actor_func)
local function our_gameactor(tilenum, ...) local function our_gameactor(tilenum, ...)
if (ffiC.g_elCallDepth > 0) then bcheck.top_level("gameactor")
error("Invalid use of gameactor: must be called from top level", 2)
end
local args = {...} local args = {...}
if (type(tilenum) ~= "number") then if (type(tilenum) ~= "number") then

View file

@ -308,7 +308,8 @@ function on.actor_end(usertype, tsamm, codetab)
local str = "" local str = ""
for i=2,math.min(#tsamm,4) do for i=2,math.min(#tsamm,4) do
if ((i==3 or i==4) and tsamm[i]=="0") then if ((i==3 or i==4) and tsamm[i]=="0") then
-- HACK, gameactor() currently doesn't support literals -- HACK, gameactor() currently doesn't support literals for actions
-- and moves.
tsamm[i] = "'NO'" tsamm[i] = "'NO'"
end end
str = str .. tostring(tsamm[i]).."," str = str .. tostring(tsamm[i])..","
@ -603,11 +604,12 @@ local function do_define_composite(labeltype, identifier, ...)
return return
end end
-- Fill up omitted arguments with zeros. -- Fill up omitted arguments denoting composites with zeros.
local isai = (labeltype == LABEL.AI) local isai = (labeltype == LABEL.AI)
local args = {...} local args = {...}
for i=#args+1,labeltype do for i=#args+1,labeltype do
-- passing nil to con.ai will make the action/move the null one -- Passing nil/nothing as remaining args to con.ai will make the
-- action/move the null one.
args[i] = (isai and i<=2) and "nil" or 0 args[i] = (isai and i<=2) and "nil" or 0
end end