Lunatic: back up generated LunaCON code, recreate Lua state on map entering.

git-svn-id: https://svn.eduke32.com/eduke32@3787 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-05-19 19:29:13 +00:00
parent e8ff49b6ef
commit 9d858b9883
7 changed files with 76 additions and 25 deletions

View file

@ -120,7 +120,7 @@ int L_CreateState(L_State *estate, const char *name, void (*StateSetupFunc)(lua_
void L_DestroyState(L_State *estate)
{
if (!estate->L)
if (!L_IsInitialized(estate))
return;
Bfree(estate->name);

View file

@ -9836,6 +9836,50 @@ static void A_InitEnemyFlags(void)
extern int32_t startwin_run(void);
static void G_SetupGameButtons(void);
#ifdef LUNATIC
// Will be used to store CON code translated to Lua.
int32_t g_elCONSize;
char *g_elCON; // NOT 0-terminated!
LUNATIC_EXTERN void El_SetCON(const char *conluacode)
{
int32_t slen = Bstrlen(conluacode);
g_elCON = Bmalloc(slen);
if (g_elCON == NULL)
G_GameExit("OUT OF MEMORY in El_SetCON!");
g_elCONSize = slen;
Bmemcpy(g_elCON, conluacode, slen);
}
void El_CreateGameState(void)
{
int32_t i;
El_DestroyState(&g_ElState);
if ((i = El_CreateState(&g_ElState, "game")))
{
initprintf("Lunatic: Error initializing global ELua state (code %d)\n", i);
}
else
{
extern const char luaJIT_BC_defs[];
if ((i = L_RunString(&g_ElState, (char *)luaJIT_BC_defs, 0,
LUNATIC_DEFS_BC_SIZE, "defs.ilua")))
{
initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i);
El_DestroyState(&g_ElState);
}
}
if (i)
G_GameExit("Failure setting up Lunatic!");
}
#endif
static void G_Startup(void)
{
int32_t i;
@ -9858,24 +9902,7 @@ static void G_Startup(void)
setbasepaltable(basepaltable, BASEPALCOUNT);
#ifdef LUNATIC
if ((i = El_CreateState(&g_ElState, "test")))
{
initprintf("Lunatic: Error initializing global ELua state (code %d)\n", i);
}
else
{
extern const char luaJIT_BC_defs[];
if ((i = L_RunString(&g_ElState, (char *)luaJIT_BC_defs, 0,
LUNATIC_DEFS_BC_SIZE, "defs.ilua")))
{
initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i);
El_DestroyState(&g_ElState);
}
}
if (i)
G_GameExit("Failure setting up Lunatic!");
El_CreateGameState();
C_InitQuotes();
#endif

View file

@ -296,6 +296,10 @@ void Yax_SetBunchZs(int32_t sectnum, int32_t cf, int32_t daz);
#define Yax_SetBunchZs(sectnum, cf, daz)
#endif
#ifdef LUNATIC
void El_CreateGameState(void);
#endif
void A_SpawnCeilingGlass(int32_t i,int32_t sectnum,int32_t n);
void A_SpawnGlass(int32_t i,int32_t n);
void A_SpawnRandomGlass(int32_t i,int32_t wallnum,int32_t n);

View file

@ -566,6 +566,10 @@ int32_t g_RETURN; // deprecated from Lua
decl("map_t MapInfo[$*$];", con_lang.MAXVOLUMES+1, con_lang.MAXLEVELS)
decl[[
int32_t g_elCONSize;
char *g_elCON;
void El_SetCON(const char *conluacode);
const char *s_buildRev;
const char *g_sizes_of_what[];
int32_t g_sizes_of[];
@ -1660,8 +1664,9 @@ G_.g_tile = g_tile
local concode
--- Compile CONs
do
--- Get Lua code for CON (+ mutator) code.
if (ffiC.g_elCONSize == 0) then
-- Compiling CON for the first time.
read_into_string = readintostr_mod -- for lunacon
local lunacon = require("lunacon")
@ -1684,6 +1689,15 @@ do
end
assert(lineinfo)
-- Back up the translated code on the C side.
assert(type(concode)=="string")
ffiC.El_SetCON(concode)
else
-- CON was already compiled.
concode = ffi.string(ffiC.g_elCON, ffiC.g_elCONSize)
end
do
-- Translate one Lua line number to a CON file name + line number
local function transline(lnum)
return string.format("%s:%d", lineinfo:getfline(tonumber(lnum)))

View file

@ -86,6 +86,10 @@ luaJIT_BC_engine_maptext;
g_elCONSize;
g_elCON;
El_SetCON;
s_buildRev;
g_sizes_of_what;
g_sizes_of;

View file

@ -52,12 +52,12 @@ local mt = {
local tout = ffi.new("uint32_t [4]")
repeat
s.y = get_rand_u32()
s.y = get_rand_u32(tin)
until (s.y ~= 0) -- y must not be zero!
s.x = get_rand_u32()
s.z = get_rand_u32()
s.c = get_rand_u32() % 698769068 + 1 -- Should be less than 698769069
s.x = get_rand_u32(tin)
s.z = get_rand_u32(tin)
s.c = get_rand_u32(tin) % 698769068 + 1 -- Should be less than 698769069
end,
},
}

View file

@ -1120,6 +1120,8 @@ static inline void prelevel(char g)
}
#ifdef LUNATIC
El_CreateGameState();
if (g_testLua)
{
if (L_IsInitialized(&g_ElState))