SpellProcess.cpp 110 KB

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