FS2_Open
Open source remastering of the Freespace 2 engine
scripting.h
Go to the documentation of this file.
1 #ifndef _SCRIPTING_H
2 #define _SCRIPTING_H
3 
4 #include "globalincs/globals.h"
5 #include "globalincs/pstypes.h"
6 #include "parse/lua.h"
7 
8 #include <stdio.h>
9 
10 //**********Scripting languages that are possible
11 #define SC_LUA (1<<0)
12 
13 //*************************Scripting structs*************************
14 #define SCRIPT_END_LIST NULL
15 
16 struct image_desc
17 {
19  int handle;
20 };
21 
22 //**********Main Conditional Hook stuff
23 
24 #define MAX_HOOK_CONDITIONS 8
25 
26 //Conditionals
27 #define CHC_NONE -1
28 #define CHC_MISSION 0
29 #define CHC_SHIP 1
30 #define CHC_SHIPCLASS 2
31 #define CHC_SHIPTYPE 3
32 #define CHC_STATE 4
33 #define CHC_CAMPAIGN 5
34 #define CHC_WEAPONCLASS 6
35 #define CHC_OBJECTTYPE 7
36 #define CHC_KEYPRESS 8
37 #define CHC_ACTION 9
38 #define CHC_VERSION 10
39 #define CHC_APPLICATION 11
40 
41 //Actions
42 #define CHA_NONE -1
43 #define CHA_WARPOUT 0
44 #define CHA_WARPIN 1
45 #define CHA_DEATH 2
46 #define CHA_ONFRAME 3
47 #define CHA_COLLIDESHIP 4
48 #define CHA_COLLIDEWEAPON 5
49 #define CHA_COLLIDEDEBRIS 6
50 #define CHA_COLLIDEASTEROID 7
51 #define CHA_HUDDRAW 8
52 #define CHA_OBJECTRENDER 9
53 #define CHA_SPLASHSCREEN 10
54 #define CHA_GAMEINIT 11
55 #define CHA_MISSIONSTART 12
56 #define CHA_MISSIONEND 13
57 #define CHA_MOUSEMOVED 14
58 #define CHA_MOUSEPRESSED 15
59 #define CHA_MOUSERELEASED 16
60 #define CHA_KEYPRESSED 17
61 #define CHA_KEYRELEASED 18
62 #define CHA_ONSTATESTART 19
63 #define CHA_ONSTATEEND 20
64 #define CHA_ONWEAPONDELETE 21
65 #define CHA_ONWPEQUIPPED 22
66 #define CHA_ONWPFIRED 23
67 #define CHA_ONWPSELECTED 24
68 #define CHA_ONWPDESELECTED 25
69 #define CHA_GAMEPLAYSTART 26
70 #define CHA_ONTURRETFIRED 27
71 #define CHA_PRIMARYFIRE 28
72 #define CHA_SECONDARYFIRE 29
73 #define CHA_ONSHIPARRIVE 30
74 #define CHA_COLLIDEBEAM 31
75 #define CHA_ONACTION 32
76 #define CHA_ONACTIONSTOPPED 33
77 #define CHA_MSGRECEIVED 34
78 #define CHA_HUDMSGRECEIVED 35
79 #define CHA_AFTERBURNSTART 36
80 #define CHA_AFTERBURNEND 37
81 #define CHA_BEAMFIRE 38
82 
83 // management stuff
86 void scripting_state_do_frame(float frametime);
87 
89 {
90 public:
92  union
93  {
95  } data;
96 
98  : condition_type(CHC_NONE)
99  {
100  memset(data.name, 0, sizeof(data.name));
101  }
102 };
103 
105 {
106 public:
109 
111  : action_type(CHA_NONE)
112  {
113  script_hook_init(&hook);
114  }
115 };
116 
118 {
119 private:
122 public:
123  bool AddCondition(script_condition *sc);
124  bool AddAction(script_action *sa);
125 
126  bool ConditionsValid(int action, class object *objp=NULL, int more_data = 0);
127  bool IsOverride(class script_state *sys, int action);
128  bool Run(class script_state *sys, int action, char format='\0', void *data=NULL);
129 };
130 
131 //**********Main script_state function
133 {
134 private:
135  char StateName[32];
136 
137  int Langs;
138  struct lua_State *LuaState;
139  const struct script_lua_lib_list *LuaLibs;
140 
141  //Utility variables
142  SCP_vector<image_desc> ScriptImages;
143  SCP_vector<ConditionedHook> ConditionalHooks;
144 
145 private:
146 
147  void ParseChunkSub(int *out_lang, int *out_index, char* debug_str=NULL);
148  int RunBytecodeSub(int in_lang, int in_idx, char format='\0', void *data=NULL);
149 
150  void SetLuaSession(struct lua_State *L);
151 
152  void OutputLuaMeta(FILE *fp);
153 
154  //Lua private helper functions
155  bool OpenHookVarTable();
156  bool CloseHookVarTable();
157 
158  //Internal Lua helper functions
159  void EndLuaFrame();
160 
161  //Destroy everything
162  void Clear();
163 
164 public:
165  //***Init/Deinit
166  script_state(char *name);
168  ~script_state();
169 
170  //***Internal scripting stuff
171  int LoadBm(char *name);
172  void UnloadImages();
173 
174  lua_State *GetLuaSession(){return LuaState;}
175 
176  //***Init functions for langs
177  int CreateLuaState();
178 
179  //***Get data
180  int OutputMeta(char *filename);
181 
182  //***Moves data
183  //void MoveData(script_state &in);
184 
185  //***Variable handling functions
186  bool GetGlobal(char *name, char format='\0', void *data=NULL);
187  void RemGlobal(char *name);
188 
189  void SetHookVar(char *name, char format, void *data=NULL);
190  void SetHookObject(char *name, object *objp);
191  void SetHookObjects(int num, ...);
192  bool GetHookVar(char *name, char format='\0', void *data=NULL);
193  void RemHookVar(char *name);
194  void RemHookVars(unsigned int num, ...);
195 
196  //***Hook creation functions
197  bool EvalString(const char *string, const char *format=NULL, void *rtn=NULL, const char *debug_str=NULL);
198  void ParseChunk(script_hook *dest, char* debug_str=NULL);
199  bool ParseCondition(const char *filename="<Unknown>");
200 
201  //***Hook running functions
202  int RunBytecode(script_hook &hd, char format='\0', void *data=NULL);
203  bool IsOverride(script_hook &hd);
204  int RunCondition(int condition, char format='\0', void *data=NULL, class object *objp = NULL, int more_data = 0);
205  bool IsConditionOverride(int action, object *objp=NULL);
206 
207  //*****Other functions
208  void EndFrame();
209 };
210 
211 
212 //**********Script registration functions
213 void script_init();
214 
215 //**********Script globals
216 extern class script_state Script_system;
217 extern bool Output_scripting_meta;
218 
219 //**********Script hook stuff (scripting.tbl)
225 
226 //*************************Conditional scripting*************************
227 
228 #endif //_SCRIPTING_H
GLenum GLsizei GLenum format
Definition: Gl.h:1509
bool IsOverride(class script_state *sys, int action)
Definition: scripting.cpp:528
bool AddAction(script_action *sa)
Definition: scripting.cpp:246
bool ParseCondition(const char *filename="<Unknown>")
Definition: scripting.cpp:1338
#define MAX_FILENAME_LEN
Definition: pstypes.h:324
bool AddCondition(script_condition *sc)
Definition: scripting.cpp:232
bool GetGlobal(char *name, char format='\0', void *data=NULL)
Definition: scripting.cpp:785
bool GetHookVar(char *name, char format='\0', void *data=NULL)
Definition: scripting.cpp:712
#define MAX_HOOK_CONDITIONS
Definition: scripting.h:24
void RemHookVar(char *name)
Definition: scripting.cpp:749
GLenum condition
Definition: Glext.h:8726
union script_condition::@261 data
script_hook Script_gameinithook
Definition: scripting.cpp:28
int handle
Definition: scripting.h:19
object * objp
Definition: lua.cpp:3105
GLuint in
Definition: Glext.h:9087
void EndFrame()
Definition: scripting.cpp:952
script_state & operator=(script_state &in)
Definition: scripting.cpp:977
void ParseChunk(script_hook *dest, char *debug_str=NULL)
Definition: scripting.cpp:1287
class script_state Script_system
void scripting_state_init()
Definition: scripting.cpp:1425
char * filename
#define CHA_NONE
Definition: scripting.h:42
#define CONDITION_LENGTH
Definition: globals.h:25
char fname[MAX_FILENAME_LEN]
Definition: scripting.h:18
void SetHookObject(char *name, object *objp)
Definition: scripting.cpp:551
cfbp fp
Definition: cfile.cpp:1065
void script_init()
Definition: scripting.cpp:188
int CreateLuaState()
Definition: lua.cpp:15872
void scripting_state_do_frame(float frametime)
Definition: scripting.cpp:1446
GLuint const GLchar * name
Definition: Glext.h:5608
int RunCondition(int condition, char format='\0', void *data=NULL, class object *objp=NULL, int more_data=0)
Definition: scripting.cpp:924
script_state(char *name)
Definition: scripting.cpp:967
void SetHookObjects(int num,...)
Definition: scripting.cpp:556
int RunBytecode(script_hook &hd, char format='\0', void *data=NULL)
Definition: scripting.cpp:918
GLuint GLuint num
Definition: Glext.h:9089
int LoadBm(char *name)
Definition: scripting.cpp:818
bool ConditionsValid(int action, class object *objp=NULL, int more_data=0)
Definition: scripting.cpp:256
bool Run(class script_state *sys, int action, char format='\0', void *data=NULL)
Definition: scripting.cpp:514
script_hook Script_simulationhook
Definition: scripting.cpp:25
#define CHC_NONE
Definition: scripting.h:27
bool Output_scripting_meta
Definition: scripting.cpp:21
void scripting_state_close()
Definition: scripting.cpp:1436
lua_State * GetLuaSession()
Definition: scripting.h:174
void RemHookVars(unsigned int num,...)
Definition: scripting.cpp:754
script_hook Script_globalhook
Definition: scripting.cpp:27
GLenum GLsizei GLenum GLenum const GLvoid * data
Definition: Gl.h:1509
void SetHookVar(char *name, char format, void *data=NULL)
Definition: scripting.cpp:650
bool EvalString(const char *string, const char *format=NULL, void *rtn=NULL, const char *debug_str=NULL)
Definition: scripting.cpp:1083
script_hook Script_hudhook
Definition: scripting.cpp:26
script_hook hook
Definition: scripting.h:108
void UnloadImages()
Definition: scripting.cpp:838
void RemGlobal(char *name)
Definition: scripting.cpp:808
script_hook Script_splashhook
Definition: scripting.cpp:24
int OutputMeta(char *filename)
Definition: scripting.cpp:1008
bool IsConditionOverride(int action, object *objp=NULL)
Definition: scripting.cpp:938
void script_hook_init(script_hook *hook)
Definition: scripting.cpp:97
bool IsOverride(script_hook &hd)
Definition: scripting.cpp:1414