LuaInterface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef LUA_INTERFACE_H
  17. #define LUA_INTERFACE_H
  18. #include "Spawn.h"
  19. #include "Spells.h"
  20. #include "../common/Mutex.h"
  21. #include "Quests.h"
  22. #include "zoneserver.h"
  23. #include "client.h"
  24. #include "../LUA/lua.hpp"
  25. using namespace std;
  26. struct ConversationOption{
  27. string option;
  28. string function;
  29. };
  30. struct OptionWindowOption {
  31. string optionName;
  32. string optionDescription;
  33. string optionCommand;
  34. int32 optionIconSheet;
  35. int16 optionIconID;
  36. string optionConfirmTitle;
  37. };
  38. //Bitmask Values
  39. #define EFFECT_FLAG_STUN 1
  40. #define EFFECT_FLAG_ROOT 2
  41. #define EFFECT_FLAG_MEZ 4
  42. #define EFFECT_FLAG_STIFLE 8
  43. #define EFFECT_FLAG_DAZE 16
  44. #define EFFECT_FLAG_FEAR 32
  45. #define EFFECT_FLAG_SPELLBONUS 64
  46. #define EFFECT_FLAG_SKILLBONUS 128
  47. #define EFFECT_FLAG_STEALTH 256
  48. #define EFFECT_FLAG_INVIS 512
  49. #define EFFECT_FLAG_SNARE 1024
  50. #define EFFECT_FLAG_WATERWALK 2048
  51. #define EFFECT_FLAG_WATERJUMP 4096
  52. #define EFFECT_FLAG_FLIGHT 8192
  53. #define EFFECT_FLAG_GLIDE 16384
  54. #define EFFECT_FLAG_AOE_IMMUNE 32768
  55. #define EFFECT_FLAG_STUN_IMMUNE 65536
  56. #define EFFECT_FLAG_MEZ_IMMUNE 131072
  57. #define EFFECT_FLAG_DAZE_IMMUNE 262144
  58. #define EFFECT_FLAG_ROOT_IMMUNE 524288
  59. #define EFFECT_FLAG_STIFLE_IMMUNE 1048576
  60. #define EFFECT_FLAG_FEAR_IMMUNE 2097152
  61. #define EFFECT_FLAG_SAFEFALL 4194304
  62. struct LuaSpell{
  63. Entity* caster;
  64. int32 initial_caster_char_id;
  65. int32 initial_target;
  66. int32 initial_target_char_id;
  67. vector<int32> targets;
  68. vector<int32> removed_targets; // previously cancelled, expired, used, so on
  69. multimap<int32, int8> char_id_targets;
  70. Spell* spell;
  71. lua_State* state;
  72. string file_name;
  73. Timer timer;
  74. bool is_recast_timer;
  75. int16 num_calls;
  76. int16 num_triggers;
  77. int8 slot_pos;
  78. int32 damage_remaining;
  79. bool resisted;
  80. bool interrupted;
  81. bool crit;
  82. bool last_spellattack_hit;
  83. bool cancel_after_all_triggers;
  84. bool had_triggers;
  85. bool had_dmg_remaining;
  86. Mutex MSpellTargets;
  87. int32 effect_bitmask;
  88. bool restored; // restored spell cross zone
  89. };
  90. class LUAUserData{
  91. public:
  92. LUAUserData();
  93. virtual ~LUAUserData(){};
  94. virtual bool IsCorrectlyInitialized();
  95. virtual bool IsConversationOption();
  96. virtual bool IsSpawnList();
  97. virtual bool IsOptionWindow();
  98. virtual bool IsSpawn();
  99. virtual bool IsQuest();
  100. virtual bool IsZone();
  101. virtual bool IsItem();
  102. virtual bool IsSkill();
  103. virtual bool IsSpell();
  104. bool correctly_initialized;
  105. Item* item;
  106. ZoneServer* zone;
  107. Spawn* spawn;
  108. vector<ConversationOption>* conversation_options;
  109. vector<OptionWindowOption>* option_window_option;
  110. vector<Spawn*>* spawn_list;
  111. Quest* quest;
  112. Skill* skill;
  113. LuaSpell* spell;
  114. };
  115. class LUAConversationOptionWrapper : public LUAUserData{
  116. public:
  117. LUAConversationOptionWrapper();
  118. bool IsConversationOption();
  119. };
  120. class LUASpawnListWrapper: public LUAUserData{
  121. public:
  122. LUASpawnListWrapper();
  123. ~LUASpawnListWrapper() { safe_delete(spawn_list); }
  124. bool IsSpawnList();
  125. };
  126. class LUAOptionWindowWrapper : public LUAUserData {
  127. public:
  128. LUAOptionWindowWrapper();
  129. bool IsOptionWindow();
  130. };
  131. class LUASpawnWrapper : public LUAUserData{
  132. public:
  133. LUASpawnWrapper();
  134. bool IsSpawn();
  135. };
  136. class LUAZoneWrapper : public LUAUserData{
  137. public:
  138. LUAZoneWrapper();
  139. bool IsZone();
  140. };
  141. class LUAQuestWrapper : public LUAUserData{
  142. public:
  143. LUAQuestWrapper();
  144. bool IsQuest();
  145. };
  146. class LUAItemWrapper : public LUAUserData{
  147. public:
  148. LUAItemWrapper();
  149. bool IsItem();
  150. };
  151. class LUASkillWrapper: public LUAUserData {
  152. public:
  153. LUASkillWrapper();
  154. bool IsSkill();
  155. };
  156. class LUASpellWrapper : public LUAUserData {
  157. public:
  158. LUASpellWrapper();
  159. bool IsSpell();
  160. };
  161. class LuaInterface {
  162. public:
  163. LuaInterface();
  164. ~LuaInterface();
  165. int GetNumberOfArgs(lua_State* state);
  166. bool LoadLuaSpell(const char* name);
  167. bool LoadLuaSpell(string name);
  168. bool LoadItemScript(string name);
  169. bool LoadItemScript(const char* name);
  170. bool LoadSpawnScript(string name);
  171. bool LoadSpawnScript(const char* name);
  172. bool LoadZoneScript(string name);
  173. bool LoadZoneScript(const char* name);
  174. bool LoadRegionScript(string name);
  175. bool LoadRegionScript(const char* name);
  176. void RemoveSpell(LuaSpell* spell, bool call_remove_function = true, bool can_delete = true, string reason = "", bool removing_all_spells = false);
  177. Spawn* GetSpawn(lua_State* state, int8 arg_num = 1);
  178. Item* GetItem(lua_State* state, int8 arg_num = 1);
  179. Quest* GetQuest(lua_State* state, int8 arg_num = 1);
  180. ZoneServer* GetZone(lua_State* state, int8 arg_num = 1);
  181. Skill* GetSkill(lua_State* state, int8 arg_num = 1);
  182. LuaSpell* GetSpell(lua_State* state, int8 arg_num = 1);
  183. vector<ConversationOption>* GetConversation(lua_State* state, int8 arg_num = 1);
  184. vector<Spawn*>* GetSpawnList(lua_State* state, int8 arg_num = 1);
  185. vector<OptionWindowOption>* GetOptionWindow(lua_State* state, int8 arg_num = 1);
  186. int8 GetInt8Value(lua_State* state, int8 arg_num = 1);
  187. int16 GetInt16Value(lua_State* state, int8 arg_num = 1);
  188. int32 GetInt32Value(lua_State* state, int8 arg_num = 1);
  189. sint32 GetSInt32Value(lua_State* state, int8 arg_num = 1);
  190. int64 GetInt64Value(lua_State* state, int8 arg_num = 1);
  191. sint64 GetSInt64Value(lua_State* state, int8 arg_num = 1);
  192. float GetFloatValue(lua_State* state, int8 arg_num = 1);
  193. string GetStringValue(lua_State* state, int8 arg_num = 1);
  194. bool GetBooleanValue(lua_State*state, int8 arg_num = 1);
  195. void Process();
  196. void SetInt32Value(lua_State* state, int32 value);
  197. void SetSInt32Value(lua_State* state, sint32 value);
  198. void SetInt64Value(lua_State* state, int64 value);
  199. void SetSInt64Value(lua_State* state, sint64 value);
  200. void SetFloatValue(lua_State* state, float value);
  201. void SetBooleanValue(lua_State* state, bool value);
  202. void SetStringValue(lua_State* state, const char* value);
  203. void SetSpawnValue(lua_State* state, Spawn* spawn);
  204. void SetSkillValue(lua_State* state, Skill* skill);
  205. void SetItemValue(lua_State* state, Item* item);
  206. void SetQuestValue(lua_State* state, Quest* quest);
  207. void SetZoneValue(lua_State* state, ZoneServer* zone);
  208. void SetSpawnListValue(lua_State* state, vector<Spawn*>* spawnList);
  209. void SetSpellValue(lua_State* state, LuaSpell* spell);
  210. void SetConversationValue(lua_State* state, vector<ConversationOption>* conversation);
  211. void SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow);
  212. std::string AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast = false, const char* function = 0, SpellScriptTimer* timer = 0, bool passLuaSpell=false, Spawn* altTarget = 0);
  213. LuaSpell* GetCurrentSpell(lua_State* state);
  214. bool CallSpellProcess(LuaSpell* spell, int8 num_parameters, std::string functionCalled);
  215. LuaSpell* GetSpell(const char* name);
  216. void UseItemScript(const char* name, lua_State* state, bool val);
  217. void UseSpawnScript(const char* name, lua_State* state, bool val);
  218. void UseZoneScript(const char* name, lua_State* state, bool val);
  219. void UseRegionScript(const char* name, lua_State* state, bool val);
  220. lua_State* GetItemScript(const char* name, bool create_new = true, bool use = false);
  221. lua_State* GetSpawnScript(const char* name, bool create_new = true, bool use = false);
  222. lua_State* GetZoneScript(const char* name, bool create_new = true, bool use = false);
  223. lua_State* GetRegionScript(const char* name, bool create_new = true, bool use = false);
  224. Quest* LoadQuest(int32 id, const char* name, const char* type, const char* zone, int8 level, const char* description, char* script_name);
  225. const char* GetScriptName(lua_State* state);
  226. void RemoveSpawnScript(const char* name);
  227. bool RunItemScript(string script_name, const char* function_name, Item* item, Spawn* spawn = 0, sint64* returnValue = 0);
  228. bool RunItemScriptWithReturnString(string script_name, const char* function_name, Item* item, Spawn* spawn = 0, std::string* returnValue = 0);
  229. bool CallItemScript(lua_State* state, int8 num_parameters, std::string* returnValue = 0);
  230. bool CallItemScript(lua_State* state, int8 num_parameters, sint64* returnValue = 0);
  231. bool RunSpawnScript(string script_name, const char* function_name, Spawn* npc, Spawn* spawn = 0, const char* message = 0, bool is_door_open = false, sint32 input_value = 0, sint32* return_value = 0);
  232. bool CallSpawnScript(lua_State* state, int8 num_parameters);
  233. bool RunZoneScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn = 0, int32 int32_arg1 = 0, const char* str_arg1 = 0, Spawn* spawn_arg1 = 0, int32 int32_arg2 = 0, const char* str_arg2 = 0, Spawn* spawn_arg2 = 0);
  234. bool RunZoneScriptWithReturn(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, int32 int32_arg1, int32 int32_arg2, int32 int32_arg3, int32* returnValue = 0);
  235. bool CallScriptInt32(lua_State* state, int8 num_parameters, int32* returnValue = 0);
  236. bool CallScriptSInt32(lua_State* state, int8 num_parameters, sint32* returnValue = 0);
  237. bool RunRegionScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn = 0, sint32 int32_arg1 = 0, int32* returnValue = 0);
  238. bool CallRegionScript(lua_State* state, int8 num_parameters, int32* returnValue);
  239. void ResetFunctionStack(lua_State* state);
  240. void DestroySpells();
  241. void DestroySpawnScripts();
  242. void DestroyItemScripts();
  243. void ReloadSpells();
  244. void DestroyQuests(bool reload = false);
  245. void DestroyZoneScripts();
  246. void DestroyRegionScripts();
  247. void SimpleLogError(const char* error);
  248. void LogError(const char* error, ...);
  249. void CallQuestFunction(Quest* quest, const char* function, Spawn* player, int32 step_id = 0xFFFFFFFF);
  250. void RemoveDebugClients(Client* client);
  251. void UpdateDebugClients(Client* client);
  252. void ProcessErrorMessage(const char* message);
  253. map<Client*, int32> GetDebugClients(){ return debug_clients; }
  254. void AddUserDataPtr(LUAUserData* data);
  255. void DeleteUserDataPtrs(bool all);
  256. void DeletePendingSpells(bool all);
  257. void DeletePendingSpell(LuaSpell* spell);
  258. Mutex* GetSpawnScriptMutex(const char* name);
  259. Mutex* GetItemScriptMutex(const char* name);
  260. Mutex* GetZoneScriptMutex(const char* name);
  261. Mutex* GetRegionScriptMutex(const char* name);
  262. Mutex* GetQuestMutex(Quest* quest);
  263. void SetLuaSystemReloading(bool val) { lua_system_reloading = val; }
  264. bool IsLuaSystemReloading() { return lua_system_reloading; }
  265. void AddPendingSpellDelete(LuaSpell* spell);
  266. void AddCustomSpell(LuaSpell* spell);
  267. void RemoveCustomSpell(int32 id);
  268. void FindCustomSpellLock() { MCustomSpell.readlock(); }
  269. void FindCustomSpellUnlock() { MCustomSpell.releasereadlock(); }
  270. LuaSpell* FindCustomSpell(int32 id);
  271. int32 GetFreeCustomSpellID();
  272. private:
  273. bool shutting_down;
  274. bool lua_system_reloading;
  275. map<LuaSpell*, int32> spells_pending_delete;
  276. Timer* user_data_timer;
  277. Timer* spell_delete_timer;
  278. map<LUAUserData*, int32> user_data;
  279. map<Client*, int32> debug_clients;
  280. map<lua_State*, LuaSpell*> current_spells;
  281. vector<string>* GetDirectoryListing(const char* directory);
  282. lua_State* LoadLuaFile(const char* name);
  283. void RegisterFunctions(lua_State* state);
  284. map<string, LuaSpell*> spells;
  285. map<lua_State*, string> inverse_spells;
  286. map<int32, Quest*> quests;
  287. map<int32, lua_State*> quest_states;
  288. map<string, map<lua_State*, bool> > item_scripts;
  289. map<string, map<lua_State*, bool> > spawn_scripts;
  290. map<string, map<lua_State*, bool> > zone_scripts;
  291. map<string, map<lua_State*, bool> > region_scripts;
  292. map<int32, LuaSpell*> custom_spells;
  293. std::deque<int32> custom_free_spell_ids;
  294. map<lua_State*, string> item_inverse_scripts;
  295. map<lua_State*, string> spawn_inverse_scripts;
  296. map<lua_State*, string> zone_inverse_scripts;
  297. map<lua_State*, string> region_inverse_scripts;
  298. map<string, Mutex*> item_scripts_mutex;
  299. map<string, Mutex*> spawn_scripts_mutex;
  300. map<string, Mutex*> zone_scripts_mutex;
  301. map<int32, Mutex*> quests_mutex;
  302. map<string, Mutex*> region_scripts_mutex;
  303. Mutex MDebugClients;
  304. Mutex MSpells;
  305. Mutex MSpawnScripts;
  306. Mutex MItemScripts;
  307. Mutex MZoneScripts;
  308. Mutex MQuests;
  309. Mutex MLUAUserData;
  310. Mutex MLUAMain;
  311. Mutex MSpellDelete;
  312. Mutex MCustomSpell;
  313. Mutex MRegionScripts;
  314. };
  315. #endif