SpellProcess.cpp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  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. spell_que.Put(caster, spell);
  764. ((Player*)caster)->QueueSpell(spell);
  765. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  766. if(client)
  767. SendSpellBookUpdate(client);
  768. }
  769. }
  770. void SpellProcess::RemoveSpellFromQueue(Spell* spell, Entity* caster){
  771. if(caster && caster->IsPlayer() && spell){
  772. spell_que.erase(caster);
  773. ((Player*)caster)->UnQueueSpell(spell);
  774. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  775. if(client)
  776. SendSpellBookUpdate(client);
  777. }
  778. }
  779. void SpellProcess::RemoveSpellFromQueue(Entity* caster, bool hostile_only) {
  780. if (caster && spell_que.count(caster) > 0) {
  781. Spell* spell = spell_que.Get(caster);
  782. if (spell) {
  783. bool remove = true;
  784. if (hostile_only && spell->GetSpellData()->target_type != SPELL_TARGET_ENEMY)
  785. remove = false;
  786. if (remove) {
  787. spell_que.erase(caster);
  788. ((Player*)caster)->UnQueueSpell(spell);
  789. Client* client = caster->GetZone()->GetClientBySpawn(caster);
  790. if (client)
  791. SendSpellBookUpdate(client);
  792. }
  793. }
  794. }
  795. }
  796. void SpellProcess::CheckSpellQueue(Entity* caster){
  797. if(caster && caster->IsPlayer()){
  798. if(spell_que.count(caster) > 0)
  799. RemoveSpellFromQueue(spell_que.Get(caster), caster);
  800. }
  801. }
  802. bool SpellProcess::CheckSpellQueue(Spell* spell, Entity* caster){
  803. if(caster->IsPlayer()){
  804. bool add = true;
  805. bool remove = false;
  806. if(spell_que.count(caster) > 0){
  807. remove = true;
  808. if(spell_que.Get(caster) == spell)
  809. add = false;
  810. }
  811. if(remove)
  812. RemoveSpellFromQueue(spell, caster);
  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 (target->IsBot() && (caster->IsPlayer() || caster->IsBot())) {
  1181. // Needed so bots or player can cast friendly spells on bots
  1182. }
  1183. else
  1184. {
  1185. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_A_FRIEND);
  1186. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1187. DeleteSpell(lua_spell);
  1188. return;
  1189. }
  1190. }
  1191. else if (spell->GetSpellData()->friendly_spell && target->IsNPC() && caster->IsNPC()) {
  1192. // TODO: faction checks? some other checks to prevent an npc casting a friendly spell on another npc
  1193. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: NPC Cast", spell->GetName());
  1194. }
  1195. else
  1196. {
  1197. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Is Not Friendly (catch all)", spell->GetName());
  1198. if (!target->IsEntity())
  1199. {
  1200. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: No target.", spell->GetName());
  1201. zone->SendSpellFailedPacket(client, SPELL_ERROR_NO_ELIGIBLE_TARGET);
  1202. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1203. DeleteSpell(lua_spell);
  1204. return;
  1205. }
  1206. if (caster == target || (!target->IsPlayer() && !target->appearance.attackable))
  1207. {
  1208. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Not an Enemy (Target: %s).", spell->GetName(), target->GetName());
  1209. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1210. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1211. DeleteSpell(lua_spell);
  1212. return;
  1213. }
  1214. if (!target->Alive())
  1215. {
  1216. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is not alive (Target: %s).", spell->GetName(), target->GetName());
  1217. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ALIVE);
  1218. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1219. DeleteSpell(lua_spell);
  1220. return;
  1221. }
  1222. if (target->GetInvulnerable())
  1223. {
  1224. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target is invulnerable (Target: %s).", spell->GetName(), target->GetName());
  1225. zone->SendSpellFailedPacket(client, SPELL_ERROR_TARGET_INVULNERABLE);
  1226. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1227. DeleteSpell(lua_spell);
  1228. return;
  1229. }
  1230. if ((target->IsPlayer() || target->IsBot()) && (caster->IsPlayer() || caster->IsBot()))
  1231. {
  1232. bool attackAllowed = (Entity*)caster->AttackAllowed((Entity*)target, 0);
  1233. if (!attackAllowed)
  1234. {
  1235. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is player and not attackable.", spell->GetName(), target->GetName());
  1236. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1237. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1238. DeleteSpell(lua_spell);
  1239. return;
  1240. }
  1241. }
  1242. if (target->IsPet() && ((NPC*)target)->GetOwner() && ((NPC*)target)->GetOwner() == caster) {
  1243. LogWrite(SPELL__DEBUG, 1, "Spell", "%s: Target (%s) is casters pet and not attackable by caster.", spell->GetName(), target->GetName());
  1244. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_AN_ENEMY);
  1245. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1246. DeleteSpell(lua_spell);
  1247. return;
  1248. }
  1249. }
  1250. }
  1251. if (lua_spell->targets.size() == 0 && spell->GetSpellData()->max_aoe_targets == 0)
  1252. {
  1253. LogWrite(SPELL__ERROR, 0, "Spell", "SpellProcess::ProcessSpell Unable to find any spell targets for spell '%s', spell type: %u, target type %u.",
  1254. spell->GetName(), spell->GetSpellData()->spell_type, spell->GetSpellData()->target_type);
  1255. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1256. DeleteSpell(lua_spell);
  1257. return;
  1258. }
  1259. if(target_type == SPELL_TARGET_ENEMY_CORPSE || target_type == SPELL_TARGET_GROUP_CORPSE)
  1260. {
  1261. if(target->Alive())
  1262. {
  1263. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_DEAD);
  1264. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1265. DeleteSpell(lua_spell);
  1266. return;
  1267. }
  1268. if(target->IsPlayer() && zone->GetClientBySpawn(target)->GetCurrentRez()->active){
  1269. zone->SendSpellFailedPacket(client, SPELL_ERROR_ALREADY_CAST);
  1270. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1271. DeleteSpell(lua_spell);
  1272. return;
  1273. }
  1274. }
  1275. if(!CheckPower(lua_spell))
  1276. {
  1277. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Power to cast (%s).", spell->GetName(), caster->GetName());
  1278. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_POWER);
  1279. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1280. DeleteSpell(lua_spell);
  1281. return;
  1282. }
  1283. if (!CheckHP(lua_spell))
  1284. {
  1285. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Health to cast (%s).", spell->GetName(), caster->GetName());
  1286. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_HEALTH);
  1287. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1288. DeleteSpell(lua_spell);
  1289. return;
  1290. }
  1291. if (!CheckSavagery(lua_spell))
  1292. {
  1293. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Savagery to cast (%s).", spell->GetName(), caster->GetName());
  1294. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_SAVAGERY);
  1295. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1296. DeleteSpell(lua_spell);
  1297. return;
  1298. }
  1299. if (!CheckDissonance(lua_spell))
  1300. {
  1301. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Dissonance to cast (%s).", spell->GetName(), caster->GetName());
  1302. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_DISSONANCE);
  1303. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1304. DeleteSpell(lua_spell);
  1305. return;
  1306. }
  1307. if (!CheckConcentration(lua_spell))
  1308. {
  1309. LogWrite(SPELL__WARNING, 1, "Spell", "%s: Caster Lacked Concentration to cast (%s).", spell->GetName(), caster->GetName());
  1310. zone->SendSpellFailedPacket(client, SPELL_ERROR_NOT_ENOUGH_CONC);
  1311. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1312. DeleteSpell(lua_spell);
  1313. return;
  1314. }
  1315. // Precast in lua
  1316. if (lua_interface) {
  1317. bool result = false;
  1318. lua_interface->AddSpawnPointers(lua_spell, false, true);
  1319. if (lua_pcall(lua_spell->state, 2, 2, 0) == 0) {
  1320. result = lua_interface->GetBooleanValue(lua_spell->state, 1);
  1321. int8 error = lua_interface->GetInt8Value(lua_spell->state, 2) == 0 ? SPELL_ERROR_CANNOT_PREPARE : lua_interface->GetInt8Value(lua_spell->state, 2);
  1322. lua_interface->ResetFunctionStack(lua_spell->state);
  1323. if (!result) {
  1324. zone->SendSpellFailedPacket(client, error);
  1325. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1326. DeleteSpell(lua_spell);
  1327. return;
  1328. }
  1329. }
  1330. else {
  1331. LogWrite(SPELL__DEBUG, 1, "Spell", "No precast function found for %s", lua_spell->spell->GetName());
  1332. lua_interface->ResetFunctionStack(lua_spell->state);
  1333. }
  1334. }
  1335. else
  1336. LogWrite(SPELL__DEBUG, 1, "Spell", "Unable to do precast check as there was no lua_interface");
  1337. if (custom_cast_time > 0)
  1338. spell->GetSpellData()->cast_time = custom_cast_time;
  1339. //Apply casting speed mod
  1340. spell->ModifyCastTime(caster);
  1341. //cancel stealth effects on cast
  1342. if(caster->IsStealthed() || caster->IsInvis())
  1343. caster->CancelAllStealth();
  1344. if(caster && caster->IsEntity() && caster->CheckFizzleSpell(lua_spell))
  1345. {
  1346. caster->GetZone()->SendCastSpellPacket(0, target ? target : caster, caster);
  1347. caster->GetZone()->SendInterruptPacket(caster, lua_spell, true);
  1348. caster->IsCasting(false);
  1349. if(caster->IsPlayer())
  1350. {
  1351. ((Player*)caster)->UnlockSpell(spell);
  1352. SendSpellBookUpdate(((Player*)caster)->GetClient());
  1353. }
  1354. // fizzle takes half
  1355. int16 power_req = spell->GetPowerRequired(caster) / 2;
  1356. TakePower(lua_spell, power_req);
  1357. int16 hp_req = spell->GetHPRequired(caster) / 2;
  1358. TakeHP(lua_spell, hp_req);
  1359. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1360. DeleteSpell(lua_spell);
  1361. return;
  1362. }
  1363. SendStartCast(lua_spell, client);
  1364. LockAllSpells(client);
  1365. if(spell->GetSpellData()->cast_time > 0)
  1366. {
  1367. CastTimer* cast_timer = new CastTimer;
  1368. cast_timer->entity_command = 0;
  1369. cast_timer->target_id = target ? target->GetID() : 0;
  1370. cast_timer->spell = lua_spell;
  1371. cast_timer->spell->caster = caster;
  1372. cast_timer->delete_timer = false;
  1373. cast_timer->timer = new Timer(spell->GetSpellData()->cast_time * 10);
  1374. cast_timer->zone = zone;
  1375. cast_timer->in_heroic_opp = in_heroic_opp;
  1376. cast_timers.Add(cast_timer);
  1377. if(caster)
  1378. caster->IsCasting(true);
  1379. }
  1380. else
  1381. {
  1382. if(!CastProcessedSpell(lua_spell, false, in_heroic_opp))
  1383. {
  1384. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  1385. DeleteSpell(lua_spell);
  1386. return;
  1387. }
  1388. }
  1389. if(caster)
  1390. caster->GetZone()->SendCastSpellPacket(lua_spell, caster);
  1391. }
  1392. }
  1393. void SpellProcess::ProcessEntityCommand(ZoneServer* zone, EntityCommand* entity_command, Entity* caster, Spawn* target, bool lock, bool in_heroic_opp) {
  1394. if (zone && entity_command && caster && target && !target->IsPlayer()) {
  1395. Client* client = zone->GetClientBySpawn(caster);
  1396. if (caster->GetDistance(target) > entity_command->distance) {
  1397. zone->SendSpellFailedPacket(client, SPELL_ERROR_TOO_FAR_AWAY);
  1398. return;
  1399. }
  1400. if (entity_command->cast_time > 0) {
  1401. PacketStruct* packet = configReader.getStruct("WS_StartCastSpell", client->GetVersion());
  1402. if (packet) {
  1403. LockAllSpells(client);
  1404. packet->setDataByName("cast_time", entity_command->cast_time * 0.01);
  1405. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1406. EQ2Packet* outapp = packet->serialize();
  1407. client->QueuePacket(outapp);
  1408. safe_delete(packet);
  1409. CastTimer* cast_timer = new CastTimer;
  1410. cast_timer->caster = client;
  1411. cast_timer->target_id = target ? target->GetID() : 0;
  1412. cast_timer->entity_command = entity_command;
  1413. cast_timer->spell = 0;
  1414. cast_timer->delete_timer = false;
  1415. cast_timer->timer = new Timer(entity_command->cast_time * 10);
  1416. cast_timer->zone = zone;
  1417. cast_timer->in_heroic_opp = in_heroic_opp;
  1418. cast_timers.Add(cast_timer);
  1419. caster->IsCasting(true);
  1420. }
  1421. }
  1422. else if (!CastProcessedEntityCommand(entity_command, client, target, in_heroic_opp))
  1423. return;
  1424. if (entity_command && entity_command->spell_visual > 0)
  1425. caster->GetZone()->SendCastEntityCommandPacket(entity_command, caster->GetID(), target->GetID());
  1426. }
  1427. }
  1428. bool SpellProcess::CastProcessedSpell(LuaSpell* spell, bool passive, bool in_heroic_opp){
  1429. if(!spell || !spell->caster || !spell->spell || spell->interrupted)
  1430. return false;
  1431. Client* client = 0;
  1432. if(spell->caster && spell->caster->IsPlayer())
  1433. client = spell->caster->GetZone()->GetClientBySpawn(spell->caster);
  1434. if (spell->spell->GetSpellData()->max_aoe_targets > 0 && spell->targets.size() == 0) {
  1435. GetSpellTargetsTrueAOE(spell);
  1436. if (spell->targets.size() == 0) {
  1437. if(client)
  1438. {
  1439. client->GetPlayer()->UnlockAllSpells(true);
  1440. SendSpellBookUpdate(client);
  1441. }
  1442. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_NO_TARGETS_IN_RANGE);
  1443. return false;
  1444. }
  1445. }
  1446. if(client && client->GetCurrentZone() &&
  1447. !spell->spell->GetSpellData()->friendly_spell)
  1448. {
  1449. ZoneServer* zone = client->GetCurrentZone();
  1450. Spawn* tmpTarget = zone->GetSpawnByID(spell->initial_target);
  1451. int8 spell_type = spell->spell->GetSpellData()->spell_type;
  1452. LuaSpell* conflictSpell = spell->caster->HasLinkedTimerID(spell, tmpTarget, (spell_type == SPELL_TYPE_DD || spell_type == SPELL_TYPE_DOT));
  1453. if(conflictSpell && tmpTarget && tmpTarget->IsEntity())
  1454. {
  1455. ((Entity*)tmpTarget)->RemoveSpellEffect(conflictSpell);
  1456. zone->RemoveTargetFromSpell(conflictSpell, tmpTarget);
  1457. }
  1458. }
  1459. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1460. bool processedSpell = false;
  1461. LuaSpell* replace_spell = 0;
  1462. // Check to see if we already casted this spell and it is in the active_spells list
  1463. /*while (itr.Next()) {
  1464. LuaSpell* luaspell = itr.value;
  1465. // Check to see if this is the same spell by comparing caster, spell id, and spell tier
  1466. if (luaspell->caster == spell->caster && luaspell->spell->GetSpellID() == spell->spell->GetSpellID() && luaspell->spell->GetSpellTier() == spell->spell->GetSpellTier() && luaspell->targets.size() == spell->targets.size()) {
  1467. vector<Spawn*>::iterator itr;
  1468. vector<Spawn*>::iterator itr2;
  1469. bool all_match = true;
  1470. // compare the target vector to make sure this is the same spell
  1471. for (itr = luaspell->targets.begin(); itr != luaspell->targets.end(); itr++) {
  1472. bool match = false;
  1473. for (itr2 = spell->targets.begin(); itr2 != spell->targets.end(); itr2++) {
  1474. if ((*itr) == (*itr2)) {
  1475. match = true;
  1476. break;
  1477. }
  1478. }
  1479. if (!match) {
  1480. all_match = false;
  1481. break;
  1482. }
  1483. }
  1484. if (all_match) {
  1485. // set a pointer to replace the spell in the active spell list so we don't have to run this loop again
  1486. replace_spell = luaspell;
  1487. // if friendly skip the calling of the lua function as they have already been called for the previous version of this spell
  1488. if (spell->spell->GetSpellData()->friendly_spell == 1)
  1489. processedSpell = true;
  1490. break;
  1491. }
  1492. }
  1493. }*/
  1494. bool allTargets = (spell->spell->GetSpellData()->spell_type == SPELL_TYPE_ALLGROUPTARGETS);
  1495. if (!processedSpell)
  1496. processedSpell = ProcessSpell(spell, true, 0, 0, allTargets);
  1497. // Quick hack to prevent a crash on spells that zones the caster (Gate)
  1498. if (!spell->caster)
  1499. return true;
  1500. Skill* skill = spell->caster->GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
  1501. // trigger potential skill increase if we succeed in casting a mastery skill and it still has room to grow (against this spell)
  1502. if(skill && skill->current_val < spell->spell->GetSpellData()->min_class_skill_req)
  1503. spell->caster->GetSkillByID(spell->spell->GetSpellData()->mastery_skill, true);
  1504. ZoneServer* zone = spell->caster->GetZone();
  1505. Spawn* target = 0;
  1506. if(processedSpell){
  1507. for (int32 i = 0; i < spell->targets.size(); i++) {
  1508. target = zone->GetSpawnByID(spell->targets[i]);
  1509. if (!target)
  1510. continue;
  1511. if (client && client->IsZoning())
  1512. continue;
  1513. // TODO: Establish actual hate per spell
  1514. if (!spell->spell->GetSpellData()->friendly_spell && target->IsNPC())
  1515. ((NPC*)target)->AddHate((Entity*)spell->caster, 50);
  1516. if(spell->spell->GetSpellData()->success_message.length() > 0){
  1517. if(client){
  1518. string success_message = spell->spell->GetSpellData()->success_message;
  1519. if(success_message.find("%t") != string::npos)
  1520. success_message.replace(success_message.find("%t"), 2, spell->caster->GetName());
  1521. client->Message(CHANNEL_SPELLS, success_message.c_str());
  1522. }
  1523. }
  1524. if(spell->spell->GetSpellData()->effect_message.length() > 0){
  1525. string effect_message = spell->spell->GetSpellData()->effect_message;
  1526. bool send_to_sender = true;
  1527. if(effect_message.find("%t") != string::npos)
  1528. effect_message.replace(effect_message.find("%t"), 2, target->GetName());
  1529. if (effect_message.find("%c") != string::npos)
  1530. effect_message.replace(effect_message.find("%c"), 2, spell->caster->GetName());
  1531. if (effect_message.find("%T") != string::npos) {
  1532. effect_message.replace(effect_message.find("%T"), 2, target->GetName());
  1533. send_to_sender = false;
  1534. }
  1535. if (effect_message.find("%C") != string::npos) {
  1536. effect_message.replace(effect_message.find("%C"), 2, spell->caster->GetName());
  1537. send_to_sender = false;
  1538. }
  1539. spell->caster->GetZone()->SimpleMessage(CHANNEL_SPELLS_OTHER, effect_message.c_str(), target, 50, send_to_sender);
  1540. }
  1541. target->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, spell->caster, spell->spell->GetName());
  1542. }
  1543. }
  1544. else{
  1545. if (!passive)
  1546. SendFinishedCast(spell, client);
  1547. return false;
  1548. }
  1549. if(!spell->resisted && (spell->spell->GetSpellDuration() > 0 || spell->spell->GetSpellData()->duration_until_cancel)) {
  1550. for (int32 i = 0; i < spell->targets.size(); i++) {
  1551. //LogWrite(SPELL__ERROR, 0, "Spell", "No precast function found for %s", ((Entity*)target)->GetName());
  1552. target = zone->GetSpawnByID(spell->targets.at(i));
  1553. if (!target && spell->targets.at(i) == spell->caster->GetID()) {
  1554. target = spell->caster;
  1555. }
  1556. if (!target) {
  1557. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Zone has not finished loading process yet. Try again later.");
  1558. continue;
  1559. }
  1560. if (i == 0 && !spell->spell->GetSpellData()->not_maintained) {
  1561. spell->caster->AddMaintainedSpell(spell);
  1562. //((Entity*)target)->AddMaintainedSpell(spell);
  1563. LogWrite(SPELL__DEBUG, 0, "Spell", "AddMaintained on %s", ((Entity*)target)->GetName());
  1564. }
  1565. SpellEffects* effect = ((Entity*)target)->GetSpellEffect(spell->spell->GetSpellID());
  1566. if (effect && effect->tier > spell->spell->GetSpellTier()) {
  1567. if(client) {
  1568. spell->caster->GetZone()->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  1569. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "The spell did not take hold as the target already has a better spell of this type.");
  1570. }
  1571. }
  1572. else{
  1573. ((Entity*)target)->AddSpellEffect(spell);
  1574. if(spell->spell->GetSpellData()->det_type > 0)
  1575. ((Entity*)target)->AddDetrimentalSpell(spell);
  1576. }
  1577. }
  1578. if (replace_spell) {
  1579. active_spells.Remove(replace_spell);
  1580. active_spells.Add(spell);
  1581. }
  1582. else {
  1583. active_spells.Add(spell);
  1584. }
  1585. if (spell->num_triggers > 0)
  1586. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->num_triggers, 0);
  1587. if (spell->damage_remaining > 0)
  1588. ClientPacketFunctions::SendMaintainedExamineUpdate(client, spell->slot_pos, spell->damage_remaining, 1);
  1589. spell->caster->GetZone()->TriggerCharSheetTimer();
  1590. }
  1591. spell->num_calls = 1;
  1592. if(!spell->resisted && spell->spell->GetSpellData()->duration1 > 0){
  1593. spell->timer.Start();
  1594. if(spell->spell->GetSpellData()->call_frequency > 0)
  1595. spell->timer.SetTimer(spell->spell->GetSpellData()->call_frequency*100);
  1596. else
  1597. spell->timer.SetTimer(spell->spell->GetSpellData()->duration1*100);
  1598. if (active_spells.count(spell) > 0) {
  1599. active_spells.Add(spell);
  1600. }
  1601. }
  1602. // if the caster is a player and the spell is a tradeskill spell check for a tradeskill event
  1603. if (client && spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL) {
  1604. spell->resisted = (spell->caster->DetermineHit(target ? target : spell->caster, 255, 0, true, spell) == DAMAGE_PACKET_RESULT_RESIST);
  1605. client->GetCurrentZone()->GetTradeskillMgr()->CheckTradeskillEvent(client, spell->resisted ? 0 : spell->spell->GetSpellData()->icon);
  1606. }
  1607. if (spell->spell->GetSpellData()->friendly_spell && zone->GetSpawnByID(spell->initial_target))
  1608. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, zone->GetSpawnByID(spell->initial_target));
  1609. // Check HO's
  1610. if (client && !in_heroic_opp) {
  1611. HeroicOP* ho = nullptr;
  1612. Spell* ho_spell = nullptr;
  1613. int32 ho_target = 0;
  1614. MSoloHO.writelock(__FUNCTION__, __LINE__);
  1615. MGroupHO.writelock(__FUNCTION__, __LINE__);
  1616. map<Client*, HeroicOP*>::iterator soloItr = m_soloHO.find(client);
  1617. if (soloItr != m_soloHO.end()) {
  1618. ho = soloItr->second;
  1619. bool match = false;
  1620. LogWrite(SPELL__ERROR, 0, "HO", "target = %u", ho->GetTarget());
  1621. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1622. for (int8 i = 0; i < spell->targets.size(); i++) {
  1623. LogWrite(SPELL__ERROR, 0, "HO", "%u", spell->targets.at(i));
  1624. if (spell->targets.at(i) == ho->GetTarget()) {
  1625. match = true;
  1626. LogWrite(SPELL__ERROR, 0, "HO", "match found");
  1627. break;
  1628. }
  1629. }
  1630. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1631. if(match && !spell->spell)
  1632. LogWrite(SPELL__ERROR, 0, "HO", "%s: spell->spell is nullptr", client->GetPlayer()->GetName());
  1633. else if (match && spell->spell && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1634. ClientPacketFunctions::SendHeroicOPUpdate(client, ho);
  1635. if (ho->GetComplete() > 0) {
  1636. if(!ho->GetWheel())
  1637. LogWrite(SPELL__ERROR, 0, "HO", "%s: Wheel is inactive (nullptr) cannot check for invalid spell", client->GetPlayer()->GetName());
  1638. else
  1639. {
  1640. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1641. ho_target = ho->GetTarget();
  1642. if (!ho_spell)
  1643. LogWrite(SPELL__ERROR, 0, "HO", "%s: Invalid spell for a HO, spell id = %u", client->GetPlayer()->GetName(), ho->GetWheel()->spell_id);
  1644. safe_delete(ho);
  1645. m_soloHO.erase(soloItr);
  1646. }
  1647. }
  1648. }
  1649. }
  1650. else if (client->GetPlayer()->GetGroupMemberInfo()) {
  1651. map<int32, HeroicOP*>::iterator groupItr = m_groupHO.find(client->GetPlayer()->GetGroupMemberInfo()->group_id);
  1652. if (groupItr != m_groupHO.end()) {
  1653. ho = groupItr->second;
  1654. int32 group_id = client->GetPlayer()->GetGroupMemberInfo()->group_id;
  1655. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1656. if (spell->targets.at(0) == ho->GetTarget() && ho->UpdateHeroicOP(spell->spell->GetSpellIconHeroicOp())) {
  1657. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1658. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1659. deque<GroupMemberInfo*>::iterator itr;
  1660. PlayerGroup* group = world.GetGroupManager()->GetGroup(group_id);
  1661. if (group)
  1662. {
  1663. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1664. deque<GroupMemberInfo*>* members = group->GetMembers();
  1665. for (itr = members->begin(); itr != members->end(); itr++) {
  1666. if ((*itr)->client)
  1667. ClientPacketFunctions::SendHeroicOPUpdate((*itr)->client, ho);
  1668. }
  1669. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1670. }
  1671. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1672. if (ho->GetComplete() > 0) {
  1673. ho_spell = master_spell_list.GetSpell(ho->GetWheel()->spell_id, 1);
  1674. ho_target = ho->GetTarget();
  1675. if (!ho_spell)
  1676. LogWrite(SPELL__ERROR, 0, "HO", "Invalid spell for a HO, spell id = %u", ho->GetWheel()->spell_id);
  1677. safe_delete(ho);
  1678. m_groupHO.erase(groupItr);
  1679. }
  1680. }
  1681. else
  1682. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1683. }
  1684. }
  1685. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  1686. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  1687. if (ho_spell && ho_target != 0)
  1688. client->GetCurrentZone()->ProcessSpell(ho_spell, client->GetPlayer(), spell->caster->GetZone()->GetSpawnByID(ho_target));
  1689. }
  1690. if (spell->spell->GetSpellData()->friendly_spell == 1 && spell->initial_target)
  1691. spell->caster->CheckProcs(PROC_TYPE_BENEFICIAL, spell->caster->GetZone()->GetSpawnByID(spell->initial_target));
  1692. if (!passive)
  1693. SendFinishedCast(spell, client);
  1694. return true;
  1695. }
  1696. bool SpellProcess::CastProcessedEntityCommand(EntityCommand* entity_command, Client* client, Spawn* target, bool in_heroic_opp) {
  1697. bool ret = false;
  1698. if (entity_command && client) {
  1699. UnlockAllSpells(client);
  1700. client->GetPlayer()->IsCasting(false);
  1701. if (entity_command->cast_time == 0) {
  1702. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1703. ret = true;
  1704. }
  1705. if (!ret) {
  1706. PacketStruct* packet = configReader.getStruct("WS_FinishCastSpell", client->GetVersion());
  1707. if (packet) {
  1708. packet->setMediumStringByName("spell_name", entity_command->name.c_str());
  1709. client->QueuePacket(packet->serialize());
  1710. safe_delete(packet);
  1711. SendSpellBookUpdate(client);
  1712. client->GetPlayer()->GetZone()->CallSpawnScript(target, SPAWN_SCRIPT_CASTED_ON, client->GetPlayer(), entity_command->command.c_str());
  1713. ret = true;
  1714. }
  1715. }
  1716. if (ret) {
  1717. EQ2_16BitString command;
  1718. command.data = entity_command->command;
  1719. command.size = entity_command->command.length();
  1720. int32 handler = commands.GetCommandHandler(command.data.c_str());
  1721. if (handler != 0xFFFFFFFF && handler < 999)
  1722. commands.Process(handler, &command, client, target);
  1723. }
  1724. }
  1725. return ret;
  1726. }
  1727. void SpellProcess::Interrupted(Entity* caster, Spawn* interruptor, int16 error_code, bool cancel, bool from_movement)
  1728. {
  1729. if(caster)
  1730. {
  1731. LogWrite(SPELL__DEBUG, 0, "Spell", "'%s' is Interrupting spell of '%s'...", interruptor ? interruptor->GetName() : "unknown", caster->GetName());
  1732. LuaSpell* spell = GetLuaSpell(caster);
  1733. if (spell && ((from_movement && !spell->spell->GetSpellData()->cast_while_moving) || (!from_movement && spell->spell->GetSpellData()->interruptable) ||
  1734. cancel))
  1735. {
  1736. Spawn* target = GetSpellTarget(caster);
  1737. InterruptStruct* interrupt = new InterruptStruct;
  1738. interrupt->interrupted = caster;
  1739. interrupt->spell = spell;
  1740. interrupt->target = target;
  1741. interrupt->error_code = error_code;
  1742. spell->interrupted = true;
  1743. interrupt_list.Add(interrupt);
  1744. Client* client = 0;
  1745. if(interruptor && interruptor->IsPlayer())
  1746. {
  1747. client = interruptor->GetZone()->GetClientBySpawn(interruptor);
  1748. client->Message(CHANNEL_SPELLS_OTHER, "You interrupt %s's ability to cast!", interruptor->GetName());
  1749. }
  1750. }
  1751. }
  1752. }
  1753. void SpellProcess::RemoveSpellTimersFromSpawn(Spawn* spawn, bool remove_all, bool delete_recast, bool call_expire_function, bool lock_spell_process){
  1754. if(lock_spell_process)
  1755. MSpellProcess.lock();
  1756. int32 i = 0;
  1757. if(cast_timers.size() > 0){
  1758. CastTimer* cast_timer = 0;
  1759. MutexList<CastTimer*>::iterator itr = cast_timers.begin();
  1760. while(itr.Next()){
  1761. cast_timer = itr->value;
  1762. if(cast_timer && cast_timer->spell && cast_timer->spell->caster == spawn){
  1763. cast_timer->spell->caster = 0;
  1764. cast_timer->delete_timer = true;
  1765. }
  1766. }
  1767. }
  1768. if(remove_all){
  1769. LuaSpell* spell = 0;
  1770. MutexList<LuaSpell*>::iterator itr = active_spells.begin();
  1771. while(itr.Next()){
  1772. spell = itr->value;
  1773. if (!spell)
  1774. continue;
  1775. if(spell->caster == spawn && call_expire_function){
  1776. DeleteCasterSpell(spell, "expired", remove_all);
  1777. continue;
  1778. }
  1779. if (spell->spell->GetSpellData()->persist_through_death)
  1780. continue;
  1781. spell->MSpellTargets.readlock(__FUNCTION__, __LINE__);
  1782. for (i = 0; i < spell->targets.size(); i++){
  1783. if (spawn->GetID() == spell->targets.at(i)){
  1784. if (spawn->IsEntity())
  1785. ((Entity*)spawn)->RemoveSpellEffect(spell);
  1786. RemoveTargetFromSpell(spell, spawn, remove_all);
  1787. break;
  1788. }
  1789. }
  1790. spell->MSpellTargets.releasereadlock(__FUNCTION__, __LINE__);
  1791. }
  1792. if(recast_timers.size() > 0 && delete_recast){
  1793. RecastTimer* recast_timer = 0;
  1794. MutexList<RecastTimer*>::iterator itr = recast_timers.begin();
  1795. while(itr.Next()){
  1796. recast_timer = itr->value;
  1797. if(recast_timer && recast_timer->caster == spawn){
  1798. safe_delete(recast_timer->timer);
  1799. recast_timers.Remove(recast_timer, true);
  1800. }
  1801. }
  1802. }
  1803. if(spell_que.size() > 0 && spawn->IsEntity()){
  1804. spell_que.erase((Entity*)spawn);
  1805. }
  1806. if(interrupt_list.size() > 0){
  1807. InterruptStruct* interrupt = 0;
  1808. MutexList<InterruptStruct*>::iterator itr = interrupt_list.begin();
  1809. while(itr.Next()){
  1810. interrupt = itr->value;
  1811. if(interrupt && interrupt->interrupted == spawn){
  1812. interrupt_list.Remove(interrupt, true);
  1813. }
  1814. }
  1815. }
  1816. }
  1817. if(lock_spell_process)
  1818. MSpellProcess.unlock();
  1819. }
  1820. void SpellProcess::GetSpellTargets(LuaSpell* luaspell)
  1821. {
  1822. if (luaspell && luaspell->spell && luaspell->caster && luaspell->spell->GetSpellData()->max_aoe_targets == 0)
  1823. {
  1824. int8 target_type = luaspell->spell->GetSpellData()->target_type;
  1825. int8 spell_type = luaspell->spell->GetSpellData()->spell_type;
  1826. Spawn* caster = luaspell->caster;
  1827. Spawn* target = caster->GetZone()->GetSpawnByID(luaspell->initial_target);
  1828. SpellData* data = luaspell->spell->GetSpellData();
  1829. bool implied = false;
  1830. Spawn* secondary_target = nullptr;
  1831. //implied target check -- only use this for players
  1832. 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))
  1833. {
  1834. if (caster->IsPlayer() && target->HasTarget())
  1835. {
  1836. secondary_target = target->GetTarget();
  1837. // check if spell is friendly
  1838. if (data->friendly_spell) {
  1839. //if target is NPC (and not a bot) on friendly spell, check to see if target is friendly
  1840. if (target->IsNPC() && !target->IsBot()) {
  1841. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1842. if (secondary_target && secondary_target->IsPlayer()) {
  1843. target = secondary_target;
  1844. luaspell->initial_target = target->GetID();
  1845. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1846. AddLuaSpellTarget(luaspell, target->GetID());
  1847. }
  1848. else if (secondary_target && secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsPlayer())
  1849. implied = true;
  1850. }
  1851. else if (target->IsPet() && ((Entity*)target)->GetOwner()->IsPlayer())
  1852. {
  1853. luaspell->initial_target = target->GetID();
  1854. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1855. AddLuaSpellTarget(luaspell, target->GetID());
  1856. }
  1857. }
  1858. } // if spell is not friendly
  1859. else { // check if there is an implied target for this non-friendly spell
  1860. if (target->IsPlayer() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsPlayer())) {
  1861. if (secondary_target && secondary_target->IsNPC()) {
  1862. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1863. implied = true;
  1864. }
  1865. }
  1866. else if (target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)target))
  1867. {
  1868. secondary_target = target;
  1869. implied = true;
  1870. luaspell->initial_target = target->GetID();
  1871. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1872. AddLuaSpellTarget(luaspell, target->GetID());
  1873. GetPlayerGroupTargets((Player*)target, caster, luaspell);
  1874. }
  1875. else if (secondary_target && target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)secondary_target))
  1876. {
  1877. implied = true;
  1878. luaspell->initial_target = secondary_target->GetID();
  1879. luaspell->initial_target_char_id = (secondary_target && secondary_target->IsPlayer()) ? ((Player*)secondary_target)->GetCharacterID() : 0;
  1880. AddLuaSpellTarget(luaspell, secondary_target->GetID());
  1881. GetPlayerGroupTargets((Player*)secondary_target, caster, luaspell);
  1882. }
  1883. }
  1884. } // end friendly spell check
  1885. }
  1886. else if (caster->IsPlayer()) {
  1887. if (data->friendly_spell) {
  1888. if (target->IsNPC() && !target->IsBot()) {
  1889. if (!target->IsPet() || (target->IsPet() && ((NPC*)target)->GetOwner()->IsNPC())) {
  1890. target = caster;
  1891. luaspell->initial_target = caster->GetID();
  1892. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1893. }
  1894. }
  1895. }
  1896. else
  1897. {
  1898. if (target->IsPlayer() && ((Entity*)caster)->AttackAllowed((Entity*)target)) {
  1899. luaspell->initial_target = target->GetID();
  1900. luaspell->initial_target_char_id = (target && target->IsPlayer()) ? ((Player*)target)->GetCharacterID() : 0;
  1901. AddLuaSpellTarget(luaspell, target->GetID());
  1902. }
  1903. }
  1904. }
  1905. }
  1906. else if (target_type == SPELL_TARGET_GROUP_AE || spell_type == SPELL_TYPE_ALLGROUPTARGETS || target_type == SPELL_TARGET_RAID_AE) {
  1907. // player handling
  1908. if (target)
  1909. {
  1910. if (data->icon_backdrop == 316 || data->icon_backdrop == 312) // using TARGET backdrop icon
  1911. {
  1912. // PLAYER LOGIC:
  1913. if ((data->friendly_spell && (target->IsPlayer() && luaspell->caster->IsPlayer() && target != luaspell->caster && ((Player*)target)->GetGroupMemberInfo() != NULL && ((Player*)luaspell->caster)->GetGroupMemberInfo() != NULL
  1914. && ((Player*)target)->GetGroupMemberInfo()->group_id == ((Player*)luaspell->caster)->GetGroupMemberInfo()->group_id))
  1915. || (!data->friendly_spell && (target->IsPlayer() && luaspell->caster->IsPlayer() && target != luaspell->caster && ((Player*)target)->GetGroupMemberInfo() != NULL)))
  1916. {
  1917. GetPlayerGroupTargets((Player*)target, caster, luaspell, true, false);
  1918. }
  1919. //TODO: NEED RAID SUPPORT
  1920. // NPC LOGIC:
  1921. else if (target->IsNPC())
  1922. {
  1923. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  1924. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  1925. if (group)
  1926. {
  1927. vector<Spawn*>::iterator itr;
  1928. // iterate through spawn group members
  1929. for (itr = group->begin(); itr != group->end(); itr++)
  1930. {
  1931. Spawn* group_member = *itr;
  1932. // 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()
  1933. if (group_member->GetZone() == caster->GetZone() &&
  1934. group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune() && (!((Entity*)group_member)->IsMezzed() || group_member == target))
  1935. AddLuaSpellTarget(luaspell, group_member->GetID());
  1936. // note: this should generate some hate towards the caster
  1937. }
  1938. } // end is spawngroup
  1939. else
  1940. AddLuaSpellTarget(luaspell, target->GetID()); // return single target NPC for non-friendly spell
  1941. }
  1942. else if(data->friendly_spell)
  1943. {
  1944. // add self
  1945. target = NULL;
  1946. AddSelfAndPet(luaspell, caster);
  1947. luaspell->initial_target = caster->GetID();
  1948. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1949. }
  1950. else if(target && target->IsPlayer())
  1951. {
  1952. if(!GetPlayerGroupTargets((Player*)target, caster, luaspell, true, false))
  1953. AddSelfAndPet(luaspell, target);
  1954. }
  1955. }
  1956. else // default self cast for group/raid AE
  1957. {
  1958. target = caster;
  1959. luaspell->initial_target = caster->GetID();
  1960. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1961. }
  1962. // spell target versus self cast
  1963. }
  1964. else if (data->icon_backdrop != 316) // default self cast for group/raid AE and not using TARGET backdrop icon
  1965. {
  1966. target = caster;
  1967. luaspell->initial_target = caster->GetID();
  1968. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1969. }
  1970. }
  1971. else if (target_type == SPELL_TARGET_SELF){
  1972. target = caster;
  1973. luaspell->initial_target = caster->GetID();
  1974. luaspell->initial_target_char_id = (caster && caster->IsPlayer()) ? ((Player*)caster)->GetCharacterID() : 0;
  1975. }
  1976. //if using implied target, target = the implied target
  1977. if (implied)
  1978. {
  1979. target = secondary_target;
  1980. luaspell->initial_target = secondary_target->GetID();
  1981. luaspell->initial_target_char_id = (secondary_target && secondary_target->IsPlayer()) ? ((Player*)secondary_target)->GetCharacterID() : 0;
  1982. }
  1983. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  1984. // Group AE type NOTE: Add support for RAID AE to affect raid members once raids have been completed
  1985. if (target_type == SPELL_TARGET_GROUP_AE || spell_type == SPELL_TYPE_ALLGROUPTARGETS || target_type == SPELL_TARGET_RAID_AE)
  1986. {
  1987. if (data->icon_backdrop == 316) // single target in a group/raid
  1988. {
  1989. if (target)
  1990. AddLuaSpellTarget(luaspell, target->GetID(), false);
  1991. }
  1992. // is friendly
  1993. else if (data->friendly_spell)
  1994. {
  1995. // caster is an Entity
  1996. if (luaspell->caster->IsEntity())
  1997. {
  1998. // entity caster is in a player group
  1999. if (((Entity*)caster)->GetGroupMemberInfo())
  2000. {
  2001. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2002. // get group members
  2003. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Entity*)caster)->GetGroupMemberInfo()->group_id);
  2004. if (group)
  2005. {
  2006. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2007. deque<GroupMemberInfo*>* members = group->GetMembers();
  2008. deque<GroupMemberInfo*>::iterator itr;
  2009. // iterate through list of group members
  2010. for (itr = members->begin(); itr != members->end(); itr++)
  2011. {
  2012. // get group member player info
  2013. Entity* group_member = (*itr)->member;
  2014. if(!group_member){
  2015. continue;
  2016. }
  2017. LogWrite(SPELL__DEBUG, 0, "Player", "%s is group member for spell %s", group_member->GetName(), luaspell->spell->GetName());
  2018. // if the group member is in the casters zone, and is alive
  2019. if( group_member->Alive())
  2020. {
  2021. if(group_member->GetZone() != caster->GetZone())
  2022. {
  2023. SpellProcess::AddSelfAndPetToCharTargets(luaspell, group_member);
  2024. }
  2025. else if (group_member->GetZone() == luaspell->caster->GetZone()) {
  2026. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2027. if (group_member->HasPet()) {
  2028. Entity* pet = group_member->GetPet();
  2029. if (!pet)
  2030. pet = group_member->GetCharmedPet();
  2031. if (pet)
  2032. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2033. 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());
  2034. }
  2035. }
  2036. }
  2037. }
  2038. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2039. }
  2040. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2041. }
  2042. else
  2043. AddSelfAndPet(luaspell, caster); // else caster is not in a group, thus alone
  2044. }
  2045. else if (caster->IsNPC()) // caster is NOT a player
  2046. {
  2047. // caster is NPC and in a spawn group with other NPCs
  2048. vector<Spawn*>* group = ((NPC*)caster)->GetSpawnGroup();
  2049. if (group)
  2050. {
  2051. vector<Spawn*>::iterator itr;
  2052. for (itr = group->begin(); itr != group->end(); itr++)
  2053. {
  2054. Spawn* group_member = *itr;
  2055. if (group_member->IsNPC() && group_member->Alive()){
  2056. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2057. if (((Entity*)group_member)->HasPet()){
  2058. Entity* pet = ((Entity*)group_member)->GetPet();
  2059. if (pet)
  2060. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2061. pet = ((Entity*)group_member)->GetCharmedPet();
  2062. if (pet)
  2063. AddLuaSpellTarget(luaspell, pet->GetID(), false);
  2064. }
  2065. }
  2066. }
  2067. }
  2068. else
  2069. AddSelfAndPet(luaspell, caster);
  2070. safe_delete(group);
  2071. } // end is player
  2072. } // end is friendly
  2073. } // end is Group AE
  2074. else if (target_type == SPELL_TARGET_SELF && caster)
  2075. AddLuaSpellTarget(luaspell, caster->GetID(), false); // if spell is SELF, return caster
  2076. else if (target_type == SPELL_TARGET_CASTER_PET && caster && caster->IsEntity() && ((Entity*)caster)->HasPet()) {
  2077. AddSelfAndPet(luaspell, caster, true);
  2078. }
  2079. else if (target_type == SPELL_TARGET_ENEMY && target && target->Alive()) // if target is enemy, and is alive
  2080. {
  2081. // if friendly spell
  2082. if (data->friendly_spell > 0)
  2083. {
  2084. // if caster is a player
  2085. if (caster->IsPlayer())
  2086. {
  2087. // if spell can affect raid, only group members or is a group spell
  2088. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  2089. {
  2090. // if caster is in a group, and target is a player and targeted player is a group member
  2091. if (((Player*)caster)->GetGroupMemberInfo() && (target->IsPlayer() || target->IsBot() || target->IsPet()) && ((Player*)caster)->IsGroupMember((Entity*)target))
  2092. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2093. else
  2094. AddLuaSpellTarget(luaspell, caster->GetID(), false); // else return the caster
  2095. }
  2096. else if (target->IsPlayer() || target->IsBot()) // else it is not raid, group only or group spell
  2097. AddLuaSpellTarget(luaspell, target->GetID(), false); // return target for single spell
  2098. else if ((luaspell->targets.size() < 1) || (!target->IsPet() || (((Entity*)target)->GetOwner() && !((Entity*)target)->GetOwner()->IsPlayer())))
  2099. AddLuaSpellTarget(luaspell, caster->GetID(), false); // and if no target, cast on self
  2100. }
  2101. else if (caster->IsNPC()) // caster is an NPC
  2102. {
  2103. // if NPC's spell can affect raid, only group members or is a group spell
  2104. if (data->can_effect_raid > 0 || data->affect_only_group_members > 0 || data->group_spell > 0)
  2105. {
  2106. if (caster->IsBot() && (target->IsBot() || target->IsPlayer())) {
  2107. GroupMemberInfo* gmi = ((Entity*)caster)->GetGroupMemberInfo();
  2108. if (gmi && target->IsEntity() && world.GetGroupManager()->IsInGroup(gmi->group_id, (Entity*)target)) {
  2109. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2110. }
  2111. else
  2112. AddSelfAndPet(luaspell, caster);
  2113. }
  2114. // if NPC caster is in a group, and target is a player and targeted player is a group member
  2115. else if (((NPC*)caster)->HasSpawnGroup() && target->IsNPC() && ((NPC*)caster)->IsInSpawnGroup((NPC*)target))
  2116. AddLuaSpellTarget(luaspell, target->GetID(), false); // return the target
  2117. else
  2118. AddSelfAndPet(luaspell, caster);
  2119. }
  2120. else if (target->IsNPC())
  2121. AddLuaSpellTarget(luaspell, target->GetID(), false); // return target for single spell
  2122. else {
  2123. if (caster->IsBot() && (target->IsBot() || target->IsPlayer()))
  2124. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2125. else
  2126. AddLuaSpellTarget(luaspell, caster->GetID(), false); // and if no target, cast on self
  2127. }
  2128. } // end is player
  2129. } // end is friendly
  2130. 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)
  2131. {
  2132. // target is non-player
  2133. if (target->IsNPC())
  2134. {
  2135. // Check to see if the npc is a spawn group by getting the group and checikng if valid
  2136. vector<Spawn*>* group = ((NPC*)target)->GetSpawnGroup();
  2137. if (group)
  2138. {
  2139. vector<Spawn*>::iterator itr;
  2140. // iterate through spawn group members
  2141. for (itr = group->begin(); itr != group->end(); itr++)
  2142. {
  2143. Spawn* group_member = *itr;
  2144. // 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()
  2145. if (group_member->GetZone() == caster->GetZone() &&
  2146. group_member->IsNPC() && group_member->Alive() && !((Entity*)group_member)->IsAOEImmune() && (!((Entity*)group_member)->IsMezzed() || group_member == target))
  2147. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2148. // note: this should generate some hate towards the caster
  2149. }
  2150. } // end is spawngroup
  2151. else
  2152. AddLuaSpellTarget(luaspell, target->GetID(), false); // return single target NPC for non-friendly spell
  2153. safe_delete(group);
  2154. } // end is NPC
  2155. else if (target->IsPlayer() && caster->IsNPC()) // the NPC is casting on a player
  2156. {
  2157. // player is in a group
  2158. if (((Player*)target)->GetGroupMemberInfo())
  2159. {
  2160. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2161. deque<GroupMemberInfo*>::iterator itr;
  2162. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2163. if (group)
  2164. {
  2165. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2166. deque<GroupMemberInfo*>* members = group->GetMembers();
  2167. // iterate through players group members
  2168. for (itr = members->begin(); itr != members->end(); itr++)
  2169. {
  2170. Entity* group_member = (*itr)->member;
  2171. // if the group member is in the same zone as caster, and group member is alive, and group member is within distance
  2172. if (group_member && group_member->GetZone() == caster->GetZone() && group_member->Alive() && caster->GetDistance(group_member) <= data->range
  2173. && (group_member == target || !group_member->IsAOEImmune()))
  2174. AddLuaSpellTarget(luaspell, group_member->GetID(), false); // add as target
  2175. }
  2176. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2177. }
  2178. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2179. }
  2180. else
  2181. AddLuaSpellTarget(luaspell, target->GetID(), false); // player not in group
  2182. } // end is caster player or npc
  2183. }
  2184. else if (target)
  2185. AddLuaSpellTarget(luaspell, target->GetID(), false); // is not friendly nor a group spell
  2186. }
  2187. //Rez spells
  2188. else if(target && target_type == SPELL_TARGET_ENEMY_CORPSE){
  2189. //is friendly
  2190. if(data->friendly_spell){
  2191. //target is player
  2192. if(target->IsPlayer()){
  2193. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2194. }
  2195. }
  2196. }
  2197. else if(target_type == SPELL_TARGET_GROUP_CORPSE){
  2198. //is friendly
  2199. if(data->friendly_spell){
  2200. //target is player
  2201. if(target && target->IsPlayer()){
  2202. //if target has group
  2203. if(((Player*)target)->GetGroupMemberInfo()) {
  2204. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  2205. deque<GroupMemberInfo*>::iterator itr;
  2206. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2207. if (group)
  2208. {
  2209. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2210. deque<GroupMemberInfo*>* members = group->GetMembers();
  2211. Entity* group_member = 0;
  2212. for (itr = members->begin(); itr != members->end(); itr++) {
  2213. group_member = (*itr)->member;
  2214. //Check if group member is in the same zone in range of the spell and dead
  2215. if (group_member && group_member->GetZone() == target->GetZone() && !group_member->Alive() && target->GetDistance(group_member) <= data->radius) {
  2216. AddLuaSpellTarget(luaspell, group_member->GetID(), false);
  2217. }
  2218. }
  2219. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2220. }
  2221. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2222. }
  2223. else
  2224. AddLuaSpellTarget(luaspell, target->GetID(), false);
  2225. }
  2226. }
  2227. }
  2228. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2229. }
  2230. if (luaspell && luaspell->targets.size() > 20)
  2231. LogWrite(SPELL__WARNING, 0, "Spell", "Warning in %s: Size of targets array is %u", __FUNCTION__, luaspell->targets.size());
  2232. }
  2233. bool SpellProcess::GetPlayerGroupTargets(Player* target, Spawn* caster, LuaSpell* luaspell, bool bypassSpellChecks, bool bypassRangeChecks)
  2234. {
  2235. if (bypassSpellChecks || luaspell->spell->GetSpellData()->group_spell > 0 || luaspell->spell->GetSpellData()->icon_backdrop == 312)
  2236. {
  2237. if (((Player*)target)->GetGroupMemberInfo())
  2238. {
  2239. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)target)->GetGroupMemberInfo()->group_id);
  2240. if (group)
  2241. {
  2242. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2243. deque<GroupMemberInfo*>* members = group->GetMembers();
  2244. deque<GroupMemberInfo*>::iterator itr;
  2245. GroupMemberInfo* info = 0;
  2246. for (itr = members->begin(); itr != members->end(); itr++) {
  2247. info = *itr;
  2248. if (info == ((Player*)target)->GetGroupMemberInfo())
  2249. continue;
  2250. else if (info && info->client &&
  2251. info->client->GetPlayer()->GetZone() == ((Player*)target)->GetZone() && info->client->GetPlayer()->Alive()
  2252. && (bypassRangeChecks || caster->GetDistance((Entity*)info->client->GetPlayer()) <= luaspell->spell->GetSpellData()->range))
  2253. {
  2254. AddSelfAndPet(luaspell, info->client->GetPlayer());
  2255. }
  2256. }
  2257. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2258. return true;
  2259. }
  2260. }
  2261. }
  2262. return false;
  2263. }
  2264. void SpellProcess::GetSpellTargetsTrueAOE(LuaSpell* luaspell) {
  2265. if (luaspell && luaspell->caster && luaspell->spell && luaspell->spell->GetSpellData()->max_aoe_targets > 0) {
  2266. if (luaspell->caster->HasTarget() && luaspell->caster->GetTarget() != luaspell->caster){
  2267. //Check if the caster has an implied target
  2268. if (luaspell->caster->GetDistance(luaspell->caster->GetTarget()) <= luaspell->spell->GetSpellData()->radius)
  2269. {
  2270. luaspell->initial_target = luaspell->caster->GetTarget()->GetID();
  2271. luaspell->initial_target_char_id = (luaspell->caster->GetTarget() && luaspell->caster->GetTarget()->IsPlayer()) ? ((Player*)luaspell->caster->GetTarget())->GetCharacterID() : 0;
  2272. }
  2273. }
  2274. int32 ignore_target = 0;
  2275. vector<Spawn*> spawns = luaspell->caster->GetZone()->GetAttackableSpawnsByDistance(luaspell->caster, luaspell->spell->GetSpellData()->radius);
  2276. luaspell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2277. for (int8 i = 0; i < spawns.size(); i++) {
  2278. Spawn* spawn = spawns.at(i);
  2279. if (i == 0){
  2280. if (luaspell->initial_target && luaspell->caster->GetID() != luaspell->initial_target){
  2281. //this is the "Direct" target and aoe can't be avoided
  2282. AddLuaSpellTarget(luaspell, luaspell->initial_target, false);
  2283. ignore_target = luaspell->initial_target;
  2284. }
  2285. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  2286. break;
  2287. }
  2288. //If we have already added this spawn, check the next spawn in the list
  2289. if (spawn && spawn->GetID() == ignore_target){
  2290. i++;
  2291. if (i < spawns.size())
  2292. spawn = spawns.at(i);
  2293. else
  2294. break;
  2295. }
  2296. if (spawn){
  2297. //If this spawn is immune to aoe, continue
  2298. if (((Entity*)spawn)->IsAOEImmune() || ((Entity*)spawn)->IsMezzed())
  2299. continue;
  2300. AddLuaSpellTarget(luaspell, spawn->GetID(), false);
  2301. }
  2302. if (luaspell->targets.size() >= luaspell->spell->GetSpellData()->max_aoe_targets)
  2303. break;
  2304. }
  2305. luaspell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2306. }
  2307. if (luaspell->targets.size() > 20)
  2308. LogWrite(SPELL__DEBUG, 0, "Spell", "Warning in SpellProcess::GetSpellTargetsTrueAOE Size of targets array is %u", luaspell->targets.size());
  2309. }
  2310. void SpellProcess::AddSpellScriptTimer(SpellScriptTimer* timer) {
  2311. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2312. m_spellScriptList.push_back(timer);
  2313. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2314. }
  2315. void SpellProcess::RemoveSpellScriptTimer(SpellScriptTimer* timer, bool locked) {
  2316. if (m_spellScriptList.size() == 0)
  2317. return;
  2318. vector<SpellScriptTimer*>::iterator itr;
  2319. if(!locked)
  2320. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2321. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2322. if ((*itr) == timer) {
  2323. SpellScriptTimer* timer = *itr;
  2324. if ((*itr) && (*itr)->deleteWhenDone && lua_interface) {
  2325. lua_interface->AddPendingSpellDelete(timer->spell);
  2326. }
  2327. m_spellScriptList.erase(itr);
  2328. safe_delete(timer);
  2329. break;
  2330. }
  2331. }
  2332. if(!locked)
  2333. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2334. }
  2335. void SpellProcess::RemoveSpellScriptTimerBySpell(LuaSpell* spell, bool clearPendingDeletes) {
  2336. vector<SpellScriptTimer*>::iterator itr;
  2337. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2338. if (lua_interface && clearPendingDeletes)
  2339. lua_interface->DeletePendingSpell(spell);
  2340. if (m_spellScriptList.size() == 0)
  2341. {
  2342. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2343. return;
  2344. }
  2345. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); ) {
  2346. if ((*itr)->spell == spell)
  2347. {
  2348. vector<SpellScriptTimer*>::iterator cur = itr;
  2349. SpellScriptTimer* timer = *itr;
  2350. m_spellScriptList.erase(cur);
  2351. safe_delete(timer);
  2352. break;
  2353. }
  2354. else
  2355. itr++;
  2356. }
  2357. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2358. }
  2359. void SpellProcess::CheckSpellScriptTimers() {
  2360. vector<SpellScriptTimer*>::iterator itr;
  2361. vector<SpellScriptTimer*> temp_list;
  2362. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2363. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2364. if (Timer::GetCurrentTime2() >= (*itr)->time) {
  2365. temp_list.push_back((*itr));
  2366. ProcessSpell((*itr)->spell, false, (*itr)->customFunction.c_str(), (*itr));
  2367. }
  2368. }
  2369. for (itr = temp_list.begin(); itr != temp_list.end(); itr++) {
  2370. RemoveSpellScriptTimer(*itr, true);
  2371. }
  2372. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2373. }
  2374. bool SpellProcess::SpellScriptTimersHasSpell(LuaSpell* spell) {
  2375. bool ret = false;
  2376. vector<SpellScriptTimer*>::iterator itr;
  2377. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  2378. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2379. SpellScriptTimer* timer = *itr;
  2380. if (timer && timer->spell == spell) {
  2381. ret = true;
  2382. break;
  2383. }
  2384. }
  2385. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  2386. return ret;
  2387. }
  2388. std::string SpellProcess::SpellScriptTimerCustomFunction(LuaSpell* spell) {
  2389. bool ret = false;
  2390. std::string val = string("");
  2391. vector<SpellScriptTimer*>::iterator itr;
  2392. MSpellScriptTimers.readlock(__FUNCTION__, __LINE__);
  2393. for (itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2394. SpellScriptTimer* timer = *itr;
  2395. if (timer && timer->spell == spell) {
  2396. val = string(timer->customFunction);
  2397. break;
  2398. }
  2399. }
  2400. MSpellScriptTimers.releasereadlock(__FUNCTION__, __LINE__);
  2401. return val;
  2402. }
  2403. void SpellProcess::ClearSpellScriptTimerList() {
  2404. vector<SpellScriptTimer*>::iterator itr;
  2405. MSpellScriptTimers.writelock(__FUNCTION__, __LINE__);
  2406. for(itr = m_spellScriptList.begin(); itr != m_spellScriptList.end(); itr++) {
  2407. if ((*itr) && (*itr)->deleteWhenDone && lua_interface)
  2408. lua_interface->AddPendingSpellDelete((*itr)->spell);
  2409. safe_delete((*itr));
  2410. }
  2411. m_spellScriptList.clear();
  2412. MSpellScriptTimers.releasewritelock(__FUNCTION__, __LINE__);
  2413. }
  2414. void SpellProcess::RemoveTargetFromSpell(LuaSpell* spell, Spawn* target, bool removeCaster){
  2415. if (!spell || !target)
  2416. return;
  2417. LogWrite(SPELL__DEBUG, 0, "Spell", "%s RemoveTargetFromSpell %s (%u).", spell->spell->GetName(), target->GetName(), target->GetID());
  2418. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  2419. if(removeCaster && spell->caster && spell->caster == target)
  2420. spell->caster = nullptr;
  2421. if (!remove_target_list[spell])
  2422. remove_target_list[spell] = new vector<int32>;
  2423. remove_target_list[spell]->push_back(target->GetID());
  2424. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  2425. }
  2426. void SpellProcess::CheckRemoveTargetFromSpell(LuaSpell* spell, bool allow_delete, bool removing_all_spells){
  2427. if (!spell)
  2428. return;
  2429. if (remove_target_list.size() > 0){
  2430. map<LuaSpell*, vector<int32>*>::iterator remove_itr;
  2431. vector<int32>::iterator remove_target_itr;
  2432. vector<int32>::iterator target_itr;
  2433. vector<int32>* targets;
  2434. vector<int32>* remove_targets = 0;
  2435. Spawn* remove_spawn = 0;
  2436. bool should_delete = false;
  2437. MRemoveTargetList.writelock(__FUNCTION__, __LINE__);
  2438. for (remove_itr = remove_target_list.begin(); remove_itr != remove_target_list.end(); remove_itr++){
  2439. if (remove_itr->first == spell){
  2440. targets = &spell->targets;
  2441. remove_targets = remove_itr->second;
  2442. if (remove_targets && targets){
  2443. for (remove_target_itr = remove_targets->begin(); remove_target_itr != remove_targets->end(); remove_target_itr++){
  2444. if(!spell->caster || !spell->caster->GetZone())
  2445. continue;
  2446. remove_spawn = spell->caster->GetZone()->GetSpawnByID((*remove_target_itr));
  2447. if (remove_spawn) {
  2448. spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2449. if(remove_spawn && remove_spawn->IsPlayer())
  2450. {
  2451. multimap<int32,int8>::iterator entries;
  2452. while((entries = spell->char_id_targets.find(((Player*)remove_spawn)->GetCharacterID())) != spell->char_id_targets.end())
  2453. {
  2454. spell->char_id_targets.erase(entries);
  2455. }
  2456. }
  2457. for (target_itr = targets->begin(); target_itr != targets->end(); target_itr++) {
  2458. if (remove_spawn->GetID() == (*target_itr)) {
  2459. ((Entity*)remove_spawn)->RemoveProc(0, spell);
  2460. ((Entity*)remove_spawn)->RemoveMaintainedSpell(spell);
  2461. LogWrite(SPELL__DEBUG, 0, "Spell", "%s CheckRemoveTargetFromSpell %s (%u).", spell->spell->GetName(), remove_spawn->GetName(), remove_spawn->GetID());
  2462. targets->erase(target_itr);
  2463. if (remove_spawn->IsEntity())
  2464. {
  2465. if(!removing_all_spells)
  2466. {
  2467. if(remove_spawn->IsPlayer())
  2468. spell->char_id_targets.insert(make_pair(((Player*)remove_spawn)->GetCharacterID(),0));
  2469. else if(remove_spawn->IsPet() && ((Entity*)remove_spawn)->GetOwner() && ((Entity*)remove_spawn)->GetOwner()->IsPlayer())
  2470. {
  2471. Entity* pet = (Entity*)remove_spawn;
  2472. Player* ownerPlayer = (Player*)pet->GetOwner();
  2473. spell->char_id_targets.insert(make_pair(ownerPlayer->GetCharacterID(),pet->GetPetType()));
  2474. }
  2475. }
  2476. ((Entity*)remove_spawn)->RemoveEffectsFromLuaSpell(spell);
  2477. }
  2478. break;
  2479. }
  2480. }
  2481. spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2482. if (targets->size() == 0 && spell->char_id_targets.size() == 0 && allow_delete) {
  2483. should_delete = true;
  2484. break;
  2485. }
  2486. }
  2487. }
  2488. }
  2489. break;
  2490. }
  2491. }
  2492. remove_target_list.erase(spell);
  2493. if (remove_targets)
  2494. remove_targets->clear();
  2495. safe_delete(remove_targets);
  2496. MRemoveTargetList.releasewritelock(__FUNCTION__, __LINE__);
  2497. if (should_delete)
  2498. DeleteCasterSpell(spell, "purged");
  2499. }
  2500. }
  2501. bool SpellProcess::AddHO(Client* client, HeroicOP* ho) {
  2502. bool ret = true;
  2503. if (client && ho) {
  2504. MSoloHO.writelock(__FUNCTION__, __LINE__);
  2505. if (m_soloHO.count(client) > 0) {
  2506. if (m_soloHO[client]->GetWheel()) {
  2507. ret = false;
  2508. }
  2509. else {
  2510. safe_delete(m_soloHO[client]);
  2511. m_soloHO[client] = ho;
  2512. }
  2513. }
  2514. else
  2515. m_soloHO[client] = ho;
  2516. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  2517. }
  2518. return ret;
  2519. }
  2520. bool SpellProcess::AddHO(int32 group_id, HeroicOP* ho) {
  2521. bool ret = true;
  2522. if (group_id > 0 && ho) {
  2523. MGroupHO.writelock(__FUNCTION__, __LINE__);
  2524. if (m_groupHO.count(group_id) > 0) {
  2525. if (m_groupHO[group_id]->GetWheel()) {
  2526. ret = false;
  2527. }
  2528. else {
  2529. safe_delete(m_groupHO[group_id]);
  2530. m_groupHO[group_id] = ho;
  2531. }
  2532. }
  2533. else
  2534. m_groupHO[group_id] = ho;
  2535. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  2536. }
  2537. return ret;
  2538. }
  2539. void SpellProcess::KillHOBySpawnID(int32 spawn_id) {
  2540. // Check solo HO's
  2541. MSoloHO.writelock(__FUNCTION__, __LINE__);
  2542. map<Client*, HeroicOP*>::iterator itr = m_soloHO.begin();
  2543. map<Client*, HeroicOP*>::iterator delete_itr;
  2544. while (itr != m_soloHO.end()) {
  2545. if (itr->second->GetTarget() == spawn_id) {
  2546. itr->second->SetComplete(1);
  2547. ClientPacketFunctions::SendHeroicOPUpdate(itr->first, itr->second);
  2548. delete_itr = itr;
  2549. safe_delete(itr->second);
  2550. itr++;
  2551. m_soloHO.erase(delete_itr);
  2552. }
  2553. else
  2554. itr++;
  2555. }
  2556. MSoloHO.releasewritelock(__FUNCTION__, __LINE__);
  2557. // Check Group HO's
  2558. MGroupHO.writelock(__FUNCTION__, __LINE__);
  2559. map<int32, HeroicOP*>::iterator itr2 = m_groupHO.begin();
  2560. map<int32, HeroicOP*>::iterator delete_itr2;
  2561. while (itr2 != m_groupHO.end()) {
  2562. if (itr2->second->GetTarget() == spawn_id) {
  2563. itr2->second->SetComplete(1);
  2564. world.GetGroupManager()->GroupLock(__FUNCTION__ , __LINE__);
  2565. deque<GroupMemberInfo*>::iterator itr3;
  2566. PlayerGroup* group = world.GetGroupManager()->GetGroup(itr2->first);
  2567. if (group)
  2568. {
  2569. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  2570. deque<GroupMemberInfo*>* members = group->GetMembers();
  2571. for (itr3 = members->begin(); itr3 != members->end(); itr3++) {
  2572. if ((*itr3)->client)
  2573. ClientPacketFunctions::SendHeroicOPUpdate((*itr3)->client, itr2->second);
  2574. }
  2575. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  2576. }
  2577. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  2578. delete_itr2 = itr2;
  2579. safe_delete(itr2->second);
  2580. itr2++;
  2581. m_groupHO.erase(delete_itr2);
  2582. }
  2583. else
  2584. itr2++;
  2585. }
  2586. MGroupHO.releasewritelock(__FUNCTION__, __LINE__);
  2587. }
  2588. void SpellProcess::AddSpellCancel(LuaSpell* spell){
  2589. MSpellCancelList.writelock(__FUNCTION__, __LINE__);
  2590. SpellCancelList.push_back(spell);
  2591. MSpellCancelList.releasewritelock(__FUNCTION__, __LINE__);
  2592. }
  2593. void SpellProcess::DeleteSpell(LuaSpell* spell)
  2594. {
  2595. RemoveSpellFromQueue(spell->spell, spell->caster);
  2596. if (spell->spell->IsCopiedSpell())
  2597. {
  2598. lua_interface->RemoveCustomSpell(spell->spell->GetSpellID());
  2599. safe_delete(spell->spell);
  2600. }
  2601. safe_delete(spell);
  2602. }
  2603. void SpellProcess::SpellCannotStack(ZoneServer* zone, Client* client, Entity* caster, LuaSpell* lua_spell, LuaSpell* conflictSpell)
  2604. {
  2605. LogWrite(SPELL__DEBUG, 1, "Spell", "%s cannot stack spell %s, conflicts with %s.", caster->GetName(), lua_spell->spell->GetName(), conflictSpell->spell->GetName());
  2606. zone->SendSpellFailedPacket(client, SPELL_ERROR_TAKE_EFFECT_MOREPOWERFUL);
  2607. lua_spell->caster->GetZone()->GetSpellProcess()->RemoveSpellScriptTimerBySpell(lua_spell);
  2608. DeleteSpell(lua_spell);
  2609. }
  2610. void SpellProcess::AddActiveSpell(LuaSpell* spell)
  2611. {
  2612. if(!active_spells.count(spell))
  2613. active_spells.Add(spell);
  2614. }
  2615. void SpellProcess::AddSelfAndPet(LuaSpell* spell, Spawn* self, bool onlyPet)
  2616. {
  2617. if(!onlyPet)
  2618. AddLuaSpellTarget(spell, self->GetID(), false);
  2619. if(self->IsEntity() && ((Entity*)self)->HasPet() && ((Entity*)self)->GetPet())
  2620. AddLuaSpellTarget(spell, ((Entity*)self)->GetPet()->GetID(), false);
  2621. if(self->IsEntity() && ((Entity*)self)->HasPet() && ((Entity*)self)->GetCharmedPet())
  2622. AddLuaSpellTarget(spell, ((Entity*)self)->GetCharmedPet()->GetID(), false);
  2623. if(!onlyPet && self->IsEntity() && ((Entity*)self)->IsPet() && ((Entity*)self)->GetOwner())
  2624. AddLuaSpellTarget(spell, ((Entity*)self)->GetOwner()->GetID(), false);
  2625. }
  2626. void SpellProcess::AddSelfAndPetToCharTargets(LuaSpell* spell, Spawn* caster, bool onlyPet)
  2627. {
  2628. if(!caster->IsPlayer())
  2629. return;
  2630. Player* player = ((Player*)caster);
  2631. int32 charID = player->GetCharacterID();
  2632. if(player->HasPet() && player->GetPet())
  2633. spell->char_id_targets.insert(make_pair(charID, player->GetPet()->GetPetType()));
  2634. if(player->HasPet() && player->GetCharmedPet())
  2635. spell->char_id_targets.insert(make_pair(charID, player->GetPet()->GetPetType()));
  2636. if(!onlyPet)
  2637. spell->char_id_targets.insert(make_pair(charID, 0x00));
  2638. }
  2639. void SpellProcess::DeleteActiveSpell(LuaSpell* spell) {
  2640. active_spells.Remove(spell);
  2641. }
  2642. bool SpellProcess::AddLuaSpellTarget(LuaSpell* lua_spell, int32 id, bool lock_spell_targets) {
  2643. bool ret = false;
  2644. if(lock_spell_targets)
  2645. lua_spell->MSpellTargets.writelock(__FUNCTION__, __LINE__);
  2646. if(std::find(lua_spell->removed_targets.begin(), lua_spell->removed_targets.end(), id) == lua_spell->removed_targets.end()) {
  2647. lua_spell->targets.push_back(id);
  2648. ret = true;
  2649. }
  2650. if(lock_spell_targets)
  2651. lua_spell->MSpellTargets.releasewritelock(__FUNCTION__, __LINE__);
  2652. return ret;
  2653. }