Browse Source

lots of changes

Marrub 3 years ago
parent
commit
4474ac5ad1
7 changed files with 146 additions and 24 deletions
  1. 1
    0
      dlls/gamerules.cpp
  2. 86
    17
      dlls/gslua.cpp
  3. 22
    4
      dlls/gslua.h
  4. 6
    2
      dlls/player.cpp
  5. 28
    0
      dlls/singleplay_gamerules.cpp
  6. 2
    0
      dlls/world.cpp
  7. 1
    1
      projects/vs2010/hl_cdll.vcxproj

+ 1
- 0
dlls/gamerules.cpp View File

@@ -25,6 +25,7 @@
25 25
 #include	"teamplay_gamerules.h"
26 26
 #include	"skill.h"
27 27
 #include	"game.h"
28
+#include "gslua.h"
28 29
 
29 30
 extern edict_t *EntSelectSpawnPoint( CBaseEntity *pPlayer );
30 31
 

+ 86
- 17
dlls/gslua.cpp View File

@@ -10,8 +10,10 @@
10 10
 #define MAX_LUA_LIB_FUNCS 200
11 11
 #define MAX_LUA_LIB_GLOBALS 200
12 12
 
13
-extern lua_State *g_L;
14
-extern BOOL g_bLuaInitialized;
13
+static void LuaRegisterGlobals(void);
14
+static int LUA_PrintConsole(lua_State *L);
15
+
16
+struct gsLuaRun_s g_gsLuaRun;
15 17
 
16 18
 typedef enum gsLuaType
17 19
 {
@@ -48,14 +50,12 @@ static gsLuaRegGlobal gsLib_GLOBALS[] = {
48 50
 	{ "at_warning",		at_warning,		LTYPE_INT },
49 51
 	{ "at_error",		at_error,		LTYPE_INT },
50 52
 	{ "at_logged",		at_logged,		LTYPE_INT },
51
-	{ "Print_Console",	0,				LTYPE_FUNC },
53
+	{ "Print_Console",	0,				LTYPE_FUNC }, // [marrub] c++ hates unions so this must be 0 and later be given a pointer
52 54
 	{ NULL }
53 55
 };
54 56
 
55 57
 // --- Init/quit  ---
56 58
 
57
-#define LuaError(...) (ALERT(at_error, __VA_ARGS__), g_bLuaInitialized = FALSE, LuaQuit(), g_L = NULL)
58
-
59 59
 void LuaInit(void)
60 60
 {
61 61
 	gsLib_GLOBALS[LF_PRINT_CONSOLE].value.fn = LUA_PrintConsole;
@@ -70,13 +70,10 @@ void LuaInit(void)
70 70
 
71 71
 	LuaRegisterGlobals();
72 72
 
73
-	if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\");") == TRUE)
73
+	if(luaL_dostring(g_L, "Print_Console(at_console, \"[HLua::Lua] Initialization success!\")") == TRUE)
74 74
 	{
75
-		LuaError("[HLua] Failed initial Lua test. Stopping Lua.\n");
76
-		return;
75
+		LuaError("[HLua] Failed initial Lua test.\n");
77 76
 	}
78
-
79
-	LuaParseScripts();
80 77
 }
81 78
 
82 79
 void LuaQuit(void)
@@ -85,21 +82,91 @@ void LuaQuit(void)
85 82
 	ALERT(at_console, "[HLua] Lua shutdown.\n");
86 83
 }
87 84
 
88
-void LuaCheckNull(void const *vpToCheck)
85
+void LuaReload(void)
86
+{
87
+	LuaQuit();
88
+	LuaInit();
89
+}
90
+
91
+BOOL LuaCheckNull(void const *vpToCheck)
92
+{
93
+	BOOL bReturn;
94
+
95
+	bReturn = (vpToCheck == NULL);
96
+
97
+	if(bReturn)
98
+	{
99
+		LuaWarning("[HLua] Null pointer exception!\n");
100
+	}
101
+
102
+	return bReturn;
103
+}
104
+
105
+void LuaCall(char *szFuncName)
106
+{
107
+	if(lua_pcall(g_L, 0, 0, 0) != LUA_OK)
108
+	{
109
+		LuaWarning("[HLua] Failed to run function %s\nLua: %s\n", szFuncName, lua_tostring(g_L, -1));
110
+	}
111
+}
112
+
113
+void LuaCall(char *szFuncName, int iArgs)
89 114
 {
90
-	if(vpToCheck == NULL)
115
+	if(lua_pcall(g_L, iArgs, 0, 0) != LUA_OK)
91 116
 	{
92
-		LuaError("[HLua] Null pointer exception!\n");
117
+		LuaWarning("[HLua] Failed to run function %s\nLua: %s\n", szFuncName, lua_tostring(g_L, -1));
93 118
 	}
94 119
 }
95 120
 
96
-// --- Misc functions ---
121
+void LuaSetTable(char *szIndex, float fValue)
122
+{
123
+	lua_pushstring(g_L, szIndex);
124
+	lua_pushnumber(g_L, fValue);
125
+	lua_settable(g_L, -3);
126
+}
127
+
128
+void LuaSetTable(int iIndex, float fValue)
129
+{
130
+	lua_pushinteger(g_L, iIndex);
131
+	lua_pushnumber(g_L, fValue);
132
+	lua_settable(g_L, -3);
133
+}
134
+
135
+void LuaSetTable(char *szIndex, vec3_t vValue)
136
+{
137
+	lua_pushstring(g_L, szIndex);
138
+	lua_createtable(g_L, 3, 0);
139
+	LuaSetTable(0, vValue.x);
140
+	LuaSetTable(1, vValue.y);
141
+	LuaSetTable(2, vValue.z);
142
+	lua_settable(g_L, -3);
143
+}
144
+
97 145
 void LuaRunScript(char *szFileName)
98 146
 {
99 147
 	if(luaL_dofile(g_L, szFileName) == TRUE)
100 148
 	{
101
-		LuaError("[HLua] Failed to run script %s\nLua: %s\n", szFileName, lua_tostring(g_L, -1));
149
+		LuaWarning("[HLua] Failed to run script %s\nLua: %s\n", szFileName, lua_tostring(g_L, -1));
150
+	}
151
+}
152
+
153
+BOOL LuaGet(char *szName, int iType, BOOL bGetField, BOOL bWarn)
154
+{
155
+	BOOL bReturn;
156
+	int iGottenType;
157
+
158
+	if(bGetField)
159
+	{
160
+		bReturn = ((iGottenType = lua_getfield(g_L, -1, szName)) == iType);
102 161
 	}
162
+	else
163
+	{
164
+		bReturn = ((iGottenType = lua_getglobal(g_L, szName)) == iType);
165
+	}
166
+
167
+	if(bWarn && !bReturn) LuaWarning("[HLua] Incorrect type for %s (expected %d, got %d)\n", szName, iType, iGottenType);
168
+
169
+	return bReturn;
103 170
 }
104 171
 
105 172
 void LuaParseScripts(void)
@@ -117,6 +184,8 @@ void LuaParseScripts(void)
117 184
 
118 185
 	fScripts = fopen(szFilenamePulledOutOfMyAss, "r");
119 186
 
187
+	ALERT(at_console, "[HLua] Running script table.\n");
188
+
120 189
 	if(fScripts == NULL)
121 190
 	{
122 191
 		LuaError("[HLua] Failed to open lua/scripts.txt!\n");
@@ -158,7 +227,7 @@ void LuaParseScripts(void)
158 227
 
159 228
 // --- Binded lua functions and globals ---
160 229
 
161
-void LuaRegisterGlobals(void)
230
+static void LuaRegisterGlobals(void)
162 231
 {
163 232
 	int i;
164 233
 	ALERT(at_console, "[HLua] Registering globals.\n");
@@ -182,7 +251,7 @@ void LuaRegisterGlobals(void)
182 251
 	ALERT(at_console, "[HLua] Global registry success, put out %d globals.\n", i);
183 252
 }
184 253
 
185
-int LUA_PrintConsole(lua_State *L)
254
+static int LUA_PrintConsole(lua_State *L)
186 255
 {
187 256
 	const char *szToWrite = luaL_checkstring(L, 2);
188 257
 	ALERT_TYPE eType = (ALERT_TYPE)luaL_checkinteger(L, 1);

+ 22
- 4
dlls/gslua.h View File

@@ -3,11 +3,29 @@
3 3
 
4 4
 #include "lua/lua.hpp"
5 5
 
6
+#define LuaError(...) (ALERT(at_error, __VA_ARGS__), g_bLuaInitialized = FALSE, LuaQuit(), g_L = NULL)
7
+#define LuaWarning(...) (ALERT(at_warning, __VA_ARGS__))
8
+
9
+struct gsLuaRun_s
10
+{
11
+	BOOL	LRUN_CHLR_PLAYER_SPAWN = TRUE,
12
+			LRUN_CHLR_PLAYER_THINK = TRUE;
13
+};
14
+
15
+extern lua_State *g_L;
16
+extern BOOL g_bLuaInitialized;
17
+extern struct gsLuaRun_s g_gsLuaRun;
18
+
6 19
 void LuaInit(void);
7 20
 void LuaQuit(void);
8
-void LuaCheckNull(void const *vpToCheck);
9
-void LuaRegisterGlobals(void);
21
+void LuaReload(void);
22
+BOOL LuaCheckNull(void const *vpToCheck);
23
+void LuaCall(char *szFuncName);
24
+void LuaCall(char *szFuncName, int iArgs);
25
+void LuaSetTable(char *szIndex, float fValue);
26
+void LuaSetTable(int iIndex, float fValue);
27
+void LuaSetTable(char *szIndex, vec3_t vValue);
28
+void LuaRunScript(char *szFileName);
29
+BOOL LuaGet(char *szName, int iType, BOOL bGetField, BOOL bWarn);
10 30
 void LuaParseScripts(void);
11
-void LuaRunScript(char *szFilename);
12
-int LUA_PrintConsole(lua_State *L);
13 31
 

+ 6
- 2
dlls/player.cpp View File

@@ -1,4 +1,4 @@
1
-	/***
1
+/***
2 2
 *
3 3
 *	Copyright (c) 1996-2001, Valve LLC. All rights reserved.
4 4
 *	
@@ -36,6 +36,7 @@
36 36
 #include "game.h"
37 37
 #include "pm_shared.h"
38 38
 #include "hltv.h"
39
+#include "gslua.h"
39 40
 
40 41
 // #define DUCKFIX
41 42
 
@@ -3507,7 +3508,7 @@ void CBasePlayer::ImpulseCommands( )
3507 3508
 void CBasePlayer::CheatImpulseCommands( int iImpulse )
3508 3509
 {
3509 3510
 #if !defined( HLDEMO_BUILD )
3510
-	if ( g_flWeaponCheat == 0.0 )
3511
+	if ( g_flWeaponCheat == 0.0 && iImpulse != 81 )
3511 3512
 	{
3512 3513
 		return;
3513 3514
 	}
@@ -3532,6 +3533,9 @@ void CBasePlayer::CheatImpulseCommands( int iImpulse )
3532 3533
 			break;
3533 3534
 		}
3534 3535
 
3536
+	case 81:
3537
+		LuaReload();
3538
+		break;
3535 3539
 
3536 3540
 	case 101:
3537 3541
 		gEvilImpulse101 = TRUE;

+ 28
- 0
dlls/singleplay_gamerules.cpp View File

@@ -23,6 +23,7 @@
23 23
 #include	"gamerules.h"
24 24
 #include	"skill.h"
25 25
 #include	"items.h"
26
+#include "gslua.h"
26 27
 
27 28
 extern DLL_GLOBAL CGameRules	*g_pGameRules;
28 29
 extern DLL_GLOBAL BOOL	g_fGameOver;
@@ -121,6 +122,14 @@ float CHalfLifeRules::FlPlayerFallDamage( CBasePlayer *pPlayer )
121 122
 //=========================================================
122 123
 void CHalfLifeRules :: PlayerSpawn( CBasePlayer *pPlayer )
123 124
 {
125
+	if(g_gsLuaRun.LRUN_CHLR_PLAYER_SPAWN)
126
+	{
127
+		BOOL bRun;
128
+		bRun = LuaGet("GAME", LUA_TTABLE, FALSE, FALSE);
129
+		if(bRun) bRun = LuaGet("PlayerSpawn", LUA_TFUNCTION, TRUE, FALSE);
130
+		if(bRun) LuaCall("GAME.PlayerSpawn");
131
+		else g_gsLuaRun.LRUN_CHLR_PLAYER_SPAWN = FALSE;
132
+	}
124 133
 }
125 134
 
126 135
 //=========================================================
@@ -134,6 +143,25 @@ BOOL CHalfLifeRules :: AllowAutoTargetCrosshair( void )
134 143
 //=========================================================
135 144
 void CHalfLifeRules :: PlayerThink( CBasePlayer *pPlayer )
136 145
 {
146
+	if(g_gsLuaRun.LRUN_CHLR_PLAYER_THINK)
147
+	{
148
+		BOOL bRun;
149
+		bRun = LuaGet("GAME", LUA_TTABLE, FALSE, FALSE);
150
+		if(bRun) bRun = LuaGet("PlayerThink", LUA_TFUNCTION, TRUE, FALSE);
151
+		if(bRun)
152
+		{
153
+			lua_createtable(g_L, 0, 3);
154
+			LuaSetTable("health", pPlayer->pev->health);
155
+			LuaSetTable("maxhealth", pPlayer->pev->max_health);
156
+			LuaSetTable("armor", pPlayer->pev->armorvalue);
157
+			LuaSetTable("view", pPlayer->pev->v_angle);
158
+			LuaCall("GAME.PlayerThink", 1);
159
+		}
160
+		else
161
+		{
162
+			g_gsLuaRun.LRUN_CHLR_PLAYER_THINK = FALSE;
163
+		}
164
+	}
137 165
 }
138 166
 
139 167
 

+ 2
- 0
dlls/world.cpp View File

@@ -33,6 +33,7 @@
33 33
 #include "weapons.h"
34 34
 #include "gamerules.h"
35 35
 #include "teamplay_gamerules.h"
36
+#include "gslua.h"
36 37
 
37 38
 extern CGraph WorldGraph;
38 39
 extern CSoundEnt *pSoundEnt;
@@ -472,6 +473,7 @@ float g_flWeaponCheat;
472 473
 void CWorld :: Spawn( void )
473 474
 {
474 475
 	g_fGameOver = FALSE;
476
+	LuaParseScripts();
475 477
 	Precache( );
476 478
 	g_flWeaponCheat = CVAR_GET_FLOAT( "sv_cheats" );  // Is the impulse 101 command allowed?
477 479
 }

+ 1
- 1
projects/vs2010/hl_cdll.vcxproj View File

@@ -58,7 +58,7 @@
58 58
       </PrecompiledHeader>
59 59
       <WarningLevel>Level3</WarningLevel>
60 60
       <Optimization>Disabled</Optimization>
61
-      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;CLIENT_DLL;CLIENT_WEAPONS;HL_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
61
+      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;CLIENT_DLL;CLIENT_WEAPONS;HL_DLL;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
62 62
       <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
63 63
       <AdditionalIncludeDirectories>..\..\dlls;..\..\cl_dll;..\..\public;..\..\common;..\..\pm_shared;..\..\engine;..\..\utils\vgui\include;..\..\game_shared;..\..\external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
64 64
       <RuntimeTypeInfo>true</RuntimeTypeInfo>

Loading…
Cancel
Save