SpellProcess.cpp 78 KB

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