SpellProcess.cpp 110 KB

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