SpellProcess.cpp 84 KB

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