2012-11-10 20:59:00 +00:00
|
|
|
/* The Lunatic Interpreter, part of EDuke32. Game-side stuff. */
|
2011-09-20 19:12:24 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2012-12-25 16:13:41 +00:00
|
|
|
#include "build.h" // printext256
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
#include "lunatic_game.h"
|
|
|
|
|
2011-09-20 19:12:24 +00:00
|
|
|
#include "osd.h"
|
2012-11-10 20:59:00 +00:00
|
|
|
#include "gamedef.h" // EventNames[]
|
|
|
|
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
L_State g_ElState;
|
2012-10-07 15:25:52 +00:00
|
|
|
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-02-09 22:45:18 +00:00
|
|
|
// this serves two purposes:
|
|
|
|
// the values as booleans and the addresses as keys to the Lua registry
|
2012-06-10 18:56:10 +00:00
|
|
|
uint8_t g_elEvents[MAXEVENTS];
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2012-06-10 18:56:15 +00:00
|
|
|
// same thing for actors:
|
2012-12-23 19:24:16 +00:00
|
|
|
el_actor_t g_elActors[MAXTILES];
|
2012-06-10 18:56:15 +00:00
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
int32_t g_elCallDepth = 0;
|
|
|
|
|
2012-07-20 21:57:44 +00:00
|
|
|
// for timing events and actors
|
|
|
|
uint32_t g_eventCalls[MAXEVENTS], g_actorCalls[MAXTILES];
|
|
|
|
double g_eventTotalMs[MAXEVENTS], g_actorTotalMs[MAXTILES];
|
|
|
|
|
2012-02-09 22:45:18 +00:00
|
|
|
|
|
|
|
// forward-decls...
|
2013-01-01 15:24:11 +00:00
|
|
|
static int32_t SetEvent_CF(lua_State *L);
|
|
|
|
static int32_t SetActor_CF(lua_State *L);
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2012-05-13 16:05:16 +00:00
|
|
|
// in lpeg.o
|
|
|
|
extern int luaopen_lpeg(lua_State *L);
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2012-08-02 10:52:28 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t x, y, z, c;
|
|
|
|
} rng_jkiss_t;
|
|
|
|
|
|
|
|
// See: Good Practice in (Pseudo) Random Number Generation for
|
|
|
|
// Bioinformatics Applications, by David Jones
|
2012-12-29 15:21:16 +00:00
|
|
|
ATTRIBUTE_OPTIMIZE("O2")
|
2012-08-02 10:52:28 +00:00
|
|
|
uint32_t rand_jkiss_u32(rng_jkiss_t *s)
|
|
|
|
{
|
|
|
|
uint64_t t;
|
|
|
|
s->x = 314527869 * s->x + 1234567;
|
|
|
|
s->y ^= s->y << 5; s->y ^= s->y >> 7; s->y ^= s->y << 22;
|
|
|
|
t = 4294584393ULL * s->z + s->c; s->c = t >> 32; s->z = t;
|
|
|
|
return s->x + s->y + s->z;
|
|
|
|
}
|
|
|
|
|
2012-12-29 15:21:16 +00:00
|
|
|
ATTRIBUTE_OPTIMIZE("O2")
|
2012-08-02 10:52:28 +00:00
|
|
|
double rand_jkiss_dbl(rng_jkiss_t *s)
|
|
|
|
{
|
|
|
|
double x;
|
|
|
|
unsigned int a, b;
|
|
|
|
a = rand_jkiss_u32(s) >> 6; /* Upper 26 bits */
|
|
|
|
b = rand_jkiss_u32(s) >> 5; /* Upper 27 bits */
|
|
|
|
x = (a * 134217728.0 + b) / 9007199254740992.0;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-20 21:57:44 +00:00
|
|
|
void El_PrintTimes(void)
|
|
|
|
{
|
2012-12-09 13:24:36 +00:00
|
|
|
int32_t i, maxlen=0;
|
|
|
|
char buf[32];
|
|
|
|
const char nn = Bstrlen("EVENT_");
|
|
|
|
|
|
|
|
for (i=0; i<MAXEVENTS; i++)
|
|
|
|
{
|
|
|
|
int32_t len = Bstrlen(EventNames[i]+nn);
|
|
|
|
Bassert(len < (int32_t)sizeof(buf));
|
|
|
|
maxlen = max(len, maxlen);
|
|
|
|
}
|
2012-07-20 21:57:44 +00:00
|
|
|
|
|
|
|
OSD_Printf("{\n {\n");
|
2012-12-09 13:24:36 +00:00
|
|
|
OSD_Printf(" -- event times, [event]={ total calls, total time [ms], mean time/call [us] }\n");
|
2012-07-20 21:57:44 +00:00
|
|
|
for (i=0; i<MAXEVENTS; i++)
|
|
|
|
if (g_eventCalls[i])
|
2012-12-09 13:24:36 +00:00
|
|
|
{
|
|
|
|
int32_t n=Bsprintf(buf, "%s", EventNames[i]+nn);
|
|
|
|
|
|
|
|
for (; n<maxlen; n++)
|
|
|
|
buf[n] = ' ';
|
|
|
|
buf[maxlen] = 0;
|
|
|
|
|
|
|
|
OSD_Printf(" [%s]={ %8d, %9.3f, %9.3f },\n",
|
|
|
|
buf, g_eventCalls[i], g_eventTotalMs[i],
|
2012-07-20 21:57:44 +00:00
|
|
|
1000*g_eventTotalMs[i]/g_eventCalls[i]);
|
2012-12-09 13:24:36 +00:00
|
|
|
}
|
2012-07-20 21:57:44 +00:00
|
|
|
|
|
|
|
OSD_Printf(" },\n\n {\n");
|
2012-12-09 13:24:36 +00:00
|
|
|
OSD_Printf(" -- actor times, [tile]={ total calls, total time [ms], mean time/call [us] }\n");
|
2012-07-20 21:57:44 +00:00
|
|
|
for (i=0; i<MAXTILES; i++)
|
|
|
|
if (g_actorCalls[i])
|
|
|
|
OSD_Printf(" [%5d]={ %8d, %9.3f, %9.3f },\n",
|
|
|
|
i, g_actorCalls[i], g_actorTotalMs[i],
|
|
|
|
1000*g_actorTotalMs[i]/g_actorCalls[i]);
|
|
|
|
OSD_Printf(" },\n}\n");
|
|
|
|
}
|
2012-05-13 16:05:16 +00:00
|
|
|
|
2012-12-25 16:13:41 +00:00
|
|
|
////////// ERROR REPORTING //////////
|
2012-12-25 16:13:45 +00:00
|
|
|
#define EL_MAXERRORS 20
|
2012-12-25 16:13:41 +00:00
|
|
|
static int32_t el_numErrors=0, el_tooMuchErrors;
|
|
|
|
static char *el_errorMsgs[EL_MAXERRORS];
|
|
|
|
|
|
|
|
// Compare against all other error messages.
|
|
|
|
// Strictly seen, this is quadratic-time, but EL_MAXERRORS is small and
|
|
|
|
// errors should be fixed anyway.
|
|
|
|
static int32_t cmp_against_others(const char *str, int32_t slen)
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<el_numErrors; i++)
|
|
|
|
if (!Bstrncmp(str, el_errorMsgs[i], slen))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void El_OnError(const char *str)
|
|
|
|
{
|
|
|
|
if (!el_tooMuchErrors)
|
|
|
|
{
|
|
|
|
char *errstr = NULL;
|
|
|
|
const char *nl = Bstrchr(str, '\n');
|
|
|
|
|
|
|
|
// First, check whether the error message matches an already saved one
|
|
|
|
if (nl)
|
|
|
|
{
|
|
|
|
// cut off string after the newline
|
|
|
|
if (cmp_against_others(str, nl-str))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// save string fully
|
|
|
|
if (cmp_against_others(str, Bstrlen(str)))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the (EL_MAXERRORS+1)'th distinct error appeared, we have too many.
|
|
|
|
if (el_numErrors==EL_MAXERRORS)
|
|
|
|
{
|
|
|
|
el_tooMuchErrors = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, allocate storage for the potentially clipped error string...
|
|
|
|
if (nl)
|
|
|
|
{
|
|
|
|
errstr = Bmalloc(nl-str+1);
|
|
|
|
if (errstr)
|
|
|
|
{
|
|
|
|
Bmemcpy(errstr, str, nl-str);
|
|
|
|
errstr[nl-str] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errstr = Bstrdup(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...and save it:
|
|
|
|
if (errstr)
|
|
|
|
el_errorMsgs[el_numErrors++] = errstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
static void El_OnOutOfMem(void)
|
|
|
|
{
|
|
|
|
extern void G_GameExit(const char *msg);
|
|
|
|
G_GameExit("Out of memory in Lunatic.");
|
|
|
|
}
|
|
|
|
|
2012-12-25 16:13:41 +00:00
|
|
|
void El_ClearErrors(void)
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<EL_MAXERRORS; i++)
|
|
|
|
{
|
|
|
|
Bfree(el_errorMsgs[i]);
|
|
|
|
el_errorMsgs[i] = NULL;
|
|
|
|
}
|
|
|
|
el_numErrors = el_tooMuchErrors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void El_DisplayErrors(void)
|
|
|
|
{
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<el_numErrors; i++)
|
|
|
|
printext256(8, 8+8*i, 242, 0, el_errorMsgs[i], 0);
|
|
|
|
if (el_tooMuchErrors)
|
|
|
|
printext256(8, 8+8*EL_MAXERRORS, 242, 0, "(more distinct errors ...)", 0);
|
|
|
|
}
|
|
|
|
|
2012-07-13 18:20:35 +00:00
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
////////// STATE CREATION/DESTRUCTIION //////////
|
2011-09-20 19:12:24 +00:00
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
static int our_traceback_CF(lua_State *L)
|
|
|
|
{
|
|
|
|
Bassert(lua_gettop(L)==1);
|
|
|
|
|
|
|
|
if (lua_type(L, 1)==LUA_TBOOLEAN)
|
|
|
|
{
|
|
|
|
lua_pushvalue(L, 1); // duplicate it
|
|
|
|
return 1; // and tell Lua to return it
|
|
|
|
}
|
|
|
|
|
|
|
|
Bassert(lua_type(L, 1)==LUA_TSTRING);
|
|
|
|
|
|
|
|
// call debug.traceback with the string
|
|
|
|
L_PushDebugTraceback(L);
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_call(L, 1, 1);
|
|
|
|
Bassert(lua_gettop(L)==2); // Lua will pop off args
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
static void El_StateSetup(lua_State *L)
|
|
|
|
{
|
2012-07-13 18:20:35 +00:00
|
|
|
luaopen_lpeg(L);
|
|
|
|
lua_pop(L, lua_gettop(L)); // pop off whatever lpeg leaves on the stack
|
|
|
|
|
2012-02-09 22:45:18 +00:00
|
|
|
// create misc. global functions in the Lua state
|
2013-01-01 15:24:11 +00:00
|
|
|
lua_pushcfunction(L, SetEvent_CF);
|
2012-12-25 16:13:45 +00:00
|
|
|
lua_setglobal(L, "gameevent_internal");
|
2013-01-01 15:24:11 +00:00
|
|
|
lua_pushcfunction(L, SetActor_CF);
|
2012-12-23 19:24:16 +00:00
|
|
|
lua_setglobal(L, "gameactor_internal");
|
2012-07-13 18:20:35 +00:00
|
|
|
|
|
|
|
Bassert(lua_gettop(L)==0);
|
2013-01-01 15:24:11 +00:00
|
|
|
|
|
|
|
lua_pushcfunction(L, &our_traceback_CF);
|
2011-09-20 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
// 0: success, <0: failure
|
|
|
|
int32_t El_CreateState(L_State *estate, const char *name)
|
2011-09-20 19:12:24 +00:00
|
|
|
{
|
2012-12-25 16:13:41 +00:00
|
|
|
L_ErrorFunc = El_OnError;
|
2013-01-01 15:24:11 +00:00
|
|
|
L_OutOfMemFunc = El_OnOutOfMem;
|
2012-12-25 16:13:41 +00:00
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
return L_CreateState(estate, name, &El_StateSetup);
|
2011-09-20 19:12:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
void El_DestroyState(L_State *estate)
|
2011-09-20 19:12:24 +00:00
|
|
|
{
|
2012-11-10 20:59:00 +00:00
|
|
|
L_DestroyState(estate);
|
2012-06-10 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////// Lua_CFunctions //////////
|
|
|
|
|
|
|
|
// gameevent(EVENT_..., lua_function)
|
2013-01-01 15:24:11 +00:00
|
|
|
static int32_t SetEvent_CF(lua_State *L)
|
2012-06-10 18:56:15 +00:00
|
|
|
{
|
2012-07-13 18:20:35 +00:00
|
|
|
int32_t eventidx;
|
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
Bassert(lua_gettop(L) == 2);
|
2012-07-13 18:20:35 +00:00
|
|
|
eventidx = luaL_checkint(L, 1);
|
2012-12-25 16:13:45 +00:00
|
|
|
Bassert((unsigned)eventidx < MAXEVENTS);
|
2012-06-10 18:56:15 +00:00
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
L_CheckAndRegisterFunction(L, &g_elEvents[eventidx]);
|
2012-02-09 22:45:18 +00:00
|
|
|
g_elEvents[eventidx] = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
// gameactor(actortile, strength, act, mov, movflags, lua_function)
|
2013-01-01 15:24:11 +00:00
|
|
|
static int32_t SetActor_CF(lua_State *L)
|
2012-02-09 22:45:18 +00:00
|
|
|
{
|
2012-12-23 19:24:16 +00:00
|
|
|
int32_t actortile, strength, movflags;
|
|
|
|
const con_action_t *act;
|
|
|
|
const con_move_t *mov;
|
2012-07-13 18:20:35 +00:00
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
el_actor_t *a;
|
|
|
|
|
|
|
|
Bassert(lua_gettop(L) == 6);
|
2012-07-13 18:20:35 +00:00
|
|
|
|
|
|
|
actortile = luaL_checkint(L, 1);
|
2012-12-23 19:24:16 +00:00
|
|
|
Bassert((unsigned)actortile < MAXTILES);
|
|
|
|
|
|
|
|
strength = luaL_checkint(L, 2);
|
|
|
|
movflags = luaL_checkint(L, 5);
|
|
|
|
|
|
|
|
act = lua_topointer(L, 3);
|
|
|
|
mov = lua_topointer(L, 4);
|
|
|
|
|
|
|
|
a = &g_elActors[actortile];
|
|
|
|
L_CheckAndRegisterFunction(L, a);
|
|
|
|
a->haveit = 1;
|
|
|
|
|
|
|
|
a->strength = strength;
|
|
|
|
a->movflags = movflags;
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2012-12-23 19:24:16 +00:00
|
|
|
Bmemcpy(&a->act, act, sizeof(con_action_t));
|
|
|
|
Bmemcpy(&a->mov, mov, sizeof(con_move_t));
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2012-06-10 18:56:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
static int32_t call_regd_function3(lua_State *L, void *keyaddr,
|
|
|
|
int32_t iActor, int32_t iPlayer, int32_t lDist)
|
2012-06-10 18:56:15 +00:00
|
|
|
{
|
|
|
|
int32_t i;
|
2013-01-01 15:24:11 +00:00
|
|
|
#if !defined NDEBUG
|
|
|
|
int32_t top = lua_gettop(L);
|
2012-12-29 15:21:32 +00:00
|
|
|
#endif
|
2012-07-13 18:20:35 +00:00
|
|
|
// get the Lua function from the registry
|
|
|
|
lua_pushlightuserdata(L, keyaddr);
|
|
|
|
lua_gettable(L, LUA_REGISTRYINDEX);
|
2012-06-10 18:56:10 +00:00
|
|
|
|
|
|
|
lua_pushinteger(L, iActor);
|
|
|
|
lua_pushinteger(L, iPlayer);
|
|
|
|
lua_pushinteger(L, lDist);
|
2012-02-09 22:45:18 +00:00
|
|
|
|
|
|
|
// -- call it! --
|
2012-12-29 15:21:32 +00:00
|
|
|
#ifdef DEBUGGINGAIDS
|
|
|
|
i = lua_pcall(L, 3, 0, 1);
|
|
|
|
#else
|
2012-06-10 18:56:10 +00:00
|
|
|
i = lua_pcall(L, 3, 0, 0);
|
2012-12-29 15:21:32 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
Bassert(lua_gettop(L) == top+(i!=0));
|
2012-06-10 18:56:15 +00:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
static int32_t g_eventIdx = 0;
|
|
|
|
static void El_EventErrorPrint(const char *errmsg)
|
|
|
|
{
|
|
|
|
OSD_Printf(OSD_ERROR "event \"%s\" runtime error: %s\n",
|
|
|
|
EventNames[g_eventIdx], errmsg);
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
int32_t El_CallEvent(L_State *estate, int32_t eventidx, int32_t iActor, int32_t iPlayer, int32_t lDist)
|
2012-06-10 18:56:15 +00:00
|
|
|
{
|
|
|
|
// XXX: estate must be the one where the events were registered...
|
|
|
|
// make a global?
|
|
|
|
|
|
|
|
lua_State *const L = estate->L;
|
2012-12-25 16:13:45 +00:00
|
|
|
int32_t i;
|
2012-06-10 18:56:15 +00:00
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
g_elCallDepth++;
|
|
|
|
i = call_regd_function3(L, &g_elEvents[eventidx], iActor, iPlayer, lDist);
|
|
|
|
g_elCallDepth--;
|
2012-02-09 22:45:18 +00:00
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
if (i != 0)
|
2012-02-09 22:45:18 +00:00
|
|
|
{
|
2013-01-01 15:24:11 +00:00
|
|
|
g_eventIdx = eventidx;
|
|
|
|
return L_HandleError(L, i, &El_EventErrorPrint);
|
2012-06-10 18:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
static int32_t g_actorTile, g_iActor;
|
|
|
|
static void El_ActorErrorPrint(const char *errmsg)
|
|
|
|
{
|
|
|
|
OSD_Printf(OSD_ERROR "actor %d (sprite %d) runtime error: %s\n",
|
|
|
|
g_actorTile, g_iActor, errmsg);
|
|
|
|
}
|
|
|
|
|
2012-11-10 20:59:00 +00:00
|
|
|
int32_t El_CallActor(L_State *estate, int32_t actortile, int32_t iActor, int32_t iPlayer, int32_t lDist)
|
2012-06-10 18:56:15 +00:00
|
|
|
{
|
|
|
|
lua_State *const L = estate->L;
|
2012-12-25 16:13:45 +00:00
|
|
|
int32_t i;
|
2012-06-10 18:56:15 +00:00
|
|
|
|
2012-12-25 16:13:45 +00:00
|
|
|
g_elCallDepth++;
|
|
|
|
i = call_regd_function3(L, &g_elActors[actortile], iActor, iPlayer, lDist);
|
|
|
|
g_elCallDepth--;
|
2012-06-10 18:56:15 +00:00
|
|
|
|
2013-01-01 15:24:11 +00:00
|
|
|
if (i != 0)
|
2012-06-10 18:56:15 +00:00
|
|
|
{
|
2013-01-01 15:24:11 +00:00
|
|
|
g_actorTile = actortile;
|
|
|
|
g_iActor = iActor;
|
|
|
|
return L_HandleError(L, i, &El_ActorErrorPrint);
|
2012-02-09 22:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|