LuaInterface.cpp 62 KB

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