SpellProcess.cpp 98 KB

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