SpellProcess.cpp 101 KB

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