SpellProcess.cpp 86 KB

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