9
3

SpellProcess.cpp 104 KB

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