Lets not compile lua code multiple time each frame x)

This commit is contained in:
indev committed 2024-07-27 03:26:37 +03:00
1 parent 60d7b0272e
commit 3864dc5833
3 files changed
+54 -16

No files matched your search

+7 -7
View File
@@ -3497,7 +3497,7 @@ void K_DriftDustHandling(mobj_t *spawner)
fixed_t spawny = P_RandomRange(-spawnrange, spawnrange)<<FRACBITS;
INT32 speedrange = 2;
mobj_t *dust;
mobjtype_t stardust = LUA_EvalMathEx("MT_STARDUST", NULL);
mobjtype_t stardust = LUA_GetConstant("MT_STARDUST");
boolean havestardust = true;
if (!stardust)
{
@@ -5860,7 +5860,7 @@ static mobj_t *K_SpawnOrMoveMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t typ
void K_SpawnWaterRunParticles(mobj_t *mobj)
{
mobjtype_t watertrailunderlay = LUA_EvalMathEx("MT_WATERTRAILUNDERLAY", NULL);
mobjtype_t watertrailunderlay = LUA_GetConstant("MT_WATERTRAILUNDERLAY");
// Technically, we'd need only 1 check after we try get all necessary states, but fetching these
// constants is a bit costly (if they are missing. If they are not, lua_glib helps us ;3),
@@ -5869,13 +5869,13 @@ void K_SpawnWaterRunParticles(mobj_t *mobj)
if (!watertrailunderlay)
return;
statenum_t watertrailunderlay_minstate = LUA_EvalMathEx("S_WATERTRAILUNDERLAY1", NULL);
statenum_t watertrailunderlay_maxstate = LUA_EvalMathEx("S_WATERTRAILUNDERLAYLAST", NULL);
statenum_t watertrailunderlay_minstate = LUA_GetConstant("S_WATERTRAILUNDERLAY1");
statenum_t watertrailunderlay_maxstate = LUA_GetConstant("S_WATERTRAILUNDERLAYLAST");
mobjtype_t watertrail = LUA_EvalMathEx("MT_WATERTRAIL", NULL);
mobjtype_t watertrail = LUA_GetConstant("MT_WATERTRAIL");
statenum_t watertrail_minstate = LUA_EvalMathEx("S_WATERTRAIL1", NULL);
statenum_t watertrail_maxstate = LUA_EvalMathEx("S_WATERTRAILLAST", NULL);
statenum_t watertrail_minstate = LUA_GetConstant("S_WATERTRAIL1");
statenum_t watertrail_maxstate = LUA_GetConstant("S_WATERTRAILLAST");
if (!(watertrailunderlay_minstate && watertrailunderlay_maxstate && watertrail && watertrail_minstate && watertrail_maxstate) || // Check if we're missing something
(watertrail_minstate > watertrail_maxstate && watertrailunderlay_minstate > watertrailunderlay_maxstate)) // Check if some of these were freeslot'd in wrong order
+46 -9
View File
@@ -364,6 +364,17 @@ void LUA_DumpFile(const char *filename)
}
#endif
static void createMathLibState(void)
{
mL = lua_newstate(LUA_Alloc, NULL);
lua_atpanic(mL, LUA_Panic);
// open only enum lib
lua_pushcfunction(mL, LUA_EnumLib);
lua_pushboolean(mL, true);
lua_call(mL, 1, 0);
}
fixed_t LUA_EvalMathEx(const char *word, const char **error)
{
char buf[1024], *b;
@@ -373,15 +384,7 @@ fixed_t LUA_EvalMathEx(const char *word, const char **error)
// make a new state so SOC can't interefere with scripts
// allocate state
if (mL == NULL)
{
mL = lua_newstate(LUA_Alloc, NULL);
lua_atpanic(mL, LUA_Panic);
// open only enum lib
lua_pushcfunction(mL, LUA_EnumLib);
lua_pushboolean(mL, true);
lua_call(mL, 1, 0);
}
createMathLibState();
// change ^ into ^^ for Lua.
strcpy(buf, "return ");
@@ -420,6 +423,40 @@ fixed_t LUA_EvalMath(const char *word)
return res;
}
// A hack to prevent lua from crashing game if constant doesn't exist
static int lua_get_constant(lua_State *L)
{
lua_pushvalue(mL, LUA_GLOBALSINDEX);
lua_pushvalue(L, 1);
lua_gettable(L, -2);
return 1;
}
fixed_t LUA_GetConstant(const char *word)
{
if (mL == NULL)
createMathLibState();
fixed_t res = 0;
lua_pushcfunction(mL, lua_get_constant);
lua_pushstring(mL, word);
if (lua_pcall(mL, 1, 1, 0) == 0)
{
if (lua_isnumber(mL, -1))
res = lua_tointeger(mL, -1);
}
else
{
// Pop error message
lua_pop(mL, 1);
}
return res;
}
void LUA_InvalidateMathlibCache(const char *name)
{
// No state => no cache => nothing to invalidate :3
+1
View File
@@ -46,6 +46,7 @@ void LUA_DumpFile(const char *filename);
#endif
fixed_t LUA_EvalMathEx(const char *word, const char **error);
fixed_t LUA_EvalMath(const char *word);
fixed_t LUA_GetConstant(const char *word);
// Need better name for this ;-;
void LUA_InvalidateMathlibCache(const char *name);