9
3

LuaInterface.cpp 77 KB

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