9
3

SpellProcess.cpp 114 KB

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