From 80ee967b8e62b5390485d19fbda703eb091a754e Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sat, 29 Dec 2012 15:21:24 +0000 Subject: [PATCH] Lunatic: beginning to glue things together. Introducing LUNATIC_ONLY. git-svn-id: https://svn.eduke32.com/eduke32@3343 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/Makefile.common | 2 +- polymer/eduke32/source/game.c | 23 ++-- polymer/eduke32/source/gamedef.c | 95 ++++++++------- polymer/eduke32/source/gamevars.c | 38 ++++-- polymer/eduke32/source/gamevars.h | 10 +- polymer/eduke32/source/lunatic/defs.ilua | 46 +++++-- polymer/eduke32/source/lunatic/dynsymlist | 4 + polymer/eduke32/source/lunatic/lunacon.lua | 132 ++++++++++++++------- polymer/eduke32/source/lunatic/test.elua | 3 + 9 files changed, 233 insertions(+), 120 deletions(-) diff --git a/polymer/eduke32/Makefile.common b/polymer/eduke32/Makefile.common index 4917e0cb9..c5b64fccd 100644 --- a/polymer/eduke32/Makefile.common +++ b/polymer/eduke32/Makefile.common @@ -421,7 +421,7 @@ ifneq ($(LUNATIC),0) else BASECOMMONFLAGS+= -I/usr/local/include/luajit-2.0 endif - BASECOMMONFLAGS+= -I$(SRC)/lunatic -DLUNATIC + BASECOMMONFLAGS+= -I$(SRC)/lunatic -DLUNATIC -DLUNATIC_ONLY ifeq ($(PLATFORM),WINDOWS) BASELIBS+= -lluajit diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 2efa125e6..953471d13 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -9488,7 +9488,7 @@ static void G_Cleanup(void) if (g_sounds[i].filename != NULL) Bfree(g_sounds[i].filename); if (g_sounds[i].filename1 != NULL) Bfree(g_sounds[i].filename1); } - +#if !defined LUNATIC_ONLY if (label != NULL && label != (char *)&sprite[0]) Bfree(label); if (labelcode != NULL && labelcode != (int32_t *)§or[0]) Bfree(labelcode); if (script != NULL) Bfree(script); @@ -9500,6 +9500,7 @@ static void G_Cleanup(void) hash_free(&h_arrays); hash_free(&h_labels); hash_free(&h_gamefuncs); +#endif } /* @@ -9532,15 +9533,18 @@ void G_Shutdown(void) static void G_CompileScripts(void) { +#if !defined LUNATIC_ONLY int32_t psm = pathsearchmode; label = (char *)&sprite[0]; // V8: 16384*44/64 = 11264 V7: 4096*44/64 = 2816 labelcode = (int32_t *)§or[0]; // V8: 4096*40/4 = 40960 V7: 1024*40/4 = 10240 labeltype = (int32_t *)&wall[0]; // V8: 16384*32/4 = 131072 V7: 8192*32/4 = 65536 +#endif if (g_scriptNamePtr != NULL) Bcorrectfilename(g_scriptNamePtr,0); +#if !defined LUNATIC_ONLY // if we compile for a V7 engine wall[] should be used for label names since it's bigger pathsearchmode = 1; @@ -9575,6 +9579,7 @@ static void G_CompileScripts(void) VM_OnEvent(EVENT_INIT, -1, -1, -1, 0); pathsearchmode = psm; +#endif } static inline void G_CheckGametype(void) @@ -10530,20 +10535,18 @@ int32_t app_main(int32_t argc, const char **argv) OSD_Exec("autoexec.cfg"); #ifdef LUNATIC - i = El_CreateState(&g_ElState, "test"); - if (i) + if ((i = El_CreateState(&g_ElState, "test"))) { initprintf("Lunatic: Error initializing global ELua state (code %d)\n", i); } - else + else if ((i = L_RunOnce(&g_ElState, "defs.ilua"))) { - i = L_RunOnce(&g_ElState, "defs.ilua"); - if (i) - { - initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i); - El_DestroyState(&g_ElState); - } + initprintf("Lunatic: Error preparing global ELua state (code %d)\n", i); + El_DestroyState(&g_ElState); } + + if (i) + G_GameExit("Failure setting up Lunatic!"); #endif if (g_networkMode != NET_DEDICATED_SERVER) diff --git a/polymer/eduke32/source/gamedef.c b/polymer/eduke32/source/gamedef.c index 2734f0a8b..d4908cb7b 100644 --- a/polymer/eduke32/source/gamedef.c +++ b/polymer/eduke32/source/gamedef.c @@ -2057,6 +2057,47 @@ static void check_filename_case(const char *fn) static void check_filename_case(const char *fn) { UNREFERENCED_PARAMETER(fn); } #endif +void G_DoGameStartup(const int32_t *params) +{ + int32_t j = 0; + + ud.const_visibility = params[j++]; + g_impactDamage = params[j++]; + g_maxPlayerHealth = g_player[0].ps->max_player_health = g_player[0].ps->max_shield_amount = params[j++]; + g_startArmorAmount = params[j++]; + g_actorRespawnTime = params[j++]; + g_itemRespawnTime = params[j++]; + g_playerFriction = params[j++]; + if (g_scriptVersion == 14) g_spriteGravity = params[j++]; + g_rpgBlastRadius = params[j++]; + g_pipebombBlastRadius = params[j++]; + g_shrinkerBlastRadius = params[j++]; + g_tripbombBlastRadius = params[j++]; + g_morterBlastRadius = params[j++]; + g_bouncemineBlastRadius = params[j++]; + g_seenineBlastRadius = params[j++]; + g_player[0].ps->max_ammo_amount[PISTOL_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[SHOTGUN_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[CHAINGUN_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[RPG_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[HANDBOMB_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[SHRINKER_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[DEVISTATOR_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[TRIPBOMB_WEAPON] = params[j++]; + g_player[0].ps->max_ammo_amount[FREEZE_WEAPON] = params[j++]; + if (g_scriptVersion == 14) g_player[0].ps->max_ammo_amount[GROW_WEAPON] = params[j++]; + g_damageCameras = params[j++]; + g_numFreezeBounces = params[j++]; + g_freezerSelfDamage = params[j++]; + if (g_scriptVersion == 14) + { + g_spriteDeleteQueueSize = params[j++]; + g_spriteDeleteQueueSize = clamp(g_spriteDeleteQueueSize, 0, 1024); + + g_tripbombLaserMode = params[j++]; + } +} + static int32_t C_ParseCommand(int32_t loop) { int32_t i, j=0, k=0, tw, otw; @@ -5678,42 +5719,7 @@ repeatcase: TRIPBOMBLASERMODE */ - j = 0; - ud.const_visibility = params[j++]; - g_impactDamage = params[j++]; - g_maxPlayerHealth = g_player[0].ps->max_player_health = g_player[0].ps->max_shield_amount = params[j++]; - g_startArmorAmount = params[j++]; - g_actorRespawnTime = params[j++]; - g_itemRespawnTime = params[j++]; - g_playerFriction = params[j++]; - if (g_scriptVersion == 14) g_spriteGravity = params[j++]; - g_rpgBlastRadius = params[j++]; - g_pipebombBlastRadius = params[j++]; - g_shrinkerBlastRadius = params[j++]; - g_tripbombBlastRadius = params[j++]; - g_morterBlastRadius = params[j++]; - g_bouncemineBlastRadius = params[j++]; - g_seenineBlastRadius = params[j++]; - g_player[0].ps->max_ammo_amount[PISTOL_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[SHOTGUN_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[CHAINGUN_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[RPG_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[HANDBOMB_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[SHRINKER_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[DEVISTATOR_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[TRIPBOMB_WEAPON] = params[j++]; - g_player[0].ps->max_ammo_amount[FREEZE_WEAPON] = params[j++]; - if (g_scriptVersion == 14) g_player[0].ps->max_ammo_amount[GROW_WEAPON] = params[j++]; - g_damageCameras = params[j++]; - g_numFreezeBounces = params[j++]; - g_freezerSelfDamage = params[j++]; - if (g_scriptVersion == 14) - { - g_spriteDeleteQueueSize = params[j++]; - g_spriteDeleteQueueSize = clamp(g_spriteDeleteQueueSize, 0, 1024); - - g_tripbombLaserMode = params[j++]; - } + G_DoGameStartup(params); } continue; } @@ -5723,8 +5729,8 @@ repeatcase: return 0; } +#if !defined LUNATIC_ONLY /* Anything added with C_AddDefinition() cannot be overwritten in the CONs */ - static void C_AddDefinition(const char *lLabel,int32_t lValue,int32_t lType) { Bstrcpy(label+(g_numLabels<<6),lLabel); @@ -5863,12 +5869,14 @@ static void C_InitProjectiles(void) } } #pragma pack(pop) +#endif extern int32_t g_numObituaries; extern int32_t g_numSelfObituaries; void C_Compile(const char *filenam) { +#if !defined LUNATIC_ONLY char *mptr; int32_t i; int32_t fs,fp; @@ -5880,7 +5888,9 @@ void C_Compile(const char *filenam) Bmemset(&g_tile[i], 0, sizeof(tiledata_t)); C_InitHashes(); +#endif Gv_Init(); +#if !defined LUNATIC_ONLY C_InitProjectiles(); fp = kopen4loadfrommod((char *)filenam,g_loadFromGroupOnly); @@ -5890,7 +5900,7 @@ void C_Compile(const char *filenam) if (g_loadFromGroupOnly == 1 || numgroupfiles == 0) { -#ifdef WIN32 +# ifdef WIN32 Bsprintf(tempbuf,"Duke Nukem 3D game data was not found. A valid copy of \"%s\" or other compatible data is needed to run EDuke32.\n\n" "You can find \"%s\" in the \"DN3DINST\" or \"ATOMINST\" directory on your Duke Nukem 3D installation CD-ROM.\n\n" "If you don't already own a copy of Duke or haven't seen your disc in years, don't worry -- you can download the full, registered " @@ -5916,13 +5926,13 @@ void C_Compile(const char *filenam) G_GameExit("Error launching default system browser!"); } G_GameExit(""); -#else +# else Bsprintf(tempbuf,"Duke Nukem 3D game data was not found. A valid copy of \"%s\" or other compatible data is needed to run EDuke32.\n" "You can find \"%s\" in the \"DN3DINST\" or \"ATOMINST\" directory on your Duke Nukem 3D installation CD-ROM.\n\n" "EDuke32 will now close.", G_GrpFile(),G_GrpFile()); G_GameExit(tempbuf); -#endif +# endif } else { @@ -6020,10 +6030,10 @@ void C_Compile(const char *filenam) } else { -#if (defined _WIN32 || (defined RENDERTYPESDL && ((defined __APPLE__ && defined OSX_STARTUPWINDOW) || defined HAVE_GTK2))) +# if (defined _WIN32 || (defined RENDERTYPESDL && ((defined __APPLE__ && defined OSX_STARTUPWINDOW) || defined HAVE_GTK2))) while (!quitevent) // keep the window open so people can copy CON errors out of it handleevents(); -#endif +# endif G_GameExit(""); } } @@ -6175,6 +6185,7 @@ void C_Compile(const char *filenam) } } } +#endif // !defined LUNATIC_ONLY } void C_ReportError(int32_t iError) diff --git a/polymer/eduke32/source/gamevars.c b/polymer/eduke32/source/gamevars.c index 492817d57..cb8ca0b71 100644 --- a/polymer/eduke32/source/gamevars.c +++ b/polymer/eduke32/source/gamevars.c @@ -66,6 +66,7 @@ static void Gv_Free(void) /* called from Gv_ReadSave() and Gv_ResetVars() */ return; } +#if !defined LUNATIC_ONLY static void Gv_Clear(void) { // only call this function ONCE... @@ -104,6 +105,7 @@ static void Gv_Clear(void) hash_init(&h_arrays); return; } +#endif int32_t Gv_ReadSave(int32_t fil, int32_t newbehav) { @@ -558,12 +560,13 @@ void __fastcall A_ResetVars(register int32_t iActor) while (i--); } +#if !defined LUNATIC_ONLY static int32_t Gv_GetVarIndex(const char *szGameLabel) { int32_t i = hash_find(&h_gamevars,szGameLabel); if (i == -1) { - OSD_Printf(OSD_ERROR "Gv_GetVarDataPtr(): INTERNAL ERROR: couldn't find gamevar %s!\n",szGameLabel); + OSD_Printf(OSD_ERROR "Gv_GetVarIndex(): INTERNAL ERROR: couldn't find gamevar %s!\n",szGameLabel); return 0; } return i; @@ -762,6 +765,7 @@ badindex: aGameVars[id].szLabel,vm.g_i,vm.g_p); return; } +#endif int32_t __fastcall Gv_GetVarX(register int32_t id) { @@ -993,6 +997,13 @@ static intptr_t *Gv_GetVarDataPtr(const char *szGameLabel) } #endif +static void G_InitProjectileData(void) +{ + int32_t i; + for (i=MAXTILES-1; i>=0; i--) + Bmemcpy(&ProjectileData[i], &g_tile[i].defproj, sizeof(projectile_t)); +} + void Gv_ResetSystemDefaults(void) { // call many times... @@ -1051,6 +1062,7 @@ void Gv_ResetSystemDefaults(void) } } #endif +#if !defined LUNATIC_ONLY g_iReturnVarID=Gv_GetVarIndex("RETURN"); g_iWeaponVarID=Gv_GetVarIndex("WEAPON"); g_iWorksLikeVarID=Gv_GetVarIndex("WORKSLIKE"); @@ -1067,9 +1079,8 @@ void Gv_ResetSystemDefaults(void) g_iWallVarID=Gv_GetVarIndex("wall"); g_iPlayerVarID=Gv_GetVarIndex("player"); g_iActorVarID=Gv_GetVarIndex("actorvar"); - - for (i=MAXTILES-1; i>=0; i--) - Bmemcpy(&ProjectileData[i], &g_tile[i].defproj, sizeof(projectile_t)); +#endif + G_InitProjectileData(); //AddLog("EOF:ResetWeaponDefaults"); } @@ -1252,7 +1263,7 @@ static void Gv_AddSystemVars(void) ADDWEAPONVAR(i, SelectSound); ADDWEAPONVAR(i, FlashColor); } - +#if !defined LUNATIC_ONLY Gv_NewVar("GRENADE_LIFETIME", NAM_GRENADE_LIFETIME, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM); Gv_NewVar("GRENADE_LIFETIME_VAR", NAM_GRENADE_LIFETIME_VAR, GAMEVAR_PERPLAYER | GAMEVAR_SYSTEM); @@ -1362,14 +1373,15 @@ static void Gv_AddSystemVars(void) Gv_NewVar("Numsprites",(intptr_t)&Numsprites, GAMEVAR_SYSTEM | GAMEVAR_INTPTR | GAMEVAR_READONLY); Gv_NewVar("lastsavepos",(intptr_t)&g_lastSaveSlot, GAMEVAR_SYSTEM | GAMEVAR_INTPTR); -#ifdef USE_OPENGL +# ifdef USE_OPENGL Gv_NewVar("rendmode",(intptr_t)&rendmode, GAMEVAR_READONLY | GAMEVAR_INTPTR | GAMEVAR_SYSTEM); -#else +# else Gv_NewVar("rendmode", 0, GAMEVAR_READONLY | GAMEVAR_SYSTEM); -#endif +# endif Gv_NewArray("tilesizx", (void *)tilesizx, MAXTILES, GAMEARRAY_READONLY|GAMEARRAY_OFSHORT); Gv_NewArray("tilesizy", (void *)tilesizy, MAXTILES, GAMEARRAY_READONLY|GAMEARRAY_OFSHORT); +#endif } void Gv_Init(void) @@ -1384,11 +1396,15 @@ void Gv_Init(void) // initprintf("Initializing game variables\n"); //AddLog("Gv_Init"); - +#ifdef LUNATIC_ONLY + Gv_AddSystemVars(); // set up weapon defaults, g_playerWeapon[][] + Gv_ResetSystemDefaults(); +#else Gv_Clear(); Gv_AddSystemVars(); Gv_InitWeaponPointers(); Gv_ResetSystemDefaults(); +#endif } void Gv_InitWeaponPointers(void) @@ -1452,6 +1468,7 @@ void Gv_InitWeaponPointers(void) void Gv_RefreshPointers(void) { +#if !defined LUNATIC_ONLY aGameVars[Gv_GetVarIndex("RESPAWN_MONSTERS")].val.lValue = (intptr_t)&ud.respawn_monsters; aGameVars[Gv_GetVarIndex("RESPAWN_ITEMS")].val.lValue = (intptr_t)&ud.respawn_items; aGameVars[Gv_GetVarIndex("RESPAWN_INVENTORY")].val.lValue = (intptr_t)&ud.respawn_inventory; @@ -1531,7 +1548,8 @@ void Gv_RefreshPointers(void) aGameVars[Gv_GetVarIndex("Numsprites")].val.lValue = (intptr_t)&Numsprites; aGameVars[Gv_GetVarIndex("lastsavepos")].val.lValue = (intptr_t)&g_lastSaveSlot; -#ifdef USE_OPENGL +# ifdef USE_OPENGL aGameVars[Gv_GetVarIndex("rendmode")].val.lValue = (intptr_t)&rendmode; +# endif #endif } diff --git a/polymer/eduke32/source/gamevars.h b/polymer/eduke32/source/gamevars.h index 0aded2c36..4d68e8138 100644 --- a/polymer/eduke32/source/gamevars.h +++ b/polymer/eduke32/source/gamevars.h @@ -94,15 +94,21 @@ extern gamearray_t aGameArrays[MAXGAMEARRAYS]; extern int32_t g_gameVarCount; extern int32_t g_gameArrayCount; +#if defined LUNATIC_ONLY +# define Gv_GetVar(id, iActor, iPlayer) (OSD_Printf("Gv_GetVar @ %s:%d\n", __FILE__, __LINE__), 0) +# define Gv_SetVar(id, lValue, iActor, iPlayer) OSD_Printf("Gv_SetVar @ %s:%d\n", __FILE__, __LINE__) +#else int32_t __fastcall Gv_GetVar(register int32_t id,register int32_t iActor,register int32_t iPlayer); +void __fastcall Gv_SetVar(register int32_t id,register int32_t lValue,register int32_t iActor,register int32_t iPlayer); +#endif int32_t __fastcall Gv_GetVarX(register int32_t id); +void __fastcall Gv_SetVarX(register int32_t id,register int32_t lValue); + int32_t Gv_GetVarByLabel(const char *szGameLabel,int32_t lDefault,int32_t iActor,int32_t iPlayer); int32_t Gv_NewArray(const char *pszLabel,void *arrayptr,intptr_t asize,uint32_t dwFlags); int32_t Gv_NewVar(const char *pszLabel,intptr_t lValue,uint32_t dwFlags); int32_t Gv_ReadSave(int32_t fil,int32_t newbehav); void __fastcall A_ResetVars(register int32_t iActor); -void __fastcall Gv_SetVar(register int32_t id,register int32_t lValue,register int32_t iActor,register int32_t iPlayer); -void __fastcall Gv_SetVarX(register int32_t id,register int32_t lValue); void G_FreeMapState(int32_t mapnum); void Gv_DumpValues(void); void Gv_Init(void); diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index b8a9793a1..3ca77008d 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -456,6 +456,7 @@ const int32_t playerswhenstarted; int32_t lastvisinc; int16_t g_spriteDeleteQueueSize; int16_t BlimpSpawnSprites[15]; +int32_t g_scriptVersion; int32_t A_IncurDamage(int32_t sn); // not bound-checked! void P_AddWeaponMaybeSwitch(DukePlayer_t *ps, int32_t weap); @@ -479,6 +480,9 @@ int32_t kopen4loadfrommod(const char *filename, char searchfirst); int32_t kfilelength(int32_t handle); void kclose(int32_t handle); int32_t kread(int32_t handle, void *buffer, int32_t leng); + +const char *G_ConFile(void); +void G_DoGameStartup(const int32_t *params); ]] @@ -840,17 +844,17 @@ local modname_stack = {} local ERRLEV = 5 local function readintostr(fn) - -- XXX: this is pretty much the same as the code in El_RunOnce() + -- XXX: this is pretty much the same as the code in L_RunOnce() local fd = ffiC.kopen4loadfrommod(fn, 0) -- TODO: g_loadFromGroupOnly if (fd < 0) then - errorf(ERRLEV, "Couldn't open file \"%s\"", fn) + return nil end local sz = ffiC.kfilelength(fd) if (sz == 0) then ffiC.kclose(fd) - errorf(ERRLEV, "Didn't load module \"%s\": zero-length file", fn) + return "" end if (sz < 0) then @@ -890,8 +894,11 @@ local function our_require(modname) return package_loaded[modname] end - -- TODO: better pattern-matching (permit "", ".lua", ".elua" ?) - local str = readintostr(modname .. ".lua") + local modfn = modname .. ".lua" + local str = readintostr(modfn) + if (str == nil) then + errorf(ERRLEV-1, "Couldn't open file \"%s\"", modfn) + end local modfunc, errmsg = loadstring(str) if (modfunc == nil) then @@ -1099,8 +1106,16 @@ G_.player = player -- from above ---=== Lunatic interpreter setup ===--- -local lunacon = require("lunacon") +local concode +--- Compile CONs +do + read_into_string = readintostr -- for lunacon + local lunacon = require("lunacon") + + local confn = ffi.string(ffiC.G_ConFile()) + concode = lunacon.compile(confn) +end -- change the environment of this chunk to the table G_ -- NOTE: all references to global variables from this point on @@ -1111,9 +1126,9 @@ setfenv(1, G_) local function printkv(label, table) print("========== Keys and values of "..label.." ("..tostring(table)..")") for k,v in pairs(table) do - print(k .. ': ' .. tostring(v)) + print(k .. ": " .. tostring(v)) end - print('----------') + print("----------") end --printkv('_G AFTER SETFENV', _G) @@ -1124,8 +1139,8 @@ end -- error(..., 2) is to blame the caller and get its line numbers local tmpmt = { - __index = function() error('dummy variable: read access forbidden', 2) end, - __newindex = function() error('dummy variable: write access forbidden', 2) end, + __index = function() error("dummy variable: read access forbidden", 2) end, + __newindex = function() error("dummy variable: write access forbidden", 2) end, } setmtonce(dummy_empty_table, tmpmt) @@ -1140,7 +1155,7 @@ setmtonce(gv, tmpmt) defs_c.create_globals(_G) ---- indirect C array access ---- -actor = defs_c.creategtab(ffiC.actor, ffiC.MAXSPRITES, 'actor[]') +actor = defs_c.creategtab(ffiC.actor, ffiC.MAXSPRITES, "actor[]") function TEMP_getvollev() -- REMOVE return ffiC.ud.volume_number+1, ffiC.ud.level_number+1 @@ -1295,3 +1310,12 @@ setmetatable( -- In particular, we need the functions defined after setting this chunk's -- environment earlier. setfenv(0, _G) + +-- Finally, run the CON code translated into Lua. +if (concode) then + local confunc, conerrmsg = loadstring(concode, "CON") + if (confunc == nil) then + error("Failure loading translated CON code: "..conerrmsg) + end + confunc() +end diff --git a/polymer/eduke32/source/lunatic/dynsymlist b/polymer/eduke32/source/lunatic/dynsymlist index 1f26e0632..6406a11f8 100644 --- a/polymer/eduke32/source/lunatic/dynsymlist +++ b/polymer/eduke32/source/lunatic/dynsymlist @@ -63,6 +63,9 @@ kfilelength; kclose; kread; +G_ConFile; +G_DoGameStartup; + actor; ud; g_player; @@ -71,6 +74,7 @@ playerswhenstarted; lastvisinc; g_spriteDeleteQueueSize; BlimpSpawnSprites; +g_scriptVersion; luaJIT_BC_lunacon; luaJIT_BC_con_lang; diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index 53563f9d3..247cce40e 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -21,6 +21,10 @@ local tostring = tostring local type = type local unpack = unpack +-- non-nil if running from EDuke32 +-- (read_into_string~=nil iff string.dump==nil) +local read_into_string = read_into_string + if (string.dump) then require("strict") end @@ -366,13 +370,22 @@ end local function parse(contents) end -- fwd-decl -local function do_include_file(dirname, filename) end -local function cmd_include(filename) end +local function do_include_file(dirname, filename) + assert(type(filename)=="string") -if (not string.dump) then - -- NOT IMPLEMENTED -else - function do_include_file(dirname, filename) + if (g_have_file[filename] ~= nil) then + printf("[%d] Fatal error: infinite loop including \"%s\"", filename) + g_numerrors = inf + return + end + + local contents + + if (read_into_string) then + -- running from EDuke32 + contents = read_into_string(filename) + else + -- running stand-alone local io = require("io") local fd, msg = io.open(dirname..filename) @@ -388,36 +401,30 @@ else end end - if (g_have_file[filename] ~= nil) then - printf("[%d] Fatal error: infinite loop including \"%s\"", filename) - g_numerrors = inf - return - end - - local contents = fd:read("*all") + contents = fd:read("*all") fd:close() - - if (contents == nil) then - -- maybe that file name turned out to be a directory or other - -- special file accidentally - printf("[%d] Fatal error: couldn't read from \"%s\"", - g_recurslevel, dirname..filename) - g_numerrors = inf - return - end - - printf("%s[%d] Translating file \"%s\"", (g_recurslevel==-1 and "\n---- ") or "", - g_recurslevel+1, dirname..filename); - - local oldfilename = g_filename - g_filename = filename - parse(contents) - g_filename = oldfilename end - function cmd_include(filename) - do_include_file(g_directory, filename) + if (contents == nil) then + -- maybe that file name turned out to be a directory or other + -- special file accidentally + printf("[%d] Fatal error: couldn't read from \"%s\"", + g_recurslevel, dirname..filename) + g_numerrors = inf + return end + + printf("%s[%d] Translating file \"%s\"", (g_recurslevel==-1 and "\n---- ") or "", + g_recurslevel+1, dirname..filename); + + local oldfilename = g_filename + g_filename = filename + parse(contents) + g_filename = oldfilename +end + +function cmd_include(filename) + do_include_file(g_directory, filename) end --- Per-module game data @@ -497,14 +504,26 @@ local function cmd_definequote(qnum, quotestr) end local function cmd_gamestartup(...) - local nums = {...} + local args = {...} - if (#nums ~= 26 and #nums ~= 30) then + if (#args ~= 26 and #args ~= 30) then errprintf("must pass either 26 (1.3D) or 30 (1.5) values") return end - g_data.startup = nums -- TODO: sanity-check them + if (string.dump == nil) then + -- running from EDuke32 + local ffi = require("ffi") + local ffiC = ffi.C + + if (args == 30) then + ffiC.g_scriptVersion = 14 + end + local params = ffi.new("int32_t [30]", args) + ffiC.G_DoGameStartup(params) + end + + g_data.startup = args -- TODO: sanity-check them end local function cmd_definesound(sndnum, fn, ...) @@ -1609,6 +1628,10 @@ local function flatten_codetab(codetab) return tmpcode end +local function get_code_string(codetab) + return table.concat(flatten_codetab(g_curcode), "\n") +end + ---=== EXPORTED FUNCTIONS ===--- @@ -1707,8 +1730,21 @@ local function handle_cmdline_arg(str) end end +local function reset_all() + reset_labels() + reset_gamedata() + reset_codegen() +end + +local function print_on_failure(msg) + if (g_lastkwpos ~= nil) then + printf("LAST KEYWORD POSITION: %s, %s", linecolstr(g_lastkwpos), g_lastkw) + end + print(msg) +end + if (string.dump) then - --- stand-alone + -- running stand-alone local i = 1 while (arg[i]) do @@ -1722,9 +1758,7 @@ if (string.dump) then for argi=1,#arg do local filename = arg[argi] - reset_labels() - reset_gamedata() - reset_codegen() + reset_all() g_directory = filename:match("(.*/)") or "" filename = filename:sub(#g_directory+1, -1) @@ -1736,17 +1770,27 @@ if (string.dump) then -- local ok, msg = true, do_include_file(g_directory, filename) if (not ok) then - if (g_lastkwpos ~= nil) then - printf("LAST KEYWORD POSITION: %s, %s", linecolstr(g_lastkwpos), g_lastkw) - end - print(msg) + print_on_failure(msg) end if (g_printcode) then local file = require("io").stderr file:write(format("-- GENERATED CODE for \"%s\":\n", filename)) - file:write(table.concat(flatten_codetab(g_curcode), "\n")) + file:write(get_code_string(g_curcode)) file:write("\n") end end +else + -- running from EDuke32 + function compile(filename) + -- TODO: pathsearchmode=1 set in G_CompileScripts + reset_all() + local ok, msg = pcall(do_include_file, "", filename) + if (not ok) then + print_on_failure(msg) + return nil + else + return get_code_string(g_curcode) + end + end end diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index ce86dd093..d14e2a494 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -89,6 +89,8 @@ if (vol==1 and lev==1) then -- E1L1 actor[562].flags = bit.bor(actor[562].flags, 2); -- pal 6 with goggles on front SEENINE end +--[[ +-- TODO: better API for all TROR things? if (vol==1 and lev==8) then print('tweaking bunch 1'); -- trueror1.map @@ -99,6 +101,7 @@ if (vol==1 and lev==8) then sector[i].floorz = sector[i].floorz - 3*1024; end end +--]] local unsafe = pcall(function() string.UNSAFE=true; end)