9
3

LuaInterface.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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, "SetFactionID", EQ2Emu_lua_SetFactionID);
  620. lua_register(state, "GetFactionID", EQ2Emu_lua_GetFactionID);
  621. lua_register(state, "GetFactionAmount", EQ2Emu_lua_GetFactionAmount);
  622. lua_register(state, "GetGender", EQ2Emu_lua_GetGender);
  623. lua_register(state, "GetTarget", EQ2Emu_lua_GetTarget);
  624. lua_register(state, "HasFreeSlot", EQ2Emu_lua_HasFreeSlot);
  625. lua_register(state, "HasItemEquipped", EQ2Emu_lua_HasItemEquipped);
  626. lua_register(state, "GetEquippedItemByID", EQ2Emu_lua_GetEquippedItemByID);
  627. lua_register(state, "GetEquippedItemBySlot", EQ2Emu_lua_GetEquippedItemBySlot);
  628. lua_register(state, "GetItemByID", EQ2Emu_lua_GetItemByID);
  629. lua_register(state, "GetItemType", EQ2Emu_lua_GetItemType);
  630. lua_register(state, "GetSpellName", EQ2Emu_lua_GetSpellName);
  631. lua_register(state, "PerformCameraShake", EQ2Emu_lua_PerformCameraShake);
  632. lua_register(state, "GetModelType", EQ2Emu_lua_GetModelType);
  633. lua_register(state, "GetSpeed", EQ2Emu_lua_GetSpeed);
  634. lua_register(state, "HasMoved", EQ2Emu_lua_HasMoved);
  635. lua_register(state, "SpellDamage", EQ2Emu_lua_SpellDamage);
  636. lua_register(state, "CastSpell", EQ2Emu_lua_CastSpell);
  637. lua_register(state, "SpellHeal", EQ2Emu_lua_SpellHeal);
  638. lua_register(state, "AddItem", EQ2Emu_lua_AddItem);
  639. lua_register(state, "SummonItem", EQ2Emu_lua_SummonItem);
  640. lua_register(state, "RemoveItem", EQ2Emu_lua_RemoveItem);
  641. lua_register(state, "HasItem", EQ2Emu_lua_HasItem);
  642. lua_register(state, "SpawnMob", EQ2Emu_lua_Spawn);
  643. lua_register(state, "SummonPet", EQ2Emu_lua_SummonPet);
  644. lua_register(state, "AddSpawnAccess", EQ2Emu_lua_AddSpawnAccess);
  645. lua_register(state, "GetZone", EQ2Emu_lua_GetZone);
  646. lua_register(state, "GetZoneName", EQ2Emu_lua_GetZoneName);
  647. lua_register(state, "GetZoneID", EQ2Emu_lua_GetZoneID);
  648. lua_register(state, "Zone", EQ2Emu_lua_Zone);
  649. lua_register(state, "AddHate", EQ2Emu_lua_AddHate);
  650. lua_register(state, "IsAlive", EQ2Emu_lua_IsAlive);
  651. lua_register(state, "IsInCombat", EQ2Emu_lua_IsInCombat);
  652. lua_register(state, "Attack", EQ2Emu_lua_Attack);
  653. lua_register(state, "ApplySpellVisual", EQ2Emu_lua_ApplySpellVisual);
  654. lua_register(state, "IsPlayer", EQ2Emu_lua_IsPlayer);
  655. lua_register(state, "FaceTarget", EQ2Emu_lua_FaceTarget);
  656. lua_register(state, "MoveToLocation", EQ2Emu_lua_MoveToLocation);
  657. lua_register(state, "Shout", EQ2Emu_lua_Shout);
  658. lua_register(state, "Say", EQ2Emu_lua_Say);
  659. lua_register(state, "SayOOC", EQ2Emu_lua_SayOOC);
  660. lua_register(state, "Emote", EQ2Emu_lua_Emote);
  661. lua_register(state, "MovementLoopAddLocation", EQ2Emu_lua_MovementLoopAdd);
  662. lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation);
  663. lua_register(state, "AddTimer", EQ2Emu_lua_AddTimer);
  664. lua_register(state, "Harvest", EQ2Emu_lua_Harvest);
  665. lua_register(state, "AddSpellBonus", EQ2Emu_lua_AddSpellBonus);
  666. lua_register(state, "RemoveSpellBonus", EQ2Emu_lua_RemoveSpellBonus);
  667. lua_register(state, "AddSkillBonus", EQ2Emu_lua_AddSkillBonus);
  668. lua_register(state, "RemoveSkillBonus", EQ2Emu_lua_RemoveSkillBonus);
  669. lua_register(state, "AddControlEffect", EQ2Emu_lua_AddControlEffect);
  670. lua_register(state, "RemoveControlEffect", EQ2Emu_lua_RemoveControlEffect);
  671. lua_register(state, "GetCurrentZoneSafeLocation", EQ2Emu_lua_GetCurrentZoneSafeLocation);
  672. lua_register(state, "GetInt", EQ2Emu_lua_GetInt);
  673. lua_register(state, "GetWis", EQ2Emu_lua_GetWis);
  674. lua_register(state, "GetSta", EQ2Emu_lua_GetSta);
  675. lua_register(state, "GetStr", EQ2Emu_lua_GetStr);
  676. lua_register(state, "GetAgi", EQ2Emu_lua_GetAgi);
  677. lua_register(state, "SetInt", EQ2Emu_lua_SetInt);
  678. lua_register(state, "SetWis", EQ2Emu_lua_SetWis);
  679. lua_register(state, "SetSta", EQ2Emu_lua_SetSta);
  680. lua_register(state, "SetStr", EQ2Emu_lua_SetStr);
  681. lua_register(state, "SetAgi", EQ2Emu_lua_SetAgi);
  682. lua_register(state, "GetIntBase", EQ2Emu_lua_GetIntBase);
  683. lua_register(state, "GetWisBase", EQ2Emu_lua_GetWisBase);
  684. lua_register(state, "GetStaBase", EQ2Emu_lua_GetStaBase);
  685. lua_register(state, "GetStrBase", EQ2Emu_lua_GetStrBase);
  686. lua_register(state, "GetAgiBase", EQ2Emu_lua_GetAgiBase);
  687. lua_register(state, "SetIntBase", EQ2Emu_lua_SetIntBase);
  688. lua_register(state, "SetWisBase", EQ2Emu_lua_SetWisBase);
  689. lua_register(state, "SetStaBase", EQ2Emu_lua_SetStaBase);
  690. lua_register(state, "SetStrBase", EQ2Emu_lua_SetStrBase);
  691. lua_register(state, "SetAgiBase", EQ2Emu_lua_SetAgiBase);
  692. lua_register(state, "GetSpawn", EQ2Emu_lua_GetSpawn);
  693. lua_register(state, "GetVariableValue", EQ2Emu_lua_GetVariableValue);
  694. lua_register(state, "GetCoinMessage", EQ2Emu_lua_GetCoinMessage);
  695. lua_register(state, "GetSpawnByGroupID", EQ2Emu_lua_GetSpawnByGroupID);
  696. lua_register(state, "GetSpawnByLocationID", EQ2Emu_lua_GetSpawnByLocationID);
  697. lua_register(state, "PlayFlavor", EQ2Emu_lua_PlayFlavor);
  698. lua_register(state, "PlaySound", EQ2Emu_lua_PlaySound);
  699. lua_register(state, "PlayVoice", EQ2Emu_lua_PlayVoice);
  700. lua_register(state, "PlayAnimation", EQ2Emu_lua_PlayAnimation);
  701. lua_register(state, "AddLootItem", EQ2Emu_lua_AddLootItem);
  702. lua_register(state, "RemoveLootItem", EQ2Emu_lua_RemoveLootItem);
  703. lua_register(state, "AddLootCoin", EQ2Emu_lua_AddLootCoin);
  704. lua_register(state, "GiveLoot", EQ2Emu_lua_GiveLoot);
  705. lua_register(state, "HasPendingLootItem", EQ2Emu_lua_HasPendingLootItem);
  706. lua_register(state, "HasPendingLoot", EQ2Emu_lua_HasPendingLoot);
  707. lua_register(state, "SetLootCoin", EQ2Emu_lua_SetLootCoin);
  708. lua_register(state, "GetLootCoin", EQ2Emu_lua_GetLootCoin);
  709. lua_register(state, "SetPlayerProximityFunction", EQ2Emu_lua_SetPlayerProximityFunction);
  710. lua_register(state, "SetLocationProximityFunction", EQ2Emu_lua_SetLocationProximityFunction);
  711. lua_register(state, "CreateConversation", EQ2Emu_lua_CreateConversation);
  712. lua_register(state, "AddConversationOption", EQ2Emu_lua_AddConversationOption);
  713. lua_register(state, "StartConversation", EQ2Emu_lua_StartConversation);
  714. lua_register(state, "CloseConversation", EQ2Emu_lua_CloseConversation);
  715. lua_register(state, "CloseItemConversation", EQ2Emu_lua_CloseItemConversation);
  716. //lua_register(state, "StartItemConversation", EQ2Emu_lua_StartItemConversation);
  717. lua_register(state, "StartDialogConversation", EQ2Emu_lua_StartDialogConversation);
  718. lua_register(state, "SpawnSet", EQ2Emu_lua_SpawnSet);
  719. lua_register(state, "SpawnSetByDistance", EQ2Emu_lua_SpawnSetByDistance);
  720. lua_register(state, "SpawnMove", EQ2Emu_lua_SpawnMove);
  721. lua_register(state, "KillSpawn", EQ2Emu_lua_KillSpawn);
  722. lua_register(state, "KillSpawnByDistance", EQ2Emu_lua_KillSpawnByDistance);
  723. lua_register(state, "Despawn", EQ2Emu_lua_Despawn);
  724. lua_register(state, "IsBindAllowed", EQ2Emu_lua_IsBindAllowed);
  725. lua_register(state, "IsGateAllowed", EQ2Emu_lua_IsGateAllowed);
  726. lua_register(state, "Bind", EQ2Emu_lua_Bind);
  727. lua_register(state, "Gate", EQ2Emu_lua_Gate);
  728. lua_register(state, "SendMessage", EQ2Emu_lua_SendMessage);
  729. lua_register(state, "SendPopUpMessage", EQ2Emu_lua_SendPopUpMessage);
  730. lua_register(state, "SetServerControlFlag", EQ2Emu_lua_SetServerControlFlag);
  731. lua_register(state, "ToggleTracking", EQ2Emu_lua_ToggleTracking);
  732. lua_register(state, "AddPrimaryEntityCommand", EQ2Emu_lua_AddPrimaryEntityCommand);
  733. lua_register(state, "AddSpellBookEntry", EQ2Emu_lua_AddSpellBookEntry);
  734. lua_register(state, "Interrupt", EQ2Emu_lua_Interrupt);
  735. lua_register(state, "Stealth", EQ2Emu_lua_Stealth);
  736. lua_register(state, "IsInvis", EQ2Emu_lua_IsInvis);
  737. lua_register(state, "IsStealthed", EQ2Emu_lua_IsStealthed);
  738. lua_register(state, "AddSpawnIDAccess", EQ2Emu_lua_AddSpawnIDAccess);
  739. lua_register(state, "RemoveSpawnIDAccess", EQ2Emu_lua_RemoveSpawnIDAccess);
  740. lua_register(state, "HasRecipeBook", EQ2Emu_lua_HasRecipeBook);
  741. lua_register(state, "SetRequiredQuest", EQ2Emu_lua_SetRequiredQuest);
  742. lua_register(state, "SetRequiredHistory", EQ2Emu_lua_SetRequiredHistory);
  743. lua_register(state, "SetStepComplete", EQ2Emu_lua_SetStepComplete);
  744. lua_register(state, "AddStepProgress", EQ2Emu_lua_AddStepProgress);
  745. lua_register(state, "UpdateQuestTaskGroupDescription", EQ2Emu_lua_UpdateQuestTaskGroupDescription);
  746. lua_register(state, "GetTaskGroupStep", EQ2Emu_lua_GetTaskGroupStep);
  747. lua_register(state, "GetQuestStep", EQ2Emu_lua_GetQuestStep);
  748. lua_register(state, "QuestStepIsComplete", EQ2Emu_lua_QuestStepIsComplete);
  749. lua_register(state, "RegisterQuest", EQ2Emu_lua_RegisterQuest);
  750. lua_register(state, "SetQuestPrereqLevel", EQ2Emu_lua_SetQuestPrereqLevel);
  751. lua_register(state, "AddQuestPrereqQuest", EQ2Emu_lua_AddQuestPrereqQuest);
  752. lua_register(state, "AddQuestPrereqItem", EQ2Emu_lua_AddQuestPrereqItem);
  753. lua_register(state, "AddQuestPrereqFaction", EQ2Emu_lua_AddQuestPrereqFaction);
  754. lua_register(state, "AddQuestPrereqRace", EQ2Emu_lua_AddQuestPrereqRace);
  755. lua_register(state, "AddQuestPrereqModelType", EQ2Emu_lua_AddQuestPrereqModelType);
  756. lua_register(state, "AddQuestPrereqClass", EQ2Emu_lua_AddQuestPrereqClass);
  757. lua_register(state, "AddQuestPrereqTradeskillLevel", EQ2Emu_lua_AddQuestPrereqTradeskillLevel);
  758. lua_register(state, "AddQuestPrereqTradeskillClass", EQ2Emu_lua_AddQuestPrereqTradeskillClass);
  759. lua_register(state, "AddQuestSelectableRewardItem", EQ2Emu_lua_AddQuestSelectableRewardItem);
  760. lua_register(state, "AddQuestRewardItem", EQ2Emu_lua_AddQuestRewardItem);
  761. lua_register(state, "AddQuestRewardCoin", EQ2Emu_lua_AddQuestRewardCoin);
  762. lua_register(state, "AddQuestRewardFaction", EQ2Emu_lua_AddQuestRewardFaction);
  763. lua_register(state, "SetQuestRewardStatus", EQ2Emu_lua_SetQuestRewardStatus);
  764. lua_register(state, "SetQuestRewardComment", EQ2Emu_lua_SetQuestRewardComment);
  765. lua_register(state, "SetQuestRewardExp", EQ2Emu_lua_SetQuestRewardExp);
  766. lua_register(state, "AddQuestStepKill", EQ2Emu_lua_AddQuestStepKill);
  767. lua_register(state, "AddQuestStep", EQ2Emu_lua_AddQuestStep);
  768. lua_register(state, "AddQuestStepChat", EQ2Emu_lua_AddQuestStepChat);
  769. lua_register(state, "AddQuestStepObtainItem", EQ2Emu_lua_AddQuestStepObtainItem);
  770. lua_register(state, "AddQuestStepLocation", EQ2Emu_lua_AddQuestStepLocation);
  771. lua_register(state, "AddQuestStepSpell", EQ2Emu_lua_AddQuestStepSpell);
  772. lua_register(state, "AddQuestStepCraft", EQ2Emu_lua_AddQuestStepCraft);
  773. lua_register(state, "AddQuestStepHarvest", EQ2Emu_lua_AddQuestStepHarvest);
  774. lua_register(state, "AddQuestStepCompleteAction", EQ2Emu_lua_AddQuestStepCompleteAction);
  775. lua_register(state, "AddQuestStepProgressAction", EQ2Emu_lua_AddQuestStepProgressAction);
  776. lua_register(state, "SetQuestCompleteAction", EQ2Emu_lua_SetQuestCompleteAction);
  777. lua_register(state, "GiveQuestReward", EQ2Emu_lua_GiveQuestReward);
  778. lua_register(state, "UpdateQuestStepDescription", EQ2Emu_lua_UpdateQuestStepDescription);
  779. lua_register(state, "UpdateQuestDescription", EQ2Emu_lua_UpdateQuestDescription);
  780. lua_register(state, "UpdateQuestZone", EQ2Emu_lua_UpdateQuestZone);
  781. lua_register(state, "SetCompletedDescription", EQ2Emu_lua_SetCompletedDescription);
  782. lua_register(state, "OfferQuest", EQ2Emu_lua_OfferQuest);
  783. lua_register(state, "ProvidesQuest", EQ2Emu_lua_ProvidesQuest);
  784. lua_register(state, "HasQuest", EQ2Emu_lua_HasQuest);
  785. lua_register(state, "HasCompletedQuest", EQ2Emu_lua_HasCompletedQuest);
  786. lua_register(state, "QuestIsComplete", EQ2Emu_lua_QuestIsComplete);
  787. lua_register(state, "QuestReturnNPC", EQ2Emu_lua_QuestReturnNPC);
  788. lua_register(state, "GetQuest", EQ2Emu_lua_GetQuest);
  789. lua_register(state, "HasCollectionsToHandIn", EQ2Emu_lua_HasCollectionsToHandIn);
  790. lua_register(state, "HandInCollections", EQ2Emu_lua_HandInCollections);
  791. lua_register(state, "UseWidget", EQ2Emu_lua_UseWidget);
  792. lua_register(state, "SetSpellList", EQ2Emu_lua_SetSpellList);
  793. lua_register(state, "GetPet", EQ2Emu_lua_GetPet);
  794. lua_register(state, "Charm", EQ2Emu_lua_Charm);
  795. lua_register(state, "GetGroup", EQ2Emu_lua_GetGroup);
  796. lua_register(state, "SetCompleteFlag", EQ2Emu_lua_SetCompleteFlag);
  797. lua_register(state, "SetQuestYellow", EQ2Emu_lua_SetQuestYellow);
  798. lua_register(state, "CanReceiveQuest", EQ2Emu_lua_CanReceiveQuest);
  799. lua_register(state, "AddTransportSpawn", EQ2Emu_lua_AddTransportSpawn);
  800. // Option window
  801. lua_register(state, "CreateOptionWindow", EQ2Emu_lua_CreateOptionWindow);
  802. lua_register(state, "AddOptionWindowOption", EQ2Emu_lua_AddOptionWindowOption);
  803. lua_register(state, "SendOptionWindow", EQ2Emu_lua_SendOptionWindow);
  804. lua_register(state, "GetTradeskillClass", EQ2Emu_lua_GetTradeskillClass);
  805. lua_register(state, "GetTradeskillLevel", EQ2Emu_lua_GetTradeskillLevel);
  806. lua_register(state, "GetTradeskillClassName", EQ2Emu_lua_GetTradeskillClassName);
  807. lua_register(state, "SetTradeskillLevel", EQ2Emu_lua_SetTradeskillLevel);
  808. lua_register(state, "SummonDeityPet", EQ2Emu_lua_SummonDeityPet);
  809. lua_register(state, "SummonCosmeticPet", EQ2Emu_lua_SummonCosmeticPet);
  810. lua_register(state, "DismissPet", EQ2Emu_lua_DismissPet);
  811. lua_register(state, "GetCharmedPet", EQ2Emu_lua_GetCharmedPet);
  812. lua_register(state, "GetDeityPet", EQ2Emu_lua_GetDeityPet);
  813. lua_register(state, "GetCosmeticPet", EQ2Emu_lua_GetCosmeticPet);
  814. lua_register(state, "SetQuestFeatherColor", EQ2Emu_lua_SetQuestFeatherColor);
  815. lua_register(state, "RemoveSpawnAccess", EQ2Emu_lua_RemoveSpawnAccess);
  816. lua_register(state, "SpawnByLocationID", EQ2Emu_lua_SpawnByLocationID);
  817. lua_register(state, "CastEntityCommand", EQ2Emu_lua_CastEntityCommand);
  818. lua_register(state, "SetLuaBrain", EQ2Emu_lua_SetLuaBrain);
  819. lua_register(state, "SetBrainTick", EQ2Emu_lua_SetBrainTick);
  820. lua_register(state, "SetFollowTarget", EQ2Emu_lua_SetFollowTarget);
  821. lua_register(state, "GetFollowTarget", EQ2Emu_lua_GetFollowTarget);
  822. lua_register(state, "ToggleFollow", EQ2Emu_lua_ToggleFollow);
  823. lua_register(state, "IsFollowing", EQ2Emu_lua_IsFollowing);
  824. lua_register(state, "SetTempVariable", EQ2Emu_lua_SetTempVariable);
  825. lua_register(state, "GetTempVariable", EQ2Emu_lua_GetTempVariable);
  826. lua_register(state, "GiveQuestItem", EQ2Emu_lua_GiveQuestItem);
  827. lua_register(state, "SetQuestRepeatable", EQ2Emu_lua_SetQuestRepeatable);
  828. lua_register(state, "AddWard", EQ2Emu_lua_AddWard);
  829. lua_register(state, "AddToWard", EQ2Emu_lua_AddToWard);
  830. lua_register(state, "RemoveWard", EQ2Emu_lua_RemoveWard);
  831. lua_register(state, "GetWardAmountLeft", EQ2Emu_lua_GetWardAmountLeft);
  832. lua_register(state, "GetWardValue", EQ2Emu_lua_GetWardValue);
  833. lua_register(state, "SetTarget", EQ2Emu_lua_SetTarget);
  834. lua_register(state, "IsPet", EQ2Emu_lua_IsPet);
  835. lua_register(state, "GetOwner", EQ2Emu_lua_GetOwner);
  836. lua_register(state, "SetInCombat", EQ2Emu_lua_SetInCombat);
  837. lua_register(state, "CompareSpawns", EQ2Emu_lua_CompareSpawns);
  838. lua_register(state, "Runback", EQ2Emu_lua_Runback);
  839. lua_register(state, "GetRunbackDistance", EQ2Emu_lua_GetRunbackDistance);
  840. lua_register(state, "IsCasting", EQ2Emu_lua_IsCasting);
  841. lua_register(state, "IsMezzed", EQ2Emu_lua_IsMezzed);
  842. lua_register(state, "IsStunned", EQ2Emu_lua_IsStunned);
  843. lua_register(state, "IsMezzedOrStunned", EQ2Emu_lua_IsMezzedOrStunned);
  844. lua_register(state, "ProcessSpell", EQ2Emu_lua_ProcessSpell);
  845. lua_register(state, "ProcessMelee", EQ2Emu_lua_ProcessMelee);
  846. lua_register(state, "HasRecovered", EQ2Emu_lua_HasRecovered);
  847. lua_register(state, "GetEncounterSize", EQ2Emu_lua_GetEncounterSize);
  848. lua_register(state, "GetMostHated", EQ2Emu_lua_GetMostHated);
  849. lua_register(state, "ClearHate", EQ2Emu_lua_ClearHate);
  850. lua_register(state, "ClearEncounter", EQ2Emu_lua_ClearEncounter);
  851. lua_register(state, "GetEncounter", EQ2Emu_lua_GetEncounter);
  852. lua_register(state, "GetHateList", EQ2Emu_lua_GetHateList);
  853. lua_register(state, "HasGroup", EQ2Emu_lua_HasGroup);
  854. lua_register(state, "HasSpellEffect", EQ2Emu_lua_HasSpellEffect);
  855. lua_register(state, "SetSuccessTimer", EQ2Emu_lua_SetSuccessTimer);
  856. lua_register(state, "SetFailureTimer", EQ2Emu_lua_SetFailureTimer);
  857. lua_register(state, "IsGroundSpawn", EQ2Emu_lua_IsGroundSpawn);
  858. lua_register(state, "CanHarvest", EQ2Emu_lua_CanHarvest);
  859. lua_register(state, "SummonDumbFirePet", EQ2Emu_lua_SummonDumbFirePet);
  860. lua_register(state, "GetSkillValue", EQ2Emu_lua_GetSkillValue);
  861. lua_register(state, "GetSkillMaxValue", EQ2Emu_lua_GetSkillMaxValue);
  862. lua_register(state, "GetSkillName", EQ2Emu_lua_GetSkillName);
  863. lua_register(state, "SetSkillMaxValue", EQ2Emu_lua_SetSkillMaxValue);
  864. lua_register(state, "SetSkillValue", EQ2Emu_lua_SetSkillValue);
  865. lua_register(state, "GetSkill", EQ2Emu_lua_GetSkill);
  866. lua_register(state, "GetSkillIDByName", EQ2Emu_lua_GetSkillIDByName);
  867. lua_register(state, "AddProc", EQ2Emu_lua_AddProc);
  868. lua_register(state, "RemoveProc", EQ2Emu_lua_RemoveProc);
  869. lua_register(state, "Knockback", EQ2Emu_lua_Knockback);
  870. lua_register(state, "IsEpic", EQ2Emu_lua_IsEpic);
  871. lua_register(state, "IsHeroic", EQ2Emu_lua_IsHeroic);
  872. lua_register(state, "ProcDamage", EQ2Emu_lua_ProcDamage);
  873. lua_register(state, "LastSpellAttackHit", EQ2Emu_lua_LastSpellAttackHit);
  874. lua_register(state, "IsBehind", EQ2Emu_lua_IsBehind);
  875. lua_register(state, "IsFlanking", EQ2Emu_lua_IsFlanking);
  876. lua_register(state, "AddSpellTimer", EQ2Emu_lua_AddSpellTimer);
  877. lua_register(state, "GetItemCount", EQ2Emu_lua_GetItemCount);
  878. lua_register(state, "SetItemCount", EQ2Emu_lua_SetItemCount);
  879. lua_register(state, "Resurrect", EQ2Emu_lua_Resurrect);
  880. lua_register(state, "BreatheUnderwater", EQ2Emu_lua_BreatheUnderwater);
  881. lua_register(state, "BlurVision", EQ2Emu_lua_BlurVision);
  882. lua_register(state, "SetVision", EQ2Emu_lua_SetVision);
  883. lua_register(state, "GetItemSkillReq", EQ2Emu_lua_GetItemSkillReq);
  884. lua_register(state, "SetSpeedMultiplier", EQ2Emu_lua_SetSpeedMultiplier);
  885. lua_register(state, "SetIllusion", EQ2Emu_lua_SetIllusion);
  886. lua_register(state, "ResetIllusion", EQ2Emu_lua_ResetIllusion);
  887. lua_register(state, "AddThreatTransfer", EQ2Emu_lua_AddThreatTransfer);
  888. lua_register(state, "RemoveThreatTransfer", EQ2Emu_lua_RemoveThreatTransfer);
  889. lua_register(state, "CureByType", EQ2Emu_lua_CureByType);
  890. lua_register(state, "CureByControlEffect", EQ2Emu_lua_CureByControlEffect);
  891. lua_register(state, "AddSpawnSpellBonus", EQ2Emu_lua_AddSpawnSpellBonus);
  892. lua_register(state, "CancelSpell", EQ2Emu_lua_CancelSpell);
  893. lua_register(state, "RemoveStealth", EQ2Emu_lua_RemoveStealth);
  894. lua_register(state, "RemoveInvis", EQ2Emu_lua_RemoveInvis);
  895. lua_register(state, "StartHeroicOpportunity", EQ2Emu_lua_StartHeroicOpportunity);
  896. lua_register(state, "CopySpawnAppearance", EQ2Emu_lua_CopySpawnAppearance);
  897. lua_register(state, "SetSpellTriggerCount", EQ2Emu_lua_SetSpellTriggerCount);
  898. lua_register(state, "GetSpellTriggerCount", EQ2Emu_lua_GetSpellTriggerCount);
  899. lua_register(state, "RemoveTriggerFromSpell", EQ2Emu_lua_RemoveTriggerFromSpell);
  900. lua_register(state, "AddImmunitySpell", EQ2Emu_lua_AddImmunitySpell);
  901. lua_register(state, "RemoveImmunitySpell", EQ2Emu_lua_RemoveImmunitySpell);
  902. lua_register(state, "SetSpellSnareValue", EQ2Emu_lua_SetSpellSnareValue);
  903. lua_register(state, "CheckRaceType", EQ2Emu_lua_CheckRaceType);
  904. lua_register(state, "GetRaceType", EQ2Emu_lua_GetRaceType);
  905. lua_register(state, "GetRaceBaseType", EQ2Emu_lua_GetRaceBaseType);
  906. lua_register(state, "GetQuestFlags", EQ2Emu_lua_GetQuestFlags);
  907. lua_register(state, "SetQuestFlags", EQ2Emu_lua_SetQuestFlags);
  908. lua_register(state, "SetQuestTimer", EQ2Emu_lua_SetQuestTimer);
  909. lua_register(state, "RemoveQuestStep", EQ2Emu_lua_RemoveQuestStep);
  910. lua_register(state, "ResetQuestStep", EQ2Emu_lua_ResetQuestStep);
  911. lua_register(state, "SetQuestTimerComplete", EQ2Emu_lua_SetQuestTimerComplete);
  912. lua_register(state, "AddQuestStepFailureAction", EQ2Emu_lua_AddQuestStepFailureAction);
  913. lua_register(state, "SetStepFailed", EQ2Emu_lua_SetStepFailed);
  914. lua_register(state, "GetQuestCompleteCount", EQ2Emu_lua_GetQuestCompleteCount);
  915. lua_register(state, "SetServerVariable", EQ2Emu_lua_SetServerVariable);
  916. lua_register(state, "GetServerVariable", EQ2Emu_lua_GetServerVariable);
  917. lua_register(state, "HasLanguage", EQ2Emu_lua_HasLanguage);
  918. lua_register(state, "AddLanguage", EQ2Emu_lua_AddLanguage);
  919. lua_register(state, "IsNight", EQ2Emu_lua_IsNight);
  920. lua_register(state, "AddMultiFloorLift", EQ2Emu_lua_AddMultiFloorLift);
  921. lua_register(state, "StartAutoMount", EQ2Emu_lua_StartAutoMount);
  922. lua_register(state, "EndAutoMount", EQ2Emu_lua_EndAutoMount);
  923. lua_register(state, "IsOnAutoMount", EQ2Emu_lua_IsOnAutoMount);
  924. lua_register(state, "SetPlayerHistory", EQ2Emu_lua_SetPlayerHistory);
  925. lua_register(state, "GetPlayerHistory", EQ2Emu_lua_GetPlayerHistory);
  926. lua_register(state, "SetGridID", EQ2Emu_lua_SetGridID);
  927. lua_register(state, "GetQuestStepProgress", EQ2Emu_lua_GetQuestStepProgress);
  928. lua_register(state, "SetPlayerLevel", EQ2Emu_lua_SetPlayerLevel);
  929. lua_register(state, "AddCoin", EQ2Emu_lua_AddCoin);
  930. lua_register(state, "RemoveCoin", EQ2Emu_lua_RemoveCoin);
  931. lua_register(state, "GetPlayersInZone", EQ2Emu_lua_GetPlayersInZone);
  932. lua_register(state, "SpawnGroupByID", EQ2Emu_lua_SpawnGroupByID);
  933. lua_register(state, "SetSpawnAnimation", EQ2Emu_lua_SetSpawnAnimation);
  934. lua_register(state, "GetClientVersion", EQ2Emu_lua_GetClientVersion);
  935. lua_register(state, "GetItemID", EQ2Emu_lua_GetItemID);
  936. lua_register(state, "IsEntity", EQ2Emu_lua_IsEntity);
  937. lua_register(state, "GetOrigX", EQ2Emu_lua_GetOrigX);
  938. lua_register(state, "GetOrigY", EQ2Emu_lua_GetOrigY);
  939. lua_register(state, "GetOrigZ", EQ2Emu_lua_GetOrigZ);
  940. lua_register(state, "GetPCTOfHP", EQ2Emu_lua_GetPCTOfHP);
  941. lua_register(state, "GetPCTOfPower", EQ2Emu_lua_GetPCTOfPower);
  942. lua_register(state, "GetBoundZoneID", EQ2Emu_lua_GetBoundZoneID);
  943. lua_register(state, "Evac", EQ2Emu_lua_Evac);
  944. lua_register(state, "GetSpellTier", EQ2Emu_lua_GetSpellTier);
  945. lua_register(state, "GetSpellID", EQ2Emu_lua_GetSpellID);
  946. lua_register(state, "StartTransmute", EQ2Emu_lua_StartTransmute);
  947. lua_register(state, "CompleteTransmute", EQ2Emu_lua_CompleteTransmute);
  948. lua_register(state, "ProcHate", EQ2Emu_lua_ProcHate);
  949. lua_register(state, "GetRandomSpawnByID", EQ2Emu_lua_GetRandomSpawnByID);
  950. lua_register(state, "AddLootToObject", EQ2Emu_lua_AddLootToObject);
  951. lua_register(state, "ShowLootWindow", EQ2Emu_lua_ShowLootWindow);
  952. lua_register(state, "AddPrimaryEntityCommandAllSpawns", EQ2Emu_lua_AddPrimaryEntityCommandAllSpawns);
  953. lua_register(state, "InstructionWindow", EQ2Emu_lua_InstructionWindow);
  954. lua_register(state, "ShowWindow", EQ2Emu_lua_ShowWindow);
  955. lua_register(state, "FlashWindow", EQ2Emu_lua_FlashWindow);
  956. lua_register(state, "DisplayText", EQ2Emu_lua_DisplayText);
  957. lua_register(state, "CheckLOS", EQ2Emu_lua_CheckLOS);
  958. lua_register(state, "CheckLOSByCoordinates", EQ2Emu_lua_CheckLOSByCoordinates);
  959. lua_register(state, "SetZoneExpansionFlag", EQ2Emu_lua_SetZoneExpansionFlag);
  960. lua_register(state, "GetZoneExpansionFlag", EQ2Emu_lua_GetZoneExpansionFlag);
  961. lua_register(state, "SetZoneHolidayFlag", EQ2Emu_lua_SetZoneHolidayFlag);
  962. lua_register(state, "GetZoneHolidayFlag", EQ2Emu_lua_GetZoneHolidayFlag);
  963. lua_register(state, "AddSpawnProximity", EQ2Emu_lua_AddSpawnProximity);
  964. lua_register(state, "CanSeeInvis", EQ2Emu_lua_CanSeeInvis);
  965. lua_register(state, "SetSeeInvis", EQ2Emu_lua_SetSeeInvis);
  966. lua_register(state, "SetSeeHide", EQ2Emu_lua_SetSeeHide);
  967. lua_register(state, "SetAccessToEntityCommand", EQ2Emu_lua_SetAccessToEntityCommand);
  968. lua_register(state, "RemovePrimaryEntityCommand", EQ2Emu_lua_RemovePrimaryEntityCommand);
  969. lua_register(state, "SendUpdateDefaultCommand", EQ2Emu_lua_SendUpdateDefaultCommand);
  970. lua_register(state, "SendTransporters", EQ2Emu_lua_SendTransporters);
  971. lua_register(state, "SetTemporaryTransportID", EQ2Emu_lua_SetTemporaryTransportID);
  972. lua_register(state, "GetTemporaryTransportID", EQ2Emu_lua_GetTemporaryTransportID);
  973. }
  974. void LuaInterface::LogError(const char* error, ...) {
  975. va_list argptr;
  976. char buffer[4096];
  977. va_start(argptr, error);
  978. vsnprintf(buffer, sizeof(buffer), error, argptr);
  979. va_end(argptr);
  980. SimpleLogError(buffer);
  981. }
  982. void LuaInterface::SimpleLogError(const char* error) {
  983. ProcessErrorMessage(error);
  984. LogWrite(LUA__ERROR, 0, "LUA", "%s", error);
  985. }
  986. void LuaInterface::ResetFunctionStack(lua_State* state) {
  987. lua_settop(state, 0);
  988. }
  989. void LuaInterface::AddUserDataPtr(LUAUserData* data) {
  990. MLUAUserData.lock();
  991. user_data[data] = Timer::GetCurrentTime2() + 300000; //allow a function to use this pointer for 5 minutes
  992. MLUAUserData.unlock();
  993. }
  994. void LuaInterface::DeletePendingSpells(bool all) {
  995. MSpellDelete.lock();
  996. if(spells_pending_delete.size() > 0){
  997. int32 time = Timer::GetCurrentTime2();
  998. map<LuaSpell*, int32>::iterator itr;
  999. vector<LuaSpell*> tmp_deletes;
  1000. vector<LuaSpell*>::iterator del_itr;
  1001. for(itr = spells_pending_delete.begin(); itr != spells_pending_delete.end(); itr++){
  1002. if(all || time >= itr->second)
  1003. tmp_deletes.push_back(itr->first);
  1004. }
  1005. LuaSpell* spell = 0;
  1006. for(del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++){
  1007. spell = *del_itr;
  1008. spells_pending_delete.erase(spell);
  1009. safe_delete(spell);
  1010. }
  1011. }
  1012. MSpellDelete.unlock();
  1013. }
  1014. void LuaInterface::DeleteUserDataPtrs(bool all) {
  1015. MLUAUserData.lock();
  1016. if(user_data.size() > 0){
  1017. map<LUAUserData*, int32>::iterator itr;
  1018. int32 time = Timer::GetCurrentTime2();
  1019. vector<LUAUserData*> tmp_deletes;
  1020. vector<LUAUserData*>::iterator del_itr;
  1021. for(itr = user_data.begin(); itr != user_data.end(); itr++){
  1022. if(all || time >= itr->second)
  1023. tmp_deletes.push_back(itr->first);
  1024. }
  1025. LUAUserData* data = 0;
  1026. for(del_itr = tmp_deletes.begin(); del_itr != tmp_deletes.end(); del_itr++){
  1027. data = *del_itr;
  1028. user_data.erase(data);
  1029. safe_delete(data);
  1030. }
  1031. }
  1032. MLUAUserData.unlock();
  1033. }
  1034. Spawn* LuaInterface::GetSpawn(lua_State* state, int8 arg_num) {
  1035. Spawn* 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("%s: GetSpawn error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1040. }
  1041. else if(!data->IsSpawn()){
  1042. lua_Debug ar;
  1043. lua_getstack (state, 1, &ar);
  1044. lua_getinfo(state, "Sln", &ar);
  1045. LogError("%s: Invalid data type used for GetSpawn in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1046. }
  1047. else
  1048. ret = data->spawn;
  1049. }
  1050. return ret;
  1051. }
  1052. vector<ConversationOption>* LuaInterface::GetConversation(lua_State* state, int8 arg_num) {
  1053. vector<ConversationOption>* 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("%s: GetConversation error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1058. }
  1059. else if(!data->IsConversationOption()){
  1060. lua_Debug ar;
  1061. lua_getstack (state, 1, &ar);
  1062. lua_getinfo(state, "Sln", &ar);
  1063. LogError("%s: Invalid data type used for GetConversation in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1064. }
  1065. else
  1066. ret = data->conversation_options;
  1067. }
  1068. return ret;
  1069. }
  1070. vector<OptionWindowOption>* LuaInterface::GetOptionWindow(lua_State* state, int8 arg_num) {
  1071. vector<OptionWindowOption>* 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("%s: GetOptionWindow error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1076. }
  1077. else if(!data->IsOptionWindow()){
  1078. lua_Debug ar;
  1079. lua_getstack (state, 1, &ar);
  1080. lua_getinfo(state, "Sln", &ar);
  1081. LogError("%s: Invalid data type used for GetOptionWindow in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1082. }
  1083. else
  1084. ret = data->option_window_option;
  1085. }
  1086. return ret;
  1087. }
  1088. Quest* LuaInterface::GetQuest(lua_State* state, int8 arg_num) {
  1089. Quest* ret = 0;
  1090. if(lua_islightuserdata(state, arg_num)){
  1091. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1092. if(!data || !data->IsCorrectlyInitialized()){
  1093. LogError("%s: GetQuest error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1094. }
  1095. else if(!data->IsQuest()){
  1096. lua_Debug ar;
  1097. lua_getstack (state, 1, &ar);
  1098. lua_getinfo(state, "Sln", &ar);
  1099. LogError("%s: Invalid data type used for GetQuest in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1100. }
  1101. else
  1102. ret = data->quest;
  1103. }
  1104. return ret;
  1105. }
  1106. Item* LuaInterface::GetItem(lua_State* state, int8 arg_num) {
  1107. Item* ret = 0;
  1108. if(lua_islightuserdata(state, arg_num)){
  1109. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1110. if(!data || !data->IsCorrectlyInitialized()){
  1111. LogError("%s: GetItem error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1112. }
  1113. else if(!data->IsItem()){
  1114. lua_Debug ar;
  1115. lua_getstack (state, 1, &ar);
  1116. lua_getinfo(state, "Sln", &ar);
  1117. LogError("%s: Invalid data type used for GetItem in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1118. }
  1119. else
  1120. ret = data->item;
  1121. }
  1122. return ret;
  1123. }
  1124. Skill* LuaInterface::GetSkill(lua_State* state, int8 arg_num) {
  1125. Skill* ret = 0;
  1126. if (lua_islightuserdata(state, arg_num)) {
  1127. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1128. if(!data || !data->IsCorrectlyInitialized()){
  1129. LogError("%s: GetSkill error while processing %s", GetScriptName(state), lua_tostring(state, 0));
  1130. }
  1131. else if(!data->IsSkill()){
  1132. lua_Debug ar;
  1133. lua_getstack (state, 1, &ar);
  1134. lua_getinfo(state, "Sln", &ar);
  1135. LogError("%s: Invalid data type used for GetSkill in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1136. }
  1137. else
  1138. ret = data->skill;
  1139. }
  1140. return ret;
  1141. }
  1142. ZoneServer* LuaInterface::GetZone(lua_State* state, int8 arg_num) {
  1143. ZoneServer* ret = 0;
  1144. if(lua_islightuserdata(state, arg_num)){
  1145. LUAUserData* data = (LUAUserData*)lua_touserdata(state, arg_num);
  1146. if(!data || !data->IsCorrectlyInitialized()){
  1147. LogError("%s: GetZone error while processing %s", GetScriptName(state), lua_tostring(state, -1));
  1148. }
  1149. else if(!data->IsZone()){
  1150. lua_Debug ar;
  1151. lua_getstack (state, 1, &ar);
  1152. lua_getinfo(state, "Sln", &ar);
  1153. LogError("%s: Invalid data type used for GetZone in %s (line %d)", GetScriptName(state), ar.source, ar.currentline);
  1154. }
  1155. else
  1156. ret = data->zone;
  1157. }
  1158. return ret;
  1159. }
  1160. sint32 LuaInterface::GetSInt32Value(lua_State* state, int8 arg_num) {
  1161. sint32 val = 0;
  1162. if(lua_isnumber(state, arg_num)){
  1163. val = lua_tointeger(state, arg_num);
  1164. }
  1165. return val;
  1166. }
  1167. int32 LuaInterface::GetInt32Value(lua_State* state, int8 arg_num) {
  1168. int32 val = 0;
  1169. if(lua_isnumber(state, arg_num)){
  1170. val = (int32)lua_tonumber(state, arg_num);
  1171. }
  1172. return val;
  1173. }
  1174. int16 LuaInterface::GetInt16Value(lua_State* state, int8 arg_num) {
  1175. int16 val = 0;
  1176. if(lua_isnumber(state, arg_num)){
  1177. val = lua_tointeger(state, arg_num);
  1178. }
  1179. return val;
  1180. }
  1181. int8 LuaInterface::GetInt8Value(lua_State* state, int8 arg_num) {
  1182. int8 val = 0;
  1183. if(lua_isnumber(state, arg_num)){
  1184. val = lua_tointeger(state, arg_num);
  1185. }
  1186. return val;
  1187. }
  1188. float LuaInterface::GetFloatValue(lua_State* state, int8 arg_num) {
  1189. float val = 0;
  1190. if(lua_isnumber(state, arg_num))
  1191. val = (float)lua_tonumber(state, arg_num);
  1192. return val;
  1193. }
  1194. string LuaInterface::GetStringValue(lua_State* state, int8 arg_num) {
  1195. string val;
  1196. if(lua_isstring(state, arg_num)){
  1197. size_t size = 0;
  1198. const char* str = lua_tolstring(state, arg_num, &size);
  1199. if(str)
  1200. val = string(str);
  1201. }
  1202. return val;
  1203. }
  1204. bool LuaInterface::GetBooleanValue(lua_State* state, int8 arg_num) {
  1205. return lua_toboolean(state, arg_num) == 1;
  1206. }
  1207. void LuaInterface::SetStringValue(lua_State* state, const char* value) {
  1208. lua_pushstring(state, value);
  1209. }
  1210. void LuaInterface::SetBooleanValue(lua_State* state, bool value) {
  1211. lua_pushboolean(state, value);
  1212. }
  1213. void LuaInterface::SetFloatValue(lua_State* state, float value) {
  1214. lua_pushnumber(state, value);
  1215. }
  1216. void LuaInterface::SetInt32Value(lua_State* state, int32 value) {
  1217. lua_pushinteger(state, value);
  1218. }
  1219. void LuaInterface::SetSInt32Value(lua_State* state, sint32 value) {
  1220. lua_pushinteger(state, value);
  1221. }
  1222. void LuaInterface::SetSpawnValue(lua_State* state, Spawn* spawn) {
  1223. LUASpawnWrapper* spawn_wrapper = new LUASpawnWrapper();
  1224. spawn_wrapper->spawn = spawn;
  1225. AddUserDataPtr(spawn_wrapper);
  1226. lua_pushlightuserdata(state, spawn_wrapper);
  1227. }
  1228. void LuaInterface::SetConversationValue(lua_State* state, vector<ConversationOption>* conversation) {
  1229. LUAConversationOptionWrapper* conv_wrapper = new LUAConversationOptionWrapper();
  1230. conv_wrapper->conversation_options = conversation;
  1231. AddUserDataPtr(conv_wrapper);
  1232. lua_pushlightuserdata(state, conv_wrapper);
  1233. }
  1234. void LuaInterface::SetOptionWindowValue(lua_State* state, vector<OptionWindowOption>* optionWindow) {
  1235. LUAOptionWindowWrapper* option_wrapper = new LUAOptionWindowWrapper();
  1236. option_wrapper->option_window_option = optionWindow;
  1237. AddUserDataPtr(option_wrapper);
  1238. lua_pushlightuserdata(state, option_wrapper);
  1239. }
  1240. void LuaInterface::SetItemValue(lua_State* state, Item* item) {
  1241. LUAItemWrapper* item_wrapper = new LUAItemWrapper();
  1242. item_wrapper->item = item;
  1243. AddUserDataPtr(item_wrapper);
  1244. lua_pushlightuserdata(state, item_wrapper);
  1245. }
  1246. void LuaInterface::SetSkillValue(lua_State* state, Skill* skill) {
  1247. LUASkillWrapper* skill_wrapper = new LUASkillWrapper();
  1248. skill_wrapper->skill = skill;
  1249. AddUserDataPtr(skill_wrapper);
  1250. lua_pushlightuserdata(state, skill_wrapper);
  1251. }
  1252. void LuaInterface::SetQuestValue(lua_State* state, Quest* quest) {
  1253. LUAQuestWrapper* quest_wrapper = new LUAQuestWrapper();
  1254. quest_wrapper->quest = quest;
  1255. AddUserDataPtr(quest_wrapper);
  1256. lua_pushlightuserdata(state, quest_wrapper);
  1257. }
  1258. void LuaInterface::SetZoneValue(lua_State* state, ZoneServer* zone) {
  1259. LUAZoneWrapper* zone_wrapper = new LUAZoneWrapper();
  1260. zone_wrapper->zone = zone;
  1261. AddUserDataPtr(zone_wrapper);
  1262. lua_pushlightuserdata(state, zone_wrapper);
  1263. }
  1264. LuaSpell* LuaInterface::GetSpell(const char* name) {
  1265. string lua_script = string(name);
  1266. if (lua_script.find(".lua") == string::npos)
  1267. lua_script.append(".lua");
  1268. if(spells.count(lua_script) > 0)
  1269. {
  1270. LogWrite(LUA__DEBUG, 0, "LUA", "Found LUA Spell Script: '%s'", lua_script.c_str());
  1271. LuaSpell* spell = spells[lua_script];
  1272. LuaSpell* new_spell = new LuaSpell;
  1273. new_spell->state = spell->state;
  1274. new_spell->file_name = spell->file_name;
  1275. new_spell->timer = spell->timer;
  1276. new_spell->timer.Disable();
  1277. new_spell->resisted = false;
  1278. new_spell->interrupted = false;
  1279. new_spell->crit = false;
  1280. new_spell->last_spellattack_hit = false;
  1281. new_spell->MSpellTargets.SetName("LuaSpell.MSpellTargets");
  1282. new_spell->cancel_after_all_triggers = false;
  1283. new_spell->num_triggers = 0;
  1284. new_spell->had_triggers = false;
  1285. new_spell->had_dmg_remaining = false;
  1286. new_spell->slot_pos = 0;
  1287. new_spell->damage_remaining = 0;
  1288. new_spell->effect_bitmask = 0;
  1289. return new_spell;
  1290. }
  1291. else{
  1292. LogWrite(LUA__ERROR, 0, "LUA", "Error LUA Spell Script: '%s'", name);
  1293. return 0;
  1294. }
  1295. }
  1296. Mutex* LuaInterface::GetItemScriptMutex(const char* name) {
  1297. Mutex* mutex = 0;
  1298. if(item_scripts_mutex.count(name) > 0)
  1299. mutex = item_scripts_mutex[name];
  1300. if(!mutex){
  1301. mutex = new Mutex();
  1302. item_scripts_mutex[name] = mutex;
  1303. }
  1304. return mutex;
  1305. }
  1306. Mutex* LuaInterface::GetSpawnScriptMutex(const char* name) {
  1307. Mutex* mutex = 0;
  1308. if(spawn_scripts_mutex.count(string(name)) > 0)
  1309. mutex = spawn_scripts_mutex[name];
  1310. if(!mutex){
  1311. mutex = new Mutex();
  1312. spawn_scripts_mutex[name] = mutex;
  1313. }
  1314. return mutex;
  1315. }
  1316. Mutex* LuaInterface::GetZoneScriptMutex(const char* name) {
  1317. Mutex* mutex = 0;
  1318. if(zone_scripts_mutex.count(name) > 0)
  1319. mutex = zone_scripts_mutex[name];
  1320. if(!mutex){
  1321. mutex = new Mutex();
  1322. zone_scripts_mutex[name] = mutex;
  1323. }
  1324. return mutex;
  1325. }
  1326. void LuaInterface::UseItemScript(const char* name, lua_State* state, bool val) {
  1327. MItemScripts.writelock(__FUNCTION__, __LINE__);
  1328. item_scripts[name][state] = val;
  1329. item_inverse_scripts[state] = name;
  1330. MItemScripts.releasewritelock(__FUNCTION__, __LINE__);
  1331. }
  1332. void LuaInterface::UseSpawnScript(const char* name, lua_State* state, bool val) {
  1333. MSpawnScripts.writelock(__FUNCTION__, __LINE__);
  1334. spawn_scripts[name][state] = val;
  1335. spawn_inverse_scripts[state] = name;
  1336. MSpawnScripts.releasewritelock(__FUNCTION__, __LINE__);
  1337. }
  1338. void LuaInterface::UseZoneScript(const char* name, lua_State* state, bool val) {
  1339. MZoneScripts.writelock(__FUNCTION__, __LINE__);
  1340. zone_scripts[name][state] = val;
  1341. zone_inverse_scripts[state] = name;
  1342. MZoneScripts.releasewritelock(__FUNCTION__, __LINE__);
  1343. }
  1344. lua_State* LuaInterface::GetItemScript(const char* name, bool create_new, bool use) {
  1345. map<string, map<lua_State*, bool> >::iterator itr;
  1346. map<lua_State*, bool>::iterator item_script_itr;
  1347. lua_State* ret = 0;
  1348. Mutex* mutex = 0;
  1349. itr = item_scripts.find(name);
  1350. if(itr != item_scripts.end()) {
  1351. mutex = GetItemScriptMutex(name);
  1352. mutex->readlock(__FUNCTION__, __LINE__);
  1353. for(item_script_itr = itr->second.begin(); item_script_itr != itr->second.end(); item_script_itr++){
  1354. if(!item_script_itr->second){ //not in use
  1355. ret = item_script_itr->first;
  1356. if (use)
  1357. {
  1358. item_script_itr->second = true;
  1359. break; // don't keep iterating, we already have our result
  1360. }
  1361. }
  1362. }
  1363. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1364. }
  1365. if(!ret && create_new){
  1366. if(name && LoadItemScript(name))
  1367. ret = GetItemScript(name, create_new, use);
  1368. else{
  1369. LogError("Error LUA Item Script '%s'", name);
  1370. return 0;
  1371. }
  1372. }
  1373. return ret;
  1374. }
  1375. lua_State* LuaInterface::GetSpawnScript(const char* name, bool create_new, bool use) {
  1376. if (spawn_scripts_reloading)
  1377. return 0;
  1378. map<string, map<lua_State*, bool> >::iterator itr;
  1379. map<lua_State*, bool>::iterator spawn_script_itr;
  1380. lua_State* ret = 0;
  1381. Mutex* mutex = 0;
  1382. itr = spawn_scripts.find(name);
  1383. if(itr != spawn_scripts.end()) {
  1384. mutex = GetSpawnScriptMutex(name);
  1385. mutex->readlock(__FUNCTION__, __LINE__);
  1386. for(spawn_script_itr = itr->second.begin(); spawn_script_itr != itr->second.end(); spawn_script_itr++){
  1387. if(!spawn_script_itr->second){ //not in use
  1388. ret = spawn_script_itr->first;
  1389. if (use)
  1390. {
  1391. spawn_script_itr->second = true;
  1392. break; // don't keep iterating, we already have our result
  1393. }
  1394. }
  1395. }
  1396. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1397. }
  1398. if(!ret && create_new){
  1399. if(name && LoadSpawnScript(name))
  1400. ret = GetSpawnScript(name, create_new, use);
  1401. else{
  1402. LogError("Error LUA Spawn Script '%s'", name);
  1403. return 0;
  1404. }
  1405. }
  1406. return ret;
  1407. }
  1408. lua_State* LuaInterface::GetZoneScript(const char* name, bool create_new, bool use) {
  1409. map<string, map<lua_State*, bool> >::iterator itr;
  1410. map<lua_State*, bool>::iterator zone_script_itr;
  1411. lua_State* ret = 0;
  1412. Mutex* mutex = 0;
  1413. itr = zone_scripts.find(name);
  1414. if(itr != zone_scripts.end()) {
  1415. mutex = GetZoneScriptMutex(name);
  1416. mutex->readlock(__FUNCTION__, __LINE__);
  1417. for(zone_script_itr = itr->second.begin(); zone_script_itr != itr->second.end(); zone_script_itr++){
  1418. if(!zone_script_itr->second){ //not in use
  1419. ret = zone_script_itr->first;
  1420. if (use)
  1421. {
  1422. zone_script_itr->second = true;
  1423. break; // don't keep iterating, we already have our result
  1424. }
  1425. }
  1426. }
  1427. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1428. }
  1429. if(!ret && create_new){
  1430. if(name && LoadZoneScript(name))
  1431. ret = GetZoneScript(name);
  1432. else{
  1433. LogError("Error LUA Zone Script '%s'", name);
  1434. return 0;
  1435. }
  1436. }
  1437. return ret;
  1438. }
  1439. bool LuaInterface::RunItemScript(string script_name, const char* function_name, Item* item, Spawn* spawn) {
  1440. if(!item)
  1441. return false;
  1442. lua_State* state = GetItemScript(script_name.c_str(), true, true);
  1443. if(state){
  1444. Mutex* mutex = GetItemScriptMutex(script_name.c_str());
  1445. if(mutex)
  1446. mutex->readlock(__FUNCTION__, __LINE__);
  1447. else{
  1448. LogError("Error getting lock for '%s'", script_name.c_str());
  1449. UseItemScript(script_name.c_str(), state, false);
  1450. return false;
  1451. }
  1452. lua_getglobal(state, function_name);
  1453. lua_getglobal(state, function_name);
  1454. if (!lua_isfunction(state, lua_gettop(state))){
  1455. lua_pop(state, 1);
  1456. mutex->releasereadlock(__FUNCTION__);
  1457. UseItemScript(script_name.c_str(), state, false);
  1458. return false;
  1459. }
  1460. SetItemValue(state, item);
  1461. int8 num_parms = 1;
  1462. if(spawn){
  1463. SetSpawnValue(state, spawn);
  1464. num_parms++;
  1465. }
  1466. if(!CallItemScript(state, num_parms)){
  1467. if(mutex)
  1468. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1469. UseItemScript(script_name.c_str(), state, false);
  1470. return false;
  1471. }
  1472. if(mutex)
  1473. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1474. UseItemScript(script_name.c_str(), state, false);
  1475. return true;
  1476. }
  1477. else
  1478. return false;
  1479. }
  1480. bool LuaInterface::RunSpawnScript(string script_name, const char* function_name, Spawn* npc, Spawn* spawn, const char* message) {
  1481. if(!npc || spawn_scripts_reloading)
  1482. return false;
  1483. lua_State* state = GetSpawnScript(script_name.c_str(), true, true);
  1484. if(state){
  1485. Mutex* mutex = GetSpawnScriptMutex(script_name.c_str());
  1486. if(mutex)
  1487. mutex->readlock(__FUNCTION__, __LINE__);
  1488. else{
  1489. LogError("Error getting lock for '%s'", script_name.c_str());
  1490. UseSpawnScript(script_name.c_str(), state, false);
  1491. return false;
  1492. }
  1493. lua_getglobal(state, function_name);
  1494. if (!lua_isfunction(state, lua_gettop(state))){
  1495. lua_pop(state, 1);
  1496. mutex->releasereadlock(__FUNCTION__);
  1497. UseSpawnScript(script_name.c_str(), state, false);
  1498. return false;
  1499. }
  1500. SetSpawnValue(state, npc);
  1501. int8 num_parms = 1;
  1502. if(spawn){
  1503. SetSpawnValue(state, spawn);
  1504. num_parms++;
  1505. }
  1506. if(message){
  1507. SetStringValue(state, message);
  1508. num_parms++;
  1509. }
  1510. if(!CallSpawnScript(state, num_parms)){
  1511. if(mutex)
  1512. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1513. UseSpawnScript(script_name.c_str(), state, false);
  1514. return false;
  1515. }
  1516. if(mutex)
  1517. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1518. UseSpawnScript(script_name.c_str(), state, false);
  1519. return true;
  1520. }
  1521. else
  1522. return false;
  1523. }
  1524. bool LuaInterface::RunZoneScript(string script_name, const char* function_name, ZoneServer* zone, Spawn* spawn, int32 grid_id, const char* signal) {
  1525. if (!zone)
  1526. return false;
  1527. lua_State* state = GetZoneScript(script_name.c_str(), true, true);
  1528. if (state) {
  1529. Mutex* mutex = GetZoneScriptMutex(script_name.c_str());
  1530. if (mutex)
  1531. mutex->readlock(__FUNCTION__, __LINE__);
  1532. else {
  1533. LogError("Error getting lock for '%s'", script_name.c_str());
  1534. UseZoneScript(script_name.c_str(), state, false);
  1535. return false;
  1536. }
  1537. lua_getglobal(state, function_name);
  1538. if (!lua_isfunction(state, lua_gettop(state))) {
  1539. lua_pop(state, 1);
  1540. mutex->releasereadlock(__FUNCTION__);
  1541. UseZoneScript(script_name.c_str(), state, false);
  1542. return false;
  1543. }
  1544. SetZoneValue(state, zone);
  1545. int8 num_params = 1;
  1546. if (spawn) {
  1547. SetSpawnValue(state, spawn);
  1548. num_params++;
  1549. }
  1550. if (grid_id > 0) {
  1551. SetInt32Value(state, grid_id);
  1552. num_params++;
  1553. }
  1554. if (signal) {
  1555. SetStringValue(state, signal);
  1556. num_params++;
  1557. }
  1558. if (!CallZoneScript(state, num_params)) {
  1559. if (mutex)
  1560. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1561. UseZoneScript(script_name.c_str(), state, false);
  1562. return false;
  1563. }
  1564. if (mutex)
  1565. mutex->releasereadlock(__FUNCTION__, __LINE__);
  1566. UseZoneScript(script_name.c_str(), state, false);
  1567. return true;
  1568. }
  1569. else
  1570. return false;
  1571. }
  1572. void LuaInterface::AddPendingSpellDelete(LuaSpell* spell) {
  1573. MSpellDelete.lock();
  1574. spells_pending_delete[spell] = Timer::GetCurrentTime2() + 10000;
  1575. MSpellDelete.unlock();
  1576. }
  1577. LUAUserData::LUAUserData(){
  1578. correctly_initialized = false;
  1579. quest = 0;
  1580. conversation_options = 0;
  1581. spawn = 0;
  1582. zone = 0;
  1583. skill = 0;
  1584. option_window_option = 0;
  1585. item = 0;
  1586. }
  1587. bool LUAUserData::IsCorrectlyInitialized(){
  1588. return correctly_initialized;
  1589. }
  1590. bool LUAUserData::IsConversationOption(){
  1591. return false;
  1592. }
  1593. bool LUAUserData::IsOptionWindow() {
  1594. return false;
  1595. }
  1596. bool LUAUserData::IsSpawn(){
  1597. return false;
  1598. }
  1599. bool LUAUserData::IsQuest(){
  1600. return false;
  1601. }
  1602. bool LUAUserData::IsZone(){
  1603. return false;
  1604. }
  1605. bool LUAUserData::IsItem(){
  1606. return false;
  1607. }
  1608. bool LUAUserData::IsSkill() {
  1609. return false;
  1610. }
  1611. LUAConversationOptionWrapper::LUAConversationOptionWrapper(){
  1612. correctly_initialized = true;
  1613. }
  1614. bool LUAConversationOptionWrapper::IsConversationOption(){
  1615. return true;
  1616. }
  1617. LUAOptionWindowWrapper::LUAOptionWindowWrapper() {
  1618. correctly_initialized = true;
  1619. }
  1620. bool LUAOptionWindowWrapper::IsOptionWindow() {
  1621. return true;
  1622. }
  1623. LUASpawnWrapper::LUASpawnWrapper(){
  1624. correctly_initialized = true;
  1625. }
  1626. bool LUASpawnWrapper::IsSpawn(){
  1627. return true;
  1628. }
  1629. LUAZoneWrapper::LUAZoneWrapper(){
  1630. correctly_initialized = true;
  1631. }
  1632. bool LUAZoneWrapper::IsZone(){
  1633. return true;
  1634. }
  1635. LUAQuestWrapper::LUAQuestWrapper(){
  1636. correctly_initialized = true;
  1637. }
  1638. bool LUAQuestWrapper::IsQuest(){
  1639. return true;
  1640. }
  1641. LUAItemWrapper::LUAItemWrapper(){
  1642. correctly_initialized = true;
  1643. }
  1644. bool LUAItemWrapper::IsItem(){
  1645. return true;
  1646. }
  1647. LUASkillWrapper::LUASkillWrapper() {
  1648. correctly_initialized = true;
  1649. }
  1650. bool LUASkillWrapper::IsSkill() {
  1651. return true;
  1652. }