SpellProcess.cpp 82 KB

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