LuaInterface.cpp 85 KB

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