From fba070f82eb2f83ba27d1aff7ba6667f0e6fefa3 Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 1 Dec 2025 21:08:14 +0300 Subject: [PATCH] Expose cameras to lua outside of hud hooks --- src/lua_hudlib.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/lua_hudlib.c b/src/lua_hudlib.c index 251a1013c..676f0b394 100644 --- a/src/lua_hudlib.c +++ b/src/lua_hudlib.c @@ -149,7 +149,8 @@ enum cameraf { camera_momx, camera_momy, camera_momz, - camera_pnum + camera_pnum, + camera_freecam, }; @@ -169,6 +170,7 @@ static const char *const camera_opt[] = { "momy", "momz", "pnum", + "freecam", NULL}; enum hudpatch { @@ -387,10 +389,43 @@ static int camera_get(lua_State *L) case camera_pnum: lua_pushinteger(L, camnum); break; + case camera_freecam: + lua_pushboolean(L, cam->freecam); + break; } return 1; } +static int lib_getCamera(lua_State *L) +{ + // No cameras on dedicated + if (dedicated) + return 0; + + if (lua_type(L, 2) == LUA_TNUMBER) + { + int i = lua_tonumber(L, 2); + + if (i < 0 || i > splitscreen) + return 0; + + LUA_PushUserdata(L, &camera[i], META_CAMERA); + return 1; + } + + return 0; +} + +static int lib_lenCamera(lua_State *L) +{ + if (dedicated) + lua_pushinteger(L, 0); + else + lua_pushinteger(L, splitscreen+1); // splitscreen == 0 -> 1 active camera, splitscreen == 1 -> 2 cameras and so on + + return 1; +} + // // lib_draw // @@ -1371,6 +1406,16 @@ int LUA_HudLib(lua_State *L) lua_setfield(L, -2, "__index"); lua_pop(L,1); + lua_newuserdata(L, 0); + lua_createtable(L, 0, 2); + lua_pushcfunction(L, lib_getCamera); + lua_setfield(L, -2, "__index"); + + lua_pushcfunction(L, lib_lenCamera); + lua_setfield(L, -2, "__len"); + lua_setmetatable(L, -2); + lua_setglobal(L, "cameras"); + camera_fields_ref = Lua_CreateFieldTable(L, camera_opt); luaL_register(L, "hud", lib_hud);