SpellProcess.cpp 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  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, "", true);
  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. (spell->restored && (spell->timer.GetSetAtTrigger() * spell->num_calls) >= spell->spell->GetSpellData()->duration1 * 100))
  116. DeleteCasterSpell(spell, "expired");
  117. }
  118. else
  119. CheckRemoveTargetFromSpell(spell);
  120. }
  121. }
  122. if (SpellCancelList.size() > 0){
  123. std::vector<LuaSpell*> tmpList;
  124. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  125. vector<LuaSpell*>::iterator itr = SpellCancelList.begin();
  126. while (itr != SpellCancelList.end()){
  127. tmpList.push_back(*itr);
  128. itr++;
  129. }
  130. SpellCancelList.clear();
  131. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  132. itr = tmpList.begin();
  133. while (itr != tmpList.end()) {
  134. DeleteCasterSpell(*itr, "canceled");
  135. itr++;
  136. }
  137. }
  138. if(interrupt_list.size(true) > 0){
  139. InterruptStruct* interrupt = 0;
  140. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  141. while(itr.Next()){
  142. interrupt = itr->value;
  143. CheckInterrupt(interrupt);
  144. safe_delete(interrupt);
  145. }
  146. interrupt_list.clear();
  147. }
  148. if(cast_timers.size(true) > 0){
  149. CastTimer* cast_timer = 0;
  150. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  151. while (itr.Next()) {
  152. cast_timer = itr->value;
  153. if (cast_timer) {
  154. if (cast_timer->timer->Check(false)) {
  155. if (cast_timer->spell) {
  156. Client* client = cast_timer->zone->GetClientBySpawn(cast_timer->spell->caster);
  157. if (client) {
  158. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  159. if (packet) {
  160. packet->setMediumStringByName("spell_name", cast_timer->spell->spell->GetSpellData()->name.data.c_str());
  161. client->QueuePacket(packet->serialize());
  162. safe_delete(packet);
  163. }
  164. }
  165. if (cast_timer->spell && cast_timer->spell->caster)
  166. cast_timer->spell->caster->IsCasting(false);
  167. cast_timer->delete_timer = true;
  168. CastProcessedSpell(cast_timer->spell, false, cast_timer->in_heroic_opp);
  169. }
  170. else if (cast_timer->entity_command) {
  171. if (cast_timer->timer->Check(false)) {
  172. cast_timer->delete_timer = true;
  173. Spawn* target = cast_timer->zone->GetSpawnByID(cast_timer->target_id);
  174. CastProcessedEntityCommand(cast_timer->entity_command, cast_timer->caster, target, cast_timer->in_heroic_opp);
  175. }
  176. }
  177. }
  178. if (cast_timer->delete_timer) {
  179. safe_delete(cast_timer->timer);
  180. cast_timers.Remove(cast_timer, true);
  181. }
  182. }
  183. }
  184. }
  185. if (recast_timers.size(true) > 0) {
  186. RecastTimer* recast_timer = 0;
  187. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  188. vector<Spell*>::iterator remove_recast_itr;
  189. while(itr.Next()){
  190. recast_timer = itr->value;
  191. if(recast_timer->timer->Check(false)){
  192. MaintainedEffects* effect = 0;
  193. if(recast_timer->caster && (!(effect = recast_timer->caster->GetMaintainedSpell(recast_timer->spell_id)) || !effect->spell->spell->GetSpellData()->duration_until_cancel))
  194. UnlockSpell(recast_timer->client, recast_timer->spell);
  195. safe_delete(recast_timer->timer);
  196. recast_timers.Remove(recast_timer, true);
  197. }
  198. }
  199. }
  200. if(spell_que.size(true) > 0){
  201. MutexMap<Entity*, Spell*>::iterator itr = spell_que.begin();
  202. while(itr.Next()){
  203. if(itr->first->IsCasting() == false && IsReady(itr->second, itr->first)){
  204. RemoveSpellFromQueue(itr->second, itr->first);
  205. ProcessSpell(itr->first->GetZone(), itr->second, itr->first, itr->first->GetTarget());
  206. }
  207. }
  208. }
  209. // Check solo HO timers
  210. MSoloHO.writelock(__FUNCTION__, __LINE__);
  211. if (m_soloHO.size() > 0) {
  212. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  213. while (itr != m_soloHO.end()) {
  214. if (itr->second->GetWheel() && Timer::GetCurrentTime2() >= (itr->second->GetStartTime() + (itr->second->GetTotalTime() * 1000))) {
  215. itr->second->SetComplete(1);
  216. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  217. safe_delete(itr->second);
  218. itr = m_soloHO.erase(itr);
  219. continue;
  220. }
  221. else
  222. itr++;
  223. }
  224. }
  225. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  226. // Check group HO timers
  227. MGroupHO.writelock(__FUNCTION__, __LINE__);
  228. if (m_groupHO.size() > 0) {
  229. map<int32, HeroicOP*>::iterator itr = m_groupHO.begin();
  230. while (itr != m_groupHO.end()) {
  231. if (itr->second->GetWheel() && Timer::GetCurrentTime2() >= (itr->second->GetStartTime() + (itr->second->GetTotalTime() * 1000))) {
  232. itr->second->SetComplete(1);
  233. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  234. deque<GroupMemberInfo*>::iterator itr2;
  235. PlayerGroup* group = world.GetGroupManager()->GetGroup(itr->first);
  236. if (group)
  237. {
  238. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  239. deque<GroupMemberInfo*>* members = group->GetMembers();
  240. for (itr2 = members->begin(); itr2 != members->end(); itr2++) {
  241. if ((*itr2)->client)
  242. ClientPacketFunctions::SendHeroicOPUpdate((*itr2)->client, itr->second);
  243. }
  244. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  245. }
  246. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  247. safe_delete(itr->second);
  248. itr = m_groupHO.erase(itr);
  249. continue;
  250. }
  251. else
  252. itr++;
  253. }
  254. }
  255. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  256. MSpellProcess.unlock();
  257. }
  258. bool SpellProcess::IsReady(Spell* spell, Entity* caster){
  259. if(caster->IsCasting()) {
  260. return false;
  261. }
  262. bool ret = true;
  263. RecastTimer* recast_timer = 0;
  264. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  265. while(itr.Next()){
  266. recast_timer = itr->value;
  267. if((recast_timer->spell == spell || recast_timer->spell_id == spell->GetSpellID()) &&
  268. recast_timer->caster == caster){
  269. ret = false;
  270. break;
  271. }
  272. }
  273. return ret;
  274. }
  275. void SpellProcess::CheckRecast(Spell* spell, Entity* caster, float timer_override, bool check_linked_timers) {
  276. if(spell && caster && spell->GetSpellData()->recast > 0){
  277. RecastTimer* timer = new RecastTimer;
  278. timer->caster = caster;
  279. if(caster->IsPlayer())
  280. timer->client = caster->GetZone()->GetClientBySpawn(caster);
  281. else
  282. timer->client = 0;
  283. timer->spell = spell;
  284. if(timer_override == 0)
  285. timer->timer = new Timer((int32)(spell->GetSpellData()->recast*1000));
  286. else
  287. timer->timer = new Timer((int32)(timer_override*1000));
  288. timer->type_group_spell_id = spell->GetSpellData()->type_group_spell_id;
  289. timer->linked_timer = spell->GetSpellData()->linked_timer;
  290. timer->spell_id = spell->GetSpellID();
  291. recast_timers.Add(timer);
  292. if(caster->IsPlayer()){
  293. if(timer_override == 0)
  294. ((Player*)caster)->LockSpell(spell, (int16)(spell->GetSpellData()->recast * 10));
  295. else
  296. ((Player*)caster)->LockSpell(spell, timer_override * 10);
  297. if (check_linked_timers && spell->GetSpellData()->linked_timer > 0) {
  298. vector<Spell*> linkedSpells = ((Player*)caster)->GetSpellBookSpellsByTimer(spell->GetSpellData()->linked_timer);
  299. for (int8 i = 0; i < linkedSpells.size(); i++) {
  300. Spell* spell2 = linkedSpells.at(i);
  301. if (spell2)
  302. CheckRecast(spell2, caster, timer_override, false);
  303. }
  304. }
  305. }
  306. }
  307. }
  308. void SpellProcess::CheckInterrupt(InterruptStruct* interrupt){
  309. if(!interrupt || !interrupt->interrupted || !interrupt->interrupted->IsEntity())
  310. return;
  311. Entity* entity = (Entity*)interrupt->interrupted;
  312. Client* client = entity->GetZone()->GetClientBySpawn(entity);
  313. if(client)
  314. SendFinishedCast(GetLuaSpell(entity), client);
  315. RemoveSpellTimersFromSpawn(entity, false);
  316. entity->IsCasting(false);
  317. entity->GetZone()->SendInterruptPacket(entity, interrupt->spell);
  318. if(interrupt->error_code > 0)
  319. entity->GetZone()->SendSpellFailedPacket(client, interrupt->error_code);
  320. if(entity->IsPlayer())
  321. {
  322. ((Player*)entity)->UnlockSpell(interrupt->spell->spell);
  323. SendSpellBookUpdate(((Player*)entity)->GetClient());
  324. }
  325. }
  326. bool SpellProcess::DeleteCasterSpell(Spawn* caster, Spell* spell, string reason){
  327. bool ret = false;
  328. // need to use size(true) to get pending updates to the list as well
  329. if (caster && spell && active_spells.size() > 0) {
  330. LuaSpell* lua_spell = 0;
  331. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  332. while (itr.Next()){
  333. lua_spell = itr->value;
  334. if (lua_spell->spell == spell && lua_spell->caster == caster) {
  335. ret = DeleteCasterSpell(lua_spell, reason);
  336. break;
  337. }
  338. }
  339. }
  340. return ret;
  341. }
  342. bool SpellProcess::DeleteCasterSpell(LuaSpell* spell, string reason, bool removing_all_spells, bool lock_spell_process, Spawn* remove_target){
  343. if(lock_spell_process)
  344. MSpellProcess.lock();
  345. bool ret = false;
  346. Spawn* target = 0;
  347. bool target_valid = false;
  348. if(spell) {
  349. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  350. if(remove_target && spell->targets.size() > 1) {
  351. for (int32 i = 0; i < spell->targets.size(); i++) {
  352. if(remove_target->GetID() == spell->targets.at(i)) {
  353. if(remove_target->IsEntity()){
  354. spell->removed_targets.push_back(remove_target->GetID());
  355. ((Entity*)remove_target)->RemoveProc(0, spell);
  356. ((Entity*)remove_target)->RemoveSpellEffect(spell);
  357. ((Entity*)remove_target)->RemoveSpellBonus(spell);
  358. if(spell->spell->GetSpellData()->det_type > 0 && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel))
  359. ((Entity*)remove_target)->RemoveDetrimentalSpell(spell);
  360. }
  361. spell->targets.erase(spell->targets.begin()+i, spell->targets.begin()+i+1);
  362. target_valid = true;
  363. break;
  364. }
  365. }
  366. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  367. if(lock_spell_process) {
  368. MSpellProcess.unlock();
  369. }
  370. return target_valid;
  371. }
  372. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  373. if (active_spells.count(spell) > 0)
  374. active_spells.Remove(spell);
  375. if (spell->caster) {
  376. if (spell->spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE){
  377. int8 actual_concentration = spell->spell->GetSpellData()->req_concentration;
  378. if (actual_concentration > 0)
  379. {
  380. int8 curConcentration = spell->caster->GetInfoStruct()->get_cur_concentration();
  381. if(curConcentration >= actual_concentration)
  382. {
  383. spell->caster->GetInfoStruct()->set_cur_concentration(curConcentration - actual_concentration);
  384. if (spell->caster->IsPlayer())
  385. spell->caster->GetZone()->TriggerCharSheetTimer();
  386. }
  387. }
  388. if(!spell->spell->GetSpellData()->duration_until_cancel)
  389. {
  390. CheckRecast(spell->spell, spell->caster);
  391. if (spell->caster && spell->caster->IsPlayer())
  392. SendSpellBookUpdate(((Player*)spell->caster)->GetClient());
  393. }
  394. }
  395. if(IsReady(spell->spell, spell->caster) && spell->caster->IsPlayer()) {
  396. ((Player*)spell->caster)->UnlockSpell(spell->spell);
  397. SendSpellBookUpdate(((Player*)spell->caster)->GetClient());
  398. }
  399. spell->caster->RemoveProc(0, spell);
  400. spell->caster->RemoveMaintainedSpell(spell);
  401. CheckRemoveTargetFromSpell(spell, removing_all_spells, removing_all_spells);
  402. ZoneServer* zone = spell->caster->GetZone();
  403. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  404. for (int32 i = 0; i < spell->targets.size(); i++) {
  405. target = zone->GetSpawnByID(spell->targets.at(i));
  406. if(target && target->IsEntity()){
  407. spell->removed_targets.push_back(target->GetID());
  408. ((Entity*)target)->RemoveProc(0, spell);
  409. ((Entity*)target)->RemoveSpellEffect(spell);
  410. ((Entity*)target)->RemoveSpellBonus(spell);
  411. if(spell->spell->GetSpellData()->det_type > 0 && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel))
  412. ((Entity*)target)->RemoveDetrimentalSpell(spell);
  413. }
  414. else{
  415. spell->caster->RemoveSpellEffect(spell);
  416. spell->caster->RemoveSpellBonus(spell);
  417. if(spell->spell->GetSpellData()->det_type > 0 && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel))
  418. spell->caster->RemoveDetrimentalSpell(spell);
  419. }
  420. if(target && target->IsPlayer() && spell->spell->GetSpellData()->fade_message.length() > 0){
  421. Client* client = target->GetZone()->GetClientBySpawn(target);
  422. if(client){
  423. bool send_to_sender = true;
  424. string fade_message = spell->spell->GetSpellData()->fade_message;
  425. if(fade_message.find("%t") != string::npos)
  426. fade_message.replace(fade_message.find("%t"), 2, target->GetName());
  427. client->Message(CHANNEL_SPELLS_OTHER, fade_message.c_str());
  428. }
  429. }
  430. if (target && target->IsPlayer() && spell->spell->GetSpellData()->fade_message.length() > 0) {
  431. Client* client = target->GetZone()->GetClientBySpawn(target);
  432. if (client) {
  433. bool send_to_sender = true;
  434. string fade_message_others = spell->spell->GetSpellData()->fade_message_others;
  435. if (fade_message_others.find("%t") != string::npos)
  436. fade_message_others.replace(fade_message_others.find("%t"), 2, target->GetName());
  437. if (fade_message_others.find("%c") != string::npos)
  438. fade_message_others.replace(fade_message_others.find("%c"), 2, spell->caster->GetName());
  439. if (fade_message_others.find("%T") != string::npos) {
  440. fade_message_others.replace(fade_message_others.find("%T"), 2, target->GetName());
  441. send_to_sender = false;
  442. }
  443. if (fade_message_others.find("%C") != string::npos) {
  444. fade_message_others.replace(fade_message_others.find("%C"), 2, spell->caster->GetName());
  445. send_to_sender = false;
  446. }
  447. spell->caster->GetZone()->SimpleMessage(CHANNEL_SPELLS_OTHER, fade_message_others.c_str(), target, 50, send_to_sender);
  448. }
  449. }
  450. }
  451. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  452. ret = true;
  453. }
  454. if(lua_interface)
  455. lua_interface->RemoveSpell(spell, true, SpellScriptTimersHasSpell(spell), reason, removing_all_spells);
  456. }
  457. if(lock_spell_process)
  458. MSpellProcess.unlock();
  459. return ret;
  460. }
  461. bool SpellProcess::ProcessSpell(LuaSpell* spell, bool first_cast, const char* function, SpellScriptTimer* timer, bool all_targets) {
  462. bool ret = false;
  463. if(!spell->state)
  464. {
  465. LogWrite(SPELL__ERROR, 0, "Spell", "Error: State is NULL! SpellProcess::ProcessSpell for Spell '%s'", (spell->spell != nullptr) ? spell->spell->GetName() : "Unknown");
  466. }
  467. else if(lua_interface && !spell->interrupted){
  468. if(all_targets)
  469. {
  470. for(int t=0;t<spell->targets.size();t++)
  471. {
  472. Spawn* altSpawn = spell->caster->GetZone()->GetSpawnByID(spell->targets[t]);
  473. if(altSpawn)
  474. {
  475. std::string functionCall = ApplyLuaFunction(spell, first_cast, function, timer, altSpawn);
  476. if(functionCall.length() < 1)
  477. ret = false;
  478. else
  479. ret = lua_interface->CallSpellProcess(spell, 2 + spell->spell->GetLUAData()->size(), functionCall);
  480. }
  481. }
  482. return true;
  483. }
  484. std::string functionCall = ApplyLuaFunction(spell, first_cast, function, timer);
  485. if(functionCall.length() < 1)
  486. ret = false;
  487. else
  488. ret = lua_interface->CallSpellProcess(spell, 2 + spell->spell->GetLUAData()->size(), functionCall);
  489. }
  490. return ret;
  491. }
  492. std::string SpellProcess::ApplyLuaFunction(LuaSpell* spell, bool first_cast, const char* function, SpellScriptTimer* timer, Spawn* altTarget)
  493. {
  494. std::string functionCall = lua_interface->AddSpawnPointers(spell, first_cast, false, function, timer, false, altTarget);
  495. vector<LUAData*>* data = spell->spell->GetLUAData();
  496. for(int32 i=0;i<data->size();i++){
  497. switch(data->at(i)->type){
  498. case 0:{
  499. lua_interface->SetSInt32Value(spell->state, data->at(i)->int_value);
  500. break;
  501. }
  502. case 1:{
  503. lua_interface->SetFloatValue(spell->state, data->at(i)->float_value);
  504. break;
  505. }
  506. case 2:{
  507. lua_interface->SetBooleanValue(spell->state, data->at(i)->bool_value);
  508. break;
  509. }
  510. case 3:{
  511. lua_interface->SetStringValue(spell->state, data->at(i)->string_value.c_str());
  512. break;
  513. }
  514. default:{
  515. LogWrite(SPELL__ERROR, 0, "Spell", "Error: Unknown LUA Type '%i' in SpellProcess::ProcessSpell for Spell '%s'", (int)data->at(i)->type, spell->spell->GetName());
  516. return string("");
  517. }
  518. }
  519. }
  520. return functionCall;
  521. }
  522. bool SpellProcess::CastPassives(Spell* spell, Entity* caster, bool remove) {
  523. CastInstant(spell, caster, caster, remove, true);
  524. return true;
  525. }
  526. bool SpellProcess::CastInstant(Spell* spell, Entity* caster, Entity* target, bool remove, bool passive) {
  527. LuaSpell* lua_spell = 0;
  528. if(lua_interface)
  529. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  530. if(!lua_spell)
  531. {
  532. string lua_script = spell->GetSpellData()->lua_script;
  533. lua_script.append(".lua");
  534. lua_spell = 0;
  535. if(lua_interface)
  536. lua_spell = lua_interface->GetSpell(lua_script.c_str());
  537. if(!lua_spell) {
  538. LogWrite(SPELL__ERROR, 0, "Spell", "Failed to get a LuaSpell for %s (%u)", spell->GetName(), spell->GetSpellID());
  539. return false;
  540. }
  541. else
  542. spell->GetSpellData()->lua_script = lua_script;
  543. }
  544. lua_spell->caster = caster;
  545. lua_spell->initial_caster_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  546. lua_spell->spell = spell;
  547. lua_spell->initial_target = target->GetID();
  548. lua_spell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  549. GetSpellTargets(lua_spell);
  550. if (!lua_spell->spell->IsCopiedSpell())
  551. {
  552. lua_getglobal(lua_spell->state, "customspell");
  553. if (lua_isfunction(lua_spell->state, lua_gettop(lua_spell->state))) {
  554. lua_pop(lua_spell->state, 1);
  555. Spell* tmpSpell = lua_spell->spell;
  556. lua_spell->spell = new Spell(lua_spell->spell);
  557. lua_interface->AddCustomSpell(lua_spell);
  558. lua_interface->AddSpawnPointers(lua_spell, false, false, "customspell",0,true);
  559. if (lua_pcall(lua_spell->state, 3, 3, 0) != 0) {
  560. lua_interface->RemoveCustomSpell(lua_spell->spell->GetSpellID());
  561. lua_interface->ResetFunctionStack(lua_spell->state);
  562. safe_delete(lua_spell->spell);
  563. lua_spell->spell = tmpSpell;
  564. }
  565. else
  566. lua_interface->ResetFunctionStack(lua_spell->state);
  567. }
  568. else
  569. lua_interface->ResetFunctionStack(lua_spell->state);
  570. }
  571. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  572. if (!remove)
  573. return CastProcessedSpell(lua_spell, passive);
  574. lua_interface->RemoveSpell(lua_spell, true, SpellScriptTimersHasSpell(lua_spell));
  575. return true;
  576. }
  577. void SpellProcess::SendStartCast(LuaSpell* spell, Client* client){
  578. if(client){
  579. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  580. if(packet){
  581. packet->setDataByName("cast_time", spell->spell->GetSpellData()->cast_time*.01);
  582. packet->setMediumStringByName("spell_name", spell->spell->GetSpellData()->name.data.c_str());
  583. EQ2Packet* outapp = packet->serialize();
  584. //DumpPacket(outapp);
  585. client->QueuePacket(outapp);
  586. safe_delete(packet);
  587. }
  588. }
  589. }
  590. void SpellProcess::SendFinishedCast(LuaSpell* spell, Client* client){
  591. if(client && spell && spell->spell){
  592. if (spell->spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE)
  593. UnlockAllSpells(client, spell->spell);
  594. else
  595. UnlockAllSpells(client);
  596. if(spell->resisted && spell->spell->GetSpellData()->recast > 0)
  597. CheckRecast(spell->spell, client->GetPlayer(), 0.5); // half sec recast on resisted spells
  598. else if (!spell->interrupted)
  599. CheckRecast(spell->spell, client->GetPlayer());
  600. else if(spell->caster && spell->caster->IsPlayer())
  601. {
  602. ((Player*)spell->caster)->LockSpell(spell->spell, (int16)(spell->spell->GetSpellData()->recast * 10));
  603. }
  604. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  605. if(packet){
  606. packet->setMediumStringByName("spell_name", spell->spell->GetSpellData()->name.data.c_str());
  607. client->QueuePacket(packet->serialize());
  608. safe_delete(packet);
  609. }
  610. SendSpellBookUpdate(client);
  611. }
  612. if (spell) {
  613. if (!spell->interrupted) {
  614. TakePower(spell);
  615. TakeHP(spell);
  616. TakeSavagery(spell);
  617. AddDissonance(spell);
  618. AddConcentration(spell);
  619. if (client && spell->spell && !spell->spell->GetSpellData()->friendly_spell)
  620. client->GetPlayer()->InCombat(true);
  621. if(client && spell->caster)
  622. client->CheckPlayerQuestsSpellUpdate(spell->spell);
  623. }
  624. }
  625. }
  626. void SpellProcess::LockAllSpells(Client* client){
  627. if(client){
  628. client->GetPlayer()->LockAllSpells();
  629. SendSpellBookUpdate(client);
  630. }
  631. }
  632. void SpellProcess::UnlockAllSpells(Client* client, Spell* exception){
  633. if(client)
  634. client->GetPlayer()->UnlockAllSpells(false, exception);
  635. }
  636. void SpellProcess::UnlockSpell(Client* client, Spell* spell){
  637. if(client && client->GetPlayer() && spell) {
  638. client->GetPlayer()->UnlockSpell(spell);
  639. SendSpellBookUpdate(client);
  640. }
  641. }
  642. bool SpellProcess::CheckPower(LuaSpell* spell){
  643. int16 req = 0;
  644. if(spell->caster){
  645. req = spell->spell->GetPowerRequired(spell->caster);
  646. if(spell->caster->GetPower() >= req)
  647. return true;
  648. }
  649. return false;
  650. }
  651. bool SpellProcess::TakePower(LuaSpell* spell, int32 custom_power_req){
  652. int16 req = 0;
  653. if(spell->caster){
  654. if(custom_power_req)
  655. req = custom_power_req;
  656. else
  657. req = spell->spell->GetPowerRequired(spell->caster);
  658. if(spell->caster->GetPower() >= req){
  659. spell->caster->SetPower(spell->caster->GetPower() - req);
  660. if(spell->caster->IsPlayer())
  661. spell->caster->GetZone()->TriggerCharSheetTimer();
  662. return true;
  663. }
  664. }
  665. return false;
  666. }
  667. bool SpellProcess::CheckHP(LuaSpell* spell) {
  668. int16 req = 0;
  669. if(spell->caster){
  670. req = spell->spell->GetHPRequired(spell->caster);
  671. if(spell->caster->GetHP() >= req)
  672. return true;
  673. }
  674. return false;
  675. }
  676. bool SpellProcess::TakeHP(LuaSpell* spell, int32 custom_hp_req) {
  677. int16 req = 0;
  678. if(spell->caster){
  679. if(custom_hp_req)
  680. req = custom_hp_req;
  681. else
  682. req = spell->spell->GetHPRequired(spell->caster);
  683. if(spell->caster->GetHP() >= req){
  684. spell->caster->SetHP(spell->caster->GetHP() - req);
  685. if(spell->caster->IsPlayer())
  686. spell->caster->GetZone()->TriggerCharSheetTimer();
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. bool SpellProcess::CheckConcentration(LuaSpell* spell) {
  693. if (spell && spell->caster) {
  694. int8 req = spell->spell->GetSpellData()->req_concentration;
  695. int8 current_avail = 5 - spell->caster->GetConcentrationCurrent();
  696. if (current_avail >= req)
  697. return true;
  698. }
  699. return false;
  700. }
  701. bool SpellProcess::AddConcentration(LuaSpell* spell) {
  702. if (spell && spell->caster) {
  703. int8 req = spell->spell->GetSpellData()->req_concentration;
  704. int8 curConcentration = spell->caster->GetInfoStruct()->get_cur_concentration();
  705. int8 current_avail = 5 - curConcentration;
  706. if (current_avail >= req) {
  707. spell->caster->GetInfoStruct()->set_cur_concentration(curConcentration + req);
  708. if (spell->caster->IsPlayer())
  709. spell->caster->GetZone()->TriggerCharSheetTimer();
  710. LogWrite(SPELL__DEBUG, 0, "Spell", "Concentration is now %u on %s", spell->caster->GetInfoStruct()->get_cur_concentration(), spell->caster->GetName());
  711. return true;
  712. }
  713. }
  714. return false;
  715. }
  716. bool SpellProcess::CheckSavagery(LuaSpell* spell) {
  717. if (spell && spell->caster) {
  718. int16 req = spell->spell->GetSavageryRequired(spell->caster);
  719. if(spell->caster->GetSavagery() >= req)
  720. return true;
  721. }
  722. return false;
  723. }
  724. bool SpellProcess::TakeSavagery(LuaSpell* spell) {
  725. int16 req = 0;
  726. if(spell && spell->caster && spell->caster->IsPlayer()){
  727. req = spell->spell->GetSavageryRequired(spell->caster);
  728. if(spell->caster->GetSavagery() >= req){
  729. spell->caster->SetSavagery(spell->caster->GetSavagery() - req);
  730. if(spell->caster->IsPlayer())
  731. spell->caster->GetZone()->TriggerCharSheetTimer();
  732. return true;
  733. }
  734. }
  735. return false;
  736. }
  737. bool SpellProcess::CheckDissonance(LuaSpell* spell) {
  738. if (spell && spell->caster) {
  739. int16 req = spell->spell->GetDissonanceRequired(spell->caster);
  740. if(spell->caster->GetDissonance() <= req)
  741. return true;
  742. }
  743. return false;
  744. }
  745. bool SpellProcess::AddDissonance(LuaSpell* spell) {
  746. int16 req = 0;
  747. if(spell && spell->caster && spell->caster->IsPlayer()){
  748. req = spell->spell->GetDissonanceRequired(spell->caster);
  749. if(spell->caster->GetDissonance() >= req){
  750. spell->caster->SetDissonance(spell->caster->GetDissonance() - req);
  751. if(spell->caster->IsPlayer())
  752. spell->caster->GetZone()->TriggerCharSheetTimer();
  753. return true;
  754. }
  755. }
  756. return false;
  757. }
  758. void SpellProcess::AddSpellToQueue(Spell* spell, Entity* caster){
  759. if(caster && caster->IsPlayer() && spell){
  760. spell_que.Put(caster, spell);
  761. ((Player*)caster)->QueueSpell(spell);
  762. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  763. if(client)
  764. SendSpellBookUpdate(client);
  765. }
  766. }
  767. void SpellProcess::RemoveSpellFromQueue(Spell* spell, Entity* caster){
  768. if(caster && caster->IsPlayer() && spell){
  769. spell_que.erase(caster);
  770. ((Player*)caster)->UnQueueSpell(spell);
  771. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  772. if(client)
  773. SendSpellBookUpdate(client);
  774. }
  775. }
  776. void SpellProcess::RemoveSpellFromQueue(Entity* caster, bool hostile_only) {
  777. if (caster && spell_que.count(caster) > 0) {
  778. Spell* spell = spell_que.Get(caster);
  779. if (spell) {
  780. bool remove = true;
  781. if (hostile_only && spell->GetSpellData()->target_type != SPELL_TARGET_ENEMY)
  782. remove = false;
  783. if (remove) {
  784. spell_que.erase(caster);
  785. ((Player*)caster)->UnQueueSpell(spell);
  786. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  787. if (client)
  788. SendSpellBookUpdate(client);
  789. }
  790. }
  791. }
  792. }
  793. void SpellProcess::CheckSpellQueue(Entity* caster){
  794. if(caster && caster->IsPlayer()){
  795. if(spell_que.count(caster) > 0)
  796. RemoveSpellFromQueue(spell_que.Get(caster), caster);
  797. }
  798. }
  799. bool SpellProcess::CheckSpellQueue(Spell* spell, Entity* caster){
  800. if(caster->IsPlayer()){
  801. bool add = true;
  802. bool remove = false;
  803. if(spell_que.count(caster) > 0){
  804. remove = true;
  805. if(spell_que.Get(caster) == spell)
  806. add = false;
  807. }
  808. if(remove)
  809. RemoveSpellFromQueue(spell, caster);
  810. if(add)
  811. {
  812. AddSpellToQueue(spell, caster);
  813. return true;
  814. }
  815. }
  816. return false;
  817. }
  818. void SpellProcess::SendSpellBookUpdate(Client* client){
  819. if(client){
  820. EQ2Packet* app = client->GetPlayer()->GetSpellBookUpdatePacket(client->GetVersion());
  821. if(app)
  822. client->QueuePacket(app);
  823. }
  824. }
  825. LuaSpell* SpellProcess::GetLuaSpell(Entity* caster){
  826. LuaSpell* spell = 0;
  827. if(caster && cast_timers.size() > 0){
  828. CastTimer* cast_timer = 0;
  829. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  830. while(itr.Next()){
  831. cast_timer = itr->value;
  832. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == caster){
  833. spell = cast_timer->spell;
  834. break;
  835. }
  836. }
  837. }
  838. return spell;
  839. }
  840. Spell* SpellProcess::GetSpell(Entity* caster){
  841. Spell* spell = 0;
  842. if(cast_timers.size() > 0){
  843. CastTimer* cast_timer = 0;
  844. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  845. while(itr.Next()){
  846. cast_timer = itr->value;
  847. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == caster){
  848. spell = cast_timer->spell->spell;
  849. break;
  850. }
  851. }
  852. }
  853. return spell;
  854. }
  855. Spawn* SpellProcess::GetSpellTarget(Entity* caster){
  856. Spawn* target = 0;
  857. if(cast_timers.size() > 0){
  858. CastTimer* cast_timer = 0;
  859. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  860. while(itr.Next()){
  861. cast_timer = itr->value;
  862. if(cast_timer && cast_timer->spell->caster == caster){
  863. target = caster->GetZone()->GetSpawnByID(cast_timer->spell->initial_target);
  864. break;
  865. }
  866. }
  867. }
  868. return target;
  869. }
  870. void SpellProcess::ProcessSpell(ZoneServer* zone, Spell* spell, Entity* caster, Spawn* target, bool lock, bool harvest_spell, LuaSpell* customSpell, int16 custom_cast_time, bool in_heroic_opp)
  871. {
  872. if((customSpell != 0 || spell != 0) && caster)
  873. {
  874. Client* client = 0;
  875. //int16 version = 0;
  876. LuaSpell* lua_spell = 0;
  877. if (customSpell)
  878. {
  879. lua_spell = customSpell;
  880. spell = lua_spell->spell;
  881. }
  882. else if(lua_interface)
  883. lua_spell = lua_interface->GetSpell(spell->GetSpellData()->lua_script.c_str());
  884. // this will only hit if customSpell is null and we go through the lua_interface
  885. if(!lua_spell)
  886. {
  887. string lua_script = spell->GetSpellData()->lua_script;
  888. lua_script.append(".lua");
  889. if(lua_interface)
  890. lua_spell = lua_interface->GetSpell(lua_script.c_str());
  891. if(!lua_spell)
  892. return;
  893. else
  894. spell->GetSpellData()->lua_script = lua_script;
  895. }
  896. if (!target)
  897. target = caster;
  898. int8 target_type = spell->GetSpellData()->target_type;
  899. int8 spell_type = spell->GetSpellData()->spell_type;
  900. lua_spell->caster = caster;
  901. lua_spell->initial_caster_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  902. lua_spell->spell = spell;
  903. int32 target_id = target->GetID();
  904. lua_spell->initial_target = target_id;
  905. lua_spell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  906. if (!harvest_spell)
  907. GetSpellTargets(lua_spell);
  908. else{
  909. AddLuaSpellTarget(lua_spell, target_id);
  910. }
  911. if (target_id == lua_spell->initial_target)
  912. {
  913. LogWrite(SPELL__DEBUG, 1, "Spell", "%s is casting %s on %s.", caster->GetName(), spell->GetName(), ( target ) ? target->GetName() : "No Target" );
  914. }
  915. else
  916. {
  917. 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());
  918. }
  919. target = lua_spell->caster->GetZone()->GetSpawnByID(lua_spell->initial_target);
  920. if(caster->IsPlayer() && zone)
  921. {
  922. client = zone->GetClientBySpawn(caster);
  923. //version = client->GetVersion();
  924. }
  925. if (!customSpell && !lua_spell->spell->IsCopiedSpell())
  926. {
  927. lua_getglobal(lua_spell->state, "customspell");
  928. if (lua_isfunction(lua_spell->state, lua_gettop(lua_spell->state))) {
  929. lua_pop(lua_spell->state, 1);
  930. Spell* tmpSpell = lua_spell->spell;
  931. lua_spell->spell = new Spell(lua_spell->spell);
  932. lua_interface->AddCustomSpell(lua_spell);
  933. lua_interface->AddSpawnPointers(lua_spell, false, false, "customspell", 0, true);
  934. if (lua_pcall(lua_spell->state, 3, 3, 0) != 0) {
  935. lua_interface->RemoveCustomSpell(lua_spell->spell->GetSpellID());
  936. lua_interface->ResetFunctionStack(lua_spell->state);
  937. safe_delete(lua_spell->spell);
  938. lua_spell->spell = tmpSpell;
  939. }
  940. else
  941. lua_interface->ResetFunctionStack(lua_spell->state);
  942. }
  943. else
  944. lua_interface->ResetFunctionStack(lua_spell->state);
  945. }
  946. //If this spell is the toggle cast type and is being toggled off, do this now
  947. if (spell->GetSpellData()->cast_type == SPELL_CAST_TYPE_TOGGLE)
  948. {
  949. bool ret_val = DeleteCasterSpell(caster, spell, "purged");
  950. if (ret_val)
  951. {
  952. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  953. DeleteSpell(lua_spell);
  954. return;
  955. }
  956. }
  957. // If a player is casting the spell AND spell is a tradeskill spell
  958. if (client && spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL)
  959. {
  960. // If the player is currently crafting
  961. if (client->IsCrafting())
  962. {
  963. Tradeskill* tradeskill = 0;
  964. zone->GetTradeskillMgr()->ReadLock(__FUNCTION__, __LINE__);
  965. tradeskill = zone->GetTradeskillMgr()->GetTradeskill(client);
  966. // Can not cast a crafting spell that doesn't match the current crafting technique
  967. if (spell->GetSpellData()->mastery_skill != tradeskill->recipe->GetTechnique())
  968. {
  969. // send a message to the client, used chat_relationship to match other tradeskill messages, not sure if it is accurate though
  970. 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());
  971. // Write a spell debug message on why we couldn't cast
  972. 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());
  973. // make sure to release the lock before we return out
  974. zone->GetTradeskillMgr()->ReleaseReadLock(__FUNCTION__, __LINE__);
  975. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  976. DeleteSpell(lua_spell);
  977. return;
  978. }
  979. // need to make sure the lock is released if the if passed
  980. zone->GetTradeskillMgr()->ReleaseReadLock(__FUNCTION__, __LINE__);
  981. }
  982. else // If the player is not currently crafting
  983. {
  984. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast a tradeskill spell (%s) while not crafting.", caster->GetName(), spell->GetName());
  985. zone->SendSpellFailedPacket(client, SPELL_ERROR_ONLY_WHEN_CRAFTING);
  986. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  987. DeleteSpell(lua_spell);
  988. return;
  989. }
  990. }
  991. if (caster != target && target != NULL && !caster->CheckLoS(target))
  992. {
  993. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot see target %s.", caster->GetName(), target->GetName());
  994. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANT_SEE_TARGET);
  995. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  996. DeleteSpell(lua_spell);
  997. return;
  998. }
  999. LuaSpell* conflictSpell = caster->HasLinkedTimerID(lua_spell, target, (spell_type == SPELL_TYPE_DD || spell_type == SPELL_TYPE_DOT));
  1000. if(conflictSpell)
  1001. {
  1002. if(conflictSpell->spell->GetSpellData()->min_class_skill_req > 0 && lua_spell->spell->GetSpellData()->min_class_skill_req > 0)
  1003. {
  1004. if(conflictSpell->spell->GetSpellData()->min_class_skill_req <= lua_spell->spell->GetSpellData()->min_class_skill_req)
  1005. {
  1006. if(lua_spell->spell->GetSpellData()->duration_until_cancel && !lua_spell->num_triggers)
  1007. {
  1008. if(spell->GetSpellData()->friendly_spell)
  1009. {
  1010. ZoneServer* zone = caster->GetZone();
  1011. Spawn* tmpTarget = zone->GetSpawnByID(conflictSpell->initial_target);
  1012. if(tmpTarget && tmpTarget->IsEntity())
  1013. {
  1014. zone->RemoveTargetFromSpell(conflictSpell, tmpTarget);
  1015. CheckRemoveTargetFromSpell(conflictSpell);
  1016. ((Entity*)tmpTarget)->RemoveSpellEffect(conflictSpell);
  1017. if(client && IsReady(conflictSpell->spell, client->GetPlayer()))
  1018. UnlockSpell(client, conflictSpell->spell);
  1019. }
  1020. DeleteSpell(lua_spell);
  1021. return;
  1022. }
  1023. else if(lua_spell->spell->GetSpellData()->spell_type == SPELL_TYPE_DEBUFF)
  1024. {
  1025. SpellCannotStack(zone, client, lua_spell->caster, lua_spell, conflictSpell);
  1026. return;
  1027. }
  1028. }
  1029. }
  1030. else
  1031. {
  1032. SpellCannotStack(zone, client, lua_spell->caster, lua_spell, conflictSpell);
  1033. return;
  1034. }
  1035. }
  1036. }
  1037. if ((caster->IsMezzed() && !spell->CastWhileMezzed()) || (caster->IsStunned() && !spell->CastWhileStunned()))
  1038. {
  1039. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (mezzed or stunned).", caster->GetName());
  1040. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STUNNED);
  1041. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1042. DeleteSpell(lua_spell);
  1043. return;
  1044. }
  1045. if (caster->IsStifled() && !spell->CastWhileStifled())
  1046. {
  1047. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (stifled).", caster->GetName());
  1048. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STIFFLED);
  1049. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1050. DeleteSpell(lua_spell);
  1051. return;
  1052. }
  1053. if (caster->IsFeared() && !spell->CastWhileFeared())
  1054. {
  1055. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (feared).", caster->GetName());
  1056. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_FEARED);
  1057. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1058. DeleteSpell(lua_spell);
  1059. return;
  1060. }
  1061. if(caster->IsPlayer() && !IsReady(spell, caster))
  1062. {
  1063. LogWrite(SPELL__DEBUG, 1, "Spell", "Queuing spell for %s.", caster->GetName());
  1064. bool queueSpell = CheckSpellQueue(spell, caster);
  1065. if(!queueSpell)
  1066. {
  1067. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1068. DeleteSpell(lua_spell);
  1069. }
  1070. return;
  1071. }
  1072. if (target_type != SPELL_TARGET_SELF && target_type != SPELL_TARGET_GROUP_AE && spell_type != SPELL_TYPE_ALLGROUPTARGETS && target_type != SPELL_TARGET_NONE && spell->GetSpellData()->max_aoe_targets == 0)
  1073. {
  1074. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not Self, Not Group AE, Not None, Max Targets = 0", spell->GetName());
  1075. if (!target)
  1076. {
  1077. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  1078. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  1079. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1080. DeleteSpell(lua_spell);
  1081. return;
  1082. }
  1083. float tmpRange = spell->GetSpellData()->range;
  1084. if (client && target != caster && !spell->GetSpellData()->range)
  1085. {
  1086. bool match = false;
  1087. int tmpTier = 0;
  1088. Spell* tmpSpell = 0;
  1089. for (tmpTier = 0; tmpTier < spell->GetSpellTier(); tmpTier++)
  1090. {
  1091. tmpSpell = master_spell_list.GetSpell(spell->GetSpellData()->id, tmpTier);
  1092. if (tmpSpell && tmpSpell->GetSpellData()->range)
  1093. {
  1094. match = true;
  1095. break;
  1096. }
  1097. }
  1098. if (tmpSpell)
  1099. tmpRange = tmpSpell->GetSpellData()->range;
  1100. if (!match)
  1101. tmpTier = -1;
  1102. char msg[512];
  1103. snprintf(msg, 512, "SpellCasted without proper spell range set %s ID %i Tier %i Range obtained from tier %i range %f", spell->GetName(), spell->GetSpellID(), spell->GetSpellTier(), tmpTier, tmpRange);
  1104. if (!world.CheckTempBugCRC(msg))
  1105. commands.Command_ReportBug(client, new Seperator(msg));
  1106. }
  1107. if(caster->GetDistance(target) > tmpRange)
  1108. {
  1109. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too far.", spell->GetName());
  1110. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  1111. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1112. DeleteSpell(lua_spell);
  1113. return;
  1114. }
  1115. if (caster->GetDistance(target) < spell->GetSpellData()->min_range)
  1116. {
  1117. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too close.", spell->GetName());
  1118. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_CLOSE);
  1119. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1120. DeleteSpell(lua_spell);
  1121. return;
  1122. }
  1123. }
  1124. if(target_type == SPELL_TARGET_SELF && spell->GetSpellData()->max_aoe_targets == 0)
  1125. {
  1126. if (harvest_spell)
  1127. {
  1128. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Harvest, Target Self, Max Targets = 0", spell->GetName());
  1129. if (!target || !target->IsGroundSpawn())
  1130. {
  1131. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target or not groundspawn.", spell->GetName());
  1132. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  1133. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1134. DeleteSpell(lua_spell);
  1135. return;
  1136. }
  1137. }
  1138. else
  1139. {
  1140. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target '%s' is Caster '%s'.", spell->GetName(), ( target ) ? target->GetName() : "None", caster->GetName());
  1141. target = caster;
  1142. }
  1143. }
  1144. // Is enemy spell AND direct-damage (no AE)
  1145. if (target_type == SPELL_TARGET_ENEMY && spell->GetSpellData()->max_aoe_targets == 0)
  1146. {
  1147. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target Enemy (%s) and Max AE Targets = 0.", spell->GetName(), target->GetName());
  1148. if (spell->GetSpellData()->friendly_spell && target->IsPlayer() )
  1149. {
  1150. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Friendly, Target is Player (%s).", spell->GetName(), target->GetName());
  1151. if (!target->IsEntity())
  1152. {
  1153. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  1154. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  1155. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1156. DeleteSpell(lua_spell);
  1157. return;
  1158. }
  1159. /*if (target->appearance.attackable) {
  1160. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  1161. return;
  1162. }*/
  1163. }
  1164. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && target->appearance.attackable)
  1165. {
  1166. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (friendly and NPC)", spell->GetName());
  1167. if (target->IsPet())
  1168. {
  1169. if (((NPC*)target)->GetOwner()->IsNPC())
  1170. {
  1171. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  1172. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1173. DeleteSpell(lua_spell);
  1174. return;
  1175. }
  1176. }
  1177. else if (target->IsBot() && (caster->IsPlayer() || caster->IsBot())) {
  1178. // Needed so bots or player can cast friendly spells on bots
  1179. }
  1180. else
  1181. {
  1182. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  1183. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1184. DeleteSpell(lua_spell);
  1185. return;
  1186. }
  1187. }
  1188. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && caster->IsNPC()) {
  1189. // TODO: faction checks? some other checks to prevent an npc casting a friendly spell on another npc
  1190. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: NPC Cast", spell->GetName());
  1191. }
  1192. else
  1193. {
  1194. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (catch all)", spell->GetName());
  1195. if (!target->IsEntity())
  1196. {
  1197. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  1198. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  1199. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1200. DeleteSpell(lua_spell);
  1201. return;
  1202. }
  1203. if (caster == target || (!target->IsPlayer() && !target->appearance.attackable))
  1204. {
  1205. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not an Enemy (Target: %s).", spell->GetName(), target->GetName());
  1206. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1207. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1208. DeleteSpell(lua_spell);
  1209. return;
  1210. }
  1211. if (!target->Alive())
  1212. {
  1213. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is not alive (Target: %s).", spell->GetName(), target->GetName());
  1214. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ALIVE);
  1215. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1216. DeleteSpell(lua_spell);
  1217. return;
  1218. }
  1219. if (target->GetInvulnerable())
  1220. {
  1221. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is invulnerable (Target: %s).", spell->GetName(), target->GetName());
  1222. zone->SendSpellFailedPacket(client, SPELL_ERROR_TARGET_INVULNERABLE);
  1223. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1224. DeleteSpell(lua_spell);
  1225. return;
  1226. }
  1227. if ((target->IsPlayer() || target->IsBot()) && (caster->IsPlayer() || caster->IsBot()))
  1228. {
  1229. bool attackAllowed = (Entity*)caster->AttackAllowed((Entity*)target, 0);
  1230. if (!attackAllowed)
  1231. {
  1232. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is player and not attackable.", spell->GetName(), target->GetName());
  1233. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1234. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1235. DeleteSpell(lua_spell);
  1236. return;
  1237. }
  1238. }
  1239. if (target->IsPet() && ((NPC*)target)->GetOwner() && ((NPC*)target)->GetOwner() == caster) {
  1240. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is casters pet and not attackable by caster.", spell->GetName(), target->GetName());
  1241. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1242. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1243. DeleteSpell(lua_spell);
  1244. return;
  1245. }
  1246. }
  1247. }
  1248. if (lua_spell->targets.size() == 0 && spell->GetSpellData()->max_aoe_targets == 0)
  1249. {
  1250. LogWrite(SPELL__ERROR, 0, "Spell", "SpellProcess::ProcessSpell Unable to find any spell targets for spell '%s', spell type: %u, target type %u.",
  1251. spell->GetName(), spell->GetSpellData()->spell_type, spell->GetSpellData()->target_type);
  1252. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1253. DeleteSpell(lua_spell);
  1254. return;
  1255. }
  1256. if(target_type == SPELL_TARGET_ENEMY_CORPSE || target_type == SPELL_TARGET_GROUP_CORPSE)
  1257. {
  1258. if(target->Alive())
  1259. {
  1260. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_DEAD);
  1261. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1262. DeleteSpell(lua_spell);
  1263. return;
  1264. }
  1265. if(target->IsPlayer() && zone->GetClientBySpawn(target)->GetCurrentRez()->active){
  1266. zone->SendSpellFailedPacket(client, SPELL_ERROR_ALREADY_CAST);
  1267. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1268. DeleteSpell(lua_spell);
  1269. return;
  1270. }
  1271. }
  1272. if(!CheckPower(lua_spell))
  1273. {
  1274. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Power to cast (%s).", spell->GetName(), caster->GetName());
  1275. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_POWER);
  1276. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1277. DeleteSpell(lua_spell);
  1278. return;
  1279. }
  1280. if (!CheckHP(lua_spell))
  1281. {
  1282. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Health to cast (%s).", spell->GetName(), caster->GetName());
  1283. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_HEALTH);
  1284. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1285. DeleteSpell(lua_spell);
  1286. return;
  1287. }
  1288. if (!CheckSavagery(lua_spell))
  1289. {
  1290. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Savagery to cast (%s).", spell->GetName(), caster->GetName());
  1291. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_SAVAGERY);
  1292. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1293. DeleteSpell(lua_spell);
  1294. return;
  1295. }
  1296. if (!CheckDissonance(lua_spell))
  1297. {
  1298. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Dissonance to cast (%s).", spell->GetName(), caster->GetName());
  1299. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_DISSONANCE);
  1300. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1301. DeleteSpell(lua_spell);
  1302. return;
  1303. }
  1304. if (!CheckConcentration(lua_spell))
  1305. {
  1306. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Concentration to cast (%s).", spell->GetName(), caster->GetName());
  1307. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_CONC);
  1308. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1309. DeleteSpell(lua_spell);
  1310. return;
  1311. }
  1312. // Precast in lua
  1313. if (lua_interface) {
  1314. bool result = false;
  1315. lua_interface->AddSpawnPointers(lua_spell, false, true);
  1316. if (lua_pcall(lua_spell->state, 2, 2, 0) == 0) {
  1317. result = lua_interface->GetBooleanValue(lua_spell->state, 1);
  1318. int8 error = lua_interface->GetInt8Value(lua_spell->state, 2) == 0 ? SPELL_ERROR_CANNOT_PREPARE : lua_interface->GetInt8Value(lua_spell->state, 2);
  1319. lua_interface->ResetFunctionStack(lua_spell->state);
  1320. if (!result) {
  1321. zone->SendSpellFailedPacket(client, error);
  1322. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1323. DeleteSpell(lua_spell);
  1324. return;
  1325. }
  1326. }
  1327. else {
  1328. LogWrite(SPELL__DEBUG, 1, "Spell", "No precast function found for %s", lua_spell->spell->GetName());
  1329. lua_interface->ResetFunctionStack(lua_spell->state);
  1330. }
  1331. }
  1332. else
  1333. LogWrite(SPELL__DEBUG, 1, "Spell", "Unable to do precast check as there was no lua_interface");
  1334. if (custom_cast_time > 0)
  1335. spell->GetSpellData()->cast_time = custom_cast_time;
  1336. //Apply casting speed mod
  1337. spell->ModifyCastTime(caster);
  1338. //cancel stealth effects on cast
  1339. if(caster->IsStealthed() || caster->IsInvis())
  1340. caster->CancelAllStealth();
  1341. if(caster && caster->IsEntity() && caster->CheckFizzleSpell(lua_spell))
  1342. {
  1343. caster->GetZone()->SendCastSpellPacket(0, target ? target : caster, caster);
  1344. caster->GetZone()->SendInterruptPacket(caster, lua_spell, true);
  1345. caster->IsCasting(false);
  1346. if(caster->IsPlayer())
  1347. {
  1348. ((Player*)caster)->UnlockSpell(spell);
  1349. SendSpellBookUpdate(((Player*)caster)->GetClient());
  1350. }
  1351. // fizzle takes half
  1352. int16 power_req = spell->GetPowerRequired(caster) / 2;
  1353. TakePower(lua_spell, power_req);
  1354. int16 hp_req = spell->GetHPRequired(caster) / 2;
  1355. TakeHP(lua_spell, hp_req);
  1356. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1357. DeleteSpell(lua_spell);
  1358. return;
  1359. }
  1360. SendStartCast(lua_spell, client);
  1361. LockAllSpells(client);
  1362. if(spell->GetSpellData()->cast_time > 0)
  1363. {
  1364. CastTimer* cast_timer = new CastTimer;
  1365. cast_timer->entity_command = 0;
  1366. cast_timer->target_id = target ? target->GetID() : 0;
  1367. cast_timer->spell = lua_spell;
  1368. cast_timer->spell->caster = caster;
  1369. cast_timer->delete_timer = false;
  1370. cast_timer->timer = new Timer(spell->GetSpellData()->cast_time * 10);
  1371. cast_timer->zone = zone;
  1372. cast_timer->in_heroic_opp = in_heroic_opp;
  1373. cast_timers.Add(cast_timer);
  1374. if(caster)
  1375. caster->IsCasting(true);
  1376. }
  1377. else
  1378. {
  1379. if(!CastProcessedSpell(lua_spell, false, in_heroic_opp))
  1380. {
  1381. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1382. DeleteSpell(lua_spell);
  1383. return;
  1384. }
  1385. }
  1386. if(caster)
  1387. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  1388. }
  1389. }
  1390. void SpellProcess::ProcessEntityCommand(ZoneServer* zone, EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock, bool in_heroic_opp) {
  1391. if (zone && entity_command && caster && target && !target->IsPlayer()) {
  1392. Client* client = zone->GetClientBySpawn(caster);
  1393. if (caster->GetDistance(target) > entity_command->distance) {
  1394. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  1395. return;
  1396. }
  1397. if (entity_command->cast_time > 0) {
  1398. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  1399. if (packet) {
  1400. LockAllSpells(client);
  1401. packet->setDataByName("cast_time", entity_command->cast_time * 0.01);
  1402. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1403. EQ2Packet* outapp = packet->serialize();
  1404. client->QueuePacket(outapp);
  1405. safe_delete(packet);
  1406. CastTimer* cast_timer = new CastTimer;
  1407. cast_timer->caster = client;
  1408. cast_timer->target_id = target ? target->GetID() : 0;
  1409. cast_timer->entity_command = entity_command;
  1410. cast_timer->spell = 0;
  1411. cast_timer->delete_timer = false;
  1412. cast_timer->timer = new Timer(entity_command->cast_time * 10);
  1413. cast_timer->zone = zone;
  1414. cast_timer->in_heroic_opp = in_heroic_opp;
  1415. cast_timers.Add(cast_timer);
  1416. caster->IsCasting(true);
  1417. }
  1418. }
  1419. else if (!CastProcessedEntityCommand(entity_command, client, target, in_heroic_opp))
  1420. return;
  1421. if (entity_command && entity_command->spell_visual > 0)
  1422. caster->GetZone()->SendCastEntityCommandPacket(entity_command, caster->GetID(), target->GetID());
  1423. }
  1424. }
  1425. bool SpellProcess::CastProcessedSpell(LuaSpell* spell, bool passive, bool in_heroic_opp){
  1426. if(!spell || !spell->caster || !spell->spell || spell->interrupted)
  1427. return false;
  1428. Client* client = 0;
  1429. if(spell->caster && spell->caster->IsPlayer())
  1430. client = spell->caster->GetZone()->GetClientBySpawn(spell->caster);
  1431. if (spell->spell->GetSpellData()->max_aoe_targets > 0 && spell->targets.size() == 0) {
  1432. GetSpellTargetsTrueAOE(spell);
  1433. if (spell->targets.size() == 0) {
  1434. if(client)
  1435. {
  1436. client->GetPlayer()->UnlockAllSpells(true);
  1437. SendSpellBookUpdate(client);
  1438. }
  1439. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_NO_TARGETS_IN_RANGE);
  1440. return false;
  1441. }
  1442. }
  1443. if(client && client->GetCurrentZone() &&
  1444. !spell->spell->GetSpellData()->friendly_spell)
  1445. {
  1446. ZoneServer* zone = client->GetCurrentZone();
  1447. Spawn* tmpTarget = zone->GetSpawnByID(spell->initial_target);
  1448. int8 spell_type = spell->spell->GetSpellData()->spell_type;
  1449. LuaSpell* conflictSpell = spell->caster->HasLinkedTimerID(spell, tmpTarget, (spell_type == SPELL_TYPE_DD || spell_type == SPELL_TYPE_DOT));
  1450. if(conflictSpell && tmpTarget && tmpTarget->IsEntity())
  1451. {
  1452. ((Entity*)tmpTarget)->RemoveSpellEffect(conflictSpell);
  1453. zone->RemoveTargetFromSpell(conflictSpell, tmpTarget);
  1454. }
  1455. }
  1456. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1457. bool processedSpell = false;
  1458. LuaSpell* replace_spell = 0;
  1459. // Check to see if we already casted this spell and it is in the active_spells list
  1460. /*while (itr.Next()) {
  1461. LuaSpell* luaspell = itr.value;
  1462. // Check to see if this is the same spell by comparing caster, spell id, and spell tier
  1463. if (luaspell->caster == spell->caster && luaspell->spell->GetSpellID() == spell->spell->GetSpellID() && luaspell->spell->GetSpellTier() == spell->spell->GetSpellTier() && luaspell->targets.size() == spell->targets.size()) {
  1464. vector<Spawn*>::iterator itr;
  1465. vector<Spawn*>::iterator itr2;
  1466. bool all_match = true;
  1467. // compare the target vector to make sure this is the same spell
  1468. for (itr = luaspell->targets.begin(); itr != luaspell->targets.end(); itr++) {
  1469. bool match = false;
  1470. for (itr2 = spell->targets.begin(); itr2 != spell->targets.end(); itr2++) {
  1471. if ((*itr) == (*itr2)) {
  1472. match = true;
  1473. break;
  1474. }
  1475. }
  1476. if (!match) {
  1477. all_match = false;
  1478. break;
  1479. }
  1480. }
  1481. if (all_match) {
  1482. // set a pointer to replace the spell in the active spell list so we don't have to run this loop again
  1483. replace_spell = luaspell;
  1484. // if friendly skip the calling of the lua function as they have already been called for the previous version of this spell
  1485. if (spell->spell->GetSpellData()->friendly_spell == 1)
  1486. processedSpell = true;
  1487. break;
  1488. }
  1489. }
  1490. }*/
  1491. bool allTargets = (spell->spell->GetSpellData()->spell_type == SPELL_TYPE_ALLGROUPTARGETS);
  1492. if (!processedSpell)
  1493. processedSpell = ProcessSpell(spell, true, 0, 0, allTargets);
  1494. // Quick hack to prevent a crash on spells that zones the caster (Gate)
  1495. if (!spell->caster)
  1496. return true;
  1497. Skill* skill = spell->caster->GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
  1498. // trigger potential skill increase if we succeed in casting a mastery skill and it still has room to grow (against this spell)
  1499. if(skill && skill->current_val < spell->spell->GetSpellData()->min_class_skill_req)
  1500. spell->caster->GetSkillByID(spell->spell->GetSpellData()->mastery_skill, true);
  1501. ZoneServer* zone = spell->caster->GetZone();
  1502. Spawn* target = 0;
  1503. if(processedSpell){
  1504. for (int32 i = 0; i < spell->targets.size(); i++) {
  1505. target = zone->GetSpawnByID(spell->targets[i]);
  1506. if (!target)
  1507. continue;
  1508. if (client && client->IsZoning())
  1509. continue;
  1510. // TODO: Establish actual hate per spell
  1511. if (!spell->spell->GetSpellData()->friendly_spell && target->IsNPC())
  1512. ((NPC*)target)->AddHate((Entity*)spell->caster, 50);
  1513. if(spell->spell->GetSpellData()->success_message.length() > 0){
  1514. if(client){
  1515. string success_message = spell->spell->GetSpellData()->success_message;
  1516. if(success_message.find("%t") != string::npos)
  1517. success_message.replace(success_message.find("%t"), 2, spell->caster->GetName());
  1518. client->Message(CHANNEL_SPELLS, success_message.c_str());
  1519. }
  1520. }
  1521. if(spell->spell->GetSpellData()->effect_message.length() > 0){
  1522. string effect_message = spell->spell->GetSpellData()->effect_message;
  1523. bool send_to_sender = true;
  1524. if(effect_message.find("%t") != string::npos)
  1525. effect_message.replace(effect_message.find("%t"), 2, target->GetName());
  1526. if (effect_message.find("%c") != string::npos)
  1527. effect_message.replace(effect_message.find("%c"), 2, spell->caster->GetName());
  1528. if (effect_message.find("%T") != string::npos) {
  1529. effect_message.replace(effect_message.find("%T"), 2, target->GetName());
  1530. send_to_sender = false;
  1531. }
  1532. if (effect_message.find("%C") != string::npos) {
  1533. effect_message.replace(effect_message.find("%C"), 2, spell->caster->GetName());
  1534. send_to_sender = false;
  1535. }
  1536. spell->caster->GetZone()->SimpleMessage(CHANNEL_SPELLS_OTHER, effect_message.c_str(), target, 50, send_to_sender);
  1537. }
  1538. target->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, spell->caster, spell->spell->GetName());
  1539. }
  1540. }
  1541. else{
  1542. if (!passive)
  1543. SendFinishedCast(spell, client);
  1544. return false;
  1545. }
  1546. if(!spell->resisted && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel)) {
  1547. for (int32 i = 0; i < spell->targets.size(); i++) {
  1548. //LogWrite(SPELL__ERROR, 0, "Spell", "No precast function found for %s", ((Entity*)target)->GetName());
  1549. target = zone->GetSpawnByID(spell->targets.at(i));
  1550. if (!target && spell->targets.at(i) == spell->caster->GetID()) {
  1551. target = spell->caster;
  1552. }
  1553. if (!target) {
  1554. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Zone has not finished loading process yet. Try again later.");
  1555. continue;
  1556. }
  1557. if (i == 0 && !spell->spell->GetSpellData()->not_maintained) {
  1558. spell->caster->AddMaintainedSpell(spell);
  1559. //((Entity*)target)->AddMaintainedSpell(spell);
  1560. LogWrite(SPELL__DEBUG, 0, "Spell", "AddMaintained on %s", ((Entity*)target)->GetName());
  1561. }
  1562. SpellEffects* effect = ((Entity*)target)->GetSpellEffect(spell->spell->GetSpellID());
  1563. if (effect && effect->tier > spell->spell->GetSpellTier()) {
  1564. if(client) {
  1565. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  1566. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "The spell did not take hold as the target already has a better spell of this type.");
  1567. }
  1568. }
  1569. else{
  1570. ((Entity*)target)->AddSpellEffect(spell);
  1571. if(spell->spell->GetSpellData()->det_type > 0)
  1572. ((Entity*)target)->AddDetrimentalSpell(spell);
  1573. }
  1574. }
  1575. if (replace_spell) {
  1576. active_spells.Remove(replace_spell);
  1577. active_spells.Add(spell);
  1578. }
  1579. else {
  1580. active_spells.Add(spell);
  1581. }
  1582. if (spell->num_triggers > 0)
  1583. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->num_triggers, 0);
  1584. if (spell->damage_remaining > 0)
  1585. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->damage_remaining, 1);
  1586. spell->caster->GetZone()->TriggerCharSheetTimer();
  1587. }
  1588. spell->num_calls = 1;
  1589. if(!spell->resisted && spell->spell->GetSpellData()->duration1 > 0){
  1590. spell->timer.Start();
  1591. if(spell->spell->GetSpellData()->call_frequency > 0)
  1592. spell->timer.SetTimer(spell->spell->GetSpellData()->call_frequency*100);
  1593. else
  1594. spell->timer.SetTimer(spell->spell->GetSpellData()->duration1*100);
  1595. if (active_spells.count(spell) > 0) {
  1596. active_spells.Add(spell);
  1597. }
  1598. }
  1599. // if the caster is a player and the spell is a tradeskill spell check for a tradeskill event
  1600. if (client && spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL)
  1601. client->GetCurrentZone()->GetTradeskillMgr()->CheckTradeskillEvent(client, spell->spell->GetSpellData()->icon);
  1602. if (spell->spell->GetSpellData()->friendly_spell && zone->GetSpawnByID(spell->initial_target))
  1603. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, zone->GetSpawnByID(spell->initial_target));
  1604. // Check HO's
  1605. if (client && !in_heroic_opp) {
  1606. HeroicOP* ho = nullptr;
  1607. Spell* ho_spell = nullptr;
  1608. int32 ho_target = 0;
  1609. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1610. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1611. map<Client*, HeroicOP*>::iterator soloItr = m_soloHO.find(client);
  1612. if (soloItr != m_soloHO.end()) {
  1613. ho = soloItr->second;
  1614. bool match = false;
  1615. LogWrite(SPELL__ERROR, 0, "HO", "target = %u", ho->GetTarget());
  1616. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1617. for (int8 i = 0; i < spell->targets.size(); i++) {
  1618. LogWrite(SPELL__ERROR, 0, "HO", "%u", spell->targets.at(i));
  1619. if (spell->targets.at(i) == ho->GetTarget()) {
  1620. match = true;
  1621. LogWrite(SPELL__ERROR, 0, "HO", "match found");
  1622. break;
  1623. }
  1624. }
  1625. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1626. if(match && !spell->spell)
  1627. LogWrite(SPELL__ERROR, 0, "HO", "%s: spell->spell is nullptr", client->GetPlayer()->GetName());
  1628. else if (match && spell->spell && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1629. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  1630. if (ho->GetComplete() > 0) {
  1631. if(!ho->GetWheel())
  1632. LogWrite(SPELL__ERROR, 0, "HO", "%s: Wheel is inactive (nullptr) cannot check for invalid spell", client->GetPlayer()->GetName());
  1633. else
  1634. {
  1635. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1636. ho_target = ho->GetTarget();
  1637. if (!ho_spell)
  1638. LogWrite(SPELL__ERROR, 0, "HO", "%s: Invalid spell for a HO, spell id = %u", client->GetPlayer()->GetName(), ho->GetWheel()->spell_id);
  1639. safe_delete(ho);
  1640. m_soloHO.erase(soloItr);
  1641. }
  1642. }
  1643. }
  1644. }
  1645. else if (client->GetPlayer()->GetGroupMemberInfo()) {
  1646. map<int32, HeroicOP*>::iterator groupItr = m_groupHO.find(client->GetPlayer()->GetGroupMemberInfo()->group_id);
  1647. if (groupItr != m_groupHO.end()) {
  1648. ho = groupItr->second;
  1649. int32 group_id = client->GetPlayer()->GetGroupMemberInfo()->group_id;
  1650. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1651. if (spell->targets.at(0) == ho->GetTarget() && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1652. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1653. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1654. deque<GroupMemberInfo*>::iterator itr;
  1655. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1656. if (group)
  1657. {
  1658. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1659. deque<GroupMemberInfo*>* members = group->GetMembers();
  1660. for (itr = members->begin(); itr != members->end(); itr++) {
  1661. if ((*itr)->client)
  1662. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  1663. }
  1664. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1665. }
  1666. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1667. if (ho->GetComplete() > 0) {
  1668. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1669. ho_target = ho->GetTarget();
  1670. if (!ho_spell)
  1671. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1672. safe_delete(ho);
  1673. m_groupHO.erase(groupItr);
  1674. }
  1675. }
  1676. else
  1677. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1678. }
  1679. }
  1680. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1681. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1682. if (ho_spell && ho_target != 0)
  1683. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(ho_target));
  1684. }
  1685. if (spell->spell->GetSpellData()->friendly_spell == 1 && spell->initial_target)
  1686. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  1687. if (!passive)
  1688. SendFinishedCast(spell, client);
  1689. return true;
  1690. }
  1691. bool SpellProcess::CastProcessedEntityCommand(EntityCommand* entity_command, Client* client, Spawn* target, bool in_heroic_opp) {
  1692. bool ret = false;
  1693. if (entity_command && client) {
  1694. UnlockAllSpells(client);
  1695. client->GetPlayer()->IsCasting(false);
  1696. if (entity_command->cast_time == 0) {
  1697. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1698. ret = true;
  1699. }
  1700. if (!ret) {
  1701. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  1702. if (packet) {
  1703. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1704. client->QueuePacket(packet->serialize());
  1705. safe_delete(packet);
  1706. SendSpellBookUpdate(client);
  1707. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1708. ret = true;
  1709. }
  1710. }
  1711. if (ret) {
  1712. EQ2_16BitString command;
  1713. command.data = entity_command->command;
  1714. command.size = entity_command->command.length();
  1715. int32 handler = commands.GetCommandHandler(command.data.c_str());
  1716. if (handler != 0xFFFFFFFF && handler < 999)
  1717. commands.Process(handler, &command, client, target);
  1718. }
  1719. }
  1720. return ret;
  1721. }
  1722. void SpellProcess::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement)
  1723. {
  1724. if(caster)
  1725. {
  1726. LogWrite(SPELL__DEBUG, 0, "Spell", "'%s' is Interrupting spell of '%s'...", interruptor ? interruptor->GetName() : "unknown", caster->GetName());
  1727. LuaSpell* spell = GetLuaSpell(caster);
  1728. if (spell && ((from_movement && !spell->spell->GetSpellData()->cast_while_moving) || (!from_movement && spell->spell->GetSpellData()->interruptable) ||
  1729. cancel))
  1730. {
  1731. Spawn* target = GetSpellTarget(caster);
  1732. InterruptStruct* interrupt = new InterruptStruct;
  1733. interrupt->interrupted = caster;
  1734. interrupt->spell = spell;
  1735. interrupt->target = target;
  1736. interrupt->error_code = error_code;
  1737. spell->interrupted = true;
  1738. interrupt_list.Add(interrupt);
  1739. Client* client = 0;
  1740. if(interruptor && interruptor->IsPlayer())
  1741. {
  1742. client = interruptor->GetZone()->GetClientBySpawn(interruptor);
  1743. client->Message(CHANNEL_SPELLS_OTHER, "You interrupt %s's ability to cast!", interruptor->GetName());
  1744. }
  1745. }
  1746. }
  1747. }
  1748. void SpellProcess::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  1749. if(lock_spell_process)
  1750. MSpellProcess.lock();
  1751. int32 i = 0;
  1752. if(cast_timers.size() > 0){
  1753. CastTimer* cast_timer = 0;
  1754. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  1755. while(itr.Next()){
  1756. cast_timer = itr->value;
  1757. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == spawn){
  1758. cast_timer->spell->caster = 0;
  1759. cast_timer->delete_timer = true;
  1760. }
  1761. }
  1762. }
  1763. if(remove_all){
  1764. LuaSpell* spell = 0;
  1765. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1766. while(itr.Next()){
  1767. spell = itr->value;
  1768. if (!spell)
  1769. continue;
  1770. if(spell->caster == spawn && call_expire_function){
  1771. DeleteCasterSpell(spell, "expired", remove_all);
  1772. continue;
  1773. }
  1774. if (spell->spell->GetSpellData()->persist_through_death)
  1775. continue;
  1776. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1777. for (i = 0; i < spell->targets.size(); i++){
  1778. if (spawn->GetID() == spell->targets.at(i)){
  1779. if (spawn->IsEntity())
  1780. ((Entity*)spawn)->RemoveSpellEffect(spell);
  1781. RemoveTargetFromSpell(spell, spawn, remove_all);
  1782. break;
  1783. }
  1784. }
  1785. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1786. }
  1787. if(recast_timers.size() > 0 && delete_recast){
  1788. RecastTimer* recast_timer = 0;
  1789. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  1790. while(itr.Next()){
  1791. recast_timer = itr->value;
  1792. if(recast_timer && recast_timer->caster == spawn){
  1793. safe_delete(recast_timer->timer);
  1794. recast_timers.Remove(recast_timer, true);
  1795. }
  1796. }
  1797. }
  1798. if(spell_que.size() > 0 && spawn->IsEntity()){
  1799. spell_que.erase((Entity*)spawn);
  1800. }
  1801. if(interrupt_list.size() > 0){
  1802. InterruptStruct* interrupt = 0;
  1803. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  1804. while(itr.Next()){
  1805. interrupt = itr->value;
  1806. if(interrupt && interrupt->interrupted == spawn){
  1807. interrupt_list.Remove(interrupt);
  1808. }
  1809. }
  1810. }
  1811. }
  1812. if(lock_spell_process)
  1813. MSpellProcess.unlock();
  1814. }
  1815. void SpellProcess::GetSpellTargets(LuaSpell* luaspell)
  1816. {
  1817. if (luaspell && luaspell->spell && luaspell->caster && luaspell->spell->GetSpellData()->max_aoe_targets == 0)
  1818. {
  1819. int8 target_type = luaspell->spell->GetSpellData()->target_type;
  1820. int8 spell_type = luaspell->spell->GetSpellData()->spell_type;
  1821. Spawn* caster = luaspell->caster;
  1822. Spawn* target = caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  1823. SpellData* data = luaspell->spell->GetSpellData();
  1824. bool implied = false;
  1825. Spawn* secondary_target = nullptr;
  1826. //implied target check -- only use this for players
  1827. 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))
  1828. {
  1829. if (caster->IsPlayer() && target->HasTarget())
  1830. {
  1831. secondary_target = target->GetTarget();
  1832. // check if spell is friendly
  1833. if (data->friendly_spell) {
  1834. //if target is NPC (and not a bot) on friendly spell, check to see if target is friendly
  1835. if (target->IsNPC() && !target->IsBot()) {
  1836. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1837. if (secondary_target && secondary_target->IsPlayer()) {
  1838. target = secondary_target;
  1839. luaspell->initial_target = target->GetID();
  1840. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1841. AddLuaSpellTarget(luaspell, target->GetID());
  1842. }
  1843. else if (secondary_target && secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsPlayer())
  1844. implied = true;
  1845. }
  1846. else if (target->IsPet() && ((Entity*)target)->GetOwner()->IsPlayer())
  1847. {
  1848. luaspell->initial_target = target->GetID();
  1849. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1850. AddLuaSpellTarget(luaspell, target->GetID());
  1851. }
  1852. }
  1853. } // if spell is not friendly
  1854. else { // check if there is an implied target for this non-friendly spell
  1855. if (target->IsPlayer() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsPlayer())) {
  1856. if (secondary_target && secondary_target->IsNPC()) {
  1857. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1858. implied = true;
  1859. }
  1860. }
  1861. else if (target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)target))
  1862. {
  1863. secondary_target = target;
  1864. implied = true;
  1865. luaspell->initial_target = target->GetID();
  1866. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1867. AddLuaSpellTarget(luaspell, target->GetID());
  1868. GetPlayerGroupTargets((Player*)target, caster, luaspell);
  1869. }
  1870. else if (secondary_target && target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)secondary_target))
  1871. {
  1872. implied = true;
  1873. luaspell->initial_target = secondary_target->GetID();
  1874. luaspell->initial_target_char_id = (secondary_target && secondary_target->IsPlayer()) ? ((Player*)secondary_target)->GetCharacterID() : 0;
  1875. AddLuaSpellTarget(luaspell, secondary_target->GetID());
  1876. GetPlayerGroupTargets((Player*)secondary_target, caster, luaspell);
  1877. }
  1878. }
  1879. } // end friendly spell check
  1880. }
  1881. else if (caster->IsPlayer()) {
  1882. if (data->friendly_spell) {
  1883. if (target->IsNPC() && !target->IsBot()) {
  1884. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1885. target = caster;
  1886. luaspell->initial_target = caster->GetID();
  1887. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1888. }
  1889. }
  1890. }
  1891. else
  1892. {
  1893. if (target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)target)) {
  1894. luaspell->initial_target = target->GetID();
  1895. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1896. AddLuaSpellTarget(luaspell, target->GetID());
  1897. }
  1898. }
  1899. }
  1900. }
  1901. else if (target_type == SPELL_TARGET_GROUP_AE || spell_type == SPELL_TYPE_ALLGROUPTARGETS || target_type == SPELL_TARGET_RAID_AE) {
  1902. // player handling
  1903. if (target)
  1904. {
  1905. if (data->icon_backdrop == 316 || data->icon_backdrop == 312) // using TARGET backdrop icon
  1906. {
  1907. // PLAYER LOGIC:
  1908. if ((data->friendly_spell && (target->IsPlayer() && luaspell->caster->IsPlayer() && target != luaspell->caster && ((Player*)target)->GetGroupMemberInfo() != NULL && ((Player*)luaspell->caster)->GetGroupMemberInfo() != NULL
  1909. && ((Player*)target)->GetGroupMemberInfo()->group_id == ((Player*)luaspell->caster)->GetGroupMemberInfo()->group_id))
  1910. || (!data->friendly_spell && (target->IsPlayer() && luaspell->caster->IsPlayer() && target != luaspell->caster && ((Player*)target)->GetGroupMemberInfo() != NULL)))
  1911. {
  1912. GetPlayerGroupTargets((Player*)target, caster, luaspell, true, false);
  1913. }
  1914. //TODO: NEED RAID SUPPORT
  1915. // NPC LOGIC:
  1916. else if (target->IsNPC())
  1917. {
  1918. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  1919. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  1920. if (group)
  1921. {
  1922. vector<Spawn*>::iterator itr;
  1923. // iterate through spawn group members
  1924. for (itr = group->begin(); itr != group->end(); itr++)
  1925. {
  1926. Spawn* group_member = *itr;
  1927. // 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 group_member->Alive()
  1928. if (group_member->GetZone() == caster->GetZone() &&
  1929. group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune() && (!((Entity*)group_member)->IsMezzed() || group_member == target))
  1930. AddLuaSpellTarget(luaspell, group_member->GetID());
  1931. // note: this should generate some hate towards the caster
  1932. }
  1933. } // end is spawngroup
  1934. else
  1935. AddLuaSpellTarget(luaspell, target->GetID()); // return single target NPC for non-friendly spell
  1936. }
  1937. else if(data->friendly_spell)
  1938. {
  1939. // add self
  1940. target = NULL;
  1941. AddSelfAndPet(luaspell, caster);
  1942. luaspell->initial_target = caster->GetID();
  1943. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1944. }
  1945. else if(target && target->IsPlayer())
  1946. {
  1947. if(!GetPlayerGroupTargets((Player*)target, caster, luaspell, true, false))
  1948. AddSelfAndPet(luaspell, target);
  1949. }
  1950. }
  1951. else // default self cast for group/raid AE
  1952. {
  1953. target = caster;
  1954. luaspell->initial_target = caster->GetID();
  1955. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1956. }
  1957. // spell target versus self cast
  1958. }
  1959. else if (data->icon_backdrop != 316) // default self cast for group/raid AE and not using TARGET backdrop icon
  1960. {
  1961. target = caster;
  1962. luaspell->initial_target = caster->GetID();
  1963. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1964. }
  1965. }
  1966. else if (target_type == SPELL_TARGET_SELF){
  1967. target = caster;
  1968. luaspell->initial_target = caster->GetID();
  1969. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1970. }
  1971. //if using implied target, target = the implied target
  1972. if (implied)
  1973. {
  1974. target = secondary_target;
  1975. luaspell->initial_target = secondary_target->GetID();
  1976. luaspell->initial_target_char_id = (secondary_target && secondary_target->IsPlayer()) ? ((Player*)secondary_target)->GetCharacterID() : 0;
  1977. }
  1978. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1979. // Group AE type NOTE: Add support for RAID AE to affect raid members once raids have been completed
  1980. if (target_type == SPELL_TARGET_GROUP_AE || spell_type == SPELL_TYPE_ALLGROUPTARGETS || target_type == SPELL_TARGET_RAID_AE)
  1981. {
  1982. if (data->icon_backdrop == 316) // single target in a group/raid
  1983. {
  1984. if (target)
  1985. AddLuaSpellTarget(luaspell, target->GetID(), false);
  1986. }
  1987. // is friendly
  1988. else if (data->friendly_spell)
  1989. {
  1990. // caster is an Entity
  1991. if (luaspell->caster->IsEntity())
  1992. {
  1993. // entity caster is in a player group
  1994. if (((Entity*)caster)->GetGroupMemberInfo())
  1995. {
  1996. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1997. // get group members
  1998. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  1999. if (group)
  2000. {
  2001. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2002. deque<GroupMemberInfo*>* members = group->GetMembers();
  2003. deque<GroupMemberInfo*>::iterator itr;
  2004. // iterate through list of group members
  2005. for (itr = members->begin(); itr != members->end(); itr++)
  2006. {
  2007. // get group member player info
  2008. Entity* group_member = (*itr)->member;
  2009. if(!group_member){
  2010. continue;
  2011. }
  2012. LogWrite(SPELL__DEBUG, 0, "Player", "%s is group member for spell %s", group_member->GetName(), luaspell->spell->GetName());
  2013. // if the group member is in the casters zone, and is alive
  2014. if( group_member->Alive())
  2015. {
  2016. if(group_member->GetZone() != caster->GetZone())
  2017. {
  2018. SpellProcess::AddSelfAndPetToCharTargets(luaspell, group_member);
  2019. }
  2020. else if (group_member->GetZone() == luaspell->caster->GetZone()) {
  2021. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2022. if (group_member->HasPet()) {
  2023. Entity* pet = group_member->GetPet();
  2024. if (!pet)
  2025. pet = group_member->GetCharmedPet();
  2026. if (pet)
  2027. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2028. LogWrite(SPELL__DEBUG, 0, "Player", "%s added a pet %s (%u) for spell %s", group_member->GetName(), pet ? pet->GetName() : "", pet ? pet->GetID() : 0, luaspell->spell->GetName());
  2029. }
  2030. }
  2031. }
  2032. }
  2033. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2034. }
  2035. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2036. }
  2037. else
  2038. AddSelfAndPet(luaspell, caster); // else caster is not in a group, thus alone
  2039. }
  2040. else if (caster->IsNPC()) // caster is NOT a player
  2041. {
  2042. // caster is NPC and in a spawn group with other NPCs
  2043. vector<Spawn*>* group = ((NPC*)caster)->GetSpawnGroup();
  2044. if (group)
  2045. {
  2046. vector<Spawn*>::iterator itr;
  2047. for (itr = group->begin(); itr != group->end(); itr++)
  2048. {
  2049. Spawn* group_member = *itr;
  2050. if (group_member->IsNPC() && group_member->Alive()){
  2051. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2052. if (((Entity*)group_member)->HasPet()){
  2053. Entity* pet = ((Entity*)group_member)->GetPet();
  2054. if (pet)
  2055. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2056. pet = ((Entity*)group_member)->GetCharmedPet();
  2057. if (pet)
  2058. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2059. }
  2060. }
  2061. }
  2062. }
  2063. else
  2064. AddSelfAndPet(luaspell, caster);
  2065. safe_delete(group);
  2066. } // end is player
  2067. } // end is friendly
  2068. } // end is Group AE
  2069. else if (target_type == SPELL_TARGET_SELF && caster)
  2070. AddLuaSpellTarget(luaspell, caster->GetID(), false); // if spell is SELF, return caster
  2071. else if (target_type == SPELL_TARGET_CASTER_PET && caster && caster->IsEntity() && ((Entity*)caster)->HasPet()) {
  2072. AddSelfAndPet(luaspell, caster, true);
  2073. }
  2074. else if (target_type == SPELL_TARGET_ENEMY && target && target->Alive()) // if target is enemy, and is alive
  2075. {
  2076. // if friendly spell
  2077. if (data->friendly_spell > 0)
  2078. {
  2079. // if caster is a player
  2080. if (caster->IsPlayer())
  2081. {
  2082. // if spell can affect raid, only group members or is a group spell
  2083. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  2084. {
  2085. // if caster is in a group, and target is a player and targeted player is a group member
  2086. if (((Player*)caster)->GetGroupMemberInfo() && (target->IsPlayer() || target->IsBot() || target->IsPet()) && ((Player*)caster)->IsGroupMember((Entity*)target))
  2087. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2088. else
  2089. AddLuaSpellTarget(luaspell, caster->GetID(), false); // else return the caster
  2090. }
  2091. else if (target->IsPlayer() || target->IsBot()) // else it is not raid, group only or group spell
  2092. AddLuaSpellTarget(luaspell, target->GetID(), false); // return target for single spell
  2093. else if ((luaspell->targets.size() < 1) || (!target->IsPet() || (((Entity*)target)->GetOwner() && !((Entity*)target)->GetOwner()->IsPlayer())))
  2094. AddLuaSpellTarget(luaspell, caster->GetID(), false); // and if no target, cast on self
  2095. }
  2096. else if (caster->IsNPC()) // caster is an NPC
  2097. {
  2098. // if NPC's spell can affect raid, only group members or is a group spell
  2099. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  2100. {
  2101. if (caster->IsBot() && (target->IsBot() || target->IsPlayer())) {
  2102. GroupMemberInfo* gmi = ((Entity*)caster)->GetGroupMemberInfo();
  2103. if (gmi && target->IsEntity() && world.GetGroupManager()->IsInGroup(gmi->group_id, (Entity*)target)) {
  2104. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2105. }
  2106. else
  2107. AddSelfAndPet(luaspell, caster);
  2108. }
  2109. // if NPC caster is in a group, and target is a player and targeted player is a group member
  2110. else if (((NPC*)caster)->HasSpawnGroup() && target->IsNPC() && ((NPC*)caster)->IsInSpawnGroup((NPC*)target))
  2111. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2112. else
  2113. AddSelfAndPet(luaspell, caster);
  2114. }
  2115. else if (target->IsNPC())
  2116. AddLuaSpellTarget(luaspell, target->GetID(), false); // return target for single spell
  2117. else {
  2118. if (caster->IsBot() && (target->IsBot() || target->IsPlayer()))
  2119. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2120. else
  2121. AddLuaSpellTarget(luaspell, caster->GetID(), false); // and if no target, cast on self
  2122. }
  2123. } // end is player
  2124. } // end is friendly
  2125. else if (target && (data->group_spell > 0 || data->icon_backdrop == 312)) // is not friendly, but is a group spell, icon_backdrop 312 is green (encounter AE)
  2126. {
  2127. // target is non-player
  2128. if (target->IsNPC())
  2129. {
  2130. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  2131. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  2132. if (group)
  2133. {
  2134. vector<Spawn*>::iterator itr;
  2135. // iterate through spawn group members
  2136. for (itr = group->begin(); itr != group->end(); itr++)
  2137. {
  2138. Spawn* group_member = *itr;
  2139. // 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 group_member->Alive()
  2140. if (group_member->GetZone() == caster->GetZone() &&
  2141. group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune() && (!((Entity*)group_member)->IsMezzed() || group_member == target))
  2142. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2143. // note: this should generate some hate towards the caster
  2144. }
  2145. } // end is spawngroup
  2146. else
  2147. AddLuaSpellTarget(luaspell, target->GetID(), false); // return single target NPC for non-friendly spell
  2148. safe_delete(group);
  2149. } // end is NPC
  2150. else if (target->IsPlayer() && caster->IsNPC()) // the NPC is casting on a player
  2151. {
  2152. // player is in a group
  2153. if (((Player*)target)->GetGroupMemberInfo())
  2154. {
  2155. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2156. deque<GroupMemberInfo*>::iterator itr;
  2157. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2158. if (group)
  2159. {
  2160. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2161. deque<GroupMemberInfo*>* members = group->GetMembers();
  2162. if(!members)
  2163. return;
  2164. // iterate through players group members
  2165. for (itr = members->begin(); itr != members->end(); itr++)
  2166. {
  2167. Entity* group_member = (*itr)->member;
  2168. // if the group member is in the same zone as caster, and group member is alive, and group member is within distance
  2169. if (group_member && group_member->GetZone() == caster->GetZone() && group_member->Alive() && caster->GetDistance(group_member) <= data->range
  2170. && (group_member == target || !group_member->IsAOEImmune()))
  2171. AddLuaSpellTarget(luaspell, group_member->GetID(), false); // add as target
  2172. }
  2173. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2174. }
  2175. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2176. }
  2177. else
  2178. AddLuaSpellTarget(luaspell, target->GetID(), false); // player not in group
  2179. } // end is caster player or npc
  2180. }
  2181. else if (target)
  2182. AddLuaSpellTarget(luaspell, target->GetID(), false); // is not friendly nor a group spell
  2183. }
  2184. //Rez spells
  2185. else if(target && target_type == SPELL_TARGET_ENEMY_CORPSE){
  2186. //is friendly
  2187. if(data->friendly_spell){
  2188. //target is player
  2189. if(target->IsPlayer()){
  2190. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2191. }
  2192. }
  2193. }
  2194. else if(target_type == SPELL_TARGET_GROUP_CORPSE){
  2195. //is friendly
  2196. if(data->friendly_spell){
  2197. //target is player
  2198. if(target && target->IsPlayer()){
  2199. //if target has group
  2200. if(((Player*)target)->GetGroupMemberInfo()) {
  2201. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2202. deque<GroupMemberInfo*>::iterator itr;
  2203. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2204. if (group)
  2205. {
  2206. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2207. deque<GroupMemberInfo*>* members = group->GetMembers();
  2208. if(!members)
  2209. return;
  2210. Entity* group_member = 0;
  2211. for (itr = members->begin(); itr != members->end(); itr++) {
  2212. group_member = (*itr)->member;
  2213. //Check if group member is in the same zone in range of the spell and dead
  2214. if (group_member && group_member->GetZone() == target->GetZone() && !group_member->Alive() && target->GetDistance(group_member) <= data->radius) {
  2215. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2216. }
  2217. }
  2218. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2219. }
  2220. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2221. }
  2222. else
  2223. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2224. }
  2225. }
  2226. }
  2227. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2228. }
  2229. if (luaspell && luaspell->targets.size() > 20)
  2230. LogWrite(SPELL__WARNING, 0, "Spell", "Warning in %s: Size of targets array is %u", __FUNCTION__, luaspell->targets.size());
  2231. }
  2232. bool SpellProcess::GetPlayerGroupTargets(Player* target, Spawn* caster, LuaSpell* luaspell, bool bypassSpellChecks, bool bypassRangeChecks)
  2233. {
  2234. if (bypassSpellChecks || luaspell->spell->GetSpellData()->group_spell > 0 || luaspell->spell->GetSpellData()->icon_backdrop == 312)
  2235. {
  2236. if (((Player*)target)->GetGroupMemberInfo())
  2237. {
  2238. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2239. if (group)
  2240. {
  2241. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2242. deque<GroupMemberInfo*>* members = group->GetMembers();
  2243. deque<GroupMemberInfo*>::iterator itr;
  2244. GroupMemberInfo* info = 0;
  2245. for (itr = members->begin(); itr != members->end(); itr++) {
  2246. info = *itr;
  2247. if (info == ((Player*)target)->GetGroupMemberInfo())
  2248. continue;
  2249. else if (info && info->client &&
  2250. info->client->GetPlayer()->GetZone() == ((Player*)target)->GetZone() && info->client->GetPlayer()->Alive()
  2251. && (bypassRangeChecks || caster->GetDistance((Entity*)info->client->GetPlayer()) <= luaspell->spell->GetSpellData()->range))
  2252. {
  2253. AddSelfAndPet(luaspell, info->client->GetPlayer());
  2254. }
  2255. }
  2256. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2257. return true;
  2258. }
  2259. }
  2260. }
  2261. return false;
  2262. }
  2263. void SpellProcess::GetSpellTargetsTrueAOE(LuaSpell* luaspell) {
  2264. if (luaspell && luaspell->caster && luaspell->spell && luaspell->spell->GetSpellData()->max_aoe_targets > 0) {
  2265. if (luaspell->caster->HasTarget() && luaspell->caster->GetTarget() != luaspell->caster){
  2266. //Check if the caster has an implied target
  2267. if (luaspell->caster->GetDistance(luaspell->caster->GetTarget()) <= luaspell->spell->GetSpellData()->radius)
  2268. {
  2269. luaspell->initial_target = luaspell->caster->GetTarget()->GetID();
  2270. luaspell->initial_target_char_id = (luaspell->caster->GetTarget() && luaspell->caster->GetTarget()->IsPlayer()) ? ((Player*)luaspell->caster->GetTarget())->GetCharacterID() : 0;
  2271. }
  2272. }
  2273. int32 ignore_target = 0;
  2274. vector<Spawn*> spawns = luaspell->caster->GetZone()->GetAttackableSpawnsByDistance(luaspell->caster, luaspell->spell->GetSpellData()->radius);
  2275. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2276. for (int8 i = 0; i < spawns.size(); i++) {
  2277. Spawn* spawn = spawns.at(i);
  2278. if (i == 0){
  2279. if (luaspell->initial_target && luaspell->caster->GetID() != luaspell->initial_target){
  2280. //this is the "Direct" target and aoe can't be avoided
  2281. AddLuaSpellTarget(luaspell, luaspell->initial_target, false);
  2282. ignore_target = luaspell->initial_target;
  2283. }
  2284. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  2285. break;
  2286. }
  2287. //If we have already added this spawn, check the next spawn in the list
  2288. if (spawn && spawn->GetID() == ignore_target){
  2289. i++;
  2290. if (i < spawns.size())
  2291. spawn = spawns.at(i);
  2292. else
  2293. break;
  2294. }
  2295. if (spawn){
  2296. //If this spawn is immune to aoe, continue
  2297. if (((Entity*)spawn)->IsAOEImmune() || ((Entity*)spawn)->IsMezzed())
  2298. continue;
  2299. AddLuaSpellTarget(luaspell, spawn->GetID(), false);
  2300. }
  2301. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  2302. break;
  2303. }
  2304. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2305. }
  2306. if (luaspell->targets.size() > 20)
  2307. LogWrite(SPELL__DEBUG, 0, "Spell", "Warning in SpellProcess::GetSpellTargetsTrueAOE Size of targets array is %u", luaspell->targets.size());
  2308. }
  2309. void SpellProcess::AddSpellScriptTimer(SpellScriptTimer* timer) {
  2310. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2311. m_spellScriptList.push_back(timer);
  2312. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2313. }
  2314. void SpellProcess::RemoveSpellScriptTimer(SpellScriptTimer* timer, bool locked) {
  2315. if (m_spellScriptList.size() == 0)
  2316. return;
  2317. vector<SpellScriptTimer*>::iterator itr;
  2318. if(!locked)
  2319. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2320. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2321. if ((*itr) == timer) {
  2322. SpellScriptTimer* timer = *itr;
  2323. if ((*itr) && (*itr)->deleteWhenDone && lua_interface) {
  2324. lua_interface->AddPendingSpellDelete(timer->spell);
  2325. }
  2326. m_spellScriptList.erase(itr);
  2327. safe_delete(timer);
  2328. break;
  2329. }
  2330. }
  2331. if(!locked)
  2332. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2333. }
  2334. void SpellProcess::RemoveSpellScriptTimerBySpell(LuaSpell* spell, bool clearPendingDeletes) {
  2335. vector<SpellScriptTimer*>::iterator itr;
  2336. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2337. if (lua_interface && clearPendingDeletes)
  2338. lua_interface->DeletePendingSpell(spell);
  2339. if (m_spellScriptList.size() == 0)
  2340. {
  2341. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2342. return;
  2343. }
  2344. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); ) {
  2345. if ((*itr)->spell == spell)
  2346. {
  2347. vector<SpellScriptTimer*>::iterator cur = itr;
  2348. SpellScriptTimer* timer = *itr;
  2349. m_spellScriptList.erase(cur);
  2350. safe_delete(timer);
  2351. break;
  2352. }
  2353. else
  2354. itr++;
  2355. }
  2356. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2357. }
  2358. void SpellProcess::CheckSpellScriptTimers() {
  2359. vector<SpellScriptTimer*>::iterator itr;
  2360. vector<SpellScriptTimer*> temp_list;
  2361. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2362. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2363. if (Timer::GetCurrentTime2() >= (*itr)->time) {
  2364. temp_list.push_back((*itr));
  2365. ProcessSpell((*itr)->spell, false, (*itr)->customFunction.c_str(), (*itr));
  2366. }
  2367. }
  2368. for (itr = temp_list.begin(); itr != temp_list.end(); itr++) {
  2369. RemoveSpellScriptTimer(*itr, true);
  2370. }
  2371. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2372. }
  2373. bool SpellProcess::SpellScriptTimersHasSpell(LuaSpell* spell) {
  2374. bool ret = false;
  2375. vector<SpellScriptTimer*>::iterator itr;
  2376. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  2377. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2378. SpellScriptTimer* timer = *itr;
  2379. if (timer && timer->spell == spell) {
  2380. ret = true;
  2381. break;
  2382. }
  2383. }
  2384. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  2385. return ret;
  2386. }
  2387. std::string SpellProcess::SpellScriptTimerCustomFunction(LuaSpell* spell) {
  2388. bool ret = false;
  2389. std::string val = string("");
  2390. vector<SpellScriptTimer*>::iterator itr;
  2391. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  2392. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2393. SpellScriptTimer* timer = *itr;
  2394. if (timer && timer->spell == spell) {
  2395. val = string(timer->customFunction);
  2396. break;
  2397. }
  2398. }
  2399. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  2400. return val;
  2401. }
  2402. void SpellProcess::ClearSpellScriptTimerList() {
  2403. vector<SpellScriptTimer*>::iterator itr;
  2404. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2405. for(itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2406. if ((*itr) && (*itr)->deleteWhenDone && lua_interface)
  2407. lua_interface->AddPendingSpellDelete((*itr)->spell);
  2408. safe_delete((*itr));
  2409. }
  2410. m_spellScriptList.clear();
  2411. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2412. }
  2413. void SpellProcess::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool removeCaster){
  2414. if (!spell || !target)
  2415. return;
  2416. LogWrite(SPELL__DEBUG, 0, "Spell", "%s RemoveTargetFromSpell %s (%u).", spell->spell->GetName(), target->GetName(), target->GetID());
  2417. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  2418. if(removeCaster && spell->caster && spell->caster == target)
  2419. spell->caster = nullptr;
  2420. if (!remove_target_list[spell])
  2421. remove_target_list[spell] = new vector<int32>;
  2422. remove_target_list[spell]->push_back(target->GetID());
  2423. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  2424. }
  2425. void SpellProcess::CheckRemoveTargetFromSpell(LuaSpell* spell, bool allow_delete, bool removing_all_spells){
  2426. if (!spell)
  2427. return;
  2428. if (remove_target_list.size() > 0){
  2429. map<LuaSpell*, vector<int32>*>::iterator remove_itr;
  2430. vector<int32>::iterator remove_target_itr;
  2431. vector<int32>::iterator target_itr;
  2432. vector<int32>* targets;
  2433. vector<int32>* remove_targets = 0;
  2434. Spawn* remove_spawn = 0;
  2435. bool should_delete = false;
  2436. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  2437. for (remove_itr = remove_target_list.begin(); remove_itr != remove_target_list.end(); remove_itr++){
  2438. if (remove_itr->first == spell){
  2439. targets = &spell->targets;
  2440. remove_targets = remove_itr->second;
  2441. if (remove_targets && targets){
  2442. for (remove_target_itr = remove_targets->begin(); remove_target_itr != remove_targets->end(); remove_target_itr++){
  2443. if(!spell->caster || !spell->caster->GetZone())
  2444. continue;
  2445. remove_spawn = spell->caster->GetZone()->GetSpawnByID((*remove_target_itr));
  2446. if (remove_spawn) {
  2447. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2448. if(remove_spawn && remove_spawn->IsPlayer())
  2449. {
  2450. multimap<int32,int8>::iterator entries;
  2451. while((entries = spell->char_id_targets.find(((Player*)remove_spawn)->GetCharacterID())) != spell->char_id_targets.end())
  2452. {
  2453. spell->char_id_targets.erase(entries);
  2454. }
  2455. }
  2456. for (target_itr = targets->begin(); target_itr != targets->end(); target_itr++) {
  2457. if (remove_spawn->GetID() == (*target_itr)) {
  2458. ((Entity*)remove_spawn)->RemoveProc(0, spell);
  2459. ((Entity*)remove_spawn)->RemoveMaintainedSpell(spell);
  2460. LogWrite(SPELL__DEBUG, 0, "Spell", "%s CheckRemoveTargetFromSpell %s (%u).", spell->spell->GetName(), remove_spawn->GetName(), remove_spawn->GetID());
  2461. targets->erase(target_itr);
  2462. if (remove_spawn->IsEntity())
  2463. {
  2464. if(!removing_all_spells)
  2465. {
  2466. if(remove_spawn->IsPlayer())
  2467. spell->char_id_targets.insert(make_pair(((Player*)remove_spawn)->GetCharacterID(),0));
  2468. else if(remove_spawn->IsPet() && ((Entity*)remove_spawn)->GetOwner() && ((Entity*)remove_spawn)->GetOwner()->IsPlayer())
  2469. {
  2470. Entity* pet = (Entity*)remove_spawn;
  2471. Player* ownerPlayer = (Player*)pet->GetOwner();
  2472. spell->char_id_targets.insert(make_pair(ownerPlayer->GetCharacterID(),pet->GetPetType()));
  2473. }
  2474. }
  2475. ((Entity*)remove_spawn)->RemoveEffectsFromLuaSpell(spell);
  2476. }
  2477. break;
  2478. }
  2479. }
  2480. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2481. if (targets->size() == 0 && spell->char_id_targets.size() == 0 && allow_delete) {
  2482. should_delete = true;
  2483. break;
  2484. }
  2485. }
  2486. }
  2487. }
  2488. break;
  2489. }
  2490. }
  2491. remove_target_list.erase(spell);
  2492. if (remove_targets)
  2493. remove_targets->clear();
  2494. safe_delete(remove_targets);
  2495. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  2496. if (should_delete)
  2497. DeleteCasterSpell(spell, "purged");
  2498. }
  2499. }
  2500. bool SpellProcess::AddHO(Client* client, HeroicOP* ho) {
  2501. bool ret = true;
  2502. if (client && ho) {
  2503. MSoloHO.writelock(__FUNCTION__, __LINE__);
  2504. if (m_soloHO.count(client) > 0) {
  2505. if (m_soloHO[client]->GetWheel()) {
  2506. ret = false;
  2507. }
  2508. else {
  2509. safe_delete(m_soloHO[client]);
  2510. m_soloHO[client] = ho;
  2511. }
  2512. }
  2513. else
  2514. m_soloHO[client] = ho;
  2515. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  2516. }
  2517. return ret;
  2518. }
  2519. bool SpellProcess::AddHO(int32 group_id, HeroicOP* ho) {
  2520. bool ret = true;
  2521. if (group_id > 0 && ho) {
  2522. MGroupHO.writelock(__FUNCTION__, __LINE__);
  2523. if (m_groupHO.count(group_id) > 0) {
  2524. if (m_groupHO[group_id]->GetWheel()) {
  2525. ret = false;
  2526. }
  2527. else {
  2528. safe_delete(m_groupHO[group_id]);
  2529. m_groupHO[group_id] = ho;
  2530. }
  2531. }
  2532. else
  2533. m_groupHO[group_id] = ho;
  2534. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  2535. }
  2536. return ret;
  2537. }
  2538. void SpellProcess::KillHOBySpawnID(int32 spawn_id) {
  2539. // Check solo HO's
  2540. MSoloHO.writelock(__FUNCTION__, __LINE__);
  2541. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  2542. map<Client*, HeroicOP*>::iterator delete_itr;
  2543. while (itr != m_soloHO.end()) {
  2544. if (itr->second->GetTarget() == spawn_id) {
  2545. itr->second->SetComplete(1);
  2546. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  2547. delete_itr = itr;
  2548. safe_delete(itr->second);
  2549. itr++;
  2550. m_soloHO.erase(delete_itr);
  2551. }
  2552. else
  2553. itr++;
  2554. }
  2555. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  2556. // Check Group HO's
  2557. MGroupHO.writelock(__FUNCTION__, __LINE__);
  2558. map<int32, HeroicOP*>::iterator itr2 = m_groupHO.begin();
  2559. map<int32, HeroicOP*>::iterator delete_itr2;
  2560. while (itr2 != m_groupHO.end()) {
  2561. if (itr2->second->GetTarget() == spawn_id) {
  2562. itr2->second->SetComplete(1);
  2563. world.GetGroupManager()->GroupLock(__FUNCTION__ , __LINE__);
  2564. deque<GroupMemberInfo*>::iterator itr3;
  2565. PlayerGroup* group = world.GetGroupManager()->GetGroup(itr2->first);
  2566. if (group)
  2567. {
  2568. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2569. deque<GroupMemberInfo*>* members = group->GetMembers();
  2570. for (itr3 = members->begin(); itr3 != members->end(); itr3++) {
  2571. if ((*itr3)->client)
  2572. ClientPacketFunctions::SendHeroicOPUpdate((*itr3)->client, itr2->second);
  2573. }
  2574. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2575. }
  2576. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2577. delete_itr2 = itr2;
  2578. safe_delete(itr2->second);
  2579. itr2++;
  2580. m_groupHO.erase(delete_itr2);
  2581. }
  2582. else
  2583. itr2++;
  2584. }
  2585. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  2586. }
  2587. void SpellProcess::AddSpellCancel(LuaSpell* spell){
  2588. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  2589. SpellCancelList.push_back(spell);
  2590. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  2591. }
  2592. void SpellProcess::DeleteSpell(LuaSpell* spell)
  2593. {
  2594. RemoveSpellFromQueue(spell->spell, spell->caster);
  2595. if (spell->spell->IsCopiedSpell())
  2596. {
  2597. lua_interface->RemoveCustomSpell(spell->spell->GetSpellID());
  2598. safe_delete(spell->spell);
  2599. }
  2600. safe_delete(spell);
  2601. }
  2602. void SpellProcess::SpellCannotStack(ZoneServer* zone, Client* client, Entity* caster, LuaSpell* lua_spell, LuaSpell* conflictSpell)
  2603. {
  2604. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot stack spell %s, conflicts with %s.", caster->GetName(), lua_spell->spell->GetName(), conflictSpell->spell->GetName());
  2605. zone->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  2606. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  2607. DeleteSpell(lua_spell);
  2608. }
  2609. void SpellProcess::AddActiveSpell(LuaSpell* spell)
  2610. {
  2611. if(!active_spells.count(spell))
  2612. active_spells.Add(spell);
  2613. }
  2614. void SpellProcess::AddSelfAndPet(LuaSpell* spell, Spawn* self, bool onlyPet)
  2615. {
  2616. if(!onlyPet)
  2617. AddLuaSpellTarget(spell, self->GetID(), false);
  2618. if(self->IsEntity() && ((Entity*)self)->HasPet() && ((Entity*)self)->GetPet())
  2619. AddLuaSpellTarget(spell, ((Entity*)self)->GetPet()->GetID(), false);
  2620. if(self->IsEntity() && ((Entity*)self)->HasPet() && ((Entity*)self)->GetCharmedPet())
  2621. AddLuaSpellTarget(spell, ((Entity*)self)->GetCharmedPet()->GetID(), false);
  2622. if(!onlyPet && self->IsEntity() && ((Entity*)self)->IsPet() && ((Entity*)self)->GetOwner())
  2623. AddLuaSpellTarget(spell, ((Entity*)self)->GetOwner()->GetID(), false);
  2624. }
  2625. void SpellProcess::AddSelfAndPetToCharTargets(LuaSpell* spell, Spawn* caster, bool onlyPet)
  2626. {
  2627. if(!caster->IsPlayer())
  2628. return;
  2629. Player* player = ((Player*)caster);
  2630. int32 charID = player->GetCharacterID();
  2631. if(player->HasPet() && player->GetPet())
  2632. spell->char_id_targets.insert(make_pair(charID, player->GetPet()->GetPetType()));
  2633. if(player->HasPet() && player->GetCharmedPet())
  2634. spell->char_id_targets.insert(make_pair(charID, player->GetPet()->GetPetType()));
  2635. if(!onlyPet)
  2636. spell->char_id_targets.insert(make_pair(charID, 0x00));
  2637. }
  2638. void SpellProcess::DeleteActiveSpell(LuaSpell* spell) {
  2639. active_spells.Remove(spell);
  2640. }
  2641. bool SpellProcess::AddLuaSpellTarget(LuaSpell* lua_spell, int32 id, bool lock_spell_targets) {
  2642. bool ret = false;
  2643. if(lock_spell_targets)
  2644. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2645. if(std::find(lua_spell->removed_targets.begin(), lua_spell->removed_targets.end(), id) == lua_spell->removed_targets.end()) {
  2646. lua_spell->targets.push_back(id);
  2647. ret = true;
  2648. }
  2649. if(lock_spell_targets)
  2650. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2651. return ret;
  2652. }