SpellProcess.cpp 88 KB

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