9
3

LuaInterface.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  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 "../common/Log.h"
  20. #ifndef WIN32
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <dirent.h>
  25. #include <pthread.h>
  26. #else
  27. #include <process.h>
  28. #endif
  29. extern WorldDatabase database;
  30. LuaInterface::LuaInterface() {
  31. shutting_down = false;
  32. spawn_scripts_reloading = false;
  33. MDebugClients.SetName("LuaInterface::MDebugClients");
  34. MSpells.SetName("LuaInterface::MSpells");
  35. MSpawnScripts.SetName("LuaInterface::MSpawnScripts");
  36. MZoneScripts.SetName("LuaInterface::MZoneScripts");
  37. MQuests.SetName("LuaInterface::MQuests");
  38. MLUAUserData.SetName("LuaInterface::MLUAUserData");
  39. MLUAMain.SetName("LuaInterface::MLUAMain");
  40. MItemScripts.SetName("LuaInterface::MItemScripts");
  41. user_data_timer = new Timer(20000);
  42. user_data_timer->Start();
  43. spell_delete_timer = new Timer(5000);
  44. spell_delete_timer->Start();
  45. }
  46. #ifdef WIN32
  47. vector<string>* LuaInterface::GetDirectoryListing(const char* directory) {
  48. vector<string>* ret = new vector<string>;
  49. WIN32_FIND_DATA fdata;
  50. HANDLE dhandle;
  51. char buf[MAX_PATH];
  52. snprintf(buf, sizeof(buf), "%s\\*", directory);
  53. if((dhandle = FindFirstFile(buf, &fdata)) == INVALID_HANDLE_VALUE) {
  54. safe_delete(ret);
  55. return 0;
  56. }
  57. ret->push_back(string(fdata.cFileName));
  58. while(1) {
  59. if(FindNextFile(dhandle, &fdata)) {
  60. ret->push_back(string(fdata.cFileName));
  61. }
  62. else{
  63. if(GetLastError() == ERROR_NO_MORE_FILES) {
  64. break;
  65. } else {
  66. safe_delete(ret);
  67. FindClose(dhandle);
  68. return 0;
  69. }
  70. }
  71. }
  72. if(FindClose(dhandle) == 0) {
  73. safe_delete(ret);
  74. return 0;
  75. }
  76. return ret;
  77. }
  78. #else
  79. vector<string>* LuaInterface::GetDirectoryListing(const char* directory) {
  80. vector<string>* ret = new vector<string>;
  81. DIR *dp;
  82. struct dirent *ep;
  83. dp = opendir (directory);
  84. if (dp != NULL){
  85. while ((ep = readdir (dp)))
  86. ret->push_back(string(ep->d_name));
  87. (void) closedir (dp);
  88. }
  89. else {
  90. safe_delete(ret);
  91. return 0;
  92. }
  93. return ret;
  94. }
  95. #endif
  96. LuaInterface::~LuaInterface() {
  97. shutting_down = true;
  98. MLUAMain.lock();
  99. DestroySpells();
  100. DestroySpawnScripts();
  101. DestroyQuests();
  102. DestroyItemScripts();
  103. DestroyZoneScripts();
  104. DeleteUserDataPtrs(true);
  105. DeletePendingSpells(true);
  106. safe_delete(user_data_timer);
  107. safe_delete(spell_delete_timer);
  108. }
  109. void LuaInterface::Process() {
  110. if(shutting_down)
  111. return;
  112. MLUAMain.lock();
  113. if(user_data_timer && user_data_timer->Check())
  114. DeleteUserDataPtrs(false);
  115. if(spell_delete_timer && spell_delete_timer->Check())
  116. DeletePendingSpells(false);
  117. MLUAMain.unlock();
  118. }
  119. void LuaInterface::DestroySpells() {
  120. map<string, LuaSpell*>::iterator itr;
  121. MSpells.lock();
  122. for(itr = spells.begin(); itr != spells.end(); itr++){
  123. lua_close(itr->second->state);
  124. safe_delete(itr->second);
  125. }
  126. spells.clear();
  127. MSpells.unlock();
  128. }
  129. void LuaInterface::DestroyQuests(bool reload) {
  130. map<int32, lua_State*>::iterator itr;
  131. MQuests.lock();
  132. for(itr = quest_states.begin(); itr != quest_states.end(); itr++){
  133. safe_delete(quests[itr->first]);
  134. lua_close(itr->second);
  135. }
  136. quests.clear();
  137. quest_states.clear();
  138. map<int32, Mutex*>::iterator mutex_itr;
  139. for(mutex_itr = quests_mutex.begin(); mutex_itr != quests_mutex.end(); mutex_itr++){
  140. safe_delete(mutex_itr->second);
  141. }
  142. quests_mutex.clear();
  143. if(reload)
  144. database.LoadQuests();
  145. MQuests.unlock();
  146. }
  147. void LuaInterface::DestroyItemScripts() {
  148. map<string, map<lua_State*, bool> >::iterator itr;
  149. map<lua_State*, bool>::iterator state_itr;
  150. Mutex* mutex = 0;
  151. MItemScripts.writelock(__FUNCTION__, __LINE__);
  152. for(itr = item_scripts.begin(); itr != item_scripts.end(); itr++){
  153. mutex = GetItemScriptMutex(itr->first.c_str());
  154. mutex->writelock(__FUNCTION__, __LINE__);
  155. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  156. lua_close(state_itr->first);
  157. mutex->releasewritelock(__FUNCTION__, __LINE__);
  158. safe_delete(mutex);
  159. }
  160. item_scripts.clear();
  161. item_scripts_mutex.clear();
  162. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  163. }
  164. void LuaInterface::DestroySpawnScripts() {
  165. map<string, map<lua_State*, bool> >::iterator itr;
  166. map<lua_State*, bool>::iterator state_itr;
  167. Mutex* mutex = 0;
  168. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  169. for(itr = spawn_scripts.begin(); itr != spawn_scripts.end(); itr++){
  170. mutex = GetSpawnScriptMutex(itr->first.c_str());
  171. mutex->writelock(__FUNCTION__, __LINE__);
  172. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  173. lua_close(state_itr->first);
  174. mutex->releasewritelock(__FUNCTION__, __LINE__);
  175. safe_delete(mutex);
  176. }
  177. spawn_scripts.clear();
  178. spawn_scripts_mutex.clear();
  179. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  180. }
  181. void LuaInterface::DestroyZoneScripts() {
  182. map<string, map<lua_State*, bool> >::iterator itr;
  183. map<lua_State*, bool>::iterator state_itr;
  184. Mutex* mutex = 0;
  185. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  186. for (itr = zone_scripts.begin(); itr != zone_scripts.end(); itr++){
  187. mutex = GetZoneScriptMutex(itr->first.c_str());
  188. mutex->writelock(__FUNCTION__, __LINE__);
  189. for(state_itr = itr->second.begin(); state_itr != itr->second.end(); state_itr++)
  190. lua_close(state_itr->first);
  191. mutex->releasewritelock(__FUNCTION__, __LINE__);
  192. safe_delete(mutex);
  193. }
  194. zone_scripts.clear();
  195. zone_scripts_mutex.clear();
  196. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  197. }
  198. void LuaInterface::ReloadSpells() {
  199. DestroySpells();
  200. database.LoadSpellScriptData();
  201. }
  202. bool LuaInterface::LoadLuaSpell(const char* name) {
  203. LuaSpell* spell = 0;
  204. string lua_script = string(name);
  205. if (lua_script.find(".lua") == string::npos)
  206. lua_script.append(".lua");
  207. lua_State* state = LoadLuaFile(lua_script.c_str());
  208. if(state){
  209. spell = new LuaSpell;
  210. spell->file_name = lua_script;
  211. spell->state = state;
  212. spell->spell = 0;
  213. spell->caster = 0;
  214. spell->initial_target = 0;
  215. spell->resisted = false;
  216. spell->interrupted = false;
  217. spell->last_spellattack_hit = false;
  218. spell->crit = false;
  219. spell->MSpellTargets.SetName("LuaSpell.MSpellTargets");
  220. spell->cancel_after_all_triggers = false;
  221. spell->num_triggers = 0;
  222. spell->had_triggers = false;
  223. spell->had_dmg_remaining = false;
  224. spell->slot_pos = 0;
  225. spell->damage_remaining = 0;
  226. spell->effect_bitmask = 0;
  227. MSpells.lock();
  228. if (spells.count(lua_script) > 0) {
  229. lua_close(spells[lua_script]->state);
  230. safe_delete(spells[lua_script]);
  231. }
  232. spells[lua_script] = spell;
  233. MSpells.unlock();
  234. return true;
  235. }
  236. return false;
  237. }
  238. bool LuaInterface::LoadLuaSpell(string name) {
  239. return LoadLuaSpell(name.c_str());
  240. }
  241. bool LuaInterface::LoadItemScript(string name) {
  242. return LoadItemScript(name.c_str());
  243. }
  244. bool LuaInterface::LoadItemScript(const char* name) {
  245. bool ret = false;
  246. if(name){
  247. lua_State* state = LoadLuaFile(name);
  248. if(state){
  249. MItemScripts.writelock(__FUNCTION__, __LINE__);
  250. item_scripts[name][state] = false;
  251. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  252. ret = true;
  253. }
  254. }
  255. return ret;
  256. }
  257. bool LuaInterface::LoadSpawnScript(const char* name) {
  258. bool ret = false;
  259. if(name){
  260. lua_State* state = LoadLuaFile(name);
  261. if(state){
  262. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  263. spawn_scripts[name][state] = false;
  264. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  265. ret = true;
  266. }
  267. }
  268. return ret;
  269. }
  270. bool LuaInterface::LoadZoneScript(const char* name) {
  271. bool ret = false;
  272. if (name) {
  273. lua_State* state = LoadLuaFile(name);
  274. if (state) {
  275. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  276. zone_scripts[name][state] = false;
  277. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  278. ret = true;
  279. }
  280. }
  281. return ret;
  282. }
  283. void LuaInterface::ProcessErrorMessage(const char* message) {
  284. MDebugClients.lock();
  285. vector<Client*> delete_clients;
  286. map<Client*, int32>::iterator itr;
  287. for(itr = debug_clients.begin(); itr != debug_clients.end(); itr++){
  288. if((Timer::GetCurrentTime2() - itr->second) > 60000)
  289. delete_clients.push_back(itr->first);
  290. else
  291. itr->first->Message(CHANNEL_COLOR_RED, "LUA Error: %s", message);
  292. }
  293. for(int32 i=0;i<delete_clients.size();i++)
  294. debug_clients.erase(delete_clients[i]);
  295. MDebugClients.unlock();
  296. }
  297. void LuaInterface::RemoveDebugClients(Client* client) {
  298. MDebugClients.lock();
  299. debug_clients.erase(client);
  300. MDebugClients.unlock();
  301. }
  302. void LuaInterface::UpdateDebugClients(Client* client) {
  303. MDebugClients.lock();
  304. debug_clients[client] = Timer::GetCurrentTime2();
  305. MDebugClients.unlock();
  306. }
  307. Mutex* LuaInterface::GetQuestMutex(Quest* quest) {
  308. Mutex* ret = 0;
  309. MQuests.lock();
  310. if(quests_mutex.count(quest->GetQuestID()) == 0){
  311. ret = new Mutex();
  312. quests_mutex[quest->GetQuestID()] = ret;
  313. ret->SetName(string("Quest::").append(quest->GetName()));
  314. }
  315. else
  316. ret = quests_mutex[quest->GetQuestID()];
  317. MQuests.unlock();
  318. return ret;
  319. }
  320. void LuaInterface::CallQuestFunction(Quest* quest, const char* function, Spawn* player, int32 step_id) {
  321. if(shutting_down)
  322. return;
  323. lua_State* state = 0;
  324. if(quest){
  325. LogWrite(LUA__DEBUG, 0, "LUA", "Quest: %s, function: %s", quest->GetName(), function);
  326. Mutex* mutex = GetQuestMutex(quest);
  327. mutex->lock();
  328. if(quest_states.count(quest->GetQuestID()) > 0)
  329. state = quest_states[quest->GetQuestID()];
  330. if(state){
  331. int8 arg_count = 3;
  332. lua_getglobal(state, function);
  333. SetQuestValue(state, quest);
  334. Spawn* spawn = player->GetZone()->GetSpawnByDatabaseID(quest->GetQuestGiver());
  335. SetSpawnValue(state, spawn);
  336. SetSpawnValue(state, player);
  337. if(step_id != 0xFFFFFFFF){
  338. SetInt32Value(state, step_id);
  339. arg_count++;
  340. }
  341. if(lua_pcall(state, arg_count, 0, 0) != 0){
  342. LogError("Error processing quest function '%s': %s ", function, lua_tostring(state, -1));
  343. lua_pop(state, 1);
  344. mutex->unlock();
  345. return;
  346. }
  347. }
  348. mutex->unlock();
  349. LogWrite(LUA__DEBUG, 0, "LUA", "Done!");
  350. }
  351. }
  352. Quest* LuaInterface::LoadQuest(int32 id, const char* name, const char* type, const char* zone, int8 level, const char* description, char* script_name) {
  353. if(shutting_down)
  354. return 0;
  355. lua_State* state = LoadLuaFile(script_name);
  356. Quest* quest = 0;
  357. if(state){
  358. quest = new Quest(id);
  359. if (name)
  360. quest->SetName(string(name));
  361. if (type)
  362. quest->SetType(string(type));
  363. if (zone)
  364. quest->SetZone(string(zone));
  365. quest->SetLevel(level);
  366. if (description)
  367. quest->SetDescription(string(description));
  368. lua_getglobal(state, "Init");
  369. SetQuestValue(state, quest);
  370. if(lua_pcall(state, 1, 0, 0) != 0){
  371. LogError("Error processing Quest \"%s\" (%u): %s", name ? name : "unknown", id, lua_tostring(state, -1));
  372. lua_pop(state, 1);
  373. safe_delete(quest);
  374. return 0;
  375. }
  376. if(!quest->GetName()){
  377. safe_delete(quest);
  378. return 0;
  379. }
  380. quest_states[id] = state;
  381. quests[id] = quest;
  382. }
  383. return quest;
  384. }
  385. bool LuaInterface::LoadSpawnScript(string name) {
  386. return LoadSpawnScript(name.c_str());
  387. }
  388. bool LuaInterface::LoadZoneScript(string name) {
  389. return LoadZoneScript(name.c_str());
  390. }
  391. void LuaInterface::AddSpawnPointers(LuaSpell* spell, bool first_cast, bool precast, const char* function, SpellScriptTimer* timer) {
  392. if (function)
  393. lua_getglobal(spell->state, function);
  394. else if (precast)
  395. lua_getglobal(spell->state, "precast");
  396. else if(first_cast)
  397. lua_getglobal(spell->state, "cast");
  398. else
  399. lua_getglobal(spell->state, "tick");
  400. Spawn* temp_spawn = 0;
  401. if (timer && timer->caster && spell->caster)
  402. temp_spawn = spell->caster->GetZone()->GetSpawnByID(timer->caster);
  403. if (temp_spawn)
  404. SetSpawnValue(spell->state, temp_spawn);
  405. else if (spell->caster)
  406. SetSpawnValue(spell->state, spell->caster);
  407. temp_spawn = 0;
  408. if (timer && timer->target && spell->caster)
  409. temp_spawn = spell->caster->GetZone()->GetSpawnByID(timer->target);
  410. if (temp_spawn)
  411. SetSpawnValue(spell->state, temp_spawn);
  412. else {
  413. if(spell->caster && spell->initial_target)
  414. SetSpawnValue(spell->state, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  415. else if(spell->caster && spell->caster->GetTarget())
  416. SetSpawnValue(spell->state, spell->caster->GetTarget());
  417. else
  418. SetSpawnValue(spell->state, 0);
  419. }
  420. }
  421. LuaSpell* LuaInterface::GetCurrentSpell(lua_State* state) {
  422. if(current_spells.count(state) > 0)
  423. return current_spells[state];
  424. return 0;
  425. }
  426. bool LuaInterface::CallSpellProcess(LuaSpell* spell, int8 num_parameters) {
  427. if(shutting_down || !spell || !spell->caster)
  428. return false;
  429. current_spells[spell->state] = spell;
  430. if(lua_pcall(spell->state, num_parameters, 0, 0) != 0){
  431. LogError("Error running %s", lua_tostring(spell->state, -1));
  432. lua_pop(spell->state, 1);
  433. RemoveSpell(spell, false);
  434. return false;
  435. }
  436. return true;
  437. }
  438. void LuaInterface::RemoveSpawnScript(const char* name) {
  439. lua_State* state = 0;
  440. Mutex* mutex = GetSpawnScriptMutex(name);
  441. while((state = GetSpawnScript(name, false))){
  442. mutex->writelock(__FUNCTION__, __LINE__);
  443. lua_close(state);
  444. spawn_scripts[name].erase(state);
  445. mutex->releasewritelock(__FUNCTION__, __LINE__);
  446. }
  447. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  448. spawn_scripts.erase(name);
  449. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  450. }
  451. bool LuaInterface::CallItemScript(lua_State* state, int8 num_parameters) {
  452. if(shutting_down)
  453. return false;
  454. if(!state || lua_pcall(state, num_parameters, 0, 0) != 0){
  455. if (state){
  456. const char* err = lua_tostring(state, -1);
  457. LogError(err);
  458. lua_pop(state, 1);
  459. }
  460. return false;
  461. }
  462. return true;
  463. }
  464. bool LuaInterface::CallSpawnScript(lua_State* state, int8 num_parameters) {
  465. if(shutting_down)
  466. return false;
  467. if(!state || lua_pcall(state, num_parameters, 0, 0) != 0){
  468. if (state){
  469. const char* err = lua_tostring(state, -1);
  470. LogError(err);
  471. lua_pop(state, 1);
  472. }
  473. return false;
  474. }
  475. return true;
  476. }
  477. bool LuaInterface::CallZoneScript(lua_State* state, int8 num_parameters) {
  478. if(shutting_down)
  479. return false;
  480. if (!state || lua_pcall(state, num_parameters, 0, 0) != 0) {
  481. if (state){
  482. const char* err = lua_tostring(state, -1);
  483. LogError(err);
  484. lua_pop(state, 1);
  485. }
  486. return false;
  487. }
  488. return true;
  489. }
  490. lua_State* LuaInterface::LoadLuaFile(const char* name) {
  491. if(shutting_down)
  492. return 0;
  493. lua_State* state = luaL_newstate();
  494. luaL_openlibs(state);
  495. if(luaL_dofile(state, name) == 0){
  496. RegisterFunctions(state);
  497. return state;
  498. }
  499. else{
  500. LogError("Error loading %s (file name: '%s')", lua_tostring(state, -1), name);
  501. lua_pop(state, 1);
  502. lua_close(state);
  503. }
  504. return 0;
  505. }
  506. void LuaInterface::RemoveSpell(LuaSpell* spell, bool call_remove_function, bool can_delete) {
  507. if(shutting_down)
  508. return;
  509. if(call_remove_function){
  510. lua_getglobal(spell->state, "remove");
  511. LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper();
  512. spawn_wrapper->spawn = spell->caster;
  513. AddUserDataPtr(spawn_wrapper);
  514. lua_pushlightuserdata(spell->state, spawn_wrapper);
  515. if(spell->caster && (spell->initial_target || spell->caster->GetTarget())){
  516. spawn_wrapper = new LUASpawnWrapper();
  517. if(!spell->initial_target)
  518. spawn_wrapper->spawn = spell->caster->GetTarget();
  519. else
  520. spawn_wrapper->spawn = spell->caster->GetZone()->GetSpawnByID(spell->initial_target);
  521. AddUserDataPtr(spawn_wrapper);
  522. lua_pushlightuserdata(spell->state, spawn_wrapper);
  523. }
  524. else
  525. lua_pushlightuserdata(spell->state, 0);
  526. current_spells[spell->state] = spell;
  527. lua_pcall(spell->state, 2, 0, 0);
  528. }
  529. if (can_delete) {
  530. MSpellDelete.lock();
  531. spells_pending_delete[spell] = Timer::GetCurrentTime2() + 10000;
  532. MSpellDelete.unlock();
  533. }
  534. }
  535. void LuaInterface::RegisterFunctions(lua_State* state) {
  536. lua_register(state, "SetHP", EQ2Emu_lua_SetCurrentHP);
  537. lua_register(state, "SetMaxHP", EQ2Emu_lua_SetMaxHP);
  538. lua_register(state, "SetMaxHPBase", EQ2Emu_lua_SetMaxHPBase);
  539. lua_register(state, "SetPower", EQ2Emu_lua_SetCurrentPower);
  540. lua_register(state, "SetMaxPower", EQ2Emu_lua_SetMaxPower);
  541. lua_register(state, "SetMaxPowerBase", EQ2Emu_lua_SetMaxPowerBase);
  542. lua_register(state, "SetPosition", EQ2Emu_lua_SetPosition);
  543. lua_register(state, "SetHeading", EQ2Emu_lua_SetHeading);
  544. lua_register(state, "SetModelType", EQ2Emu_lua_SetModelType);
  545. lua_register(state, "SetAdventureClass", EQ2Emu_lua_SetAdventureClass);
  546. lua_register(state, "SetTradeskillClass", EQ2Emu_lua_SetTradeskillClass);
  547. lua_register(state, "SetMount", EQ2Emu_lua_SetMount);
  548. lua_register(state, "SetMountColor", EQ2Emu_lua_SetMountColor);
  549. lua_register(state, "GetMount", EQ2Emu_lua_GetMount);
  550. lua_register(state, "GetRace", EQ2Emu_lua_GetRace);
  551. lua_register(state, "GetRaceName", EQ2Emu_lua_GetRaceName);
  552. lua_register(state, "GetClass", EQ2Emu_lua_GetClass);
  553. lua_register(state, "GetClassName", EQ2Emu_lua_GetClassName);
  554. lua_register(state, "GetArchetypeName", EQ2Emu_lua_GetArchetypeName);
  555. lua_register(state, "SetSpeed", EQ2Emu_lua_SetSpeed);
  556. lua_register(state, "ModifyPower", EQ2Emu_lua_ModifyPower);
  557. lua_register(state, "ModifyHP", EQ2Emu_lua_ModifyHP);
  558. lua_register(state, "GetDistance", EQ2Emu_lua_GetDistance);
  559. lua_register(state, "GetHeading", EQ2Emu_lua_GetHeading);
  560. lua_register(state, "GetLevel", EQ2Emu_lua_GetLevel);
  561. lua_register(state, "GetHP", EQ2Emu_lua_GetCurrentHP);
  562. lua_register(state, "GetMaxHP", EQ2Emu_lua_GetMaxHP);
  563. lua_register(state, "GetMaxHPBase", EQ2Emu_lua_GetMaxHPBase);
  564. lua_register(state, "GetMaxPower", EQ2Emu_lua_GetMaxPower);
  565. lua_register(state, "GetMaxPowerBase", EQ2Emu_lua_GetMaxPowerBase);
  566. lua_register(state, "GetName", EQ2Emu_lua_GetName);
  567. lua_register(state, "GetPower", EQ2Emu_lua_GetCurrentPower);
  568. lua_register(state, "GetX", EQ2Emu_lua_GetX);
  569. lua_register(state, "GetY", EQ2Emu_lua_GetY);
  570. lua_register(state, "GetZ", EQ2Emu_lua_GetZ);
  571. lua_register(state, "GetSpawnID", EQ2Emu_lua_GetSpawnID);
  572. lua_register(state, "GetSpawnGroupID", EQ2Emu_lua_GetSpawnGroupID);
  573. lua_register(state, "GetSpawnLocationID", EQ2Emu_lua_GetSpawnLocationID);
  574. lua_register(state, "GetSpawnLocationPlacementID", EQ2Emu_lua_GetSpawnLocationPlacementID);
  575. lua_register(state, "GetFactionAmount", EQ2Emu_lua_GetFactionAmount);
  576. lua_register(state, "GetGender", EQ2Emu_lua_GetGender);
  577. lua_register(state, "GetTarget", EQ2Emu_lua_GetTarget);
  578. lua_register(state, "HasFreeSlot", EQ2Emu_lua_HasFreeSlot);
  579. lua_register(state, "HasItemEquipped", EQ2Emu_lua_HasItemEquipped);
  580. lua_register(state, "GetEquippedItemByID", EQ2Emu_lua_GetEquippedItemByID);
  581. lua_register(state, "GetEquippedItemBySlot", EQ2Emu_lua_GetEquippedItemBySlot);
  582. lua_register(state, "GetItemByID", EQ2Emu_lua_GetItemByID);
  583. lua_register(state, "GetItemType", EQ2Emu_lua_GetItemType);
  584. lua_register(state, "GetSpellName", EQ2Emu_lua_GetSpellName);
  585. lua_register(state, "PerformCameraShake", EQ2Emu_lua_PerformCameraShake);
  586. lua_register(state, "GetModelType", EQ2Emu_lua_GetModelType);
  587. lua_register(state, "GetSpeed", EQ2Emu_lua_GetSpeed);
  588. lua_register(state, "HasMoved", EQ2Emu_lua_HasMoved);
  589. lua_register(state, "SpellDamage", EQ2Emu_lua_SpellDamage);
  590. lua_register(state, "CastSpell", EQ2Emu_lua_CastSpell);
  591. lua_register(state, "SpellHeal", EQ2Emu_lua_SpellHeal);
  592. lua_register(state, "SummonItem", EQ2Emu_lua_SummonItem);
  593. lua_register(state, "RemoveItem", EQ2Emu_lua_RemoveItem);
  594. lua_register(state, "HasItem", EQ2Emu_lua_HasItem);
  595. lua_register(state, "SpawnMob", EQ2Emu_lua_Spawn);
  596. lua_register(state, "SummonPet", EQ2Emu_lua_SummonPet);
  597. lua_register(state, "AddSpawnAccess", EQ2Emu_lua_AddSpawnAccess);
  598. lua_register(state, "GetZone", EQ2Emu_lua_GetZone);
  599. lua_register(state, "GetZoneName", EQ2Emu_lua_GetZoneName);
  600. lua_register(state, "GetZoneID", EQ2Emu_lua_GetZoneID);
  601. lua_register(state, "Zone", EQ2Emu_lua_Zone);
  602. lua_register(state, "AddHate", EQ2Emu_lua_AddHate);
  603. lua_register(state, "IsAlive", EQ2Emu_lua_IsAlive);
  604. lua_register(state, "IsInCombat", EQ2Emu_lua_IsInCombat);
  605. lua_register(state, "Attack", EQ2Emu_lua_Attack);
  606. lua_register(state, "ApplySpellVisual", EQ2Emu_lua_ApplySpellVisual);
  607. lua_register(state, "IsPlayer", EQ2Emu_lua_IsPlayer);
  608. lua_register(state, "FaceTarget", EQ2Emu_lua_FaceTarget);
  609. lua_register(state, "MoveToLocation", EQ2Emu_lua_MoveToLocation);
  610. lua_register(state, "Shout", EQ2Emu_lua_Shout);
  611. lua_register(state, "Say", EQ2Emu_lua_Say);
  612. lua_register(state, "SayOOC", EQ2Emu_lua_SayOOC);
  613. lua_register(state, "Emote", EQ2Emu_lua_Emote);
  614. lua_register(state, "MovementLoopAddLocation", EQ2Emu_lua_MovementLoopAdd);
  615. lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation);
  616. lua_register(state, "AddTimer", EQ2Emu_lua_AddTimer);
  617. lua_register(state, "Harvest", EQ2Emu_lua_Harvest);
  618. lua_register(state, "AddSpellBonus", EQ2Emu_lua_AddSpellBonus);
  619. lua_register(state, "RemoveSpellBonus", EQ2Emu_lua_RemoveSpellBonus);
  620. lua_register(state, "AddSkillBonus", EQ2Emu_lua_AddSkillBonus);
  621. lua_register(state, "RemoveSkillBonus", EQ2Emu_lua_RemoveSkillBonus);
  622. lua_register(state, "AddControlEffect", EQ2Emu_lua_AddControlEffect);
  623. lua_register(state, "RemoveControlEffect", EQ2Emu_lua_RemoveControlEffect);
  624. lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation);
  625. lua_register(state, "GetInt", EQ2Emu_lua_GetInt);
  626. lua_register(state, "GetWis", EQ2Emu_lua_GetWis);
  627. lua_register(state, "GetSta", EQ2Emu_lua_GetSta);
  628. lua_register(state, "GetStr", EQ2Emu_lua_GetStr);
  629. lua_register(state, "GetAgi", EQ2Emu_lua_GetAgi);
  630. lua_register(state, "SetInt", EQ2Emu_lua_SetInt);
  631. lua_register(state, "SetWis", EQ2Emu_lua_SetWis);
  632. lua_register(state, "SetSta", EQ2Emu_lua_SetSta);
  633. lua_register(state, "SetStr", EQ2Emu_lua_SetStr);
  634. lua_register(state, "SetAgi", EQ2Emu_lua_SetAgi);
  635. lua_register(state, "GetIntBase", EQ2Emu_lua_GetIntBase);
  636. lua_register(state, "GetWisBase", EQ2Emu_lua_GetWisBase);
  637. lua_register(state, "GetStaBase", EQ2Emu_lua_GetStaBase);
  638. lua_register(state, "GetStrBase", EQ2Emu_lua_GetStrBase);
  639. lua_register(state, "GetAgiBase", EQ2Emu_lua_GetAgiBase);
  640. lua_register(state, "SetIntBase", EQ2Emu_lua_SetIntBase);
  641. lua_register(state, "SetWisBase", EQ2Emu_lua_SetWisBase);
  642. lua_register(state, "SetStaBase", EQ2Emu_lua_SetStaBase);
  643. lua_register(state, "SetStrBase", EQ2Emu_lua_SetStrBase);
  644. lua_register(state, "SetAgiBase", EQ2Emu_lua_SetAgiBase);
  645. lua_register(state, "GetSpawn", EQ2Emu_lua_GetSpawn);
  646. lua_register(state, "GetVariableValue", EQ2Emu_lua_GetVariableValue);
  647. lua_register(state, "GetCoinMessage", EQ2Emu_lua_GetCoinMessage);
  648. lua_register(state, "GetSpawnByGroupID", EQ2Emu_lua_GetSpawnByGroupID);
  649. lua_register(state, "GetSpawnByLocationID", EQ2Emu_lua_GetSpawnByLocationID);
  650. lua_register(state, "PlayFlavor", EQ2Emu_lua_PlayFlavor);
  651. lua_register(state, "PlaySound", EQ2Emu_lua_PlaySound);
  652. lua_register(state, "PlayVoice", EQ2Emu_lua_PlayVoice);
  653. lua_register(state, "PlayAnimation", EQ2Emu_lua_PlayAnimation);
  654. lua_register(state, "AddLootItem", EQ2Emu_lua_AddLootItem);
  655. lua_register(state, "RemoveLootItem", EQ2Emu_lua_RemoveLootItem);
  656. lua_register(state, "AddLootCoin", EQ2Emu_lua_AddLootCoin);
  657. lua_register(state, "GiveLoot", EQ2Emu_lua_GiveLoot);
  658. lua_register(state, "HasPendingLootItem", EQ2Emu_lua_HasPendingLootItem);
  659. lua_register(state, "HasPendingLoot", EQ2Emu_lua_HasPendingLoot);
  660. lua_register(state, "SetLootCoin", EQ2Emu_lua_SetLootCoin);
  661. lua_register(state, "GetLootCoin", EQ2Emu_lua_GetLootCoin);
  662. lua_register(state, "SetPlayerProximityFunction", EQ2Emu_lua_SetPlayerProximityFunction);
  663. lua_register(state, "SetLocationProximityFunction", EQ2Emu_lua_SetLocationProximityFunction);
  664. lua_register(state, "CreateConversation", EQ2Emu_lua_CreateConversation);
  665. lua_register(state, "AddConversationOption", EQ2Emu_lua_AddConversationOption);
  666. lua_register(state, "StartConversation", EQ2Emu_lua_StartConversation);
  667. lua_register(state, "CloseConversation", EQ2Emu_lua_CloseConversation);
  668. lua_register(state, "CloseItemConversation", EQ2Emu_lua_CloseItemConversation);
  669. //lua_register(state, "StartItemConversation", EQ2Emu_lua_StartItemConversation);
  670. lua_register(state, "StartDialogConversation", EQ2Emu_lua_StartDialogConversation);
  671. lua_register(state, "SpawnSet", EQ2Emu_lua_SpawnSet);
  672. lua_register(state, "SpawnSetByDistance", EQ2Emu_lua_SpawnSetByDistance);
  673. lua_register(state, "SpawnMove", EQ2Emu_lua_SpawnMove);
  674. lua_register(state, "KillSpawn", EQ2Emu_lua_KillSpawn);
  675. lua_register(state, "KillSpawnByDistance", EQ2Emu_lua_KillSpawnByDistance);
  676. lua_register(state, "Despawn", EQ2Emu_lua_Despawn);
  677. lua_register(state, "IsBindAllowed", EQ2Emu_lua_IsBindAllowed);
  678. lua_register(state, "IsGateAllowed", EQ2Emu_lua_IsGateAllowed);
  679. lua_register(state, "Bind", EQ2Emu_lua_Bind);
  680. lua_register(state, "Gate", EQ2Emu_lua_Gate);
  681. lua_register(state, "SendMessage", EQ2Emu_lua_SendMessage);
  682. lua_register(state, "SendPopUpMessage", EQ2Emu_lua_SendPopUpMessage);
  683. lua_register(state, "SetServerControlFlag", EQ2Emu_lua_SetServerControlFlag);
  684. lua_register(state, "ToggleTracking", EQ2Emu_lua_ToggleTracking);
  685. lua_register(state, "AddPrimaryEntityCommand", EQ2Emu_lua_AddPrimaryEntityCommand);
  686. lua_register(state, "AddSpellBookEntry", EQ2Emu_lua_AddSpellBookEntry);
  687. lua_register(state, "Interrupt", EQ2Emu_lua_Interrupt);
  688. lua_register(state, "Stealth", EQ2Emu_lua_Stealth);
  689. lua_register(state, "IsInvis", EQ2Emu_lua_IsInvis);
  690. lua_register(state, "IsStealthed", EQ2Emu_lua_IsStealthed);
  691. lua_register(state, "AddSpawnIDAccess", EQ2Emu_lua_AddSpawnIDAccess);
  692. lua_register(state, "RemoveSpawnIDAccess", EQ2Emu_lua_RemoveSpawnIDAccess);
  693. lua_register(state, "HasRecipeBook", EQ2Emu_lua_HasRecipeBook);
  694. lua_register(state, "SetRequiredQuest", EQ2Emu_lua_SetRequiredQuest);
  695. lua_register(state, "SetRequiredHistory", EQ2Emu_lua_SetRequiredHistory);
  696. lua_register(state, "SetStepComplete", EQ2Emu_lua_SetStepComplete);
  697. lua_register(state, "AddStepProgress", EQ2Emu_lua_AddStepProgress);
  698. lua_register(state, "UpdateQuestTaskGroupDescription", EQ2Emu_lua_UpdateQuestTaskGroupDescription);
  699. lua_register(state, "GetTaskGroupStep", EQ2Emu_lua_GetTaskGroupStep);
  700. lua_register(state, "GetQuestStep", EQ2Emu_lua_GetQuestStep);
  701. lua_register(state, "QuestStepIsComplete", EQ2Emu_lua_QuestStepIsComplete);
  702. lua_register(state, "RegisterQuest", EQ2Emu_lua_RegisterQuest);
  703. lua_register(state, "SetQuestPrereqLevel", EQ2Emu_lua_SetQuestPrereqLevel);
  704. lua_register(state, "AddQuestPrereqQuest", EQ2Emu_lua_AddQuestPrereqQuest);
  705. lua_register(state, "AddQuestPrereqItem", EQ2Emu_lua_AddQuestPrereqItem);
  706. lua_register(state, "AddQuestPrereqFaction", EQ2Emu_lua_AddQuestPrereqFaction);
  707. lua_register(state, "AddQuestPrereqRace", EQ2Emu_lua_AddQuestPrereqRace);
  708. lua_register(state, "AddQuestPrereqModelType", EQ2Emu_lua_AddQuestPrereqModelType);
  709. lua_register(state, "AddQuestPrereqClass", EQ2Emu_lua_AddQuestPrereqClass);
  710. lua_register(state, "AddQuestPrereqTradeskillLevel", EQ2Emu_lua_AddQuestPrereqTradeskillLevel);
  711. lua_register(state, "AddQuestPrereqTradeskillClass", EQ2Emu_lua_AddQuestPrereqTradeskillClass);
  712. lua_register(state, "AddQuestSelectableRewardItem", EQ2Emu_lua_AddQuestSelectableRewardItem);
  713. lua_register(state, "AddQuestRewardItem", EQ2Emu_lua_AddQuestRewardItem);
  714. lua_register(state, "AddQuestRewardCoin", EQ2Emu_lua_AddQuestRewardCoin);
  715. lua_register(state, "AddQuestRewardFaction", EQ2Emu_lua_AddQuestRewardFaction);
  716. lua_register(state, "SetQuestRewardStatus", EQ2Emu_lua_SetQuestRewardStatus);
  717. lua_register(state, "SetQuestRewardComment", EQ2Emu_lua_SetQuestRewardComment);
  718. lua_register(state, "SetQuestRewardExp", EQ2Emu_lua_SetQuestRewardExp);
  719. lua_register(state, "AddQuestStepKill", EQ2Emu_lua_AddQuestStepKill);
  720. lua_register(state, "AddQuestStep", EQ2Emu_lua_AddQuestStep);
  721. lua_register(state, "AddQuestStepChat", EQ2Emu_lua_AddQuestStepChat);
  722. lua_register(state, "AddQuestStepObtainItem", EQ2Emu_lua_AddQuestStepObtainItem);
  723. lua_register(state, "AddQuestStepLocation", EQ2Emu_lua_AddQuestStepLocation);
  724. lua_register(state, "AddQuestStepSpell", EQ2Emu_lua_AddQuestStepSpell);
  725. lua_register(state, "AddQuestStepCraft", EQ2Emu_lua_AddQuestStepCraft);
  726. lua_register(state, "AddQuestStepHarvest", EQ2Emu_lua_AddQuestStepHarvest);
  727. lua_register(state, "AddQuestStepCompleteAction", EQ2Emu_lua_AddQuestStepCompleteAction);
  728. lua_register(state, "AddQuestStepProgressAction", EQ2Emu_lua_AddQuestStepProgressAction);
  729. lua_register(state, "SetQuestCompleteAction", EQ2Emu_lua_SetQuestCompleteAction);
  730. lua_register(state, "GiveQuestReward", EQ2Emu_lua_GiveQuestReward);
  731. lua_register(state, "UpdateQuestStepDescription", EQ2Emu_lua_UpdateQuestStepDescription);
  732. lua_register(state, "UpdateQuestDescription", EQ2Emu_lua_UpdateQuestDescription);
  733. lua_register(state, "UpdateQuestZone", EQ2Emu_lua_UpdateQuestZone);
  734. lua_register(state, "SetCompletedDescription", EQ2Emu_lua_SetCompletedDescription);
  735. lua_register(state, "OfferQuest", EQ2Emu_lua_OfferQuest);
  736. lua_register(state, "ProvidesQuest", EQ2Emu_lua_ProvidesQuest);
  737. lua_register(state, "HasQuest", EQ2Emu_lua_HasQuest);
  738. lua_register(state, "HasCompletedQuest", EQ2Emu_lua_HasCompletedQuest);
  739. lua_register(state, "QuestIsComplete", EQ2Emu_lua_QuestIsComplete);
  740. lua_register(state, "QuestReturnNPC", EQ2Emu_lua_QuestReturnNPC);
  741. lua_register(state, "GetQuest", EQ2Emu_lua_GetQuest);
  742. lua_register(state, "HasCollectionsToHandIn", EQ2Emu_lua_HasCollectionsToHandIn);
  743. lua_register(state, "HandInCollections", EQ2Emu_lua_HandInCollections);
  744. lua_register(state, "UseWidget", EQ2Emu_lua_UseWidget);
  745. lua_register(state, "SetSpellList", EQ2Emu_lua_SetSpellList);
  746. lua_register(state, "GetPet", EQ2Emu_lua_GetPet);
  747. lua_register(state, "Charm", EQ2Emu_lua_Charm);
  748. lua_register(state, "GetGroup", EQ2Emu_lua_GetGroup);
  749. lua_register(state, "SetCompleteFlag", EQ2Emu_lua_SetCompleteFlag);
  750. lua_register(state, "SetQuestYellow", EQ2Emu_lua_SetQuestYellow);
  751. lua_register(state, "CanReceiveQuest", EQ2Emu_lua_CanReceiveQuest);
  752. lua_register(state, "AddTransportSpawn", EQ2Emu_lua_AddTransportSpawn);
  753. // Option window
  754. lua_register(state, "CreateOptionWindow", EQ2Emu_lua_CreateOptionWindow);
  755. lua_register(state, "AddOptionWindowOption", EQ2Emu_lua_AddOptionWindowOption);
  756. lua_register(state, "SendOptionWindow", EQ2Emu_lua_SendOptionWindow);
  757. lua_register(state, "GetTradeskillClass", EQ2Emu_lua_GetTradeskillClass);
  758. lua_register(state, "GetTradeskillLevel", EQ2Emu_lua_GetTradeskillLevel);
  759. lua_register(state, "GetTradeskillClassName", EQ2Emu_lua_GetTradeskillClassName);
  760. lua_register(state, "SetTradeskillLevel", EQ2Emu_lua_SetTradeskillLevel);
  761. lua_register(state, "SummonDeityPet", EQ2Emu_lua_SummonDeityPet);
  762. lua_register(state, "SummonCosmeticPet", EQ2Emu_lua_SummonCosmeticPet);
  763. lua_register(state, "DismissPet", EQ2Emu_lua_DismissPet);
  764. lua_register(state, "GetCharmedPet", EQ2Emu_lua_GetCharmedPet);
  765. lua_register(state, "GetDeityPet", EQ2Emu_lua_GetDeityPet);
  766. lua_register(state, "GetCosmeticPet", EQ2Emu_lua_GetCosmeticPet);
  767. lua_register(state, "SetQuestFeatherColor", EQ2Emu_lua_SetQuestFeatherColor);
  768. lua_register(state, "RemoveSpawnAccess", EQ2Emu_lua_RemoveSpawnAccess);
  769. lua_register(state, "SpawnByLocationID", EQ2Emu_lua_SpawnByLocationID);
  770. lua_register(state, "CastEntityCommand", EQ2Emu_lua_CastEntityCommand);
  771. lua_register(state, "SetLuaBrain", EQ2Emu_lua_SetLuaBrain);
  772. lua_register(state, "SetBrainTick", EQ2Emu_lua_SetBrainTick);
  773. lua_register(state, "SetFollowTarget", EQ2Emu_lua_SetFollowTarget);
  774. lua_register(state, "GetFollowTarget", EQ2Emu_lua_GetFollowTarget);
  775. lua_register(state, "ToggleFollow", EQ2Emu_lua_ToggleFollow);
  776. lua_register(state, "IsFollowing", EQ2Emu_lua_IsFollowing);
  777. lua_register(state, "SetTempVariable", EQ2Emu_lua_SetTempVariable);
  778. lua_register(state, "GetTempVariable", EQ2Emu_lua_GetTempVariable);
  779. lua_register(state, "GiveQuestItem", EQ2Emu_lua_GiveQuestItem);
  780. lua_register(state, "SetQuestRepeatable", EQ2Emu_lua_SetQuestRepeatable);
  781. lua_register(state, "AddWard", EQ2Emu_lua_AddWard);
  782. lua_register(state, "AddToWard", EQ2Emu_lua_AddToWard);
  783. lua_register(state, "RemoveWard", EQ2Emu_lua_RemoveWard);
  784. lua_register(state, "GetWardAmountLeft", EQ2Emu_lua_GetWardAmountLeft);
  785. lua_register(state, "SetTarget", EQ2Emu_lua_SetTarget);
  786. lua_register(state, "IsPet", EQ2Emu_lua_IsPet);
  787. lua_register(state, "GetOwner", EQ2Emu_lua_GetOwner);
  788. lua_register(state, "SetInCombat", EQ2Emu_lua_SetInCombat);
  789. lua_register(state, "CompareSpawns", EQ2Emu_lua_CompareSpawns);
  790. lua_register(state, "Runback", EQ2Emu_lua_Runback);
  791. lua_register(state, "GetRunbackDistance", EQ2Emu_lua_GetRunbackDistance);
  792. lua_register(state, "IsCasting", EQ2Emu_lua_IsCasting);
  793. lua_register(state, "IsMezzed", EQ2Emu_lua_IsMezzed);
  794. lua_register(state, "IsStunned", EQ2Emu_lua_IsStunned);
  795. lua_register(state, "IsMezzedOrStunned", EQ2Emu_lua_IsMezzedOrStunned);
  796. lua_register(state, "ProcessSpell", EQ2Emu_lua_ProcessSpell);
  797. lua_register(state, "ProcessMelee", EQ2Emu_lua_ProcessMelee);
  798. lua_register(state, "HasRecovered", EQ2Emu_lua_HasRecovered);
  799. lua_register(state, "GetEncounterSize", EQ2Emu_lua_GetEncounterSize);
  800. lua_register(state, "GetMostHated", EQ2Emu_lua_GetMostHated);
  801. lua_register(state, "ClearHate", EQ2Emu_lua_ClearHate);
  802. lua_register(state, "ClearEncounter", EQ2Emu_lua_ClearEncounter);
  803. lua_register(state, "GetEncounter", EQ2Emu_lua_GetEncounter);
  804. lua_register(state, "GetHateList", EQ2Emu_lua_GetHateList);
  805. lua_register(state, "HasGroup", EQ2Emu_lua_HasGroup);
  806. lua_register(state, "HasSpellEffect", EQ2Emu_lua_HasSpellEffect);
  807. lua_register(state, "SetSuccessTimer", EQ2Emu_lua_SetSuccessTimer);
  808. lua_register(state, "SetFailureTimer", EQ2Emu_lua_SetFailureTimer);
  809. lua_register(state, "IsGroundSpawn", EQ2Emu_lua_IsGroundSpawn);
  810. lua_register(state, "CanHarvest", EQ2Emu_lua_CanHarvest);
  811. lua_register(state, "SummonDumbFirePet", EQ2Emu_lua_SummonDumbFirePet);
  812. lua_register(state, "GetSkillValue", EQ2Emu_lua_GetSkillValue);
  813. lua_register(state, "GetSkillMaxValue", EQ2Emu_lua_GetSkillMaxValue);
  814. lua_register(state, "GetSkillName", EQ2Emu_lua_GetSkillName);
  815. lua_register(state, "SetSkillMaxValue", EQ2Emu_lua_SetSkillMaxValue);
  816. lua_register(state, "SetSkillValue", EQ2Emu_lua_SetSkillValue);
  817. lua_register(state, "GetSkill", EQ2Emu_lua_GetSkill);
  818. lua_register(state, "GetSkillIDByName", EQ2Emu_lua_GetSkillIDByName);
  819. lua_register(state, "AddProc", EQ2Emu_lua_AddProc);
  820. lua_register(state, "RemoveProc", EQ2Emu_lua_RemoveProc);
  821. lua_register(state, "Knockback", EQ2Emu_lua_Knockback);
  822. lua_register(state, "IsEpic", EQ2Emu_lua_IsEpic);
  823. lua_register(state, "IsHeroic", EQ2Emu_lua_IsHeroic);
  824. lua_register(state, "ProcDamage", EQ2Emu_lua_ProcDamage);
  825. lua_register(state, "LastSpellAttackHit", EQ2Emu_lua_LastSpellAttackHit);
  826. lua_register(state, "IsBehind", EQ2Emu_lua_IsBehind);
  827. lua_register(state, "IsFlanking", EQ2Emu_lua_IsFlanking);
  828. lua_register(state, "AddSpellTimer", EQ2Emu_lua_AddSpellTimer);
  829. lua_register(state, "GetItemCount", EQ2Emu_lua_GetItemCount);
  830. lua_register(state, "SetItemCount", EQ2Emu_lua_SetItemCount);
  831. lua_register(state, "Resurrect", EQ2Emu_lua_Resurrect);
  832. lua_register(state, "BreatheUnderwater", EQ2Emu_lua_BreatheUnderwater);
  833. lua_register(state, "BlurVision", EQ2Emu_lua_BlurVision);
  834. lua_register(state, "SetVision", EQ2Emu_lua_SetVision);
  835. lua_register(state, "GetItemSkillReq", EQ2Emu_lua_GetItemSkillReq);
  836. lua_register(state, "SetSpeedMultiplier", EQ2Emu_lua_SetSpeeedMultiplier);
  837. lua_register(state, "SetIllusion", EQ2Emu_lua_SetIllusion);
  838. lua_register(state, "ResetIllusion", EQ2Emu_lua_ResetIllusion);
  839. lua_register(state, "AddThreatTransfer", EQ2Emu_lua_AddThreatTransfer);
  840. lua_register(state, "RemoveThreatTransfer", EQ2Emu_lua_RemoveThreatTransfer);
  841. lua_register(state, "CureByType", EQ2Emu_lua_CureByType);
  842. lua_register(state, "CureByControlEffect", EQ2Emu_lua_CureByControlEffect);
  843. lua_register(state, "AddSpawnSpellBonus", EQ2Emu_lua_AddSpawnSpellBonus);
  844. lua_register(state, "CancelSpell", EQ2Emu_lua_CancelSpell);
  845. lua_register(state, "RemoveStealth", EQ2Emu_lua_RemoveStealth);
  846. lua_register(state, "RemoveInvis", EQ2Emu_lua_RemoveInvis);
  847. lua_register(state, "StartHeroicOpportunity", EQ2Emu_lua_StartHeroicOpportunity);
  848. lua_register(state, "CopySpawnAppearance", EQ2Emu_lua_CopySpawnAppearance);
  849. lua_register(state, "SetSpellTriggerCount", EQ2Emu_lua_SetSpellTriggerCount);
  850. lua_register(state, "GetSpellTriggerCount", EQ2Emu_lua_GetSpellTriggerCount);
  851. lua_register(state, "RemoveTriggerFromSpell", EQ2Emu_lua_RemoveTriggerFromSpell);
  852. lua_register(state, "AddImmunitySpell", EQ2Emu_lua_AddImmunitySpell);
  853. lua_register(state, "RemoveImmunitySpell", EQ2Emu_lua_RemoveImmunitySpell);
  854. lua_register(state, "SetSpellSnareValue", EQ2Emu_lua_SetSpellSnareValue);
  855. lua_register(state, "CheckRaceType", EQ2Emu_lua_CheckRaceType);
  856. lua_register(state, "GetRaceType", EQ2Emu_lua_GetRaceType);
  857. lua_register(state, "GetRaceBaseType", EQ2Emu_lua_GetRaceBaseType);
  858. lua_register(state, "GetQuestFlags", EQ2Emu_lua_GetQuestFlags);
  859. lua_register(state, "SetQuestFlags", EQ2Emu_lua_SetQuestFlags);
  860. lua_register(state, "SetQuestTimer", EQ2Emu_lua_SetQuestTimer);
  861. lua_register(state, "RemoveQuestStep", EQ2Emu_lua_RemoveQuestStep);
  862. lua_register(state, "ResetQuestStep", EQ2Emu_lua_ResetQuestStep);
  863. lua_register(state, "SetQuestTimerComplete", EQ2Emu_lua_SetQuestTimerComplete);
  864. lua_register(state, "AddQuestStepFailureAction", EQ2Emu_lua_AddQuestStepFailureAction);
  865. lua_register(state, "SetStepFailed", EQ2Emu_lua_SetStepFailed);
  866. lua_register(state, "GetQuestCompleteCount", EQ2Emu_lua_GetQuestCompleteCount);
  867. lua_register(state, "SetServerVariable", EQ2Emu_lua_SetServerVariable);
  868. lua_register(state, "GetServerVariable", EQ2Emu_lua_GetServerVariable);
  869. lua_register(state, "HasLanguage", EQ2Emu_lua_HasLanguage);
  870. lua_register(state, "AddLanguage", EQ2Emu_lua_AddLanguage);
  871. lua_register(state, "IsNight", EQ2Emu_lua_IsNight);
  872. lua_register(state, "AddMultiFloorLift", EQ2Emu_lua_AddMultiFloorLift);
  873. lua_register(state, "StartAutoMount", EQ2Emu_lua_StartAutoMount);
  874. lua_register(state, "EndAutoMount", EQ2Emu_lua_EndAutoMount);
  875. lua_register(state, "IsOnAutoMount", EQ2Emu_lua_IsOnAutoMount);
  876. lua_register(state, "SetPlayerHistory", EQ2Emu_lua_SetPlayerHistory);
  877. lua_register(state, "GetPlayerHistory", EQ2Emu_lua_GetPlayerHistory);
  878. lua_register(state, "SetGridID", EQ2Emu_lua_SetGridID);
  879. lua_register(state, "GetQuestStepProgress", EQ2Emu_lua_GetQuestStepProgress);
  880. lua_register(state, "SetPlayerLevel", EQ2Emu_lua_SetPlayerLevel);
  881. lua_register(state, "AddCoin", EQ2Emu_lua_AddCoin);
  882. lua_register(state, "RemoveCoin", EQ2Emu_lua_RemoveCoin);
  883. lua_register(state, "GetPlayersInZone", EQ2Emu_lua_GetPlayersInZone);
  884. lua_register(state, "SpawnGroupByID", EQ2Emu_lua_SpawnGroupByID);
  885. lua_register(state, "SetSpawnAnimation", EQ2Emu_lua_SetSpawnAnimation);
  886. lua_register(state, "GetClientVersion", EQ2Emu_lua_GetClientVersion);
  887. lua_register(state, "GetItemID", EQ2Emu_lua_GetItemID);
  888. lua_register(state, "IsEntity", EQ2Emu_lua_IsEntity);
  889. lua_register(state, "GetOrigX", EQ2Emu_lua_GetOrigX);
  890. lua_register(state, "GetOrigY", EQ2Emu_lua_GetOrigY);
  891. lua_register(state, "GetOrigZ", EQ2Emu_lua_GetOrigZ);
  892. lua_register(state, "GetPCTOfHP", EQ2Emu_lua_GetPCTOfHP);
  893. lua_register(state, "GetPCTOfPower", EQ2Emu_lua_GetPCTOfPower);
  894. lua_register(state, "GetBoundZoneID", EQ2Emu_lua_GetBoundZoneID);
  895. lua_register(state, "Evac", EQ2Emu_lua_Evac);
  896. lua_register(state, "GetSpellTier", EQ2Emu_lua_GetSpellTier);
  897. lua_register(state, "GetSpellID", EQ2Emu_lua_GetSpellID);
  898. lua_register(state, "StartTransmute", EQ2Emu_lua_StartTransmute);
  899. lua_register(state, "CompleteTransmute", EQ2Emu_lua_CompleteTransmute);
  900. lua_register(state, "ProcHate", EQ2Emu_lua_ProcHate);
  901. }
  902. void LuaInterface::LogError(const char* error, ...) {
  903. va_list argptr;
  904. char buffer[4096];
  905. va_start(argptr, error);
  906. vsnprintf(buffer, sizeof(buffer), error, argptr);
  907. va_end(argptr);
  908. SimpleLogError(buffer);
  909. }
  910. void LuaInterface::SimpleLogError(const char* error) {
  911. ProcessErrorMessage(error);
  912. LogWrite(LUA__ERROR, 0, "LUA", "%s", error);
  913. }
  914. void LuaInterface::ResetFunctionStack(lua_State* state) {
  915. lua_settop(state, 0);
  916. }
  917. void LuaInterface::AddUserDataPtr(LUAUserData* data) {
  918. MLUAUserData.lock();
  919. user_data[data] = Timer::GetCurrentTime2() + 300000; //allow a function to use this pointer for 5 minutes
  920. MLUAUserData.unlock();
  921. }
  922. void LuaInterface::DeletePendingSpells(bool all) {
  923. MSpellDelete.lock();
  924. if(spells_pending_delete.size() > 0){
  925. int32 time = Timer::GetCurrentTime2();
  926. map<LuaSpell*, int32>::iterator itr;
  927. vector<LuaSpell*> tmp_deletes;
  928. vector<LuaSpell*>::iterator del_itr;
  929. for(itr = spells_pending_delete.begin(); itr != spells_pending_delete.end(); itr++){
  930. if(all || time >= itr->second)
  931. tmp_deletes.push_back(itr->first);
  932. }
  933. LuaSpell* spell = 0;
  934. for(del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++){
  935. spell = *del_itr;
  936. spells_pending_delete.erase(spell);
  937. safe_delete(spell);
  938. }
  939. }
  940. MSpellDelete.unlock();
  941. }
  942. void LuaInterface::DeleteUserDataPtrs(bool all) {
  943. MLUAUserData.lock();
  944. if(user_data.size() > 0){
  945. map<LUAUserData*, int32>::iterator itr;
  946. int32 time = Timer::GetCurrentTime2();
  947. vector<LUAUserData*> tmp_deletes;
  948. vector<LUAUserData*>::iterator del_itr;
  949. for(itr = user_data.begin(); itr != user_data.end(); itr++){
  950. if(all || time >= itr->second)
  951. tmp_deletes.push_back(itr->first);
  952. }
  953. LUAUserData* data = 0;
  954. for(del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++){
  955. data = *del_itr;
  956. user_data.erase(data);
  957. safe_delete(data);
  958. }
  959. }
  960. MLUAUserData.unlock();
  961. }
  962. Spawn* LuaInterface::GetSpawn(lua_State* state, int8 arg_num) {
  963. Spawn* ret = 0;
  964. if (lua_islightuserdata(state, arg_num)){
  965. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  966. if(!data || !data->IsCorrectlyInitialized()){
  967. LogError("GetSpawn error while processing %s", lua_tostring(state, -1));
  968. }
  969. else if(!data->IsSpawn()){
  970. lua_Debug ar;
  971. lua_getstack (state, 1, &ar);
  972. lua_getinfo(state, "Sln", &ar);
  973. LogError("Invalid data type used for GetSpawn in %s (line %d)", ar.source, ar.currentline);
  974. }
  975. else
  976. ret = data->spawn;
  977. }
  978. return ret;
  979. }
  980. vector<ConversationOption>* LuaInterface::GetConversation(lua_State* state, int8 arg_num) {
  981. vector<ConversationOption>* ret = 0;
  982. if(lua_islightuserdata(state, arg_num)){
  983. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  984. if(!data || !data->IsCorrectlyInitialized()){
  985. LogError("GetConversation error while processing %s", lua_tostring(state, -1));
  986. }
  987. else if(!data->IsConversationOption()){
  988. lua_Debug ar;
  989. lua_getstack (state, 1, &ar);
  990. lua_getinfo(state, "Sln", &ar);
  991. LogError("Invalid data type used for GetConversation in %s (line %d)", ar.source, ar.currentline);
  992. }
  993. else
  994. ret = data->conversation_options;
  995. }
  996. return ret;
  997. }
  998. vector<OptionWindowOption>* LuaInterface::GetOptionWindow(lua_State* state, int8 arg_num) {
  999. vector<OptionWindowOption>* ret = 0;
  1000. if(lua_islightuserdata(state, arg_num)){
  1001. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1002. if(!data || !data->IsCorrectlyInitialized()){
  1003. LogError("GetOptionWindow error while processing %s", lua_tostring(state, -1));
  1004. }
  1005. else if(!data->IsOptionWindow()){
  1006. lua_Debug ar;
  1007. lua_getstack (state, 1, &ar);
  1008. lua_getinfo(state, "Sln", &ar);
  1009. LogError("Invalid data type used for GetOptionWindow in %s (line %d)", ar.source, ar.currentline);
  1010. }
  1011. else
  1012. ret = data->option_window_option;
  1013. }
  1014. return ret;
  1015. }
  1016. Quest* LuaInterface::GetQuest(lua_State* state, int8 arg_num) {
  1017. Quest* ret = 0;
  1018. if(lua_islightuserdata(state, arg_num)){
  1019. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1020. if(!data || !data->IsCorrectlyInitialized()){
  1021. LogError("GetQuest error while processing %s", lua_tostring(state, 0));
  1022. }
  1023. else if(!data->IsQuest()){
  1024. lua_Debug ar;
  1025. lua_getstack (state, 1, &ar);
  1026. lua_getinfo(state, "Sln", &ar);
  1027. LogError("Invalid data type used for GetQuest in %s (line %d)", ar.source, ar.currentline);
  1028. }
  1029. else
  1030. ret = data->quest;
  1031. }
  1032. return ret;
  1033. }
  1034. Item* LuaInterface::GetItem(lua_State* state, int8 arg_num) {
  1035. Item* ret = 0;
  1036. if(lua_islightuserdata(state, arg_num)){
  1037. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1038. if(!data || !data->IsCorrectlyInitialized()){
  1039. LogError("GetItem error while processing %s", lua_tostring(state, 0));
  1040. }
  1041. else if(!data->IsItem()){
  1042. lua_Debug ar;
  1043. lua_getstack (state, 1, &ar);
  1044. lua_getinfo(state, "Sln", &ar);
  1045. LogError("Invalid data type used for GetItem in %s (line %d)", ar.source, ar.currentline);
  1046. }
  1047. else
  1048. ret = data->item;
  1049. }
  1050. return ret;
  1051. }
  1052. Skill* LuaInterface::GetSkill(lua_State* state, int8 arg_num) {
  1053. Skill* ret = 0;
  1054. if (lua_islightuserdata(state, arg_num)) {
  1055. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1056. if(!data || !data->IsCorrectlyInitialized()){
  1057. LogError("GetSkill error while processing %s", lua_tostring(state, 0));
  1058. }
  1059. else if(!data->IsSkill()){
  1060. lua_Debug ar;
  1061. lua_getstack (state, 1, &ar);
  1062. lua_getinfo(state, "Sln", &ar);
  1063. LogError("Invalid data type used for GetSkill in %s (line %d)", ar.source, ar.currentline);
  1064. }
  1065. else
  1066. ret = data->skill;
  1067. }
  1068. return ret;
  1069. }
  1070. ZoneServer* LuaInterface::GetZone(lua_State* state, int8 arg_num) {
  1071. ZoneServer* ret = 0;
  1072. if(lua_islightuserdata(state, arg_num)){
  1073. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1074. if(!data || !data->IsCorrectlyInitialized()){
  1075. LogError("GetZone error while processing %s", lua_tostring(state, -1));
  1076. }
  1077. else if(!data->IsZone()){
  1078. lua_Debug ar;
  1079. lua_getstack (state, 1, &ar);
  1080. lua_getinfo(state, "Sln", &ar);
  1081. LogError("Invalid data type used for GetZone in %s (line %d)", ar.source, ar.currentline);
  1082. }
  1083. else
  1084. ret = data->zone;
  1085. }
  1086. return ret;
  1087. }
  1088. sint32 LuaInterface::GetSInt32Value(lua_State* state, int8 arg_num) {
  1089. sint32 val = 0;
  1090. if(lua_isnumber(state, arg_num)){
  1091. val = lua_tointeger(state, arg_num);
  1092. }
  1093. return val;
  1094. }
  1095. int32 LuaInterface::GetInt32Value(lua_State* state, int8 arg_num) {
  1096. int32 val = 0;
  1097. if(lua_isnumber(state, arg_num)){
  1098. val = (int32)lua_tonumber(state, arg_num);
  1099. }
  1100. return val;
  1101. }
  1102. int16 LuaInterface::GetInt16Value(lua_State* state, int8 arg_num) {
  1103. int16 val = 0;
  1104. if(lua_isnumber(state, arg_num)){
  1105. val = lua_tointeger(state, arg_num);
  1106. }
  1107. return val;
  1108. }
  1109. int8 LuaInterface::GetInt8Value(lua_State* state, int8 arg_num) {
  1110. int8 val = 0;
  1111. if(lua_isnumber(state, arg_num)){
  1112. val = lua_tointeger(state, arg_num);
  1113. }
  1114. return val;
  1115. }
  1116. float LuaInterface::GetFloatValue(lua_State* state, int8 arg_num) {
  1117. float val = 0;
  1118. if(lua_isnumber(state, arg_num))
  1119. val = (float)lua_tonumber(state, arg_num);
  1120. return val;
  1121. }
  1122. string LuaInterface::GetStringValue(lua_State* state, int8 arg_num) {
  1123. string val;
  1124. if(lua_isstring(state, arg_num)){
  1125. size_t size = 0;
  1126. const char* str = lua_tolstring(state, arg_num, &size);
  1127. if(str)
  1128. val = string(str);
  1129. }
  1130. return val;
  1131. }
  1132. bool LuaInterface::GetBooleanValue(lua_State* state, int8 arg_num) {
  1133. return lua_toboolean(state, arg_num) == 1;
  1134. }
  1135. void LuaInterface::SetStringValue(lua_State* state, const char* value) {
  1136. lua_pushstring(state, value);
  1137. }
  1138. void LuaInterface::SetBooleanValue(lua_State* state, bool value) {
  1139. lua_pushboolean(state, value);
  1140. }
  1141. void LuaInterface::SetFloatValue(lua_State* state, float value) {
  1142. lua_pushnumber(state, value);
  1143. }
  1144. void LuaInterface::SetInt32Value(lua_State* state, int32 value) {
  1145. lua_pushinteger(state, value);
  1146. }
  1147. void LuaInterface::SetSInt32Value(lua_State* state, sint32 value) {
  1148. lua_pushinteger(state, value);
  1149. }
  1150. void LuaInterface::SetSpawnValue(lua_State* state, Spawn* spawn) {
  1151. LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper();
  1152. spawn_wrapper->spawn = spawn;
  1153. AddUserDataPtr(spawn_wrapper);
  1154. lua_pushlightuserdata(state, spawn_wrapper);
  1155. }
  1156. void LuaInterface::SetConversationValue(lua_State* state, vector<ConversationOption>* conversation) {
  1157. LUAConversationOptionWrapper* conv_wrapper = new LUAConversationOptionWrapper();
  1158. conv_wrapper->conversation_options = conversation;
  1159. AddUserDataPtr(conv_wrapper);
  1160. lua_pushlightuserdata(state, conv_wrapper);
  1161. }
  1162. void LuaInterface::SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow) {
  1163. LUAOptionWindowWrapper* option_wrapper = new LUAOptionWindowWrapper();
  1164. option_wrapper->option_window_option = optionWindow;
  1165. AddUserDataPtr(option_wrapper);
  1166. lua_pushlightuserdata(state, option_wrapper);
  1167. }
  1168. void LuaInterface::SetItemValue(lua_State* state, Item* item) {
  1169. LUAItemWrapper* item_wrapper = new LUAItemWrapper();
  1170. item_wrapper->item = item;
  1171. AddUserDataPtr(item_wrapper);
  1172. lua_pushlightuserdata(state, item_wrapper);
  1173. }
  1174. void LuaInterface::SetSkillValue(lua_State* state, Skill* skill) {
  1175. LUASkillWrapper* skill_wrapper = new LUASkillWrapper();
  1176. skill_wrapper->skill = skill;
  1177. AddUserDataPtr(skill_wrapper);
  1178. lua_pushlightuserdata(state, skill_wrapper);
  1179. }
  1180. void LuaInterface::SetQuestValue(lua_State* state, Quest* quest) {
  1181. LUAQuestWrapper* quest_wrapper = new LUAQuestWrapper();
  1182. quest_wrapper->quest = quest;
  1183. AddUserDataPtr(quest_wrapper);
  1184. lua_pushlightuserdata(state, quest_wrapper);
  1185. }
  1186. void LuaInterface::SetZoneValue(lua_State* state, ZoneServer* zone) {
  1187. LUAZoneWrapper* zone_wrapper = new LUAZoneWrapper();
  1188. zone_wrapper->zone = zone;
  1189. AddUserDataPtr(zone_wrapper);
  1190. lua_pushlightuserdata(state, zone_wrapper);
  1191. }
  1192. LuaSpell* LuaInterface::GetSpell(const char* name) {
  1193. string lua_script = string(name);
  1194. if (lua_script.find(".lua") == string::npos)
  1195. lua_script.append(".lua");
  1196. if(spells.count(lua_script) > 0)
  1197. {
  1198. LogWrite(LUA__DEBUG, 0, "LUA", "Found LUA Spell Script: '%s'", lua_script.c_str());
  1199. LuaSpell* spell = spells[lua_script];
  1200. LuaSpell* new_spell = new LuaSpell;
  1201. new_spell->state = spell->state;
  1202. new_spell->file_name = spell->file_name;
  1203. new_spell->timer = spell->timer;
  1204. new_spell->timer.Disable();
  1205. new_spell->resisted = false;
  1206. new_spell->interrupted = false;
  1207. new_spell->crit = false;
  1208. new_spell->last_spellattack_hit = false;
  1209. new_spell->MSpellTargets.SetName("LuaSpell.MSpellTargets");
  1210. new_spell->cancel_after_all_triggers = false;
  1211. new_spell->num_triggers = 0;
  1212. new_spell->had_triggers = false;
  1213. new_spell->had_dmg_remaining = false;
  1214. new_spell->slot_pos = 0;
  1215. new_spell->damage_remaining = 0;
  1216. new_spell->effect_bitmask = 0;
  1217. return new_spell;
  1218. }
  1219. else{
  1220. LogWrite(LUA__ERROR, 0, "LUA", "Error LUA Spell Script: '%s'", name);
  1221. return 0;
  1222. }
  1223. }
  1224. Mutex* LuaInterface::GetItemScriptMutex(const char* name) {
  1225. Mutex* mutex = 0;
  1226. if(item_scripts_mutex.count(name) > 0)
  1227. mutex = item_scripts_mutex[name];
  1228. if(!mutex){
  1229. mutex = new Mutex();
  1230. item_scripts_mutex[name] = mutex;
  1231. }
  1232. return mutex;
  1233. }
  1234. Mutex* LuaInterface::GetSpawnScriptMutex(const char* name) {
  1235. Mutex* mutex = 0;
  1236. if(spawn_scripts_mutex.count(string(name)) > 0)
  1237. mutex = spawn_scripts_mutex[name];
  1238. if(!mutex){
  1239. mutex = new Mutex();
  1240. spawn_scripts_mutex[name] = mutex;
  1241. }
  1242. return mutex;
  1243. }
  1244. Mutex* LuaInterface::GetZoneScriptMutex(const char* name) {
  1245. Mutex* mutex = 0;
  1246. if(zone_scripts_mutex.count(name) > 0)
  1247. mutex = zone_scripts_mutex[name];
  1248. if(!mutex){
  1249. mutex = new Mutex();
  1250. zone_scripts_mutex[name] = mutex;
  1251. }
  1252. return mutex;
  1253. }
  1254. void LuaInterface::UseItemScript(const char* name, lua_State* state, bool val) {
  1255. MItemScripts.writelock(__FUNCTION__, __LINE__);
  1256. item_scripts[name][state] = val;
  1257. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  1258. }
  1259. void LuaInterface::UseSpawnScript(const char* name, lua_State* state, bool val) {
  1260. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  1261. spawn_scripts[name][state] = val;
  1262. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  1263. }
  1264. void LuaInterface::UseZoneScript(const char* name, lua_State* state, bool val) {
  1265. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  1266. zone_scripts[name][state] = val;
  1267. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  1268. }
  1269. lua_State* LuaInterface::GetItemScript(const char* name, bool create_new, bool use) {
  1270. map<string, map<lua_State*, bool> >::iterator itr;
  1271. map<lua_State*, bool>::iterator item_script_itr;
  1272. lua_State* ret = 0;
  1273. Mutex* mutex = 0;
  1274. itr = item_scripts.find(name);
  1275. if(itr != item_scripts.end()) {
  1276. mutex = GetItemScriptMutex(name);
  1277. mutex->readlock(__FUNCTION__, __LINE__);
  1278. for(item_script_itr = itr->second.begin(); item_script_itr != itr->second.end(); item_script_itr++){
  1279. if(!item_script_itr->second){ //not in use
  1280. ret = item_script_itr->first;
  1281. if (use)
  1282. {
  1283. item_script_itr->second = true;
  1284. break; // don't keep iterating, we already have our result
  1285. }
  1286. }
  1287. }
  1288. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1289. }
  1290. if(!ret && create_new){
  1291. if(name && LoadItemScript(name))
  1292. ret = GetItemScript(name, create_new, use);
  1293. else{
  1294. LogError("Error LUA Item Script '%s'", name);
  1295. return 0;
  1296. }
  1297. }
  1298. return ret;
  1299. }
  1300. lua_State* LuaInterface::GetSpawnScript(const char* name, bool create_new, bool use) {
  1301. if (spawn_scripts_reloading)
  1302. return 0;
  1303. map<string, map<lua_State*, bool> >::iterator itr;
  1304. map<lua_State*, bool>::iterator spawn_script_itr;
  1305. lua_State* ret = 0;
  1306. Mutex* mutex = 0;
  1307. itr = spawn_scripts.find(name);
  1308. if(itr != spawn_scripts.end()) {
  1309. mutex = GetSpawnScriptMutex(name);
  1310. mutex->readlock(__FUNCTION__, __LINE__);
  1311. for(spawn_script_itr = itr->second.begin(); spawn_script_itr != itr->second.end(); spawn_script_itr++){
  1312. if(!spawn_script_itr->second){ //not in use
  1313. ret = spawn_script_itr->first;
  1314. if (use)
  1315. {
  1316. spawn_script_itr->second = true;
  1317. break; // don't keep iterating, we already have our result
  1318. }
  1319. }
  1320. }
  1321. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1322. }
  1323. if(!ret && create_new){
  1324. if(name && LoadSpawnScript(name))
  1325. ret = GetSpawnScript(name, create_new, use);
  1326. else{
  1327. LogError("Error LUA Spawn Script '%s'", name);
  1328. return 0;
  1329. }
  1330. }
  1331. return ret;
  1332. }
  1333. lua_State* LuaInterface::GetZoneScript(const char* name, bool create_new, bool use) {
  1334. map<string, map<lua_State*, bool> >::iterator itr;
  1335. map<lua_State*, bool>::iterator zone_script_itr;
  1336. lua_State* ret = 0;
  1337. Mutex* mutex = 0;
  1338. itr = zone_scripts.find(name);
  1339. if(itr != zone_scripts.end()) {
  1340. mutex = GetZoneScriptMutex(name);
  1341. mutex->readlock(__FUNCTION__, __LINE__);
  1342. for(zone_script_itr = itr->second.begin(); zone_script_itr != itr->second.end(); zone_script_itr++){
  1343. if(!zone_script_itr->second){ //not in use
  1344. ret = zone_script_itr->first;
  1345. if (use)
  1346. {
  1347. zone_script_itr->second = true;
  1348. break; // don't keep iterating, we already have our result
  1349. }
  1350. }
  1351. }
  1352. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1353. }
  1354. if(!ret && create_new){
  1355. if(name && LoadZoneScript(name))
  1356. ret = GetZoneScript(name);
  1357. else{
  1358. LogError("Error LUA Zone Script '%s'", name);
  1359. return 0;
  1360. }
  1361. }
  1362. return ret;
  1363. }
  1364. bool LuaInterface::RunItemScript(string script_name, const char* function_name, Item* item, Spawn* spawn) {
  1365. if(!item)
  1366. return false;
  1367. lua_State* state = GetItemScript(script_name.c_str(), true, true);
  1368. if(state){
  1369. Mutex* mutex = GetItemScriptMutex(script_name.c_str());
  1370. if(mutex)
  1371. mutex->readlock(__FUNCTION__, __LINE__);
  1372. else{
  1373. LogError("Error getting lock for '%s'", script_name.c_str());
  1374. UseItemScript(script_name.c_str(), state, false);
  1375. return false;
  1376. }
  1377. lua_getglobal(state, function_name);
  1378. lua_getglobal(state, function_name);
  1379. if (!lua_isfunction(state, lua_gettop(state))){
  1380. lua_pop(state, 1);
  1381. mutex->releasereadlock(__FUNCTION__);
  1382. UseItemScript(script_name.c_str(), state, false);
  1383. return false;
  1384. }
  1385. SetItemValue(state, item);
  1386. int8 num_parms = 1;
  1387. if(spawn){
  1388. SetSpawnValue(state, spawn);
  1389. num_parms++;
  1390. }
  1391. if(!CallItemScript(state, num_parms)){
  1392. if(mutex)
  1393. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1394. UseItemScript(script_name.c_str(), state, false);
  1395. return false;
  1396. }
  1397. if(mutex)
  1398. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1399. UseItemScript(script_name.c_str(), state, false);
  1400. return true;
  1401. }
  1402. else
  1403. return false;
  1404. }
  1405. bool LuaInterface::RunSpawnScript(string script_name, const char* function_name, Spawn* npc, Spawn* spawn, const char* message) {
  1406. if(!npc || spawn_scripts_reloading)
  1407. return false;
  1408. lua_State* state = GetSpawnScript(script_name.c_str(), true, true);
  1409. if(state){
  1410. Mutex* mutex = GetSpawnScriptMutex(script_name.c_str());
  1411. if(mutex)
  1412. mutex->readlock(__FUNCTION__, __LINE__);
  1413. else{
  1414. LogError("Error getting lock for '%s'", script_name.c_str());
  1415. UseSpawnScript(script_name.c_str(), state, false);
  1416. return false;
  1417. }
  1418. lua_getglobal(state, function_name);
  1419. if (!lua_isfunction(state, lua_gettop(state))){
  1420. lua_pop(state, 1);
  1421. mutex->releasereadlock(__FUNCTION__);
  1422. UseSpawnScript(script_name.c_str(), state, false);
  1423. return false;
  1424. }
  1425. SetSpawnValue(state, npc);
  1426. int8 num_parms = 1;
  1427. if(spawn){
  1428. SetSpawnValue(state, spawn);
  1429. num_parms++;
  1430. }
  1431. if(message){
  1432. SetStringValue(state, message);
  1433. num_parms++;
  1434. }
  1435. if(!CallSpawnScript(state, num_parms)){
  1436. if(mutex)
  1437. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1438. UseSpawnScript(script_name.c_str(), state, false);
  1439. return false;
  1440. }
  1441. if(mutex)
  1442. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1443. UseSpawnScript(script_name.c_str(), state, false);
  1444. return true;
  1445. }
  1446. else
  1447. return false;
  1448. }
  1449. bool LuaInterface::RunZoneScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, int32 grid_id) {
  1450. if (!zone)
  1451. return false;
  1452. lua_State* state = GetZoneScript(script_name.c_str(), true, true);
  1453. if (state) {
  1454. Mutex* mutex = GetZoneScriptMutex(script_name.c_str());
  1455. if (mutex)
  1456. mutex->readlock(__FUNCTION__, __LINE__);
  1457. else {
  1458. LogError("Error getting lock for '%s'", script_name.c_str());
  1459. UseZoneScript(script_name.c_str(), state, false);
  1460. return false;
  1461. }
  1462. lua_getglobal(state, function_name);
  1463. if (!lua_isfunction(state, lua_gettop(state))){
  1464. lua_pop(state, 1);
  1465. mutex->releasereadlock(__FUNCTION__);
  1466. UseZoneScript(script_name.c_str(), state, false);
  1467. return false;
  1468. }
  1469. SetZoneValue(state, zone);
  1470. int8 num_params = 1;
  1471. if (spawn) {
  1472. SetSpawnValue(state, spawn);
  1473. num_params++;
  1474. }
  1475. if (grid_id > 0) {
  1476. SetInt32Value(state, grid_id);
  1477. num_params++;
  1478. }
  1479. if (!CallZoneScript(state, num_params)) {
  1480. if (mutex)
  1481. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1482. UseZoneScript(script_name.c_str(), state, false);
  1483. return false;
  1484. }
  1485. if (mutex)
  1486. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1487. UseZoneScript(script_name.c_str(), state, false);
  1488. return true;
  1489. }
  1490. else
  1491. return false;
  1492. }
  1493. void LuaInterface::AddPendingSpellDelete(LuaSpell* spell) {
  1494. MSpellDelete.lock();
  1495. spells_pending_delete[spell] = Timer::GetCurrentTime2() + 10000;
  1496. MSpellDelete.unlock();
  1497. }
  1498. LUAUserData::LUAUserData(){
  1499. correctly_initialized = false;
  1500. quest = 0;
  1501. conversation_options = 0;
  1502. spawn = 0;
  1503. zone = 0;
  1504. skill = 0;
  1505. option_window_option = 0;
  1506. item = 0;
  1507. }
  1508. bool LUAUserData::IsCorrectlyInitialized(){
  1509. return correctly_initialized;
  1510. }
  1511. bool LUAUserData::IsConversationOption(){
  1512. return false;
  1513. }
  1514. bool LUAUserData::IsOptionWindow() {
  1515. return false;
  1516. }
  1517. bool LUAUserData::IsSpawn(){
  1518. return false;
  1519. }
  1520. bool LUAUserData::IsQuest(){
  1521. return false;
  1522. }
  1523. bool LUAUserData::IsZone(){
  1524. return false;
  1525. }
  1526. bool LUAUserData::IsItem(){
  1527. return false;
  1528. }
  1529. bool LUAUserData::IsSkill() {
  1530. return false;
  1531. }
  1532. LUAConversationOptionWrapper::LUAConversationOptionWrapper(){
  1533. correctly_initialized = true;
  1534. }
  1535. bool LUAConversationOptionWrapper::IsConversationOption(){
  1536. return true;
  1537. }
  1538. LUAOptionWindowWrapper::LUAOptionWindowWrapper() {
  1539. correctly_initialized = true;
  1540. }
  1541. bool LUAOptionWindowWrapper::IsOptionWindow() {
  1542. return true;
  1543. }
  1544. LUASpawnWrapper::LUASpawnWrapper(){
  1545. correctly_initialized = true;
  1546. }
  1547. bool LUASpawnWrapper::IsSpawn(){
  1548. return true;
  1549. }
  1550. LUAZoneWrapper::LUAZoneWrapper(){
  1551. correctly_initialized = true;
  1552. }
  1553. bool LUAZoneWrapper::IsZone(){
  1554. return true;
  1555. }
  1556. LUAQuestWrapper::LUAQuestWrapper(){
  1557. correctly_initialized = true;
  1558. }
  1559. bool LUAQuestWrapper::IsQuest(){
  1560. return true;
  1561. }
  1562. LUAItemWrapper::LUAItemWrapper(){
  1563. correctly_initialized = true;
  1564. }
  1565. bool LUAItemWrapper::IsItem(){
  1566. return true;
  1567. }
  1568. LUASkillWrapper::LUASkillWrapper() {
  1569. correctly_initialized = true;
  1570. }
  1571. bool LUASkillWrapper::IsSkill() {
  1572. return true;
  1573. }