SpellProcess.cpp 97 KB

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