SpellProcess.cpp 73 KB

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