9
3

SpellProcess.cpp 106 KB

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