SpellProcess.cpp 107 KB

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