LuaInterface.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_target;
  65. vector<int32> targets;
  66. Spell* spell;
  67. lua_State* state;
  68. string file_name;
  69. Timer timer;
  70. int16 num_calls;
  71. int16 num_triggers;
  72. int8 slot_pos;
  73. int32 damage_remaining;
  74. bool resisted;
  75. bool interrupted;
  76. bool crit;
  77. bool last_spellattack_hit;
  78. bool cancel_after_all_triggers;
  79. bool had_triggers;
  80. bool had_dmg_remaining;
  81. Mutex MSpellTargets;
  82. int32 effect_bitmask;
  83. };
  84. class LUAUserData{
  85. public:
  86. LUAUserData();
  87. virtual ~LUAUserData(){};
  88. virtual bool IsCorrectlyInitialized();
  89. virtual bool IsConversationOption();
  90. virtual bool IsSpawnList();
  91. virtual bool IsOptionWindow();
  92. virtual bool IsSpawn();
  93. virtual bool IsQuest();
  94. virtual bool IsZone();
  95. virtual bool IsItem();
  96. virtual bool IsSkill();
  97. virtual bool IsSpell();
  98. bool correctly_initialized;
  99. Item* item;
  100. ZoneServer* zone;
  101. Spawn* spawn;
  102. vector<ConversationOption>* conversation_options;
  103. vector<OptionWindowOption>* option_window_option;
  104. vector<Spawn*>* spawn_list;
  105. Quest* quest;
  106. Skill* skill;
  107. LuaSpell* spell;
  108. };
  109. class LUAConversationOptionWrapper : public LUAUserData{
  110. public:
  111. LUAConversationOptionWrapper();
  112. bool IsConversationOption();
  113. };
  114. class LUASpawnListWrapper: public LUAUserData{
  115. public:
  116. LUASpawnListWrapper();
  117. ~LUASpawnListWrapper() { safe_delete(spawn_list); }
  118. bool IsSpawnList();
  119. };
  120. class LUAOptionWindowWrapper : public LUAUserData {
  121. public:
  122. LUAOptionWindowWrapper();
  123. bool IsOptionWindow();
  124. };
  125. class LUASpawnWrapper : public LUAUserData{
  126. public:
  127. LUASpawnWrapper();
  128. bool IsSpawn();
  129. };
  130. class LUAZoneWrapper : public LUAUserData{
  131. public:
  132. LUAZoneWrapper();
  133. bool IsZone();
  134. };
  135. class LUAQuestWrapper : public LUAUserData{
  136. public:
  137. LUAQuestWrapper();
  138. bool IsQuest();
  139. };
  140. class LUAItemWrapper : public LUAUserData{
  141. public:
  142. LUAItemWrapper();
  143. bool IsItem();
  144. };
  145. class LUASkillWrapper: public LUAUserData {
  146. public:
  147. LUASkillWrapper();
  148. bool IsSkill();
  149. };
  150. class LUASpellWrapper : public LUAUserData {
  151. public:
  152. LUASpellWrapper();
  153. bool IsSpell();
  154. };
  155. class LuaInterface {
  156. public:
  157. LuaInterface();
  158. ~LuaInterface();
  159. int GetNumberOfArgs(lua_State* state);
  160. bool LoadLuaSpell(const char* name);
  161. bool LoadLuaSpell(string name);
  162. bool LoadItemScript(string name);
  163. bool LoadItemScript(const char* name);
  164. bool LoadSpawnScript(string name);
  165. bool LoadSpawnScript(const char* name);
  166. bool LoadZoneScript(string name);
  167. bool LoadZoneScript(const char* name);
  168. void RemoveSpell(LuaSpell* spell, bool call_remove_function = true, bool can_delete = true, string reason = "");
  169. Spawn* GetSpawn(lua_State* state, int8 arg_num = 1);
  170. Item* GetItem(lua_State* state, int8 arg_num = 1);
  171. Quest* GetQuest(lua_State* state, int8 arg_num = 1);
  172. ZoneServer* GetZone(lua_State* state, int8 arg_num = 1);
  173. Skill* GetSkill(lua_State* state, int8 arg_num = 1);
  174. LuaSpell* GetSpell(lua_State* state, int8 arg_num = 1);
  175. vector<ConversationOption>* GetConversation(lua_State* state, int8 arg_num = 1);
  176. vector<Spawn*>* GetSpawnList(lua_State* state, int8 arg_num = 1);
  177. vector<OptionWindowOption>* GetOptionWindow(lua_State* state, int8 arg_num = 1);
  178. int8 GetInt8Value(lua_State* state, int8 arg_num = 1);
  179. int16 GetInt16Value(lua_State* state, int8 arg_num = 1);
  180. int32 GetInt32Value(lua_State* state, int8 arg_num = 1);
  181. sint32 GetSInt32Value(lua_State* state, int8 arg_num = 1);
  182. float GetFloatValue(lua_State* state, int8 arg_num = 1);
  183. string GetStringValue(lua_State* state, int8 arg_num = 1);
  184. bool GetBooleanValue(lua_State*state, int8 arg_num = 1);
  185. void Process();
  186. void SetInt32Value(lua_State* state, int32 value);
  187. void SetSInt32Value(lua_State* state, sint32 value);
  188. void SetFloatValue(lua_State* state, float value);
  189. void SetBooleanValue(lua_State* state, bool value);
  190. void SetStringValue(lua_State* state, const char* value);
  191. void SetSpawnValue(lua_State* state, Spawn* spawn);
  192. void SetSkillValue(lua_State* state, Skill* skill);
  193. void SetItemValue(lua_State* state, Item* item);
  194. void SetQuestValue(lua_State* state, Quest* quest);
  195. void SetZoneValue(lua_State* state, ZoneServer* zone);
  196. void SetSpawnListValue(lua_State* state, vector<Spawn*>* spawnList);
  197. void SetSpellValue(lua_State* state, LuaSpell* spell);
  198. void SetConversationValue(lua_State* state, vector<ConversationOption>* conversation);
  199. void SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow);
  200. void AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast = false, const char* function = 0, SpellScriptTimer* timer = 0, bool passLuaSpell=false);
  201. LuaSpell* GetCurrentSpell(lua_State* state);
  202. bool CallSpellProcess(LuaSpell* spell, int8 num_parameters);
  203. LuaSpell* GetSpell(const char* name);
  204. void UseItemScript(const char* name, lua_State* state, bool val);
  205. void UseSpawnScript(const char* name, lua_State* state, bool val);
  206. void UseZoneScript(const char* name, lua_State* state, bool val);
  207. lua_State* GetItemScript(const char* name, bool create_new = true, bool use = false);
  208. lua_State* GetSpawnScript(const char* name, bool create_new = true, bool use = false);
  209. lua_State* GetZoneScript(const char* name, bool create_new = true, bool use = false);
  210. Quest* LoadQuest(int32 id, const char* name, const char* type, const char* zone, int8 level, const char* description, char* script_name);
  211. const char* GetScriptName(lua_State* state);
  212. void RemoveSpawnScript(const char* name);
  213. bool RunItemScript(string script_name, const char* function_name, Item* item, Spawn* spawn = 0);
  214. bool CallItemScript(lua_State* state, int8 num_parameters);
  215. bool RunSpawnScript(string script_name, const char* function_name, Spawn* npc, Spawn* spawn = 0, const char* message = 0);
  216. bool CallSpawnScript(lua_State* state, int8 num_parameters);
  217. 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);
  218. bool CallZoneScript(lua_State* state, int8 num_parameters);
  219. void ResetFunctionStack(lua_State* state);
  220. void DestroySpells();
  221. void DestroySpawnScripts();
  222. void DestroyItemScripts();
  223. void ReloadSpells();
  224. void DestroyQuests(bool reload = false);
  225. void DestroyZoneScripts();
  226. void SimpleLogError(const char* error);
  227. void LogError(const char* error, ...);
  228. void CallQuestFunction(Quest* quest, const char* function, Spawn* player, int32 step_id = 0xFFFFFFFF);
  229. void RemoveDebugClients(Client* client);
  230. void UpdateDebugClients(Client* client);
  231. void ProcessErrorMessage(const char* message);
  232. map<Client*, int32> GetDebugClients(){ return debug_clients; }
  233. void AddUserDataPtr(LUAUserData* data);
  234. void DeleteUserDataPtrs(bool all);
  235. void DeletePendingSpells(bool all);
  236. void DeletePendingSpell(LuaSpell* spell);
  237. Mutex* GetSpawnScriptMutex(const char* name);
  238. Mutex* GetItemScriptMutex(const char* name);
  239. Mutex* GetZoneScriptMutex(const char* name);
  240. Mutex* GetQuestMutex(Quest* quest);
  241. void SetSpawnScriptsReloading(bool val) { spawn_scripts_reloading = val; }
  242. void AddPendingSpellDelete(LuaSpell* spell);
  243. void AddCustomSpell(LuaSpell* spell);
  244. void RemoveCustomSpell(int32 id);
  245. void FindCustomSpellLock() { MCustomSpell.readlock(); }
  246. void FindCustomSpellUnlock() { MCustomSpell.releasereadlock(); }
  247. LuaSpell* FindCustomSpell(int32 id);
  248. int32 GetFreeCustomSpellID();
  249. private:
  250. bool shutting_down;
  251. bool spawn_scripts_reloading;
  252. map<LuaSpell*, int32> spells_pending_delete;
  253. Timer* user_data_timer;
  254. Timer* spell_delete_timer;
  255. map<LUAUserData*, int32> user_data;
  256. map<Client*, int32> debug_clients;
  257. map<lua_State*, LuaSpell*> current_spells;
  258. vector<string>* GetDirectoryListing(const char* directory);
  259. lua_State* LoadLuaFile(const char* name);
  260. void RegisterFunctions(lua_State* state);
  261. map<string, LuaSpell*> spells;
  262. map<lua_State*, string> inverse_spells;
  263. map<int32, Quest*> quests;
  264. map<int32, lua_State*> quest_states;
  265. map<string, map<lua_State*, bool> > item_scripts;
  266. map<string, map<lua_State*, bool> > spawn_scripts;
  267. map<string, map<lua_State*, bool> > zone_scripts;
  268. map<int32, LuaSpell*> custom_spells;
  269. std::deque<int32> custom_free_spell_ids;
  270. map<lua_State*, string> item_inverse_scripts;
  271. map<lua_State*, string> spawn_inverse_scripts;
  272. map<lua_State*, string> zone_inverse_scripts;
  273. map<string, Mutex*> item_scripts_mutex;
  274. map<string, Mutex*> spawn_scripts_mutex;
  275. map<string, Mutex*> zone_scripts_mutex;
  276. map<int32, Mutex*> quests_mutex;
  277. Mutex MDebugClients;
  278. Mutex MSpells;
  279. Mutex MSpawnScripts;
  280. Mutex MItemScripts;
  281. Mutex MZoneScripts;
  282. Mutex MQuests;
  283. Mutex MLUAUserData;
  284. Mutex MLUAMain;
  285. Mutex MSpellDelete;
  286. Mutex MCustomSpell;
  287. };
  288. #endif