SpellProcess.cpp 110 KB

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