SpellProcess.cpp 108 KB

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