File: scripting.h

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (228 lines) | stat: -rw-r--r-- 5,446 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef _SCRIPTING_H
#define _SCRIPTING_H

#include "globalincs/globals.h"
#include "globalincs/pstypes.h"
#include "parse/lua.h"

#include <stdio.h>

//**********Scripting languages that are possible
#define SC_LUA			(1<<0)

//*************************Scripting structs*************************
#define SCRIPT_END_LIST		NULL

struct image_desc
{
	char fname[MAX_FILENAME_LEN];
	int handle;
};

//**********Main Conditional Hook stuff

#define MAX_HOOK_CONDITIONS	8

//Conditionals
#define CHC_NONE			-1
#define CHC_MISSION			0
#define CHC_SHIP			1
#define CHC_SHIPCLASS		2
#define CHC_SHIPTYPE		3
#define CHC_STATE			4
#define CHC_CAMPAIGN		5
#define CHC_WEAPONCLASS		6
#define CHC_OBJECTTYPE		7
#define CHC_KEYPRESS		8
#define CHC_ACTION			9
#define CHC_VERSION			10
#define CHC_APPLICATION		11

//Actions
#define CHA_NONE			-1
#define CHA_WARPOUT			0
#define CHA_WARPIN			1
#define CHA_DEATH			2
#define CHA_ONFRAME			3
#define CHA_COLLIDESHIP		4
#define CHA_COLLIDEWEAPON	5
#define CHA_COLLIDEDEBRIS	6
#define CHA_COLLIDEASTEROID	7
#define CHA_HUDDRAW			8
#define CHA_OBJECTRENDER	9
#define CHA_SPLASHSCREEN	10
#define CHA_GAMEINIT		11
#define CHA_MISSIONSTART	12
#define CHA_MISSIONEND		13
#define CHA_MOUSEMOVED		14
#define CHA_MOUSEPRESSED	15
#define CHA_MOUSERELEASED	16
#define CHA_KEYPRESSED		17
#define CHA_KEYRELEASED		18
#define CHA_ONSTATESTART	19
#define CHA_ONSTATEEND		20
#define CHA_ONWEAPONDELETE	21
#define CHA_ONWPEQUIPPED	22
#define CHA_ONWPFIRED		23
#define CHA_ONWPSELECTED	24
#define CHA_ONWPDESELECTED	25
#define CHA_GAMEPLAYSTART	26
#define CHA_ONTURRETFIRED	27
#define CHA_PRIMARYFIRE		28
#define CHA_SECONDARYFIRE	29
#define CHA_ONSHIPARRIVE	30
#define CHA_COLLIDEBEAM		31
#define CHA_ONACTION		32
#define CHA_ONACTIONSTOPPED	33
#define CHA_MSGRECEIVED		34
#define CHA_HUDMSGRECEIVED	35
#define CHA_AFTERBURNSTART	36
#define CHA_AFTERBURNEND    37
#define CHA_BEAMFIRE        38

// management stuff
void scripting_state_init();
void scripting_state_close();
void scripting_state_do_frame(float frametime);

class script_condition
{
public:
	int condition_type;
	union
	{
		char name[CONDITION_LENGTH];
	} data;

	script_condition()
		: condition_type(CHC_NONE)
	{
		memset(data.name, 0, sizeof(data.name));
	}
};

class script_action
{
public:
	int action_type;
	script_hook hook;

	script_action()
		: action_type(CHA_NONE)
	{
		script_hook_init(&hook);
	}
};

class ConditionedHook
{
private:
	SCP_vector<script_action> Actions;
	script_condition Conditions[MAX_HOOK_CONDITIONS];
public:
	bool AddCondition(script_condition *sc);
	bool AddAction(script_action *sa);

	bool ConditionsValid(int action, class object *objp=NULL, int more_data = 0);
	bool IsOverride(class script_state *sys, int action);
	bool Run(class script_state *sys, int action, char format='\0', void *data=NULL);
};

//**********Main script_state function
class script_state
{
private:
	char StateName[32];

	int Langs;
	struct lua_State *LuaState;
	const struct script_lua_lib_list *LuaLibs;

	//Utility variables
	SCP_vector<image_desc> ScriptImages;
	SCP_vector<ConditionedHook> ConditionalHooks;

private:

	void ParseChunkSub(int *out_lang, int *out_index, char* debug_str=NULL);
	int RunBytecodeSub(int in_lang, int in_idx, char format='\0', void *data=NULL);

	void SetLuaSession(struct lua_State *L);

	void OutputLuaMeta(FILE *fp);
	
	//Lua private helper functions
	bool OpenHookVarTable();
	bool CloseHookVarTable();

	//Internal Lua helper functions
	void EndLuaFrame();

	//Destroy everything
	void Clear();

public:
	//***Init/Deinit
	script_state(char *name);
	script_state& operator=(script_state &in);
	~script_state();

	//***Internal scripting stuff
	int LoadBm(char *name);
	void UnloadImages();

	lua_State *GetLuaSession(){return LuaState;}

	//***Init functions for langs
	int CreateLuaState();

	//***Get data
	int OutputMeta(char *filename);

	//***Moves data
	//void MoveData(script_state &in);

	//***Variable handling functions
	bool GetGlobal(char *name, char format='\0', void *data=NULL);
	void RemGlobal(char *name);

	void SetHookVar(char *name, char format, void *data=NULL);
	void SetHookObject(char *name, object *objp);
	void SetHookObjects(int num, ...);
	bool GetHookVar(char *name, char format='\0', void *data=NULL);
	void RemHookVar(char *name);
	void RemHookVars(unsigned int num, ...);

	//***Hook creation functions
	bool EvalString(const char *string, const char *format=NULL, void *rtn=NULL, const char *debug_str=NULL);
	void ParseChunk(script_hook *dest, char* debug_str=NULL);
	bool ParseCondition(const char *filename="<Unknown>");

	//***Hook running functions
	int RunBytecode(script_hook &hd, char format='\0', void *data=NULL);
	bool IsOverride(script_hook &hd);
	int RunCondition(int condition, char format='\0', void *data=NULL, class object *objp = NULL, int more_data = 0);
	bool IsConditionOverride(int action, object *objp=NULL);

	//*****Other functions
	void EndFrame();
};


//**********Script registration functions
void script_init();

//**********Script globals
extern class script_state Script_system;
extern bool Output_scripting_meta;

//**********Script hook stuff (scripting.tbl)
extern script_hook Script_globalhook;
extern script_hook Script_simulationhook;
extern script_hook Script_hudhook;
extern script_hook Script_splashhook;
extern script_hook Script_gameinithook;

//*************************Conditional scripting*************************

#endif //_SCRIPTING_H