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 { 18 char fname[MAX_FILENAME_LEN]; 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 78 // management stuff 79 void scripting_state_init(); 80 void scripting_state_close(); 81 void scripting_state_do_frame(float frametime); 82 83 class script_condition 84 { 85 public: 86 int condition_type; 87 union 88 { 89 char name[CONDITION_LENGTH]; 90 } data; 91 script_condition()92 script_condition() 93 : condition_type(CHC_NONE) 94 { 95 memset(data.name, 0, sizeof(data.name)); 96 } 97 }; 98 99 class script_action 100 { 101 public: 102 int action_type; 103 script_hook hook; 104 script_action()105 script_action() 106 : action_type(CHA_NONE) 107 { 108 script_hook_init(&hook); 109 } 110 }; 111 112 class ConditionedHook 113 { 114 private: 115 SCP_vector<script_action> Actions; 116 script_condition Conditions[MAX_HOOK_CONDITIONS]; 117 public: 118 bool AddCondition(script_condition *sc); 119 bool AddAction(script_action *sa); 120 121 bool ConditionsValid(int action, class object *objp=NULL, int more_data = 0); 122 bool IsOverride(class script_state *sys, int action); 123 bool Run(class script_state *sys, int action, char format='\0', void *data=NULL); 124 }; 125 126 //**********Main script_state function 127 class script_state 128 { 129 private: 130 char StateName[32]; 131 132 int Langs; 133 struct lua_State *LuaState; 134 const struct script_lua_lib_list *LuaLibs; 135 136 //Utility variables 137 SCP_vector<image_desc> ScriptImages; 138 SCP_vector<ConditionedHook> ConditionalHooks; 139 140 private: 141 142 void ParseChunkSub(int *out_lang, int *out_index, char* debug_str=NULL); 143 int RunBytecodeSub(int in_lang, int in_idx, char format='\0', void *data=NULL); 144 145 void SetLuaSession(struct lua_State *L); 146 147 void OutputLuaMeta(FILE *fp); 148 149 //Lua private helper functions 150 bool OpenHookVarTable(); 151 bool CloseHookVarTable(); 152 153 //Internal Lua helper functions 154 void EndLuaFrame(); 155 156 //Destroy everything 157 void Clear(); 158 159 public: 160 //***Init/Deinit 161 script_state(char *name); 162 script_state& operator=(script_state &in); 163 ~script_state(); 164 165 //***Internal scripting stuff 166 int LoadBm(char *name); 167 void UnloadImages(); 168 GetLuaSession()169 lua_State *GetLuaSession(){return LuaState;} 170 171 //***Init functions for langs 172 int CreateLuaState(); 173 174 //***Get data 175 int OutputMeta(char *filename); 176 177 //***Moves data 178 //void MoveData(script_state &in); 179 180 //***Variable handling functions 181 bool GetGlobal(char *name, char format='\0', void *data=NULL); 182 void RemGlobal(char *name); 183 184 void SetHookVar(char *name, char format, void *data=NULL); 185 void SetHookObject(char *name, object *objp); 186 void SetHookObjects(int num, ...); 187 bool GetHookVar(char *name, char format='\0', void *data=NULL); 188 void RemHookVar(char *name); 189 void RemHookVars(unsigned int num, ...); 190 191 //***Hook creation functions 192 bool EvalString(char* string, char *format=NULL, void *rtn=NULL, char *debug_str=NULL); 193 void ParseChunk(script_hook *dest, char* debug_str=NULL); 194 bool ParseCondition(const char *filename="<Unknown>"); 195 196 //***Hook running functions 197 int RunBytecode(script_hook &hd, char format='\0', void *data=NULL); 198 bool IsOverride(script_hook &hd); 199 int RunCondition(int condition, char format='\0', void *data=NULL, class object *objp = NULL, int more_data = 0); 200 bool IsConditionOverride(int action, object *objp=NULL); 201 202 //*****Other functions 203 void EndFrame(); 204 }; 205 206 207 //**********Script registration functions 208 void script_init(); 209 210 //**********Script globals 211 extern class script_state Script_system; 212 extern bool Output_scripting_meta; 213 214 //**********Script hook stuff (scripting.tbl) 215 extern script_hook Script_globalhook; 216 extern script_hook Script_simulationhook; 217 extern script_hook Script_hudhook; 218 extern script_hook Script_splashhook; 219 extern script_hook Script_gameinithook; 220 221 //*************************Conditional scripting************************* 222 223 #endif //_SCRIPTING_H 224