SpellProcess.cpp 74 KB

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