9
3

SpellProcess.cpp 114 KB

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