From 366b1f65a18a89b26afcd30c71255bcd9d1995be Mon Sep 17 00:00:00 2001 From: lachablock Date: Sat, 10 Jul 2021 18:22:07 +1000 Subject: [PATCH] Allow Lua write access to camera_t variables & expose the cameras globally --- src/lua_hudlib.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lua_script.c | 8 +++++++ 2 files changed, 64 insertions(+) diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 9a3e676d5..306ffaf94 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -384,6 +384,59 @@ static int camera_get(lua_State *L) return 1; } +static int camera_set(lua_State *L) +{ + camera_t *cam = *((camera_t **)luaL_checkudata(L, 1, META_CAMERA)); + enum cameraf field = luaL_checkoption(L, 2, NULL, camera_opt); + + I_Assert(cam != NULL); + + switch(field) + { + case camera_subsector: + case camera_floorz: + case camera_ceilingz: + case camera_height: + case camera_radius: + return luaL_error(L, LUA_QL("camera_t") " field " LUA_QS " should not be set directly.", camera_opt[field]); + case camera_chase: + if (cam == &camera) + CV_SetValue(&cv_chasecam, (INT32)luaL_checkboolean(L, 3)); + else if (cam == &camera2) + CV_SetValue(&cv_chasecam2, (INT32)luaL_checkboolean(L, 3)); + else // ??? this should never happen, but ok + cam->chase = luaL_checkboolean(L, 3); + break; + case camera_aiming: + cam->aiming = luaL_checkangle(L, 3); + break; + case camera_x: + cam->x = luaL_checkfixed(L, 3); + break; + case camera_y: + cam->y = luaL_checkfixed(L, 3); + break; + case camera_z: + cam->z = luaL_checkfixed(L, 3); + break; + case camera_angle: + cam->angle = luaL_checkangle(L, 3); + break; + case camera_momx: + cam->momx = luaL_checkfixed(L, 3); + break; + case camera_momy: + cam->momy = luaL_checkfixed(L, 3); + break; + case camera_momz: + cam->momz = luaL_checkfixed(L, 3); + break; + default: + return luaL_error(L, LUA_QL("camera_t") " has no field named " LUA_QS, camera_opt[field]); + } + return 0; +} + // // lib_draw // @@ -1283,6 +1336,9 @@ int LUA_HudLib(lua_State *L) luaL_newmetatable(L, META_CAMERA); lua_pushcfunction(L, camera_get); lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, camera_set); + lua_setfield(L, -2, "__newindex"); lua_pop(L,1); luaL_register(L, "hud", lib_hud); diff --git a/src/lua_script.c b/src/lua_script.c index 6faff8729..54ab124fc 100644 --- a/src/lua_script.c +++ b/src/lua_script.c @@ -393,6 +393,14 @@ int LUA_PushGlobals(lua_State *L, const char *word) } else if (fastcmp(word, "mouse2")) { LUA_PushUserdata(L, &mouse2, META_MOUSE); return 1; + } else if (fastcmp(word, "camera")) { + LUA_PushUserdata(L, &camera, META_CAMERA); + return 1; + } else if (fastcmp(word, "camera2")) { + if (!splitscreen) + return 0; + LUA_PushUserdata(L, &camera2, META_CAMERA); + return 1; } return 0; }