SpellProcess.cpp 73 KB

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