Combat.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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 <math.h>
  30. extern Classes classes;
  31. extern ConfigReader configReader;
  32. extern MasterSkillList master_skill_list;
  33. extern RuleManager rule_manager;
  34. extern LuaInterface* lua_interface;
  35. /* ******************************************************************************
  36. DamageSpawn() - Damage equation
  37. MeleeAttack() - Melee auto attacks
  38. RangeAttack() - Range auto attacks
  39. DetermineHit() - ToHit chance as well as defender parry / dodge / block / riposte
  40. CheckInterruptSpell() - Interrupt equations
  41. No mitigation equations yet
  42. ****************************************************************************** */
  43. /* New Combat code */
  44. bool Entity::PrimaryWeaponReady() {
  45. //Can only be ready if no ranged timer
  46. if (GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) {
  47. if (GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool Entity::SecondaryWeaponReady() {
  53. //Can only be ready if no ranged timer
  54. // if(IsDualWield() && (GetPrimaryLastAttackTime()
  55. if (IsDualWield() && (GetSecondaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay())))) {
  56. if(GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool Entity::RangeWeaponReady() {
  62. //Ranged can only be ready if no other attack timers are active
  63. if(GetRangeLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))) {
  64. if((GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) && (GetSecondaryLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay()))){
  65. if(!IsPlayer() || ((Player*)this)->GetRangeAttack()) {
  66. return true;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. bool Entity::AttackAllowed(Entity* target, float distance, bool range_attack) {
  73. Entity* attacker = this;
  74. Client* client = 0;
  75. if(!target || IsMezzedOrStunned() || IsDazed()) {
  76. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: no target, mezzed, stunned or dazed");
  77. return false;
  78. }
  79. if (IsPlayer())
  80. client = GetZone()->GetClientBySpawn(this);
  81. if (IsPet())
  82. attacker = ((NPC*)this)->GetOwner();
  83. if (target->IsNPC() && ((NPC*)target)->IsPet()){
  84. if (((NPC*)target)->GetOwner())
  85. target = ((NPC*)target)->GetOwner();
  86. }
  87. if (attacker == target) {
  88. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: attacker tried to attack himself or his pet.");
  89. return false;
  90. }
  91. if (IsPlayer() && target->GetAttackable() == 0) {
  92. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is not attackable");
  93. return false;
  94. }
  95. if (IsPlayer() && target->IsBot()) {
  96. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: players are not allowed to attack bots");
  97. return false;
  98. }
  99. if (attacker->IsPlayer() && target->IsPlayer())
  100. {
  101. bool pvp_allowed = rule_manager.GetGlobalRule(R_PVP, AllowPVP)->GetBool();
  102. if (!pvp_allowed) {
  103. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp is not allowed");
  104. return false;
  105. }
  106. else
  107. {
  108. sint32 pvpLevelRange = rule_manager.GetGlobalRule(R_PVP, LevelRange)->GetSInt32();
  109. int32 attackerLevel = attacker->GetLevel();
  110. int32 defenderLevel = target->GetLevel();
  111. if ((sint32)abs((sint32)attackerLevel - (sint32)defenderLevel) > pvpLevelRange)
  112. {
  113. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp range of %i exceeded abs(%i-%i).", pvpLevelRange, attackerLevel, defenderLevel);
  114. return false;
  115. }
  116. }
  117. }
  118. if (target->GetHP() <= 0) {
  119. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is dead");
  120. return false;
  121. }
  122. if(range_attack && distance != 0) {
  123. Item* weapon = 0;
  124. Item* ammo = 0;
  125. if(attacker->IsPlayer()) {
  126. weapon = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  127. ammo = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  128. }
  129. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  130. // Distance is less then min weapon range
  131. if(distance < weapon->ranged_info->range_low) {
  132. if (client)
  133. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too close! Move back!");
  134. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target to close");
  135. return false;
  136. }
  137. // Distance is greater then max weapon range
  138. if (distance > (weapon->ranged_info->range_high + ammo->thrown_info->range)) {
  139. if (client)
  140. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too far away! Move closer!");
  141. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target is to far");
  142. return false;
  143. }
  144. }
  145. }
  146. else if (distance != 0) {
  147. if(distance >= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  148. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: distance is beyond melee range");
  149. return false;
  150. }
  151. }
  152. LogWrite(MISC__TODO, 3, "TODO", "Add more AttackAllowed calculations\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  153. return true;
  154. }
  155. void Entity::MeleeAttack(Spawn* victim, float distance, bool primary, bool multi_attack) {
  156. if(!victim)
  157. return;
  158. int8 damage_type = 0;
  159. int32 min_damage = 0;
  160. int32 max_damage = 0;
  161. if(primary) {
  162. damage_type = GetPrimaryWeaponType();
  163. min_damage = GetPrimaryWeaponMinDamage();
  164. max_damage = GetPrimaryWeaponMaxDamage();
  165. }
  166. else {
  167. damage_type = GetSecondaryWeaponType();
  168. min_damage = GetSecondaryWeaponMinDamage();
  169. max_damage = GetSecondaryWeaponMaxDamage();
  170. }
  171. if (IsStealthed() || IsInvis())
  172. CancelAllStealth();
  173. int8 hit_result = DetermineHit(victim, damage_type, 0, false);
  174. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL){
  175. /*if(GetAdventureClass() == MONK){
  176. max_damage*=3;
  177. crit_chance = GetLevel()/4+5;
  178. }
  179. else if(GetAdventureClass() == BRUISER){
  180. min_damage = GetLevel();
  181. max_damage*=3;
  182. crit_chance = GetLevel()/3+5;
  183. }
  184. if(rand()%100 <=crit_chance){
  185. max_damage*= 2;
  186. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG, damage_type, min_damage, max_damage, 0);
  187. }
  188. else*/
  189. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, damage_type, min_damage, max_damage, 0);
  190. if (!multi_attack) {
  191. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  192. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  193. }
  194. }
  195. else{
  196. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, hit_result, damage_type, 0, 0);
  197. if(hit_result == DAMAGE_PACKET_RESULT_RIPOSTE && victim->IsEntity())
  198. ((Entity*)victim)->MeleeAttack(this, distance, true);
  199. }
  200. //Multi Attack roll
  201. if(!multi_attack){
  202. float multi_attack = info_struct.get_multi_attack();
  203. if(multi_attack > 0){
  204. float chance = multi_attack;
  205. if (multi_attack > 100){
  206. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  207. chance = (multi_attack - (floor((float) ((multi_attack / 100) * 100))));
  208. while(automatic_multi > 0){
  209. MeleeAttack(victim, 100, primary, true);
  210. automatic_multi--;
  211. }
  212. }
  213. if (MakeRandomFloat(0, 100) <= chance)
  214. MeleeAttack(victim, 100, primary, true);
  215. }
  216. }
  217. //Apply attack speed mods
  218. if(!multi_attack)
  219. SetAttackDelay(primary);
  220. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  221. ((NPC*)victim)->AddHate(this, 50);
  222. }
  223. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  224. Entity* pet = 0;
  225. bool AddHate = false;
  226. if (victim->IsPlayer()) {
  227. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  228. AddHate = true;
  229. }
  230. else
  231. AddHate = true;
  232. if (AddHate) {
  233. pet = ((Entity*)victim)->GetPet();
  234. if (pet)
  235. pet->AddHate(this, 1);
  236. pet = ((Entity*)victim)->GetCharmedPet();
  237. if (pet)
  238. pet->AddHate(this, 1);
  239. }
  240. }
  241. }
  242. void Entity::RangeAttack(Spawn* victim, float distance, Item* weapon, Item* ammo, bool multi_attack) {
  243. if(!victim)
  244. return;
  245. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  246. if(weapon->ranged_info->range_low <= distance && (weapon->ranged_info->range_high + ammo->thrown_info->range) >= distance) {
  247. int8 hit_result = DetermineHit(victim, ammo->thrown_info->damage_type, ammo->thrown_info->hit_bonus, false);
  248. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  249. 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);
  250. if (!multi_attack) {
  251. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  252. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  253. }
  254. }
  255. else
  256. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, hit_result, ammo->thrown_info->damage_type, 0, 0);
  257. // If is a player subtract ammo
  258. if (IsPlayer()) {
  259. if (ammo->details.count > 1) {
  260. ammo->details.count -= 1;
  261. ammo->save_needed = true;
  262. }
  263. else
  264. ((Player*)this)->equipment_list.RemoveItem(ammo->details.slot_id, true);
  265. Client* client = GetZone()->GetClientBySpawn(this);
  266. EQ2Packet* outapp = ((Player*)this)->GetEquipmentList()->serialize(client->GetVersion());
  267. if(outapp)
  268. client->QueuePacket(outapp);
  269. }
  270. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  271. ((NPC*)victim)->AddHate(this, 50);
  272. }
  273. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  274. Entity* pet = 0;
  275. bool AddHate = false;
  276. if (victim->IsPlayer()) {
  277. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  278. AddHate = true;
  279. }
  280. else
  281. AddHate = true;
  282. if (AddHate) {
  283. pet = ((Entity*)victim)->GetPet();
  284. if (pet)
  285. pet->AddHate(this, 1);
  286. pet = ((Entity*)victim)->GetCharmedPet();
  287. if (pet)
  288. pet->AddHate(this, 1);
  289. }
  290. }
  291. // Check Ranged attack proc
  292. CheckProcs(PROC_TYPE_RANGED_ATTACK, victim);
  293. // Check Ranged defence proc
  294. if (victim->IsEntity())
  295. ((Entity*)victim)->CheckProcs(PROC_TYPE_RANGED_DEFENSE, this);
  296. SetRangeLastAttackTime(Timer::GetCurrentTime2());
  297. }
  298. }
  299. //Multi Attack roll
  300. if(!multi_attack){
  301. float multi_attack = info_struct.get_multi_attack();
  302. if(multi_attack > 0){
  303. float chance = multi_attack;
  304. if (multi_attack > 100){
  305. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  306. chance = (multi_attack - (floor((float)(multi_attack / 100) * 100)));
  307. while(automatic_multi > 0){
  308. RangeAttack(victim, 100, weapon, ammo, true);
  309. automatic_multi--;
  310. }
  311. }
  312. if (MakeRandomFloat(0, 100) <= chance)
  313. RangeAttack(victim, 100, weapon, ammo, true);
  314. }
  315. }
  316. //Apply attack speed mods
  317. if(!multi_attack)
  318. SetAttackDelay(false, true);
  319. }
  320. bool Entity::SpellAttack(Spawn* victim, float distance, LuaSpell* luaspell, int8 damage_type, int32 low_damage, int32 high_damage, int8 crit_mod, bool no_calcs){
  321. if(!victim || !luaspell || !luaspell->spell)
  322. return false;
  323. Spell* spell = luaspell->spell;
  324. float bonus = 0;
  325. Skill* skill = nullptr;
  326. if(spell->GetSpellData()->resistibility > 0)
  327. bonus -= (1 - spell->GetSpellData()->resistibility)*100;
  328. skill = master_skill_list.GetSkill(spell->GetSpellData()->mastery_skill);
  329. if(skill){
  330. skill = GetSkillByName(skill->name.data.c_str(), true);
  331. if(skill)
  332. bonus += skill->current_val / 25;
  333. }
  334. int8 hit_result = 0;
  335. bool is_tick = false; // if spell is already active, this is a tick
  336. if (GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell)){
  337. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  338. is_tick = true;
  339. }
  340. else if(spell->GetSpellData()->type == SPELL_BOOK_TYPE_COMBAT_ART)
  341. hit_result = DetermineHit(victim, damage_type, 0, false);
  342. else
  343. hit_result = DetermineHit(victim, damage_type, 0, true);
  344. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  345. luaspell->last_spellattack_hit = true;
  346. //If this spell is a tick and has already crit, force the tick to crit
  347. if(is_tick){
  348. if(luaspell->crit)
  349. crit_mod = 1;
  350. else
  351. crit_mod = 2;
  352. }
  353. if(DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, spell->GetName(), crit_mod, is_tick, no_calcs) && !luaspell->crit)
  354. luaspell->crit = true;
  355. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  356. CheckProcs(PROC_TYPE_MAGICAL_OFFENSIVE, victim);
  357. if(spell->GetSpellData()->success_message.length() > 0){
  358. Client* client = nullptr;
  359. if(IsPlayer())
  360. client = GetZone()->GetClientBySpawn(this);
  361. if(client){
  362. string success_message = spell->GetSpellData()->success_message;
  363. if(success_message.find("%t") < 0xFFFFFFFF)
  364. success_message.replace(success_message.find("%t"), 2, victim->GetName());
  365. client->Message(CHANNEL_YOU_CAST, success_message.c_str());
  366. //commented out the following line as it was causing a duplicate message EmemJR 5/4/2019
  367. //GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  368. }
  369. }
  370. if(spell->GetSpellData()->effect_message.length() > 0){
  371. string effect_message = spell->GetSpellData()->effect_message;
  372. if(effect_message.find("%t") < 0xFFFFFFFF)
  373. effect_message.replace(effect_message.find("%t"), 2, victim->GetName());
  374. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_message.c_str(), victim, 50);
  375. }
  376. }
  377. else {
  378. if(hit_result == DAMAGE_PACKET_RESULT_RESIST)
  379. luaspell->resisted = true;
  380. if(victim->IsNPC())
  381. ((NPC*)victim)->AddHate(this, 5);
  382. luaspell->last_spellattack_hit = false;
  383. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  384. }
  385. if(EngagedInCombat() == false)
  386. {
  387. 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__);
  388. int8 class1_ = GetInfoStruct()->get_class1();
  389. if(class1_ == COMMONER ||
  390. class1_ == FIGHTER ||
  391. class1_ == WARRIOR ||
  392. class1_ == GUARDIAN ||
  393. class1_ == BERSERKER ||
  394. class1_ == BRAWLER ||
  395. class1_ == MONK ||
  396. class1_ == BRUISER ||
  397. class1_ == CRUSADER ||
  398. class1_ == SHADOWKNIGHT ||
  399. class1_ == PALADIN ||
  400. class1_ == SCOUT ||
  401. class1_ == ROGUE ||
  402. class1_ == SWASHBUCKLER ||
  403. class1_ == BRIGAND ||
  404. class1_ == BARD ||
  405. class1_ == TROUBADOR ||
  406. class1_ == DIRGE ||
  407. class1_ == PREDATOR ||
  408. class1_ == RANGER ||
  409. class1_ == ASSASSIN ||
  410. class1_ == ANIMALIST ||
  411. class1_ == BEASTLORD ||
  412. class1_ == SHAPER ||
  413. class1_ == CHANNELER) //note: it would probably be better to add a column to the spells table for "starts autoattack".
  414. {
  415. if (victim->IsNPC())
  416. ((NPC*)victim)->AddHate(this, 5);
  417. else
  418. InCombat(true);
  419. }
  420. }
  421. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  422. Entity* pet = 0;
  423. bool AddHate = false;
  424. if (victim->IsPlayer()) {
  425. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  426. AddHate = true;
  427. }
  428. else
  429. AddHate = true;
  430. if (AddHate) {
  431. pet = ((Entity*)victim)->GetPet();
  432. if (pet)
  433. pet->AddHate(this, 1);
  434. pet = ((Entity*)victim)->GetCharmedPet();
  435. if (pet)
  436. pet->AddHate(this, 1);
  437. }
  438. }
  439. return true;
  440. }
  441. bool Entity::ProcAttack(Spawn* victim, int8 damage_type, int32 low_damage, int32 high_damage, string name, string success_msg, string effect_msg) {
  442. int8 hit_result = DetermineHit(victim, damage_type, 0, true);
  443. if (hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  444. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, name.c_str());
  445. if (success_msg.length() > 0) {
  446. Client* client = 0;
  447. if(IsPlayer())
  448. client = GetZone()->GetClientBySpawn(this);
  449. if(client) {
  450. if(success_msg.find("%t") < 0xFFFFFFFF)
  451. success_msg.replace(success_msg.find("%t"), 2, victim->GetName());
  452. client->Message(CHANNEL_YOU_CAST, success_msg.c_str());
  453. }
  454. }
  455. if (effect_msg.length() > 0) {
  456. if(effect_msg.find("%t") < 0xFFFFFFFF)
  457. effect_msg.replace(effect_msg.find("%t"), 2, victim->GetName());
  458. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_msg.c_str(), victim, 50);
  459. }
  460. }
  461. else {
  462. if(victim->IsNPC())
  463. ((NPC*)victim)->AddHate(this, 5);
  464. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, name.c_str());
  465. }
  466. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  467. Entity* pet = 0;
  468. bool AddHate = false;
  469. if (victim->IsPlayer()) {
  470. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  471. AddHate = true;
  472. }
  473. else
  474. AddHate = true;
  475. if (AddHate) {
  476. pet = ((Entity*)victim)->GetPet();
  477. if (pet)
  478. pet->AddHate(this, 1);
  479. pet = ((Entity*)victim)->GetCharmedPet();
  480. if (pet)
  481. pet->AddHate(this, 1);
  482. }
  483. }
  484. return true;
  485. }
  486. // this is used exclusively by LUA, heal_type is forced lower case via boost::lower(heal_type); in the LUA Functions used by this
  487. 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){
  488. if(!target || !luaspell || !luaspell->spell)
  489. return false;
  490. if (!target->Alive())
  491. return false;
  492. if (target->GetHP() == target->GetTotalHP())
  493. return true;
  494. int32 heal_amt = 0;
  495. bool crit = false;
  496. if(high_heal < low_heal)
  497. high_heal = low_heal;
  498. if(high_heal == low_heal)
  499. heal_amt = high_heal;
  500. else
  501. heal_amt = MakeRandomInt(low_heal, high_heal);
  502. if(!no_calcs){
  503. // if spell is already active, this is a tick
  504. bool is_tick = GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell);
  505. //if is a tick and the spell has crit, force crit, else disable
  506. if(is_tick){
  507. if(luaspell->crit)
  508. crit_mod = 1;
  509. else
  510. crit_mod = 2;
  511. }
  512. if (heal_amt > 0){
  513. //int32 base_roll = heal_amt;
  514. //potency mod
  515. MStats.lock();
  516. heal_amt *= (stats[ITEM_STAT_POTENCY] / 100 + 1);
  517. MStats.unlock();
  518. //primary stat mod, insert forula here when done
  519. //heal_amt += base_roll * (GetPrimaryStat()
  520. //Ability Modifier can only be up to half of base roll + potency and primary stat bonus
  521. heal_amt += (int32)min(info_struct.get_ability_modifier(), (float)(heal_amt / 2));
  522. }
  523. if(!crit_mod || crit_mod == 1){
  524. if(crit_mod == 1)
  525. crit = true;
  526. else {
  527. // Crit Roll
  528. float chance = max((float)0, info_struct.get_crit_chance());
  529. crit = (MakeRandomFloat(0, 100) <= chance);
  530. }
  531. if(crit){
  532. //Apply total crit multiplier with crit bonus
  533. heal_amt *= (info_struct.get_crit_bonus() / 100) + 1.3;
  534. if(luaspell->spell)
  535. luaspell->crit = true;
  536. }
  537. }
  538. }
  539. int16 type = 0;
  540. if (heal_type == "heal") {
  541. if(crit)
  542. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  543. else
  544. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  545. //apply heal
  546. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  547. heal_amt = target->GetTotalHP() - target->GetHP();
  548. target->SetHP(target->GetHP() + heal_amt);
  549. /*
  550. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  551. target->SetHP(target->GetTotalHP());
  552. else
  553. target->SetHP(target->GetHP() + heal_amt);
  554. */
  555. }
  556. else if (heal_type == "power"){
  557. if(crit)
  558. type = HEAL_PACKET_TYPE_CRIT_MANA;
  559. else
  560. type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  561. //give power
  562. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  563. heal_amt = target->GetTotalPower() - target->GetPower();
  564. target->SetPower(GetPower() + heal_amt);
  565. /*
  566. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  567. target->SetPower(target->GetTotalPower());
  568. else
  569. target->SetPower(GetPower() + heal_amt);
  570. */
  571. }
  572. /*else if (heal_type == "Savagery"){
  573. if(crit)
  574. type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
  575. else
  576. type = HEAL_PACKET_TYPE_SAVAGERY;
  577. }
  578. else if (heal_type == "Repair"){
  579. if(crit)
  580. type = HEAL_PACKET_TYPE_CRIT_REPAIR;
  581. else
  582. type = HEAL_PACKET_TYPE_REPAIR;
  583. }*/
  584. else{ //default to heal if type cannot be determined
  585. if(crit)
  586. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  587. else
  588. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  589. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  590. heal_amt = target->GetTotalHP() - target->GetHP();
  591. target->SetHP(target->GetHP() + heal_amt);
  592. /*
  593. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  594. target->SetHP(target->GetTotalHP());
  595. else
  596. target->SetHP(target->GetHP() + heal_amt);
  597. */
  598. }
  599. target->GetZone()->TriggerCharSheetTimer();
  600. if (heal_amt > 0)
  601. GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
  602. CheckProcs(PROC_TYPE_HEALING, target);
  603. CheckProcs(PROC_TYPE_BENEFICIAL, target);
  604. if (target->IsEntity()) {
  605. int32 hate_amt = heal_amt / 2;
  606. set<int32>::iterator itr;
  607. ((Entity*)target)->MHatedBy.lock();
  608. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  609. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  610. if (spawn && spawn->IsEntity() && target != this) {
  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 damage_type, float ToHitBonus, bool 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. // Monk added with Brawler to 360 degree support per KoS Prima Official eGuide Fighter: Monk, pg 138, denoted '360-Degree Avoidance!'
  627. if(!victim->IsEntity() || (!spell && victim->GetAdventureClass() != BRAWLER && victim->GetAdventureClass() != MONK && (behind = BehindTarget(victim)))) {
  628. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  629. }
  630. float bonus = ToHitBonus;
  631. Skill* skill = GetSkillByWeaponType(damage_type, true);
  632. float skillAddedByWeapon = 0.0f;
  633. if(skill)
  634. {
  635. int16 skillID = master_item_list.GetItemStatIDByName(skill->name.data);
  636. if(skillID != 0xFFFFFFFF)
  637. {
  638. MStats.lock();
  639. skillAddedByWeapon = stats[skillID];
  640. MStats.unlock();
  641. }
  642. }
  643. if (skill)
  644. bonus += (skill->current_val+skillAddedByWeapon) / 25;
  645. if (victim->IsEntity())
  646. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  647. Entity* entity_victim = (Entity*)victim;
  648. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  649. sint16 roll_chance = 100;
  650. if(skill)
  651. roll_chance -= skill->current_val / 25;
  652. if(!spell){ // melee or range attack
  653. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  654. if(skill)
  655. roll_chance -= skill->current_val / 25;
  656. if(entity_victim->IsImmune(IMMUNITY_TYPE_RIPOSTE))
  657. return DAMAGE_PACKET_RESULT_RIPOSTE;
  658. // Avoidance Instructions: https://forums.daybreakgames.com/eq2/index.php?threads/avoidance-faq.482979/
  659. /*Parry: reads as parry in the avoidance tooltip, increased by items with +parry on them
  660. Caps at 70%. For plate tanks, works in the front quadrant only, for brawlers this is 360 degrees.
  661. A small % of parries will be ripostes, which not only avoid the attack but also damage your attacker
  662. */
  663. skill = entity_victim->GetSkillByName("Parry", true);
  664. if(skill){
  665. float parryChance = entity_victim->GetInfoStruct()->get_parry();
  666. float chanceValue = (100.0f - parryChance);
  667. if(rand()%roll_chance >= chanceValue){ //successful parry
  668. /* You may only riposte things in the front quadrant.
  669. Riposte is based off of parry: a certain % of parries turn into ripostes.
  670. */
  671. 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
  672. 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.
  673. if(rand()%100 <= riposteChanceValue) {
  674. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  675. return DAMAGE_PACKET_RESULT_RIPOSTE;
  676. }
  677. }
  678. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  679. return DAMAGE_PACKET_RESULT_PARRY;
  680. }
  681. }
  682. skill = nullptr;
  683. float blockChance = 0.0f;
  684. if(victim->GetAdventureClass() == BRAWLER)
  685. skill = entity_victim->GetSkillByName("Deflection", true);
  686. blockChance = entity_victim->GetInfoStruct()->get_block();
  687. if(blockChance > 0.0f)
  688. {
  689. blockChance += (blockChance*(entity_victim->GetInfoStruct()->get_block_chance()/100.0f));
  690. float chanceValue = (100.0f - blockChance);
  691. /* Non-brawlers may only block things in the front quadrant.
  692. Riposte is based off of parry: a certain % of parries turn into ripostes.
  693. */
  694. float rnd = rand()%roll_chance;
  695. if(rnd >= chanceValue){ //successful block
  696. 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
  697. entity_victim->CheckProcs(PROC_TYPE_BLOCK, this);
  698. return (victim->GetAdventureClass() == BRAWLER) ? DAMAGE_PACKET_RESULT_DEFLECT : DAMAGE_PACKET_RESULT_BLOCK;
  699. }
  700. }
  701. }
  702. skill = entity_victim->GetSkillByName("Defense", true);
  703. float dodgeChance = entity_victim->GetInfoStruct()->get_avoidance_base();
  704. if(dodgeChance > 0.0f)
  705. {
  706. float chanceValue = (100.0f - dodgeChance);
  707. float rnd = rand()%roll_chance;
  708. if(rnd >= chanceValue){ //successful dodge
  709. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  710. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  711. }
  712. }
  713. if(rand() % roll_chance >= chance)
  714. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  715. }
  716. else{
  717. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  718. if(skill)
  719. chance -= skill->current_val / 25;
  720. if(rand()%roll_chance >= chance) {
  721. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  722. }
  723. }
  724. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  725. }
  726. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  727. float ret = 1;
  728. switch(damage_type) {
  729. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  730. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  731. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  732. Skill* skill = GetSkillByName("Defense", true);
  733. if(skill)
  734. ret += skill->current_val / 25;
  735. if(IsNPC())
  736. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  737. break;
  738. }
  739. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  740. ret += GetInfoStruct()->get_heat() / 50;
  741. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  742. break;
  743. }
  744. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  745. ret += GetInfoStruct()->get_cold() / 50;
  746. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  747. break;
  748. }
  749. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  750. ret += GetInfoStruct()->get_magic() / 50;
  751. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  752. break;
  753. }
  754. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  755. ret += GetInfoStruct()->get_mental() / 50;
  756. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  757. break;
  758. }
  759. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  760. ret += GetInfoStruct()->get_divine() / 50;
  761. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  762. break;
  763. }
  764. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  765. ret += GetInfoStruct()->get_disease() / 50;
  766. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  767. break;
  768. }
  769. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  770. ret += GetInfoStruct()->get_poison() / 50;
  771. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  772. break;
  773. }
  774. }
  775. return ret;
  776. }
  777. Skill* Entity::GetSkillByWeaponType(int8 type, bool update) {
  778. switch(type) {
  779. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  780. return GetSkillByName("Slashing", update);
  781. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  782. return GetSkillByName("Crushing", update);
  783. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  784. return GetSkillByName("Piercing", update);
  785. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  786. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  787. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  788. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  789. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  790. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  791. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  792. return GetSkillByName("Disruption", update);
  793. }
  794. return 0;
  795. }
  796. 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) {
  797. if(!victim || victim->GetHP() == 0)
  798. return false;
  799. int8 hit_result = 0;
  800. int16 blow_type = 0;
  801. sint32 damage = 0;
  802. bool crit = false;
  803. if(low_damage > high_damage)
  804. high_damage = low_damage;
  805. if(low_damage == high_damage)
  806. damage = low_damage;
  807. else
  808. damage = MakeRandomInt(low_damage, high_damage);
  809. if(!no_calcs) {
  810. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  811. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  812. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  813. //DPS mod is only applied to auto attacks
  814. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE ) {
  815. damage *= (info_struct.get_dps_multiplier());
  816. }
  817. //Potency and ability mod is only applied to spells/CAs
  818. else {
  819. // Potency mod
  820. MStats.lock();
  821. damage *= ((stats[ITEM_STAT_POTENCY] / 100) + 1);
  822. MStats.unlock();
  823. // Ability mod can only give up to half of damage after potency
  824. int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / 2));
  825. damage += mod;
  826. }
  827. if(!crit_mod || crit_mod == 1){
  828. //force crit if crit_mod == 1
  829. if(crit_mod == 1)
  830. crit = true;
  831. // Crit Roll
  832. else {
  833. victim->MStats.lock();
  834. float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  835. victim->MStats.unlock();
  836. if (MakeRandomFloat(0, 100) <= chance)
  837. crit = true;
  838. }
  839. if(crit){
  840. //Apply total crit multiplier with crit bonus
  841. if(info_struct.get_crit_bonus() > 0)
  842. damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
  843. else
  844. damage *= 1.3;
  845. // Change packet type to crit
  846. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  847. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  848. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  849. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  850. }
  851. }
  852. // TODO: Mitigation equation from http://www.guildportal.com/Guild.aspx?GuildID=20881&TabID=189653&ForumID=95908&TopicID=9024250
  853. }
  854. LogWrite(MISC__TODO, 3, "TODO", "Take players armor into account\nfile: %s, func: %s, line: %i)", __FILE__, __FUNCTION__, __LINE__);
  855. bool useWards = false;
  856. if(damage <= 0){
  857. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  858. damage = 0;
  859. }
  860. else{
  861. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  862. GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this);
  863. int32 prevDmg = damage;
  864. damage = victim->CheckWards(this, damage, damage_type);
  865. if (damage < (sint64)prevDmg)
  866. useWards = true;
  867. victim->TakeDamage(damage);
  868. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  869. if (IsPlayer()) {
  870. switch (damage_type) {
  871. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  872. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  873. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  874. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  875. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  876. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  877. break;
  878. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  879. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  880. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  881. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  882. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  883. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  884. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  885. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  886. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  887. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  888. break;
  889. }
  890. }
  891. }
  892. if(victim->IsNPC() && victim->GetHP() > 0)
  893. ((Entity*)victim)->AddHate(this, damage);
  894. Entity* attacker = nullptr;
  895. if(!ignore_attacker)
  896. attacker = this;
  897. if (damage > 0) {
  898. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  899. if (IsStealthed() || IsInvis())
  900. CancelAllStealth();
  901. if (victim->IsEntity())
  902. ((Entity*)victim)->CheckInterruptSpell(this);
  903. }
  904. else if (useWards)
  905. {
  906. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  907. }
  908. if (victim->GetHP() <= 0)
  909. KillSpawn(victim, damage_type, blow_type);
  910. else {
  911. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  912. if (spell_name)
  913. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  914. else
  915. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  916. }
  917. return crit;
  918. }
  919. void Entity::AddHate(Entity* attacker, sint32 hate) {
  920. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  921. return;
  922. // If a players pet and protect self is off
  923. if (IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
  924. return;
  925. if (IsNPC()) {
  926. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  927. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  928. // if encounter size is 0 then add the attacker to the encounter
  929. if (((NPC*)this)->Brain()->GetEncounterSize() == 0)
  930. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  931. }
  932. if (attacker->GetThreatTransfer() && hate > 0) {
  933. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  934. if (transfer_target && transfer_target->IsEntity()) {
  935. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  936. hate -= transfered_hate;
  937. this->AddHate((Entity*)transfer_target, transfered_hate);
  938. }
  939. }
  940. // If pet is adding hate add some to the pets owner as well
  941. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  942. AddHate(((NPC*)attacker)->GetOwner(), 1);
  943. // If player and player has a pet and protect master is set add hate to the pet
  944. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
  945. // If we have a combat pet add hate to it
  946. if (((Player*)this)->GetPet())
  947. AddHate(((Player*)this)->GetPet(), 1);
  948. if (((Player*)this)->GetCharmedPet())
  949. AddHate(((Player*)this)->GetCharmedPet(), 1);
  950. }
  951. // If this spawn has a spawn group then add the attacker to the hate list of the other
  952. // group members if not already in their list
  953. if (HasSpawnGroup()) {
  954. vector<Spawn*>* group = GetSpawnGroup();
  955. vector<Spawn*>::iterator itr;
  956. for (itr = group->begin(); itr != group->end(); itr++) {
  957. if (!(*itr)->IsNPC())
  958. continue;
  959. NPC* spawn = (NPC*)(*itr);
  960. if (spawn->Brain()->GetHate(attacker) == 0)
  961. spawn->Brain()->AddHate(attacker, 1);
  962. }
  963. safe_delete(group);
  964. }
  965. }
  966. bool Entity::CheckInterruptSpell(Entity* attacker) {
  967. if(!IsCasting())
  968. return false;
  969. Spell* spell = GetZone()->GetSpell(this);
  970. if(!spell || spell->GetSpellData()->interruptable == 0)
  971. return false;
  972. //originally base of 30 percent chance to continue casting if attacked
  973. //modified to 50% and added global rule, 30% was too small at starting levels
  974. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  975. Skill* skill = GetSkillByName("Focus", true);
  976. float focusSkillPts = 0.0f;
  977. MStats.lock();
  978. focusSkillPts = stats[ITEM_STAT_FOCUS];
  979. MStats.unlock();
  980. if(skill)
  981. percent += ((skill->current_val + 1 + focusSkillPts)/6);
  982. if(MakeRandomInt(1, 100) > percent) {
  983. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  984. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  985. return true;
  986. }
  987. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  988. return false;
  989. }
  990. void Entity::KillSpawn(Spawn* dead, int8 damage_type, int16 kill_blow_type) {
  991. if(!dead)
  992. return;
  993. if (IsPlayer()) {
  994. Client* client = GetZone()->GetClientBySpawn(this);
  995. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  996. if (packet) {
  997. client->QueuePacket(packet->serialize());
  998. }
  999. safe_delete(packet);
  1000. ((Player*)this)->InCombat(false);
  1001. }
  1002. if (IsPlayer() && dead->IsEntity())
  1003. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  1004. //if (dead->IsEntity()) same code called in zone server
  1005. //((Entity*)dead)->InCombat(false);
  1006. /* just for sake of not knowing if we are in a read lock, write lock, or no lock
  1007. ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
  1008. */
  1009. if (dead->IsPet())
  1010. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
  1011. else if (dead->IsEntity()) {
  1012. // remove all pets for this entity
  1013. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetPet(), false, true);
  1014. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCharmedPet(), false, true);
  1015. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetDeityPet(), false, true);
  1016. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCosmeticPet(), false, true);
  1017. }
  1018. // If not in combat and no one in the encounter list add this killer to the list
  1019. if(dead->EngagedInCombat() == false && dead->IsNPC() && ((NPC*)dead)->Brain()->GetEncounterSize() == 0)
  1020. ((NPC*)dead)->Brain()->AddToEncounter(this);
  1021. if (IsCasting())
  1022. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  1023. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  1024. // Kill movement for the dead npc so the corpse doesn't move
  1025. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  1026. dead->ClearRunningLocations();
  1027. dead->CalculateRunningLocation(true);
  1028. GetZone()->KillSpawn(true, dead, this, true, damage_type, kill_blow_type);
  1029. }
  1030. void Entity::ProcessCombat() {
  1031. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  1032. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  1033. }
  1034. void NPC::ProcessCombat() {
  1035. MBrain.writelock(__FUNCTION__, __LINE__);
  1036. // Check to see if it is time to call the AI again
  1037. if (GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  1038. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  1039. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  1040. m_brain->Think();
  1041. // Set the time for when the brain was last called
  1042. m_brain->SetLastTick(Timer::GetCurrentTime2());
  1043. }
  1044. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  1045. }
  1046. void Player::ProcessCombat() {
  1047. // if not in combat OR casting a spell OR dazed OR feared return out
  1048. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  1049. return;
  1050. //If no target delete combat_target and return out
  1051. Spawn* Target = GetZone()->GetSpawnByID(target);
  1052. if (!Target) {
  1053. combat_target = 0;
  1054. if (target > 0) {
  1055. SetTarget(0);
  1056. }
  1057. return;
  1058. }
  1059. // If is not an entity return out
  1060. if (!Target->IsEntity())
  1061. return;
  1062. // Reset combat target
  1063. combat_target = 0;
  1064. if (Target->HasTarget()) {
  1065. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1066. Spawn* secondary_target = Target->GetTarget();
  1067. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1068. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1069. combat_target = secondary_target;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. // If combat_target wasn't set in the above if set it to the original target
  1075. if (!combat_target)
  1076. combat_target = Target;
  1077. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1078. if(!combat_target)
  1079. return;
  1080. float distance = 0;
  1081. distance = GetDistance(combat_target);
  1082. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1083. if (GetRangeAttack()) {
  1084. // We are doing ranged auto attacks
  1085. //check to see if we can attack the target AND the ranged weapon is ready
  1086. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1087. Item* weapon = 0;
  1088. Item* ammo = 0;
  1089. // Get the currently equiped weapon and ammo for the ranged attack
  1090. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1091. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1092. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1093. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1094. if(weapon && ammo) {
  1095. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1096. RangeAttack(combat_target, distance, weapon, ammo);
  1097. }
  1098. else {
  1099. Client* client = GetZone()->GetClientBySpawn(this);
  1100. if (client) {
  1101. // 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
  1102. if (!ammo)
  1103. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1104. if (!weapon)
  1105. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1106. }
  1107. }
  1108. }
  1109. }
  1110. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1111. // We are doing melee auto attacks and are within range
  1112. // Check to see if we can attack the target
  1113. if(AttackAllowed((Entity*)combat_target)) {
  1114. // Check to see if the primary melee weapon is ready
  1115. if(PrimaryWeaponReady()) {
  1116. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1117. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1118. MeleeAttack(combat_target, distance, true);
  1119. }
  1120. // Check to see if the secondary weapon is ready
  1121. if(SecondaryWeaponReady()) {
  1122. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1123. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1124. MeleeAttack(combat_target, distance, false);
  1125. }
  1126. }
  1127. }
  1128. }
  1129. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1130. float mod = CalculateAttackSpeedMod();
  1131. bool dual_wield = IsDualWield();
  1132. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1133. //Add 33% longer delay if dual wielding
  1134. if(dual_wield && ! ranged) {
  1135. if(primary)
  1136. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1137. else
  1138. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1139. }
  1140. else {
  1141. if(primary)
  1142. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1143. else if(ranged)
  1144. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1145. else
  1146. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1147. }
  1148. }
  1149. float Entity::CalculateAttackSpeedMod(){
  1150. float aspeed = info_struct.get_attackspeed();
  1151. if(aspeed > 0) {
  1152. if (aspeed <= 100)
  1153. return (aspeed / 100 + 1);
  1154. else if (aspeed <= 200)
  1155. return 2.25;
  1156. }
  1157. return 1;
  1158. }
  1159. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1160. if (type == 0) {
  1161. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1162. return;
  1163. }
  1164. if (!item && !spell) {
  1165. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1166. return;
  1167. }
  1168. MProcList.writelock(__FUNCTION__, __LINE__);
  1169. Proc* proc = new Proc();
  1170. proc->chance = chance;
  1171. proc->item = item;
  1172. proc->spell = spell;
  1173. proc->spellid = spell->spell->GetSpellID();
  1174. m_procList[type].push_back(proc);
  1175. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1176. }
  1177. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1178. if (!item && !spell) {
  1179. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1180. return;
  1181. }
  1182. MProcList.writelock(__FUNCTION__, __LINE__);
  1183. map<int8, vector<Proc*> >::iterator proc_itr;
  1184. vector<Proc*>::iterator itr;
  1185. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1186. itr = proc_itr->second.begin();
  1187. while (itr != proc_itr->second.end()) {
  1188. Proc* proc = *itr;
  1189. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1190. itr = proc_itr->second.erase(itr);
  1191. safe_delete(proc);
  1192. }
  1193. else
  1194. itr++;
  1195. }
  1196. }
  1197. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1198. }
  1199. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1200. lua_State* state = 0;
  1201. bool item_proc = false;
  1202. int8 num_args = 3;
  1203. if (proc->spell) {
  1204. state = proc->spell->state;
  1205. }
  1206. else if (proc->item) {
  1207. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1208. item_proc = true;
  1209. }
  1210. if (!state) {
  1211. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1212. return false;
  1213. }
  1214. lua_getglobal(state, "proc");
  1215. if (item_proc) {
  1216. num_args++;
  1217. lua_interface->SetItemValue(state, proc->item);
  1218. }
  1219. lua_interface->SetSpawnValue(state, this);
  1220. lua_interface->SetSpawnValue(state, target);
  1221. lua_interface->SetInt32Value(state, type);
  1222. /*
  1223. Add spell data from db in case of a spell proc here...
  1224. */
  1225. if (!item_proc) {
  1226. // Append spell data to the param list
  1227. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1228. for(int32 i = 0; i < data->size(); i++) {
  1229. switch(data->at(i)->type) {
  1230. case 0:{
  1231. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1232. break;
  1233. }
  1234. case 1:{
  1235. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1236. break;
  1237. }
  1238. case 2:{
  1239. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1240. break;
  1241. }
  1242. case 3:{
  1243. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1244. break;
  1245. }
  1246. default:{
  1247. 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());
  1248. return false;
  1249. }
  1250. }
  1251. num_args++;
  1252. }
  1253. }
  1254. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1255. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1256. lua_pop(state, 1);
  1257. return false;
  1258. }
  1259. return true;
  1260. }
  1261. void Entity::CheckProcs(int8 type, Spawn* target) {
  1262. if (type == 0) {
  1263. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1264. return;
  1265. }
  1266. float roll = MakeRandomFloat(0, 100);
  1267. vector<Proc*> tmpList;
  1268. MProcList.readlock(__FUNCTION__, __LINE__);
  1269. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1270. Proc* proc = m_procList[type].at(i);
  1271. if (roll <= proc->chance)
  1272. {
  1273. Proc* tmpProc = new Proc();
  1274. tmpProc->chance = proc->chance;
  1275. tmpProc->item = proc->item;
  1276. tmpProc->spell = proc->spell;
  1277. tmpProc->spellid = proc->spellid;
  1278. tmpList.push_back(tmpProc);
  1279. }
  1280. }
  1281. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1282. vector<Proc*>::iterator proc_itr;
  1283. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1284. Proc* tmpProc = *proc_itr;
  1285. CastProc(tmpProc, type, target);
  1286. proc_itr++;
  1287. safe_delete(tmpProc);
  1288. }
  1289. }
  1290. void Entity::ClearProcs() {
  1291. MProcList.writelock(__FUNCTION__, __LINE__);
  1292. map<int8, vector<Proc*> >::iterator proc_itr;
  1293. vector<Proc*>::iterator itr;
  1294. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1295. itr = proc_itr->second.begin();
  1296. while (itr != proc_itr->second.end()) {
  1297. safe_delete(*itr);
  1298. itr = proc_itr->second.erase(itr);
  1299. }
  1300. proc_itr->second.clear();
  1301. }
  1302. m_procList.clear();
  1303. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1304. }