SpellProcess.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  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 "SpellProcess.h"
  17. #include "../common/Log.h"
  18. #include "Tradeskills/Tradeskills.h"
  19. #include "ClientPacketFunctions.h"
  20. #include "Rules/Rules.h"
  21. extern MasterSpellList master_spell_list;
  22. extern MasterSkillList master_skill_list;
  23. extern ConfigReader configReader;
  24. extern LuaInterface* lua_interface;
  25. extern Commands commands;
  26. extern World world;
  27. extern RuleManager rule_manager;
  28. SpellProcess::SpellProcess(){
  29. last_checked_time = 0;
  30. MSpellProcess.SetName("SpellProcess::MSpellProcess");
  31. MRemoveTargetList.SetName("SpellProcess::MRemoveTargetList");
  32. MSoloHO.SetName("SpellProcess::m_soloHO");
  33. MGroupHO.SetName("SpellProcess:m_groupHO");
  34. MSpellCancelList.SetName("SpellProcess::SpellCancelList");
  35. }
  36. SpellProcess::~SpellProcess(){
  37. RemoveAllSpells();
  38. }
  39. void SpellProcess::RemoveAllSpells(){
  40. MSpellProcess.lock();
  41. ClearSpellScriptTimerList();
  42. MutexList<LuaSpell*>::iterator active_spells_itr = active_spells.begin();
  43. while(active_spells_itr.Next()){
  44. DeleteCasterSpell(active_spells_itr->value);
  45. }
  46. active_spells_itr = active_spells.begin();
  47. while(active_spells_itr.Next()){
  48. active_spells.Remove(active_spells_itr->value, true);
  49. }
  50. active_spells.clear();
  51. InterruptStruct* interrupt = 0;
  52. MutexList<InterruptStruct*>::iterator interrupt_list_itr = interrupt_list.begin();
  53. while(interrupt_list_itr.Next()){
  54. interrupt = interrupt_list_itr->value;
  55. CheckInterrupt(interrupt);
  56. interrupt_list.Remove(interrupt_list_itr->value, true);
  57. }
  58. interrupt_list.clear();
  59. MutexList<CastTimer*>::iterator cast_timers_itr = cast_timers.begin();
  60. while(cast_timers_itr.Next()){
  61. safe_delete(cast_timers_itr->value->timer);
  62. safe_delete(cast_timers_itr->value->spell);
  63. cast_timers.Remove(cast_timers_itr->value, true);
  64. }
  65. cast_timers.clear();
  66. MutexList<RecastTimer*>::iterator recast_timers_itr = recast_timers.begin();
  67. while(recast_timers_itr.Next()){
  68. recast_timers.Remove(recast_timers_itr->value, true);
  69. }
  70. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  71. remove_target_list.clear();
  72. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  73. map<Client*, HeroicOP*>::iterator solo_ho_itr;
  74. MSoloHO.writelock(__FUNCTION__, __LINE__);
  75. for (solo_ho_itr = m_soloHO.begin(); solo_ho_itr != m_soloHO.end(); solo_ho_itr++)
  76. safe_delete(solo_ho_itr->second);
  77. m_soloHO.clear();
  78. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  79. map<int32, HeroicOP*>::iterator group_ho_itr;
  80. MGroupHO.writelock(__FUNCTION__, __LINE__);
  81. for (group_ho_itr = m_groupHO.begin(); group_ho_itr != m_groupHO.end(); group_ho_itr++)
  82. safe_delete(group_ho_itr->second);
  83. m_groupHO.clear();
  84. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  85. recast_timers.clear();
  86. spell_que.clear();
  87. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  88. SpellCancelList.clear();
  89. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  90. MSpellProcess.unlock();
  91. }
  92. void SpellProcess::Process(){
  93. if(last_checked_time > Timer::GetCurrentTime2())
  94. return;
  95. last_checked_time = Timer::GetCurrentTime2() + 50;
  96. MSpellProcess.lock();
  97. CheckSpellScriptTimers();
  98. if(active_spells.size(true) > 0){
  99. LuaSpell* spell = 0;
  100. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  101. map<LuaSpell*, vector<Spawn*> >::iterator remove_itr;
  102. vector<Spawn*>::iterator target_itr;
  103. vector<Spawn*>::iterator remove_target_itr;
  104. while(itr.Next()){
  105. spell = itr->value;
  106. if (!spell->spell->GetSpellData()->duration_until_cancel && spell->timer.Check()){
  107. spell->num_calls++;
  108. // ProcessSpell(spell, flase) will atempt to call the tick() function in the lua script
  109. // if there is no tick function it will return false, this will cause the server to crash in the event
  110. // of a spell that has a duration but is not a "until canceled" spell or a spell with a tick (tradeskill buffs)
  111. // to counter this check to see if the spell has a call_frequency > 0 before we call ProcessSpell()
  112. if (spell->spell->GetSpellData()->call_frequency > 0 && !ProcessSpell(spell, false))
  113. active_spells.Remove(spell, true, 2000);
  114. else if ((spell->timer.GetDuration() * spell->num_calls) >= spell->spell->GetSpellData()->duration1 * 100)
  115. DeleteCasterSpell(spell);
  116. }
  117. else
  118. CheckRemoveTargetFromSpell(spell);
  119. }
  120. }
  121. if (SpellCancelList.size() > 0){
  122. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  123. vector<LuaSpell*>::iterator itr = SpellCancelList.begin();
  124. while (itr != SpellCancelList.end()){
  125. DeleteCasterSpell(*itr);
  126. itr++;
  127. }
  128. SpellCancelList.clear();
  129. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  130. }
  131. if(interrupt_list.size(true) > 0){
  132. InterruptStruct* interrupt = 0;
  133. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  134. while(itr.Next()){
  135. interrupt = itr->value;
  136. CheckInterrupt(interrupt);
  137. safe_delete(interrupt);
  138. }
  139. interrupt_list.clear();
  140. }
  141. if(cast_timers.size(true) > 0){
  142. CastTimer* cast_timer = 0;
  143. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  144. while (itr.Next()) {
  145. cast_timer = itr->value;
  146. if (cast_timer) {
  147. if (cast_timer->timer->Check(false)) {
  148. if (cast_timer->spell) {
  149. Client* client = cast_timer->zone->GetClientBySpawn(cast_timer->spell->caster);
  150. if (client) {
  151. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  152. if (packet) {
  153. packet->setMediumStringByName("spell_name", cast_timer->spell->spell->GetSpellData()->name.data.c_str());
  154. client->QueuePacket(packet->serialize());
  155. safe_delete(packet);
  156. }
  157. }
  158. if (cast_timer->spell && cast_timer->spell->caster)
  159. cast_timer->spell->caster->IsCasting(false);
  160. cast_timer->delete_timer = true;
  161. CastProcessedSpell(cast_timer->spell);
  162. }
  163. else if (cast_timer->entity_command) {
  164. if (cast_timer->timer->Check(false)) {
  165. cast_timer->delete_timer = true;
  166. Spawn* target = cast_timer->zone->GetSpawnByID(cast_timer->target_id);
  167. CastProcessedEntityCommand(cast_timer->entity_command, cast_timer->caster, target);
  168. }
  169. }
  170. }
  171. if (cast_timer->delete_timer) {
  172. safe_delete(cast_timer->timer);
  173. cast_timers.Remove(cast_timer, true);
  174. }
  175. }
  176. }
  177. }
  178. if (recast_timers.size(true) > 0) {
  179. RecastTimer* recast_timer = 0;
  180. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  181. vector<Spell*>::iterator remove_recast_itr;
  182. while(itr.Next()){
  183. recast_timer = itr->value;
  184. if(recast_timer->timer->Check(false)){
  185. UnlockSpell(recast_timer->client, recast_timer->spell);
  186. safe_delete(recast_timer->timer);
  187. recast_timers.Remove(recast_timer, true);
  188. }
  189. }
  190. }
  191. if(spell_que.size(true) > 0){
  192. MutexMap<Entity*, Spell*>::iterator itr = spell_que.begin();
  193. while(itr.Next()){
  194. if(itr->first->IsCasting() == false && IsReady(itr->second, itr->first)){
  195. RemoveSpellFromQueue(itr->second, itr->first);
  196. ProcessSpell(itr->first->GetZone(), itr->second, itr->first, itr->first->GetTarget());
  197. }
  198. }
  199. }
  200. // Check solo HO timers
  201. MSoloHO.writelock(__FUNCTION__, __LINE__);
  202. if (m_soloHO.size() > 0) {
  203. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  204. map<Client*, HeroicOP*>::iterator delete_itr;
  205. while (itr != m_soloHO.end()) {
  206. if (itr->second->GetWheel() && Timer::GetCurrentTime2() >= (itr->second->GetStartTime() + (itr->second->GetTotalTime() * 1000))) {
  207. itr->second->SetComplete(1);
  208. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  209. safe_delete(itr->second);
  210. delete_itr = itr;
  211. itr++;
  212. m_soloHO.erase(delete_itr);
  213. }
  214. else
  215. itr++;
  216. }
  217. }
  218. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  219. // Check group HO timers
  220. MGroupHO.writelock(__FUNCTION__, __LINE__);
  221. if (m_groupHO.size() > 0) {
  222. map<int32, HeroicOP*>::iterator itr = m_groupHO.begin();
  223. map<int32, HeroicOP*>::iterator delete_itr;
  224. while (itr != m_groupHO.end()) {
  225. if (itr->second->GetWheel() && Timer::GetCurrentTime2() >= (itr->second->GetStartTime() + (itr->second->GetTotalTime() * 1000))) {
  226. itr->second->SetComplete(1);
  227. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  228. deque<GroupMemberInfo*>::iterator itr2;
  229. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(itr->first);
  230. for (itr2 = members->begin(); itr2 != members->end(); itr2++) {
  231. if ((*itr2)->client)
  232. ClientPacketFunctions::SendHeroicOPUpdate((*itr2)->client, itr->second);
  233. }
  234. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  235. safe_delete(itr->second);
  236. delete_itr = itr;
  237. itr++;
  238. m_groupHO.erase(delete_itr);
  239. }
  240. else
  241. itr++;
  242. }
  243. }
  244. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  245. MSpellProcess.unlock();
  246. }
  247. bool SpellProcess::IsReady(Spell* spell, Entity* caster){
  248. if(caster->IsCasting())
  249. return false;
  250. bool ret = true;
  251. RecastTimer* recast_timer = 0;
  252. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  253. while(itr.Next()){
  254. recast_timer = itr->value;
  255. if(recast_timer->spell == spell && recast_timer->caster == caster){
  256. ret = false;
  257. break;
  258. }
  259. }
  260. return ret;
  261. }
  262. void SpellProcess::CheckRecast(Spell* spell, Entity* caster, float timer_override, bool check_linked_timers) {
  263. if(spell && caster && spell->GetSpellData()->recast > 0){
  264. RecastTimer* timer = new RecastTimer;
  265. timer->caster = caster;
  266. if(caster->IsPlayer())
  267. timer->client = caster->GetZone()->GetClientBySpawn(caster);
  268. else
  269. timer->client = 0;
  270. timer->spell = spell;
  271. if(timer_override == 0)
  272. timer->timer = new Timer((int32)(spell->GetSpellData()->recast*1000));
  273. else
  274. timer->timer = new Timer((int32)(timer_override*1000));
  275. recast_timers.Add(timer);
  276. if(caster->IsPlayer()){
  277. if(timer_override == 0)
  278. ((Player*)caster)->LockSpell(spell, (int16)(spell->GetSpellData()->recast * 10));
  279. else
  280. ((Player*)caster)->LockSpell(spell, timer_override * 10);
  281. if (check_linked_timers && spell->GetSpellData()->linked_timer > 0) {
  282. vector<Spell*> linkedSpells = ((Player*)caster)->GetSpellBookSpellsByTimer(spell->GetSpellData()->linked_timer);
  283. for (int8 i = 0; i < linkedSpells.size(); i++) {
  284. Spell* spell2 = linkedSpells.at(i);
  285. if (spell2)
  286. CheckRecast(spell2, caster, timer_override, false);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. void SpellProcess::CheckInterrupt(InterruptStruct* interrupt){
  293. if(!interrupt || !interrupt->interrupted || !interrupt->interrupted->IsEntity())
  294. return;
  295. Entity* entity = (Entity*)interrupt->interrupted;
  296. Client* client = entity->GetZone()->GetClientBySpawn(entity);
  297. if(client)
  298. SendFinishedCast(GetLuaSpell(entity), client);
  299. RemoveSpellTimersFromSpawn(entity, false);
  300. entity->IsCasting(false);
  301. entity->GetZone()->SendInterruptPacket(entity, interrupt->spell);
  302. if(interrupt->error_code > 0)
  303. entity->GetZone()->SendSpellFailedPacket(client, interrupt->error_code);
  304. }
  305. bool SpellProcess::DeleteCasterSpell(Spawn* caster, Spell* spell){
  306. bool ret = false;
  307. // need to use size(true) to get pending updates to the list as well
  308. if (caster && spell && active_spells.size() > 0) {
  309. LuaSpell* lua_spell = 0;
  310. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  311. while (itr.Next()){
  312. lua_spell = itr->value;
  313. if (lua_spell->spell == spell && lua_spell->caster == caster) {
  314. ret = DeleteCasterSpell(lua_spell);
  315. break;
  316. }
  317. }
  318. }
  319. return ret;
  320. }
  321. bool SpellProcess::DeleteCasterSpell(LuaSpell* spell){
  322. bool ret = false;
  323. Spawn* target = 0;
  324. if(spell) {
  325. if (active_spells.count(spell) > 0)
  326. active_spells.Remove(spell);
  327. if (spell->caster) {
  328. if (spell->spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE){
  329. CheckRecast(spell->spell, spell->caster);
  330. if (spell->caster && spell->caster->IsPlayer())
  331. SendSpellBookUpdate(spell->caster->GetZone()->GetClientBySpawn(spell->caster));
  332. }
  333. spell->caster->RemoveMaintainedSpell(spell);
  334. CheckRemoveTargetFromSpell(spell, false);
  335. ZoneServer* zone = spell->caster->GetZone();
  336. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  337. for (int32 i = 0; i < spell->targets.size(); i++) {
  338. target = zone->GetSpawnByID(spell->targets.at(i));
  339. if(target && target->IsEntity()){
  340. ((Entity*)target)->RemoveSpellEffect(spell);
  341. if(spell->spell->GetSpellData()->det_type > 0 && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel))
  342. ((Entity*)target)->RemoveDetrimentalSpell(spell);
  343. }
  344. else{
  345. spell->caster->RemoveSpellEffect(spell);
  346. if(spell->spell->GetSpellData()->det_type > 0 && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel))
  347. spell->caster->RemoveDetrimentalSpell(spell);
  348. }
  349. if(target && target->IsPlayer() && spell->spell->GetSpellData()->fade_message.length() > 0){
  350. Client* client = target->GetZone()->GetClientBySpawn(target);
  351. if(client){
  352. string fade_message = spell->spell->GetSpellData()->fade_message;
  353. if(fade_message.find("%t") != string::npos)
  354. fade_message.replace(fade_message.find("%t"), 2, target->GetName());
  355. client->Message(CHANNEL_COLOR_SPELL_FADE, fade_message.c_str());
  356. }
  357. }
  358. }
  359. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  360. ret = true;
  361. }
  362. if(lua_interface)
  363. lua_interface->RemoveSpell(spell, true, SpellScriptTimersHasSpell(spell));
  364. }
  365. return ret;
  366. }
  367. bool SpellProcess::ProcessSpell(LuaSpell* spell, bool first_cast, const char* function, SpellScriptTimer* timer) {
  368. bool ret = false;
  369. if(lua_interface && !spell->interrupted){
  370. lua_interface->AddSpawnPointers(spell, first_cast, false, function, timer);
  371. vector<LUAData*>* data = spell->spell->GetLUAData();
  372. for(int32 i=0;i<data->size();i++){
  373. switch(data->at(i)->type){
  374. case 0:{
  375. lua_interface->SetSInt32Value(spell->state, data->at(i)->int_value);
  376. break;
  377. }
  378. case 1:{
  379. lua_interface->SetFloatValue(spell->state, data->at(i)->float_value);
  380. break;
  381. }
  382. case 2:{
  383. lua_interface->SetBooleanValue(spell->state, data->at(i)->bool_value);
  384. break;
  385. }
  386. case 3:{
  387. lua_interface->SetStringValue(spell->state, data->at(i)->string_value.c_str());
  388. break;
  389. }
  390. default:{
  391. LogWrite(SPELL__ERROR, 0, "Spell", "Error: Unknown LUA Type '%i' in SpellProcess::ProcessSpell for Spell '%s'", (int)data->at(i)->type, spell->spell->GetName());
  392. return false;
  393. }
  394. }
  395. }
  396. ret = lua_interface->CallSpellProcess(spell, 2 + data->size());
  397. }
  398. return ret;
  399. }
  400. bool SpellProcess::CastPassives(Spell* spell, Entity* caster, bool remove) {
  401. CastInstant(spell, caster, caster, remove, true);
  402. return true;
  403. }
  404. bool SpellProcess::CastInstant(Spell* spell, Entity* caster, Entity* target, bool remove, bool passive) {
  405. LuaSpell* lua_spell = 0;
  406. if(lua_interface)
  407. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  408. if(!lua_spell)
  409. {
  410. string lua_script = spell->GetSpellData()->lua_script;
  411. lua_script.append(".lua");
  412. lua_spell = 0;
  413. if(lua_interface)
  414. lua_spell = lua_interface->GetSpell(lua_script.c_str());
  415. if(!lua_spell) {
  416. LogWrite(SPELL__ERROR, 0, "Spell", "Failed to get a LuaSpell for %s (%u)", spell->GetName(), spell->GetSpellID());
  417. return false;
  418. }
  419. else
  420. spell->GetSpellData()->lua_script = lua_script;
  421. }
  422. lua_spell->caster = caster;
  423. lua_spell->spell = spell;
  424. lua_spell->initial_target = target->GetID();
  425. GetSpellTargets(lua_spell);
  426. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  427. if (!remove)
  428. return CastProcessedSpell(lua_spell, passive);
  429. lua_interface->RemoveSpell(lua_spell, true, SpellScriptTimersHasSpell(lua_spell));
  430. return true;
  431. }
  432. void SpellProcess::SendStartCast(LuaSpell* spell, Client* client){
  433. if(client){
  434. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  435. if(packet){
  436. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01);
  437. packet->setMediumStringByName("spell_name", spell->spell->GetSpellData()->name.data.c_str());
  438. EQ2Packet* outapp = packet->serialize();
  439. //DumpPacket(outapp);
  440. client->QueuePacket(outapp);
  441. safe_delete(packet);
  442. }
  443. }
  444. }
  445. void SpellProcess::SendFinishedCast(LuaSpell* spell, Client* client){
  446. if(client && spell && spell->spell){
  447. UnlockAllSpells(client);
  448. if(spell->resisted && spell->spell->GetSpellData()->recast > 0)
  449. CheckRecast(spell->spell, client->GetPlayer(), 0.5); // half sec recast on resisted spells
  450. else if (!spell->interrupted && spell->spell->GetSpellData()->cast_type != SPELL_CAST_TYPE_TOGGLE)
  451. CheckRecast(spell->spell, client->GetPlayer());
  452. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  453. if(packet){
  454. packet->setMediumStringByName("spell_name", spell->spell->GetSpellData()->name.data.c_str());
  455. client->QueuePacket(packet->serialize());
  456. safe_delete(packet);
  457. }
  458. SendSpellBookUpdate(client);
  459. }
  460. if (spell) {
  461. if (!spell->interrupted) {
  462. TakePower(spell);
  463. TakeHP(spell);
  464. TakeSavagery(spell);
  465. AddDissonance(spell);
  466. AddConcentration(spell);
  467. if (client && spell->spell && !spell->spell->GetSpellData()->friendly_spell)
  468. client->GetPlayer()->InCombat(true);
  469. if(client && spell->caster)
  470. client->CheckPlayerQuestsSpellUpdate(spell->spell);
  471. }
  472. }
  473. }
  474. void SpellProcess::LockAllSpells(Client* client){
  475. if(client){
  476. client->GetPlayer()->LockAllSpells();
  477. SendSpellBookUpdate(client);
  478. }
  479. }
  480. void SpellProcess::UnlockAllSpells(Client* client){
  481. if(client)
  482. client->GetPlayer()->UnlockAllSpells();
  483. }
  484. void SpellProcess::UnlockSpell(Client* client, Spell* spell){
  485. if(client && client->GetPlayer() && spell) {
  486. client->GetPlayer()->UnlockSpell(spell);
  487. SendSpellBookUpdate(client);
  488. }
  489. }
  490. bool SpellProcess::CheckPower(LuaSpell* spell){
  491. int16 req = 0;
  492. if(spell->caster){
  493. req = spell->spell->GetPowerRequired(spell->caster);
  494. if(spell->caster->GetPower() >= req)
  495. return true;
  496. }
  497. return false;
  498. }
  499. bool SpellProcess::TakePower(LuaSpell* spell){
  500. int16 req = 0;
  501. if(spell->caster){
  502. req = spell->spell->GetPowerRequired(spell->caster);
  503. if(spell->caster->GetPower() >= req){
  504. spell->caster->SetPower(spell->caster->GetPower() - req);
  505. if(spell->caster->IsPlayer())
  506. spell->caster->GetZone()->TriggerCharSheetTimer();
  507. return true;
  508. }
  509. }
  510. return false;
  511. }
  512. bool SpellProcess::CheckHP(LuaSpell* spell) {
  513. int16 req = 0;
  514. if(spell->caster){
  515. req = spell->spell->GetHPRequired(spell->caster);
  516. if(spell->caster->GetHP() >= req)
  517. return true;
  518. }
  519. return false;
  520. }
  521. bool SpellProcess::TakeHP(LuaSpell* spell) {
  522. int16 req = 0;
  523. if(spell->caster && spell->caster->IsPlayer()){
  524. req = spell->spell->GetHPRequired(spell->caster);
  525. if(spell->caster->GetHP() >= req){
  526. spell->caster->SetHP(spell->caster->GetHP() - req);
  527. if(spell->caster->IsPlayer())
  528. spell->caster->GetZone()->TriggerCharSheetTimer();
  529. return true;
  530. }
  531. }
  532. return false;
  533. }
  534. bool SpellProcess::CheckConcentration(LuaSpell* spell) {
  535. if (spell && spell->caster) {
  536. int8 req = spell->spell->GetSpellData()->req_concentration / 256;
  537. int8 current_avail = 5 - spell->caster->GetConcentrationCurrent();
  538. if (current_avail >= req)
  539. return true;
  540. }
  541. return false;
  542. }
  543. bool SpellProcess::AddConcentration(LuaSpell* spell) {
  544. if (spell && spell->caster) {
  545. int8 req = spell->spell->GetSpellData()->req_concentration / 256;
  546. int8 current_avail = 5 - spell->caster->GetConcentrationCurrent();
  547. if (current_avail >= req) {
  548. spell->caster->GetInfoStruct()->cur_concentration += req;
  549. if (spell->caster->IsPlayer())
  550. spell->caster->GetZone()->TriggerCharSheetTimer();
  551. LogWrite(SPELL__DEBUG, 0, "Spell", "Concentration is now %u on %s", spell->caster->GetInfoStruct()->cur_concentration, spell->caster->GetName());
  552. return true;
  553. }
  554. }
  555. return false;
  556. }
  557. bool SpellProcess::CheckSavagery(LuaSpell* spell) {
  558. if (spell && spell->caster) {
  559. int16 req = spell->spell->GetSavageryRequired(spell->caster);
  560. if(spell->caster->GetSavagery() >= req)
  561. return true;
  562. }
  563. return false;
  564. }
  565. bool SpellProcess::TakeSavagery(LuaSpell* spell) {
  566. int16 req = 0;
  567. if(spell && spell->caster && spell->caster->IsPlayer()){
  568. req = spell->spell->GetSavageryRequired(spell->caster);
  569. if(spell->caster->GetSavagery() >= req){
  570. spell->caster->SetSavagery(spell->caster->GetSavagery() - req);
  571. if(spell->caster->IsPlayer())
  572. spell->caster->GetZone()->TriggerCharSheetTimer();
  573. return true;
  574. }
  575. }
  576. return false;
  577. }
  578. bool SpellProcess::CheckDissonance(LuaSpell* spell) {
  579. if (spell && spell->caster) {
  580. int16 req = spell->spell->GetDissonanceRequired(spell->caster);
  581. if(spell->caster->GetDissonance() <= req)
  582. return true;
  583. }
  584. return false;
  585. }
  586. bool SpellProcess::AddDissonance(LuaSpell* spell) {
  587. int16 req = 0;
  588. if(spell && spell->caster && spell->caster->IsPlayer()){
  589. req = spell->spell->GetDissonanceRequired(spell->caster);
  590. if(spell->caster->GetDissonance() >= req){
  591. spell->caster->SetDissonance(spell->caster->GetDissonance() - req);
  592. if(spell->caster->IsPlayer())
  593. spell->caster->GetZone()->TriggerCharSheetTimer();
  594. return true;
  595. }
  596. }
  597. return false;
  598. }
  599. void SpellProcess::AddSpellToQueue(Spell* spell, Entity* caster){
  600. if(caster && caster->IsPlayer() && spell){
  601. spell_que.Put(caster, spell);
  602. ((Player*)caster)->QueueSpell(spell);
  603. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  604. if(client)
  605. SendSpellBookUpdate(client);
  606. }
  607. }
  608. void SpellProcess::RemoveSpellFromQueue(Spell* spell, Entity* caster){
  609. if(caster && caster->IsPlayer() && spell){
  610. spell_que.erase(caster);
  611. ((Player*)caster)->UnQueueSpell(spell);
  612. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  613. if(client)
  614. SendSpellBookUpdate(client);
  615. }
  616. }
  617. void SpellProcess::RemoveSpellFromQueue(Entity* caster, bool hostile_only) {
  618. if (caster && spell_que.count(caster) > 0) {
  619. Spell* spell = spell_que.Get(caster);
  620. if (spell) {
  621. bool remove = true;
  622. if (hostile_only && spell->GetSpellData()->target_type != SPELL_TARGET_ENEMY)
  623. remove = false;
  624. if (remove) {
  625. spell_que.erase(caster);
  626. ((Player*)caster)->UnQueueSpell(spell);
  627. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  628. if (client)
  629. SendSpellBookUpdate(client);
  630. }
  631. }
  632. }
  633. }
  634. void SpellProcess::CheckSpellQueue(Entity* caster){
  635. if(caster && caster->IsPlayer()){
  636. if(spell_que.count(caster) > 0)
  637. RemoveSpellFromQueue(spell_que.Get(caster), caster);
  638. }
  639. }
  640. void SpellProcess::CheckSpellQueue(Spell* spell, Entity* caster){
  641. if(caster->IsPlayer()){
  642. bool add = true;
  643. bool remove = false;
  644. if(spell_que.count(caster) > 0){
  645. remove = true;
  646. if(spell_que.Get(caster) == spell)
  647. add = false;
  648. }
  649. if(remove)
  650. RemoveSpellFromQueue(spell, caster);
  651. if(add)
  652. AddSpellToQueue(spell, caster);
  653. }
  654. }
  655. void SpellProcess::SendSpellBookUpdate(Client* client){
  656. if(client){
  657. EQ2Packet* app = client->GetPlayer()->GetSpellBookUpdatePacket(client->GetVersion());
  658. if(app)
  659. client->QueuePacket(app);
  660. }
  661. }
  662. LuaSpell* SpellProcess::GetLuaSpell(Entity* caster){
  663. LuaSpell* spell = 0;
  664. if(caster && cast_timers.size() > 0){
  665. CastTimer* cast_timer = 0;
  666. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  667. while(itr.Next()){
  668. cast_timer = itr->value;
  669. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == caster){
  670. spell = cast_timer->spell;
  671. break;
  672. }
  673. }
  674. }
  675. return spell;
  676. }
  677. Spell* SpellProcess::GetSpell(Entity* caster){
  678. Spell* spell = 0;
  679. if(cast_timers.size() > 0){
  680. CastTimer* cast_timer = 0;
  681. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  682. while(itr.Next()){
  683. cast_timer = itr->value;
  684. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == caster){
  685. spell = cast_timer->spell->spell;
  686. break;
  687. }
  688. }
  689. }
  690. return spell;
  691. }
  692. Spawn* SpellProcess::GetSpellTarget(Entity* caster){
  693. Spawn* target = 0;
  694. if(cast_timers.size() > 0){
  695. CastTimer* cast_timer = 0;
  696. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  697. while(itr.Next()){
  698. cast_timer = itr->value;
  699. if(cast_timer && cast_timer->spell->caster == caster){
  700. target = caster->GetZone()->GetSpawnByID(cast_timer->spell->initial_target);
  701. break;
  702. }
  703. }
  704. }
  705. return target;
  706. }
  707. void SpellProcess::ProcessSpell(ZoneServer* zone, Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell)
  708. {
  709. if(spell && caster)
  710. {
  711. Client* client = 0;
  712. int8 target_type = spell->GetSpellData()->target_type;
  713. //int16 version = 0;
  714. LuaSpell* lua_spell = 0;
  715. if(lua_interface)
  716. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  717. if(!lua_spell)
  718. {
  719. string lua_script = spell->GetSpellData()->lua_script;
  720. lua_script.append(".lua");
  721. if(lua_interface)
  722. lua_spell = lua_interface->GetSpell(lua_script.c_str());
  723. if(!lua_spell)
  724. return;
  725. else
  726. spell->GetSpellData()->lua_script = lua_script;
  727. }
  728. if (!target)
  729. target = caster;
  730. lua_spell->caster = caster;
  731. lua_spell->spell = spell;
  732. int32 target_id = target->GetID();
  733. lua_spell->initial_target = target_id;
  734. if (!harvest_spell)
  735. GetSpellTargets(lua_spell);
  736. else{
  737. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  738. lua_spell->targets.push_back(target_id);
  739. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  740. }
  741. if (target_id == lua_spell->initial_target)
  742. {
  743. LogWrite(SPELL__DEBUG, 1, "Spell", "%s is casting %s on %s.", caster->GetName(), spell->GetName(), ( target ) ? target->GetName() : "No Target" );
  744. }
  745. else
  746. {
  747. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Casting on %s through %s as target.", spell->GetName(), caster->GetZone()->GetSpawnByID(lua_spell->initial_target)->GetName(), target->GetName());
  748. }
  749. target = lua_spell->caster->GetZone()->GetSpawnByID(lua_spell->initial_target);
  750. if(caster->IsPlayer() && zone)
  751. {
  752. client = zone->GetClientBySpawn(caster);
  753. //version = client->GetVersion();
  754. }
  755. //If this spell is the toggle cast type and is being toggled off, do this now
  756. if (spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE)
  757. {
  758. bool ret_val = DeleteCasterSpell(caster, spell);
  759. if (ret_val)
  760. {
  761. int8 actual_concentration = spell->GetSpellData()->req_concentration / 256;
  762. if (actual_concentration > 0)
  763. {
  764. caster->GetInfoStruct()->cur_concentration -= actual_concentration;
  765. if (caster->IsPlayer())
  766. caster->GetZone()->TriggerCharSheetTimer();
  767. }
  768. safe_delete(lua_spell);
  769. return;
  770. }
  771. }
  772. // If a player is casting the spell AND spell is a tradeskill spell
  773. if (client && spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL)
  774. {
  775. // If the player is currently crafting
  776. if (client->IsCrafting())
  777. {
  778. Tradeskill* tradeskill = 0;
  779. zone->GetTradeskillMgr()->ReadLock(__FUNCTION__, __LINE__);
  780. tradeskill = zone->GetTradeskillMgr()->GetTradeskill(client);
  781. // Can not cast a crafting spell that doesn't match the current crafting technique
  782. if (spell->GetSpellData()->mastery_skill != tradeskill->recipe->GetTechnique())
  783. {
  784. // send a message to the client, used chat_relationship to match other tradeskill messages, not sure if it is accurate though
  785. client->Message(CHANNEL_COLOR_CHAT_RELATIONSHIP, "You are not using %s on this recipe.", master_skill_list.GetSkill(spell->GetSpellData()->mastery_skill)->name.data.c_str());
  786. // Write a spell debug message on why we couldn't cast
  787. LogWrite(SPELL__DEBUG, 1, "Spell", "%s could not cast tradeskill spell (%s), skills did not match. spell mastery skill = %u, tradeskill technique = %u", caster->GetName(), spell->GetName(), spell->GetSpellData()->mastery_skill, tradeskill->recipe->GetTechnique());
  788. // make sure to release the lock before we return out
  789. zone->GetTradeskillMgr()->ReleaseReadLock(__FUNCTION__, __LINE__);
  790. safe_delete(lua_spell);
  791. return;
  792. }
  793. // need to make sure the lock is released if the if passed
  794. zone->GetTradeskillMgr()->ReleaseReadLock(__FUNCTION__, __LINE__);
  795. }
  796. else // If the player is not currently crafting
  797. {
  798. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast a tradeskill spell (%s) while not crafting.", caster->GetName(), spell->GetName());
  799. zone->SendSpellFailedPacket(client, SPELL_ERROR_ONLY_WHEN_CRAFTING);
  800. safe_delete(lua_spell);
  801. return;
  802. }
  803. }
  804. if (caster != target && target != NULL && !caster->CheckLoS(target))
  805. {
  806. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot see target %s.", caster->GetName(), target->GetName());
  807. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANT_SEE_TARGET);
  808. safe_delete(lua_spell);
  809. return;
  810. }
  811. if ((caster->IsMezzed() && !spell->CastWhileMezzed()) || (caster->IsStunned() && !spell->CastWhileStunned()))
  812. {
  813. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (mezzed or stunned).", caster->GetName());
  814. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STUNNED);
  815. safe_delete(lua_spell);
  816. return;
  817. }
  818. if (caster->IsStifled() && !spell->CastWhileStifled())
  819. {
  820. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (stifled).", caster->GetName());
  821. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STIFFLED);
  822. safe_delete(lua_spell);
  823. return;
  824. }
  825. if (caster->IsFeared() && !spell->CastWhileFeared())
  826. {
  827. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (feared).", caster->GetName());
  828. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_FEARED);
  829. safe_delete(lua_spell);
  830. return;
  831. }
  832. if(caster->IsPlayer() && !IsReady(spell, caster))
  833. {
  834. LogWrite(SPELL__DEBUG, 1, "Spell", "Queuing spell for %s.", caster->GetName());
  835. CheckSpellQueue(spell, caster);
  836. safe_delete(lua_spell);
  837. return;
  838. }
  839. if (target_type != SPELL_TARGET_SELF && target_type != SPELL_TARGET_GROUP_AE && target_type != SPELL_TARGET_NONE && spell->GetSpellData()->max_aoe_targets == 0)
  840. {
  841. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not Self, Not Group AE, Not None, Max Targets = 0", spell->GetName());
  842. if (!target)
  843. {
  844. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  845. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  846. safe_delete(lua_spell);
  847. return;
  848. }
  849. float tmpRange = spell->GetSpellData()->range;
  850. if (client && target != caster && !spell->GetSpellData()->range)
  851. {
  852. bool match = false;
  853. int tmpTier = 0;
  854. Spell* tmpSpell = 0;
  855. for (tmpTier = 0; tmpTier < spell->GetSpellTier(); tmpTier++)
  856. {
  857. tmpSpell = master_spell_list.GetSpell(spell->GetSpellData()->id, tmpTier);
  858. if (tmpSpell && tmpSpell->GetSpellData()->range)
  859. {
  860. match = true;
  861. break;
  862. }
  863. }
  864. if (tmpSpell)
  865. tmpRange = tmpSpell->GetSpellData()->range;
  866. if (!match)
  867. tmpTier = -1;
  868. char msg[512];
  869. snprintf(msg, 512, "%s: SpellCasted without proper spell range set: %s ID: %i Tier: %i Range obtained from tier %i range %f", caster->GetName(), spell->GetName(), spell->GetSpellID(), spell->GetSpellTier(), tmpTier, tmpRange);
  870. commands.Command_ReportBug(client, new Seperator(msg));
  871. }
  872. if(caster->GetDistance(target) > tmpRange)
  873. {
  874. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too far.", spell->GetName());
  875. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  876. safe_delete(lua_spell);
  877. return;
  878. }
  879. if (caster->GetDistance(target) < spell->GetSpellData()->min_range)
  880. {
  881. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too close.", spell->GetName());
  882. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_CLOSE);
  883. safe_delete(lua_spell);
  884. return;
  885. }
  886. }
  887. if(target_type == SPELL_TARGET_SELF && spell->GetSpellData()->max_aoe_targets == 0)
  888. {
  889. if (harvest_spell)
  890. {
  891. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Harvest, Target Self, Max Targets = 0", spell->GetName());
  892. if (!target || !target->IsGroundSpawn())
  893. {
  894. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target or not groundspawn.", spell->GetName());
  895. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  896. safe_delete(lua_spell);
  897. return;
  898. }
  899. }
  900. else
  901. {
  902. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target '%s' is Caster '%s'.", spell->GetName(), ( target ) ? target->GetName() : "None", caster->GetName());
  903. target = caster;
  904. }
  905. }
  906. // Is enemy spell AND direct-damage (no AE)
  907. if (target_type == SPELL_TARGET_ENEMY && spell->GetSpellData()->max_aoe_targets == 0)
  908. {
  909. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target Enemy (%s) and Max AE Targets = 0.", spell->GetName(), target->GetName());
  910. if (spell->GetSpellData()->friendly_spell && target->IsPlayer() )
  911. {
  912. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Friendly, Target is Player (%s).", spell->GetName(), target->GetName());
  913. if (!target->IsEntity())
  914. {
  915. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  916. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  917. safe_delete(lua_spell);
  918. return;
  919. }
  920. /*if (target->appearance.attackable) {
  921. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  922. return;
  923. }*/
  924. }
  925. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && target->appearance.attackable)
  926. {
  927. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (friendly and NPC)", spell->GetName());
  928. if (target->IsPet())
  929. {
  930. if (((NPC*)target)->GetOwner()->IsNPC())
  931. {
  932. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  933. safe_delete(lua_spell);
  934. return;
  935. }
  936. }
  937. else if (target->IsBot() && (caster->IsPlayer() || caster->IsBot())) {
  938. // Needed so bots or player can cast friendly spells on bots
  939. }
  940. else
  941. {
  942. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  943. safe_delete(lua_spell);
  944. return;
  945. }
  946. }
  947. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && caster->IsNPC()) {
  948. // TODO: faction checks? some other checks to prevent an npc casting a friendly spell on another npc
  949. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: NPC Cast", spell->GetName());
  950. }
  951. else
  952. {
  953. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (catch all)", spell->GetName());
  954. if (!target->IsEntity())
  955. {
  956. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  957. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  958. safe_delete(lua_spell);
  959. return;
  960. }
  961. if (caster == target || (!target->IsPlayer() && !target->appearance.attackable))
  962. {
  963. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not an Enemy (Target: %s).", spell->GetName(), target->GetName());
  964. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  965. safe_delete(lua_spell);
  966. return;
  967. }
  968. if (!target->Alive())
  969. {
  970. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is not alive (Target: %s).", spell->GetName(), target->GetName());
  971. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ALIVE);
  972. safe_delete(lua_spell);
  973. return;
  974. }
  975. if (target->GetInvulnerable())
  976. {
  977. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is invulnerable (Target: %s).", spell->GetName(), target->GetName());
  978. zone->SendSpellFailedPacket(client, SPELL_ERROR_TARGET_INVULNERABLE);
  979. safe_delete(lua_spell);
  980. return;
  981. }
  982. if ((target->IsPlayer() || target->IsBot()) && (caster->IsPlayer() || caster->IsBot()))
  983. {
  984. bool attackAllowed = (Entity*)caster->AttackAllowed((Entity*)target, 0);
  985. if (!attackAllowed)
  986. {
  987. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is player and not attackable.", spell->GetName(), target->GetName());
  988. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  989. safe_delete(lua_spell);
  990. return;
  991. }
  992. }
  993. if (target->IsPet() && ((NPC*)target)->GetOwner() && ((NPC*)target)->GetOwner() == caster) {
  994. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is casters pet and not attackable by caster.", spell->GetName(), target->GetName());
  995. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  996. safe_delete(lua_spell);
  997. return;
  998. }
  999. }
  1000. }
  1001. if (lua_spell->targets.size() == 0 && spell->GetSpellData()->max_aoe_targets == 0)
  1002. {
  1003. LogWrite(SPELL__ERROR, 0, "Spell", "SpellProcess::ProcessSpell Unable to find any spell targets for spell '%s'.", spell->GetName());
  1004. safe_delete(lua_spell);
  1005. return;
  1006. }
  1007. if(target_type == SPELL_TARGET_ENEMY_CORPSE || target_type == SPELL_TARGET_GROUP_CORPSE)
  1008. {
  1009. if(target->Alive())
  1010. {
  1011. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_DEAD);
  1012. safe_delete(lua_spell);
  1013. return;
  1014. }
  1015. if(target->IsPlayer() && zone->GetClientBySpawn(target)->GetCurrentRez()->active){
  1016. zone->SendSpellFailedPacket(client, SPELL_ERROR_ALREADY_CAST);
  1017. safe_delete(lua_spell);
  1018. return;
  1019. }
  1020. }
  1021. if(!CheckPower(lua_spell))
  1022. {
  1023. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_POWER);
  1024. safe_delete(lua_spell);
  1025. return;
  1026. }
  1027. if (!CheckHP(lua_spell))
  1028. {
  1029. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_HEALTH);
  1030. safe_delete(lua_spell);
  1031. return;
  1032. }
  1033. if (!CheckSavagery(lua_spell))
  1034. {
  1035. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_SAVAGERY);
  1036. safe_delete(lua_spell);
  1037. return;
  1038. }
  1039. if (!CheckDissonance(lua_spell))
  1040. {
  1041. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_DISSONANCE);
  1042. safe_delete(lua_spell);
  1043. return;
  1044. }
  1045. if (!CheckConcentration(lua_spell))
  1046. {
  1047. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_CONC);
  1048. safe_delete(lua_spell);
  1049. return;
  1050. }
  1051. // Precast in lua
  1052. if (lua_interface) {
  1053. bool result = false;
  1054. lua_interface->AddSpawnPointers(lua_spell, false, true);
  1055. if (lua_pcall(lua_spell->state, 2, 2, 0) == 0) {
  1056. result = lua_interface->GetBooleanValue(lua_spell->state, 1);
  1057. int8 error = lua_interface->GetInt8Value(lua_spell->state, 2) == 0 ? SPELL_ERROR_CANNOT_PREPARE : lua_interface->GetInt8Value(lua_spell->state, 2);
  1058. lua_interface->ResetFunctionStack(lua_spell->state);
  1059. if (!result) {
  1060. zone->SendSpellFailedPacket(client, error);
  1061. safe_delete(lua_spell);
  1062. return;
  1063. }
  1064. }
  1065. else {
  1066. LogWrite(SPELL__DEBUG, 1, "Spell", "No precast function found for %s", lua_spell->spell->GetName());
  1067. lua_interface->ResetFunctionStack(lua_spell->state);
  1068. }
  1069. }
  1070. else
  1071. LogWrite(SPELL__DEBUG, 1, "Spell", "Unable to do precast check as there was no lua_interface");
  1072. //Apply casting speed mod
  1073. spell->ModifyCastTime(caster);
  1074. LockAllSpells(client);
  1075. //cancel stealth effects on cast
  1076. if(caster->IsStealthed() || caster->IsInvis())
  1077. caster->CancelAllStealth();
  1078. SendStartCast(lua_spell, client);
  1079. if(spell->GetSpellData()->cast_time > 0)
  1080. {
  1081. CastTimer* cast_timer = new CastTimer;
  1082. cast_timer->entity_command = 0;
  1083. cast_timer->target_id = target ? target->GetID() : 0;
  1084. cast_timer->spell = lua_spell;
  1085. cast_timer->spell->caster = caster;
  1086. cast_timer->delete_timer = false;
  1087. cast_timer->timer = new Timer(spell->GetSpellData()->cast_time * 10);
  1088. cast_timer->zone = zone;
  1089. cast_timers.Add(cast_timer);
  1090. if(caster)
  1091. caster->IsCasting(true);
  1092. }
  1093. else
  1094. {
  1095. if(!CastProcessedSpell(lua_spell))
  1096. {
  1097. safe_delete(lua_spell);
  1098. return;
  1099. }
  1100. }
  1101. if(caster)
  1102. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  1103. }
  1104. }
  1105. void SpellProcess::ProcessEntityCommand(ZoneServer* zone, EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  1106. if (zone && entity_command && caster && target && !target->IsPlayer()) {
  1107. Client* client = zone->GetClientBySpawn(caster);
  1108. if (caster->GetDistance(target) > entity_command->distance) {
  1109. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  1110. return;
  1111. }
  1112. if (entity_command->cast_time > 0) {
  1113. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  1114. if (packet) {
  1115. LockAllSpells(client);
  1116. packet->setDataByName("cast_time", entity_command->cast_time * 0.01);
  1117. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1118. EQ2Packet* outapp = packet->serialize();
  1119. client->QueuePacket(outapp);
  1120. safe_delete(packet);
  1121. CastTimer* cast_timer = new CastTimer;
  1122. cast_timer->caster = client;
  1123. cast_timer->target_id = target ? target->GetID() : 0;
  1124. cast_timer->entity_command = entity_command;
  1125. cast_timer->spell = 0;
  1126. cast_timer->delete_timer = false;
  1127. cast_timer->timer = new Timer(entity_command->cast_time * 10);
  1128. cast_timer->zone = zone;
  1129. cast_timers.Add(cast_timer);
  1130. caster->IsCasting(true);
  1131. }
  1132. }
  1133. else if (!CastProcessedEntityCommand(entity_command, client, target))
  1134. return;
  1135. if (entity_command && entity_command->spell_visual > 0)
  1136. caster->GetZone()->SendCastEntityCommandPacket(entity_command, caster->GetID(), target->GetID());
  1137. }
  1138. }
  1139. bool SpellProcess::CastProcessedSpell(LuaSpell* spell, bool passive){
  1140. if(!spell || !spell->caster || !spell->spell || spell->interrupted)
  1141. return false;
  1142. Client* client = 0;
  1143. if(spell->caster && spell->caster->IsPlayer())
  1144. client = spell->caster->GetZone()->GetClientBySpawn(spell->caster);
  1145. if (spell->spell->GetSpellData()->max_aoe_targets > 0 && spell->targets.size() == 0) {
  1146. GetSpellTargetsTrueAOE(spell);
  1147. if (spell->targets.size() == 0) {
  1148. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_NO_TARGETS_IN_RANGE);
  1149. return false;
  1150. }
  1151. }
  1152. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1153. bool processedSpell = false;
  1154. LuaSpell* replace_spell = 0;
  1155. // Check to see if we already casted this spell and it is in the active_spells list
  1156. /*while (itr.Next()) {
  1157. LuaSpell* luaspell = itr.value;
  1158. // Check to see if this is the same spell by comparing caster, spell id, and spell tier
  1159. if (luaspell->caster == spell->caster && luaspell->spell->GetSpellID() == spell->spell->GetSpellID() && luaspell->spell->GetSpellTier() == spell->spell->GetSpellTier() && luaspell->targets.size() == spell->targets.size()) {
  1160. vector<Spawn*>::iterator itr;
  1161. vector<Spawn*>::iterator itr2;
  1162. bool all_match = true;
  1163. // compare the target vector to make sure this is the same spell
  1164. for (itr = luaspell->targets.begin(); itr != luaspell->targets.end(); itr++) {
  1165. bool match = false;
  1166. for (itr2 = spell->targets.begin(); itr2 != spell->targets.end(); itr2++) {
  1167. if ((*itr) == (*itr2)) {
  1168. match = true;
  1169. break;
  1170. }
  1171. }
  1172. if (!match) {
  1173. all_match = false;
  1174. break;
  1175. }
  1176. }
  1177. if (all_match) {
  1178. // set a pointer to replace the spell in the active spell list so we don't have to run this loop again
  1179. replace_spell = luaspell;
  1180. // if friendly skip the calling of the lua function as they have already been called for the previous version of this spell
  1181. if (spell->spell->GetSpellData()->friendly_spell == 1)
  1182. processedSpell = true;
  1183. break;
  1184. }
  1185. }
  1186. }*/
  1187. if (!processedSpell)
  1188. processedSpell = ProcessSpell(spell);
  1189. // Quick hack to prevent a crash on spells that zones the caster (Gate)
  1190. if (!spell->caster)
  1191. return true;
  1192. ZoneServer* zone = spell->caster->GetZone();
  1193. Spawn* target = 0;
  1194. if(processedSpell){
  1195. for (int32 i = 0; i < spell->targets.size(); i++) {
  1196. target = zone->GetSpawnByID(spell->targets[i]);
  1197. if (!target)
  1198. continue;
  1199. if (client && client->IsZoning())
  1200. continue;
  1201. // TODO: Establish actual hate per spell
  1202. if (!spell->spell->GetSpellData()->friendly_spell && target->IsNPC())
  1203. ((NPC*)target)->AddHate((Entity*)spell->caster, 50);
  1204. if(spell->spell->GetSpellData()->success_message.length() > 0){
  1205. if(client){
  1206. string success_message = spell->spell->GetSpellData()->success_message;
  1207. if(success_message.find("%t") != string::npos)
  1208. success_message.replace(success_message.find("%t"), 2, spell->caster->GetName());
  1209. client->Message(CHANNEL_COLOR_SPELL, success_message.c_str());
  1210. }
  1211. }
  1212. if(spell->spell->GetSpellData()->effect_message.length() > 0){
  1213. string effect_message = spell->spell->GetSpellData()->effect_message;
  1214. if(effect_message.find("%t") < 0xFFFFFFFF)
  1215. effect_message.replace(effect_message.find("%t"), 2, target->GetName());
  1216. if (effect_message.find("%c") != string::npos)
  1217. effect_message.replace(effect_message.find("%c"), 2, spell->caster->GetName());
  1218. spell->caster->GetZone()->SimpleMessage(CHANNEL_COLOR_SPELL_EFFECT, effect_message.c_str(), target, 50);
  1219. }
  1220. target->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, spell->caster, spell->spell->GetName());
  1221. }
  1222. }
  1223. else{
  1224. if (!passive)
  1225. SendFinishedCast(spell, client);
  1226. return false;
  1227. }
  1228. if(!spell->resisted && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel)) {
  1229. for (int32 i = 0; i < spell->targets.size(); i++) {
  1230. //LogWrite(SPELL__ERROR, 0, "Spell", "No precast function found for %s", ((Entity*)target)->GetName());
  1231. target = zone->GetSpawnByID(spell->targets.at(i));
  1232. if (i == 0 && !spell->spell->GetSpellData()->not_maintained) {
  1233. spell->caster->AddMaintainedSpell(spell);
  1234. //((Entity*)target)->AddMaintainedSpell(spell);
  1235. LogWrite(SPELL__DEBUG, 0, "Spell", "AddMaintained on %s", ((Entity*)target)->GetName());
  1236. }
  1237. SpellEffects* effect = ((Entity*)target)->GetSpellEffect(spell->spell->GetSpellID());
  1238. if (effect && effect->tier > spell->spell->GetSpellTier()) {
  1239. if(client) {
  1240. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  1241. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "The spell did not take hold as the target already has a better spell of this type.");
  1242. }
  1243. }
  1244. else{
  1245. ((Entity*)target)->AddSpellEffect(spell);
  1246. if(spell->spell->GetSpellData()->det_type > 0)
  1247. ((Entity*)target)->AddDetrimentalSpell(spell);
  1248. }
  1249. }
  1250. if (replace_spell) {
  1251. active_spells.Remove(replace_spell);
  1252. active_spells.Add(spell);
  1253. }
  1254. else {
  1255. active_spells.Add(spell);
  1256. }
  1257. if (spell->num_triggers > 0)
  1258. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->num_triggers, 0);
  1259. if (spell->damage_remaining > 0)
  1260. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->damage_remaining, 1);
  1261. spell->caster->GetZone()->TriggerCharSheetTimer();
  1262. }
  1263. spell->num_calls = 1;
  1264. if(!spell->resisted && spell->spell->GetSpellData()->duration1 > 0){
  1265. spell->timer.Start();
  1266. if(spell->spell->GetSpellData()->call_frequency > 0)
  1267. spell->timer.SetTimer(spell->spell->GetSpellData()->call_frequency*100);
  1268. else
  1269. spell->timer.SetTimer(spell->spell->GetSpellData()->duration1*100);
  1270. if (active_spells.count(spell) > 0) {
  1271. active_spells.Add(spell);
  1272. }
  1273. }
  1274. // if the caster is a player and the spell is a tradeskill spell check for a tradeskill event
  1275. if (client && spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL)
  1276. client->GetCurrentZone()->GetTradeskillMgr()->CheckTradeskillEvent(client, spell->spell->GetSpellData()->icon);
  1277. if (spell->spell->GetSpellData()->friendly_spell && zone->GetSpawnByID(spell->initial_target))
  1278. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, zone->GetSpawnByID(spell->initial_target));
  1279. // Check HO's
  1280. if (client) {
  1281. HeroicOP* ho = nullptr;
  1282. Spell* ho_spell = nullptr;
  1283. int32 ho_target = 0;
  1284. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1285. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1286. map<Client*, HeroicOP*>::iterator soloItr = m_soloHO.find(client);
  1287. if (soloItr != m_soloHO.end()) {
  1288. ho = soloItr->second;
  1289. bool match = false;
  1290. LogWrite(SPELL__ERROR, 0, "HO", "target = %u", ho->GetTarget());
  1291. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1292. for (int8 i = 0; i < spell->targets.size(); i++) {
  1293. LogWrite(SPELL__ERROR, 0, "HO", "%u", spell->targets.at(i));
  1294. if (spell->targets.at(i) == ho->GetTarget()) {
  1295. match = true;
  1296. LogWrite(SPELL__ERROR, 0, "HO", "match found");
  1297. break;
  1298. }
  1299. }
  1300. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1301. if (match && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1302. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  1303. if (ho->GetComplete() > 0) {
  1304. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1305. ho_target = ho->GetTarget();
  1306. if (!ho_spell)
  1307. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1308. safe_delete(ho);
  1309. m_soloHO.erase(soloItr);
  1310. }
  1311. }
  1312. }
  1313. else if (client->GetPlayer()->GetGroupMemberInfo()) {
  1314. map<int32, HeroicOP*>::iterator groupItr = m_groupHO.find(client->GetPlayer()->GetGroupMemberInfo()->group_id);
  1315. if (groupItr != m_groupHO.end()) {
  1316. ho = groupItr->second;
  1317. int32 group_id = client->GetPlayer()->GetGroupMemberInfo()->group_id;
  1318. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1319. if (spell->targets.at(0) == ho->GetTarget() && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1320. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1321. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1322. deque<GroupMemberInfo*>::iterator itr;
  1323. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(group_id);
  1324. for (itr = members->begin(); itr != members->end(); itr++) {
  1325. if ((*itr)->client)
  1326. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  1327. }
  1328. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1329. if (ho->GetComplete() > 0) {
  1330. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1331. ho_target = ho->GetTarget();
  1332. if (!ho_spell)
  1333. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1334. safe_delete(ho);
  1335. m_groupHO.erase(groupItr);
  1336. }
  1337. }
  1338. else
  1339. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1340. }
  1341. }
  1342. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1343. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1344. if (ho_spell && ho_target != 0)
  1345. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(ho_target));
  1346. }
  1347. if (spell->spell->GetSpellData()->friendly_spell == 1 && spell->initial_target)
  1348. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  1349. if (!passive)
  1350. SendFinishedCast(spell, client);
  1351. return true;
  1352. }
  1353. bool SpellProcess::CastProcessedEntityCommand(EntityCommand* entity_command, Client* client, Spawn* target) {
  1354. bool ret = false;
  1355. if (entity_command && client) {
  1356. UnlockAllSpells(client);
  1357. client->GetPlayer()->IsCasting(false);
  1358. if (entity_command->cast_time == 0) {
  1359. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1360. ret = true;;
  1361. }
  1362. if (!ret) {
  1363. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  1364. if (packet) {
  1365. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1366. client->QueuePacket(packet->serialize());
  1367. safe_delete(packet);
  1368. SendSpellBookUpdate(client);
  1369. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1370. ret = true;
  1371. }
  1372. }
  1373. if (ret) {
  1374. EQ2_16BitString command;
  1375. command.data = entity_command->command;
  1376. command.size = entity_command->command.length();
  1377. int32 handler = commands.GetCommandHandler(command.data.c_str());
  1378. if (handler != 0xFFFFFFFF && handler < 999)
  1379. commands.Process(handler, &command, client, target);
  1380. }
  1381. }
  1382. return ret;
  1383. }
  1384. void SpellProcess::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement)
  1385. {
  1386. if(caster)
  1387. {
  1388. LogWrite(SPELL__DEBUG, 0, "Spell", "'%s' is Interrupting spell of '%s'...", interruptor ? interruptor->GetName() : "unknown", caster->GetName());
  1389. LuaSpell* spell = GetLuaSpell(caster);
  1390. if (spell && ((from_movement && !spell->spell->GetSpellData()->cast_while_moving) || (!from_movement && spell->spell->GetSpellData()->interruptable) ||
  1391. cancel))
  1392. {
  1393. Spawn* target = GetSpellTarget(caster);
  1394. InterruptStruct* interrupt = new InterruptStruct;
  1395. interrupt->interrupted = caster;
  1396. interrupt->spell = spell;
  1397. interrupt->target = target;
  1398. interrupt->error_code = error_code;
  1399. spell->interrupted = true;
  1400. interrupt_list.Add(interrupt);
  1401. Client* client = 0;
  1402. if(interruptor && interruptor->IsPlayer())
  1403. {
  1404. client = interruptor->GetZone()->GetClientBySpawn(interruptor);
  1405. client->Message(CHANNEL_COLOR_SPELL_INTERRUPT, "You interrupt %s's ability to cast!", interruptor->GetName());
  1406. }
  1407. }
  1408. }
  1409. }
  1410. void SpellProcess::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast){
  1411. int32 i = 0;
  1412. if(cast_timers.size() > 0){
  1413. CastTimer* cast_timer = 0;
  1414. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  1415. while(itr.Next()){
  1416. cast_timer = itr->value;
  1417. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == spawn){
  1418. cast_timer->spell->caster = 0;
  1419. cast_timer->delete_timer = true;
  1420. }
  1421. }
  1422. }
  1423. if(remove_all){
  1424. LuaSpell* spell = 0;
  1425. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1426. while(itr.Next()){
  1427. spell = itr->value;
  1428. if (!spell)
  1429. continue;
  1430. if (spell->spell->GetSpellData()->persist_though_death)
  1431. continue;
  1432. if(spell->caster == spawn){
  1433. DeleteCasterSpell(spell);
  1434. continue;
  1435. }
  1436. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1437. for (i = 0; i < spell->targets.size(); i++){
  1438. if (spawn->GetID() == spell->targets.at(i)){
  1439. if (spawn->IsEntity())
  1440. ((Entity*)spawn)->RemoveSpellEffect(spell);
  1441. RemoveTargetFromSpell(spell, spawn);
  1442. break;
  1443. }
  1444. }
  1445. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1446. }
  1447. if(recast_timers.size() > 0 && delete_recast){
  1448. RecastTimer* recast_timer = 0;
  1449. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  1450. while(itr.Next()){
  1451. recast_timer = itr->value;
  1452. if(recast_timer && recast_timer->caster == spawn){
  1453. safe_delete(recast_timer->timer);
  1454. recast_timers.Remove(recast_timer, true);
  1455. }
  1456. }
  1457. }
  1458. if(spell_que.size() > 0 && spawn->IsEntity()){
  1459. spell_que.erase((Entity*)spawn);
  1460. }
  1461. if(interrupt_list.size() > 0){
  1462. InterruptStruct* interrupt = 0;
  1463. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  1464. while(itr.Next()){
  1465. interrupt = itr->value;
  1466. if(interrupt && interrupt->interrupted == spawn){
  1467. interrupt_list.Remove(interrupt);
  1468. }
  1469. }
  1470. }
  1471. }
  1472. }
  1473. void SpellProcess::GetSpellTargets(LuaSpell* luaspell)
  1474. {
  1475. if (luaspell && luaspell->spell && luaspell->caster && luaspell->spell->GetSpellData()->max_aoe_targets == 0)
  1476. {
  1477. int8 target_type = luaspell->spell->GetSpellData()->target_type;
  1478. Spawn* caster = luaspell->caster;
  1479. Spawn* target = caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  1480. SpellData* data = luaspell->spell->GetSpellData();
  1481. bool implied = false;
  1482. Spawn* secondary_target = nullptr;
  1483. //implied target check -- only use this for players
  1484. if (target && (target_type == SPELL_TARGET_ENEMY || target_type == SPELL_TARGET_ENEMY_CORPSE || target_type == SPELL_TARGET_GROUP_CORPSE || target_type == SPELL_TARGET_OTHER_GROUP_AE))
  1485. {
  1486. if (caster->IsPlayer() && target->HasTarget())
  1487. {
  1488. secondary_target = target->GetTarget();
  1489. if (secondary_target) {
  1490. // check if spell is friendly
  1491. if (data->friendly_spell) {
  1492. //if target is NPC (and not a bot) on friendly spell, check to see if target is friendly
  1493. if (target->IsNPC() && !target->IsBot()) {
  1494. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1495. if (secondary_target->IsPlayer()) {
  1496. implied = true;
  1497. }
  1498. else if (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsPlayer())
  1499. implied = true;
  1500. }
  1501. }
  1502. } // if spell is not friendly
  1503. else { // check if there is an implied target for this non-friendly spell
  1504. if (target->IsPlayer() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsPlayer())) {
  1505. if (secondary_target->IsNPC()) {
  1506. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1507. implied = true;
  1508. }
  1509. }
  1510. }
  1511. } // end friendly spell check
  1512. }
  1513. }
  1514. else if (caster->IsPlayer()) {
  1515. if (data->friendly_spell) {
  1516. if (target->IsNPC() && !target->IsBot()) {
  1517. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1518. target = caster;
  1519. luaspell->initial_target = caster->GetID();
  1520. }
  1521. }
  1522. }
  1523. }
  1524. }
  1525. else if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE) {
  1526. target = caster;
  1527. luaspell->initial_target = caster->GetID();
  1528. }
  1529. else if (target_type == SPELL_TARGET_SELF){
  1530. target = caster;
  1531. luaspell->initial_target = caster->GetID();
  1532. }
  1533. //if using implied target, target = the implied target
  1534. if (implied)
  1535. {
  1536. target = secondary_target;
  1537. luaspell->initial_target = secondary_target->GetID();
  1538. }
  1539. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1540. // Group AE type NOTE: Add support for RAID AE to affect raid members once raids have been completed
  1541. if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE)
  1542. {
  1543. // is friendly
  1544. if (data->friendly_spell)
  1545. {
  1546. // caster is an Entity
  1547. if (luaspell->caster->IsEntity())
  1548. {
  1549. // entity caster is in a player group
  1550. if (((Entity*)caster)->GetGroupMemberInfo())
  1551. {
  1552. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1553. // get group members
  1554. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  1555. deque<GroupMemberInfo*>::iterator itr;
  1556. // iterate through list of group members
  1557. for (itr = members->begin(); itr != members->end(); itr++)
  1558. {
  1559. // get group member player info
  1560. Entity* group_member = (*itr)->member;
  1561. // if the group member is in the casters zone, and is alive
  1562. if (group_member->GetZone() == luaspell->caster->GetZone() && group_member->Alive()){
  1563. luaspell->targets.push_back(group_member->GetID());
  1564. if (group_member->HasPet()){
  1565. Entity* pet = group_member->GetPet();
  1566. if (!pet)
  1567. pet = group_member->GetCharmedPet();
  1568. if (pet)
  1569. luaspell->targets.push_back(pet->GetID());
  1570. }
  1571. }
  1572. }
  1573. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1574. }
  1575. else
  1576. luaspell->targets.push_back(caster->GetID()); // else caster is not in a group, thus alone
  1577. }
  1578. else if (caster->IsNPC()) // caster is NOT a player
  1579. {
  1580. // caster is NPC and in a spawn group with other NPCs
  1581. vector<Spawn*>* group = ((NPC*)caster)->GetSpawnGroup();
  1582. if (group)
  1583. {
  1584. vector<Spawn*>::iterator itr;
  1585. for (itr = group->begin(); itr != group->end(); itr++)
  1586. {
  1587. Spawn* group_member = *itr;
  1588. if (group_member->IsNPC() && group_member->Alive()){
  1589. luaspell->targets.push_back(group_member->GetID());
  1590. if (((Entity*)group_member)->HasPet()){
  1591. Entity* pet = ((Entity*)group_member)->GetPet();
  1592. if (pet)
  1593. luaspell->targets.push_back(pet->GetID());
  1594. pet = ((Entity*)group_member)->GetCharmedPet();
  1595. if (pet)
  1596. luaspell->targets.push_back(pet->GetID());
  1597. }
  1598. }
  1599. }
  1600. }
  1601. else
  1602. luaspell->targets.push_back(caster->GetID());
  1603. safe_delete(group);
  1604. } // end is player
  1605. } // end is friendly
  1606. } // end is Group AE
  1607. else if (target_type == SPELL_TARGET_SELF && caster)
  1608. luaspell->targets.push_back(caster->GetID()); // if spell is SELF, return caster
  1609. else if (target_type == SPELL_TARGET_CASTER_PET && caster && caster->IsEntity() && ((Entity*)caster)->HasPet()) {
  1610. if (((Entity*)caster)->GetPet())
  1611. luaspell->targets.push_back(((Entity*)caster)->GetPet()->GetID());
  1612. if (((Entity*)caster)->GetCharmedPet())
  1613. luaspell->targets.push_back(((Entity*)caster)->GetCharmedPet()->GetID());
  1614. }
  1615. else if (target_type == SPELL_TARGET_ENEMY && target && target->Alive()) // if target is enemy, and is alive
  1616. {
  1617. // if friendly spell
  1618. if (data->friendly_spell > 0)
  1619. {
  1620. // if caster is a player
  1621. if (caster->IsPlayer())
  1622. {
  1623. // if spell can affect raid, only group members or is a group spell
  1624. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  1625. {
  1626. // if caster is in a group, and target is a player and targeted player is a group member
  1627. if (((Player*)caster)->GetGroupMemberInfo() && (target->IsPlayer() || target->IsBot()) && ((Player*)caster)->IsGroupMember((Entity*)target))
  1628. luaspell->targets.push_back(target->GetID()); // return the target
  1629. else
  1630. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1631. }
  1632. else if (target->IsPlayer() || target->IsBot()) // else it is not raid, group only or group spell
  1633. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1634. else
  1635. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1636. }
  1637. else if (caster->IsNPC()) // caster is an NPC
  1638. {
  1639. // if NPC's spell can affect raid, only group members or is a group spell
  1640. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  1641. {
  1642. if (caster->IsBot() && (target->IsBot() || target->IsPlayer())) {
  1643. GroupMemberInfo* gmi = ((Entity*)caster)->GetGroupMemberInfo();
  1644. if (gmi && target->IsEntity() && world.GetGroupManager()->IsInGroup(gmi->group_id, (Entity*)target)) {
  1645. luaspell->targets.push_back(target->GetID()); // return the target
  1646. }
  1647. else
  1648. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1649. }
  1650. // if NPC caster is in a group, and target is a player and targeted player is a group member
  1651. else if (((NPC*)caster)->HasSpawnGroup() && target->IsNPC() && ((NPC*)caster)->IsInSpawnGroup((NPC*)target))
  1652. luaspell->targets.push_back(target->GetID()); // return the target
  1653. else
  1654. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1655. }
  1656. else if (target->IsNPC())
  1657. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1658. else {
  1659. if (caster->IsBot() && (target->IsBot() || target->IsPlayer()))
  1660. luaspell->targets.push_back(target->GetID());
  1661. else
  1662. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1663. }
  1664. } // end is player
  1665. } // end is friendly
  1666. else if (data->group_spell > 0 || data->icon_backdrop == 312) // is not friendly, but is a group spell, icon_backdrop 312 is green (encounter AE)
  1667. {
  1668. // target is non-player
  1669. if (target->IsNPC())
  1670. {
  1671. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  1672. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  1673. if (group)
  1674. {
  1675. vector<Spawn*>::iterator itr;
  1676. // iterate through spawn group members
  1677. for (itr = group->begin(); itr != group->end(); itr++)
  1678. {
  1679. Spawn* group_member = *itr;
  1680. // if NPC group member is (still) an NPC (wtf?) and is alive, send the NPC group member back as a successful target of non-friendly spell
  1681. if (group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune())
  1682. luaspell->targets.push_back(group_member->GetID());
  1683. // note: this should generate some hate towards the caster
  1684. }
  1685. } // end is spawngroup
  1686. else
  1687. luaspell->targets.push_back(target->GetID()); // return single target NPC for non-friendly spell
  1688. safe_delete(group);
  1689. } // end is NPC
  1690. else if (target->IsPlayer() && caster->IsNPC()) // the NPC is casting on a player
  1691. {
  1692. // player is in a group
  1693. if (((Player*)target)->GetGroupMemberInfo())
  1694. {
  1695. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1696. deque<GroupMemberInfo*>::iterator itr;
  1697. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1698. // iterate through players group members
  1699. for (itr = members->begin(); itr != members->end(); itr++)
  1700. {
  1701. Entity* group_member = (*itr)->member;
  1702. // if the group member is in the same zone as caster, and group member is alive, and group member is within distance
  1703. if (group_member->GetZone() == caster->GetZone() && group_member->Alive() && caster->GetDistance(group_member) <= data->range
  1704. && (group_member == target || !group_member->IsAOEImmune()))
  1705. luaspell->targets.push_back(group_member->GetID()); // add as target
  1706. }
  1707. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1708. }
  1709. else
  1710. luaspell->targets.push_back(target->GetID()); // player not in group
  1711. } // end is caster player or npc
  1712. }
  1713. else
  1714. luaspell->targets.push_back(target->GetID()); // is not friendly nor a group spell
  1715. }
  1716. //Rez spells
  1717. else if(target_type == SPELL_TARGET_ENEMY_CORPSE){
  1718. //is friendly
  1719. if(data->friendly_spell){
  1720. //target is player
  1721. if(target->IsPlayer()){
  1722. luaspell->targets.push_back(target->GetID());
  1723. }
  1724. }
  1725. }
  1726. else if(target_type == SPELL_TARGET_GROUP_CORPSE){
  1727. //is friendly
  1728. if(data->friendly_spell){
  1729. //target is player
  1730. if(target && target->IsPlayer()){
  1731. //if target has group
  1732. if(((Player*)target)->GetGroupMemberInfo()) {
  1733. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1734. deque<GroupMemberInfo*>::iterator itr;
  1735. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1736. Entity* group_member = 0;
  1737. for(itr = members->begin(); itr != members->end(); itr++) {
  1738. group_member = (*itr)->member;
  1739. //Check if group member is in the same zone in range of the spell and dead
  1740. if(group_member->GetZone() == target->GetZone() && !group_member->Alive() && target->GetDistance(group_member) <= data->radius){
  1741. luaspell->targets.push_back(group_member->GetID());
  1742. }
  1743. }
  1744. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1745. }
  1746. else
  1747. luaspell->targets.push_back(target->GetID());
  1748. }
  1749. }
  1750. }
  1751. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1752. }
  1753. if (luaspell && luaspell->targets.size() > 20)
  1754. LogWrite(SPELL__WARNING, 0, "Spell", "Warning in %s: Size of targets array is %u", __FUNCTION__, luaspell->targets.size());
  1755. }
  1756. void SpellProcess::GetSpellTargetsTrueAOE(LuaSpell* luaspell) {
  1757. if (luaspell && luaspell->caster && luaspell->spell && luaspell->spell->GetSpellData()->max_aoe_targets > 0) {
  1758. if (luaspell->caster->HasTarget() && luaspell->caster->GetTarget() != luaspell->caster){
  1759. //Check if the caster has an implied target
  1760. if (luaspell->caster->GetDistance(luaspell->caster->GetTarget()) <= luaspell->spell->GetSpellData()->radius)
  1761. luaspell->initial_target = luaspell->caster->GetTarget()->GetID();
  1762. }
  1763. int32 ignore_target = 0;
  1764. vector<Spawn*> spawns = luaspell->caster->GetZone()->GetAttackableSpawnsByDistance(luaspell->caster, luaspell->spell->GetSpellData()->radius);
  1765. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1766. for (int8 i = 0; i < spawns.size(); i++) {
  1767. Spawn* spawn = spawns.at(i);
  1768. if (i == 0){
  1769. if (luaspell->initial_target && luaspell->caster->GetID() != luaspell->initial_target){
  1770. //this is the "Direct" target and aoe can't be avoided
  1771. luaspell->targets.push_back(luaspell->initial_target);
  1772. ignore_target = luaspell->initial_target;
  1773. }
  1774. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1775. break;
  1776. }
  1777. //If we have already added this spawn, check the next spawn in the list
  1778. if (spawn && spawn->GetID() == ignore_target){
  1779. i++;
  1780. if (i < spawns.size())
  1781. spawn = spawns.at(i);
  1782. else
  1783. break;
  1784. }
  1785. if (spawn){
  1786. //If this spawn is immune to aoe, continue
  1787. if (((Entity*)spawn)->IsAOEImmune())
  1788. continue;
  1789. luaspell->targets.push_back(spawn->GetID());
  1790. }
  1791. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1792. break;
  1793. }
  1794. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1795. }
  1796. if (luaspell->targets.size() > 20)
  1797. LogWrite(SPELL__DEBUG, 0, "Spell", "Warning in SpellProcess::GetSpellTargetsTrueAOE Size of targets array is %u", luaspell->targets.size());
  1798. }
  1799. void SpellProcess::AddSpellScriptTimer(SpellScriptTimer* timer) {
  1800. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1801. m_spellScriptList.push_back(timer);
  1802. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1803. }
  1804. void SpellProcess::RemoveSpellScriptTimer(SpellScriptTimer* timer) {
  1805. vector<SpellScriptTimer*>::iterator itr;
  1806. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1807. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1808. if ((*itr) == timer) {
  1809. if ((*itr) && (*itr)->deleteWhenDone && lua_interface) {
  1810. lua_interface->AddPendingSpellDelete(timer->spell);
  1811. }
  1812. m_spellScriptList.erase(itr);
  1813. break;
  1814. }
  1815. }
  1816. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1817. }
  1818. void SpellProcess::CheckSpellScriptTimers() {
  1819. vector<SpellScriptTimer*>::iterator itr;
  1820. vector<SpellScriptTimer*> temp_list;
  1821. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1822. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1823. if (Timer::GetCurrentTime2() >= (*itr)->time) {
  1824. temp_list.push_back((*itr));
  1825. ProcessSpell((*itr)->spell, false, (*itr)->customFunction.c_str(), (*itr));
  1826. }
  1827. }
  1828. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1829. for (itr = temp_list.begin(); itr != temp_list.end(); itr++) {
  1830. RemoveSpellScriptTimer(*itr);
  1831. safe_delete(*itr);
  1832. }
  1833. }
  1834. bool SpellProcess::SpellScriptTimersHasSpell(LuaSpell* spell) {
  1835. bool ret = false;
  1836. vector<SpellScriptTimer*>::iterator itr;
  1837. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1838. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1839. SpellScriptTimer* timer = *itr;
  1840. if (timer && timer->spell == spell) {
  1841. ret = true;
  1842. break;
  1843. }
  1844. }
  1845. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1846. return ret;
  1847. }
  1848. void SpellProcess::ClearSpellScriptTimerList() {
  1849. vector<SpellScriptTimer*>::iterator itr;
  1850. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1851. for(itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1852. if ((*itr) && (*itr)->deleteWhenDone && lua_interface)
  1853. lua_interface->AddPendingSpellDelete((*itr)->spell);
  1854. safe_delete((*itr));
  1855. }
  1856. m_spellScriptList.clear();
  1857. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1858. }
  1859. void SpellProcess::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target){
  1860. if (!spell || !target)
  1861. return;
  1862. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1863. if (!remove_target_list[spell])
  1864. remove_target_list[spell] = new vector<Spawn*>;
  1865. remove_target_list[spell]->push_back(target);
  1866. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1867. }
  1868. void SpellProcess::CheckRemoveTargetFromSpell(LuaSpell* spell, bool allow_delete){
  1869. if (!spell)
  1870. return;
  1871. if (remove_target_list.size() > 0){
  1872. map<LuaSpell*, vector<Spawn*>*>::iterator remove_itr;
  1873. vector<Spawn*>::iterator remove_target_itr;
  1874. vector<int32>::iterator target_itr;
  1875. vector<int32>* targets;
  1876. vector<Spawn*>* remove_targets = 0;
  1877. Spawn* remove_spawn = 0;
  1878. bool should_delete = false;
  1879. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1880. for (remove_itr = remove_target_list.begin(); remove_itr != remove_target_list.end(); remove_itr++){
  1881. if (remove_itr->first == spell){
  1882. targets = &spell->targets;
  1883. remove_targets = remove_itr->second;
  1884. if (remove_targets && targets){
  1885. for (remove_target_itr = remove_targets->begin(); remove_target_itr != remove_targets->end(); remove_target_itr++){
  1886. remove_spawn = (*remove_target_itr);
  1887. if (remove_spawn){
  1888. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1889. for (target_itr = targets->begin(); target_itr != targets->end(); target_itr++){
  1890. if (remove_spawn->GetID() == (*target_itr)){
  1891. targets->erase(target_itr);
  1892. if (remove_spawn->IsEntity())
  1893. ((Entity*)remove_spawn)->RemoveEffectsFromLuaSpell(spell);
  1894. break;
  1895. }
  1896. }
  1897. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1898. if (targets->size() == 0 && allow_delete){
  1899. should_delete = true;
  1900. break;
  1901. }
  1902. }
  1903. }
  1904. }
  1905. break;
  1906. }
  1907. }
  1908. remove_target_list.erase(spell);
  1909. if (remove_targets)
  1910. remove_targets->clear();
  1911. safe_delete(remove_targets);
  1912. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1913. if (should_delete)
  1914. DeleteCasterSpell(spell);
  1915. }
  1916. }
  1917. bool SpellProcess::AddHO(Client* client, HeroicOP* ho) {
  1918. bool ret = true;
  1919. if (client && ho) {
  1920. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1921. if (m_soloHO.count(client) > 0) {
  1922. if (m_soloHO[client]->GetWheel()) {
  1923. ret = false;
  1924. }
  1925. else {
  1926. safe_delete(m_soloHO[client]);
  1927. m_soloHO[client] = ho;
  1928. }
  1929. }
  1930. else
  1931. m_soloHO[client] = ho;
  1932. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1933. }
  1934. return ret;
  1935. }
  1936. bool SpellProcess::AddHO(int32 group_id, HeroicOP* ho) {
  1937. bool ret = true;
  1938. if (group_id > 0 && ho) {
  1939. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1940. if (m_groupHO.count(group_id) > 0) {
  1941. if (m_groupHO[group_id]->GetWheel()) {
  1942. ret = false;
  1943. }
  1944. else {
  1945. safe_delete(m_groupHO[group_id]);
  1946. m_groupHO[group_id] = ho;
  1947. }
  1948. }
  1949. else
  1950. m_groupHO[group_id] = ho;
  1951. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1952. }
  1953. return ret;
  1954. }
  1955. void SpellProcess::KillHOBySpawnID(int32 spawn_id) {
  1956. // Check solo HO's
  1957. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1958. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  1959. map<Client*, HeroicOP*>::iterator delete_itr;
  1960. while (itr != m_soloHO.end()) {
  1961. if (itr->second->GetTarget() == spawn_id) {
  1962. itr->second->SetComplete(1);
  1963. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  1964. delete_itr = itr;
  1965. safe_delete(itr->second);
  1966. itr++;
  1967. m_soloHO.erase(delete_itr);
  1968. }
  1969. else
  1970. itr++;
  1971. }
  1972. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1973. // Check Group HO's
  1974. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1975. map<int32, HeroicOP*>::iterator itr2 = m_groupHO.begin();
  1976. map<int32, HeroicOP*>::iterator delete_itr2;
  1977. while (itr2 != m_groupHO.end()) {
  1978. if (itr2->second->GetTarget() == spawn_id) {
  1979. itr2->second->SetComplete(1);
  1980. world.GetGroupManager()->GroupLock(__FUNCTION__ , __LINE__);
  1981. deque<GroupMemberInfo*>::iterator itr3;
  1982. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(itr2->first);
  1983. for (itr3 = members->begin(); itr3 != members->end(); itr3++) {
  1984. if ((*itr3)->client)
  1985. ClientPacketFunctions::SendHeroicOPUpdate((*itr3)->client, itr2->second);
  1986. }
  1987. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1988. delete_itr2 = itr2;
  1989. safe_delete(itr2->second);
  1990. itr2++;
  1991. m_groupHO.erase(delete_itr2);
  1992. }
  1993. else
  1994. itr2++;
  1995. }
  1996. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1997. }
  1998. void SpellProcess::AddSpellCancel(LuaSpell* spell){
  1999. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  2000. SpellCancelList.push_back(spell);
  2001. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  2002. }