SpellProcess.cpp 110 KB

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