SpellProcess.cpp 110 KB

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