SpellProcess.cpp 86 KB

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