SpellProcess.cpp 111 KB

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