SpellProcess.cpp 108 KB

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