SpellProcess.cpp 102 KB

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