9
3

Combat.cpp 57 KB

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