Combat.cpp 69 KB

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