SpellProcess.cpp 110 KB

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