player userdata implemented
parent
4474ac5ad1
commit
183e7b4a82
104
dlls/gslua.cpp
104
dlls/gslua.cpp
|
@ -1,16 +1,28 @@
|
|||
/* Copyright (C) 2015 Graham Sanderson, All Rights Reserved */
|
||||
#include "extdll.h"
|
||||
#include "eiface.h"
|
||||
#include "util.h"
|
||||
#include "game.h"
|
||||
#include "gslua.h"
|
||||
|
||||
#include "cbase.h"
|
||||
#include "player.h"
|
||||
#include "trains.h"
|
||||
#include "nodes.h"
|
||||
#include "weapons.h"
|
||||
#include "soundent.h"
|
||||
#include "monsters.h"
|
||||
#include "shake.h"
|
||||
#include "decals.h"
|
||||
#include "gamerules.h"
|
||||
#include "game.h"
|
||||
#include "pm_shared.h"
|
||||
#include "hltv.h"
|
||||
#include "gslua.h"
|
||||
#include "lua/lua.hpp"
|
||||
|
||||
#define MAX_LUA_LIB_FUNCS 200
|
||||
#define MAX_LUA_LIB_GLOBALS 200
|
||||
|
||||
static void LuaRegisterGlobals(void);
|
||||
static void LuaRegisterPlayerUData(void);
|
||||
static int LUA_PrintConsole(lua_State *L);
|
||||
|
||||
struct gsLuaRun_s g_gsLuaRun;
|
||||
|
@ -69,6 +81,7 @@ void LuaInit(void)
|
|||
ALERT(at_console, "[HLua] Lua initialized.\n");
|
||||
|
||||
LuaRegisterGlobals();
|
||||
LuaRegisterPlayerUData();
|
||||
|
||||
if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\")") == TRUE)
|
||||
{
|
||||
|
@ -136,9 +149,9 @@ void LuaSetTable(char *szIndex, vec3_t vValue)
|
|||
{
|
||||
lua_pushstring(g_L, szIndex);
|
||||
lua_createtable(g_L, 3, 0);
|
||||
LuaSetTable(0, vValue.x);
|
||||
LuaSetTable(1, vValue.y);
|
||||
LuaSetTable(2, vValue.z);
|
||||
LuaSetTable(1, vValue.x);
|
||||
LuaSetTable(2, vValue.y);
|
||||
LuaSetTable(3, vValue.z);
|
||||
lua_settable(g_L, -3);
|
||||
}
|
||||
|
||||
|
@ -259,3 +272,82 @@ static int LUA_PrintConsole(lua_State *L)
|
|||
ALERT(eType, "%s\n", szToWrite);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- Player userdata ---
|
||||
|
||||
struct PLAYER_s *PLAYER_New(CBasePlayer *pPlayer)
|
||||
{
|
||||
struct PLAYER_s *player = (struct PLAYER_s *)lua_newuserdata(g_L, sizeof(struct PLAYER_s));
|
||||
luaL_getmetatable(g_L, "PLAYER_s");
|
||||
lua_setmetatable(g_L, -2);
|
||||
|
||||
player->ply = pPlayer;
|
||||
player->pev = pPlayer->pev;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
static struct PLAYER_s *PLAYER_Get(lua_State *L)
|
||||
{
|
||||
struct PLAYER_s *player;
|
||||
|
||||
luaL_checktype(L, 1, LUA_TUSERDATA);
|
||||
player = (struct PLAYER_s *)luaL_checkudata(L, 1, "PLAYER_s");
|
||||
if(player == NULL) LuaWarning("[HLua] PLAYER got invalid type\n");
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
#define TEDIUM_REMOVER(n, v, nv) \
|
||||
static int n(lua_State *L) \
|
||||
{ \
|
||||
struct PLAYER_s *player = PLAYER_Get(L);\
|
||||
v;\
|
||||
return nv;\
|
||||
}
|
||||
|
||||
TEDIUM_REMOVER(PLAYER_GetHealth, lua_pushnumber(L, player->pev->health), 1)
|
||||
TEDIUM_REMOVER(PLAYER_GetMaxHealth, lua_pushnumber(L, player->pev->max_health), 1)
|
||||
TEDIUM_REMOVER(PLAYER_GetArmor, lua_pushnumber(L, player->pev->armorvalue), 1)
|
||||
TEDIUM_REMOVER(PLAYER_GetPosition,
|
||||
lua_createtable(g_L, 3, 0);
|
||||
LuaSetTable(1, player->pev->origin.x);
|
||||
LuaSetTable(2, player->pev->origin.y);
|
||||
LuaSetTable(3, player->pev->origin.z), 1)
|
||||
TEDIUM_REMOVER(PLAYER_GetVelocity,
|
||||
lua_createtable(g_L, 3, 0);
|
||||
LuaSetTable(1, player->pev->velocity.x);
|
||||
LuaSetTable(2, player->pev->velocity.y);
|
||||
LuaSetTable(3, player->pev->velocity.z), 1)
|
||||
TEDIUM_REMOVER(PLAYER_SetHealth, float fNewVal = luaL_checknumber(L, 2); player->pev->health = fNewVal, 0)
|
||||
TEDIUM_REMOVER(PLAYER_SetMaxHealth, float fNewVal = luaL_checknumber(L, 2); player->pev->max_health = fNewVal, 0)
|
||||
TEDIUM_REMOVER(PLAYER_SetArmor, float fNewVal = luaL_checknumber(L, 2); player->pev->armorvalue = fNewVal, 0)
|
||||
TEDIUM_REMOVER(PLAYER_SetPosition,
|
||||
float fX = luaL_checknumber(L, 2); float fY = luaL_checknumber(L, 3); float fZ = luaL_checknumber(L, 4);
|
||||
(player->pev->origin.x = fX, player->pev->origin.y = fY, player->pev->origin.z = fZ), 0)
|
||||
TEDIUM_REMOVER(PLAYER_SetVelocity,
|
||||
float fX = luaL_checknumber(L, 2); float fY = luaL_checknumber(L, 3); float fZ = luaL_checknumber(L, 4);
|
||||
(player->pev->velocity.x = fX, player->pev->velocity.y = fY, player->pev->velocity.z = fZ), 0)
|
||||
|
||||
static const luaL_Reg gsLib_PLAYER[] = {
|
||||
{ "GetHealth", PLAYER_GetHealth },
|
||||
{ "GetMaxHealth", PLAYER_GetMaxHealth },
|
||||
{ "GetArmor", PLAYER_GetArmor },
|
||||
{ "GetPosition", PLAYER_GetPosition },
|
||||
{ "GetVelocity", PLAYER_GetVelocity },
|
||||
{ "SetHealth", PLAYER_SetHealth },
|
||||
{ "SetMaxHealth", PLAYER_SetMaxHealth },
|
||||
{ "SetArmor", PLAYER_SetArmor },
|
||||
{ "SetPosition", PLAYER_SetPosition },
|
||||
{ "SetVelocity", PLAYER_SetVelocity },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static void LuaRegisterPlayerUData(void)
|
||||
{
|
||||
luaL_newmetatable(g_L, "PLAYER_s");
|
||||
lua_pushstring(g_L, "__index");
|
||||
luaL_newlibtable(g_L, gsLib_PLAYER);
|
||||
luaL_setfuncs(g_L, gsLib_PLAYER, 0);
|
||||
lua_settable(g_L, -3);
|
||||
}
|
||||
|
|
|
@ -12,10 +12,18 @@ struct gsLuaRun_s
|
|||
LRUN_CHLR_PLAYER_THINK = TRUE;
|
||||
};
|
||||
|
||||
struct PLAYER_s
|
||||
{
|
||||
CBasePlayer *ply;
|
||||
entvars_t *pev;
|
||||
};
|
||||
|
||||
extern lua_State *g_L;
|
||||
extern BOOL g_bLuaInitialized;
|
||||
extern struct gsLuaRun_s g_gsLuaRun;
|
||||
|
||||
struct PLAYER_s *PLAYER_New(CBasePlayer *pPlayer);
|
||||
|
||||
void LuaInit(void);
|
||||
void LuaQuit(void);
|
||||
void LuaReload(void);
|
||||
|
|
|
@ -127,8 +127,15 @@ void CHalfLifeRules :: PlayerSpawn( CBasePlayer *pPlayer )
|
|||
BOOL bRun;
|
||||
bRun = LuaGet("GAME", LUA_TTABLE, FALSE, FALSE);
|
||||
if(bRun) bRun = LuaGet("PlayerSpawn", LUA_TFUNCTION, TRUE, FALSE);
|
||||
if(bRun) LuaCall("GAME.PlayerSpawn");
|
||||
else g_gsLuaRun.LRUN_CHLR_PLAYER_SPAWN = FALSE;
|
||||
if(bRun)
|
||||
{
|
||||
PLAYER_New(pPlayer);
|
||||
LuaCall("GAME.PlayerSpawn");
|
||||
}
|
||||
else
|
||||
{
|
||||
g_gsLuaRun.LRUN_CHLR_PLAYER_SPAWN = FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -150,11 +157,7 @@ void CHalfLifeRules :: PlayerThink( CBasePlayer *pPlayer )
|
|||
if(bRun) bRun = LuaGet("PlayerThink", LUA_TFUNCTION, TRUE, FALSE);
|
||||
if(bRun)
|
||||
{
|
||||
lua_createtable(g_L, 0, 3);
|
||||
LuaSetTable("health", pPlayer->pev->health);
|
||||
LuaSetTable("maxhealth", pPlayer->pev->max_health);
|
||||
LuaSetTable("armor", pPlayer->pev->armorvalue);
|
||||
LuaSetTable("view", pPlayer->pev->v_angle);
|
||||
PLAYER_New(pPlayer);
|
||||
LuaCall("GAME.PlayerThink", 1);
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue