SpellProcess.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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->IsMezzed() && !spell->CastWhileMezzed()) || (caster->IsStunned() && !spell->CastWhileStunned()))
  805. {
  806. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (mezzed or stunned).", caster->GetName());
  807. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STUNNED);
  808. safe_delete(lua_spell);
  809. return;
  810. }
  811. if (caster->IsStifled() && !spell->CastWhileStifled())
  812. {
  813. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (stifled).", caster->GetName());
  814. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_STIFFLED);
  815. safe_delete(lua_spell);
  816. return;
  817. }
  818. if (caster->IsFeared() && !spell->CastWhileFeared())
  819. {
  820. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot cast (feared).", caster->GetName());
  821. zone->SendSpellFailedPacket(client, SPELL_ERROR_CANNOT_CAST_FEARED);
  822. safe_delete(lua_spell);
  823. return;
  824. }
  825. if(caster->IsPlayer() && !IsReady(spell, caster))
  826. {
  827. LogWrite(SPELL__DEBUG, 1, "Spell", "Queuing spell for %s.", caster->GetName());
  828. CheckSpellQueue(spell, caster);
  829. safe_delete(lua_spell);
  830. return;
  831. }
  832. if (target_type != SPELL_TARGET_SELF && target_type != SPELL_TARGET_GROUP_AE && target_type != SPELL_TARGET_NONE && spell->GetSpellData()->max_aoe_targets == 0)
  833. {
  834. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not Self, Not Group AE, Not None, Max Targets = 0", spell->GetName());
  835. if (!target)
  836. {
  837. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  838. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  839. safe_delete(lua_spell);
  840. return;
  841. }
  842. if(caster->GetDistance(target) > spell->GetSpellData()->range)
  843. {
  844. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too far.", spell->GetName());
  845. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  846. safe_delete(lua_spell);
  847. return;
  848. }
  849. if (caster->GetDistance(target) < spell->GetSpellData()->min_range)
  850. {
  851. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Too close.", spell->GetName());
  852. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_CLOSE);
  853. safe_delete(lua_spell);
  854. return;
  855. }
  856. }
  857. if(target_type == SPELL_TARGET_SELF && spell->GetSpellData()->max_aoe_targets == 0)
  858. {
  859. if (harvest_spell)
  860. {
  861. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Harvest, Target Self, Max Targets = 0", spell->GetName());
  862. if (!target || !target->IsGroundSpawn())
  863. {
  864. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target or not groundspawn.", spell->GetName());
  865. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  866. safe_delete(lua_spell);
  867. return;
  868. }
  869. }
  870. else
  871. {
  872. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target '%s' is Caster '%s'.", spell->GetName(), ( target ) ? target->GetName() : "None", caster->GetName());
  873. target = caster;
  874. }
  875. }
  876. // Is enemy spell AND direct-damage (no AE)
  877. if (target_type == SPELL_TARGET_ENEMY && spell->GetSpellData()->max_aoe_targets == 0)
  878. {
  879. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target Enemy (%s) and Max AE Targets = 0.", spell->GetName(), target->GetName());
  880. if (spell->GetSpellData()->friendly_spell && target->IsPlayer() )
  881. {
  882. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Friendly, Target is Player (%s).", spell->GetName(), target->GetName());
  883. if (!target->IsEntity())
  884. {
  885. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  886. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  887. safe_delete(lua_spell);
  888. return;
  889. }
  890. /*if (target->appearance.attackable) {
  891. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  892. return;
  893. }*/
  894. }
  895. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && target->appearance.attackable)
  896. {
  897. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (friendly and NPC)", spell->GetName());
  898. if (target->IsPet())
  899. {
  900. if (((NPC*)target)->GetOwner()->IsNPC())
  901. {
  902. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  903. safe_delete(lua_spell);
  904. return;
  905. }
  906. }
  907. else if (target->IsBot() && (caster->IsPlayer() || caster->IsBot())) {
  908. // Needed so bots or player can cast friendly spells on bots
  909. }
  910. else
  911. {
  912. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  913. safe_delete(lua_spell);
  914. return;
  915. }
  916. }
  917. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && caster->IsNPC()) {
  918. // TODO: faction checks? some other checks to prevent an npc casting a friendly spell on another npc
  919. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: NPC Cast", spell->GetName());
  920. }
  921. else
  922. {
  923. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (catch all)", spell->GetName());
  924. if (!target->IsEntity())
  925. {
  926. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  927. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  928. safe_delete(lua_spell);
  929. return;
  930. }
  931. if (caster == target || (!target->IsPlayer() && !target->appearance.attackable))
  932. {
  933. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not an Enemy (Target: %s).", spell->GetName(), target->GetName());
  934. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  935. safe_delete(lua_spell);
  936. return;
  937. }
  938. if (!target->Alive())
  939. {
  940. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is not alive (Target: %s).", spell->GetName(), target->GetName());
  941. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ALIVE);
  942. safe_delete(lua_spell);
  943. return;
  944. }
  945. if (target->GetInvulnerable())
  946. {
  947. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is invulnerable (Target: %s).", spell->GetName(), target->GetName());
  948. zone->SendSpellFailedPacket(client, SPELL_ERROR_TARGET_INVULNERABLE);
  949. safe_delete(lua_spell);
  950. return;
  951. }
  952. if ((target->IsPlayer() || target->IsBot()) && (caster->IsPlayer() || caster->IsBot()))
  953. {
  954. bool attackAllowed = (Entity*)caster->AttackAllowed((Entity*)target, 0);
  955. if (!attackAllowed)
  956. {
  957. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is player and not attackable.", spell->GetName(), target->GetName());
  958. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  959. safe_delete(lua_spell);
  960. return;
  961. }
  962. }
  963. if (target->IsPet() && ((NPC*)target)->GetOwner() && ((NPC*)target)->GetOwner() == caster) {
  964. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is casters pet and not attackable by caster.", spell->GetName(), target->GetName());
  965. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  966. safe_delete(lua_spell);
  967. return;
  968. }
  969. }
  970. }
  971. if (lua_spell->targets.size() == 0 && spell->GetSpellData()->max_aoe_targets == 0)
  972. {
  973. LogWrite(SPELL__ERROR, 0, "Spell", "SpellProcess::ProcessSpell Unable to find any spell targets for spell '%s'.", spell->GetName());
  974. safe_delete(lua_spell);
  975. return;
  976. }
  977. if(target_type == SPELL_TARGET_ENEMY_CORPSE || target_type == SPELL_TARGET_GROUP_CORPSE)
  978. {
  979. if(target->Alive())
  980. {
  981. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_DEAD);
  982. safe_delete(lua_spell);
  983. return;
  984. }
  985. if(target->IsPlayer() && zone->GetClientBySpawn(target)->GetCurrentRez()->active){
  986. zone->SendSpellFailedPacket(client, SPELL_ERROR_ALREADY_CAST);
  987. safe_delete(lua_spell);
  988. return;
  989. }
  990. }
  991. if(!CheckPower(lua_spell))
  992. {
  993. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_POWER);
  994. safe_delete(lua_spell);
  995. return;
  996. }
  997. if (!CheckHP(lua_spell))
  998. {
  999. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_HEALTH);
  1000. safe_delete(lua_spell);
  1001. return;
  1002. }
  1003. if (!CheckSavagery(lua_spell))
  1004. {
  1005. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_SAVAGERY);
  1006. safe_delete(lua_spell);
  1007. return;
  1008. }
  1009. if (!CheckDissonance(lua_spell))
  1010. {
  1011. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_DISSONANCE);
  1012. safe_delete(lua_spell);
  1013. return;
  1014. }
  1015. if (!CheckConcentration(lua_spell))
  1016. {
  1017. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_CONC);
  1018. safe_delete(lua_spell);
  1019. return;
  1020. }
  1021. // Precast in lua
  1022. if (lua_interface) {
  1023. bool result = false;
  1024. lua_interface->AddSpawnPointers(lua_spell, false, true);
  1025. if (lua_pcall(lua_spell->state, 2, 2, 0) == 0) {
  1026. result = lua_interface->GetBooleanValue(lua_spell->state, 1);
  1027. int8 error = lua_interface->GetInt8Value(lua_spell->state, 2) == 0 ? SPELL_ERROR_CANNOT_PREPARE : lua_interface->GetInt8Value(lua_spell->state, 2);
  1028. lua_interface->ResetFunctionStack(lua_spell->state);
  1029. if (!result) {
  1030. zone->SendSpellFailedPacket(client, error);
  1031. safe_delete(lua_spell);
  1032. return;
  1033. }
  1034. }
  1035. else {
  1036. LogWrite(SPELL__DEBUG, 1, "Spell", "No precast function found for %s", lua_spell->spell->GetName());
  1037. lua_interface->ResetFunctionStack(lua_spell->state);
  1038. }
  1039. }
  1040. else
  1041. LogWrite(SPELL__DEBUG, 1, "Spell", "Unable to do precast check as there was no lua_interface");
  1042. //Apply casting speed mod
  1043. spell->ModifyCastTime(caster);
  1044. LockAllSpells(client);
  1045. //cancel stealth effects on cast
  1046. if(caster->IsStealthed() || caster->IsInvis())
  1047. caster->CancelAllStealth();
  1048. SendStartCast(lua_spell, client);
  1049. if(spell->GetSpellData()->cast_time > 0)
  1050. {
  1051. CastTimer* cast_timer = new CastTimer;
  1052. cast_timer->entity_command = 0;
  1053. cast_timer->target_id = target ? target->GetID() : 0;
  1054. cast_timer->spell = lua_spell;
  1055. cast_timer->spell->caster = caster;
  1056. cast_timer->delete_timer = false;
  1057. cast_timer->timer = new Timer(spell->GetSpellData()->cast_time * 10);
  1058. cast_timer->zone = zone;
  1059. cast_timers.Add(cast_timer);
  1060. if(caster)
  1061. caster->IsCasting(true);
  1062. }
  1063. else
  1064. {
  1065. if(!CastProcessedSpell(lua_spell))
  1066. {
  1067. safe_delete(lua_spell);
  1068. return;
  1069. }
  1070. }
  1071. if(caster)
  1072. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  1073. }
  1074. }
  1075. void SpellProcess::ProcessEntityCommand(ZoneServer* zone, EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock) {
  1076. if (zone && entity_command && caster && target && !target->IsPlayer()) {
  1077. Client* client = zone->GetClientBySpawn(caster);
  1078. if (caster->GetDistance(target) > entity_command->distance) {
  1079. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  1080. return;
  1081. }
  1082. if (entity_command->cast_time > 0) {
  1083. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  1084. if (packet) {
  1085. LockAllSpells(client);
  1086. packet->setDataByName("cast_time", entity_command->cast_time * 0.01);
  1087. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1088. EQ2Packet* outapp = packet->serialize();
  1089. client->QueuePacket(outapp);
  1090. safe_delete(packet);
  1091. CastTimer* cast_timer = new CastTimer;
  1092. cast_timer->caster = client;
  1093. cast_timer->target_id = target ? target->GetID() : 0;
  1094. cast_timer->entity_command = entity_command;
  1095. cast_timer->spell = 0;
  1096. cast_timer->delete_timer = false;
  1097. cast_timer->timer = new Timer(entity_command->cast_time * 10);
  1098. cast_timer->zone = zone;
  1099. cast_timers.Add(cast_timer);
  1100. caster->IsCasting(true);
  1101. }
  1102. }
  1103. else if (!CastProcessedEntityCommand(entity_command, client, target))
  1104. return;
  1105. if (entity_command && entity_command->spell_visual > 0)
  1106. caster->GetZone()->SendCastEntityCommandPacket(entity_command, caster->GetID(), target->GetID());
  1107. }
  1108. }
  1109. bool SpellProcess::CastProcessedSpell(LuaSpell* spell, bool passive){
  1110. if(!spell || !spell->caster || !spell->spell || spell->interrupted)
  1111. return false;
  1112. Client* client = 0;
  1113. if(spell->caster && spell->caster->IsPlayer())
  1114. client = spell->caster->GetZone()->GetClientBySpawn(spell->caster);
  1115. if (spell->spell->GetSpellData()->max_aoe_targets > 0 && spell->targets.size() == 0) {
  1116. GetSpellTargetsTrueAOE(spell);
  1117. if (spell->targets.size() == 0) {
  1118. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_NO_TARGETS_IN_RANGE);
  1119. return false;
  1120. }
  1121. }
  1122. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1123. bool processedSpell = false;
  1124. LuaSpell* replace_spell = 0;
  1125. // Check to see if we already casted this spell and it is in the active_spells list
  1126. /*while (itr.Next()) {
  1127. LuaSpell* luaspell = itr.value;
  1128. // Check to see if this is the same spell by comparing caster, spell id, and spell tier
  1129. if (luaspell->caster == spell->caster && luaspell->spell->GetSpellID() == spell->spell->GetSpellID() && luaspell->spell->GetSpellTier() == spell->spell->GetSpellTier() && luaspell->targets.size() == spell->targets.size()) {
  1130. vector<Spawn*>::iterator itr;
  1131. vector<Spawn*>::iterator itr2;
  1132. bool all_match = true;
  1133. // compare the target vector to make sure this is the same spell
  1134. for (itr = luaspell->targets.begin(); itr != luaspell->targets.end(); itr++) {
  1135. bool match = false;
  1136. for (itr2 = spell->targets.begin(); itr2 != spell->targets.end(); itr2++) {
  1137. if ((*itr) == (*itr2)) {
  1138. match = true;
  1139. break;
  1140. }
  1141. }
  1142. if (!match) {
  1143. all_match = false;
  1144. break;
  1145. }
  1146. }
  1147. if (all_match) {
  1148. // set a pointer to replace the spell in the active spell list so we don't have to run this loop again
  1149. replace_spell = luaspell;
  1150. // if friendly skip the calling of the lua function as they have already been called for the previous version of this spell
  1151. if (spell->spell->GetSpellData()->friendly_spell == 1)
  1152. processedSpell = true;
  1153. break;
  1154. }
  1155. }
  1156. }*/
  1157. if (!processedSpell)
  1158. processedSpell = ProcessSpell(spell);
  1159. // Quick hack to prevent a crash on spells that zones the caster (Gate)
  1160. if (!spell->caster)
  1161. return true;
  1162. ZoneServer* zone = spell->caster->GetZone();
  1163. Spawn* target = 0;
  1164. if(processedSpell){
  1165. for (int32 i = 0; i < spell->targets.size(); i++) {
  1166. target = zone->GetSpawnByID(spell->targets[i]);
  1167. if (!target)
  1168. continue;
  1169. if (client && client->IsZoning())
  1170. continue;
  1171. if(spell->spell->GetSpellData()->success_message.length() > 0){
  1172. if(client){
  1173. string success_message = spell->spell->GetSpellData()->success_message;
  1174. if(success_message.find("%t") != string::npos)
  1175. success_message.replace(success_message.find("%t"), 2, spell->caster->GetName());
  1176. client->Message(CHANNEL_COLOR_SPELL, success_message.c_str());
  1177. }
  1178. }
  1179. if(spell->spell->GetSpellData()->effect_message.length() > 0){
  1180. string effect_message = spell->spell->GetSpellData()->effect_message;
  1181. if(effect_message.find("%t") < 0xFFFFFFFF)
  1182. effect_message.replace(effect_message.find("%t"), 2, target->GetName());
  1183. if (effect_message.find("%c") != string::npos)
  1184. effect_message.replace(effect_message.find("%c"), 2, spell->caster->GetName());
  1185. spell->caster->GetZone()->SimpleMessage(CHANNEL_COLOR_SPELL_EFFECT, effect_message.c_str(), target, 50);
  1186. }
  1187. target->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, spell->caster, spell->spell->GetName());
  1188. }
  1189. }
  1190. else{
  1191. if (!passive)
  1192. SendFinishedCast(spell, client);
  1193. return false;
  1194. }
  1195. if(!spell->resisted && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel)) {
  1196. for (int32 i = 0; i < spell->targets.size(); i++) {
  1197. //LogWrite(SPELL__ERROR, 0, "Spell", "No precast function found for %s", ((Entity*)target)->GetName());
  1198. target = zone->GetSpawnByID(spell->targets.at(i));
  1199. if (i == 0 && !spell->spell->GetSpellData()->not_maintained) {
  1200. spell->caster->AddMaintainedSpell(spell);
  1201. //((Entity*)target)->AddMaintainedSpell(spell);
  1202. LogWrite(SPELL__DEBUG, 0, "Spell", "AddMaintained on %s", ((Entity*)target)->GetName());
  1203. }
  1204. SpellEffects* effect = ((Entity*)target)->GetSpellEffect(spell->spell->GetSpellID());
  1205. if (effect && effect->tier > spell->spell->GetSpellTier()) {
  1206. if(client) {
  1207. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  1208. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "The spell did not take hold as the target already has a better spell of this type.");
  1209. }
  1210. }
  1211. else{
  1212. ((Entity*)target)->AddSpellEffect(spell);
  1213. if(spell->spell->GetSpellData()->det_type > 0)
  1214. ((Entity*)target)->AddDetrimentalSpell(spell);
  1215. }
  1216. }
  1217. if (replace_spell) {
  1218. active_spells.Remove(replace_spell);
  1219. active_spells.Add(spell);
  1220. }
  1221. else {
  1222. active_spells.Add(spell);
  1223. }
  1224. if (spell->num_triggers > 0)
  1225. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->num_triggers, 0);
  1226. if (spell->damage_remaining > 0)
  1227. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->damage_remaining, 1);
  1228. spell->caster->GetZone()->TriggerCharSheetTimer();
  1229. }
  1230. spell->num_calls = 1;
  1231. if(!spell->resisted && spell->spell->GetSpellData()->duration1 > 0){
  1232. spell->timer.Start();
  1233. if(spell->spell->GetSpellData()->call_frequency > 0)
  1234. spell->timer.SetTimer(spell->spell->GetSpellData()->call_frequency*100);
  1235. else
  1236. spell->timer.SetTimer(spell->spell->GetSpellData()->duration1*100);
  1237. if (active_spells.count(spell) > 0) {
  1238. active_spells.Add(spell);
  1239. }
  1240. }
  1241. // if the caster is a player and the spell is a tradeskill spell check for a tradeskill event
  1242. if (client && spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL)
  1243. client->GetCurrentZone()->GetTradeskillMgr()->CheckTradeskillEvent(client, spell->spell->GetSpellData()->icon);
  1244. if (spell->spell->GetSpellData()->friendly_spell && zone->GetSpawnByID(spell->initial_target))
  1245. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, zone->GetSpawnByID(spell->initial_target));
  1246. // Check HO's
  1247. if (client) {
  1248. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1249. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1250. if (m_soloHO.count(client) > 0) {
  1251. bool match = false;
  1252. LogWrite(SPELL__ERROR, 0, "HO", "target = %u", m_soloHO[client]->GetTarget());
  1253. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1254. for (int8 i = 0; i < spell->targets.size(); i++) {
  1255. LogWrite(SPELL__ERROR, 0, "HO", "%u", spell->targets.at(i));
  1256. if (spell->targets.at(i) == m_soloHO[client]->GetTarget()) {
  1257. match = true;
  1258. LogWrite(SPELL__ERROR, 0, "HO", "match found");
  1259. break;
  1260. }
  1261. }
  1262. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1263. if (match && m_soloHO[client]->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1264. ClientPacketFunctions::SendHeroicOPUpdate(client, m_soloHO[client]);
  1265. if (m_soloHO[client]->GetComplete() > 0) {
  1266. Spell* ho_spell = master_spell_list.GetSpell(m_soloHO[client]->GetWheel()->spell_id, 1);
  1267. if (ho_spell)
  1268. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(m_soloHO[client]->GetTarget()));
  1269. else
  1270. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", m_soloHO[client]->GetWheel()->spell_id);
  1271. safe_delete(m_soloHO[client]);
  1272. m_soloHO.erase(client);
  1273. }
  1274. }
  1275. }
  1276. else if (client->GetPlayer()->GetGroupMemberInfo() && m_groupHO.count(client->GetPlayer()->GetGroupMemberInfo()->group_id)) {
  1277. int32 group_id = client->GetPlayer()->GetGroupMemberInfo()->group_id;
  1278. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1279. if (spell->targets.at(0) == m_groupHO[group_id]->GetTarget() && m_groupHO[group_id]->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1280. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1281. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1282. deque<GroupMemberInfo*>::iterator itr;
  1283. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(group_id);
  1284. for (itr = members->begin(); itr != members->end(); itr++) {
  1285. if ((*itr)->client)
  1286. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, m_groupHO[group_id]);
  1287. }
  1288. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1289. if (m_groupHO[group_id]->GetComplete() > 0) {
  1290. Spell* ho_spell = master_spell_list.GetSpell(m_groupHO[group_id]->GetWheel()->spell_id, 1);
  1291. if (ho_spell)
  1292. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(spell->targets.at(0)));
  1293. else
  1294. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", m_groupHO[group_id]->GetWheel()->spell_id);
  1295. safe_delete(m_groupHO[group_id]);
  1296. m_groupHO.erase(group_id);
  1297. }
  1298. }
  1299. else
  1300. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1301. }
  1302. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1303. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1304. }
  1305. if (spell->spell->GetSpellData()->friendly_spell == 1 && spell->initial_target)
  1306. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  1307. if (!passive)
  1308. SendFinishedCast(spell, client);
  1309. return true;
  1310. }
  1311. bool SpellProcess::CastProcessedEntityCommand(EntityCommand* entity_command, Client* client, Spawn* target) {
  1312. bool ret = false;
  1313. if (entity_command && client) {
  1314. UnlockAllSpells(client);
  1315. client->GetPlayer()->IsCasting(false);
  1316. if (entity_command->cast_time == 0) {
  1317. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1318. ret = true;;
  1319. }
  1320. if (!ret) {
  1321. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  1322. if (packet) {
  1323. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1324. client->QueuePacket(packet->serialize());
  1325. safe_delete(packet);
  1326. SendSpellBookUpdate(client);
  1327. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1328. ret = true;
  1329. }
  1330. }
  1331. if (ret) {
  1332. EQ2_16BitString command;
  1333. command.data = entity_command->command;
  1334. command.size = entity_command->command.length();
  1335. int32 handler = commands.GetCommandHandler(command.data.c_str());
  1336. if (handler != 0xFFFFFFFF && handler < 999)
  1337. commands.Process(handler, &command, client, target);
  1338. }
  1339. }
  1340. return ret;
  1341. }
  1342. void SpellProcess::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement)
  1343. {
  1344. if(caster)
  1345. {
  1346. LogWrite(SPELL__DEBUG, 0, "Spell", "'%s' is Interrupting spell of '%s'...", interruptor ? interruptor->GetName() : "unknown", caster->GetName());
  1347. LuaSpell* spell = GetLuaSpell(caster);
  1348. if (spell && ((from_movement && !spell->spell->GetSpellData()->cast_while_moving) || (!from_movement && spell->spell->GetSpellData()->interruptable) ||
  1349. cancel))
  1350. {
  1351. Spawn* target = GetSpellTarget(caster);
  1352. InterruptStruct* interrupt = new InterruptStruct;
  1353. interrupt->interrupted = caster;
  1354. interrupt->spell = spell;
  1355. interrupt->target = target;
  1356. interrupt->error_code = error_code;
  1357. spell->interrupted = true;
  1358. interrupt_list.Add(interrupt);
  1359. Client* client = 0;
  1360. if(interruptor && interruptor->IsPlayer())
  1361. {
  1362. client = interruptor->GetZone()->GetClientBySpawn(interruptor);
  1363. client->Message(CHANNEL_COLOR_SPELL_INTERRUPT, "You interrupt %s's ability to cast!", interruptor->GetName());
  1364. }
  1365. }
  1366. }
  1367. }
  1368. void SpellProcess::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast){
  1369. int32 i = 0;
  1370. if(cast_timers.size() > 0){
  1371. CastTimer* cast_timer = 0;
  1372. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  1373. while(itr.Next()){
  1374. cast_timer = itr->value;
  1375. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == spawn){
  1376. cast_timer->spell->caster = 0;
  1377. cast_timer->delete_timer = true;
  1378. }
  1379. }
  1380. }
  1381. if(remove_all){
  1382. LuaSpell* spell = 0;
  1383. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1384. while(itr.Next()){
  1385. spell = itr->value;
  1386. if (!spell)
  1387. continue;
  1388. if (spell->spell->GetSpellData()->persist_though_death)
  1389. continue;
  1390. if(spell->caster == spawn){
  1391. DeleteCasterSpell(spell);
  1392. continue;
  1393. }
  1394. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1395. for (i = 0; i < spell->targets.size(); i++){
  1396. if (spawn->GetID() == spell->targets.at(i)){
  1397. if (spawn->IsEntity())
  1398. ((Entity*)spawn)->RemoveSpellEffect(spell);
  1399. RemoveTargetFromSpell(spell, spawn);
  1400. break;
  1401. }
  1402. }
  1403. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1404. }
  1405. if(recast_timers.size() > 0 && delete_recast){
  1406. RecastTimer* recast_timer = 0;
  1407. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  1408. while(itr.Next()){
  1409. recast_timer = itr->value;
  1410. if(recast_timer && recast_timer->caster == spawn){
  1411. safe_delete(recast_timer->timer);
  1412. recast_timers.Remove(recast_timer, true);
  1413. }
  1414. }
  1415. }
  1416. if(spell_que.size() > 0 && spawn->IsEntity()){
  1417. spell_que.erase((Entity*)spawn);
  1418. }
  1419. if(interrupt_list.size() > 0){
  1420. InterruptStruct* interrupt = 0;
  1421. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  1422. while(itr.Next()){
  1423. interrupt = itr->value;
  1424. if(interrupt && interrupt->interrupted == spawn){
  1425. interrupt_list.Remove(interrupt);
  1426. }
  1427. }
  1428. }
  1429. }
  1430. }
  1431. void SpellProcess::GetSpellTargets(LuaSpell* luaspell)
  1432. {
  1433. if (luaspell && luaspell->spell && luaspell->caster && luaspell->spell->GetSpellData()->max_aoe_targets == 0)
  1434. {
  1435. int8 target_type = luaspell->spell->GetSpellData()->target_type;
  1436. Spawn* caster = luaspell->caster;
  1437. Spawn* target = caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  1438. SpellData* data = luaspell->spell->GetSpellData();
  1439. bool implied = false;
  1440. Spawn* secondary_target = nullptr;
  1441. //implied target check -- only use this for players
  1442. 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))
  1443. {
  1444. if (caster->IsPlayer() && target->HasTarget())
  1445. {
  1446. secondary_target = target->GetTarget();
  1447. if (secondary_target) {
  1448. // check if spell is friendly
  1449. if (data->friendly_spell) {
  1450. //if target is NPC (and not a bot) on friendly spell, check to see if target is friendly
  1451. if (target->IsNPC() && !target->IsBot()) {
  1452. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1453. if (secondary_target->IsPlayer()) {
  1454. implied = true;
  1455. }
  1456. else if (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsPlayer())
  1457. implied = true;
  1458. }
  1459. }
  1460. } // if spell is not friendly
  1461. else { // check if there is an implied target for this non-friendly spell
  1462. if (target->IsPlayer() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsPlayer())) {
  1463. if (secondary_target->IsNPC()) {
  1464. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1465. implied = true;
  1466. }
  1467. }
  1468. }
  1469. } // end friendly spell check
  1470. }
  1471. }
  1472. else if (caster->IsPlayer()) {
  1473. if (data->friendly_spell) {
  1474. if (target->IsNPC() && !target->IsBot()) {
  1475. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1476. target = caster;
  1477. luaspell->initial_target = caster->GetID();
  1478. }
  1479. }
  1480. }
  1481. }
  1482. }
  1483. else if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE) {
  1484. target = caster;
  1485. luaspell->initial_target = caster->GetID();
  1486. }
  1487. else if (target_type == SPELL_TARGET_SELF){
  1488. target = caster;
  1489. luaspell->initial_target = caster->GetID();
  1490. }
  1491. //if using implied target, target = the implied target
  1492. if (implied)
  1493. {
  1494. target = secondary_target;
  1495. luaspell->initial_target = secondary_target->GetID();
  1496. }
  1497. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1498. // Group AE type NOTE: Add support for RAID AE to affect raid members once raids have been completed
  1499. if (target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE)
  1500. {
  1501. // is friendly
  1502. if (data->friendly_spell)
  1503. {
  1504. // caster is an Entity
  1505. if (luaspell->caster->IsEntity())
  1506. {
  1507. // entity caster is in a player group
  1508. if (((Entity*)caster)->GetGroupMemberInfo())
  1509. {
  1510. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1511. // get group members
  1512. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  1513. deque<GroupMemberInfo*>::iterator itr;
  1514. // iterate through list of group members
  1515. for (itr = members->begin(); itr != members->end(); itr++)
  1516. {
  1517. // get group member player info
  1518. Entity* group_member = (*itr)->member;
  1519. // if the group member is in the casters zone, and is alive
  1520. if (group_member->GetZone() == luaspell->caster->GetZone() && group_member->Alive()){
  1521. luaspell->targets.push_back(group_member->GetID());
  1522. if (group_member->HasPet()){
  1523. Entity* pet = group_member->GetPet();
  1524. if (!pet)
  1525. pet = group_member->GetCharmedPet();
  1526. if (pet)
  1527. luaspell->targets.push_back(pet->GetID());
  1528. }
  1529. }
  1530. }
  1531. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1532. }
  1533. else
  1534. luaspell->targets.push_back(caster->GetID()); // else caster is not in a group, thus alone
  1535. }
  1536. else if (caster->IsNPC()) // caster is NOT a player
  1537. {
  1538. // caster is NPC and in a spawn group with other NPCs
  1539. vector<Spawn*>* group = ((NPC*)caster)->GetSpawnGroup();
  1540. if (group)
  1541. {
  1542. vector<Spawn*>::iterator itr;
  1543. for (itr = group->begin(); itr != group->end(); itr++)
  1544. {
  1545. Spawn* group_member = *itr;
  1546. if (group_member->IsNPC() && group_member->Alive()){
  1547. luaspell->targets.push_back(group_member->GetID());
  1548. if (((Entity*)group_member)->HasPet()){
  1549. Entity* pet = ((Entity*)group_member)->GetPet();
  1550. if (pet)
  1551. luaspell->targets.push_back(pet->GetID());
  1552. pet = ((Entity*)group_member)->GetCharmedPet();
  1553. if (pet)
  1554. luaspell->targets.push_back(pet->GetID());
  1555. }
  1556. }
  1557. }
  1558. }
  1559. else
  1560. luaspell->targets.push_back(caster->GetID());
  1561. safe_delete(group);
  1562. } // end is player
  1563. } // end is friendly
  1564. } // end is Group AE
  1565. else if (target_type == SPELL_TARGET_SELF && caster)
  1566. luaspell->targets.push_back(caster->GetID()); // if spell is SELF, return caster
  1567. else if (target_type == SPELL_TARGET_CASTER_PET && caster && caster->IsEntity() && ((Entity*)caster)->HasPet()) {
  1568. if (((Entity*)caster)->GetPet())
  1569. luaspell->targets.push_back(((Entity*)caster)->GetPet()->GetID());
  1570. if (((Entity*)caster)->GetCharmedPet())
  1571. luaspell->targets.push_back(((Entity*)caster)->GetCharmedPet()->GetID());
  1572. }
  1573. else if (target_type == SPELL_TARGET_ENEMY && target && target->Alive()) // if target is enemy, and is alive
  1574. {
  1575. // if friendly spell
  1576. if (data->friendly_spell > 0)
  1577. {
  1578. // if caster is a player
  1579. if (caster->IsPlayer())
  1580. {
  1581. // if spell can affect raid, only group members or is a group spell
  1582. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  1583. {
  1584. // if caster is in a group, and target is a player and targeted player is a group member
  1585. if (((Player*)caster)->GetGroupMemberInfo() && (target->IsPlayer() || target->IsBot()) && ((Player*)caster)->IsGroupMember((Entity*)target))
  1586. luaspell->targets.push_back(target->GetID()); // return the target
  1587. else
  1588. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1589. }
  1590. else if (target->IsPlayer() || target->IsBot()) // else it is not raid, group only or group spell
  1591. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1592. else
  1593. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1594. }
  1595. else if (caster->IsNPC()) // caster is an NPC
  1596. {
  1597. // if NPC's 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->IsBot() && (target->IsBot() || target->IsPlayer())) {
  1601. GroupMemberInfo* gmi = ((Entity*)caster)->GetGroupMemberInfo();
  1602. if (gmi && target->IsEntity() && world.GetGroupManager()->IsInGroup(gmi->group_id, (Entity*)target)) {
  1603. luaspell->targets.push_back(target->GetID()); // return the target
  1604. }
  1605. else
  1606. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1607. }
  1608. // if NPC caster is in a group, and target is a player and targeted player is a group member
  1609. else if (((NPC*)caster)->HasSpawnGroup() && target->IsNPC() && ((NPC*)caster)->IsInSpawnGroup((NPC*)target))
  1610. luaspell->targets.push_back(target->GetID()); // return the target
  1611. else
  1612. luaspell->targets.push_back(caster->GetID()); // else return the caster
  1613. }
  1614. else if (target->IsNPC())
  1615. luaspell->targets.push_back(target->GetID()); // return target for single spell
  1616. else {
  1617. if (caster->IsBot() && (target->IsBot() || target->IsPlayer()))
  1618. luaspell->targets.push_back(target->GetID());
  1619. else
  1620. luaspell->targets.push_back(caster->GetID()); // and if no target, cast on self
  1621. }
  1622. } // end is player
  1623. } // end is friendly
  1624. else if (data->group_spell > 0) // is not friendly, but is a group spell
  1625. {
  1626. // target is non-player
  1627. if (target->IsNPC())
  1628. {
  1629. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  1630. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  1631. if (group)
  1632. {
  1633. vector<Spawn*>::iterator itr;
  1634. // iterate through spawn group members
  1635. for (itr = group->begin(); itr != group->end(); itr++)
  1636. {
  1637. Spawn* group_member = *itr;
  1638. // 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
  1639. if (group_member->IsNPC() && group_member->Alive() && group_member != target && !((Entity*)group_member)->IsAOEImmune())
  1640. luaspell->targets.push_back(group_member->GetID());
  1641. // note: this should generate some hate towards the caster
  1642. }
  1643. } // end is spawngroup
  1644. else
  1645. luaspell->targets.push_back(target->GetID()); // return single target NPC for non-friendly spell
  1646. safe_delete(group);
  1647. } // end is NPC
  1648. else if (target->IsPlayer() && caster->IsNPC()) // the NPC is casting on a player
  1649. {
  1650. // player is in a group
  1651. if (((Player*)target)->GetGroupMemberInfo())
  1652. {
  1653. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1654. deque<GroupMemberInfo*>::iterator itr;
  1655. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1656. // iterate through players group members
  1657. for (itr = members->begin(); itr != members->end(); itr++)
  1658. {
  1659. Entity* group_member = (*itr)->member;
  1660. // if the group member is in the same zone as caster, and group member is alive, and group member is within distance
  1661. if (group_member->GetZone() == caster->GetZone() && group_member->Alive() && caster->GetDistance(group_member) <= data->range
  1662. && (group_member == target || !group_member->IsAOEImmune()))
  1663. luaspell->targets.push_back(group_member->GetID()); // add as target
  1664. }
  1665. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1666. }
  1667. else
  1668. luaspell->targets.push_back(target->GetID()); // player not in group
  1669. } // end is caster player or npc
  1670. }
  1671. else
  1672. luaspell->targets.push_back(target->GetID()); // is not friendly nor a group spell
  1673. }
  1674. //Rez spells
  1675. else if(target_type == SPELL_TARGET_ENEMY_CORPSE){
  1676. //is friendly
  1677. if(data->friendly_spell){
  1678. //target is player
  1679. if(target->IsPlayer()){
  1680. luaspell->targets.push_back(target->GetID());
  1681. }
  1682. }
  1683. }
  1684. else if(target_type == SPELL_TARGET_GROUP_CORPSE){
  1685. //is friendly
  1686. if(data->friendly_spell){
  1687. //target is player
  1688. if(target && target->IsPlayer()){
  1689. //if target has group
  1690. if(((Player*)target)->GetGroupMemberInfo()) {
  1691. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1692. deque<GroupMemberInfo*>::iterator itr;
  1693. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(((Player*)target)->GetGroupMemberInfo()->group_id);
  1694. Entity* group_member = 0;
  1695. for(itr = members->begin(); itr != members->end(); itr++) {
  1696. group_member = (*itr)->member;
  1697. //Check if group member is in the same zone in range of the spell and dead
  1698. if(group_member->GetZone() == target->GetZone() && !group_member->Alive() && target->GetDistance(group_member) <= data->radius){
  1699. luaspell->targets.push_back(group_member->GetID());
  1700. }
  1701. }
  1702. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1703. }
  1704. else
  1705. luaspell->targets.push_back(target->GetID());
  1706. }
  1707. }
  1708. }
  1709. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1710. }
  1711. if (luaspell && luaspell->targets.size() > 20)
  1712. LogWrite(SPELL__WARNING, 0, "Spell", "Warning in %s: Size of targets array is %u", __FUNCTION__, luaspell->targets.size());
  1713. }
  1714. void SpellProcess::GetSpellTargetsTrueAOE(LuaSpell* luaspell) {
  1715. if (luaspell && luaspell->caster && luaspell->spell && luaspell->spell->GetSpellData()->max_aoe_targets > 0) {
  1716. if (luaspell->caster->HasTarget() && luaspell->caster->GetTarget() != luaspell->caster){
  1717. //Check if the caster has an implied target
  1718. if (luaspell->caster->GetDistance(luaspell->caster->GetTarget()) <= luaspell->spell->GetSpellData()->radius)
  1719. luaspell->initial_target = luaspell->caster->GetTarget()->GetID();
  1720. }
  1721. int32 ignore_target = 0;
  1722. vector<Spawn*> spawns = luaspell->caster->GetZone()->GetAttackableSpawnsByDistance(luaspell->caster, luaspell->spell->GetSpellData()->radius);
  1723. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1724. for (int8 i = 0; i < spawns.size(); i++) {
  1725. Spawn* spawn = spawns.at(i);
  1726. if (i == 0){
  1727. if (luaspell->initial_target && luaspell->caster->GetID() != luaspell->initial_target){
  1728. //this is the "Direct" target and aoe can't be avoided
  1729. luaspell->targets.push_back(luaspell->initial_target);
  1730. ignore_target = luaspell->initial_target;
  1731. }
  1732. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1733. break;
  1734. }
  1735. //If we have already added this spawn, check the next spawn in the list
  1736. if (spawn && spawn->GetID() == ignore_target){
  1737. i++;
  1738. if (i < spawns.size())
  1739. spawn = spawns.at(i);
  1740. else
  1741. break;
  1742. }
  1743. if (spawn){
  1744. //If this spawn is immune to aoe, continue
  1745. if (((Entity*)spawn)->IsAOEImmune())
  1746. continue;
  1747. luaspell->targets.push_back(spawn->GetID());
  1748. }
  1749. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  1750. break;
  1751. }
  1752. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1753. }
  1754. if (luaspell->targets.size() > 20)
  1755. LogWrite(SPELL__DEBUG, 0, "Spell", "Warning in SpellProcess::GetSpellTargetsTrueAOE Size of targets array is %u", luaspell->targets.size());
  1756. }
  1757. void SpellProcess::AddSpellScriptTimer(SpellScriptTimer* timer) {
  1758. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1759. m_spellScriptList.push_back(timer);
  1760. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1761. }
  1762. void SpellProcess::RemoveSpellScriptTimer(SpellScriptTimer* timer) {
  1763. vector<SpellScriptTimer*>::iterator itr;
  1764. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1765. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1766. if ((*itr) == timer) {
  1767. if ((*itr) && (*itr)->deleteWhenDone && lua_interface) {
  1768. lua_interface->AddPendingSpellDelete(timer->spell);
  1769. }
  1770. m_spellScriptList.erase(itr);
  1771. break;
  1772. }
  1773. }
  1774. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1775. }
  1776. void SpellProcess::CheckSpellScriptTimers() {
  1777. vector<SpellScriptTimer*>::iterator itr;
  1778. vector<SpellScriptTimer*> temp_list;
  1779. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1780. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1781. if (Timer::GetCurrentTime2() >= (*itr)->time) {
  1782. temp_list.push_back((*itr));
  1783. ProcessSpell((*itr)->spell, false, (*itr)->customFunction.c_str(), (*itr));
  1784. }
  1785. }
  1786. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1787. for (itr = temp_list.begin(); itr != temp_list.end(); itr++) {
  1788. RemoveSpellScriptTimer(*itr);
  1789. safe_delete(*itr);
  1790. }
  1791. }
  1792. bool SpellProcess::SpellScriptTimersHasSpell(LuaSpell* spell) {
  1793. bool ret = false;
  1794. vector<SpellScriptTimer*>::iterator itr;
  1795. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  1796. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1797. SpellScriptTimer* timer = *itr;
  1798. if (timer && timer->spell == spell) {
  1799. ret = true;
  1800. break;
  1801. }
  1802. }
  1803. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  1804. return ret;
  1805. }
  1806. void SpellProcess::ClearSpellScriptTimerList() {
  1807. vector<SpellScriptTimer*>::iterator itr;
  1808. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  1809. for(itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  1810. if ((*itr) && (*itr)->deleteWhenDone && lua_interface)
  1811. lua_interface->AddPendingSpellDelete((*itr)->spell);
  1812. safe_delete((*itr));
  1813. }
  1814. m_spellScriptList.clear();
  1815. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  1816. }
  1817. void SpellProcess::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target){
  1818. if (!spell || !target)
  1819. return;
  1820. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1821. if (!remove_target_list[spell])
  1822. remove_target_list[spell] = new vector<Spawn*>;
  1823. remove_target_list[spell]->push_back(target);
  1824. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1825. }
  1826. void SpellProcess::CheckRemoveTargetFromSpell(LuaSpell* spell, bool allow_delete){
  1827. if (!spell)
  1828. return;
  1829. if (remove_target_list.size() > 0){
  1830. map<LuaSpell*, vector<Spawn*>*>::iterator remove_itr;
  1831. vector<Spawn*>::iterator remove_target_itr;
  1832. vector<int32>::iterator target_itr;
  1833. vector<int32>* targets;
  1834. vector<Spawn*>* remove_targets = 0;
  1835. Spawn* remove_spawn = 0;
  1836. bool should_delete = false;
  1837. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  1838. for (remove_itr = remove_target_list.begin(); remove_itr != remove_target_list.end(); remove_itr++){
  1839. if (remove_itr->first == spell){
  1840. targets = &spell->targets;
  1841. remove_targets = remove_itr->second;
  1842. if (remove_targets && targets){
  1843. for (remove_target_itr = remove_targets->begin(); remove_target_itr != remove_targets->end(); remove_target_itr++){
  1844. remove_spawn = (*remove_target_itr);
  1845. if (remove_spawn){
  1846. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1847. for (target_itr = targets->begin(); target_itr != targets->end(); target_itr++){
  1848. if (remove_spawn->GetID() == (*target_itr)){
  1849. targets->erase(target_itr);
  1850. if (remove_spawn->IsEntity())
  1851. ((Entity*)remove_spawn)->RemoveEffectsFromLuaSpell(spell);
  1852. break;
  1853. }
  1854. }
  1855. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  1856. if (targets->size() == 0 && allow_delete){
  1857. should_delete = true;
  1858. break;
  1859. }
  1860. }
  1861. }
  1862. }
  1863. break;
  1864. }
  1865. }
  1866. remove_target_list.erase(spell);
  1867. if (remove_targets)
  1868. remove_targets->clear();
  1869. safe_delete(remove_targets);
  1870. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  1871. if (should_delete)
  1872. DeleteCasterSpell(spell);
  1873. }
  1874. }
  1875. bool SpellProcess::AddHO(Client* client, HeroicOP* ho) {
  1876. bool ret = true;
  1877. if (client && ho) {
  1878. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1879. if (m_soloHO.count(client) > 0) {
  1880. if (m_soloHO[client]->GetWheel()) {
  1881. ret = false;
  1882. }
  1883. else {
  1884. safe_delete(m_soloHO[client]);
  1885. m_soloHO[client] = ho;
  1886. }
  1887. }
  1888. else
  1889. m_soloHO[client] = ho;
  1890. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1891. }
  1892. return ret;
  1893. }
  1894. bool SpellProcess::AddHO(int32 group_id, HeroicOP* ho) {
  1895. bool ret = true;
  1896. if (group_id > 0 && ho) {
  1897. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1898. if (m_groupHO.count(group_id) > 0) {
  1899. if (m_groupHO[group_id]->GetWheel()) {
  1900. ret = false;
  1901. }
  1902. else {
  1903. safe_delete(m_groupHO[group_id]);
  1904. m_groupHO[group_id] = ho;
  1905. }
  1906. }
  1907. else
  1908. m_groupHO[group_id] = ho;
  1909. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1910. }
  1911. return ret;
  1912. }
  1913. void SpellProcess::KillHOBySpawnID(int32 spawn_id) {
  1914. // Check solo HO's
  1915. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1916. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  1917. map<Client*, HeroicOP*>::iterator delete_itr;
  1918. while (itr != m_soloHO.end()) {
  1919. if (itr->second->GetTarget() == spawn_id) {
  1920. itr->second->SetComplete(1);
  1921. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  1922. delete_itr = itr;
  1923. safe_delete(itr->second);
  1924. itr++;
  1925. m_soloHO.erase(delete_itr);
  1926. }
  1927. else
  1928. itr++;
  1929. }
  1930. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1931. // Check Group HO's
  1932. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1933. map<int32, HeroicOP*>::iterator itr2 = m_groupHO.begin();
  1934. map<int32, HeroicOP*>::iterator delete_itr2;
  1935. while (itr2 != m_groupHO.end()) {
  1936. if (itr2->second->GetTarget() == spawn_id) {
  1937. itr2->second->SetComplete(1);
  1938. world.GetGroupManager()->GroupLock(__FUNCTION__ , __LINE__);
  1939. deque<GroupMemberInfo*>::iterator itr3;
  1940. deque<GroupMemberInfo*>* members = world.GetGroupManager()->GetGroupMembers(itr2->first);
  1941. for (itr3 = members->begin(); itr3 != members->end(); itr3++) {
  1942. if ((*itr3)->client)
  1943. ClientPacketFunctions::SendHeroicOPUpdate((*itr3)->client, itr2->second);
  1944. }
  1945. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1946. delete_itr2 = itr2;
  1947. safe_delete(itr2->second);
  1948. itr2++;
  1949. m_groupHO.erase(delete_itr2);
  1950. }
  1951. else
  1952. itr2++;
  1953. }
  1954. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1955. }
  1956. void SpellProcess::AddSpellCancel(LuaSpell* spell){
  1957. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  1958. SpellCancelList.push_back(spell);
  1959. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  1960. }