9
3

Combat.cpp 61 KB

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