SpellProcess.cpp 88 KB

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