SpellProcess.cpp 108 KB

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