SpellProcess.cpp 82 KB

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