SpellProcess.cpp 110 KB

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