SpellProcess.cpp 88 KB

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