Expose loop variables to Lua (get only)

Nep Note: Had to adjust a bit of this to account for the fact player_t grabbing isn't ass anymore
This commit is contained in:
JugadorXEI 2025-04-19 17:55:27 +02:00 committed by NepDisk
parent ba9381acc2
commit bffc25f198
3 changed files with 179 additions and 0 deletions

View file

@ -236,6 +236,8 @@ static const struct {
{META_OVERLAY, "t_overlay_t"},
{META_TERRAIN, "terrain_t"},
{META_SONICLOOPVARS, "sonicloopvars_t"},
{META_SONICLOOPCAMVARS, "sonicloopcamvars_t"},
{NULL, NULL}
};

View file

@ -111,6 +111,9 @@ extern lua_State *gL;
#define META_OVERLAY "T_OVERLAY_T*"
#define META_TERRAIN "TERRAIN_T*"
#define META_SONICLOOPVARS "SONICLOOPVARS_T*"
#define META_SONICLOOPCAMVARS "SONICLOOPCAMVARS_T*"
boolean luaL_checkboolean(lua_State *L, int narg);
int LUA_EnumLib(lua_State *L);

View file

@ -347,6 +347,7 @@ enum player_e
player_bumpertime,
player_ping,
player_packetloss,
player_loop,
#ifdef HWRENDER
player_fovadd,
#endif
@ -534,6 +535,7 @@ static const char *const player_opt[] = {
"bumpertime",
"ping",
"packetloss",
"loop",
#ifdef HWRENDER
"fovadd",
#endif
@ -1127,6 +1129,9 @@ static int player_get(lua_State *L)
case player_packetloss:
lua_pushinteger(L, playerpacketlosstable[plr - players]);
break;
case player_loop:
LUA_PushUserdata(L, &plr->loop, META_SONICLOOPVARS);
break;
#ifdef HWRENDER
case player_fovadd:
lua_pushfixed(L, plr->fovadd);
@ -1752,6 +1757,8 @@ static int player_set(lua_State *L)
return NOSET;
case player_packetloss:
return NOSET;
case player_loop:
return NOSET;
#ifdef HWRENDER
case player_fovadd:
plr->fovadd = luaL_checkfixed(L, 3);
@ -2547,6 +2554,163 @@ static int ticcmd_set(lua_State *L)
#undef NOFIELD
enum sonicloopvars {
sonicloopvars_radius = 0,
sonicloopvars_revolution,
sonicloopvars_min_revolution,
sonicloopvars_max_revolution,
sonicloopvars_yaw,
sonicloopvars_origin_x,
sonicloopvars_origin_y,
sonicloopvars_origin_z,
sonicloopvars_origin_shift_x,
sonicloopvars_origin_shift_y,
sonicloopvars_shift_x,
sonicloopvars_shift_y,
sonicloopvars_flip,
sonicloopvars_camera,
};
static const char *const sonicloopvars_opt[] = {
"radius",
"revolution",
"min_revolution",
"max_revolution",
"yaw",
"origin_x",
"origin_y",
"origin_z",
"origin_shift_x",
"origin_shift_y",
"shift_x",
"shift_y",
"flip",
"camera",
NULL
};
static int sonicloopvars_get(lua_State *L)
{
sonicloopvars_t *sonicloopvars = *((sonicloopvars_t **)luaL_checkudata(L, 1, META_SONICLOOPVARS));
enum sonicloopvars field = luaL_checkoption(L, 2, NULL, sonicloopvars_opt);
// This should always be valid.
I_Assert(sonicloopvars != NULL);
switch (field)
{
case sonicloopvars_radius:
lua_pushfixed(L, sonicloopvars->radius);
break;
case sonicloopvars_revolution:
lua_pushfixed(L, sonicloopvars->revolution);
break;
case sonicloopvars_min_revolution:
lua_pushfixed(L, sonicloopvars->min_revolution);
break;
case sonicloopvars_max_revolution:
lua_pushfixed(L, sonicloopvars->max_revolution);
break;
case sonicloopvars_yaw:
lua_pushangle(L, sonicloopvars->yaw);
break;
case sonicloopvars_origin_x:
lua_pushfixed(L, sonicloopvars->origin.x);
break;
case sonicloopvars_origin_y:
lua_pushfixed(L, sonicloopvars->origin.y);
break;
case sonicloopvars_origin_z:
lua_pushfixed(L, sonicloopvars->origin.z);
break;
case sonicloopvars_origin_shift_x:
lua_pushfixed(L, sonicloopvars->origin_shift.x);
break;
case sonicloopvars_origin_shift_y:
lua_pushfixed(L, sonicloopvars->origin_shift.y);
break;
case sonicloopvars_shift_x:
lua_pushfixed(L, sonicloopvars->shift.x);
break;
case sonicloopvars_shift_y:
lua_pushfixed(L, sonicloopvars->shift.y);
break;
case sonicloopvars_flip:
lua_pushboolean(L, sonicloopvars->flip);
break;
case sonicloopvars_camera:
LUA_PushUserdata(L, &sonicloopvars->camera, META_SONICLOOPCAMVARS);
break;
}
return 1;
}
enum sonicloopcamvars {
sonicloopcamvars_enter_tic = 0,
sonicloopcamvars_exit_tic,
sonicloopcamvars_zoom_in_speed,
sonicloopcamvars_zoom_out_speed,
sonicloopcamvars_dist,
sonicloopcamvars_pan,
sonicloopcamvars_pan_speed,
sonicloopcamvars_pan_accel,
sonicloopcamvars_pan_back,
};
static const char *const sonicloopcamvars_opt[] = {
"enter_tic",
"exit_tic",
"zoom_in_speed",
"zoom_out_speed",
"dist",
"pan",
"pan_speed",
"pan_accel",
"pan_back",
NULL
};
static int sonicloopcamvars_get(lua_State *L)
{
sonicloopcamvars_t *sonicloopcamvars = *((sonicloopcamvars_t **)luaL_checkudata(L, 1, META_SONICLOOPCAMVARS));
enum sonicloopcamvars field = luaL_checkoption(L, 2, NULL, sonicloopcamvars_opt);
// This should always be valid.
I_Assert(sonicloopcamvars != NULL);
switch (field)
{
case sonicloopcamvars_enter_tic:
lua_pushinteger(L, sonicloopcamvars->enter_tic);
break;
case sonicloopcamvars_exit_tic:
lua_pushinteger(L, sonicloopcamvars->exit_tic);
break;
case sonicloopcamvars_zoom_in_speed:
lua_pushinteger(L, sonicloopcamvars->zoom_in_speed);
break;
case sonicloopcamvars_zoom_out_speed:
lua_pushinteger(L, sonicloopcamvars->zoom_out_speed);
break;
case sonicloopcamvars_dist:
lua_pushfixed(L, sonicloopcamvars->dist);
break;
case sonicloopcamvars_pan:
lua_pushangle(L, sonicloopcamvars->pan);
break;
case sonicloopcamvars_pan_speed:
lua_pushfixed(L, sonicloopcamvars->pan_speed);
break;
case sonicloopcamvars_pan_accel:
lua_pushinteger(L, sonicloopcamvars->pan_accel);
break;
case sonicloopcamvars_pan_back:
lua_pushinteger(L, sonicloopcamvars->pan_back);
break;
}
return 1;
}
int LUA_PlayerLib(lua_State *L)
{
luaL_newmetatable(L, META_PLAYER);
@ -2600,6 +2764,16 @@ int LUA_PlayerLib(lua_State *L)
lua_pushcfunction(L, ticcmd_set);
lua_setfield(L, -2, "__newindex");
lua_pop(L,1);
luaL_newmetatable(L, META_SONICLOOPVARS);
lua_pushcfunction(L, sonicloopvars_get);
lua_setfield(L, -2, "__index");
lua_pop(L,1);
luaL_newmetatable(L, META_SONICLOOPCAMVARS);
lua_pushcfunction(L, sonicloopcamvars_get);
lua_setfield(L, -2, "__index");
lua_pop(L,1);
lua_newuserdata(L, 0);
lua_createtable(L, 0, 2);