Combat.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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((!target->IsPlayer() && !target->GetAttackable()) || (!IsPlayer() && !GetAttackable())) {
  82. return false;
  83. }
  84. if (IsPlayer())
  85. client = ((Player*)this)->GetClient();
  86. if (IsPet())
  87. attacker = ((NPC*)this)->GetOwner();
  88. if (target->IsNPC() && ((NPC*)target)->IsPet()){
  89. if (((NPC*)target)->GetOwner())
  90. target = ((NPC*)target)->GetOwner();
  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. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  618. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  619. if (spawn && spawn->IsEntity() && target != this) {
  620. CheckEncounterState((Entity*)spawn);
  621. ((Entity*)spawn)->AddHate(this, hate_amt);
  622. }
  623. }
  624. ((Entity*)target)->MHatedBy.unlock();
  625. }
  626. return true;
  627. }
  628. int8 Entity::DetermineHit(Spawn* victim, int8 type, int8 damage_type, float ToHitBonus, bool is_caster_spell, LuaSpell* lua_spell){
  629. if(lua_spell) {
  630. lua_spell->is_damage_spell = true;
  631. }
  632. if(!victim) {
  633. return DAMAGE_PACKET_RESULT_MISS;
  634. }
  635. if(victim->GetInvulnerable()) {
  636. return DAMAGE_PACKET_RESULT_INVULNERABLE;
  637. }
  638. bool behind = false;
  639. // move this above the if statement to assure skill-ups on successful damage return
  640. Skill* skill = GetSkillByWeaponType(type, damage_type, true);
  641. // Monk added with Brawler to 360 degree support per KoS Prima Official eGuide Fighter: Monk, pg 138, denoted '360-Degree Avoidance!'
  642. if(!victim->IsEntity() || (!is_caster_spell && victim->GetAdventureClass() != BRAWLER && victim->GetAdventureClass() != MONK && (behind = BehindTarget(victim)))) {
  643. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  644. }
  645. float bonus = ToHitBonus;
  646. float skillAddedByWeapon = 0.0f;
  647. if(skill)
  648. {
  649. int16 skillID = master_item_list.GetItemStatIDByName(skill->name.data);
  650. if(skillID != 0xFFFFFFFF)
  651. {
  652. MStats.lock();
  653. skillAddedByWeapon = stats[skillID];
  654. if(!is_caster_spell) {
  655. float item_stat_weapon_skill = stats[ITEM_STAT_WEAPON_SKILLS];
  656. skillAddedByWeapon += item_stat_weapon_skill;
  657. }
  658. MStats.unlock();
  659. float max_bonus_skill = GetRuleSkillMaxBonus();
  660. if(skillAddedByWeapon > max_bonus_skill) {
  661. skillAddedByWeapon = max_bonus_skill;
  662. }
  663. }
  664. }
  665. if (skill)
  666. bonus += (skill->current_val+skillAddedByWeapon) / 25;
  667. if(is_caster_spell && lua_spell) {
  668. if(lua_spell->spell->GetSpellData()->resistibility > 0)
  669. bonus -= (1.0f - lua_spell->spell->GetSpellData()->resistibility)*100.0f;
  670. // Here we take into account Subjugation, Disruption and Ordination (debuffs)
  671. if(lua_spell->spell->GetSpellData()->mastery_skill) {
  672. int32 master_skill_reduce = rule_manager.GetGlobalRule(R_Spells, MasterSkillReduceSpellResist)->GetInt32();
  673. if(master_skill_reduce < 1)
  674. master_skill_reduce = 25;
  675. if(IsPlayer() && lua_spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL &&
  676. !((Player*)this)->GetSkills()->HasSkill(lua_spell->spell->GetSpellData()->mastery_skill)) {
  677. ((Player*)this)->AddSkill(lua_spell->spell->GetSpellData()->mastery_skill, 1, ((Player*)this)->GetLevel() * 5, true);
  678. }
  679. Skill* master_skill = GetSkillByID(lua_spell->spell->GetSpellData()->mastery_skill, true);
  680. if(master_skill && (lua_spell->spell->GetSpellData()->spell_book_type == SPELL_BOOK_TYPE_TRADESKILL ||
  681. ((master_skill->name.data == "Subjugation" || master_skill->name.data == "Disruption" || master_skill->name.data == "Ordination" || master_skill->name.data == "Aggression")))) {
  682. float item_stat_bonus = 0.0f;
  683. int32 item_stat = master_item_list.GetItemStatIDByName(::ToLower(master_skill->name.data));
  684. if(item_stat != 0xFFFFFFFF) {
  685. item_stat_bonus = GetStat(item_stat);
  686. }
  687. bonus += (master_skill->current_val + item_stat_bonus) / master_skill_reduce;
  688. }
  689. }
  690. }
  691. if (victim->IsEntity())
  692. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  693. Entity* entity_victim = (Entity*)victim;
  694. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  695. sint16 roll_chance = 100;
  696. if(skill)
  697. roll_chance -= skill->current_val / 25;
  698. if(!is_caster_spell){ // melee or range attack
  699. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  700. if(skill)
  701. roll_chance -= skill->current_val / 25;
  702. if(entity_victim->IsImmune(IMMUNITY_TYPE_RIPOSTE))
  703. return DAMAGE_PACKET_RESULT_RIPOSTE;
  704. // Avoidance Instructions: https://forums.daybreakgames.com/eq2/index.php?threads/avoidance-faq.482979/
  705. /*Parry: reads as parry in the avoidance tooltip, increased by items with +parry on them
  706. Caps at 70%. For plate tanks, works in the front quadrant only, for brawlers this is 360 degrees.
  707. A small % of parries will be ripostes, which not only avoid the attack but also damage your attacker
  708. */
  709. skill = entity_victim->GetSkillByName("Parry", true);
  710. if(skill){
  711. float parryChance = entity_victim->GetInfoStruct()->get_parry();
  712. float chanceValue = (100.0f - parryChance);
  713. if(rand()%roll_chance >= chanceValue){ //successful parry
  714. /* You may only riposte things in the front quadrant.
  715. Riposte is based off of parry: a certain % of parries turn into ripostes.
  716. */
  717. 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
  718. 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.
  719. if(rand()%100 <= riposteChanceValue) {
  720. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  721. return DAMAGE_PACKET_RESULT_RIPOSTE;
  722. }
  723. }
  724. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  725. return DAMAGE_PACKET_RESULT_PARRY;
  726. }
  727. }
  728. skill = nullptr;
  729. float blockChance = 0.0f;
  730. if(victim->GetAdventureClass() == BRAWLER)
  731. skill = entity_victim->GetSkillByName("Deflection", true);
  732. blockChance = entity_victim->GetInfoStruct()->get_block();
  733. if(blockChance > 0.0f)
  734. {
  735. blockChance += (blockChance*(entity_victim->GetInfoStruct()->get_block_chance()/100.0f));
  736. float chanceValue = (100.0f - blockChance);
  737. /* Non-brawlers may only block things in the front quadrant.
  738. Riposte is based off of parry: a certain % of parries turn into ripostes.
  739. */
  740. float rnd = rand()%roll_chance;
  741. if(rnd >= chanceValue){ //successful block
  742. 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
  743. entity_victim->CheckProcs(PROC_TYPE_BLOCK, this);
  744. return (victim->GetAdventureClass() == BRAWLER) ? DAMAGE_PACKET_RESULT_DEFLECT : DAMAGE_PACKET_RESULT_BLOCK;
  745. }
  746. }
  747. }
  748. skill = entity_victim->GetSkillByName("Defense", true);
  749. float dodgeChance = entity_victim->GetInfoStruct()->get_avoidance_base();
  750. if(dodgeChance > 0.0f)
  751. {
  752. float chanceValue = (100.0f - dodgeChance);
  753. float rnd = rand()%roll_chance;
  754. if(rnd >= chanceValue){ //successful dodge
  755. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  756. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  757. }
  758. }
  759. if(rand() % roll_chance >= chance)
  760. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  761. }
  762. else{
  763. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  764. if(skill)
  765. chance -= skill->current_val / 25;
  766. if(rand()%roll_chance >= chance) {
  767. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  768. }
  769. }
  770. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  771. }
  772. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  773. float ret = 1;
  774. switch(damage_type) {
  775. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  776. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  777. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  778. Skill* skill = GetSkillByName("Defense", true);
  779. if(skill)
  780. ret += skill->current_val / 25;
  781. if(IsNPC())
  782. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  783. break;
  784. }
  785. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  786. ret += GetInfoStruct()->get_heat() / 50;
  787. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  788. break;
  789. }
  790. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  791. ret += GetInfoStruct()->get_cold() / 50;
  792. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  793. break;
  794. }
  795. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  796. ret += GetInfoStruct()->get_magic() / 50;
  797. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  798. break;
  799. }
  800. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  801. ret += GetInfoStruct()->get_mental() / 50;
  802. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  803. break;
  804. }
  805. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  806. ret += GetInfoStruct()->get_divine() / 50;
  807. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  808. break;
  809. }
  810. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  811. ret += GetInfoStruct()->get_disease() / 50;
  812. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  813. break;
  814. }
  815. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  816. ret += GetInfoStruct()->get_poison() / 50;
  817. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  818. break;
  819. }
  820. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS: {
  821. ret = 0.0f;
  822. break;
  823. }
  824. }
  825. return ret;
  826. }
  827. Skill* Entity::GetSkillByWeaponType(int8 type, int8 damage_type, bool update) {
  828. if(type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  829. return GetSkillByName("Ranged", update);
  830. }
  831. switch(damage_type) {
  832. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  833. return GetSkillByName("Slashing", update);
  834. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  835. return GetSkillByName("Crushing", update);
  836. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  837. return GetSkillByName("Piercing", update);
  838. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  839. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  840. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  841. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  842. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  843. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  844. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  845. return GetSkillByName("Disruption", update);
  846. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS:
  847. return GetSkillByName("Focus", update);
  848. }
  849. return 0;
  850. }
  851. 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) {
  852. if(spell) {
  853. spell->is_damage_spell = true;
  854. }
  855. bool has_damaged = false;
  856. if(!victim || !victim->Alive() || victim->GetHP() == 0)
  857. return false;
  858. int8 hit_result = 0;
  859. int16 blow_type = 0;
  860. sint32 damage = 0;
  861. sint32 damage_before_crit = 0;
  862. bool crit = false;
  863. if(low_damage > high_damage)
  864. high_damage = low_damage;
  865. if(low_damage == high_damage)
  866. damage = low_damage;
  867. else
  868. damage = MakeRandomInt(low_damage, high_damage);
  869. if(!no_calcs) {
  870. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  871. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  872. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  873. //DPS mod is only applied to auto attacks
  874. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  875. if(info_struct.get_dps_multiplier() != 0.0f) {
  876. damage *= (info_struct.get_dps_multiplier());
  877. }
  878. }
  879. //Potency and ability mod is only applied to spells/CAs
  880. else {
  881. damage = CalculateDamageAmount(victim, damage, type, damage_type, spell);
  882. }
  883. if(!crit_mod || crit_mod == 1){
  884. //force crit if crit_mod == 1
  885. if(crit_mod == 1)
  886. crit = true;
  887. // Crit Roll
  888. else {
  889. victim->MStats.lock();
  890. float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  891. victim->MStats.unlock();
  892. if (MakeRandomFloat(0, 100) <= chance)
  893. crit = true;
  894. }
  895. if(crit){
  896. damage_before_crit = damage;
  897. //Apply total crit multiplier with crit bonus
  898. if(info_struct.get_crit_bonus() > 0)
  899. damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
  900. else
  901. damage *= 1.3;
  902. // Change packet type to crit
  903. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  904. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  905. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  906. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  907. }
  908. }
  909. if(type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE) {
  910. int16 effective_level_attacker = GetInfoStruct()->get_effective_level() != 0 ? GetInfoStruct()->get_effective_level() : GetLevel();
  911. float mit_percentage = CalculateMitigation(type, damage_type, effective_level_attacker, (IsPlayer() && victim->IsPlayer()));
  912. sint32 damage_to_reduce = (damage * mit_percentage);
  913. if(damage_to_reduce > damage)
  914. damage = 0;
  915. else
  916. damage -= damage_to_reduce;
  917. // if we reduce damage back below crit level then its no longer a crit, but we don't go below base damage
  918. if(type == DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG && damage <= damage_before_crit) {
  919. damage = damage_before_crit;
  920. type = DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE;
  921. }
  922. }
  923. }
  924. bool useWards = false;
  925. if(damage <= 0){
  926. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  927. damage = 0;
  928. if(victim->IsNPC() && victim->GetHP() > 0)
  929. ((Entity*)victim)->AddHate(this, damage);
  930. }
  931. else{
  932. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  933. sint32 return_damage = 0;
  934. if(GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this, 0, false, damage, &return_damage) && return_damage != 0)
  935. {
  936. // anything not 0 (no return) becomes considered 'immune' to the damage
  937. if(return_damage < 0) {
  938. damage = 0;
  939. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  940. }
  941. else if(return_damage != 0) {
  942. // otherwise we use what was given back to us (either less or greater)
  943. damage = return_damage;
  944. }
  945. }
  946. if(victim->IsNPC() && victim->GetHP() > 0)
  947. ((Entity*)victim)->AddHate(this, damage);
  948. if(damage) {
  949. int32 prevDmg = damage;
  950. damage = victim->CheckWards(this, damage, damage_type);
  951. if (damage < (sint64)prevDmg)
  952. useWards = true;
  953. if(damage > 0 && spell) {
  954. has_damaged = true;
  955. spell->has_damaged = true;
  956. }
  957. if(take_power) {
  958. sint32 curPower = victim->GetPower();
  959. if(curPower < damage)
  960. curPower = 0;
  961. else
  962. curPower -= damage;
  963. victim->SetPower(curPower);
  964. }
  965. else {
  966. victim->TakeDamage(damage);
  967. }
  968. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  969. if (IsPlayer()) {
  970. switch (damage_type) {
  971. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  972. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  973. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  974. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  975. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  976. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  977. break;
  978. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  979. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  980. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  981. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  982. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  983. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  984. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  985. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  986. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  987. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  988. break;
  989. }
  990. }
  991. }
  992. }
  993. Entity* attacker = nullptr;
  994. if(!ignore_attacker)
  995. attacker = this;
  996. if (damage > 0) {
  997. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  998. if (IsStealthed() || IsInvis())
  999. CancelAllStealth();
  1000. if (victim->IsEntity())
  1001. ((Entity*)victim)->CheckInterruptSpell(this);
  1002. }
  1003. else if (useWards)
  1004. {
  1005. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  1006. }
  1007. if (victim->GetHP() <= 0)
  1008. KillSpawn(victim, type, damage_type, blow_type);
  1009. else {
  1010. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  1011. if (spell_name)
  1012. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  1013. else
  1014. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  1015. }
  1016. if(spell)
  1017. spell->crit = crit;
  1018. return has_damaged;
  1019. }
  1020. float Entity::CalculateMitigation(int8 type, int8 damage_type, int16 effective_level_attacker, bool for_pvp) {
  1021. int16 effective_level_victim = GetInfoStruct()->get_effective_level() != 0 ? GetInfoStruct()->get_effective_level() : GetLevel();
  1022. if(effective_level_attacker < 1 && effective_level_victim)
  1023. effective_level_attacker = effective_level_victim;
  1024. else
  1025. effective_level_attacker = 1;
  1026. int32 effective_mit_cap = effective_level_victim * rule_manager.GetGlobalRule(R_Combat, EffectiveMitigationCapLevel)->GetInt32();
  1027. float max_mit = (float)GetInfoStruct()->get_max_mitigation();
  1028. if(max_mit == 0.0f)
  1029. max_mit = effective_level_victim * 100.0f;
  1030. int32 mit_to_use = 0;
  1031. switch(type) {
  1032. case DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE:
  1033. case DAMAGE_PACKET_TYPE_RANGE_DAMAGE:
  1034. mit_to_use = GetInfoStruct()->get_cur_mitigation();
  1035. break;
  1036. case DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG:
  1037. // since critical mitigation is a percentage we will reverse the mit value so we can add skill from specific types of weapons
  1038. mit_to_use = (int32)((float)GetInfoStruct()->get_max_mitigation() * (float)(GetInfoStruct()->get_critical_mitigation()/100.0f));
  1039. break;
  1040. }
  1041. switch(damage_type) {
  1042. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  1043. mit_to_use += GetInfoStruct()->get_mitigation_skill1(); // slash
  1044. break;
  1045. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  1046. mit_to_use += GetInfoStruct()->get_mitigation_skill2(); // pierce
  1047. break;
  1048. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  1049. mit_to_use += GetInfoStruct()->get_mitigation_skill3(); // crush
  1050. break;
  1051. case DAMAGE_PACKET_DAMAGE_TYPE_FOCUS:
  1052. return 0.0f; // focus cannot be mitigated, just break out of this now
  1053. break;
  1054. default:
  1055. // do nothing
  1056. break;
  1057. }
  1058. if(for_pvp) {
  1059. mit_to_use += effective_level_victim * rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetInt32();
  1060. }
  1061. if(mit_to_use > effective_mit_cap) {
  1062. mit_to_use = effective_mit_cap;
  1063. }
  1064. float level_diff = ((float)effective_level_victim / (float)effective_level_attacker);
  1065. if(level_diff > rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat()) {
  1066. level_diff = rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat();
  1067. }
  1068. else if(level_diff < rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMax)->GetFloat()) {
  1069. level_diff = rule_manager.GetGlobalRule(R_Combat, MitigationLevelEffectivenessMin)->GetFloat();
  1070. }
  1071. float mit_percentage = ((float)mit_to_use / max_mit) * level_diff;
  1072. if(!for_pvp && mit_percentage > rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowed)->GetFloat()) {
  1073. mit_percentage = rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowed)->GetFloat();
  1074. }
  1075. else if(for_pvp && mit_percentage > rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowedPVP)->GetFloat()) {
  1076. mit_percentage = rule_manager.GetGlobalRule(R_Combat, MaxMitigationAllowedPVP)->GetFloat();
  1077. }
  1078. return mit_percentage;
  1079. }
  1080. void Entity::AddHate(Entity* attacker, sint32 hate) {
  1081. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  1082. return;
  1083. // If a players pet and protect self is off
  1084. if (IsPet() && ((NPC*)this)->GetOwner() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
  1085. return;
  1086. hate = attacker->CalculateHateAmount(this, hate);
  1087. if (IsNPC() && ((NPC*)this)->Brain()) {
  1088. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  1089. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  1090. int8 loot_state = ((NPC*)this)->GetLockedNoLoot();
  1091. // if encounter size is 0 then add the attacker to the encounter
  1092. if ((loot_state != ENCOUNTER_STATE_AVAILABLE && loot_state != ENCOUNTER_STATE_BROKEN && ((NPC*)this)->Brain()->GetEncounterSize() == 0)) {
  1093. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  1094. AddTargetToEncounter(attacker);
  1095. }
  1096. }
  1097. if (attacker->GetThreatTransfer() && hate > 0) {
  1098. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  1099. if (transfer_target && transfer_target->IsEntity()) {
  1100. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  1101. hate -= transfered_hate;
  1102. this->AddHate((Entity*)transfer_target, transfered_hate);
  1103. }
  1104. }
  1105. // If pet is adding hate add some to the pets owner as well
  1106. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  1107. AddHate(((NPC*)attacker)->GetOwner(), 1);
  1108. // If player and player has a pet and protect master is set add hate to the pet
  1109. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
  1110. // If we have a combat pet add hate to it
  1111. if (((Player*)this)->GetPet())
  1112. AddHate(((Player*)this)->GetPet(), 1);
  1113. if (((Player*)this)->GetCharmedPet())
  1114. AddHate(((Player*)this)->GetCharmedPet(), 1);
  1115. }
  1116. // If this spawn has a spawn group then add the attacker to the hate list of the other
  1117. // group members if not already in their list
  1118. if (HasSpawnGroup()) {
  1119. vector<Spawn*>* group = GetSpawnGroup();
  1120. vector<Spawn*>::iterator itr;
  1121. for (itr = group->begin(); itr != group->end(); itr++) {
  1122. if (!(*itr)->IsNPC())
  1123. continue;
  1124. NPC* spawn = (NPC*)(*itr);
  1125. if (spawn->Brain()->GetHate(attacker) == 0)
  1126. spawn->Brain()->AddHate(attacker, 1);
  1127. }
  1128. safe_delete(group);
  1129. }
  1130. }
  1131. bool Entity::CheckFizzleSpell(LuaSpell* spell) {
  1132. if(!spell || !rule_manager.GetGlobalRule(R_Spells, EnableFizzleSpells)->GetInt8()
  1133. || spell->spell->GetSpellData()->can_fizzle == false)
  1134. return false;
  1135. float fizzleMaxSkill = rule_manager.GetGlobalRule(R_Spells, FizzleMaxSkill)->GetFloat();
  1136. float baseFizzle = rule_manager.GetGlobalRule(R_Spells, DefaultFizzleChance)->GetFloat()/100.0f; // 10%
  1137. float skillObtained = rule_manager.GetGlobalRule(R_Spells, FizzleDefaultSkill)->GetFloat(); // default of .2f so we don't go over the threshold if no skill
  1138. Skill* skill = GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
  1139. if(skill && spell->spell->GetSpellData()->min_class_skill_req > 0)
  1140. {
  1141. float skillObtained = skill->current_val / spell->spell->GetSpellData()->min_class_skill_req;
  1142. if(skillObtained > fizzleMaxSkill) // 120% over the skill value
  1143. {
  1144. skillObtained = fizzleMaxSkill;
  1145. }
  1146. baseFizzle = (fizzleMaxSkill - skillObtained) * baseFizzle;
  1147. float totalSuccessChance = 1.0f - baseFizzle;
  1148. float randResult = MakeRandomFloat(0.0f, 1.0f);
  1149. if(randResult > totalSuccessChance)
  1150. return true;
  1151. }
  1152. return false;
  1153. }
  1154. bool Entity::CheckInterruptSpell(Entity* attacker) {
  1155. if(!IsCasting())
  1156. return false;
  1157. Spell* spell = GetZone()->GetSpell(this);
  1158. if(!spell || spell->GetSpellData()->interruptable == 0)
  1159. return false;
  1160. if(GetInfoStruct()->get_no_interrupt())
  1161. return false;
  1162. //originally base of 30 percent chance to continue casting if attacked
  1163. //modified to 50% and added global rule, 30% was too small at starting levels
  1164. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  1165. float focus_skill_with_bonus = CalculateSkillWithBonus("Focus", ITEM_STAT_FOCUS, true);
  1166. percent += ((1 + focus_skill_with_bonus)/6);
  1167. if(MakeRandomInt(1, 100) > percent) {
  1168. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  1169. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  1170. return true;
  1171. }
  1172. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  1173. return false;
  1174. }
  1175. void Entity::KillSpawn(Spawn* dead, int8 type, int8 damage_type, int16 kill_blow_type) {
  1176. if(!dead)
  1177. return;
  1178. if (IsPlayer()) {
  1179. Client* client = ((Player*)this)->GetClient();
  1180. if(client) {
  1181. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  1182. if (packet) {
  1183. client->QueuePacket(packet->serialize());
  1184. }
  1185. safe_delete(packet);
  1186. }
  1187. ((Player*)this)->InCombat(false);
  1188. }
  1189. if (IsPlayer() && dead->IsEntity())
  1190. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  1191. /* just for sake of not knowing if we are in a read lock, write lock, or no lock
  1192. ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
  1193. */
  1194. if (dead->IsPet() && ((NPC*)dead)->GetOwner())
  1195. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
  1196. else if (dead->IsEntity()) {
  1197. // remove all pets for this entity
  1198. ((Entity*)dead)->DismissAllPets(false, true);
  1199. }
  1200. if (IsCasting())
  1201. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  1202. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  1203. // Kill movement for the dead npc so the corpse doesn't move
  1204. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  1205. dead->ClearRunningLocations();
  1206. dead->CalculateRunningLocation(true);
  1207. GetZone()->KillSpawn(true, dead, this, true, type, damage_type, kill_blow_type);
  1208. }
  1209. void Entity::HandleDeathExperienceDebt(Spawn* killer)
  1210. {
  1211. if(!IsPlayer())
  1212. return;
  1213. float ruleDebt = 0.0f;
  1214. if(killer && killer->IsPlayer())
  1215. ruleDebt = rule_manager.GetGlobalRule(R_Combat, PVPDeathExperienceDebt)->GetFloat()/100.0f;
  1216. else
  1217. ruleDebt = rule_manager.GetGlobalRule(R_Combat, DeathExperienceDebt)->GetFloat()/100.0f;
  1218. if(ruleDebt > 0.0f)
  1219. {
  1220. bool groupDebt = rule_manager.GetGlobalRule(R_Combat, GroupExperienceDebt)->GetBool();
  1221. if(groupDebt && ((Player*)this)->GetGroupMemberInfo())
  1222. {
  1223. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1224. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)this)->GetGroupMemberInfo()->group_id);
  1225. if (group)
  1226. {
  1227. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1228. deque<GroupMemberInfo*>* members = group->GetMembers();
  1229. int32 size = members->size();
  1230. float xpDebtPerMember = ruleDebt/(float)size;
  1231. deque<GroupMemberInfo*>::iterator itr;
  1232. for (itr = members->begin(); itr != members->end(); itr++) {
  1233. GroupMemberInfo* gmi = *itr;
  1234. if (gmi->client && gmi->client->GetPlayer()) {
  1235. gmi->client->GetPlayer()->GetInfoStruct()->set_xp_debt(gmi->client->GetPlayer()->GetInfoStruct()->get_xp_debt()+xpDebtPerMember);
  1236. gmi->client->GetPlayer()->SetCharSheetChanged(true);
  1237. }
  1238. }
  1239. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1240. }
  1241. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1242. }
  1243. else
  1244. {
  1245. ((Player*)this)->GetInfoStruct()->set_xp_debt(((Player*)this)->GetInfoStruct()->get_xp_debt()+ruleDebt);
  1246. ((Player*)this)->SetCharSheetChanged(true);
  1247. }
  1248. }
  1249. }
  1250. void Entity::ProcessCombat() {
  1251. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  1252. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  1253. }
  1254. void NPC::ProcessCombat() {
  1255. MBrain.writelock(__FUNCTION__, __LINE__);
  1256. // Check to see if it is time to call the AI again
  1257. if (m_brain && GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  1258. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  1259. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  1260. m_brain->Think();
  1261. // Set the time for when the brain was last called
  1262. m_brain->SetLastTick(Timer::GetCurrentTime2());
  1263. }
  1264. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  1265. }
  1266. void Player::ProcessCombat() {
  1267. // if not in combat OR casting a spell OR dazed OR feared return out
  1268. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  1269. return;
  1270. //If no target delete combat_target and return out
  1271. Spawn* Target = GetZone()->GetSpawnByID(target);
  1272. if (!Target) {
  1273. combat_target = 0;
  1274. if (target > 0) {
  1275. SetTarget(0);
  1276. }
  1277. return;
  1278. }
  1279. // If is not an entity return out
  1280. if (!Target->IsEntity())
  1281. return;
  1282. // Reset combat target
  1283. combat_target = 0;
  1284. if (Target->HasTarget()) {
  1285. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1286. Spawn* secondary_target = Target->GetTarget();
  1287. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1288. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1289. combat_target = secondary_target;
  1290. }
  1291. }
  1292. }
  1293. }
  1294. // If combat_target wasn't set in the above if set it to the original target
  1295. if (!combat_target)
  1296. combat_target = Target;
  1297. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1298. if(!combat_target)
  1299. return;
  1300. float distance = 0;
  1301. distance = GetDistance(combat_target);
  1302. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1303. if (GetRangeAttack()) {
  1304. // We are doing ranged auto attacks
  1305. //check to see if we can attack the target AND the ranged weapon is ready
  1306. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1307. Item* weapon = 0;
  1308. Item* ammo = 0;
  1309. // Get the currently equiped weapon and ammo for the ranged attack
  1310. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1311. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1312. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1313. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1314. if(weapon && ammo) {
  1315. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1316. RangeAttack(combat_target, distance, weapon, ammo);
  1317. }
  1318. else {
  1319. Client* client = ((Player*)this)->GetClient();
  1320. if (client) {
  1321. // 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
  1322. if (!ammo)
  1323. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1324. if (!weapon)
  1325. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1326. }
  1327. }
  1328. }
  1329. }
  1330. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1331. // We are doing melee auto attacks and are within range
  1332. // Check to see if we can attack the target
  1333. if(AttackAllowed((Entity*)combat_target)) {
  1334. // Check to see if the primary melee weapon is ready
  1335. if(PrimaryWeaponReady()) {
  1336. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1337. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1338. MeleeAttack(combat_target, distance, true);
  1339. }
  1340. // Check to see if the secondary weapon is ready
  1341. if(SecondaryWeaponReady()) {
  1342. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1343. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1344. MeleeAttack(combat_target, distance, false);
  1345. }
  1346. }
  1347. }
  1348. }
  1349. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1350. float mod = CalculateAttackSpeedMod();
  1351. bool dual_wield = IsDualWield();
  1352. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1353. //Add 33% longer delay if dual wielding
  1354. if(dual_wield && ! ranged) {
  1355. if(primary)
  1356. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1357. else
  1358. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1359. }
  1360. else {
  1361. if(primary)
  1362. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1363. else if(ranged)
  1364. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1365. else
  1366. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1367. }
  1368. }
  1369. float Entity::CalculateAttackSpeedMod(){
  1370. float aspeed = info_struct.get_attackspeed();
  1371. if(aspeed > 0) {
  1372. if (aspeed <= 100)
  1373. return (aspeed / 100 + 1);
  1374. else if (aspeed <= 200)
  1375. return 2.25;
  1376. }
  1377. return 1;
  1378. }
  1379. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1380. if (type == 0) {
  1381. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1382. return;
  1383. }
  1384. if (!item && !spell) {
  1385. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1386. return;
  1387. }
  1388. MProcList.writelock(__FUNCTION__, __LINE__);
  1389. Proc* proc = new Proc();
  1390. proc->chance = chance;
  1391. proc->item = item;
  1392. proc->spell = spell;
  1393. proc->spellid = spell->spell->GetSpellID();
  1394. m_procList[type].push_back(proc);
  1395. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1396. }
  1397. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1398. if (!item && !spell) {
  1399. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1400. return;
  1401. }
  1402. MProcList.writelock(__FUNCTION__, __LINE__);
  1403. map<int8, vector<Proc*> >::iterator proc_itr;
  1404. vector<Proc*>::iterator itr;
  1405. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1406. itr = proc_itr->second.begin();
  1407. while (itr != proc_itr->second.end()) {
  1408. Proc* proc = *itr;
  1409. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1410. itr = proc_itr->second.erase(itr);
  1411. safe_delete(proc);
  1412. }
  1413. else
  1414. itr++;
  1415. }
  1416. }
  1417. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1418. }
  1419. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1420. lua_State* state = 0;
  1421. bool item_proc = false;
  1422. int8 num_args = 3;
  1423. if (proc->spell) {
  1424. state = proc->spell->state;
  1425. }
  1426. else if (proc->item) {
  1427. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1428. item_proc = true;
  1429. }
  1430. if (!state) {
  1431. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1432. return false;
  1433. }
  1434. lua_getglobal(state, "proc");
  1435. if (item_proc) {
  1436. num_args++;
  1437. lua_interface->SetItemValue(state, proc->item);
  1438. }
  1439. lua_interface->SetSpawnValue(state, this);
  1440. lua_interface->SetSpawnValue(state, target);
  1441. lua_interface->SetInt32Value(state, type);
  1442. /*
  1443. Add spell data from db in case of a spell proc here...
  1444. */
  1445. if (!item_proc) {
  1446. // Append spell data to the param list
  1447. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1448. for(int32 i = 0; i < data->size(); i++) {
  1449. switch(data->at(i)->type) {
  1450. case 0:{
  1451. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1452. break;
  1453. }
  1454. case 1:{
  1455. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1456. break;
  1457. }
  1458. case 2:{
  1459. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1460. break;
  1461. }
  1462. case 3:{
  1463. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1464. break;
  1465. }
  1466. default:{
  1467. 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());
  1468. return false;
  1469. }
  1470. }
  1471. num_args++;
  1472. }
  1473. }
  1474. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1475. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1476. lua_pop(state, 1);
  1477. return false;
  1478. }
  1479. return true;
  1480. }
  1481. void Entity::CheckProcs(int8 type, Spawn* target) {
  1482. if (type == 0) {
  1483. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1484. return;
  1485. }
  1486. float roll = MakeRandomFloat(0, 100);
  1487. vector<Proc*> tmpList;
  1488. MProcList.readlock(__FUNCTION__, __LINE__);
  1489. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1490. Proc* proc = m_procList[type].at(i);
  1491. if (roll <= proc->chance)
  1492. {
  1493. Proc* tmpProc = new Proc();
  1494. tmpProc->chance = proc->chance;
  1495. tmpProc->item = proc->item;
  1496. tmpProc->spell = proc->spell;
  1497. tmpProc->spellid = proc->spellid;
  1498. tmpList.push_back(tmpProc);
  1499. }
  1500. }
  1501. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1502. vector<Proc*>::iterator proc_itr;
  1503. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1504. Proc* tmpProc = *proc_itr;
  1505. CastProc(tmpProc, type, target);
  1506. proc_itr++;
  1507. safe_delete(tmpProc);
  1508. }
  1509. }
  1510. void Entity::ClearProcs() {
  1511. MProcList.writelock(__FUNCTION__, __LINE__);
  1512. map<int8, vector<Proc*> >::iterator proc_itr;
  1513. vector<Proc*>::iterator itr;
  1514. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1515. itr = proc_itr->second.begin();
  1516. while (itr != proc_itr->second.end()) {
  1517. safe_delete(*itr);
  1518. itr = proc_itr->second.erase(itr);
  1519. }
  1520. proc_itr->second.clear();
  1521. }
  1522. m_procList.clear();
  1523. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1524. }
  1525. sint32 Entity::CalculateHateAmount(Spawn* target, sint32 amt) {
  1526. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AMOUNT);
  1527. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1528. amt = CalculateFormulaByStat(amt, ITEM_STAT_ABILITY_MODIFIER);
  1529. return amt;
  1530. }
  1531. sint32 Entity::CalculateHealAmount(Spawn* target, sint32 amt, int8 crit_mod, bool* crit, bool skip_crit_mod) {
  1532. amt = CalculateFormulaByStat(amt, ITEM_STAT_HEAL_AMOUNT);
  1533. amt = CalculateFormulaByStat(amt, ITEM_STAT_SPELL_AND_HEAL);
  1534. //Potency Mod
  1535. amt = CalculateFormulaByStat(amt, ITEM_STAT_POTENCY);
  1536. //Ability Mod
  1537. amt += (int32)min((int32)GetInfoStruct()->get_ability_modifier(), (int32)(amt / 2));
  1538. if(!skip_crit_mod){
  1539. if(!crit_mod || crit_mod == 1){
  1540. if(crit_mod == 1)
  1541. *crit = true;
  1542. else if(!*crit) {
  1543. // Crit Roll
  1544. float chance = (float)max((float)0, (float)GetInfoStruct()->get_crit_chance());
  1545. *crit = (MakeRandomFloat(0, 100) <= chance);
  1546. }
  1547. if(*crit){
  1548. //Apply total crit multiplier with crit bonus
  1549. amt *= ((GetInfoStruct()->get_crit_bonus() / 100) + 1.3);
  1550. }
  1551. }
  1552. }
  1553. return amt;
  1554. }
  1555. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, LuaSpell* spell) {
  1556. return CalculateDamageAmount(target, damage, base_type, damage_type, (spell && spell->spell) ? spell->spell->GetSpellData()->target_type : 0);
  1557. }
  1558. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, int8 target_type) {
  1559. if(damage_type == DAMAGE_PACKET_DAMAGE_TYPE_FOCUS) {
  1560. return damage; // cannot avoid focus damage
  1561. }
  1562. // only spells may add spell damage item stat
  1563. if(damage_type >= DAMAGE_PACKET_DAMAGE_TYPE_HEAT && damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_POISON)
  1564. {
  1565. // https://forums.daybreakgames.com/eq2/index.php?threads/potency-and-ability-mod-what.4316/
  1566. // Spell damage model assuming 100% crit chance:
  1567. // (Spell base damage * Base damage modifier * Int bonus * Skill bonus * Potency + Ability modifier) * Crit bonus * Spell double attack
  1568. /** Spell base damage: Get all master spells
  1569. Base damage modifier: Very very rare. Available from Wizard aa Wisdom line(Brainstorm). Get it, cherish it.
  1570. 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.
  1571. 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.
  1572. Potency: A straight damage modifier, the more you can get the better. No practical cap.
  1573. 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.
  1574. 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.
  1575. 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.
  1576. Makes the spell cast twice with some limitations.
  1577. **/
  1578. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_DAMAGE);
  1579. }
  1580. if(base_type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  1581. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_AND_COMBAT_ART_DAMAGE);
  1582. // combat abilities only bonus
  1583. if(damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_PIERCE)
  1584. damage = CalculateFormulaByStat(damage, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1585. // Potency mod
  1586. damage = CalculateFormulaByStat(damage, ITEM_STAT_POTENCY);
  1587. int32 modifier = 2;
  1588. if(target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE || target_type == SPELL_TARGET_OTHER_GROUP_AE)
  1589. {
  1590. modifier = 3;
  1591. }
  1592. // Ability mod can only give up to half of damage after potency
  1593. int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / modifier));
  1594. damage += mod;
  1595. return damage;
  1596. }
  1597. sint32 Entity::CalculateFormulaByStat(sint32 value, int16 stat) {
  1598. sint32 outValue = value;
  1599. MStats.lock();
  1600. std::map<int16, float>::iterator itr = stats.find(stat);
  1601. if(itr != stats.end())
  1602. outValue = (sint32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1603. MStats.unlock();
  1604. return outValue;
  1605. }
  1606. int32 Entity::CalculateFormulaByStat(int32 value, int16 stat) {
  1607. int32 outValue = value;
  1608. MStats.lock();
  1609. std::map<int16, float>::iterator itr = stats.find(stat);
  1610. if(itr != stats.end())
  1611. outValue = (int32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1612. MStats.unlock();
  1613. return outValue;
  1614. }
  1615. int32 Entity::CalculateFormulaBonus(int32 value, float percent_bonus) {
  1616. return (int32)((float)value * ((percent_bonus / 100.0f) + 1.0f));
  1617. }