SpellProcess.cpp 74 KB

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