SpellProcess.cpp 110 KB

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