Combat.cpp 65 KB

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