SpellProcess.cpp 109 KB

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