SpellProcess.cpp 107 KB

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