SpellProcess.cpp 73 KB

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