LuaInterface.cpp 90 KB

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