9
3

SpellProcess.cpp 114 KB

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