SpellProcess.cpp 92 KB

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