LuaInterface.cpp 95 KB

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