LuaInterface.cpp 87 KB

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