Combat.cpp 65 KB

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