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