SpellProcess.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  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. HeroicOP* ho = nullptr;
  1256. Spell* ho_spell = nullptr;
  1257. int32 ho_target = 0;
  1258. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1259. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1260. map<Client*, HeroicOP*>::iterator soloItr = m_soloHO.find(client);
  1261. if (soloItr != m_soloHO.end()) {
  1262. ho = soloItr->second;
  1263. bool match = false;
  1264. LogWrite(SPELL__ERROR, 0, "HO", "target = %u", ho->GetTarget());
  1265. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1266. for (int8 i = 0; i < spell->targets.size(); i++) {
  1267. LogWrite(SPELL__ERROR, 0, "HO", "%u", spell->targets.at(i));
  1268. if (spell->targets.at(i) == ho->GetTarget()) {
  1269. match = true;
  1270. LogWrite(SPELL__ERROR, 0, "HO", "match found");
  1271. break;
  1272. }
  1273. }
  1274. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1275. if (match && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1276. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  1277. if (ho->GetComplete() > 0) {
  1278. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1279. ho_target = ho->GetTarget();
  1280. if (!ho_spell)
  1281. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1282. safe_delete(ho);
  1283. m_soloHO.erase(soloItr);
  1284. }
  1285. }
  1286. }
  1287. else if (client->GetPlayer()->GetGroupMemberInfo()) {
  1288. map<int32, HeroicOP*>::iterator groupItr = m_groupHO.find(client->GetPlayer()->GetGroupMemberInfo()->group_id);
  1289. if (groupItr != m_groupHO.end()) {
  1290. ho = groupItr->second;
  1291. int32 group_id = client->GetPlayer()->GetGroupMemberInfo()->group_id;
  1292. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1293. if (spell->targets.at(0) == ho->GetTarget() && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1294. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1295. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1296. deque<GroupMemberInfo*>::iterator itr;
  1297. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(group_id);
  1298. for (itr = members->begin(); itr != members->end(); itr++) {
  1299. if ((*itr)->client)
  1300. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  1301. }
  1302. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1303. if (ho->GetComplete() > 0) {
  1304. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1305. ho_target = ho->GetTarget();
  1306. if (!ho_spell)
  1307. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1308. safe_delete(ho);
  1309. m_groupHO.erase(groupItr);
  1310. }
  1311. }
  1312. else
  1313. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1314. }
  1315. }
  1316. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1317. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1318. if (ho_spell && ho_target != 0)
  1319. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(ho_target));
  1320. }
  1321. if (spell->spell->GetSpellData()->friendly_spell == 1 && spell->initial_target)
  1322. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  1323. if (!passive)
  1324. SendFinishedCast(spell, client);
  1325. return true;
  1326. }
  1327. bool SpellProcess::CastProcessedEntityCommand(EntityCommand* entity_command, Client* client, Spawn* target) {
  1328. bool ret = false;
  1329. if (entity_command && client) {
  1330. UnlockAllSpells(client);
  1331. client->GetPlayer()->IsCasting(false);
  1332. if (entity_command->cast_time == 0) {
  1333. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1334. ret = true;;
  1335. }
  1336. if (!ret) {
  1337. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  1338. if (packet) {
  1339. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1340. client->QueuePacket(packet->serialize());
  1341. safe_delete(packet);
  1342. SendSpellBookUpdate(client);
  1343. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1344. ret = true;
  1345. }
  1346. }
  1347. if (ret) {
  1348. EQ2_16BitString command;
  1349. command.data = entity_command->command;
  1350. command.size = entity_command->command.length();
  1351. int32 handler = commands.GetCommandHandler(command.data.c_str());
  1352. if (handler != 0xFFFFFFFF && handler < 999)
  1353. commands.Process(handler, &command, client, target);
  1354. }
  1355. }
  1356. return ret;
  1357. }
  1358. void SpellProcess::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement)
  1359. {
  1360. if(caster)
  1361. {
  1362. LogWrite(SPELL__DEBUG, 0, "Spell", "'%s' is Interrupting spell of '%s'...", interruptor ? interruptor->GetName() : "unknown", caster->GetName());
  1363. LuaSpell* spell = GetLuaSpell(caster);
  1364. if (spell && ((from_movement && !spell->spell->GetSpellData()->cast_while_moving) || (!from_movement && spell->spell->GetSpellData()->interruptable) ||
  1365. cancel))
  1366. {
  1367. Spawn* target = GetSpellTarget(caster);
  1368. InterruptStruct* interrupt = new InterruptStruct;
  1369. interrupt->interrupted = caster;
  1370. interrupt->spell = spell;
  1371. interrupt->target = target;
  1372. interrupt->error_code = error_code;
  1373. spell->interrupted = true;
  1374. interrupt_list.Add(interrupt);
  1375. Client* client = 0;
  1376. if(interruptor && interruptor->IsPlayer())
  1377. {
  1378. client = interruptor->GetZone()->GetClientBySpawn(interruptor);
  1379. client->Message(CHANNEL_COLOR_SPELL_INTERRUPT, "You interrupt %s's ability to cast!", interruptor->GetName());
  1380. }
  1381. }
  1382. }
  1383. }
  1384. void SpellProcess::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast){
  1385. int32 i = 0;
  1386. if(cast_timers.size() > 0){
  1387. CastTimer* cast_timer = 0;
  1388. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  1389. while(itr.Next()){
  1390. cast_timer = itr->value;
  1391. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == spawn){
  1392. cast_timer->spell->caster = 0;
  1393. cast_timer->delete_timer = true;
  1394. }
  1395. }
  1396. }
  1397. if(remove_all){
  1398. LuaSpell* spell = 0;
  1399. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1400. while(itr.Next()){
  1401. spell = itr->value;
  1402. if (!spell)
  1403. continue;
  1404. if (spell->spell->GetSpellData()->persist_though_death)
  1405. continue;
  1406. if(spell->caster == spawn){
  1407. DeleteCasterSpell(spell);
  1408. continue;
  1409. }
  1410. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1411. for (i = 0; i < spell->targets.size(); i++){
  1412. if (spawn->GetID() == spell->targets.at(i)){
  1413. if (spawn->IsEntity())
  1414. ((Entity*)spawn)->RemoveSpellEffect(spell);
  1415. RemoveTargetFromSpell(spell, spawn);
  1416. break;
  1417. }
  1418. }
  1419. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1420. }
  1421. if(recast_timers.size() > 0 && delete_recast){
  1422. RecastTimer* recast_timer = 0;
  1423. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  1424. while(itr.Next()){
  1425. recast_timer = itr->value;
  1426. if(recast_timer && recast_timer->caster == spawn){
  1427. safe_delete(recast_timer->timer);
  1428. recast_timers.Remove(recast_timer, true);
  1429. }
  1430. }
  1431. }
  1432. if(spell_que.size() > 0 && spawn->IsEntity()){
  1433. spell_que.erase((Entity*)spawn);
  1434. }
  1435. if(interrupt_list.size() > 0){
  1436. InterruptStruct* interrupt = 0;
  1437. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  1438. while(itr.Next()){
  1439. interrupt = itr->value;
  1440. if(interrupt && interrupt->interrupted == spawn){
  1441. interrupt_list.Remove(interrupt);
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. void SpellProcess::GetSpellTargets(LuaSpell* luaspell)
  1448. {
  1449. if (luaspell && luaspell->spell && luaspell->caster && luaspell->spell->GetSpellData()->max_aoe_targets == 0)
  1450. {
  1451. int8 target_type = luaspell->spell->GetSpellData()->target_type;
  1452. Spawn* caster = luaspell->caster;
  1453. Spawn* target = caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  1454. SpellData* data = luaspell->spell->GetSpellData();
  1455. bool implied = false;
  1456. Spawn* secondary_target = nullptr;
  1457. //implied target check -- only use this for players
  1458. 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))
  1459. {
  1460. if (caster->IsPlayer() && target->HasTarget())
  1461. {
  1462. secondary_target = target->GetTarget();
  1463. if (secondary_target) {
  1464. // check if spell is friendly
  1465. if (data->friendly_spell) {
  1466. //if target is NPC (and not a bot) on friendly spell, check to see if target is friendly
  1467. if (target->IsNPC() && !target->IsBot()) {
  1468. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1469. if (secondary_target->IsPlayer()) {
  1470. implied = true;
  1471. }
  1472. else if (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsPlayer())
  1473. implied = true;
  1474. }
  1475. }
  1476. } // if spell is not friendly
  1477. else { // check if there is an implied target for this non-friendly spell
  1478. if (target->IsPlayer() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsPlayer())) {
  1479. if (secondary_target->IsNPC()) {
  1480. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1481. implied = true;
  1482. }
  1483. }
  1484. }
  1485. } // end friendly spell check
  1486. }
  1487. }
  1488. else if (caster->IsPlayer()) {
  1489. if (data->friendly_spell) {
  1490. if (target->IsNPC() && !target->IsBot()) {
  1491. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1492. target = caster;
  1493. luaspell->initial_target = caster->GetID();
  1494. }
  1495. }
  1496. }
  1497. }
  1498. }
  1499. else if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE) {
  1500. target = caster;
  1501. luaspell->initial_target = caster->GetID();
  1502. }
  1503. else if (target_type == SPELL_TARGET_SELF){
  1504. target = caster;
  1505. luaspell->initial_target = caster->GetID();
  1506. }
  1507. //if using implied target, target = the implied target
  1508. if (implied)
  1509. {
  1510. target = secondary_target;
  1511. luaspell->initial_target = secondary_target->GetID();
  1512. }
  1513. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1514. // Group AE type NOTE: Add support for RAID AE to affect raid members once raids have been completed
  1515. if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE)
  1516. {
  1517. // is friendly
  1518. if (data->friendly_spell)
  1519. {
  1520. // caster is an Entity
  1521. if (luaspell->caster->IsEntity())
  1522. {
  1523. // entity caster is in a player group
  1524. if (((Entity*)caster)->GetGroupMemberInfo())
  1525. {
  1526. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1527. // get group members
  1528. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  1529. deque<GroupMemberInfo*>::iterator itr;
  1530. // iterate through list of group members
  1531. for (itr = members->begin(); itr != members->end(); itr++)
  1532. {
  1533. // get group member player info
  1534. Entity* group_member = (*itr)->member;
  1535. // if the group member is in the casters zone, and is alive
  1536. if (group_member->GetZone() == luaspell->caster->GetZone() && group_member->Alive()){
  1537. luaspell->targets.push_back(group_member->GetID());
  1538. if (group_member->HasPet()){
  1539. Entity* pet = group_member->GetPet();
  1540. if (!pet)
  1541. pet = group_member->GetCharmedPet();
  1542. if (pet)
  1543. luaspell->targets.push_back(pet->GetID());
  1544. }
  1545. }
  1546. }
  1547. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1548. }
  1549. else
  1550. luaspell->targets.push_back(caster->GetID()); // else caster is not in a group, thus alone
  1551. }
  1552. else if (caster->IsNPC()) // caster is NOT a player
  1553. {
  1554. // caster is NPC and in a spawn group with other NPCs
  1555. vector<Spawn*>* group = ((NPC*)caster)->GetSpawnGroup();
  1556. if (group)
  1557. {
  1558. vector<Spawn*>::iterator itr;
  1559. for (itr = group->begin(); itr != group->end(); itr++)
  1560. {
  1561. Spawn* group_member = *itr;
  1562. if (group_member->IsNPC() && group_member->Alive()){
  1563. luaspell->targets.push_back(group_member->GetID());
  1564. if (((Entity*)group_member)->HasPet()){
  1565. Entity* pet = ((Entity*)group_member)->GetPet();
  1566. if (pet)
  1567. luaspell->targets.push_back(pet->GetID());
  1568. pet = ((Entity*)group_member)->GetCharmedPet();
  1569. if (pet)
  1570. luaspell->targets.push_back(pet->GetID());
  1571. }
  1572. }
  1573. }
  1574. }
  1575. else
  1576. luaspell->targets.push_back(caster->GetID());
  1577. safe_delete(group);
  1578. } // end is player
  1579. } // end is friendly
  1580. } // end is Group AE
  1581. else if (target_type == SPELL_TARGET_SELF && caster)
  1582. luaspell->targets.push_back(caster->GetID()); // if spell is SELF, return caster
  1583. else if (target_type == SPELL_TARGET_CASTER_PET && caster && caster->IsEntity() && ((Entity*)caster)->HasPet()) {
  1584. if (((Entity*)caster)->GetPet())
  1585. luaspell->targets.push_back(((Entity*)caster)->GetPet()->GetID());
  1586. if (((Entity*)caster)->GetCharmedPet())
  1587. luaspell->targets.push_back(((Entity*)caster)->GetCharmedPet()->GetID());
  1588. }
  1589. else if (target_type == SPELL_TARGET_ENEMY && target && target->Alive()) // if target is enemy, and is alive
  1590. {
  1591. // if friendly spell
  1592. if (data->friendly_spell > 0)
  1593. {
  1594. // if caster is a player
  1595. if (caster->IsPlayer())
  1596. {
  1597. // if spell can affect raid, only group members or is a group spell
  1598. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  1599. {
  1600. // if caster is in a group, and target is a player and targeted player is a group member
  1601. if (((Player*)caster)->GetGroupMemberInfo() && (target->IsPlayer() || target->IsBot()) && ((Player*)caster)->IsGroupMember((Entity*)target))
  1602. luaspell->targets.push_back(target->GetID()); // return the target
  1603. else
  1604. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1605. }
  1606. else if (target->IsPlayer() || target->IsBot()) // else it is not raid, group only or group spell
  1607. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1608. else
  1609. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1610. }
  1611. else if (caster->IsNPC()) // caster is an NPC
  1612. {
  1613. // if NPC's spell can affect raid, only group members or is a group spell
  1614. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  1615. {
  1616. if (caster->IsBot() && (target->IsBot() || target->IsPlayer())) {
  1617. GroupMemberInfo* gmi = ((Entity*)caster)->GetGroupMemberInfo();
  1618. if (gmi && target->IsEntity() && world.GetGroupManager()->IsInGroup(gmi->group_id, (Entity*)target)) {
  1619. luaspell->targets.push_back(target->GetID()); // return the target
  1620. }
  1621. else
  1622. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1623. }
  1624. // if NPC caster is in a group, and target is a player and targeted player is a group member
  1625. else if (((NPC*)caster)->HasSpawnGroup() && target->IsNPC() && ((NPC*)caster)->IsInSpawnGroup((NPC*)target))
  1626. luaspell->targets.push_back(target->GetID()); // return the target
  1627. else
  1628. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1629. }
  1630. else if (target->IsNPC())
  1631. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1632. else {
  1633. if (caster->IsBot() && (target->IsBot() || target->IsPlayer()))
  1634. luaspell->targets.push_back(target->GetID());
  1635. else
  1636. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1637. }
  1638. } // end is player
  1639. } // end is friendly
  1640. else if (data->group_spell > 0) // is not friendly, but is a group spell
  1641. {
  1642. // target is non-player
  1643. if (target->IsNPC())
  1644. {
  1645. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  1646. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  1647. if (group)
  1648. {
  1649. vector<Spawn*>::iterator itr;
  1650. // iterate through spawn group members
  1651. for (itr = group->begin(); itr != group->end(); itr++)
  1652. {
  1653. Spawn* group_member = *itr;
  1654. // 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
  1655. if (group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune())
  1656. luaspell->targets.push_back(group_member->GetID());
  1657. // note: this should generate some hate towards the caster
  1658. }
  1659. } // end is spawngroup
  1660. else
  1661. luaspell->targets.push_back(target->GetID()); // return single target NPC for non-friendly spell
  1662. safe_delete(group);
  1663. } // end is NPC
  1664. else if (target->IsPlayer() && caster->IsNPC()) // the NPC is casting on a player
  1665. {
  1666. // player is in a group
  1667. if (((Player*)target)->GetGroupMemberInfo())
  1668. {
  1669. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1670. deque<GroupMemberInfo*>::iterator itr;
  1671. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1672. // iterate through players group members
  1673. for (itr = members->begin(); itr != members->end(); itr++)
  1674. {
  1675. Entity* group_member = (*itr)->member;
  1676. // if the group member is in the same zone as caster, and group member is alive, and group member is within distance
  1677. if (group_member->GetZone() == caster->GetZone() && group_member->Alive() && caster->GetDistance(group_member) <= data->range
  1678. && (group_member == target || !group_member->IsAOEImmune()))
  1679. luaspell->targets.push_back(group_member->GetID()); // add as target
  1680. }
  1681. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1682. }
  1683. else
  1684. luaspell->targets.push_back(target->GetID()); // player not in group
  1685. } // end is caster player or npc
  1686. }
  1687. else
  1688. luaspell->targets.push_back(target->GetID()); // is not friendly nor a group spell
  1689. }
  1690. //Rez spells
  1691. else if(target_type == SPELL_TARGET_ENEMY_CORPSE){
  1692. //is friendly
  1693. if(data->friendly_spell){
  1694. //target is player
  1695. if(target->IsPlayer()){
  1696. luaspell->targets.push_back(target->GetID());
  1697. }
  1698. }
  1699. }
  1700. else if(target_type == SPELL_TARGET_GROUP_CORPSE){
  1701. //is friendly
  1702. if(data->friendly_spell){
  1703. //target is player
  1704. if(target && target->IsPlayer()){
  1705. //if target has group
  1706. if(((Player*)target)->GetGroupMemberInfo()) {
  1707. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1708. deque<GroupMemberInfo*>::iterator itr;
  1709. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1710. Entity* group_member = 0;
  1711. for(itr = members->begin(); itr != members->end(); itr++) {
  1712. group_member = (*itr)->member;
  1713. //Check if group member is in the same zone in range of the spell and dead
  1714. if(group_member->GetZone() == target->GetZone() && !group_member->Alive() && target->GetDistance(group_member) <= data->radius){
  1715. luaspell->targets.push_back(group_member->GetID());
  1716. }
  1717. }
  1718. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1719. }
  1720. else
  1721. luaspell->targets.push_back(target->GetID());
  1722. }
  1723. }
  1724. }
  1725. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1726. }
  1727. if (luaspell && luaspell->targets.size() > 20)
  1728. LogWrite(SPELL__WARNING, 0, "Spell", "Warning in %s: Size of targets array is %u", __FUNCTION__, luaspell->targets.size());
  1729. }
  1730. void SpellProcess::GetSpellTargetsTrueAOE(LuaSpell* luaspell) {
  1731. if (luaspell && luaspell->caster && luaspell->spell && luaspell->spell->GetSpellData()->max_aoe_targets > 0) {
  1732. if (luaspell->caster->HasTarget() && luaspell->caster->GetTarget() != luaspell->caster){
  1733. //Check if the caster has an implied target
  1734. if (luaspell->caster->GetDistance(luaspell->caster->GetTarget()) <= luaspell->spell->GetSpellData()->radius)
  1735. luaspell->initial_target = luaspell->caster->GetTarget()->GetID();
  1736. }
  1737. int32 ignore_target = 0;
  1738. vector<Spawn*> spawns = luaspell->caster->GetZone()->GetAttackableSpawnsByDistance(luaspell->caster, luaspell->spell->GetSpellData()->radius);
  1739. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1740. for (int8 i = 0; i < spawns.size(); i++) {
  1741. Spawn* spawn = spawns.at(i);
  1742. if (i == 0){
  1743. if (luaspell->initial_target && luaspell->caster->GetID() != luaspell->initial_target){
  1744. //this is the "Direct" target and aoe can't be avoided
  1745. luaspell->targets.push_back(luaspell->initial_target);
  1746. ignore_target = luaspell->initial_target;
  1747. }
  1748. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1749. break;
  1750. }
  1751. //If we have already added this spawn, check the next spawn in the list
  1752. if (spawn && spawn->GetID() == ignore_target){
  1753. i++;
  1754. if (i < spawns.size())
  1755. spawn = spawns.at(i);
  1756. else
  1757. break;
  1758. }
  1759. if (spawn){
  1760. //If this spawn is immune to aoe, continue
  1761. if (((Entity*)spawn)->IsAOEImmune())
  1762. continue;
  1763. luaspell->targets.push_back(spawn->GetID());
  1764. }
  1765. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1766. break;
  1767. }
  1768. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1769. }
  1770. if (luaspell->targets.size() > 20)
  1771. LogWrite(SPELL__DEBUG, 0, "Spell", "Warning in SpellProcess::GetSpellTargetsTrueAOE Size of targets array is %u", luaspell->targets.size());
  1772. }
  1773. void SpellProcess::AddSpellScriptTimer(SpellScriptTimer* timer) {
  1774. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1775. m_spellScriptList.push_back(timer);
  1776. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1777. }
  1778. void SpellProcess::RemoveSpellScriptTimer(SpellScriptTimer* timer) {
  1779. vector<SpellScriptTimer*>::iterator itr;
  1780. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1781. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1782. if ((*itr) == timer) {
  1783. if ((*itr) && (*itr)->deleteWhenDone && lua_interface) {
  1784. lua_interface->AddPendingSpellDelete(timer->spell);
  1785. }
  1786. m_spellScriptList.erase(itr);
  1787. break;
  1788. }
  1789. }
  1790. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1791. }
  1792. void SpellProcess::CheckSpellScriptTimers() {
  1793. vector<SpellScriptTimer*>::iterator itr;
  1794. vector<SpellScriptTimer*> temp_list;
  1795. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1796. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1797. if (Timer::GetCurrentTime2() >= (*itr)->time) {
  1798. temp_list.push_back((*itr));
  1799. ProcessSpell((*itr)->spell, false, (*itr)->customFunction.c_str(), (*itr));
  1800. }
  1801. }
  1802. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1803. for (itr = temp_list.begin(); itr != temp_list.end(); itr++) {
  1804. RemoveSpellScriptTimer(*itr);
  1805. safe_delete(*itr);
  1806. }
  1807. }
  1808. bool SpellProcess::SpellScriptTimersHasSpell(LuaSpell* spell) {
  1809. bool ret = false;
  1810. vector<SpellScriptTimer*>::iterator itr;
  1811. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1812. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1813. SpellScriptTimer* timer = *itr;
  1814. if (timer && timer->spell == spell) {
  1815. ret = true;
  1816. break;
  1817. }
  1818. }
  1819. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1820. return ret;
  1821. }
  1822. void SpellProcess::ClearSpellScriptTimerList() {
  1823. vector<SpellScriptTimer*>::iterator itr;
  1824. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1825. for(itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1826. if ((*itr) && (*itr)->deleteWhenDone && lua_interface)
  1827. lua_interface->AddPendingSpellDelete((*itr)->spell);
  1828. safe_delete((*itr));
  1829. }
  1830. m_spellScriptList.clear();
  1831. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1832. }
  1833. void SpellProcess::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target){
  1834. if (!spell || !target)
  1835. return;
  1836. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1837. if (!remove_target_list[spell])
  1838. remove_target_list[spell] = new vector<Spawn*>;
  1839. remove_target_list[spell]->push_back(target);
  1840. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1841. }
  1842. void SpellProcess::CheckRemoveTargetFromSpell(LuaSpell* spell, bool allow_delete){
  1843. if (!spell)
  1844. return;
  1845. if (remove_target_list.size() > 0){
  1846. map<LuaSpell*, vector<Spawn*>*>::iterator remove_itr;
  1847. vector<Spawn*>::iterator remove_target_itr;
  1848. vector<int32>::iterator target_itr;
  1849. vector<int32>* targets;
  1850. vector<Spawn*>* remove_targets = 0;
  1851. Spawn* remove_spawn = 0;
  1852. bool should_delete = false;
  1853. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1854. for (remove_itr = remove_target_list.begin(); remove_itr != remove_target_list.end(); remove_itr++){
  1855. if (remove_itr->first == spell){
  1856. targets = &spell->targets;
  1857. remove_targets = remove_itr->second;
  1858. if (remove_targets && targets){
  1859. for (remove_target_itr = remove_targets->begin(); remove_target_itr != remove_targets->end(); remove_target_itr++){
  1860. remove_spawn = (*remove_target_itr);
  1861. if (remove_spawn){
  1862. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1863. for (target_itr = targets->begin(); target_itr != targets->end(); target_itr++){
  1864. if (remove_spawn->GetID() == (*target_itr)){
  1865. targets->erase(target_itr);
  1866. if (remove_spawn->IsEntity())
  1867. ((Entity*)remove_spawn)->RemoveEffectsFromLuaSpell(spell);
  1868. break;
  1869. }
  1870. }
  1871. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1872. if (targets->size() == 0 && allow_delete){
  1873. should_delete = true;
  1874. break;
  1875. }
  1876. }
  1877. }
  1878. }
  1879. break;
  1880. }
  1881. }
  1882. remove_target_list.erase(spell);
  1883. if (remove_targets)
  1884. remove_targets->clear();
  1885. safe_delete(remove_targets);
  1886. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1887. if (should_delete)
  1888. DeleteCasterSpell(spell);
  1889. }
  1890. }
  1891. bool SpellProcess::AddHO(Client* client, HeroicOP* ho) {
  1892. bool ret = true;
  1893. if (client && ho) {
  1894. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1895. if (m_soloHO.count(client) > 0) {
  1896. if (m_soloHO[client]->GetWheel()) {
  1897. ret = false;
  1898. }
  1899. else {
  1900. safe_delete(m_soloHO[client]);
  1901. m_soloHO[client] = ho;
  1902. }
  1903. }
  1904. else
  1905. m_soloHO[client] = ho;
  1906. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1907. }
  1908. return ret;
  1909. }
  1910. bool SpellProcess::AddHO(int32 group_id, HeroicOP* ho) {
  1911. bool ret = true;
  1912. if (group_id > 0 && ho) {
  1913. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1914. if (m_groupHO.count(group_id) > 0) {
  1915. if (m_groupHO[group_id]->GetWheel()) {
  1916. ret = false;
  1917. }
  1918. else {
  1919. safe_delete(m_groupHO[group_id]);
  1920. m_groupHO[group_id] = ho;
  1921. }
  1922. }
  1923. else
  1924. m_groupHO[group_id] = ho;
  1925. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1926. }
  1927. return ret;
  1928. }
  1929. void SpellProcess::KillHOBySpawnID(int32 spawn_id) {
  1930. // Check solo HO's
  1931. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1932. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  1933. map<Client*, HeroicOP*>::iterator delete_itr;
  1934. while (itr != m_soloHO.end()) {
  1935. if (itr->second->GetTarget() == spawn_id) {
  1936. itr->second->SetComplete(1);
  1937. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  1938. delete_itr = itr;
  1939. safe_delete(itr->second);
  1940. itr++;
  1941. m_soloHO.erase(delete_itr);
  1942. }
  1943. else
  1944. itr++;
  1945. }
  1946. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1947. // Check Group HO's
  1948. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1949. map<int32, HeroicOP*>::iterator itr2 = m_groupHO.begin();
  1950. map<int32, HeroicOP*>::iterator delete_itr2;
  1951. while (itr2 != m_groupHO.end()) {
  1952. if (itr2->second->GetTarget() == spawn_id) {
  1953. itr2->second->SetComplete(1);
  1954. world.GetGroupManager()->GroupLock(__FUNCTION__ , __LINE__);
  1955. deque<GroupMemberInfo*>::iterator itr3;
  1956. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(itr2->first);
  1957. for (itr3 = members->begin(); itr3 != members->end(); itr3++) {
  1958. if ((*itr3)->client)
  1959. ClientPacketFunctions::SendHeroicOPUpdate((*itr3)->client, itr2->second);
  1960. }
  1961. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1962. delete_itr2 = itr2;
  1963. safe_delete(itr2->second);
  1964. itr2++;
  1965. m_groupHO.erase(delete_itr2);
  1966. }
  1967. else
  1968. itr2++;
  1969. }
  1970. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1971. }
  1972. void SpellProcess::AddSpellCancel(LuaSpell* spell){
  1973. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  1974. SpellCancelList.push_back(spell);
  1975. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  1976. }