LuaInterface.cpp 93 KB

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