SpellProcess.cpp 89 KB

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