From 8293e72a95d0bbf887963456e77efbd0d6f8cd00 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 25 Nov 2012 13:18:46 +0000 Subject: [PATCH] Lunatic translator: rework how composites are passed around. git-svn-id: https://svn.eduke32.com/eduke32@3226 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/control.lua | 17 +- polymer/eduke32/source/lunatic/defs.ilua | 21 +++ polymer/eduke32/source/lunatic/lunacon.lua | 197 +++++++++++---------- polymer/eduke32/source/lunatic/test.elua | 11 +- 4 files changed, 147 insertions(+), 99 deletions(-) diff --git a/polymer/eduke32/source/lunatic/control.lua b/polymer/eduke32/source/lunatic/control.lua index b2db38d7b..95cb278e8 100644 --- a/polymer/eduke32/source/lunatic/control.lua +++ b/polymer/eduke32/source/lunatic/control.lua @@ -7,7 +7,6 @@ local bit = require("bit") local setmetatable = setmetatable -local assert = assert local error = error local type = type @@ -31,11 +30,15 @@ local function check_name(name, what, errlev) end local function action_or_move(what, numargs, tab, name, ...) - assert(lastid[what] > -(2^31)) + if (lastid[what] <= -(2^31)) then + error("Too many "..what.."s defined", 3); + end check_name(name, what, 3) local args = {...} - assert(#args <= numargs) + if (#args > numargs) then + error("Too many arguments passed to "..what, 3) + end for i=1,#args do local n = args[i] @@ -79,11 +82,13 @@ local function get_action_or_move(what, val, argi) end -- TODO: literal number actions/moves? - error("bad argument #"..argi.." to ai: must be string or "..what) + error("bad argument #"..argi.." to ai: must be string or "..what, 3) end function ai(name, action, move, flags) - assert(lastid.ai > -(2^31)) + if (lastid.ai <= -(2^31)) then + error("Too many AIs defined", 2); + end check_name(name, "ai", 2) lastid.ai = lastid.ai-1 @@ -108,7 +113,7 @@ end function rotatesprite(x, y, zoom, ang, tilenum, shade, pal, orientation, cx1, cy1, cx2, cy2) if (type(tilenum) ~= "number" or not (tilenum >= 0 and tilenum < ffiC.MAXTILES)) then - error("bad argument #5 to rotatesprite: must be number in [0.."..ffiC.MAXTILES.."]") + error("bad argument #5 to rotatesprite: must be number in [0.."..ffiC.MAXTILES.."]", 2) end ffiC.rotatesprite(65536*x, 65536*y, zoom, ang, tilenum, shade, pal, bit.bor(2,orientation), diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 2ee6ddd66..4d19583c4 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -450,6 +450,7 @@ end -- declares struct action and struct move, and their ID-wrapped types -- con_action_t and con_move_t local con = require("control") +local MV, AC, AI = con.MV, con.AC, con.AI -- All-zero action and move local nullac, nullmv = ffi.new("const struct action"), ffi.new("const struct move") @@ -474,6 +475,11 @@ local actor_mt = { -- action set_action = function(a, act) a = ffi.cast(actor_ptr_ct, a) + if (type(act)=="string") then + act = AC[act]; + end + -- TODO: disallow passing the FFI types altogether, in favor of + -- strings (also move, ai)? if (ffi.istype(con_action_ct, act)) then a.t_data[4] = act.id a.ac = act.ac @@ -489,6 +495,9 @@ local actor_mt = { has_action = function(a, act) a = ffi.cast(actor_ptr_ct, a) + if (type(act)=="string") then + act = AC[act]; + end if (ffi.istype(con_action_ct, act)) then return (a.t_data[4]==act.id) else @@ -518,6 +527,9 @@ local actor_mt = { -- move set_move = function(a, mov, movflags) a = ffi.cast(actor_ptr_ct, a) + if (type(mov)=="string") then + mov = MV[mov]; + end if (ffi.istype(con_move_ct, mov)) then a.t_data[1] = mov.id a.mv = mov.mv @@ -536,6 +548,9 @@ local actor_mt = { has_move = function(a, mov) a = ffi.cast(actor_ptr_ct, a) + if (type(mov)=="string") then + mov = MV[mov]; + end if (ffi.istype(con_move_ct, mov)) then return (a.t_data[1]==mov.id) else @@ -549,6 +564,9 @@ local actor_mt = { local oa = a a = ffi.cast(actor_ptr_ct, a) + if (type(mov)=="string") then + ai = AI[ai]; + end -- TODO: literal number AIs? if (not ffi.istype(con_ai_ct, ai)) then error("bad argument: expected ai", 2) @@ -566,6 +584,9 @@ local actor_mt = { has_ai = function(a, ai) a = ffi.cast(actor_ptr_ct, a) + if (type(mov)=="string") then + ai = AI[ai]; + end if (ffi.istype(con_ai_ct, ai)) then return (a.t_data[5]==ai.id) else diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index 6d3961e43..67e8ef87e 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -65,7 +65,8 @@ local g_loadactor_code = {} -- [actornum]=gencode_table local function getlinecol(pos) end -- fwd-decl local function reset_codegen() - g_file_code, g_curcode = {}, nil + g_file_code = {} + g_curcode = nil g_actor_code, g_event_code, g_loadactor_code = {}, {}, {} end @@ -82,7 +83,7 @@ local function on_actor_end(usertype, tsamm, codetab) local tilenum = tsamm[1] -- usertype is non-nil only for 'useractor' - addcodef("gameactor(%d,\nfunction(actori, playeri, dist)", tilenum) + addcodef("gameactor(%d, function(_aci, _pli, _dist)", tilenum) g_actor_code[tilenum] = codetab addcode(codetab) @@ -136,35 +137,37 @@ local function parse_number(pos, numstr) end -local LABEL = { MOVE=2, AI=3, ACTION=5, [2]="move", [3]="ai", [5]="action" } +-- Mapping of various "define" types to the respective number of members and +-- vice versa +local LABEL = { MOVE=2, AI=3, ACTION=5, [2]="move", [3]="ai", [5]="action", + NUMBER=1, [1]="number" } -local MOVE_NO = {0,0} -local ACTION_NO = {0,0,0,0,0} -local LABEL_NO = { [2]=MOVE_NO, [3]={ACTION_NO, MOVE_NO, 0}, [5]=ACTION_NO } +-- Table names in the 'con' module +--local LABEL_TABNAME = { [2]="MV", [3]="AI", [5]="AC" } +-- Function names in the 'con' module +local LABEL_FUNCNAME = { [2]="move", [3]="ai", [5]="action" } --- will contain: --- * scalar numbers: `define'd values --- * tables of length 2, 3, 5: move, ai, action definitions (respectively) --- - move: { hvel , vvel } --- - ai: { action