SpellProcess.cpp 109 KB

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