Combat.cpp 62 KB

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