SpellProcess.cpp 109 KB

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