LuaInterface.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663
  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. #include "LuaInterface.h"
  17. #include "LuaFunctions.h"
  18. #include "WorldDatabase.h"
  19. #include "SpellProcess.h"
  20. #include "../common/Log.h"
  21. #include "World.h"
  22. #ifndef WIN32
  23. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <dirent.h>
  27. #include <pthread.h>
  28. #else
  29. #include <process.h>
  30. #endif
  31. extern WorldDatabase database;
  32. extern ZoneList zone_list;
  33. LuaInterface::LuaInterface() {
  34. shutting_down = false;
  35. lua_system_reloading = false;
  36. MDebugClients.SetName("LuaInterface::MDebugClients");
  37. MSpells.SetName("LuaInterface::MSpells");
  38. MSpawnScripts.SetName("LuaInterface::MSpawnScripts");
  39. MZoneScripts.SetName("LuaInterface::MZoneScripts");
  40. MQuests.SetName("LuaInterface::MQuests");
  41. MLUAUserData.SetName("LuaInterface::MLUAUserData");
  42. MLUAMain.SetName("LuaInterface::MLUAMain");
  43. MItemScripts.SetName("LuaInterface::MItemScripts");
  44. MSpellDelete.SetName("LuaInterface::MSpellDelete");
  45. MCustomSpell.SetName("LuaInterface::MCustomSpell");
  46. MRegionScripts.SetName("LuaInterface::MRegionScripts");
  47. user_data_timer = new Timer(20000);
  48. user_data_timer->Start();
  49. spell_delete_timer = new Timer(5000);
  50. spell_delete_timer->Start();
  51. }
  52. #ifdef WIN32
  53. vector<string>* LuaInterface::GetDirectoryListing(const char* directory) {
  54. vector<string>* ret = new vector<string>;
  55. WIN32_FIND_DATA fdata;
  56. HANDLE dhandle;
  57. char buf[MAX_PATH];
  58. snprintf(buf, sizeof(buf), "%s\\*", directory);
  59. if((dhandle = FindFirstFile(buf, &fdata)) == INVALID_HANDLE_VALUE) {
  60. safe_delete(ret);
  61. return 0;
  62. }
  63. ret->push_back(string(fdata.cFileName));
  64. while(1) {
  65. if(FindNextFile(dhandle, &fdata)) {
  66. ret->push_back(string(fdata.cFileName));
  67. }
  68. else{
  69. if(GetLastError() == ERROR_NO_MORE_FILES) {
  70. break;
  71. } else {
  72. safe_delete(ret);
  73. FindClose(dhandle);
  74. return 0;
  75. }
  76. }
  77. }
  78. if(FindClose(dhandle) == 0) {
  79. safe_delete(ret);
  80. return 0;
  81. }
  82. return ret;
  83. }
  84. #else
  85. vector<string>* LuaInterface::GetDirectoryListing(const char* directory) {
  86. vector<string>* ret = new vector<string>;
  87. DIR *dp;
  88. struct dirent *ep;
  89. dp = opendir (directory);
  90. if (dp != NULL){
  91. while ((ep = readdir (dp)))
  92. ret->push_back(string(ep->d_name));
  93. (void) closedir (dp);
  94. }
  95. else {
  96. safe_delete(ret);
  97. return 0;
  98. }
  99. return ret;
  100. }
  101. #endif
  102. LuaInterface::~LuaInterface() {
  103. shutting_down = true;
  104. MLUAMain.lock();
  105. DestroySpells();
  106. DestroySpawnScripts();
  107. DestroyQuests();
  108. DestroyItemScripts();
  109. DestroyZoneScripts();
  110. DestroyRegionScripts();
  111. DeleteUserDataPtrs(true);
  112. DeletePendingSpells(true);
  113. safe_delete(user_data_timer);
  114. safe_delete(spell_delete_timer);
  115. MLUAMain.unlock();
  116. }
  117. int LuaInterface::GetNumberOfArgs(lua_State* state) {
  118. return lua_gettop(state);
  119. }
  120. void LuaInterface::Process() {
  121. if(shutting_down)
  122. return;
  123. MLUAMain.lock();
  124. if(user_data_timer && user_data_timer->Check())
  125. DeleteUserDataPtrs(false);
  126. if(spell_delete_timer && spell_delete_timer->Check())
  127. DeletePendingSpells(false);
  128. MLUAMain.unlock();
  129. }
  130. void LuaInterface::DestroySpells() {
  131. map<string, LuaSpell*>::iterator itr;
  132. MSpells.lock();
  133. for(itr = spells.begin(); itr != spells.end(); itr++){
  134. lua_close(itr->second->state);
  135. safe_delete(itr->second);
  136. }
  137. spells.clear();
  138. MSpells.unlock();
  139. }
  140. void LuaInterface::DestroyQuests(bool reload) {
  141. map<int32, lua_State*>::iterator itr;
  142. MQuests.lock();
  143. for(itr = quest_states.begin(); itr != quest_states.end(); itr++){
  144. safe_delete(quests[itr->first]);
  145. lua_close(itr->second);
  146. }
  147. quests.clear();
  148. quest_states.clear();
  149. map<int32, Mutex*>::iterator mutex_itr;
  150. for(mutex_itr = quests_mutex.begin(); mutex_itr != quests_mutex.end(); mutex_itr++){
  151. safe_delete(mutex_itr->second);
  152. }
  153. quests_mutex.clear();
  154. if(reload)
  155. database.LoadQuests();
  156. MQuests.unlock();
  157. }
  158. void LuaInterface::DestroyItemScripts() {
  159. map<string, map<lua_State*, bool> >::iterator itr;
  160. map<lua_State*, bool>::iterator state_itr;
  161. Mutex* mutex = 0;
  162. MItemScripts.writelock(__FUNCTION__, __LINE__);
  163. for(itr = item_scripts.begin(); itr != item_scripts.end(); itr++){
  164. mutex = GetItemScriptMutex(itr->first.c_str());
  165. mutex->writelock(__FUNCTION__, __LINE__);
  166. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  167. lua_close(state_itr->first);
  168. mutex->releasewritelock(__FUNCTION__, __LINE__);
  169. safe_delete(mutex);
  170. }
  171. item_scripts.clear();
  172. item_inverse_scripts.clear();
  173. item_scripts_mutex.clear();
  174. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  175. }
  176. void LuaInterface::DestroySpawnScripts() {
  177. map<string, map<lua_State*, bool> >::iterator itr;
  178. map<lua_State*, bool>::iterator state_itr;
  179. Mutex* mutex = 0;
  180. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  181. for(itr = spawn_scripts.begin(); itr != spawn_scripts.end(); itr++){
  182. mutex = GetSpawnScriptMutex(itr->first.c_str());
  183. mutex->writelock(__FUNCTION__, __LINE__);
  184. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  185. lua_close(state_itr->first);
  186. mutex->releasewritelock(__FUNCTION__, __LINE__);
  187. safe_delete(mutex);
  188. }
  189. spawn_scripts.clear();
  190. spawn_inverse_scripts.clear();
  191. spawn_scripts_mutex.clear();
  192. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  193. }
  194. void LuaInterface::DestroyZoneScripts() {
  195. map<string, map<lua_State*, bool> >::iterator itr;
  196. map<lua_State*, bool>::iterator state_itr;
  197. Mutex* mutex = 0;
  198. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  199. for (itr = zone_scripts.begin(); itr != zone_scripts.end(); itr++){
  200. mutex = GetZoneScriptMutex(itr->first.c_str());
  201. mutex->writelock(__FUNCTION__, __LINE__);
  202. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  203. lua_close(state_itr->first);
  204. mutex->releasewritelock(__FUNCTION__, __LINE__);
  205. safe_delete(mutex);
  206. }
  207. zone_scripts.clear();
  208. zone_inverse_scripts.clear();
  209. zone_scripts_mutex.clear();
  210. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  211. }
  212. void LuaInterface::DestroyRegionScripts() {
  213. map<string, map<lua_State*, bool> >::iterator itr;
  214. map<lua_State*, bool>::iterator state_itr;
  215. Mutex* mutex = 0;
  216. MRegionScripts.writelock(__FUNCTION__, __LINE__);
  217. for (itr = region_scripts.begin(); itr != region_scripts.end(); itr++){
  218. mutex = GetRegionScriptMutex(itr->first.c_str());
  219. mutex->writelock(__FUNCTION__, __LINE__);
  220. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  221. lua_close(state_itr->first);
  222. mutex->releasewritelock(__FUNCTION__, __LINE__);
  223. safe_delete(mutex);
  224. }
  225. region_scripts.clear();
  226. region_inverse_scripts.clear();
  227. region_scripts_mutex.clear();
  228. MRegionScripts.releasewritelock(__FUNCTION__, __LINE__);
  229. }
  230. void LuaInterface::ReloadSpells() {
  231. DestroySpells();
  232. database.LoadSpellScriptData();
  233. }
  234. bool LuaInterface::LoadLuaSpell(const char* name) {
  235. LuaSpell* spell = 0;
  236. string lua_script = string(name);
  237. if (lua_script.find(".lua") == string::npos)
  238. lua_script.append(".lua");
  239. lua_State* state = LoadLuaFile(lua_script.c_str());
  240. if(state){
  241. spell = new LuaSpell;
  242. spell->file_name = lua_script;
  243. spell->state = state;
  244. spell->spell = 0;
  245. spell->caster = 0;
  246. spell->initial_target = 0;
  247. spell->resisted = false;
  248. spell->interrupted = false;
  249. spell->last_spellattack_hit = false;
  250. spell->crit = false;
  251. spell->MSpellTargets.SetName("LuaSpell.MSpellTargets");
  252. spell->cancel_after_all_triggers = false;
  253. spell->num_triggers = 0;
  254. spell->num_calls = 0;
  255. spell->is_recast_timer = false;
  256. spell->had_triggers = false;
  257. spell->had_dmg_remaining = false;
  258. spell->slot_pos = 0;
  259. spell->damage_remaining = 0;
  260. spell->effect_bitmask = 0;
  261. spell->restored = false;
  262. spell->initial_caster_char_id = 0;
  263. spell->initial_target_char_id = 0;
  264. MSpells.lock();
  265. if (spells.count(lua_script) > 0) {
  266. lua_close(spells[lua_script]->state);
  267. safe_delete(spells[lua_script]);
  268. }
  269. spells[lua_script] = spell;
  270. MSpells.unlock();
  271. return true;
  272. }
  273. return false;
  274. }
  275. bool LuaInterface::LoadLuaSpell(string name) {
  276. return LoadLuaSpell(name.c_str());
  277. }
  278. bool LuaInterface::LoadItemScript(string name) {
  279. return LoadItemScript(name.c_str());
  280. }
  281. bool LuaInterface::LoadItemScript(const char* name) {
  282. bool ret = false;
  283. if(name){
  284. lua_State* state = LoadLuaFile(name);
  285. if(state){
  286. MItemScripts.writelock(__FUNCTION__, __LINE__);
  287. item_scripts[name][state] = false;
  288. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  289. ret = true;
  290. }
  291. }
  292. return ret;
  293. }
  294. bool LuaInterface::LoadSpawnScript(const char* name) {
  295. bool ret = false;
  296. if(name){
  297. lua_State* state = LoadLuaFile(name);
  298. if(state){
  299. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  300. spawn_scripts[name][state] = false;
  301. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  302. ret = true;
  303. }
  304. }
  305. return ret;
  306. }
  307. bool LuaInterface::LoadZoneScript(const char* name) {
  308. bool ret = false;
  309. if (name) {
  310. lua_State* state = LoadLuaFile(name);
  311. if (state) {
  312. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  313. zone_scripts[name][state] = false;
  314. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  315. ret = true;
  316. }
  317. }
  318. return ret;
  319. }
  320. bool LuaInterface::LoadRegionScript(const char* name) {
  321. bool ret = false;
  322. if (name) {
  323. lua_State* state = LoadLuaFile(name);
  324. if (state) {
  325. MRegionScripts.writelock(__FUNCTION__, __LINE__);
  326. region_scripts[name][state] = false;
  327. MRegionScripts.releasewritelock(__FUNCTION__, __LINE__);
  328. ret = true;
  329. }
  330. }
  331. return ret;
  332. }
  333. void LuaInterface::ProcessErrorMessage(const char* message) {
  334. MDebugClients.lock();
  335. vector<Client*> delete_clients;
  336. map<Client*, int32>::iterator itr;
  337. for(itr = debug_clients.begin(); itr != debug_clients.end(); itr++){
  338. if((Timer::GetCurrentTime2() - itr->second) > 60000)
  339. delete_clients.push_back(itr->first);
  340. else
  341. itr->first->Message(CHANNEL_COLOR_RED, "LUA Error: %s", message);
  342. }
  343. for(int32 i=0;i<delete_clients.size();i++)
  344. debug_clients.erase(delete_clients[i]);
  345. MDebugClients.unlock();
  346. }
  347. void LuaInterface::RemoveDebugClients(Client* client) {
  348. MDebugClients.lock();
  349. debug_clients.erase(client);
  350. MDebugClients.unlock();
  351. }
  352. void LuaInterface::UpdateDebugClients(Client* client) {
  353. MDebugClients.lock();
  354. debug_clients[client] = Timer::GetCurrentTime2();
  355. MDebugClients.unlock();
  356. }
  357. Mutex* LuaInterface::GetQuestMutex(Quest* quest) {
  358. Mutex* ret = 0;
  359. MQuests.lock();
  360. if(quests_mutex.count(quest->GetQuestID()) == 0){
  361. ret = new Mutex();
  362. quests_mutex[quest->GetQuestID()] = ret;
  363. ret->SetName(string("Quest::").append(quest->GetName()));
  364. }
  365. else
  366. ret = quests_mutex[quest->GetQuestID()];
  367. MQuests.unlock();
  368. return ret;
  369. }
  370. bool LuaInterface::CallQuestFunction(Quest* quest, const char* function, Spawn* player, int32 step_id, int32* returnValue) {
  371. if(shutting_down)
  372. return false;
  373. lua_State* state = 0;
  374. if(quest){
  375. LogWrite(LUA__DEBUG, 0, "LUA", "Quest: %s, function: %s", quest->GetName(), function);
  376. Mutex* mutex = GetQuestMutex(quest);
  377. mutex->readlock(__FUNCTION__, __LINE__);
  378. if(quest_states.count(quest->GetQuestID()) > 0)
  379. state = quest_states[quest->GetQuestID()];
  380. bool success = false; // if no state then we return false
  381. if(state){
  382. int8 arg_count = 3;
  383. lua_getglobal(state, function);
  384. if (!lua_isfunction(state, lua_gettop(state))){
  385. lua_pop(state, 1);
  386. mutex->releasereadlock(__FUNCTION__);
  387. return false;
  388. }
  389. SetQuestValue(state, quest);
  390. Spawn* spawn = player->GetZone()->GetSpawnByDatabaseID(quest->GetQuestGiver());
  391. SetSpawnValue(state, spawn);
  392. SetSpawnValue(state, player);
  393. if(step_id != 0xFFFFFFFF){
  394. SetInt32Value(state, step_id);
  395. arg_count++;
  396. }
  397. success = CallScriptInt32(state, arg_count, returnValue);
  398. }
  399. mutex->releasereadlock(__FUNCTION__, __LINE__);
  400. LogWrite(LUA__DEBUG, 0, "LUA", "Done!");
  401. return success;
  402. }
  403. return false;
  404. }
  405. Quest* LuaInterface::LoadQuest(int32 id, const char* name, const char* type, const char* zone, int8 level, const char* description, char* script_name) {
  406. if(shutting_down)
  407. return 0;
  408. lua_State* state = LoadLuaFile(script_name);
  409. Quest* quest = 0;
  410. if(state){
  411. quest = new Quest(id);
  412. if (name)
  413. quest->SetName(string(name));
  414. if (type)
  415. quest->SetType(string(type));
  416. if (zone)
  417. quest->SetZone(string(zone));
  418. quest->SetLevel(level);
  419. if (description)
  420. quest->SetDescription(string(description));
  421. lua_getglobal(state, "Init");
  422. SetQuestValue(state, quest);
  423. if(lua_pcall(state, 1, 0, 0) != 0){
  424. LogError("Error processing Quest \"%s\" (%u): %s", name ? name : "unknown", id, lua_tostring(state, -1));
  425. lua_pop(state, 1);
  426. safe_delete(quest);
  427. return 0;
  428. }
  429. if(!quest->GetName()){
  430. safe_delete(quest);
  431. return 0;
  432. }
  433. quest_states[id] = state;
  434. quests[id] = quest;
  435. }
  436. return quest;
  437. }
  438. const char* LuaInterface::GetScriptName(lua_State* state)
  439. {
  440. map<lua_State*, string>::iterator itr;
  441. MItemScripts.writelock(__FUNCTION__, __LINE__);
  442. itr = item_inverse_scripts.find(state);
  443. if (itr != item_inverse_scripts.end())
  444. {
  445. const char* scriptName = itr->second.c_str();
  446. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  447. return scriptName;
  448. }
  449. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  450. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  451. itr = spawn_inverse_scripts.find(state);
  452. if (itr != spawn_inverse_scripts.end())
  453. {
  454. const char* scriptName = itr->second.c_str();
  455. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  456. return scriptName;
  457. }
  458. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  459. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  460. itr = zone_inverse_scripts.find(state);
  461. if (itr != zone_inverse_scripts.end())
  462. {
  463. const char* scriptName = itr->second.c_str();
  464. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  465. return scriptName;
  466. }
  467. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  468. MRegionScripts.writelock(__FUNCTION__, __LINE__);
  469. itr = region_inverse_scripts.find(state);
  470. if (itr != region_inverse_scripts.end())
  471. {
  472. const char* scriptName = itr->second.c_str();
  473. MRegionScripts.releasewritelock(__FUNCTION__, __LINE__);
  474. return scriptName;
  475. }
  476. MRegionScripts.releasewritelock(__FUNCTION__, __LINE__);
  477. MSpells.lock();
  478. LuaSpell* spell = GetCurrentSpell(state);
  479. if (spell)
  480. {
  481. const char* fileName = (spell->file_name.length() > 0) ? spell->file_name.c_str() : "";
  482. MSpells.unlock();
  483. return fileName;
  484. }
  485. MSpells.unlock();
  486. return "";
  487. }
  488. bool LuaInterface::LoadSpawnScript(string name) {
  489. return LoadSpawnScript(name.c_str());
  490. }
  491. bool LuaInterface::LoadZoneScript(string name) {
  492. return LoadZoneScript(name.c_str());
  493. }
  494. bool LuaInterface::LoadRegionScript(string name) {
  495. return LoadRegionScript(name.c_str());
  496. }
  497. std::string LuaInterface::AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast, const char* function, SpellScriptTimer* timer, bool passLuaSpell, Spawn* altTarget) {
  498. std::string functionCalled = string("");
  499. if (function)
  500. {
  501. functionCalled = string(function);
  502. lua_getglobal(spell->state, function);
  503. }
  504. else if (precast)
  505. {
  506. functionCalled = "precast";
  507. lua_getglobal(spell->state, "precast");
  508. }
  509. else if(first_cast)
  510. {
  511. functionCalled = "cast";
  512. lua_getglobal(spell->state, "cast");
  513. }
  514. else
  515. {
  516. functionCalled = "tick";
  517. lua_getglobal(spell->state, "tick");
  518. }
  519. if(passLuaSpell)
  520. SetSpellValue(spell->state, spell);
  521. Spawn* temp_spawn = 0;
  522. if (timer && timer->caster && spell->caster)
  523. temp_spawn = spell->caster->GetZone()->GetSpawnByID(timer->caster);
  524. if (temp_spawn)
  525. SetSpawnValue(spell->state, temp_spawn);
  526. else if (spell->caster)
  527. SetSpawnValue(spell->state, spell->caster);
  528. temp_spawn = 0;
  529. if (timer && timer->target && spell->caster)
  530. temp_spawn = spell->caster->GetZone()->GetSpawnByID(timer->target);
  531. if (temp_spawn)
  532. SetSpawnValue(spell->state, temp_spawn);
  533. else {
  534. if(altTarget)
  535. {
  536. SetSpawnValue(spell->state, altTarget);
  537. }
  538. else if(spell->caster && spell->caster->GetZone() != nullptr && spell->initial_target)
  539. {
  540. // easier to debug target id as ptr
  541. Spawn* new_target = spell->caster->GetZone()->GetSpawnByID(spell->initial_target);
  542. SetSpawnValue(spell->state, new_target);
  543. }
  544. else if(spell->caster && spell->caster->GetTarget())
  545. SetSpawnValue(spell->state, spell->caster->GetTarget());
  546. else
  547. SetSpawnValue(spell->state, 0);
  548. }
  549. return functionCalled;
  550. }
  551. LuaSpell* LuaInterface::GetCurrentSpell(lua_State* state) {
  552. if(current_spells.count(state) > 0)
  553. return current_spells[state];
  554. return 0;
  555. }
  556. bool LuaInterface::CallSpellProcess(LuaSpell* spell, int8 num_parameters, std::string customFunction) {
  557. if(shutting_down || !spell || !spell->caster)
  558. return false;
  559. MSpells.lock();
  560. current_spells[spell->state] = spell;
  561. MSpells.unlock();
  562. if(lua_pcall(spell->state, num_parameters, 0, 0) != 0){
  563. LogError("Error running function '%s' in %s: %s", customFunction.c_str(), spell->spell->GetName(), lua_tostring(spell->state, -1));
  564. lua_pop(spell->state, 1);
  565. RemoveSpell(spell, false); // may be in a lock
  566. return false;
  567. }
  568. return true;
  569. }
  570. void LuaInterface::RemoveSpawnScript(const char* name) {
  571. lua_State* state = 0;
  572. Mutex* mutex = GetSpawnScriptMutex(name);
  573. while((state = GetSpawnScript(name, false))){
  574. mutex->writelock(__FUNCTION__, __LINE__);
  575. lua_close(state);
  576. spawn_scripts[name].erase(state);
  577. mutex->releasewritelock(__FUNCTION__, __LINE__);
  578. }
  579. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  580. spawn_scripts.erase(name);
  581. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  582. }
  583. bool LuaInterface::CallItemScript(lua_State* state, int8 num_parameters, std::string* returnValue) {
  584. if(shutting_down)
  585. return false;
  586. if(!state || lua_pcall(state, num_parameters, 1, 0) != 0){
  587. if (state){
  588. const char* err = lua_tostring(state, -1);
  589. LogError("%s: %s", GetScriptName(state), err);
  590. lua_pop(state, 1);
  591. }
  592. return false;
  593. }
  594. std::string result = std::string("");
  595. if(lua_isstring(state, -1)){
  596. size_t size = 0;
  597. const char* str = lua_tolstring(state, -1, &size);
  598. if(str)
  599. result = string(str);
  600. }
  601. if(returnValue)
  602. *returnValue = std::string(result);
  603. return true;
  604. }
  605. bool LuaInterface::CallItemScript(lua_State* state, int8 num_parameters, sint64* returnValue) {
  606. if(shutting_down)
  607. return false;
  608. if(!state || lua_pcall(state, num_parameters, 1, 0) != 0){
  609. if (state){
  610. const char* err = lua_tostring(state, -1);
  611. LogError("%s: %s", GetScriptName(state), err);
  612. lua_pop(state, 1);
  613. }
  614. return false;
  615. }
  616. sint64 result = 0;
  617. if (lua_isnumber(state, -1))
  618. {
  619. result = (sint64)lua_tonumber(state, -1);
  620. lua_pop(state, 1);
  621. }
  622. if(returnValue)
  623. *returnValue = result;
  624. return true;
  625. }
  626. bool LuaInterface::CallSpawnScript(lua_State* state, int8 num_parameters) {
  627. if(shutting_down || lua_system_reloading)
  628. return false;
  629. if(!state || lua_pcall(state, num_parameters, 0, 0) != 0){
  630. if (state){
  631. const char* err = lua_tostring(state, -1);
  632. LogError("%s: %s", GetScriptName(state), err);
  633. lua_pop(state, 1);
  634. }
  635. return false;
  636. }
  637. return true;
  638. }
  639. bool LuaInterface::CallScriptInt32(lua_State* state, int8 num_parameters, int32* returnValue) {
  640. if(shutting_down)
  641. return false;
  642. if (!state || lua_pcall(state, num_parameters, 1, 0) != 0) {
  643. if (state){
  644. const char* err = lua_tostring(state, -1);
  645. LogError("%s: %s", GetScriptName(state), err);
  646. lua_pop(state, 1);
  647. }
  648. return false;
  649. }
  650. int32 result = 0;
  651. if (lua_isnumber(state, -1))
  652. {
  653. result = (int32)lua_tonumber(state, -1);
  654. lua_pop(state, 1);
  655. }
  656. if(returnValue)
  657. *returnValue = result;
  658. return true;
  659. }
  660. bool LuaInterface::CallScriptSInt32(lua_State* state, int8 num_parameters, sint32* returnValue) {
  661. if(shutting_down)
  662. return false;
  663. if (!state || lua_pcall(state, num_parameters, 1, 0) != 0) {
  664. if (state){
  665. const char* err = lua_tostring(state, -1);
  666. LogError("%s: %s", GetScriptName(state), err);
  667. lua_pop(state, 1);
  668. }
  669. return false;
  670. }
  671. sint32 result = 0;
  672. if (lua_isnumber(state, -1))
  673. {
  674. result = (sint32)lua_tointeger(state, -1);
  675. lua_pop(state, 1);
  676. }
  677. if(returnValue)
  678. *returnValue = result;
  679. return true;
  680. }
  681. bool LuaInterface::CallRegionScript(lua_State* state, int8 num_parameters, int32* returnValue) {
  682. if(shutting_down)
  683. return false;
  684. if (!state || lua_pcall(state, num_parameters, 1, 0) != 0) {
  685. if (state){
  686. const char* err = lua_tostring(state, -1);
  687. LogError("%s: %s", GetScriptName(state), err);
  688. lua_pop(state, 1);
  689. }
  690. return false;
  691. }
  692. int32 result = 0;
  693. if (lua_isnumber(state, -1))
  694. {
  695. result = (int32)lua_tonumber(state, -1);
  696. lua_pop(state, 1);
  697. }
  698. if(returnValue)
  699. *returnValue = result;
  700. return true;
  701. }
  702. lua_State* LuaInterface::LoadLuaFile(const char* name) {
  703. if(shutting_down)
  704. return 0;
  705. lua_State* state = luaL_newstate();
  706. luaL_openlibs(state);
  707. if(luaL_dofile(state, name) == 0){
  708. RegisterFunctions(state);
  709. return state;
  710. }
  711. else{
  712. LogError("Error loading %s (file name: '%s')", lua_tostring(state, -1), name);
  713. lua_pop(state, 1);
  714. lua_close(state);
  715. }
  716. return 0;
  717. }
  718. void LuaInterface::RemoveSpell(LuaSpell* spell, bool call_remove_function, bool can_delete, string reason, bool removing_all_spells) {
  719. if(shutting_down)
  720. return;
  721. if(call_remove_function){
  722. lua_getglobal(spell->state, "remove");
  723. LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper();
  724. spawn_wrapper->spawn = spell->caster;
  725. AddUserDataPtr(spawn_wrapper);
  726. lua_pushlightuserdata(spell->state, spawn_wrapper);
  727. if(spell->caster && (spell->initial_target || spell->caster->GetTarget())){
  728. spawn_wrapper = new LUASpawnWrapper();
  729. if(!spell->initial_target)
  730. spawn_wrapper->spawn = spell->caster->GetTarget();
  731. else
  732. spawn_wrapper->spawn = spell->caster->GetZone()->GetSpawnByID(spell->initial_target);
  733. AddUserDataPtr(spawn_wrapper);
  734. lua_pushlightuserdata(spell->state, spawn_wrapper);
  735. }
  736. else
  737. lua_pushlightuserdata(spell->state, 0);
  738. if (spell->caster && !spell->caster->Alive())
  739. reason = "dead";
  740. lua_pushstring(spell->state, (char*)reason.c_str());
  741. MSpells.lock();
  742. current_spells[spell->state] = spell;
  743. MSpells.unlock();
  744. lua_pcall(spell->state, 3, 0, 0);
  745. ResetFunctionStack(spell->state);
  746. }
  747. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  748. if(spell->caster) {
  749. for (int8 i = 0; i < spell->targets.size(); i++) {
  750. Spawn* target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  751. if (!target || !target->IsEntity())
  752. continue;
  753. ((Entity*)target)->RemoveProc(0, spell);
  754. ((Entity*)target)->RemoveSpellEffect(spell);
  755. ((Entity*)target)->RemoveSpellBonus(spell);
  756. }
  757. }
  758. multimap<int32,int8>::iterator entries;
  759. for(entries = spell->char_id_targets.begin(); entries != spell->char_id_targets.end(); entries++)
  760. {
  761. Client* tmpClient = zone_list.GetClientByCharID(entries->first);
  762. if(tmpClient && tmpClient->GetPlayer())
  763. {
  764. tmpClient->GetPlayer()->RemoveProc(0, spell);
  765. tmpClient->GetPlayer()->RemoveSpellEffect(spell);
  766. tmpClient->GetPlayer()->RemoveSpellBonus(spell);
  767. }
  768. }
  769. spell->char_id_targets.clear(); // TODO: reach out to those clients in different
  770. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  771. // we need to make sure all memory is purged for a copied spell, its only used once
  772. if (spell->spell->IsCopiedSpell())
  773. can_delete = true;
  774. if (can_delete) {
  775. AddPendingSpellDelete(spell);
  776. }
  777. if (spell->caster)
  778. {
  779. spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(spell);
  780. spell->caster->RemoveProc(0, spell);
  781. spell->caster->RemoveMaintainedSpell(spell);
  782. int8 spell_type = spell->spell->GetSpellData()->spell_type;
  783. if(spell->caster->IsPlayer() && !removing_all_spells)
  784. {
  785. Player* player = (Player*)spell->caster;
  786. switch(spell_type)
  787. {
  788. case SPELL_TYPE_FOOD:
  789. if(player->get_character_flag(CF_FOOD_AUTO_CONSUME))
  790. {
  791. Item* item = player->GetEquipmentList()->GetItem(EQ2_FOOD_SLOT);
  792. if(item && player->GetClient())
  793. player->GetClient()->ConsumeFoodDrink(item, EQ2_FOOD_SLOT);
  794. }
  795. break;
  796. case SPELL_TYPE_DRINK:
  797. if(player->get_character_flag(CF_DRINK_AUTO_CONSUME))
  798. {
  799. Item* item = player->GetEquipmentList()->GetItem(EQ2_DRINK_SLOT);
  800. if(item && player->GetClient())
  801. player->GetClient()->ConsumeFoodDrink(item, EQ2_DRINK_SLOT);
  802. }
  803. break;
  804. }
  805. }
  806. }
  807. }
  808. void LuaInterface::RegisterFunctions(lua_State* state) {
  809. lua_register(state, "SetHP", EQ2Emu_lua_SetCurrentHP);
  810. lua_register(state, "SetMaxHP", EQ2Emu_lua_SetMaxHP);
  811. lua_register(state, "SetMaxHPBase", EQ2Emu_lua_SetMaxHPBase);
  812. lua_register(state, "SetPower", EQ2Emu_lua_SetCurrentPower);
  813. lua_register(state, "SetMaxPower", EQ2Emu_lua_SetMaxPower);
  814. lua_register(state, "SetMaxPowerBase", EQ2Emu_lua_SetMaxPowerBase);
  815. lua_register(state, "SetPosition", EQ2Emu_lua_SetPosition);
  816. lua_register(state, "SetHeading", EQ2Emu_lua_SetHeading);
  817. lua_register(state, "SetModelType", EQ2Emu_lua_SetModelType);
  818. lua_register(state, "SetAdventureClass", EQ2Emu_lua_SetAdventureClass);
  819. lua_register(state, "SetTradeskillClass", EQ2Emu_lua_SetTradeskillClass);
  820. lua_register(state, "SetMount", EQ2Emu_lua_SetMount);
  821. lua_register(state, "SetMountColor", EQ2Emu_lua_SetMountColor);
  822. lua_register(state, "GetMount", EQ2Emu_lua_GetMount);
  823. lua_register(state, "GetRace", EQ2Emu_lua_GetRace);
  824. lua_register(state, "GetRaceName", EQ2Emu_lua_GetRaceName);
  825. lua_register(state, "GetClass", EQ2Emu_lua_GetClass);
  826. lua_register(state, "GetClassName", EQ2Emu_lua_GetClassName);
  827. lua_register(state, "GetArchetypeName", EQ2Emu_lua_GetArchetypeName);
  828. lua_register(state, "SetSpeed", EQ2Emu_lua_SetSpeed);
  829. lua_register(state, "ModifyPower", EQ2Emu_lua_ModifyPower);
  830. lua_register(state, "ModifyHP", EQ2Emu_lua_ModifyHP);
  831. lua_register(state, "GetDistance", EQ2Emu_lua_GetDistance);
  832. lua_register(state, "GetHeading", EQ2Emu_lua_GetHeading);
  833. lua_register(state, "GetLevel", EQ2Emu_lua_GetLevel);
  834. lua_register(state, "GetHP", EQ2Emu_lua_GetCurrentHP);
  835. lua_register(state, "GetMaxHP", EQ2Emu_lua_GetMaxHP);
  836. lua_register(state, "GetMaxHPBase", EQ2Emu_lua_GetMaxHPBase);
  837. lua_register(state, "GetMaxPower", EQ2Emu_lua_GetMaxPower);
  838. lua_register(state, "GetMaxPowerBase", EQ2Emu_lua_GetMaxPowerBase);
  839. lua_register(state, "GetName", EQ2Emu_lua_GetName);
  840. lua_register(state, "GetPower", EQ2Emu_lua_GetCurrentPower);
  841. lua_register(state, "GetX", EQ2Emu_lua_GetX);
  842. lua_register(state, "GetY", EQ2Emu_lua_GetY);
  843. lua_register(state, "GetZ", EQ2Emu_lua_GetZ);
  844. lua_register(state, "GetSpawnID", EQ2Emu_lua_GetSpawnID);
  845. lua_register(state, "GetSpawnGroupID", EQ2Emu_lua_GetSpawnGroupID);
  846. lua_register(state, "SetSpawnGroupID", EQ2Emu_lua_SetSpawnGroupID);
  847. lua_register(state, "AddSpawnToGroup", EQ2Emu_lua_AddSpawnToGroup);
  848. lua_register(state, "GetSpawnLocationID", EQ2Emu_lua_GetSpawnLocationID);
  849. lua_register(state, "GetSpawnLocationPlacementID", EQ2Emu_lua_GetSpawnLocationPlacementID);
  850. lua_register(state, "CreateSpawnList", EQ2Emu_lua_CreateSpawnList);
  851. lua_register(state, "AddSpawnToSpawnList", EQ2Emu_lua_AddSpawnToSpawnList);
  852. lua_register(state, "RemoveSpawnFromSpawnList", EQ2Emu_lua_RemoveSpawnFromSpawnList);
  853. lua_register(state, "GetSpawnListBySpawnID", EQ2Emu_lua_GetSpawnListBySpawnID);
  854. lua_register(state, "GetSpawnListByRailID", EQ2Emu_lua_GetSpawnListByRailID);
  855. lua_register(state, "GetPassengerSpawnList", EQ2Emu_lua_GetPassengerSpawnList);
  856. lua_register(state, "GetSpawnFromList", EQ2Emu_lua_GetSpawnFromList);
  857. lua_register(state, "GetSpawnListSize", EQ2Emu_lua_GetSpawnListSize);
  858. lua_register(state, "SetFactionID", EQ2Emu_lua_SetFactionID);
  859. lua_register(state, "GetFactionID", EQ2Emu_lua_GetFactionID);
  860. lua_register(state, "GetFactionAmount", EQ2Emu_lua_GetFactionAmount);
  861. lua_register(state, "ChangeFaction", EQ2Emu_lua_ChangeFaction);
  862. lua_register(state, "GetGender", EQ2Emu_lua_GetGender);
  863. lua_register(state, "GetTarget", EQ2Emu_lua_GetTarget);
  864. lua_register(state, "HasFreeSlot", EQ2Emu_lua_HasFreeSlot);
  865. lua_register(state, "HasItemEquipped", EQ2Emu_lua_HasItemEquipped);
  866. lua_register(state, "GetEquippedItemByID", EQ2Emu_lua_GetEquippedItemByID);
  867. lua_register(state, "SetEquippedItemByID", EQ2Emu_lua_SetEquippedItemByID);
  868. lua_register(state, "SetEquippedItem", EQ2Emu_lua_SetEquippedItem);
  869. lua_register(state, "UnequipSlot", EQ2Emu_lua_UnequipSlot);
  870. lua_register(state, "SetEquipment", EQ2Emu_lua_SetEquipment);
  871. lua_register(state, "GetEquippedItemBySlot", EQ2Emu_lua_GetEquippedItemBySlot);
  872. lua_register(state, "GetItemByID", EQ2Emu_lua_GetItemByID);
  873. lua_register(state, "GetItemType", EQ2Emu_lua_GetItemType);
  874. lua_register(state, "GetItemEffectType", EQ2Emu_lua_GetItemEffectType);
  875. lua_register(state, "GetSpellName", EQ2Emu_lua_GetSpellName);
  876. lua_register(state, "PerformCameraShake", EQ2Emu_lua_PerformCameraShake);
  877. lua_register(state, "GetModelType", EQ2Emu_lua_GetModelType);
  878. lua_register(state, "GetSpeed", EQ2Emu_lua_GetSpeed);
  879. lua_register(state, "HasMoved", EQ2Emu_lua_HasMoved);
  880. lua_register(state, "SpellDamage", EQ2Emu_lua_SpellDamage);
  881. lua_register(state, "CastSpell", EQ2Emu_lua_CastSpell);
  882. lua_register(state, "SpellHeal", EQ2Emu_lua_SpellHeal);
  883. lua_register(state, "SpellHealPct", EQ2Emu_lua_SpellHealPct);
  884. lua_register(state, "AddItem", EQ2Emu_lua_AddItem);
  885. lua_register(state, "SummonItem", EQ2Emu_lua_SummonItem);
  886. lua_register(state, "RemoveItem", EQ2Emu_lua_RemoveItem);
  887. lua_register(state, "HasItem", EQ2Emu_lua_HasItem);
  888. lua_register(state, "SpawnMob", EQ2Emu_lua_Spawn);
  889. lua_register(state, "SummonPet", EQ2Emu_lua_SummonPet);
  890. lua_register(state, "AddSpawnAccess", EQ2Emu_lua_AddSpawnAccess);
  891. lua_register(state, "GetZone", EQ2Emu_lua_GetZone);
  892. lua_register(state, "GetZoneName", EQ2Emu_lua_GetZoneName);
  893. lua_register(state, "GetZoneID", EQ2Emu_lua_GetZoneID);
  894. lua_register(state, "Zone", EQ2Emu_lua_Zone);
  895. lua_register(state, "AddHate", EQ2Emu_lua_AddHate);
  896. lua_register(state, "IsAlive", EQ2Emu_lua_IsAlive);
  897. lua_register(state, "IsInCombat", EQ2Emu_lua_IsInCombat);
  898. lua_register(state, "Attack", EQ2Emu_lua_Attack);
  899. lua_register(state, "ApplySpellVisual", EQ2Emu_lua_ApplySpellVisual);
  900. lua_register(state, "IsPlayer", EQ2Emu_lua_IsPlayer);
  901. lua_register(state, "GetCharacterID", EQ2Emu_lua_GetCharacterID);
  902. lua_register(state, "FaceTarget", EQ2Emu_lua_FaceTarget);
  903. lua_register(state, "MoveToLocation", EQ2Emu_lua_MoveToLocation);
  904. lua_register(state, "ClearRunningLocations", EQ2Emu_lua_ClearRunningLocations);
  905. lua_register(state, "Shout", EQ2Emu_lua_Shout);
  906. lua_register(state, "Say", EQ2Emu_lua_Say);
  907. lua_register(state, "SayOOC", EQ2Emu_lua_SayOOC);
  908. lua_register(state, "Emote", EQ2Emu_lua_Emote);
  909. lua_register(state, "MovementLoopAddLocation", EQ2Emu_lua_MovementLoopAdd); // do not remove this function, it is already heavily used by the content team
  910. lua_register(state, "MovementLoopAdd", EQ2Emu_lua_MovementLoopAdd);
  911. // lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation); // This is already added below.
  912. lua_register(state, "AddTimer", EQ2Emu_lua_AddTimer);
  913. lua_register(state, "StopTimer", EQ2Emu_lua_StopTimer);
  914. lua_register(state, "Harvest", EQ2Emu_lua_Harvest);
  915. lua_register(state, "SetAttackable", EQ2Emu_lua_SetAttackable);
  916. lua_register(state, "AddSpellBonus", EQ2Emu_lua_AddSpellBonus);
  917. lua_register(state, "RemoveSpellBonus", EQ2Emu_lua_RemoveSpellBonus);
  918. lua_register(state, "AddSkillBonus", EQ2Emu_lua_AddSkillBonus);
  919. lua_register(state, "RemoveSkillBonus", EQ2Emu_lua_RemoveSkillBonus);
  920. lua_register(state, "AddControlEffect", EQ2Emu_lua_AddControlEffect);
  921. lua_register(state, "RemoveControlEffect", EQ2Emu_lua_RemoveControlEffect);
  922. lua_register(state, "HasControlEffect", EQ2Emu_lua_HasControlEffect);
  923. lua_register(state, "GetBaseAggroRadius", EQ2Emu_lua_GetBaseAggroRadius);
  924. lua_register(state, "GetAggroRadius", EQ2Emu_lua_GetAggroRadius);
  925. lua_register(state, "SetAggroRadius", EQ2Emu_lua_SetAggroRadius);
  926. lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation);
  927. lua_register(state, "SetDeity", EQ2Emu_lua_SetDeity);
  928. lua_register(state, "GetDeity", EQ2Emu_lua_GetDeity);
  929. lua_register(state, "GetInt", EQ2Emu_lua_GetInt);
  930. lua_register(state, "GetWis", EQ2Emu_lua_GetWis);
  931. lua_register(state, "GetSta", EQ2Emu_lua_GetSta);
  932. lua_register(state, "GetStr", EQ2Emu_lua_GetStr);
  933. lua_register(state, "GetAgi", EQ2Emu_lua_GetAgi);
  934. lua_register(state, "SetInt", EQ2Emu_lua_SetInt);
  935. lua_register(state, "SetWis", EQ2Emu_lua_SetWis);
  936. lua_register(state, "SetSta", EQ2Emu_lua_SetSta);
  937. lua_register(state, "SetStr", EQ2Emu_lua_SetStr);
  938. lua_register(state, "SetAgi", EQ2Emu_lua_SetAgi);
  939. lua_register(state, "GetIntBase", EQ2Emu_lua_GetIntBase);
  940. lua_register(state, "GetWisBase", EQ2Emu_lua_GetWisBase);
  941. lua_register(state, "GetStaBase", EQ2Emu_lua_GetStaBase);
  942. lua_register(state, "GetStrBase", EQ2Emu_lua_GetStrBase);
  943. lua_register(state, "GetAgiBase", EQ2Emu_lua_GetAgiBase);
  944. lua_register(state, "SetIntBase", EQ2Emu_lua_SetIntBase);
  945. lua_register(state, "SetWisBase", EQ2Emu_lua_SetWisBase);
  946. lua_register(state, "SetStaBase", EQ2Emu_lua_SetStaBase);
  947. lua_register(state, "SetStrBase", EQ2Emu_lua_SetStrBase);
  948. lua_register(state, "SetAgiBase", EQ2Emu_lua_SetAgiBase);
  949. lua_register(state, "GetSpawn", EQ2Emu_lua_GetSpawn);
  950. lua_register(state, "GetVariableValue", EQ2Emu_lua_GetVariableValue);
  951. lua_register(state, "GetCoinMessage", EQ2Emu_lua_GetCoinMessage);
  952. lua_register(state, "GetSpawnByGroupID", EQ2Emu_lua_GetSpawnByGroupID);
  953. lua_register(state, "GetSpawnByLocationID", EQ2Emu_lua_GetSpawnByLocationID);
  954. lua_register(state, "PlayFlavor", EQ2Emu_lua_PlayFlavor);
  955. lua_register(state, "PlayFlavorID", EQ2Emu_lua_PlayFlavorID);
  956. lua_register(state, "PlaySound", EQ2Emu_lua_PlaySound);
  957. lua_register(state, "PlayVoice", EQ2Emu_lua_PlayVoice);
  958. lua_register(state, "PlayAnimation", EQ2Emu_lua_PlayAnimation);
  959. lua_register(state, "AddLootItem", EQ2Emu_lua_AddLootItem);
  960. lua_register(state, "HasLootItem", EQ2Emu_lua_HasLootItem);
  961. lua_register(state, "RemoveLootItem", EQ2Emu_lua_RemoveLootItem);
  962. lua_register(state, "AddLootCoin", EQ2Emu_lua_AddLootCoin);
  963. lua_register(state, "GiveLoot", EQ2Emu_lua_GiveLoot);
  964. lua_register(state, "HasPendingLootItem", EQ2Emu_lua_HasPendingLootItem);
  965. lua_register(state, "HasPendingLoot", EQ2Emu_lua_HasPendingLoot);
  966. lua_register(state, "SetLootCoin", EQ2Emu_lua_SetLootCoin);
  967. lua_register(state, "HasCoin", EQ2Emu_lua_HasCoin);
  968. lua_register(state, "GetLootCoin", EQ2Emu_lua_GetLootCoin);
  969. lua_register(state, "SetPlayerProximityFunction", EQ2Emu_lua_SetPlayerProximityFunction);
  970. lua_register(state, "SetLocationProximityFunction", EQ2Emu_lua_SetLocationProximityFunction);
  971. lua_register(state, "CreateConversation", EQ2Emu_lua_CreateConversation);
  972. lua_register(state, "AddConversationOption", EQ2Emu_lua_AddConversationOption);
  973. lua_register(state, "StartConversation", EQ2Emu_lua_StartConversation);
  974. lua_register(state, "CloseConversation", EQ2Emu_lua_CloseConversation);
  975. lua_register(state, "CloseItemConversation", EQ2Emu_lua_CloseItemConversation);
  976. //lua_register(state, "StartItemConversation", EQ2Emu_lua_StartItemConversation);
  977. lua_register(state, "StartDialogConversation", EQ2Emu_lua_StartDialogConversation);
  978. lua_register(state, "SendStateCommand", EQ2Emu_lua_SendStateCommand);
  979. lua_register(state, "SpawnSet", EQ2Emu_lua_SpawnSet);
  980. lua_register(state, "SpawnSetByDistance", EQ2Emu_lua_SpawnSetByDistance);
  981. lua_register(state, "SpawnMove", EQ2Emu_lua_SpawnMove);
  982. lua_register(state, "KillSpawn", EQ2Emu_lua_KillSpawn);
  983. lua_register(state, "KillSpawnByDistance", EQ2Emu_lua_KillSpawnByDistance);
  984. lua_register(state, "Despawn", EQ2Emu_lua_Despawn);
  985. lua_register(state, "ChangeHandIcon", EQ2Emu_lua_ChangeHandIcon);
  986. lua_register(state, "SetVisualFlag", EQ2Emu_lua_SetVisualFlag);
  987. lua_register(state, "SetInfoFlag", EQ2Emu_lua_SetInfoFlag);
  988. lua_register(state, "IsBindAllowed", EQ2Emu_lua_IsBindAllowed);
  989. lua_register(state, "IsGateAllowed", EQ2Emu_lua_IsGateAllowed);
  990. lua_register(state, "Bind", EQ2Emu_lua_Bind);
  991. lua_register(state, "Gate", EQ2Emu_lua_Gate);
  992. lua_register(state, "SendMessage", EQ2Emu_lua_SendMessage);
  993. lua_register(state, "SendPopUpMessage", EQ2Emu_lua_SendPopUpMessage);
  994. lua_register(state, "SetServerControlFlag", EQ2Emu_lua_SetServerControlFlag);
  995. lua_register(state, "ToggleTracking", EQ2Emu_lua_ToggleTracking);
  996. lua_register(state, "AddPrimaryEntityCommand", EQ2Emu_lua_AddPrimaryEntityCommand);
  997. lua_register(state, "AddSpellBookEntry", EQ2Emu_lua_AddSpellBookEntry);
  998. lua_register(state, "HasSpell", EQ2Emu_lua_HasSpell);
  999. lua_register(state, "Interrupt", EQ2Emu_lua_Interrupt);
  1000. lua_register(state, "Stealth", EQ2Emu_lua_Stealth);
  1001. lua_register(state, "IsInvis", EQ2Emu_lua_IsInvis);
  1002. lua_register(state, "IsStealthed", EQ2Emu_lua_IsStealthed);
  1003. lua_register(state, "AddSpawnIDAccess", EQ2Emu_lua_AddSpawnIDAccess);
  1004. lua_register(state, "RemoveSpawnIDAccess", EQ2Emu_lua_RemoveSpawnIDAccess);
  1005. lua_register(state, "HasRecipeBook", EQ2Emu_lua_HasRecipeBook);
  1006. lua_register(state, "SetRequiredQuest", EQ2Emu_lua_SetRequiredQuest);
  1007. lua_register(state, "SetRequiredHistory", EQ2Emu_lua_SetRequiredHistory);
  1008. lua_register(state, "SetStepComplete", EQ2Emu_lua_SetStepComplete);
  1009. lua_register(state, "AddStepProgress", EQ2Emu_lua_AddStepProgress);
  1010. lua_register(state, "UpdateQuestTaskGroupDescription", EQ2Emu_lua_UpdateQuestTaskGroupDescription);
  1011. lua_register(state, "GetTaskGroupStep", EQ2Emu_lua_GetTaskGroupStep);
  1012. lua_register(state, "GetQuestStep", EQ2Emu_lua_GetQuestStep);
  1013. lua_register(state, "QuestStepIsComplete", EQ2Emu_lua_QuestStepIsComplete);
  1014. lua_register(state, "RegisterQuest", EQ2Emu_lua_RegisterQuest);
  1015. lua_register(state, "SetQuestPrereqLevel", EQ2Emu_lua_SetQuestPrereqLevel);
  1016. lua_register(state, "AddQuestPrereqQuest", EQ2Emu_lua_AddQuestPrereqQuest);
  1017. lua_register(state, "AddQuestPrereqItem", EQ2Emu_lua_AddQuestPrereqItem);
  1018. lua_register(state, "AddQuestPrereqFaction", EQ2Emu_lua_AddQuestPrereqFaction);
  1019. lua_register(state, "AddQuestPrereqRace", EQ2Emu_lua_AddQuestPrereqRace);
  1020. lua_register(state, "AddQuestPrereqModelType", EQ2Emu_lua_AddQuestPrereqModelType);
  1021. lua_register(state, "AddQuestPrereqClass", EQ2Emu_lua_AddQuestPrereqClass);
  1022. lua_register(state, "AddQuestPrereqTradeskillLevel", EQ2Emu_lua_AddQuestPrereqTradeskillLevel);
  1023. lua_register(state, "AddQuestPrereqTradeskillClass", EQ2Emu_lua_AddQuestPrereqTradeskillClass);
  1024. lua_register(state, "AddQuestSelectableRewardItem", EQ2Emu_lua_AddQuestSelectableRewardItem);
  1025. lua_register(state, "HasQuestRewardItem", EQ2Emu_lua_HasQuestRewardItem);
  1026. lua_register(state, "AddQuestRewardItem", EQ2Emu_lua_AddQuestRewardItem);
  1027. lua_register(state, "AddQuestRewardCoin", EQ2Emu_lua_AddQuestRewardCoin);
  1028. lua_register(state, "AddQuestRewardFaction", EQ2Emu_lua_AddQuestRewardFaction);
  1029. lua_register(state, "SetQuestRewardStatus", EQ2Emu_lua_SetQuestRewardStatus);
  1030. lua_register(state, "SetStatusTmpReward", EQ2Emu_lua_SetStatusTmpReward);
  1031. lua_register(state, "SetCoinTmpReward", EQ2Emu_lua_SetCoinTmpReward);
  1032. lua_register(state, "SetQuestRewardComment", EQ2Emu_lua_SetQuestRewardComment);
  1033. lua_register(state, "SetQuestRewardExp", EQ2Emu_lua_SetQuestRewardExp);
  1034. lua_register(state, "AddQuestStepKill", EQ2Emu_lua_AddQuestStepKill);
  1035. lua_register(state, "AddQuestStepKillByRace", EQ2Emu_lua_AddQuestStepKillByRace);
  1036. lua_register(state, "AddQuestStep", EQ2Emu_lua_AddQuestStep);
  1037. lua_register(state, "AddQuestStepChat", EQ2Emu_lua_AddQuestStepChat);
  1038. lua_register(state, "AddQuestStepObtainItem", EQ2Emu_lua_AddQuestStepObtainItem);
  1039. lua_register(state, "AddQuestStepZoneLoc", EQ2Emu_lua_AddQuestStepZoneLoc);
  1040. lua_register(state, "AddQuestStepLocation", EQ2Emu_lua_AddQuestStepLocation);
  1041. lua_register(state, "AddQuestStepSpell", EQ2Emu_lua_AddQuestStepSpell);
  1042. lua_register(state, "AddQuestStepCraft", EQ2Emu_lua_AddQuestStepCraft);
  1043. lua_register(state, "AddQuestStepHarvest", EQ2Emu_lua_AddQuestStepHarvest);
  1044. lua_register(state, "AddQuestStepCompleteAction", EQ2Emu_lua_AddQuestStepCompleteAction);
  1045. lua_register(state, "AddQuestStepProgressAction", EQ2Emu_lua_AddQuestStepProgressAction);
  1046. lua_register(state, "SetQuestCompleteAction", EQ2Emu_lua_SetQuestCompleteAction);
  1047. lua_register(state, "GiveQuestReward", EQ2Emu_lua_GiveQuestReward);
  1048. lua_register(state, "UpdateQuestStepDescription", EQ2Emu_lua_UpdateQuestStepDescription);
  1049. lua_register(state, "UpdateQuestDescription", EQ2Emu_lua_UpdateQuestDescription);
  1050. lua_register(state, "UpdateQuestZone", EQ2Emu_lua_UpdateQuestZone);
  1051. lua_register(state, "SetCompletedDescription", EQ2Emu_lua_SetCompletedDescription);
  1052. lua_register(state, "OfferQuest", EQ2Emu_lua_OfferQuest);
  1053. lua_register(state, "ProvidesQuest", EQ2Emu_lua_ProvidesQuest);
  1054. lua_register(state, "HasQuest", EQ2Emu_lua_HasQuest);
  1055. lua_register(state, "HasCompletedQuest", EQ2Emu_lua_HasCompletedQuest);
  1056. lua_register(state, "QuestIsComplete", EQ2Emu_lua_QuestIsComplete);
  1057. lua_register(state, "QuestReturnNPC", EQ2Emu_lua_QuestReturnNPC);
  1058. lua_register(state, "GetQuest", EQ2Emu_lua_GetQuest);
  1059. lua_register(state, "HasCollectionsToHandIn", EQ2Emu_lua_HasCollectionsToHandIn);
  1060. lua_register(state, "HandInCollections", EQ2Emu_lua_HandInCollections);
  1061. lua_register(state, "UseWidget", EQ2Emu_lua_UseWidget);
  1062. lua_register(state, "SetSpellList", EQ2Emu_lua_SetSpellList);
  1063. lua_register(state, "GetPet", EQ2Emu_lua_GetPet);
  1064. lua_register(state, "Charm", EQ2Emu_lua_Charm);
  1065. lua_register(state, "GetGroup", EQ2Emu_lua_GetGroup);
  1066. lua_register(state, "SetCompleteFlag", EQ2Emu_lua_SetCompleteFlag);
  1067. lua_register(state, "SetQuestYellow", EQ2Emu_lua_SetQuestYellow);
  1068. lua_register(state, "CanReceiveQuest", EQ2Emu_lua_CanReceiveQuest);
  1069. lua_register(state, "AddTransportSpawn", EQ2Emu_lua_AddTransportSpawn);
  1070. lua_register(state, "IsTransportSpawn", EQ2Emu_lua_IsTransportSpawn);
  1071. // Option window
  1072. lua_register(state, "CreateOptionWindow", EQ2Emu_lua_CreateOptionWindow);
  1073. lua_register(state, "AddOptionWindowOption", EQ2Emu_lua_AddOptionWindowOption);
  1074. lua_register(state, "SendOptionWindow", EQ2Emu_lua_SendOptionWindow);
  1075. lua_register(state, "GetTradeskillClass", EQ2Emu_lua_GetTradeskillClass);
  1076. lua_register(state, "GetTradeskillLevel", EQ2Emu_lua_GetTradeskillLevel);
  1077. lua_register(state, "GetTradeskillClassName", EQ2Emu_lua_GetTradeskillClassName);
  1078. lua_register(state, "SetTradeskillLevel", EQ2Emu_lua_SetTradeskillLevel);
  1079. lua_register(state, "SummonDeityPet", EQ2Emu_lua_SummonDeityPet);
  1080. lua_register(state, "SummonCosmeticPet", EQ2Emu_lua_SummonCosmeticPet);
  1081. lua_register(state, "DismissPet", EQ2Emu_lua_DismissPet);
  1082. lua_register(state, "GetCharmedPet", EQ2Emu_lua_GetCharmedPet);
  1083. lua_register(state, "GetDeityPet", EQ2Emu_lua_GetDeityPet);
  1084. lua_register(state, "GetCosmeticPet", EQ2Emu_lua_GetCosmeticPet);
  1085. lua_register(state, "SetQuestFeatherColor", EQ2Emu_lua_SetQuestFeatherColor);
  1086. lua_register(state, "RemoveSpawnAccess", EQ2Emu_lua_RemoveSpawnAccess);
  1087. lua_register(state, "SpawnByLocationID", EQ2Emu_lua_SpawnByLocationID);
  1088. lua_register(state, "CastEntityCommand", EQ2Emu_lua_CastEntityCommand);
  1089. lua_register(state, "SetLuaBrain", EQ2Emu_lua_SetLuaBrain);
  1090. lua_register(state, "SetBrainTick", EQ2Emu_lua_SetBrainTick);
  1091. lua_register(state, "SetFollowTarget", EQ2Emu_lua_SetFollowTarget);
  1092. lua_register(state, "GetFollowTarget", EQ2Emu_lua_GetFollowTarget);
  1093. lua_register(state, "ToggleFollow", EQ2Emu_lua_ToggleFollow);
  1094. lua_register(state, "IsFollowing", EQ2Emu_lua_IsFollowing);
  1095. lua_register(state, "SetTempVariable", EQ2Emu_lua_SetTempVariable);
  1096. lua_register(state, "GetTempVariable", EQ2Emu_lua_GetTempVariable);
  1097. lua_register(state, "GiveQuestItem", EQ2Emu_lua_GiveQuestItem);
  1098. lua_register(state, "SetQuestRepeatable", EQ2Emu_lua_SetQuestRepeatable);
  1099. lua_register(state, "AddWaypoint", EQ2Emu_lua_AddWaypoint);
  1100. lua_register(state, "RemoveWaypoint", EQ2Emu_lua_RemoveWaypoint);
  1101. lua_register(state, "SendWaypoints", EQ2Emu_lua_SendWaypoints);
  1102. lua_register(state, "AddWard", EQ2Emu_lua_AddWard);
  1103. lua_register(state, "AddToWard", EQ2Emu_lua_AddToWard);
  1104. lua_register(state, "RemoveWard", EQ2Emu_lua_RemoveWard);
  1105. lua_register(state, "GetWardAmountLeft", EQ2Emu_lua_GetWardAmountLeft);
  1106. lua_register(state, "GetWardValue", EQ2Emu_lua_GetWardValue);
  1107. lua_register(state, "SetTarget", EQ2Emu_lua_SetTarget);
  1108. lua_register(state, "IsPet", EQ2Emu_lua_IsPet);
  1109. lua_register(state, "GetOwner", EQ2Emu_lua_GetOwner);
  1110. lua_register(state, "SetInCombat", EQ2Emu_lua_SetInCombat);
  1111. lua_register(state, "CompareSpawns", EQ2Emu_lua_CompareSpawns);
  1112. lua_register(state, "ClearRunback", EQ2Emu_lua_ClearRunback);
  1113. lua_register(state, "Runback", EQ2Emu_lua_Runback);
  1114. lua_register(state, "GetRunbackDistance", EQ2Emu_lua_GetRunbackDistance);
  1115. lua_register(state, "IsCasting", EQ2Emu_lua_IsCasting);
  1116. lua_register(state, "IsMezzed", EQ2Emu_lua_IsMezzed);
  1117. lua_register(state, "IsStunned", EQ2Emu_lua_IsStunned);
  1118. lua_register(state, "IsMezzedOrStunned", EQ2Emu_lua_IsMezzedOrStunned);
  1119. lua_register(state, "ProcessSpell", EQ2Emu_lua_ProcessSpell);
  1120. lua_register(state, "ProcessMelee", EQ2Emu_lua_ProcessMelee);
  1121. lua_register(state, "HasRecovered", EQ2Emu_lua_HasRecovered);
  1122. lua_register(state, "GetEncounterSize", EQ2Emu_lua_GetEncounterSize);
  1123. lua_register(state, "GetMostHated", EQ2Emu_lua_GetMostHated);
  1124. lua_register(state, "ClearHate", EQ2Emu_lua_ClearHate);
  1125. lua_register(state, "ClearEncounter", EQ2Emu_lua_ClearEncounter);
  1126. lua_register(state, "GetEncounter", EQ2Emu_lua_GetEncounter);
  1127. lua_register(state, "GetHateList", EQ2Emu_lua_GetHateList);
  1128. lua_register(state, "HasGroup", EQ2Emu_lua_HasGroup);
  1129. lua_register(state, "HasSpellEffect", EQ2Emu_lua_HasSpellEffect);
  1130. lua_register(state, "SetSuccessTimer", EQ2Emu_lua_SetSuccessTimer);
  1131. lua_register(state, "SetFailureTimer", EQ2Emu_lua_SetFailureTimer);
  1132. lua_register(state, "IsGroundSpawn", EQ2Emu_lua_IsGroundSpawn);
  1133. lua_register(state, "CanHarvest", EQ2Emu_lua_CanHarvest);
  1134. lua_register(state, "SummonDumbFirePet", EQ2Emu_lua_SummonDumbFirePet);
  1135. lua_register(state, "GetSkillValue", EQ2Emu_lua_GetSkillValue);
  1136. lua_register(state, "GetSkillMaxValue", EQ2Emu_lua_GetSkillMaxValue);
  1137. lua_register(state, "GetSkillName", EQ2Emu_lua_GetSkillName);
  1138. lua_register(state, "SetSkillMaxValue", EQ2Emu_lua_SetSkillMaxValue);
  1139. lua_register(state, "SetSkillValue", EQ2Emu_lua_SetSkillValue);
  1140. lua_register(state, "GetSkill", EQ2Emu_lua_GetSkill);
  1141. lua_register(state, "GetSkillIDByName", EQ2Emu_lua_GetSkillIDByName);
  1142. lua_register(state, "HasSkill", EQ2Emu_lua_HasSkill);
  1143. lua_register(state, "AddSkill", EQ2Emu_lua_AddSkill);
  1144. lua_register(state, "IncreaseSkillCapsByType", EQ2Emu_lua_IncreaseSkillCapsByType);
  1145. lua_register(state, "RemoveSkill", EQ2Emu_lua_RemoveSkill);
  1146. lua_register(state, "AddProc", EQ2Emu_lua_AddProc);
  1147. lua_register(state, "RemoveProc", EQ2Emu_lua_RemoveProc);
  1148. lua_register(state, "Knockback", EQ2Emu_lua_Knockback);
  1149. lua_register(state, "IsEpic", EQ2Emu_lua_IsEpic);
  1150. lua_register(state, "IsHeroic", EQ2Emu_lua_IsHeroic);
  1151. lua_register(state, "ProcDamage", EQ2Emu_lua_ProcDamage);
  1152. lua_register(state, "LastSpellAttackHit", EQ2Emu_lua_LastSpellAttackHit);
  1153. lua_register(state, "IsBehind", EQ2Emu_lua_IsBehind);
  1154. lua_register(state, "IsFlanking", EQ2Emu_lua_IsFlanking);
  1155. lua_register(state, "AddSpellTimer", EQ2Emu_lua_AddSpellTimer);
  1156. lua_register(state, "GetItemCount", EQ2Emu_lua_GetItemCount);
  1157. lua_register(state, "SetItemCount", EQ2Emu_lua_SetItemCount);
  1158. lua_register(state, "Resurrect", EQ2Emu_lua_Resurrect);
  1159. lua_register(state, "BreatheUnderwater", EQ2Emu_lua_BreatheUnderwater);
  1160. lua_register(state, "BlurVision", EQ2Emu_lua_BlurVision);
  1161. lua_register(state, "SetVision", EQ2Emu_lua_SetVision);
  1162. lua_register(state, "GetItemSkillReq", EQ2Emu_lua_GetItemSkillReq);
  1163. lua_register(state, "SetSpeedMultiplier", EQ2Emu_lua_SetSpeedMultiplier);
  1164. lua_register(state, "SetIllusion", EQ2Emu_lua_SetIllusion);
  1165. lua_register(state, "ResetIllusion", EQ2Emu_lua_ResetIllusion);
  1166. lua_register(state, "AddThreatTransfer", EQ2Emu_lua_AddThreatTransfer);
  1167. lua_register(state, "RemoveThreatTransfer", EQ2Emu_lua_RemoveThreatTransfer);
  1168. lua_register(state, "CureByType", EQ2Emu_lua_CureByType);
  1169. lua_register(state, "CureByControlEffect", EQ2Emu_lua_CureByControlEffect);
  1170. lua_register(state, "AddSpawnSpellBonus", EQ2Emu_lua_AddSpawnSpellBonus);
  1171. lua_register(state, "RemoveSpawnSpellBonus", EQ2Emu_lua_RemoveSpawnSpellBonus);
  1172. lua_register(state, "CancelSpell", EQ2Emu_lua_CancelSpell);
  1173. lua_register(state, "RemoveStealth", EQ2Emu_lua_RemoveStealth);
  1174. lua_register(state, "RemoveInvis", EQ2Emu_lua_RemoveInvis);
  1175. lua_register(state, "StartHeroicOpportunity", EQ2Emu_lua_StartHeroicOpportunity);
  1176. lua_register(state, "CopySpawnAppearance", EQ2Emu_lua_CopySpawnAppearance);
  1177. lua_register(state, "SetSpellTriggerCount", EQ2Emu_lua_SetSpellTriggerCount);
  1178. lua_register(state, "GetSpellTriggerCount", EQ2Emu_lua_GetSpellTriggerCount);
  1179. lua_register(state, "RemoveTriggerFromSpell", EQ2Emu_lua_RemoveTriggerFromSpell);
  1180. lua_register(state, "HasSpellImmunity", EQ2Emu_lua_HasSpellImmunity);
  1181. lua_register(state, "AddImmunitySpell", EQ2Emu_lua_AddImmunitySpell);
  1182. lua_register(state, "RemoveImmunitySpell", EQ2Emu_lua_RemoveImmunitySpell);
  1183. lua_register(state, "SetSpellSnareValue", EQ2Emu_lua_SetSpellSnareValue);
  1184. lua_register(state, "CheckRaceType", EQ2Emu_lua_CheckRaceType);
  1185. lua_register(state, "GetRaceType", EQ2Emu_lua_GetRaceType);
  1186. lua_register(state, "GetRaceBaseType", EQ2Emu_lua_GetRaceBaseType);
  1187. lua_register(state, "GetQuestFlags", EQ2Emu_lua_GetQuestFlags);
  1188. lua_register(state, "SetQuestFlags", EQ2Emu_lua_SetQuestFlags);
  1189. lua_register(state, "SetQuestTimer", EQ2Emu_lua_SetQuestTimer);
  1190. lua_register(state, "RemoveQuestStep", EQ2Emu_lua_RemoveQuestStep);
  1191. lua_register(state, "ResetQuestStep", EQ2Emu_lua_ResetQuestStep);
  1192. lua_register(state, "SetQuestTimerComplete", EQ2Emu_lua_SetQuestTimerComplete);
  1193. lua_register(state, "AddQuestStepFailureAction", EQ2Emu_lua_AddQuestStepFailureAction);
  1194. lua_register(state, "SetStepFailed", EQ2Emu_lua_SetStepFailed);
  1195. lua_register(state, "GetQuestCompleteCount", EQ2Emu_lua_GetQuestCompleteCount);
  1196. lua_register(state, "SetServerVariable", EQ2Emu_lua_SetServerVariable);
  1197. lua_register(state, "GetServerVariable", EQ2Emu_lua_GetServerVariable);
  1198. lua_register(state, "HasLanguage", EQ2Emu_lua_HasLanguage);
  1199. lua_register(state, "AddLanguage", EQ2Emu_lua_AddLanguage);
  1200. lua_register(state, "IsNight", EQ2Emu_lua_IsNight);
  1201. lua_register(state, "AddMultiFloorLift", EQ2Emu_lua_AddMultiFloorLift);
  1202. lua_register(state, "StartAutoMount", EQ2Emu_lua_StartAutoMount);
  1203. lua_register(state, "EndAutoMount", EQ2Emu_lua_EndAutoMount);
  1204. lua_register(state, "IsOnAutoMount", EQ2Emu_lua_IsOnAutoMount);
  1205. lua_register(state, "SetPlayerHistory", EQ2Emu_lua_SetPlayerHistory);
  1206. lua_register(state, "GetPlayerHistory", EQ2Emu_lua_GetPlayerHistory);
  1207. lua_register(state, "SetGridID", EQ2Emu_lua_SetGridID);
  1208. lua_register(state, "GetQuestStepProgress", EQ2Emu_lua_GetQuestStepProgress);
  1209. lua_register(state, "SetPlayerLevel", EQ2Emu_lua_SetPlayerLevel);
  1210. lua_register(state, "AddCoin", EQ2Emu_lua_AddCoin);
  1211. lua_register(state, "RemoveCoin", EQ2Emu_lua_RemoveCoin);
  1212. lua_register(state, "GetPlayersInZone", EQ2Emu_lua_GetPlayersInZone);
  1213. lua_register(state, "SpawnGroupByID", EQ2Emu_lua_SpawnGroupByID);
  1214. lua_register(state, "SetSpawnAnimation", EQ2Emu_lua_SetSpawnAnimation);
  1215. lua_register(state, "GetClientVersion", EQ2Emu_lua_GetClientVersion);
  1216. lua_register(state, "GetItemID", EQ2Emu_lua_GetItemID);
  1217. lua_register(state, "IsEntity", EQ2Emu_lua_IsEntity);
  1218. lua_register(state, "GetOrigX", EQ2Emu_lua_GetOrigX);
  1219. lua_register(state, "GetOrigY", EQ2Emu_lua_GetOrigY);
  1220. lua_register(state, "GetOrigZ", EQ2Emu_lua_GetOrigZ);
  1221. lua_register(state, "GetPCTOfHP", EQ2Emu_lua_GetPCTOfHP);
  1222. lua_register(state, "GetPCTOfPower", EQ2Emu_lua_GetPCTOfPower);
  1223. lua_register(state, "GetBoundZoneID", EQ2Emu_lua_GetBoundZoneID);
  1224. lua_register(state, "Evac", EQ2Emu_lua_Evac);
  1225. lua_register(state, "GetSpellTier", EQ2Emu_lua_GetSpellTier);
  1226. lua_register(state, "GetSpellID", EQ2Emu_lua_GetSpellID);
  1227. lua_register(state, "StartTransmute", EQ2Emu_lua_StartTransmute);
  1228. lua_register(state, "CompleteTransmute", EQ2Emu_lua_CompleteTransmute);
  1229. lua_register(state, "ProcHate", EQ2Emu_lua_ProcHate);
  1230. lua_register(state, "GetRandomSpawnByID", EQ2Emu_lua_GetRandomSpawnByID);
  1231. lua_register(state, "ShowLootWindow", EQ2Emu_lua_ShowLootWindow);
  1232. lua_register(state, "AddPrimaryEntityCommandAllSpawns", EQ2Emu_lua_AddPrimaryEntityCommandAllSpawns);
  1233. lua_register(state, "InstructionWindow", EQ2Emu_lua_InstructionWindow);
  1234. lua_register(state, "InstructionWindowClose", EQ2Emu_lua_InstructionWindowClose);
  1235. lua_register(state, "InstructionWindowGoal", EQ2Emu_lua_InstructionWindowGoal);
  1236. lua_register(state, "ShowWindow", EQ2Emu_lua_ShowWindow);
  1237. lua_register(state, "FlashWindow", EQ2Emu_lua_FlashWindow);
  1238. lua_register(state, "EnableGameEvent", EQ2Emu_lua_EnableGameEvent);
  1239. lua_register(state, "GetTutorialStep", EQ2Emu_lua_GetTutorialStep);
  1240. lua_register(state, "SetTutorialStep", EQ2Emu_lua_SetTutorialStep);
  1241. lua_register(state, "DisplayText", EQ2Emu_lua_DisplayText);
  1242. lua_register(state, "GiveExp", EQ2Emu_lua_GiveExp);
  1243. lua_register(state, "CheckLOS", EQ2Emu_lua_CheckLOS);
  1244. lua_register(state, "CheckLOSByCoordinates", EQ2Emu_lua_CheckLOSByCoordinates);
  1245. lua_register(state, "SetZoneExpansionFlag", EQ2Emu_lua_SetZoneExpansionFlag);
  1246. lua_register(state, "GetZoneExpansionFlag", EQ2Emu_lua_GetZoneExpansionFlag);
  1247. lua_register(state, "SetZoneHolidayFlag", EQ2Emu_lua_SetZoneHolidayFlag);
  1248. lua_register(state, "GetZoneHolidayFlag", EQ2Emu_lua_GetZoneHolidayFlag);
  1249. lua_register(state, "SetCanBind", EQ2Emu_lua_SetCanBind);
  1250. lua_register(state, "GetCanBind", EQ2Emu_lua_GetCanBind);
  1251. lua_register(state, "GetCanGate", EQ2Emu_lua_GetCanGate);
  1252. lua_register(state, "SetCanGate", EQ2Emu_lua_SetCanGate);
  1253. lua_register(state, "AddSpawnProximity", EQ2Emu_lua_AddSpawnProximity);
  1254. lua_register(state, "CanSeeInvis", EQ2Emu_lua_CanSeeInvis);
  1255. lua_register(state, "SetSeeInvis", EQ2Emu_lua_SetSeeInvis);
  1256. lua_register(state, "SetSeeHide", EQ2Emu_lua_SetSeeHide);
  1257. lua_register(state, "SetAccessToEntityCommand", EQ2Emu_lua_SetAccessToEntityCommand);
  1258. lua_register(state, "SetAccessToEntityCommandByCharID", EQ2Emu_lua_SetAccessToEntityCommandByCharID);
  1259. lua_register(state, "RemovePrimaryEntityCommand", EQ2Emu_lua_RemovePrimaryEntityCommand);
  1260. lua_register(state, "SendUpdateDefaultCommand", EQ2Emu_lua_SendUpdateDefaultCommand);
  1261. lua_register(state, "SendTransporters", EQ2Emu_lua_SendTransporters);
  1262. lua_register(state, "SetTemporaryTransportID", EQ2Emu_lua_SetTemporaryTransportID);
  1263. lua_register(state, "GetTemporaryTransportID", EQ2Emu_lua_GetTemporaryTransportID);
  1264. lua_register(state, "SetAlignment", EQ2Emu_lua_SetAlignment);
  1265. lua_register(state, "GetAlignment", EQ2Emu_lua_GetAlignment);
  1266. lua_register(state, "GetSpell", EQ2Emu_lua_GetSpell);
  1267. lua_register(state, "GetSpellData", EQ2Emu_lua_GetSpellData);
  1268. lua_register(state, "SetSpellData", EQ2Emu_lua_SetSpellData);
  1269. lua_register(state, "CastCustomSpell", EQ2Emu_lua_CastCustomSpell);
  1270. lua_register(state, "SetSpellDataIndex", EQ2Emu_lua_SetSpellDataIndex);
  1271. lua_register(state, "GetSpellDataIndex", EQ2Emu_lua_GetSpellDataIndex);
  1272. lua_register(state, "SetSpellDisplayEffect", EQ2Emu_lua_SetSpellDisplayEffect);
  1273. lua_register(state, "GetSpellDisplayEffect", EQ2Emu_lua_GetSpellDisplayEffect);
  1274. lua_register(state, "InWater", EQ2Emu_lua_InWater);
  1275. lua_register(state, "InLava", EQ2Emu_lua_InLava);
  1276. lua_register(state, "DamageSpawn", EQ2Emu_lua_DamageSpawn);
  1277. lua_register(state, "IsInvulnerable", EQ2Emu_lua_IsInvulnerable);
  1278. lua_register(state, "SetInvulnerable", EQ2Emu_lua_SetInvulnerable);
  1279. lua_register(state, "GetRuleFlagBool", EQ2Emu_lua_GetRuleFlagBool);
  1280. lua_register(state, "GetRuleFlagInt32", EQ2Emu_lua_GetRuleFlagInt32);
  1281. lua_register(state, "GetRuleFlagFloat", EQ2Emu_lua_GetRuleFlagFloat);
  1282. lua_register(state, "GetAAInfo", EQ2Emu_lua_GetAAInfo);
  1283. lua_register(state, "SetAAInfo", EQ2Emu_lua_SetAAInfo);
  1284. lua_register(state, "AddMasterTitle", EQ2Emu_lua_AddMasterTitle);
  1285. lua_register(state, "AddCharacterTitle", EQ2Emu_lua_AddCharacterTitle);
  1286. lua_register(state, "SetCharacterTitleSuffix", EQ2Emu_lua_SetCharacterTitleSuffix);
  1287. lua_register(state, "SetCharacterTitlePrefix", EQ2Emu_lua_SetCharacterTitlePrefix);
  1288. lua_register(state, "ResetCharacterTitleSuffix", EQ2Emu_lua_ResetCharacterTitleSuffix);
  1289. lua_register(state, "ResetCharacterTitlePrefix", EQ2Emu_lua_ResetCharacterTitlePrefix);
  1290. lua_register(state, "GetInfoStructString", EQ2Emu_lua_GetInfoStructString);
  1291. lua_register(state, "GetInfoStructUInt", EQ2Emu_lua_GetInfoStructUInt);
  1292. lua_register(state, "GetInfoStructSInt", EQ2Emu_lua_GetInfoStructSInt);
  1293. lua_register(state, "GetInfoStructFloat", EQ2Emu_lua_GetInfoStructFloat);
  1294. lua_register(state, "SetInfoStructString", EQ2Emu_lua_SetInfoStructString);
  1295. lua_register(state, "SetInfoStructUInt", EQ2Emu_lua_SetInfoStructUInt);
  1296. lua_register(state, "SetInfoStructSInt", EQ2Emu_lua_SetInfoStructSInt);
  1297. lua_register(state, "SetInfoStructFloat", EQ2Emu_lua_SetInfoStructFloat);
  1298. lua_register(state, "SetCharSheetChanged", EQ2Emu_lua_SetCharSheetChanged);
  1299. lua_register(state, "AddPlayerMail", EQ2Emu_lua_AddPlayerMail);
  1300. lua_register(state, "AddPlayerMailByCharID", EQ2Emu_lua_AddPlayerMailByCharID);
  1301. lua_register(state, "OpenDoor", EQ2Emu_lua_OpenDoor);
  1302. lua_register(state, "CloseDoor", EQ2Emu_lua_CloseDoor);
  1303. lua_register(state, "IsOpen", EQ2Emu_lua_IsOpen);
  1304. lua_register(state, "MakeRandomInt", EQ2Emu_lua_MakeRandomInt);
  1305. lua_register(state, "MakeRandomFloat", EQ2Emu_lua_MakeRandomFloat);
  1306. lua_register(state, "AddIconValue", EQ2Emu_lua_AddIconValue);
  1307. lua_register(state, "RemoveIconValue", EQ2Emu_lua_RemoveIconValue);
  1308. lua_register(state, "GetShardID", EQ2Emu_lua_GetShardID);
  1309. lua_register(state, "GetShardCharID", EQ2Emu_lua_GetShardCharID);
  1310. lua_register(state, "GetShardCreatedTimestamp", EQ2Emu_lua_GetShardCreatedTimestamp);
  1311. lua_register(state, "DeleteDBShardID", EQ2Emu_lua_DeleteDBShardID);
  1312. lua_register(state, "PauseMovement", EQ2Emu_lua_PauseMovement);
  1313. lua_register(state, "StopMovement", EQ2Emu_lua_StopMovement);
  1314. lua_register(state, "GetArrowColor", EQ2Emu_lua_GetArrowColor);
  1315. lua_register(state, "GetTSArrowColor", EQ2Emu_lua_GetTSArrowColor);
  1316. lua_register(state, "GetSpawnByRailID", EQ2Emu_lua_GetSpawnByRailID);
  1317. lua_register(state, "SetRailID", EQ2Emu_lua_SetRailID);
  1318. lua_register(state, "IsZoneLoading", EQ2Emu_lua_IsZoneLoading);
  1319. lua_register(state, "IsRunning", EQ2Emu_lua_IsRunning);
  1320. lua_register(state, "GetZoneLockoutTimer", EQ2Emu_lua_GetZoneLockoutTimer);
  1321. lua_register(state, "SetWorldTime", EQ2Emu_lua_SetWorldTime);
  1322. lua_register(state, "GetWorldTimeYear", EQ2Emu_lua_GetWorldTimeYear);
  1323. lua_register(state, "GetWorldTimeMonth", EQ2Emu_lua_GetWorldTimeMonth);
  1324. lua_register(state, "GetWorldTimeHour", EQ2Emu_lua_GetWorldTimeHour);
  1325. lua_register(state, "GetWorldTimeMinute", EQ2Emu_lua_GetWorldTimeMinute);
  1326. lua_register(state, "SendTimeUpdate", EQ2Emu_lua_SendTimeUpdate);
  1327. lua_register(state, "GetLootTier", EQ2Emu_lua_GetLootTier);
  1328. lua_register(state, "SetLootTier", EQ2Emu_lua_SetLootTier);
  1329. lua_register(state, "GetLootDropType", EQ2Emu_lua_GetLootDropType);
  1330. lua_register(state, "SetLootDropType", EQ2Emu_lua_SetLootDropType);
  1331. lua_register(state, "DamageEquippedItems", EQ2Emu_lua_DamageEquippedItems);
  1332. lua_register(state, "CreateWidgetRegion", EQ2Emu_lua_CreateWidgetRegion);
  1333. lua_register(state, "RemoveRegion", EQ2Emu_lua_RemoveRegion);
  1334. lua_register(state, "SetPlayerPOVGhost", EQ2Emu_lua_SetPlayerPOVGhost);
  1335. lua_register(state, "SetCastOnAggroComplete", EQ2Emu_lua_SetCastOnAggroComplete);
  1336. lua_register(state, "IsCastOnAggroComplete", EQ2Emu_lua_IsCastOnAggroComplete);
  1337. }
  1338. void LuaInterface::LogError(const char* error, ...) {
  1339. va_list argptr;
  1340. char buffer[4096];
  1341. va_start(argptr, error);
  1342. vsnprintf(buffer, sizeof(buffer), error, argptr);
  1343. va_end(argptr);
  1344. SimpleLogError(buffer);
  1345. }
  1346. void LuaInterface::SimpleLogError(const char* error) {
  1347. ProcessErrorMessage(error);
  1348. LogWrite(LUA__ERROR, 0, "LUA", "%s", error);
  1349. }
  1350. void LuaInterface::ResetFunctionStack(lua_State* state) {
  1351. lua_settop(state, 0);
  1352. }
  1353. void LuaInterface::AddUserDataPtr(LUAUserData* data) {
  1354. MLUAUserData.lock();
  1355. user_data[data] = Timer::GetCurrentTime2() + 300000; //allow a function to use this pointer for 5 minutes
  1356. MLUAUserData.unlock();
  1357. }
  1358. void LuaInterface::DeletePendingSpells(bool all) {
  1359. MSpellDelete.lock();
  1360. if (spells_pending_delete.size() > 0) {
  1361. int32 time = Timer::GetCurrentTime2();
  1362. map<LuaSpell*, int32>::iterator itr;
  1363. vector<LuaSpell*> tmp_deletes;
  1364. vector<LuaSpell*>::iterator del_itr;
  1365. for (itr = spells_pending_delete.begin(); itr != spells_pending_delete.end(); itr++) {
  1366. if (all || time >= itr->second)
  1367. tmp_deletes.push_back(itr->first);
  1368. }
  1369. LuaSpell* spell = 0;
  1370. for (del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++) {
  1371. spell = *del_itr;
  1372. if (spell->caster) {
  1373. spell->caster->GetZone()->GetSpellProcess()->DeleteActiveSpell(spell);
  1374. }
  1375. else if(spell->targets.size() > 0) {
  1376. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1377. for (int8 i = 0; i < spell->targets.size(); i++) {
  1378. Spawn* target = spell->caster->GetZone()->GetSpawnByID(spell->targets.at(i));
  1379. if (!target || !target->IsEntity())
  1380. continue;
  1381. target->GetZone()->GetSpellProcess()->DeleteActiveSpell(spell);
  1382. }
  1383. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1384. }
  1385. spells_pending_delete.erase(spell);
  1386. if (spell->spell->IsCopiedSpell())
  1387. {
  1388. RemoveCustomSpell(spell->spell->GetSpellID());
  1389. safe_delete(spell->spell);
  1390. }
  1391. safe_delete(spell);
  1392. }
  1393. }
  1394. MSpellDelete.unlock();
  1395. }
  1396. void LuaInterface::DeletePendingSpell(LuaSpell* spell) {
  1397. MSpellDelete.lock();
  1398. if (spells_pending_delete.size() > 0) {
  1399. map<LuaSpell*, int32>::iterator itr = spells_pending_delete.find(spell);
  1400. if (itr != spells_pending_delete.end())
  1401. spells_pending_delete.erase(itr);
  1402. }
  1403. MSpellDelete.unlock();
  1404. }
  1405. void LuaInterface::DeleteUserDataPtrs(bool all) {
  1406. MLUAUserData.lock();
  1407. if(user_data.size() > 0){
  1408. map<LUAUserData*, int32>::iterator itr;
  1409. int32 time = Timer::GetCurrentTime2();
  1410. vector<LUAUserData*> tmp_deletes;
  1411. vector<LUAUserData*>::iterator del_itr;
  1412. for(itr = user_data.begin(); itr != user_data.end(); itr++){
  1413. if(all || time >= itr->second)
  1414. tmp_deletes.push_back(itr->first);
  1415. }
  1416. LUAUserData* data = 0;
  1417. for(del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++){
  1418. data = *del_itr;
  1419. user_data.erase(data);
  1420. safe_delete(data);
  1421. }
  1422. }
  1423. MLUAUserData.unlock();
  1424. }
  1425. Spawn* LuaInterface::GetSpawn(lua_State* state, int8 arg_num) {
  1426. Spawn* ret = 0;
  1427. if (lua_islightuserdata(state, arg_num)){
  1428. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1429. if(!data || !data->IsCorrectlyInitialized()){
  1430. LogError("%s: GetSpawn error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1431. }
  1432. else if(!data->IsSpawn()){
  1433. lua_Debug ar;
  1434. lua_getstack (state, 1, &ar);
  1435. lua_getinfo(state, "Sln", &ar);
  1436. LogError("%s: Invalid data type used for GetSpawn in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1437. }
  1438. else
  1439. ret = data->spawn;
  1440. }
  1441. return ret;
  1442. }
  1443. vector<ConversationOption>* LuaInterface::GetConversation(lua_State* state, int8 arg_num) {
  1444. vector<ConversationOption>* ret = 0;
  1445. if(lua_islightuserdata(state, arg_num)){
  1446. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1447. if(!data || !data->IsCorrectlyInitialized()){
  1448. LogError("%s: GetConversation error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1449. }
  1450. else if(!data->IsConversationOption()){
  1451. lua_Debug ar;
  1452. lua_getstack (state, 1, &ar);
  1453. lua_getinfo(state, "Sln", &ar);
  1454. LogError("%s: Invalid data type used for GetConversation in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1455. }
  1456. else
  1457. ret = data->conversation_options;
  1458. }
  1459. return ret;
  1460. }
  1461. vector<Spawn*>* LuaInterface::GetSpawnList(lua_State* state, int8 arg_num) {
  1462. vector<Spawn*>* ret = 0;
  1463. if (lua_islightuserdata(state, arg_num)) {
  1464. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1465. if (!data || !data->IsCorrectlyInitialized()) {
  1466. LogError("%s: GetSpawnList error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1467. }
  1468. else if (!data->IsSpawnList()) {
  1469. lua_Debug ar;
  1470. lua_getstack(state, 1, &ar);
  1471. lua_getinfo(state, "Sln", &ar);
  1472. LogError("%s: Invalid data type used for GetSpawnList in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1473. }
  1474. else
  1475. ret = data->spawn_list;
  1476. }
  1477. return ret;
  1478. }
  1479. vector<OptionWindowOption>* LuaInterface::GetOptionWindow(lua_State* state, int8 arg_num) {
  1480. vector<OptionWindowOption>* ret = 0;
  1481. if(lua_islightuserdata(state, arg_num)){
  1482. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1483. if(!data || !data->IsCorrectlyInitialized()){
  1484. LogError("%s: GetOptionWindow error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1485. }
  1486. else if(!data->IsOptionWindow()){
  1487. lua_Debug ar;
  1488. lua_getstack (state, 1, &ar);
  1489. lua_getinfo(state, "Sln", &ar);
  1490. LogError("%s: Invalid data type used for GetOptionWindow in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1491. }
  1492. else
  1493. ret = data->option_window_option;
  1494. }
  1495. return ret;
  1496. }
  1497. Quest* LuaInterface::GetQuest(lua_State* state, int8 arg_num) {
  1498. Quest* ret = 0;
  1499. if(lua_islightuserdata(state, arg_num)){
  1500. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1501. if(!data || !data->IsCorrectlyInitialized()){
  1502. LogError("%s: GetQuest error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1503. }
  1504. else if(!data->IsQuest()){
  1505. lua_Debug ar;
  1506. lua_getstack (state, 1, &ar);
  1507. lua_getinfo(state, "Sln", &ar);
  1508. LogError("%s: Invalid data type used for GetQuest in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1509. }
  1510. else
  1511. ret = data->quest;
  1512. }
  1513. return ret;
  1514. }
  1515. Item* LuaInterface::GetItem(lua_State* state, int8 arg_num) {
  1516. Item* ret = 0;
  1517. if(lua_islightuserdata(state, arg_num)){
  1518. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1519. if(!data || !data->IsCorrectlyInitialized()){
  1520. LogError("%s: GetItem error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1521. }
  1522. else if(!data->IsItem()){
  1523. lua_Debug ar;
  1524. lua_getstack (state, 1, &ar);
  1525. lua_getinfo(state, "Sln", &ar);
  1526. LogError("%s: Invalid data type used for GetItem in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1527. }
  1528. else
  1529. ret = data->item;
  1530. }
  1531. return ret;
  1532. }
  1533. Skill* LuaInterface::GetSkill(lua_State* state, int8 arg_num) {
  1534. Skill* ret = 0;
  1535. if (lua_islightuserdata(state, arg_num)) {
  1536. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1537. if (!data || !data->IsCorrectlyInitialized()) {
  1538. LogError("%s: GetSkill error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1539. }
  1540. else if (!data->IsSkill()) {
  1541. lua_Debug ar;
  1542. lua_getstack(state, 1, &ar);
  1543. lua_getinfo(state, "Sln", &ar);
  1544. LogError("%s: Invalid data type used for GetSkill in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1545. }
  1546. else
  1547. ret = data->skill;
  1548. }
  1549. return ret;
  1550. }
  1551. LuaSpell* LuaInterface::GetSpell(lua_State* state, int8 arg_num) {
  1552. LuaSpell* ret = 0;
  1553. if (lua_islightuserdata(state, arg_num)) {
  1554. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1555. if (!data || !data->IsCorrectlyInitialized()) {
  1556. LogError("%s: GetSpell error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1557. }
  1558. else if (!data->IsSpell()) {
  1559. lua_Debug ar;
  1560. lua_getstack(state, 1, &ar);
  1561. lua_getinfo(state, "Sln", &ar);
  1562. LogError("%s: Invalid data type used for GetSpell in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1563. }
  1564. else
  1565. ret = data->spell;
  1566. }
  1567. return ret;
  1568. }
  1569. ZoneServer* LuaInterface::GetZone(lua_State* state, int8 arg_num) {
  1570. ZoneServer* ret = 0;
  1571. if(lua_islightuserdata(state, arg_num)){
  1572. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1573. if(!data || !data->IsCorrectlyInitialized()){
  1574. LogError("%s: GetZone error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1575. }
  1576. else if(!data->IsZone()){
  1577. lua_Debug ar;
  1578. lua_getstack (state, 1, &ar);
  1579. lua_getinfo(state, "Sln", &ar);
  1580. LogError("%s: Invalid data type used for GetZone in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1581. }
  1582. else
  1583. ret = data->zone;
  1584. }
  1585. return ret;
  1586. }
  1587. sint64 LuaInterface::GetSInt64Value(lua_State* state, int8 arg_num) {
  1588. sint64 val = 0;
  1589. if(lua_isnumber(state, arg_num)){
  1590. val = (sint64)lua_tointeger(state, arg_num);
  1591. }
  1592. return val;
  1593. }
  1594. int64 LuaInterface::GetInt64Value(lua_State* state, int8 arg_num) {
  1595. int64 val = 0;
  1596. if(lua_isnumber(state, arg_num)){
  1597. val = (int64)lua_tonumber(state, arg_num);
  1598. }
  1599. return val;
  1600. }
  1601. sint32 LuaInterface::GetSInt32Value(lua_State* state, int8 arg_num) {
  1602. sint32 val = 0;
  1603. if(lua_isnumber(state, arg_num)){
  1604. val = lua_tointeger(state, arg_num);
  1605. }
  1606. return val;
  1607. }
  1608. int32 LuaInterface::GetInt32Value(lua_State* state, int8 arg_num) {
  1609. int32 val = 0;
  1610. if(lua_isnumber(state, arg_num)){
  1611. val = (int32)lua_tonumber(state, arg_num);
  1612. }
  1613. return val;
  1614. }
  1615. int16 LuaInterface::GetInt16Value(lua_State* state, int8 arg_num) {
  1616. int16 val = 0;
  1617. if(lua_isnumber(state, arg_num)){
  1618. val = lua_tointeger(state, arg_num);
  1619. }
  1620. return val;
  1621. }
  1622. int8 LuaInterface::GetInt8Value(lua_State* state, int8 arg_num) {
  1623. int8 val = 0;
  1624. if(lua_isnumber(state, arg_num)){
  1625. val = lua_tointeger(state, arg_num);
  1626. }
  1627. return val;
  1628. }
  1629. float LuaInterface::GetFloatValue(lua_State* state, int8 arg_num) {
  1630. float val = 0;
  1631. if(lua_isnumber(state, arg_num))
  1632. val = (float)lua_tonumber(state, arg_num);
  1633. return val;
  1634. }
  1635. string LuaInterface::GetStringValue(lua_State* state, int8 arg_num) {
  1636. string val;
  1637. if(lua_isstring(state, arg_num)){
  1638. size_t size = 0;
  1639. const char* str = lua_tolstring(state, arg_num, &size);
  1640. if(str)
  1641. val = string(str);
  1642. }
  1643. return val;
  1644. }
  1645. bool LuaInterface::GetBooleanValue(lua_State* state, int8 arg_num) {
  1646. return lua_toboolean(state, arg_num) == 1;
  1647. }
  1648. void LuaInterface::SetStringValue(lua_State* state, const char* value) {
  1649. lua_pushstring(state, value);
  1650. }
  1651. void LuaInterface::SetBooleanValue(lua_State* state, bool value) {
  1652. lua_pushboolean(state, value);
  1653. }
  1654. void LuaInterface::SetFloatValue(lua_State* state, float value) {
  1655. lua_pushnumber(state, value);
  1656. }
  1657. void LuaInterface::SetInt32Value(lua_State* state, int32 value) {
  1658. lua_pushinteger(state, value);
  1659. }
  1660. void LuaInterface::SetSInt32Value(lua_State* state, sint32 value) {
  1661. lua_pushinteger(state, value);
  1662. }
  1663. void LuaInterface::SetInt64Value(lua_State* state, int64 value) {
  1664. lua_pushinteger(state, value);
  1665. }
  1666. void LuaInterface::SetSInt64Value(lua_State* state, sint64 value) {
  1667. lua_pushinteger(state, value);
  1668. }
  1669. void LuaInterface::SetSpawnValue(lua_State* state, Spawn* spawn) {
  1670. LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper();
  1671. spawn_wrapper->spawn = spawn;
  1672. AddUserDataPtr(spawn_wrapper);
  1673. lua_pushlightuserdata(state, spawn_wrapper);
  1674. }
  1675. void LuaInterface::SetSpawnListValue(lua_State* state, vector<Spawn*>* spawnList) {
  1676. LUASpawnListWrapper* spawnList_wrapper = new LUASpawnListWrapper();
  1677. spawnList_wrapper->spawn_list = spawnList;
  1678. AddUserDataPtr(spawnList_wrapper);
  1679. lua_pushlightuserdata(state, spawnList_wrapper);
  1680. }
  1681. void LuaInterface::SetConversationValue(lua_State* state, vector<ConversationOption>* conversation) {
  1682. LUAConversationOptionWrapper* conv_wrapper = new LUAConversationOptionWrapper();
  1683. conv_wrapper->conversation_options = conversation;
  1684. AddUserDataPtr(conv_wrapper);
  1685. lua_pushlightuserdata(state, conv_wrapper);
  1686. }
  1687. void LuaInterface::SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow) {
  1688. LUAOptionWindowWrapper* option_wrapper = new LUAOptionWindowWrapper();
  1689. option_wrapper->option_window_option = optionWindow;
  1690. AddUserDataPtr(option_wrapper);
  1691. lua_pushlightuserdata(state, option_wrapper);
  1692. }
  1693. void LuaInterface::SetItemValue(lua_State* state, Item* item) {
  1694. LUAItemWrapper* item_wrapper = new LUAItemWrapper();
  1695. item_wrapper->item = item;
  1696. AddUserDataPtr(item_wrapper);
  1697. lua_pushlightuserdata(state, item_wrapper);
  1698. }
  1699. void LuaInterface::SetSkillValue(lua_State* state, Skill* skill) {
  1700. LUASkillWrapper* skill_wrapper = new LUASkillWrapper();
  1701. skill_wrapper->skill = skill;
  1702. AddUserDataPtr(skill_wrapper);
  1703. lua_pushlightuserdata(state, skill_wrapper);
  1704. }
  1705. void LuaInterface::SetQuestValue(lua_State* state, Quest* quest) {
  1706. LUAQuestWrapper* quest_wrapper = new LUAQuestWrapper();
  1707. quest_wrapper->quest = quest;
  1708. AddUserDataPtr(quest_wrapper);
  1709. lua_pushlightuserdata(state, quest_wrapper);
  1710. }
  1711. void LuaInterface::SetZoneValue(lua_State* state, ZoneServer* zone) {
  1712. LUAZoneWrapper* zone_wrapper = new LUAZoneWrapper();
  1713. zone_wrapper->zone = zone;
  1714. AddUserDataPtr(zone_wrapper);
  1715. lua_pushlightuserdata(state, zone_wrapper);
  1716. }
  1717. void LuaInterface::SetSpellValue(lua_State* state, LuaSpell* spell) {
  1718. LUASpellWrapper* spell_wrapper = new LUASpellWrapper();
  1719. spell_wrapper->spell = spell;
  1720. AddUserDataPtr(spell_wrapper);
  1721. lua_pushlightuserdata(state, spell_wrapper);
  1722. }
  1723. LuaSpell* LuaInterface::GetSpell(const char* name) {
  1724. string lua_script = string(name);
  1725. if (lua_script.find(".lua") == string::npos)
  1726. lua_script.append(".lua");
  1727. if(spells.count(lua_script) > 0)
  1728. {
  1729. LogWrite(LUA__DEBUG, 0, "LUA", "Found LUA Spell Script: '%s'", lua_script.c_str());
  1730. LuaSpell* spell = spells[lua_script];
  1731. LuaSpell* new_spell = new LuaSpell;
  1732. new_spell->state = spell->state;
  1733. new_spell->file_name = string(spell->file_name);
  1734. new_spell->timer = spell->timer;
  1735. new_spell->timer.Disable();
  1736. new_spell->resisted = false;
  1737. new_spell->interrupted = false;
  1738. new_spell->crit = false;
  1739. new_spell->last_spellattack_hit = false;
  1740. new_spell->MSpellTargets.SetName("LuaSpell.MSpellTargets");
  1741. new_spell->cancel_after_all_triggers = false;
  1742. new_spell->num_triggers = 0;
  1743. new_spell->num_calls = 0;
  1744. new_spell->is_recast_timer = false;
  1745. new_spell->had_triggers = false;
  1746. new_spell->had_dmg_remaining = false;
  1747. new_spell->slot_pos = 0;
  1748. new_spell->damage_remaining = 0;
  1749. new_spell->effect_bitmask = 0;
  1750. new_spell->caster = 0;
  1751. new_spell->initial_target = 0;
  1752. new_spell->spell = 0;
  1753. new_spell->restored = false;
  1754. new_spell->initial_caster_char_id = 0;
  1755. new_spell->initial_target_char_id = 0;
  1756. return new_spell;
  1757. }
  1758. else{
  1759. LogWrite(LUA__ERROR, 0, "LUA", "Error LUA Spell Script: '%s'", name);
  1760. return 0;
  1761. }
  1762. }
  1763. Mutex* LuaInterface::GetItemScriptMutex(const char* name) {
  1764. Mutex* mutex = 0;
  1765. if(item_scripts_mutex.count(name) > 0)
  1766. mutex = item_scripts_mutex[name];
  1767. if(!mutex){
  1768. mutex = new Mutex();
  1769. item_scripts_mutex[name] = mutex;
  1770. }
  1771. return mutex;
  1772. }
  1773. Mutex* LuaInterface::GetSpawnScriptMutex(const char* name) {
  1774. Mutex* mutex = 0;
  1775. if(spawn_scripts_mutex.count(string(name)) > 0)
  1776. mutex = spawn_scripts_mutex[name];
  1777. if(!mutex){
  1778. mutex = new Mutex();
  1779. spawn_scripts_mutex[name] = mutex;
  1780. }
  1781. return mutex;
  1782. }
  1783. Mutex* LuaInterface::GetZoneScriptMutex(const char* name) {
  1784. Mutex* mutex = 0;
  1785. if(zone_scripts_mutex.count(name) > 0)
  1786. mutex = zone_scripts_mutex[name];
  1787. if(!mutex){
  1788. mutex = new Mutex();
  1789. zone_scripts_mutex[name] = mutex;
  1790. }
  1791. return mutex;
  1792. }
  1793. Mutex* LuaInterface::GetRegionScriptMutex(const char* name) {
  1794. Mutex* mutex = 0;
  1795. if(region_scripts_mutex.count(name) > 0)
  1796. mutex = region_scripts_mutex[name];
  1797. if(!mutex){
  1798. mutex = new Mutex();
  1799. region_scripts_mutex[name] = mutex;
  1800. }
  1801. return mutex;
  1802. }
  1803. void LuaInterface::UseItemScript(const char* name, lua_State* state, bool val) {
  1804. MItemScripts.writelock(__FUNCTION__, __LINE__);
  1805. item_scripts[name][state] = val;
  1806. item_inverse_scripts[state] = name;
  1807. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  1808. }
  1809. void LuaInterface::UseSpawnScript(const char* name, lua_State* state, bool val) {
  1810. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  1811. spawn_scripts[name][state] = val;
  1812. spawn_inverse_scripts[state] = name;
  1813. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  1814. }
  1815. void LuaInterface::UseZoneScript(const char* name, lua_State* state, bool val) {
  1816. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  1817. zone_scripts[name][state] = val;
  1818. zone_inverse_scripts[state] = name;
  1819. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  1820. }
  1821. void LuaInterface::UseRegionScript(const char* name, lua_State* state, bool val) {
  1822. MRegionScripts.writelock(__FUNCTION__, __LINE__);
  1823. region_scripts[name][state] = val;
  1824. region_inverse_scripts[state] = name;
  1825. MRegionScripts.releasewritelock(__FUNCTION__, __LINE__);
  1826. }
  1827. lua_State* LuaInterface::GetItemScript(const char* name, bool create_new, bool use) {
  1828. map<string, map<lua_State*, bool> >::iterator itr;
  1829. map<lua_State*, bool>::iterator item_script_itr;
  1830. lua_State* ret = 0;
  1831. Mutex* mutex = 0;
  1832. itr = item_scripts.find(name);
  1833. if(itr != item_scripts.end()) {
  1834. mutex = GetItemScriptMutex(name);
  1835. mutex->readlock(__FUNCTION__, __LINE__);
  1836. for(item_script_itr = itr->second.begin(); item_script_itr != itr->second.end(); item_script_itr++){
  1837. if(!item_script_itr->second){ //not in use
  1838. ret = item_script_itr->first;
  1839. if (use)
  1840. {
  1841. item_script_itr->second = true;
  1842. break; // don't keep iterating, we already have our result
  1843. }
  1844. }
  1845. }
  1846. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1847. }
  1848. if(!ret && create_new){
  1849. if(name && LoadItemScript(name))
  1850. ret = GetItemScript(name, create_new, use);
  1851. else{
  1852. LogError("Error LUA Item Script '%s'", name);
  1853. return 0;
  1854. }
  1855. }
  1856. return ret;
  1857. }
  1858. lua_State* LuaInterface::GetSpawnScript(const char* name, bool create_new, bool use) {
  1859. if (lua_system_reloading)
  1860. return 0;
  1861. map<string, map<lua_State*, bool> >::iterator itr;
  1862. map<lua_State*, bool>::iterator spawn_script_itr;
  1863. lua_State* ret = 0;
  1864. Mutex* mutex = 0;
  1865. itr = spawn_scripts.find(name);
  1866. if(itr != spawn_scripts.end()) {
  1867. mutex = GetSpawnScriptMutex(name);
  1868. mutex->readlock(__FUNCTION__, __LINE__);
  1869. for(spawn_script_itr = itr->second.begin(); spawn_script_itr != itr->second.end(); spawn_script_itr++){
  1870. if(!spawn_script_itr->second){ //not in use
  1871. ret = spawn_script_itr->first;
  1872. if (use)
  1873. {
  1874. spawn_script_itr->second = true;
  1875. break; // don't keep iterating, we already have our result
  1876. }
  1877. }
  1878. }
  1879. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1880. }
  1881. if(!ret && create_new){
  1882. if(name && LoadSpawnScript(name))
  1883. ret = GetSpawnScript(name, create_new, use);
  1884. else{
  1885. LogError("Error LUA Spawn Script '%s'", name);
  1886. return 0;
  1887. }
  1888. }
  1889. return ret;
  1890. }
  1891. lua_State* LuaInterface::GetZoneScript(const char* name, bool create_new, bool use) {
  1892. map<string, map<lua_State*, bool> >::iterator itr;
  1893. map<lua_State*, bool>::iterator zone_script_itr;
  1894. lua_State* ret = 0;
  1895. Mutex* mutex = 0;
  1896. itr = zone_scripts.find(name);
  1897. if(itr != zone_scripts.end()) {
  1898. mutex = GetZoneScriptMutex(name);
  1899. mutex->readlock(__FUNCTION__, __LINE__);
  1900. for(zone_script_itr = itr->second.begin(); zone_script_itr != itr->second.end(); zone_script_itr++){
  1901. if(!zone_script_itr->second){ //not in use
  1902. ret = zone_script_itr->first;
  1903. if (use)
  1904. {
  1905. zone_script_itr->second = true;
  1906. break; // don't keep iterating, we already have our result
  1907. }
  1908. }
  1909. }
  1910. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1911. }
  1912. if(!ret && create_new){
  1913. if(name && LoadZoneScript(name))
  1914. ret = GetZoneScript(name);
  1915. else{
  1916. LogError("Error LUA Zone Script '%s'", name);
  1917. return 0;
  1918. }
  1919. }
  1920. return ret;
  1921. }
  1922. lua_State* LuaInterface::GetRegionScript(const char* name, bool create_new, bool use) {
  1923. map<string, map<lua_State*, bool> >::iterator itr;
  1924. map<lua_State*, bool>::iterator region_script_itr;
  1925. lua_State* ret = 0;
  1926. Mutex* mutex = 0;
  1927. itr = region_scripts.find(name);
  1928. if(itr != region_scripts.end()) {
  1929. mutex = GetRegionScriptMutex(name);
  1930. mutex->readlock(__FUNCTION__, __LINE__);
  1931. for(region_script_itr = itr->second.begin(); region_script_itr != itr->second.end(); region_script_itr++){
  1932. if(!region_script_itr->second){ //not in use
  1933. ret = region_script_itr->first;
  1934. if (use)
  1935. {
  1936. region_script_itr->second = true;
  1937. break; // don't keep iterating, we already have our result
  1938. }
  1939. }
  1940. }
  1941. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1942. }
  1943. if(!ret && create_new){
  1944. if(name && LoadRegionScript(name))
  1945. ret = GetRegionScript(name);
  1946. else{
  1947. LogError("Error LUA Zone Script '%s'", name);
  1948. return 0;
  1949. }
  1950. }
  1951. return ret;
  1952. }
  1953. bool LuaInterface::RunItemScript(string script_name, const char* function_name, Item* item, Spawn* spawn, Spawn* target, sint64* returnValue) {
  1954. if(!item)
  1955. return false;
  1956. lua_State* state = GetItemScript(script_name.c_str(), true, true);
  1957. if(state){
  1958. Mutex* mutex = GetItemScriptMutex(script_name.c_str());
  1959. if(mutex)
  1960. mutex->readlock(__FUNCTION__, __LINE__);
  1961. else{
  1962. LogError("Error getting lock for '%s'", script_name.c_str());
  1963. UseItemScript(script_name.c_str(), state, false);
  1964. return false;
  1965. }
  1966. lua_getglobal(state, function_name);
  1967. if (!lua_isfunction(state, lua_gettop(state))){
  1968. lua_pop(state, 1);
  1969. mutex->releasereadlock(__FUNCTION__);
  1970. UseItemScript(script_name.c_str(), state, false);
  1971. return false;
  1972. }
  1973. SetItemValue(state, item);
  1974. int8 num_parms = 1;
  1975. if(spawn){
  1976. SetSpawnValue(state, spawn);
  1977. num_parms++;
  1978. }
  1979. if(target){
  1980. SetSpawnValue(state, target);
  1981. num_parms++;
  1982. }
  1983. if(!CallItemScript(state, num_parms, returnValue)){
  1984. if(mutex)
  1985. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1986. UseItemScript(script_name.c_str(), state, false);
  1987. return false;
  1988. }
  1989. if(mutex)
  1990. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1991. UseItemScript(script_name.c_str(), state, false);
  1992. return true;
  1993. }
  1994. else
  1995. return false;
  1996. }
  1997. bool LuaInterface::RunItemScriptWithReturnString(string script_name, const char* function_name, Item* item, Spawn* spawn, std::string* returnValue) {
  1998. if(!item)
  1999. return false;
  2000. lua_State* state = GetItemScript(script_name.c_str(), true, true);
  2001. if(state){
  2002. Mutex* mutex = GetItemScriptMutex(script_name.c_str());
  2003. if(mutex)
  2004. mutex->readlock(__FUNCTION__, __LINE__);
  2005. else{
  2006. LogError("Error getting lock for '%s'", script_name.c_str());
  2007. UseItemScript(script_name.c_str(), state, false);
  2008. return false;
  2009. }
  2010. lua_getglobal(state, function_name);
  2011. if (!lua_isfunction(state, lua_gettop(state))){
  2012. lua_pop(state, 1);
  2013. mutex->releasereadlock(__FUNCTION__);
  2014. UseItemScript(script_name.c_str(), state, false);
  2015. return false;
  2016. }
  2017. SetItemValue(state, item);
  2018. int8 num_parms = 1;
  2019. if(spawn){
  2020. SetSpawnValue(state, spawn);
  2021. num_parms++;
  2022. }
  2023. if(!CallItemScript(state, num_parms, returnValue)){
  2024. if(mutex)
  2025. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2026. UseItemScript(script_name.c_str(), state, false);
  2027. return false;
  2028. }
  2029. if(mutex)
  2030. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2031. UseItemScript(script_name.c_str(), state, false);
  2032. return true;
  2033. }
  2034. else
  2035. return false;
  2036. }
  2037. bool LuaInterface::RunSpawnScript(string script_name, const char* function_name, Spawn* npc, Spawn* spawn, const char* message, bool is_door_open, sint32 input_value, sint32* return_value) {
  2038. if(!npc || lua_system_reloading)
  2039. return false;
  2040. bool isUseDoorFunction = false;
  2041. if(!strcmp(function_name,"usedoor"))
  2042. isUseDoorFunction = true;
  2043. lua_State* state = GetSpawnScript(script_name.c_str(), true, true);
  2044. if(state){
  2045. Mutex* mutex = GetSpawnScriptMutex(script_name.c_str());
  2046. if(mutex)
  2047. mutex->readlock(__FUNCTION__, __LINE__);
  2048. else{
  2049. LogError("Error getting lock for '%s'", script_name.c_str());
  2050. UseSpawnScript(script_name.c_str(), state, false);
  2051. return false;
  2052. }
  2053. lua_getglobal(state, function_name);
  2054. if (!lua_isfunction(state, lua_gettop(state))){
  2055. lua_pop(state, 1);
  2056. mutex->releasereadlock(__FUNCTION__);
  2057. UseSpawnScript(script_name.c_str(), state, false);
  2058. return false;
  2059. }
  2060. SetSpawnValue(state, npc);
  2061. int8 num_parms = 1;
  2062. // we always send spawn, even if null (nil) when its 'usedoor' function
  2063. if(spawn || isUseDoorFunction){
  2064. SetSpawnValue(state, spawn);
  2065. num_parms++;
  2066. }
  2067. // usedoor function always passes just npc, spawn and is_door_open
  2068. if(isUseDoorFunction){
  2069. SetBooleanValue(state, is_door_open);
  2070. num_parms++;
  2071. }
  2072. else {
  2073. if(message){
  2074. SetStringValue(state, message);
  2075. num_parms++;
  2076. }
  2077. if(!strcmp(function_name,"healthchanged"))
  2078. {
  2079. // passes as damage dealt
  2080. SetSInt32Value(state, input_value);
  2081. num_parms++;
  2082. }
  2083. }
  2084. if(!CallScriptSInt32(state, num_parms, return_value)){
  2085. if(mutex)
  2086. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2087. UseSpawnScript(script_name.c_str(), state, false);
  2088. return false;
  2089. }
  2090. if(mutex)
  2091. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2092. UseSpawnScript(script_name.c_str(), state, false);
  2093. return true;
  2094. }
  2095. else
  2096. return false;
  2097. }
  2098. bool LuaInterface::RunZoneScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, int32 int32_arg1, const char* str_arg1, Spawn* spawn_arg1, int32 int32_arg2, const char* str_arg2, Spawn* spawn_arg2) {
  2099. if (!zone)
  2100. return false;
  2101. lua_State* state = GetZoneScript(script_name.c_str(), true, true);
  2102. if (state) {
  2103. Mutex* mutex = GetZoneScriptMutex(script_name.c_str());
  2104. if (mutex)
  2105. mutex->readlock(__FUNCTION__, __LINE__);
  2106. else {
  2107. LogError("Error getting lock for '%s'", script_name.c_str());
  2108. UseZoneScript(script_name.c_str(), state, false);
  2109. return false;
  2110. }
  2111. lua_getglobal(state, function_name);
  2112. if (!lua_isfunction(state, lua_gettop(state))) {
  2113. lua_pop(state, 1);
  2114. mutex->releasereadlock(__FUNCTION__);
  2115. UseZoneScript(script_name.c_str(), state, false);
  2116. return false;
  2117. }
  2118. SetZoneValue(state, zone);
  2119. int8 num_params = 1;
  2120. if (spawn) {
  2121. SetSpawnValue(state, spawn);
  2122. num_params++;
  2123. }
  2124. if (int32_arg1 > 0) {
  2125. SetInt32Value(state, int32_arg1);
  2126. num_params++;
  2127. }
  2128. if (str_arg1) {
  2129. SetStringValue(state, str_arg1);
  2130. num_params++;
  2131. }
  2132. if (spawn_arg1) {
  2133. SetSpawnValue(state, spawn_arg1);
  2134. num_params++;
  2135. }
  2136. if (int32_arg2 > 0) {
  2137. SetInt32Value(state, int32_arg2);
  2138. num_params++;
  2139. }
  2140. if (str_arg2) {
  2141. SetStringValue(state, str_arg2);
  2142. num_params++;
  2143. }
  2144. if (spawn_arg2) {
  2145. SetSpawnValue(state, spawn_arg2);
  2146. num_params++;
  2147. }
  2148. if (!CallScriptInt32(state, num_params)) {
  2149. if (mutex)
  2150. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2151. UseZoneScript(script_name.c_str(), state, false);
  2152. return false;
  2153. }
  2154. if (mutex)
  2155. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2156. UseZoneScript(script_name.c_str(), state, false);
  2157. return true;
  2158. }
  2159. else
  2160. return false;
  2161. }
  2162. bool LuaInterface::RunZoneScriptWithReturn(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, int32 int32_arg1, int32 int32_arg2, int32 int32_arg3, int32* returnValue) {
  2163. if (!zone)
  2164. return false;
  2165. lua_State* state = GetZoneScript(script_name.c_str(), true, true);
  2166. if (state) {
  2167. Mutex* mutex = GetZoneScriptMutex(script_name.c_str());
  2168. if (mutex)
  2169. mutex->readlock(__FUNCTION__, __LINE__);
  2170. else {
  2171. LogError("Error getting lock for '%s'", script_name.c_str());
  2172. UseZoneScript(script_name.c_str(), state, false);
  2173. return false;
  2174. }
  2175. lua_getglobal(state, function_name);
  2176. if (!lua_isfunction(state, lua_gettop(state))) {
  2177. lua_pop(state, 1);
  2178. mutex->releasereadlock(__FUNCTION__);
  2179. UseZoneScript(script_name.c_str(), state, false);
  2180. return false;
  2181. }
  2182. SetZoneValue(state, zone);
  2183. int8 num_params = 1;
  2184. SetSpawnValue(state, spawn);
  2185. num_params++;
  2186. SetInt32Value(state, int32_arg1);
  2187. num_params++;
  2188. SetInt32Value(state, int32_arg2);
  2189. num_params++;
  2190. SetInt32Value(state, int32_arg3);
  2191. num_params++;
  2192. if (!CallScriptInt32(state, num_params, returnValue)) {
  2193. if (mutex)
  2194. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2195. UseZoneScript(script_name.c_str(), state, false);
  2196. return false;
  2197. }
  2198. if (mutex)
  2199. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2200. UseZoneScript(script_name.c_str(), state, false);
  2201. return true;
  2202. }
  2203. else
  2204. return false;
  2205. }
  2206. bool LuaInterface::RunRegionScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, sint32 int32_arg1, int32* returnValue) {
  2207. if (!zone)
  2208. return false;
  2209. lua_State* state = GetRegionScript(script_name.c_str(), true, true);
  2210. if (state) {
  2211. Mutex* mutex = GetRegionScriptMutex(script_name.c_str());
  2212. if (mutex)
  2213. mutex->readlock(__FUNCTION__, __LINE__);
  2214. else {
  2215. LogError("Error getting lock for '%s'", script_name.c_str());
  2216. UseRegionScript(script_name.c_str(), state, false);
  2217. return false;
  2218. }
  2219. lua_getglobal(state, function_name);
  2220. if (!lua_isfunction(state, lua_gettop(state))) {
  2221. lua_pop(state, 1);
  2222. mutex->releasereadlock(__FUNCTION__);
  2223. UseRegionScript(script_name.c_str(), state, false);
  2224. return false;
  2225. }
  2226. SetZoneValue(state, zone);
  2227. int8 num_params = 1;
  2228. if (spawn) {
  2229. SetSpawnValue(state, spawn);
  2230. num_params++;
  2231. }
  2232. if (int32_arg1 > 0) {
  2233. SetSInt32Value(state, int32_arg1);
  2234. num_params++;
  2235. }
  2236. if (!CallRegionScript(state, num_params, returnValue)) {
  2237. if (mutex)
  2238. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2239. UseRegionScript(script_name.c_str(), state, false);
  2240. return false;
  2241. }
  2242. if (mutex)
  2243. mutex->releasereadlock(__FUNCTION__, __LINE__);
  2244. UseRegionScript(script_name.c_str(), state, false);
  2245. return true;
  2246. }
  2247. else
  2248. return false;
  2249. }
  2250. void LuaInterface::AddPendingSpellDelete(LuaSpell* spell) {
  2251. MSpellDelete.lock();
  2252. if ( spells_pending_delete.count(spell) == 0 )
  2253. spells_pending_delete[spell] = Timer::GetCurrentTime2() + 10000;
  2254. MSpellDelete.unlock();
  2255. }
  2256. void LuaInterface::AddCustomSpell(LuaSpell* spell)
  2257. {
  2258. MCustomSpell.writelock();
  2259. custom_spells[spell->spell->GetSpellID()] = spell;
  2260. MCustomSpell.releasewritelock();
  2261. }
  2262. void LuaInterface::RemoveCustomSpell(int32 id)
  2263. {
  2264. MCustomSpell.writelock();
  2265. map<int32, LuaSpell*>::iterator itr = custom_spells.find(id);
  2266. if (itr != custom_spells.end())
  2267. {
  2268. custom_spells.erase(itr);
  2269. custom_free_spell_ids.push_front(id);
  2270. }
  2271. MCustomSpell.releasewritelock();
  2272. }
  2273. // prior to calling FindCustomSpell you should call FindCustomSpellLock and after FindCustomSpellUnlock
  2274. LuaSpell* LuaInterface::FindCustomSpell(int32 id)
  2275. {
  2276. LuaSpell* spell = 0;
  2277. map<int32, LuaSpell*>::iterator itr = custom_spells.find(id);
  2278. if (itr != custom_spells.end())
  2279. spell = itr->second;
  2280. return spell;
  2281. }
  2282. int32 LuaInterface::GetFreeCustomSpellID()
  2283. {
  2284. int32 id = 0;
  2285. MCustomSpell.writelock();
  2286. if (!custom_free_spell_ids.empty())
  2287. {
  2288. id = custom_free_spell_ids.front();
  2289. custom_free_spell_ids.pop_front();
  2290. }
  2291. MCustomSpell.releasewritelock();
  2292. return id;
  2293. }
  2294. LUAUserData::LUAUserData(){
  2295. correctly_initialized = false;
  2296. quest = 0;
  2297. conversation_options = 0;
  2298. spawn = 0;
  2299. zone = 0;
  2300. skill = 0;
  2301. option_window_option = 0;
  2302. item = 0;
  2303. spawn_list = 0;
  2304. }
  2305. bool LUAUserData::IsCorrectlyInitialized(){
  2306. return correctly_initialized;
  2307. }
  2308. bool LUAUserData::IsConversationOption(){
  2309. return false;
  2310. }
  2311. bool LUAUserData::IsSpawnList() {
  2312. return false;
  2313. }
  2314. bool LUAUserData::IsOptionWindow() {
  2315. return false;
  2316. }
  2317. bool LUAUserData::IsSpawn(){
  2318. return false;
  2319. }
  2320. bool LUAUserData::IsQuest(){
  2321. return false;
  2322. }
  2323. bool LUAUserData::IsZone(){
  2324. return false;
  2325. }
  2326. bool LUAUserData::IsItem(){
  2327. return false;
  2328. }
  2329. bool LUAUserData::IsSkill() {
  2330. return false;
  2331. }
  2332. bool LUAUserData::IsSpell() {
  2333. return false;
  2334. }
  2335. LUAConversationOptionWrapper::LUAConversationOptionWrapper(){
  2336. correctly_initialized = true;
  2337. }
  2338. bool LUAConversationOptionWrapper::IsConversationOption(){
  2339. return true;
  2340. }
  2341. LUASpawnListWrapper::LUASpawnListWrapper() {
  2342. correctly_initialized = true;
  2343. }
  2344. bool LUASpawnListWrapper::IsSpawnList() {
  2345. return true;
  2346. }
  2347. LUAOptionWindowWrapper::LUAOptionWindowWrapper() {
  2348. correctly_initialized = true;
  2349. }
  2350. bool LUAOptionWindowWrapper::IsOptionWindow() {
  2351. return true;
  2352. }
  2353. LUASpawnWrapper::LUASpawnWrapper(){
  2354. correctly_initialized = true;
  2355. }
  2356. bool LUASpawnWrapper::IsSpawn(){
  2357. return true;
  2358. }
  2359. LUAZoneWrapper::LUAZoneWrapper(){
  2360. correctly_initialized = true;
  2361. }
  2362. bool LUAZoneWrapper::IsZone(){
  2363. return true;
  2364. }
  2365. LUAQuestWrapper::LUAQuestWrapper(){
  2366. correctly_initialized = true;
  2367. }
  2368. bool LUAQuestWrapper::IsQuest(){
  2369. return true;
  2370. }
  2371. LUAItemWrapper::LUAItemWrapper(){
  2372. correctly_initialized = true;
  2373. }
  2374. bool LUAItemWrapper::IsItem(){
  2375. return true;
  2376. }
  2377. LUASkillWrapper::LUASkillWrapper() {
  2378. correctly_initialized = true;
  2379. }
  2380. bool LUASkillWrapper::IsSkill() {
  2381. return true;
  2382. }
  2383. LUASpellWrapper::LUASpellWrapper() {
  2384. correctly_initialized = true;
  2385. }
  2386. bool LUASpellWrapper::IsSpell() {
  2387. return true;
  2388. }