Combat.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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 "Combat.h"
  17. #include "client.h"
  18. #include "../common/ConfigReader.h"
  19. #include "classes.h"
  20. #include "../common/debug.h"
  21. #include "../common/Log.h"
  22. #include "zoneserver.h"
  23. #include "Skills.h"
  24. #include "classes.h"
  25. #include "World.h"
  26. #include "LuaInterface.h"
  27. #include "Rules/Rules.h"
  28. #include "SpellProcess.h"
  29. #include "World.h"
  30. #include <math.h>
  31. extern Classes classes;
  32. extern ConfigReader configReader;
  33. extern MasterSkillList master_skill_list;
  34. extern RuleManager rule_manager;
  35. extern LuaInterface* lua_interface;
  36. extern World world;
  37. /* ******************************************************************************
  38. DamageSpawn() - Damage equation
  39. MeleeAttack() - Melee auto attacks
  40. RangeAttack() - Range auto attacks
  41. DetermineHit() - ToHit chance as well as defender parry / dodge / block / riposte
  42. CheckInterruptSpell() - Interrupt equations
  43. No mitigation equations yet
  44. ****************************************************************************** */
  45. /* New Combat code */
  46. bool Entity::PrimaryWeaponReady() {
  47. //Can only be ready if no ranged timer
  48. if (GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) {
  49. if (GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  50. return true;
  51. }
  52. return false;
  53. }
  54. bool Entity::SecondaryWeaponReady() {
  55. //Can only be ready if no ranged timer
  56. // if(IsDualWield() && (GetPrimaryLastAttackTime()
  57. if (IsDualWield() && (GetSecondaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay())))) {
  58. if(GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool Entity::RangeWeaponReady() {
  64. //Ranged can only be ready if no other attack timers are active
  65. if(GetRangeLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))) {
  66. if((GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) && (GetSecondaryLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay()))){
  67. if(!IsPlayer() || ((Player*)this)->GetRangeAttack()) {
  68. return true;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. bool Entity::AttackAllowed(Entity* target, float distance, bool range_attack) {
  75. Entity* attacker = this;
  76. Client* client = 0;
  77. if(!target || IsMezzedOrStunned() || IsDazed()) {
  78. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: no target, mezzed, stunned or dazed");
  79. return false;
  80. }
  81. if (IsPlayer())
  82. client = GetZone()->GetClientBySpawn(this);
  83. if (IsPet())
  84. attacker = ((NPC*)this)->GetOwner();
  85. if (target->IsNPC() && ((NPC*)target)->IsPet()){
  86. if (((NPC*)target)->GetOwner())
  87. target = ((NPC*)target)->GetOwner();
  88. }
  89. if (attacker == target) {
  90. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: attacker tried to attack himself or his pet.");
  91. return false;
  92. }
  93. if (IsPlayer() && target->GetAttackable() == 0) {
  94. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is not attackable");
  95. return false;
  96. }
  97. if (IsPlayer() && target->IsBot()) {
  98. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: players are not allowed to attack bots");
  99. return false;
  100. }
  101. if(rule_manager.GetGlobalRule(R_Combat, LockedEncounterNoAttack)->GetBool()) {
  102. if(target->IsNPC() && (target->GetLockedNoLoot() == ENCOUNTER_STATE_LOCKED || target->GetLockedNoLoot() == ENCOUNTER_STATE_OVERMATCHED) &&
  103. !attacker->IsEngagedBySpawnID(target->GetID())) {
  104. return false;
  105. }
  106. else if(IsNPC() && (GetLockedNoLoot() == ENCOUNTER_STATE_LOCKED || GetLockedNoLoot() == ENCOUNTER_STATE_OVERMATCHED) &&
  107. !target->IsEngagedBySpawnID(GetID())) {
  108. return false;
  109. }
  110. }
  111. if (attacker->IsPlayer() && target->IsPlayer())
  112. {
  113. bool pvp_allowed = rule_manager.GetGlobalRule(R_PVP, AllowPVP)->GetBool();
  114. if (!pvp_allowed) {
  115. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp is not allowed");
  116. return false;
  117. }
  118. else
  119. {
  120. sint32 pvpLevelRange = rule_manager.GetGlobalRule(R_PVP, LevelRange)->GetSInt32();
  121. int32 attackerLevel = attacker->GetLevel();
  122. int32 defenderLevel = target->GetLevel();
  123. if ((sint32)abs((sint32)attackerLevel - (sint32)defenderLevel) > pvpLevelRange)
  124. {
  125. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp range of %i exceeded abs(%i-%i).", pvpLevelRange, attackerLevel, defenderLevel);
  126. return false;
  127. }
  128. }
  129. }
  130. if (target->GetHP() <= 0) {
  131. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is dead");
  132. return false;
  133. }
  134. if(range_attack && distance != 0) {
  135. Item* weapon = 0;
  136. Item* ammo = 0;
  137. if(attacker->IsPlayer()) {
  138. weapon = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  139. ammo = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  140. }
  141. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  142. // Distance is less then min weapon range
  143. if(distance < weapon->ranged_info->range_low) {
  144. if (client)
  145. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too close! Move back!");
  146. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target to close");
  147. return false;
  148. }
  149. // Distance is greater then max weapon range
  150. if (distance > (weapon->ranged_info->range_high + ammo->thrown_info->range)) {
  151. if (client)
  152. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too far away! Move closer!");
  153. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target is to far");
  154. return false;
  155. }
  156. }
  157. }
  158. else if (distance != 0) {
  159. if(distance >= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  160. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: distance is beyond melee range");
  161. return false;
  162. }
  163. }
  164. if((target->IsPrivateSpawn() && !target->AllowedAccess(this)) || (IsPrivateSpawn() && AllowedAccess(target))) {
  165. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Access to spawn disallowed.");
  166. return false;
  167. }
  168. return true;
  169. }
  170. void Entity::MeleeAttack(Spawn* victim, float distance, bool primary, bool multi_attack) {
  171. if(!victim)
  172. return;
  173. int8 damage_type = 0;
  174. int32 min_damage = 0;
  175. int32 max_damage = 0;
  176. if(primary) {
  177. damage_type = GetPrimaryWeaponType();
  178. min_damage = GetPrimaryWeaponMinDamage();
  179. max_damage = GetPrimaryWeaponMaxDamage();
  180. }
  181. else {
  182. damage_type = GetSecondaryWeaponType();
  183. min_damage = GetSecondaryWeaponMinDamage();
  184. max_damage = GetSecondaryWeaponMaxDamage();
  185. }
  186. if (IsStealthed() || IsInvis())
  187. CancelAllStealth();
  188. int8 hit_result = DetermineHit(victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, damage_type, 0, false);
  189. if(victim->IsEntity()) {
  190. CheckEncounterState((Entity*)victim);
  191. }
  192. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL){
  193. /*if(GetAdventureClass() == MONK){
  194. max_damage*=3;
  195. crit_chance = GetLevel()/4+5;
  196. }
  197. else if(GetAdventureClass() == BRUISER){
  198. min_damage = GetLevel();
  199. max_damage*=3;
  200. crit_chance = GetLevel()/3+5;
  201. }
  202. if(rand()%100 <=crit_chance){
  203. max_damage*= 2;
  204. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG, damage_type, min_damage, max_damage, 0);
  205. }
  206. else*/
  207. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, damage_type, min_damage, max_damage, 0);
  208. if (!multi_attack) {
  209. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  210. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  211. }
  212. }
  213. else{
  214. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, hit_result, damage_type, 0, 0);
  215. if(hit_result == DAMAGE_PACKET_RESULT_RIPOSTE && victim->IsEntity())
  216. ((Entity*)victim)->MeleeAttack(this, distance, true);
  217. }
  218. //Multi Attack roll
  219. if(!multi_attack){
  220. float multi_attack = info_struct.get_multi_attack();
  221. if(multi_attack > 0){
  222. float chance = multi_attack;
  223. if (multi_attack > 100){
  224. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  225. chance = (multi_attack - (floor((float) ((multi_attack / 100) * 100))));
  226. while(automatic_multi > 0){
  227. MeleeAttack(victim, 100, primary, true);
  228. automatic_multi--;
  229. }
  230. }
  231. if (MakeRandomFloat(0, 100) <= chance)
  232. MeleeAttack(victim, 100, primary, true);
  233. }
  234. }
  235. //Apply attack speed mods
  236. if(!multi_attack)
  237. SetAttackDelay(primary);
  238. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  239. ((NPC*)victim)->AddHate(this, 50);
  240. }
  241. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  242. Entity* pet = 0;
  243. bool AddHate = false;
  244. if (victim->IsPlayer()) {
  245. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  246. AddHate = true;
  247. }
  248. else
  249. AddHate = true;
  250. if (AddHate) {
  251. pet = ((Entity*)victim)->GetPet();
  252. if (pet)
  253. pet->AddHate(this, 1);
  254. pet = ((Entity*)victim)->GetCharmedPet();
  255. if (pet)
  256. pet->AddHate(this, 1);
  257. }
  258. }
  259. }
  260. void Entity::RangeAttack(Spawn* victim, float distance, Item* weapon, Item* ammo, bool multi_attack) {
  261. if(!victim)
  262. return;
  263. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  264. if(weapon->ranged_info->range_low <= distance && (weapon->ranged_info->range_high + ammo->thrown_info->range) >= distance) {
  265. int8 hit_result = DetermineHit(victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, ammo->thrown_info->damage_type, ammo->thrown_info->hit_bonus, false);
  266. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  267. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, ammo->thrown_info->damage_type, weapon->ranged_info->weapon_info.damage_low3, weapon->ranged_info->weapon_info.damage_high3+ammo->thrown_info->damage_modifier, 0);
  268. if (!multi_attack) {
  269. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  270. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  271. }
  272. }
  273. else
  274. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, hit_result, ammo->thrown_info->damage_type, 0, 0);
  275. // If is a player subtract ammo
  276. if (IsPlayer()) {
  277. if (ammo->details.count > 1) {
  278. ammo->details.count -= 1;
  279. ammo->save_needed = true;
  280. }
  281. else
  282. ((Player*)this)->equipment_list.RemoveItem(ammo->details.slot_id, true);
  283. Client* client = GetZone()->GetClientBySpawn(this);
  284. EQ2Packet* outapp = ((Player*)this)->GetEquipmentList()->serialize(client->GetVersion(), (Player*)this);
  285. if(outapp)
  286. client->QueuePacket(outapp);
  287. }
  288. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  289. ((NPC*)victim)->AddHate(this, 50);
  290. }
  291. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  292. Entity* pet = 0;
  293. bool AddHate = false;
  294. if (victim->IsPlayer()) {
  295. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  296. AddHate = true;
  297. }
  298. else
  299. AddHate = true;
  300. if (AddHate) {
  301. pet = ((Entity*)victim)->GetPet();
  302. if (pet)
  303. pet->AddHate(this, 1);
  304. pet = ((Entity*)victim)->GetCharmedPet();
  305. if (pet)
  306. pet->AddHate(this, 1);
  307. }
  308. }
  309. // Check Ranged attack proc
  310. CheckProcs(PROC_TYPE_RANGED_ATTACK, victim);
  311. // Check Ranged defence proc
  312. if (victim->IsEntity())
  313. ((Entity*)victim)->CheckProcs(PROC_TYPE_RANGED_DEFENSE, this);
  314. SetRangeLastAttackTime(Timer::GetCurrentTime2());
  315. }
  316. }
  317. //Multi Attack roll
  318. if(!multi_attack){
  319. float multi_attack = info_struct.get_multi_attack();
  320. if(multi_attack > 0){
  321. float chance = multi_attack;
  322. if (multi_attack > 100){
  323. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  324. chance = (multi_attack - (floor((float)(multi_attack / 100) * 100)));
  325. while(automatic_multi > 0){
  326. RangeAttack(victim, 100, weapon, ammo, true);
  327. automatic_multi--;
  328. }
  329. }
  330. if (MakeRandomFloat(0, 100) <= chance)
  331. RangeAttack(victim, 100, weapon, ammo, true);
  332. }
  333. }
  334. //Apply attack speed mods
  335. if(!multi_attack)
  336. SetAttackDelay(false, true);
  337. }
  338. bool Entity::SpellAttack(Spawn* victim, float distance, LuaSpell* luaspell, int8 damage_type, int32 low_damage, int32 high_damage, int8 crit_mod, bool no_calcs){
  339. if(!victim || !luaspell || !luaspell->spell)
  340. return false;
  341. Spell* spell = luaspell->spell;
  342. Skill* skill = nullptr;
  343. int8 hit_result = 0;
  344. bool is_tick = false; // if spell is already active, this is a tick
  345. if (GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell)){
  346. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  347. is_tick = true;
  348. }
  349. else if(spell->GetSpellData()->type == SPELL_BOOK_TYPE_COMBAT_ART)
  350. hit_result = DetermineHit(victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, 0, false);
  351. else
  352. hit_result = DetermineHit(victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, 0, true, luaspell);
  353. if(victim->IsEntity()) {
  354. CheckEncounterState((Entity*)victim);
  355. }
  356. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  357. luaspell->last_spellattack_hit = true;
  358. //If this spell is a tick and has already crit, force the tick to crit
  359. if(is_tick){
  360. if(luaspell->crit)
  361. crit_mod = 1;
  362. else
  363. crit_mod = 2;
  364. }
  365. if(DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, spell->GetName(), crit_mod, is_tick, no_calcs, luaspell) && !luaspell->crit)
  366. luaspell->crit = true;
  367. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  368. CheckProcs(PROC_TYPE_MAGICAL_OFFENSIVE, victim);
  369. if(spell->GetSpellData()->success_message.length() > 0){
  370. Client* client = nullptr;
  371. if(IsPlayer())
  372. client = GetZone()->GetClientBySpawn(this);
  373. if(client){
  374. string success_message = spell->GetSpellData()->success_message;
  375. if(success_message.find("%t") < 0xFFFFFFFF)
  376. success_message.replace(success_message.find("%t"), 2, victim->GetName());
  377. client->Message(CHANNEL_YOU_CAST, success_message.c_str());
  378. //commented out the following line as it was causing a duplicate message EmemJR 5/4/2019
  379. //GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  380. }
  381. }
  382. if(spell->GetSpellData()->effect_message.length() > 0){
  383. string effect_message = spell->GetSpellData()->effect_message;
  384. if(effect_message.find("%t") < 0xFFFFFFFF)
  385. effect_message.replace(effect_message.find("%t"), 2, victim->GetName());
  386. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_message.c_str(), victim, 50);
  387. }
  388. }
  389. else {
  390. if(hit_result == DAMAGE_PACKET_RESULT_RESIST)
  391. luaspell->resisted = true;
  392. if(victim->IsNPC())
  393. ((NPC*)victim)->AddHate(this, 5);
  394. luaspell->last_spellattack_hit = false;
  395. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  396. }
  397. if(EngagedInCombat() == false)
  398. {
  399. LogWrite(MISC__TODO, 1, "TODO", "//It would probably be better to add a column to the spells table for 'starts autoattack'\nfile: %s, func: %s, Line: %i", __FILE__, __FUNCTION__, __LINE__);
  400. int8 class1_ = GetInfoStruct()->get_class1();
  401. if(class1_ == COMMONER ||
  402. class1_ == FIGHTER ||
  403. class1_ == WARRIOR ||
  404. class1_ == GUARDIAN ||
  405. class1_ == BERSERKER ||
  406. class1_ == BRAWLER ||
  407. class1_ == MONK ||
  408. class1_ == BRUISER ||
  409. class1_ == CRUSADER ||
  410. class1_ == SHADOWKNIGHT ||
  411. class1_ == PALADIN ||
  412. class1_ == SCOUT ||
  413. class1_ == ROGUE ||
  414. class1_ == SWASHBUCKLER ||
  415. class1_ == BRIGAND ||
  416. class1_ == BARD ||
  417. class1_ == TROUBADOR ||
  418. class1_ == DIRGE ||
  419. class1_ == PREDATOR ||
  420. class1_ == RANGER ||
  421. class1_ == ASSASSIN ||
  422. class1_ == ANIMALIST ||
  423. class1_ == BEASTLORD ||
  424. class1_ == SHAPER ||
  425. class1_ == CHANNELER) //note: it would probably be better to add a column to the spells table for "starts autoattack".
  426. {
  427. if (victim->IsNPC())
  428. ((NPC*)victim)->AddHate(this, 5);
  429. else {
  430. Client* client = 0;
  431. if(IsPlayer())
  432. client = GetZone()->GetClientBySpawn(this);
  433. if(client) {
  434. client->GetPlayer()->InCombat(true, client->GetPlayer()->GetRangeAttack());
  435. }
  436. InCombat(true);
  437. }
  438. }
  439. }
  440. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  441. Entity* pet = 0;
  442. bool AddHate = false;
  443. if (victim->IsPlayer()) {
  444. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  445. AddHate = true;
  446. }
  447. else
  448. AddHate = true;
  449. if (AddHate) {
  450. pet = ((Entity*)victim)->GetPet();
  451. if (pet)
  452. pet->AddHate(this, 1);
  453. pet = ((Entity*)victim)->GetCharmedPet();
  454. if (pet)
  455. pet->AddHate(this, 1);
  456. }
  457. }
  458. return true;
  459. }
  460. bool Entity::ProcAttack(Spawn* victim, int8 damage_type, int32 low_damage, int32 high_damage, string name, string success_msg, string effect_msg) {
  461. int8 hit_result = DetermineHit(victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, 0, true);
  462. if (hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  463. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, name.c_str());
  464. if (success_msg.length() > 0) {
  465. Client* client = 0;
  466. if(IsPlayer())
  467. client = GetZone()->GetClientBySpawn(this);
  468. if(client) {
  469. if(success_msg.find("%t") < 0xFFFFFFFF)
  470. success_msg.replace(success_msg.find("%t"), 2, victim->GetName());
  471. client->Message(CHANNEL_YOU_CAST, success_msg.c_str());
  472. }
  473. }
  474. if (effect_msg.length() > 0) {
  475. if(effect_msg.find("%t") < 0xFFFFFFFF)
  476. effect_msg.replace(effect_msg.find("%t"), 2, victim->GetName());
  477. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_msg.c_str(), victim, 50);
  478. }
  479. }
  480. else {
  481. if(victim->IsNPC())
  482. ((NPC*)victim)->AddHate(this, 5);
  483. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, name.c_str());
  484. }
  485. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  486. Entity* pet = 0;
  487. bool AddHate = false;
  488. if (victim->IsPlayer()) {
  489. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  490. AddHate = true;
  491. }
  492. else
  493. AddHate = true;
  494. if (AddHate) {
  495. pet = ((Entity*)victim)->GetPet();
  496. if (pet)
  497. pet->AddHate(this, 1);
  498. pet = ((Entity*)victim)->GetCharmedPet();
  499. if (pet)
  500. pet->AddHate(this, 1);
  501. }
  502. }
  503. return true;
  504. }
  505. // this is used exclusively by LUA, heal_type is forced lower case via boost::lower(heal_type); in the LUA Functions used by this
  506. bool Entity::SpellHeal(Spawn* target, float distance, LuaSpell* luaspell, string heal_type, int32 low_heal, int32 high_heal, int8 crit_mod, bool no_calcs, string custom_spell_name){
  507. if(!target || !luaspell || !luaspell->spell || (target->IsPrivateSpawn() && !target->AllowedAccess(this)))
  508. return false;
  509. if (!target->Alive())
  510. return false;
  511. if (target->GetHP() == target->GetTotalHP())
  512. return true;
  513. int32 heal_amt = 0;
  514. bool crit = false;
  515. if(high_heal < low_heal)
  516. high_heal = low_heal;
  517. if(high_heal == low_heal)
  518. heal_amt = high_heal;
  519. else
  520. heal_amt = MakeRandomInt(low_heal, high_heal);
  521. if(!no_calcs){
  522. // if spell is already active, this is a tick
  523. bool is_tick = GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell);
  524. //if is a tick and the spell has crit, force crit, else disable
  525. if(is_tick){
  526. if(luaspell->crit)
  527. crit_mod = 1;
  528. else
  529. crit_mod = 2;
  530. }
  531. if (heal_amt > 0){
  532. if(target->IsEntity())
  533. heal_amt = (int32)CalculateHealAmount((Entity*)target, (sint32)heal_amt, crit_mod, &crit);
  534. else
  535. heal_amt = (int32)CalculateHealAmount(nullptr, (sint32)heal_amt, crit_mod, &crit);
  536. }
  537. }
  538. int16 type = 0;
  539. if (heal_type == "heal") {
  540. if(crit)
  541. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  542. else
  543. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  544. //apply heal
  545. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  546. heal_amt = target->GetTotalHP() - target->GetHP();
  547. target->SetHP(target->GetHP() + heal_amt);
  548. /*
  549. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  550. target->SetHP(target->GetTotalHP());
  551. else
  552. target->SetHP(target->GetHP() + heal_amt);
  553. */
  554. }
  555. else if (heal_type == "power"){
  556. if(crit)
  557. type = HEAL_PACKET_TYPE_CRIT_MANA;
  558. else
  559. type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  560. //give power
  561. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  562. heal_amt = target->GetTotalPower() - target->GetPower();
  563. target->SetPower(GetPower() + heal_amt);
  564. /*
  565. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  566. target->SetPower(target->GetTotalPower());
  567. else
  568. target->SetPower(GetPower() + heal_amt);
  569. */
  570. }
  571. /*else if (heal_type == "Savagery"){
  572. if(crit)
  573. type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
  574. else
  575. type = HEAL_PACKET_TYPE_SAVAGERY;
  576. }
  577. else if (heal_type == "Repair"){
  578. if(crit)
  579. type = HEAL_PACKET_TYPE_CRIT_REPAIR;
  580. else
  581. type = HEAL_PACKET_TYPE_REPAIR;
  582. }*/
  583. else{ //default to heal if type cannot be determined
  584. if(crit)
  585. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  586. else
  587. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  588. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  589. heal_amt = target->GetTotalHP() - target->GetHP();
  590. target->SetHP(target->GetHP() + heal_amt);
  591. /*
  592. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  593. target->SetHP(target->GetTotalHP());
  594. else
  595. target->SetHP(target->GetHP() + heal_amt);
  596. */
  597. }
  598. target->GetZone()->TriggerCharSheetTimer();
  599. if (heal_amt > 0)
  600. GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
  601. CheckProcs(PROC_TYPE_HEALING, target);
  602. CheckProcs(PROC_TYPE_BENEFICIAL, target);
  603. if (target->IsEntity()) {
  604. int32 hate_amt = heal_amt / 2;
  605. set<int32>::iterator itr;
  606. ((Entity*)target)->MHatedBy.lock();
  607. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  608. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  609. if (spawn && spawn->IsEntity() && target != this) {
  610. CheckEncounterState((Entity*)spawn);
  611. ((Entity*)spawn)->AddHate(this, hate_amt);
  612. }
  613. }
  614. ((Entity*)target)->MHatedBy.unlock();
  615. }
  616. return true;
  617. }
  618. int8 Entity::DetermineHit(Spawn* victim, int8 type, int8 damage_type, float ToHitBonus, bool is_caster_spell, LuaSpell* lua_spell){
  619. if(!victim) {
  620. return DAMAGE_PACKET_RESULT_MISS;
  621. }
  622. if(victim->GetInvulnerable()) {
  623. return DAMAGE_PACKET_RESULT_INVULNERABLE;
  624. }
  625. bool behind = false;
  626. // move this above the if statement to assure skill-ups on successful damage return
  627. Skill* skill = GetSkillByWeaponType(type, damage_type, true);
  628. // Monk added with Brawler to 360 degree support per KoS Prima Official eGuide Fighter: Monk, pg 138, denoted '360-Degree Avoidance!'
  629. if(!victim->IsEntity() || (!is_caster_spell && victim->GetAdventureClass() != BRAWLER && victim->GetAdventureClass() != MONK && (behind = BehindTarget(victim)))) {
  630. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  631. }
  632. float bonus = ToHitBonus;
  633. float skillAddedByWeapon = 0.0f;
  634. if(skill)
  635. {
  636. int16 skillID = master_item_list.GetItemStatIDByName(skill->name.data);
  637. if(skillID != 0xFFFFFFFF)
  638. {
  639. MStats.lock();
  640. skillAddedByWeapon = stats[skillID];
  641. if(!is_caster_spell) {
  642. float item_stat_weapon_skill = stats[ITEM_STAT_WEAPON_SKILLS];
  643. skillAddedByWeapon += item_stat_weapon_skill;
  644. }
  645. MStats.unlock();
  646. float max_bonus_skill = GetRuleSkillMaxBonus();
  647. if(skillAddedByWeapon > max_bonus_skill) {
  648. skillAddedByWeapon = max_bonus_skill;
  649. }
  650. }
  651. }
  652. if (skill)
  653. bonus += (skill->current_val+skillAddedByWeapon) / 25;
  654. if(is_caster_spell && lua_spell) {
  655. if(lua_spell->spell->GetSpellData()->resistibility > 0)
  656. bonus -= (1.0f - lua_spell->spell->GetSpellData()->resistibility)*100.0f;
  657. // Here we take into account Subjugation, Disruption and Ordination (debuffs)
  658. if(lua_spell->spell->GetSpellData()->mastery_skill) {
  659. int32 master_skill_reduce = rule_manager.GetGlobalRule(R_Spells, MasterSkillReduceSpellResist)->GetInt32();
  660. if(master_skill_reduce < 1)
  661. master_skill_reduce = 25;
  662. if(IsPlayer() && lua_spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL &&
  663. !((Player*)this)->GetSkills()->HasSkill(lua_spell->spell->GetSpellData()->mastery_skill)) {
  664. ((Player*)this)->AddSkill(lua_spell->spell->GetSpellData()->mastery_skill, 1, ((Player*)this)->GetLevel() * 5, true);
  665. }
  666. Skill* master_skill = GetSkillByID(lua_spell->spell->GetSpellData()->mastery_skill, true);
  667. if(master_skill && (lua_spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL ||
  668. ((master_skill->name.data == "Subjugation" || master_skill->name.data == "Disruption" || master_skill->name.data == "Ordination" || master_skill->name.data == "Aggression")))) {
  669. float item_stat_bonus = 0.0f;
  670. int32 item_stat = master_item_list.GetItemStatIDByName(::ToLower(master_skill->name.data));
  671. if(item_stat != 0xFFFFFFFF) {
  672. item_stat_bonus = GetStat(item_stat);
  673. }
  674. bonus += (master_skill->current_val + item_stat_bonus) / master_skill_reduce;
  675. }
  676. }
  677. }
  678. if (victim->IsEntity())
  679. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  680. Entity* entity_victim = (Entity*)victim;
  681. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  682. sint16 roll_chance = 100;
  683. if(skill)
  684. roll_chance -= skill->current_val / 25;
  685. if(!is_caster_spell){ // melee or range attack
  686. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  687. if(skill)
  688. roll_chance -= skill->current_val / 25;
  689. if(entity_victim->IsImmune(IMMUNITY_TYPE_RIPOSTE))
  690. return DAMAGE_PACKET_RESULT_RIPOSTE;
  691. // Avoidance Instructions: https://forums.daybreakgames.com/eq2/index.php?threads/avoidance-faq.482979/
  692. /*Parry: reads as parry in the avoidance tooltip, increased by items with +parry on them
  693. Caps at 70%. For plate tanks, works in the front quadrant only, for brawlers this is 360 degrees.
  694. A small % of parries will be ripostes, which not only avoid the attack but also damage your attacker
  695. */
  696. skill = entity_victim->GetSkillByName("Parry", true);
  697. if(skill){
  698. float parryChance = entity_victim->GetInfoStruct()->get_parry();
  699. float chanceValue = (100.0f - parryChance);
  700. if(rand()%roll_chance >= chanceValue){ //successful parry
  701. /* You may only riposte things in the front quadrant.
  702. Riposte is based off of parry: a certain % of parries turn into ripostes.
  703. */
  704. if(!behind && victim->InFrontSpawn((Spawn*)this, victim->GetX(), victim->GetZ())) { // if the attacker is not behind the victim, and the victim is facing the attacker (in front of spawn) then we can riposte
  705. float riposteChanceValue = parryChance / 7.0f; // Riposte is based off of parry: a certain % of parries turn into ripostes. Unknown what the value is divided by, 7 to make it 10% even.
  706. if(rand()%100 <= riposteChanceValue) {
  707. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  708. return DAMAGE_PACKET_RESULT_RIPOSTE;
  709. }
  710. }
  711. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  712. return DAMAGE_PACKET_RESULT_PARRY;
  713. }
  714. }
  715. skill = nullptr;
  716. float blockChance = 0.0f;
  717. if(victim->GetAdventureClass() == BRAWLER)
  718. skill = entity_victim->GetSkillByName("Deflection", true);
  719. blockChance = entity_victim->GetInfoStruct()->get_block();
  720. if(blockChance > 0.0f)
  721. {
  722. blockChance += (blockChance*(entity_victim->GetInfoStruct()->get_block_chance()/100.0f));
  723. float chanceValue = (100.0f - blockChance);
  724. /* Non-brawlers may only block things in the front quadrant.
  725. Riposte is based off of parry: a certain % of parries turn into ripostes.
  726. */
  727. float rnd = rand()%roll_chance;
  728. if(rnd >= chanceValue){ //successful block
  729. if((victim->GetAdventureClass() == BRAWLER || victim->GetAdventureClass() == MONK) || (!behind && victim->InFrontSpawn((Spawn*)this, victim->GetX(), victim->GetZ()))) { // if the attacker is not behind the victim, and the victim is facing the attacker (in front of spawn) then we can block
  730. entity_victim->CheckProcs(PROC_TYPE_BLOCK, this);
  731. return (victim->GetAdventureClass() == BRAWLER) ? DAMAGE_PACKET_RESULT_DEFLECT : DAMAGE_PACKET_RESULT_BLOCK;
  732. }
  733. }
  734. }
  735. skill = entity_victim->GetSkillByName("Defense", true);
  736. float dodgeChance = entity_victim->GetInfoStruct()->get_avoidance_base();
  737. if(dodgeChance > 0.0f)
  738. {
  739. float chanceValue = (100.0f - dodgeChance);
  740. float rnd = rand()%roll_chance;
  741. if(rnd >= chanceValue){ //successful dodge
  742. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  743. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  744. }
  745. }
  746. if(rand() % roll_chance >= chance)
  747. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  748. }
  749. else{
  750. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  751. if(skill)
  752. chance -= skill->current_val / 25;
  753. if(rand()%roll_chance >= chance) {
  754. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  755. }
  756. }
  757. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  758. }
  759. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  760. float ret = 1;
  761. switch(damage_type) {
  762. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  763. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  764. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  765. Skill* skill = GetSkillByName("Defense", true);
  766. if(skill)
  767. ret += skill->current_val / 25;
  768. if(IsNPC())
  769. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  770. break;
  771. }
  772. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  773. ret += GetInfoStruct()->get_heat() / 50;
  774. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  775. break;
  776. }
  777. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  778. ret += GetInfoStruct()->get_cold() / 50;
  779. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  780. break;
  781. }
  782. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  783. ret += GetInfoStruct()->get_magic() / 50;
  784. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  785. break;
  786. }
  787. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  788. ret += GetInfoStruct()->get_mental() / 50;
  789. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  790. break;
  791. }
  792. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  793. ret += GetInfoStruct()->get_divine() / 50;
  794. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  795. break;
  796. }
  797. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  798. ret += GetInfoStruct()->get_disease() / 50;
  799. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  800. break;
  801. }
  802. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  803. ret += GetInfoStruct()->get_poison() / 50;
  804. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  805. break;
  806. }
  807. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS: {
  808. ret = 0.0f;
  809. break;
  810. }
  811. }
  812. return ret;
  813. }
  814. Skill* Entity::GetSkillByWeaponType(int8 type, int8 damage_type, bool update) {
  815. if(type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  816. return GetSkillByName("Ranged", update);
  817. }
  818. switch(damage_type) {
  819. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  820. return GetSkillByName("Slashing", update);
  821. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  822. return GetSkillByName("Crushing", update);
  823. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  824. return GetSkillByName("Piercing", update);
  825. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  826. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  827. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  828. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  829. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  830. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  831. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  832. return GetSkillByName("Disruption", update);
  833. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS:
  834. return GetSkillByName("Focus", update);
  835. }
  836. return 0;
  837. }
  838. bool Entity::DamageSpawn(Entity* victim, int8 type, int8 damage_type, int32 low_damage, int32 high_damage, const char* spell_name, int8 crit_mod, bool is_tick, bool no_calcs, bool ignore_attacker, LuaSpell* spell) {
  839. if(!victim || !victim->Alive() || victim->GetHP() == 0)
  840. return false;
  841. int8 hit_result = 0;
  842. int16 blow_type = 0;
  843. sint32 damage = 0;
  844. sint32 damage_before_crit = 0;
  845. bool crit = false;
  846. if(low_damage > high_damage)
  847. high_damage = low_damage;
  848. if(low_damage == high_damage)
  849. damage = low_damage;
  850. else
  851. damage = MakeRandomInt(low_damage, high_damage);
  852. if(!no_calcs) {
  853. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  854. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  855. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  856. //DPS mod is only applied to auto attacks
  857. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  858. if(info_struct.get_dps_multiplier() != 0.0f) {
  859. damage *= (info_struct.get_dps_multiplier());
  860. }
  861. }
  862. //Potency and ability mod is only applied to spells/CAs
  863. else {
  864. damage = CalculateDamageAmount(victim, damage, type, damage_type, spell);
  865. }
  866. if(!crit_mod || crit_mod == 1){
  867. //force crit if crit_mod == 1
  868. if(crit_mod == 1)
  869. crit = true;
  870. // Crit Roll
  871. else {
  872. victim->MStats.lock();
  873. float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  874. victim->MStats.unlock();
  875. if (MakeRandomFloat(0, 100) <= chance)
  876. crit = true;
  877. }
  878. if(crit){
  879. damage_before_crit = damage;
  880. //Apply total crit multiplier with crit bonus
  881. if(info_struct.get_crit_bonus() > 0)
  882. damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
  883. else
  884. damage *= 1.3;
  885. // Change packet type to crit
  886. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  887. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  888. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  889. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  890. }
  891. }
  892. if(type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  893. int16 effective_level_attacker = GetInfoStruct()->get_effective_level() != 0 ? GetInfoStruct()->get_effective_level() : GetLevel();
  894. float mit_percentage = CalculateMitigation(type, damage_type, effective_level_attacker, (IsPlayer() && victim->IsPlayer()));
  895. sint32 damage_to_reduce = (damage * mit_percentage);
  896. if(damage_to_reduce > damage)
  897. damage = 0;
  898. else
  899. damage -= damage_to_reduce;
  900. // if we reduce damage back below crit level then its no longer a crit, but we don't go below base damage
  901. if(type == DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG && damage <= damage_before_crit) {
  902. damage = damage_before_crit;
  903. type = DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE;
  904. }
  905. }
  906. }
  907. bool useWards = false;
  908. if(damage <= 0){
  909. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  910. damage = 0;
  911. if(victim->IsNPC() && victim->GetHP() > 0)
  912. ((Entity*)victim)->AddHate(this, damage);
  913. }
  914. else{
  915. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  916. sint32 return_damage = 0;
  917. if(GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this, 0, false, damage, &return_damage) && return_damage != 0)
  918. {
  919. // anything not 0 (no return) becomes considered 'immune' to the damage
  920. if(return_damage < 0) {
  921. damage = 0;
  922. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  923. }
  924. else if(return_damage != 0) {
  925. // otherwise we use what was given back to us (either less or greater)
  926. damage = return_damage;
  927. }
  928. }
  929. if(victim->IsNPC() && victim->GetHP() > 0)
  930. ((Entity*)victim)->AddHate(this, damage);
  931. if(damage) {
  932. int32 prevDmg = damage;
  933. damage = victim->CheckWards(this, damage, damage_type);
  934. if (damage < (sint64)prevDmg)
  935. useWards = true;
  936. victim->TakeDamage(damage);
  937. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  938. if (IsPlayer()) {
  939. switch (damage_type) {
  940. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  941. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  942. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  943. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  944. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  945. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  946. break;
  947. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  948. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  949. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  950. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  951. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  952. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  953. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  954. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  955. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  956. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  957. break;
  958. }
  959. }
  960. }
  961. }
  962. Entity* attacker = nullptr;
  963. if(!ignore_attacker)
  964. attacker = this;
  965. if (damage > 0) {
  966. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  967. if (IsStealthed() || IsInvis())
  968. CancelAllStealth();
  969. if (victim->IsEntity())
  970. ((Entity*)victim)->CheckInterruptSpell(this);
  971. }
  972. else if (useWards)
  973. {
  974. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  975. }
  976. if (victim->GetHP() <= 0)
  977. KillSpawn(victim, type, damage_type, blow_type);
  978. else {
  979. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  980. if (spell_name)
  981. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  982. else
  983. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  984. }
  985. return crit;
  986. }
  987. float Entity::CalculateMitigation(int8 type, int8 damage_type, int16 effective_level_attacker, bool for_pvp) {
  988. int16 effective_level_victim = GetInfoStruct()->get_effective_level() != 0 ? GetInfoStruct()->get_effective_level() : GetLevel();
  989. if(effective_level_attacker < 1 && effective_level_victim)
  990. effective_level_attacker = effective_level_victim;
  991. else
  992. effective_level_attacker = 1;
  993. int32 effective_mit_cap = effective_level_victim * rule_manager.GetGlobalRule(R_Combat, EffectiveMitigationCapLevel)->GetInt32();
  994. float max_mit = (float)GetInfoStruct()->get_max_mitigation();
  995. if(max_mit == 0.0f)
  996. max_mit = effective_level_victim * 100.0f;
  997. int32 mit_to_use = 0;
  998. switch(type) {
  999. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  1000. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  1001. mit_to_use = GetInfoStruct()->get_cur_mitigation();
  1002. break;
  1003. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  1004. // since critical mitigation is a percentage we will reverse the mit value so we can add skill from specific types of weapons
  1005. mit_to_use = (int32)((float)GetInfoStruct()->get_max_mitigation() * (float)(GetInfoStruct()->get_critical_mitigation()/100.0f));
  1006. break;
  1007. }
  1008. switch(damage_type) {
  1009. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  1010. mit_to_use += GetInfoStruct()->get_mitigation_skill1(); // slash
  1011. break;
  1012. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  1013. mit_to_use += GetInfoStruct()->get_mitigation_skill2(); // pierce
  1014. break;
  1015. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  1016. mit_to_use += GetInfoStruct()->get_mitigation_skill3(); // crush
  1017. break;
  1018. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS:
  1019. return 0.0f; // focus cannot be mitigated, just break out of this now
  1020. break;
  1021. default:
  1022. // do nothing
  1023. break;
  1024. }
  1025. if(for_pvp) {
  1026. mit_to_use += effective_level_victim * rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetInt32();
  1027. }
  1028. if(mit_to_use > effective_mit_cap) {
  1029. mit_to_use = effective_mit_cap;
  1030. }
  1031. float level_diff = ((float)effective_level_victim / (float)effective_level_attacker);
  1032. if(level_diff > rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat()) {
  1033. level_diff = rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat();
  1034. }
  1035. else if(level_diff < rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat()) {
  1036. level_diff = rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMin)->GetFloat();
  1037. }
  1038. float mit_percentage = ((float)mit_to_use / max_mit) * level_diff;
  1039. if(!for_pvp && mit_percentage > rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowed)->GetFloat()) {
  1040. mit_percentage = rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowed)->GetFloat();
  1041. }
  1042. else if(for_pvp && mit_percentage > rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowedPVP)->GetFloat()) {
  1043. mit_percentage = rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowedPVP)->GetFloat();
  1044. }
  1045. return mit_percentage;
  1046. }
  1047. void Entity::AddHate(Entity* attacker, sint32 hate) {
  1048. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  1049. return;
  1050. // If a players pet and protect self is off
  1051. if (IsPet() && ((NPC*)this)->GetOwner() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
  1052. return;
  1053. hate = attacker->CalculateHateAmount(this, hate);
  1054. if (IsNPC() && ((NPC*)this)->Brain()) {
  1055. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  1056. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  1057. int8 loot_state = ((NPC*)this)->GetLockedNoLoot();
  1058. // if encounter size is 0 then add the attacker to the encounter
  1059. if ((loot_state != ENCOUNTER_STATE_AVAILABLE && loot_state != ENCOUNTER_STATE_BROKEN && ((NPC*)this)->Brain()->GetEncounterSize() == 0)) {
  1060. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  1061. AddTargetToEncounter(attacker);
  1062. }
  1063. }
  1064. if (attacker->GetThreatTransfer() && hate > 0) {
  1065. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  1066. if (transfer_target && transfer_target->IsEntity()) {
  1067. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  1068. hate -= transfered_hate;
  1069. this->AddHate((Entity*)transfer_target, transfered_hate);
  1070. }
  1071. }
  1072. // If pet is adding hate add some to the pets owner as well
  1073. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  1074. AddHate(((NPC*)attacker)->GetOwner(), 1);
  1075. // If player and player has a pet and protect master is set add hate to the pet
  1076. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
  1077. // If we have a combat pet add hate to it
  1078. if (((Player*)this)->GetPet())
  1079. AddHate(((Player*)this)->GetPet(), 1);
  1080. if (((Player*)this)->GetCharmedPet())
  1081. AddHate(((Player*)this)->GetCharmedPet(), 1);
  1082. }
  1083. // If this spawn has a spawn group then add the attacker to the hate list of the other
  1084. // group members if not already in their list
  1085. if (HasSpawnGroup()) {
  1086. vector<Spawn*>* group = GetSpawnGroup();
  1087. vector<Spawn*>::iterator itr;
  1088. for (itr = group->begin(); itr != group->end(); itr++) {
  1089. if (!(*itr)->IsNPC())
  1090. continue;
  1091. NPC* spawn = (NPC*)(*itr);
  1092. if (spawn->Brain()->GetHate(attacker) == 0)
  1093. spawn->Brain()->AddHate(attacker, 1);
  1094. }
  1095. safe_delete(group);
  1096. }
  1097. }
  1098. bool Entity::CheckFizzleSpell(LuaSpell* spell) {
  1099. if(!spell || !rule_manager.GetGlobalRule(R_Spells, EnableFizzleSpells)->GetInt8()
  1100. || spell->spell->GetSpellData()->can_fizzle == false)
  1101. return false;
  1102. float fizzleMaxSkill = rule_manager.GetGlobalRule(R_Spells, FizzleMaxSkill)->GetFloat();
  1103. float baseFizzle = rule_manager.GetGlobalRule(R_Spells, DefaultFizzleChance)->GetFloat()/100.0f; // 10%
  1104. float skillObtained = rule_manager.GetGlobalRule(R_Spells, FizzleDefaultSkill)->GetFloat(); // default of .2f so we don't go over the threshold if no skill
  1105. Skill* skill = GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
  1106. if(skill && spell->spell->GetSpellData()->min_class_skill_req > 0)
  1107. {
  1108. float skillObtained = skill->current_val / spell->spell->GetSpellData()->min_class_skill_req;
  1109. if(skillObtained > fizzleMaxSkill) // 120% over the skill value
  1110. {
  1111. skillObtained = fizzleMaxSkill;
  1112. }
  1113. baseFizzle = (fizzleMaxSkill - skillObtained) * baseFizzle;
  1114. float totalSuccessChance = 1.0f - baseFizzle;
  1115. float randResult = MakeRandomFloat(0.0f, 1.0f);
  1116. if(randResult > totalSuccessChance)
  1117. return true;
  1118. }
  1119. return false;
  1120. }
  1121. bool Entity::CheckInterruptSpell(Entity* attacker) {
  1122. if(!IsCasting())
  1123. return false;
  1124. Spell* spell = GetZone()->GetSpell(this);
  1125. if(!spell || spell->GetSpellData()->interruptable == 0)
  1126. return false;
  1127. if(GetInfoStruct()->get_no_interrupt())
  1128. return false;
  1129. //originally base of 30 percent chance to continue casting if attacked
  1130. //modified to 50% and added global rule, 30% was too small at starting levels
  1131. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  1132. float focus_skill_with_bonus = CalculateSkillWithBonus("Focus", ITEM_STAT_FOCUS, true);
  1133. percent += ((1 + focus_skill_with_bonus)/6);
  1134. if(MakeRandomInt(1, 100) > percent) {
  1135. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  1136. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  1137. return true;
  1138. }
  1139. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  1140. return false;
  1141. }
  1142. void Entity::KillSpawn(Spawn* dead, int8 type, int8 damage_type, int16 kill_blow_type) {
  1143. if(!dead)
  1144. return;
  1145. if (IsPlayer()) {
  1146. Client* client = GetZone()->GetClientBySpawn(this);
  1147. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  1148. if (packet) {
  1149. client->QueuePacket(packet->serialize());
  1150. }
  1151. safe_delete(packet);
  1152. ((Player*)this)->InCombat(false);
  1153. }
  1154. if (IsPlayer() && dead->IsEntity())
  1155. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  1156. /* just for sake of not knowing if we are in a read lock, write lock, or no lock
  1157. ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
  1158. */
  1159. if (dead->IsPet() && ((NPC*)dead)->GetOwner())
  1160. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
  1161. else if (dead->IsEntity()) {
  1162. // remove all pets for this entity
  1163. ((Entity*)dead)->DismissAllPets(false, true);
  1164. }
  1165. if (IsCasting())
  1166. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  1167. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  1168. // Kill movement for the dead npc so the corpse doesn't move
  1169. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  1170. dead->ClearRunningLocations();
  1171. dead->CalculateRunningLocation(true);
  1172. GetZone()->KillSpawn(true, dead, this, true, type, damage_type, kill_blow_type);
  1173. }
  1174. void Entity::HandleDeathExperienceDebt(Spawn* killer)
  1175. {
  1176. if(!IsPlayer())
  1177. return;
  1178. float ruleDebt = 0.0f;
  1179. if(killer && killer->IsPlayer())
  1180. ruleDebt = rule_manager.GetGlobalRule(R_Combat, PVPDeathExperienceDebt)->GetFloat()/100.0f;
  1181. else
  1182. ruleDebt = rule_manager.GetGlobalRule(R_Combat, DeathExperienceDebt)->GetFloat()/100.0f;
  1183. if(ruleDebt > 0.0f)
  1184. {
  1185. bool groupDebt = rule_manager.GetGlobalRule(R_Combat, GroupExperienceDebt)->GetBool();
  1186. if(groupDebt && ((Player*)this)->GetGroupMemberInfo())
  1187. {
  1188. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1189. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)this)->GetGroupMemberInfo()->group_id);
  1190. if (group)
  1191. {
  1192. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1193. deque<GroupMemberInfo*>* members = group->GetMembers();
  1194. int32 size = members->size();
  1195. float xpDebtPerMember = ruleDebt/(float)size;
  1196. deque<GroupMemberInfo*>::iterator itr;
  1197. for (itr = members->begin(); itr != members->end(); itr++) {
  1198. GroupMemberInfo* gmi = *itr;
  1199. if (gmi->client && gmi->client->GetPlayer()) {
  1200. gmi->client->GetPlayer()->GetInfoStruct()->set_xp_debt(gmi->client->GetPlayer()->GetInfoStruct()->get_xp_debt()+xpDebtPerMember);
  1201. gmi->client->GetPlayer()->SetCharSheetChanged(true);
  1202. }
  1203. }
  1204. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1205. }
  1206. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1207. }
  1208. else
  1209. {
  1210. ((Player*)this)->GetInfoStruct()->set_xp_debt(((Player*)this)->GetInfoStruct()->get_xp_debt()+ruleDebt);
  1211. ((Player*)this)->SetCharSheetChanged(true);
  1212. }
  1213. }
  1214. }
  1215. void Entity::ProcessCombat() {
  1216. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  1217. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  1218. }
  1219. void NPC::ProcessCombat() {
  1220. MBrain.writelock(__FUNCTION__, __LINE__);
  1221. // Check to see if it is time to call the AI again
  1222. if (m_brain && GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  1223. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  1224. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  1225. m_brain->Think();
  1226. // Set the time for when the brain was last called
  1227. m_brain->SetLastTick(Timer::GetCurrentTime2());
  1228. }
  1229. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  1230. }
  1231. void Player::ProcessCombat() {
  1232. // if not in combat OR casting a spell OR dazed OR feared return out
  1233. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  1234. return;
  1235. //If no target delete combat_target and return out
  1236. Spawn* Target = GetZone()->GetSpawnByID(target);
  1237. if (!Target) {
  1238. combat_target = 0;
  1239. if (target > 0) {
  1240. SetTarget(0);
  1241. }
  1242. return;
  1243. }
  1244. // If is not an entity return out
  1245. if (!Target->IsEntity())
  1246. return;
  1247. // Reset combat target
  1248. combat_target = 0;
  1249. if (Target->HasTarget()) {
  1250. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1251. Spawn* secondary_target = Target->GetTarget();
  1252. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1253. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1254. combat_target = secondary_target;
  1255. }
  1256. }
  1257. }
  1258. }
  1259. // If combat_target wasn't set in the above if set it to the original target
  1260. if (!combat_target)
  1261. combat_target = Target;
  1262. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1263. if(!combat_target)
  1264. return;
  1265. float distance = 0;
  1266. distance = GetDistance(combat_target);
  1267. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1268. if (GetRangeAttack()) {
  1269. // We are doing ranged auto attacks
  1270. //check to see if we can attack the target AND the ranged weapon is ready
  1271. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1272. Item* weapon = 0;
  1273. Item* ammo = 0;
  1274. // Get the currently equiped weapon and ammo for the ranged attack
  1275. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1276. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1277. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1278. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1279. if(weapon && ammo) {
  1280. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1281. RangeAttack(combat_target, distance, weapon, ammo);
  1282. }
  1283. else {
  1284. Client* client = GetZone()->GetClientBySpawn(this);
  1285. if (client) {
  1286. // Need to get messages from live, made these up so the player knows what is wrong in game if weapon or ammo are not valid
  1287. if (!ammo)
  1288. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1289. if (!weapon)
  1290. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1291. }
  1292. }
  1293. }
  1294. }
  1295. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1296. // We are doing melee auto attacks and are within range
  1297. // Check to see if we can attack the target
  1298. if(AttackAllowed((Entity*)combat_target)) {
  1299. // Check to see if the primary melee weapon is ready
  1300. if(PrimaryWeaponReady()) {
  1301. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1302. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1303. MeleeAttack(combat_target, distance, true);
  1304. }
  1305. // Check to see if the secondary weapon is ready
  1306. if(SecondaryWeaponReady()) {
  1307. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1308. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1309. MeleeAttack(combat_target, distance, false);
  1310. }
  1311. }
  1312. }
  1313. }
  1314. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1315. float mod = CalculateAttackSpeedMod();
  1316. bool dual_wield = IsDualWield();
  1317. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1318. //Add 33% longer delay if dual wielding
  1319. if(dual_wield && ! ranged) {
  1320. if(primary)
  1321. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1322. else
  1323. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1324. }
  1325. else {
  1326. if(primary)
  1327. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1328. else if(ranged)
  1329. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1330. else
  1331. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1332. }
  1333. }
  1334. float Entity::CalculateAttackSpeedMod(){
  1335. float aspeed = info_struct.get_attackspeed();
  1336. if(aspeed > 0) {
  1337. if (aspeed <= 100)
  1338. return (aspeed / 100 + 1);
  1339. else if (aspeed <= 200)
  1340. return 2.25;
  1341. }
  1342. return 1;
  1343. }
  1344. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1345. if (type == 0) {
  1346. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1347. return;
  1348. }
  1349. if (!item && !spell) {
  1350. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1351. return;
  1352. }
  1353. MProcList.writelock(__FUNCTION__, __LINE__);
  1354. Proc* proc = new Proc();
  1355. proc->chance = chance;
  1356. proc->item = item;
  1357. proc->spell = spell;
  1358. proc->spellid = spell->spell->GetSpellID();
  1359. m_procList[type].push_back(proc);
  1360. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1361. }
  1362. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1363. if (!item && !spell) {
  1364. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1365. return;
  1366. }
  1367. MProcList.writelock(__FUNCTION__, __LINE__);
  1368. map<int8, vector<Proc*> >::iterator proc_itr;
  1369. vector<Proc*>::iterator itr;
  1370. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1371. itr = proc_itr->second.begin();
  1372. while (itr != proc_itr->second.end()) {
  1373. Proc* proc = *itr;
  1374. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1375. itr = proc_itr->second.erase(itr);
  1376. safe_delete(proc);
  1377. }
  1378. else
  1379. itr++;
  1380. }
  1381. }
  1382. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1383. }
  1384. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1385. lua_State* state = 0;
  1386. bool item_proc = false;
  1387. int8 num_args = 3;
  1388. if (proc->spell) {
  1389. state = proc->spell->state;
  1390. }
  1391. else if (proc->item) {
  1392. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1393. item_proc = true;
  1394. }
  1395. if (!state) {
  1396. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1397. return false;
  1398. }
  1399. lua_getglobal(state, "proc");
  1400. if (item_proc) {
  1401. num_args++;
  1402. lua_interface->SetItemValue(state, proc->item);
  1403. }
  1404. lua_interface->SetSpawnValue(state, this);
  1405. lua_interface->SetSpawnValue(state, target);
  1406. lua_interface->SetInt32Value(state, type);
  1407. /*
  1408. Add spell data from db in case of a spell proc here...
  1409. */
  1410. if (!item_proc) {
  1411. // Append spell data to the param list
  1412. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1413. for(int32 i = 0; i < data->size(); i++) {
  1414. switch(data->at(i)->type) {
  1415. case 0:{
  1416. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1417. break;
  1418. }
  1419. case 1:{
  1420. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1421. break;
  1422. }
  1423. case 2:{
  1424. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1425. break;
  1426. }
  1427. case 3:{
  1428. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1429. break;
  1430. }
  1431. default:{
  1432. LogWrite(SPELL__ERROR, 0, "Spell", "Error: Unknown LUA Type '%i' in Entity::CastProc for Spell '%s'", (int)data->at(i)->type, proc->spell->spell->GetName());
  1433. return false;
  1434. }
  1435. }
  1436. num_args++;
  1437. }
  1438. }
  1439. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1440. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1441. lua_pop(state, 1);
  1442. return false;
  1443. }
  1444. return true;
  1445. }
  1446. void Entity::CheckProcs(int8 type, Spawn* target) {
  1447. if (type == 0) {
  1448. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1449. return;
  1450. }
  1451. float roll = MakeRandomFloat(0, 100);
  1452. vector<Proc*> tmpList;
  1453. MProcList.readlock(__FUNCTION__, __LINE__);
  1454. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1455. Proc* proc = m_procList[type].at(i);
  1456. if (roll <= proc->chance)
  1457. {
  1458. Proc* tmpProc = new Proc();
  1459. tmpProc->chance = proc->chance;
  1460. tmpProc->item = proc->item;
  1461. tmpProc->spell = proc->spell;
  1462. tmpProc->spellid = proc->spellid;
  1463. tmpList.push_back(tmpProc);
  1464. }
  1465. }
  1466. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1467. vector<Proc*>::iterator proc_itr;
  1468. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1469. Proc* tmpProc = *proc_itr;
  1470. CastProc(tmpProc, type, target);
  1471. proc_itr++;
  1472. safe_delete(tmpProc);
  1473. }
  1474. }
  1475. void Entity::ClearProcs() {
  1476. MProcList.writelock(__FUNCTION__, __LINE__);
  1477. map<int8, vector<Proc*> >::iterator proc_itr;
  1478. vector<Proc*>::iterator itr;
  1479. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1480. itr = proc_itr->second.begin();
  1481. while (itr != proc_itr->second.end()) {
  1482. safe_delete(*itr);
  1483. itr = proc_itr->second.erase(itr);
  1484. }
  1485. proc_itr->second.clear();
  1486. }
  1487. m_procList.clear();
  1488. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1489. }
  1490. sint32 Entity::CalculateHateAmount(Spawn* target, sint32 amt) {
  1491. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AMOUNT);
  1492. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1493. amt = CalculateFormulaByStat(amt, ITEM_STAT_ABILITY_MODIFIER);
  1494. return amt;
  1495. }
  1496. sint32 Entity::CalculateHealAmount(Spawn* target, sint32 amt, int8 crit_mod, bool* crit, bool skip_crit_mod) {
  1497. amt = CalculateFormulaByStat(amt, ITEM_STAT_HEAL_AMOUNT);
  1498. amt = CalculateFormulaByStat(amt, ITEM_STAT_SPELL_AND_HEAL);
  1499. //Potency Mod
  1500. amt = CalculateFormulaByStat(amt, ITEM_STAT_POTENCY);
  1501. //Ability Mod
  1502. amt += (int32)min((int32)GetInfoStruct()->get_ability_modifier(), (int32)(amt / 2));
  1503. if(!skip_crit_mod){
  1504. if(!crit_mod || crit_mod == 1){
  1505. if(crit_mod == 1)
  1506. *crit = true;
  1507. else if(!*crit) {
  1508. // Crit Roll
  1509. float chance = (float)max((float)0, (float)GetInfoStruct()->get_crit_chance());
  1510. *crit = (MakeRandomFloat(0, 100) <= chance);
  1511. }
  1512. if(*crit){
  1513. //Apply total crit multiplier with crit bonus
  1514. amt *= ((GetInfoStruct()->get_crit_bonus() / 100) + 1.3);
  1515. }
  1516. }
  1517. }
  1518. return amt;
  1519. }
  1520. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, LuaSpell* spell) {
  1521. return CalculateDamageAmount(target, damage, base_type, damage_type, (spell && spell->spell) ? spell->spell->GetSpellData()->target_type : 0);
  1522. }
  1523. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, int8 target_type) {
  1524. if(damage_type == DAMAGE_PACKET_DAMAGE_TYPE_FOCUS) {
  1525. return damage; // cannot avoid focus damage
  1526. }
  1527. // only spells may add spell damage item stat
  1528. if(damage_type >= DAMAGE_PACKET_DAMAGE_TYPE_HEAT && damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_POISON)
  1529. {
  1530. // https://forums.daybreakgames.com/eq2/index.php?threads/potency-and-ability-mod-what.4316/
  1531. // Spell damage model assuming 100% crit chance:
  1532. // (Spell base damage * Base damage modifier * Int bonus * Skill bonus * Potency + Ability modifier) * Crit bonus * Spell double attack
  1533. /** Spell base damage: Get all master spells
  1534. Base damage modifier: Very very rare. Available from Wizard aa Wisdom line(Brainstorm). Get it, cherish it.
  1535. Int bonus: Past 1200 or so int, you will get a 10% increase in spell damage per 30% increase in int. Look at int tooltip to see the numerical value.
  1536. Skill bonus: Past skill cap, 100 points skill is worth 2% increase in minimum spell damage, which translates to 1% overall increase. Looks at the skill that the spell uses, for wizards mostly disruption. Cap is 6.5*level.
  1537. Potency: A straight damage modifier, the more you can get the better. No practical cap.
  1538. Ability modifier: A straight damage modifier. Limited to 50% of the spell base damage, but for a wizard this usually has little consequence. However note that this is not affected by Potency.
  1539. Crit bonus: A straight damage modifier, the more you can get the better. No practical cap. Wizards got 50% intrinsic Crit Bonus that does not show up in the stat window, just add 50 to stat value for calculations.
  1540. Spell double cast: A straight damage modifier, the more you can get the better. You won't be able to get very much of this.
  1541. Makes the spell cast twice with some limitations.
  1542. **/
  1543. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_DAMAGE);
  1544. }
  1545. if(base_type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  1546. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_AND_COMBAT_ART_DAMAGE);
  1547. // combat abilities only bonus
  1548. if(damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_PIERCE)
  1549. damage = CalculateFormulaByStat(damage, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1550. // Potency mod
  1551. damage = CalculateFormulaByStat(damage, ITEM_STAT_POTENCY);
  1552. int32 modifier = 2;
  1553. if(target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE || target_type == SPELL_TARGET_OTHER_GROUP_AE)
  1554. {
  1555. modifier = 3;
  1556. }
  1557. // Ability mod can only give up to half of damage after potency
  1558. int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / modifier));
  1559. damage += mod;
  1560. return damage;
  1561. }
  1562. sint32 Entity::CalculateFormulaByStat(sint32 value, int16 stat) {
  1563. sint32 outValue = value;
  1564. MStats.lock();
  1565. std::map<int16, float>::iterator itr = stats.find(stat);
  1566. if(itr != stats.end())
  1567. outValue = (sint32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1568. MStats.unlock();
  1569. return outValue;
  1570. }
  1571. int32 Entity::CalculateFormulaByStat(int32 value, int16 stat) {
  1572. int32 outValue = value;
  1573. MStats.lock();
  1574. std::map<int16, float>::iterator itr = stats.find(stat);
  1575. if(itr != stats.end())
  1576. outValue = (int32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1577. MStats.unlock();
  1578. return outValue;
  1579. }
  1580. int32 Entity::CalculateFormulaBonus(int32 value, float percent_bonus) {
  1581. return (int32)((float)value * ((percent_bonus / 100.0f) + 1.0f));
  1582. }