LuaInterface.h 13 KB

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