Combat.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. float bonus = 0;
  327. Skill* skill = nullptr;
  328. if(spell->GetSpellData()->resistibility > 0)
  329. bonus -= (1 - spell->GetSpellData()->resistibility)*100;
  330. skill = GetSkillByID(spell->GetSpellData()->mastery_skill, false);
  331. if(skill)
  332. bonus += skill->current_val / 25;
  333. int8 hit_result = 0;
  334. bool is_tick = false; // if spell is already active, this is a tick
  335. if (GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell)){
  336. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  337. is_tick = true;
  338. }
  339. else if(spell->GetSpellData()->type == SPELL_BOOK_TYPE_COMBAT_ART)
  340. hit_result = DetermineHit(victim, damage_type, 0, false);
  341. else
  342. hit_result = DetermineHit(victim, damage_type, 0, true);
  343. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  344. luaspell->last_spellattack_hit = true;
  345. //If this spell is a tick and has already crit, force the tick to crit
  346. if(is_tick){
  347. if(luaspell->crit)
  348. crit_mod = 1;
  349. else
  350. crit_mod = 2;
  351. }
  352. 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)
  353. luaspell->crit = true;
  354. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  355. CheckProcs(PROC_TYPE_MAGICAL_OFFENSIVE, victim);
  356. if(spell->GetSpellData()->success_message.length() > 0){
  357. Client* client = nullptr;
  358. if(IsPlayer())
  359. client = GetZone()->GetClientBySpawn(this);
  360. if(client){
  361. string success_message = spell->GetSpellData()->success_message;
  362. if(success_message.find("%t") < 0xFFFFFFFF)
  363. success_message.replace(success_message.find("%t"), 2, victim->GetName());
  364. client->Message(CHANNEL_YOU_CAST, success_message.c_str());
  365. //commented out the following line as it was causing a duplicate message EmemJR 5/4/2019
  366. //GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  367. }
  368. }
  369. if(spell->GetSpellData()->effect_message.length() > 0){
  370. string effect_message = spell->GetSpellData()->effect_message;
  371. if(effect_message.find("%t") < 0xFFFFFFFF)
  372. effect_message.replace(effect_message.find("%t"), 2, victim->GetName());
  373. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_message.c_str(), victim, 50);
  374. }
  375. }
  376. else {
  377. if(hit_result == DAMAGE_PACKET_RESULT_RESIST)
  378. luaspell->resisted = true;
  379. if(victim->IsNPC())
  380. ((NPC*)victim)->AddHate(this, 5);
  381. luaspell->last_spellattack_hit = false;
  382. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  383. }
  384. if(EngagedInCombat() == false)
  385. {
  386. 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__);
  387. int8 class1_ = GetInfoStruct()->get_class1();
  388. if(class1_ == COMMONER ||
  389. class1_ == FIGHTER ||
  390. class1_ == WARRIOR ||
  391. class1_ == GUARDIAN ||
  392. class1_ == BERSERKER ||
  393. class1_ == BRAWLER ||
  394. class1_ == MONK ||
  395. class1_ == BRUISER ||
  396. class1_ == CRUSADER ||
  397. class1_ == SHADOWKNIGHT ||
  398. class1_ == PALADIN ||
  399. class1_ == SCOUT ||
  400. class1_ == ROGUE ||
  401. class1_ == SWASHBUCKLER ||
  402. class1_ == BRIGAND ||
  403. class1_ == BARD ||
  404. class1_ == TROUBADOR ||
  405. class1_ == DIRGE ||
  406. class1_ == PREDATOR ||
  407. class1_ == RANGER ||
  408. class1_ == ASSASSIN ||
  409. class1_ == ANIMALIST ||
  410. class1_ == BEASTLORD ||
  411. class1_ == SHAPER ||
  412. class1_ == CHANNELER) //note: it would probably be better to add a column to the spells table for "starts autoattack".
  413. {
  414. if (victim->IsNPC())
  415. ((NPC*)victim)->AddHate(this, 5);
  416. else
  417. InCombat(true);
  418. }
  419. }
  420. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  421. Entity* pet = 0;
  422. bool AddHate = false;
  423. if (victim->IsPlayer()) {
  424. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  425. AddHate = true;
  426. }
  427. else
  428. AddHate = true;
  429. if (AddHate) {
  430. pet = ((Entity*)victim)->GetPet();
  431. if (pet)
  432. pet->AddHate(this, 1);
  433. pet = ((Entity*)victim)->GetCharmedPet();
  434. if (pet)
  435. pet->AddHate(this, 1);
  436. }
  437. }
  438. return true;
  439. }
  440. bool Entity::ProcAttack(Spawn* victim, int8 damage_type, int32 low_damage, int32 high_damage, string name, string success_msg, string effect_msg) {
  441. int8 hit_result = DetermineHit(victim, damage_type, 0, true);
  442. if (hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  443. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, name.c_str());
  444. if (success_msg.length() > 0) {
  445. Client* client = 0;
  446. if(IsPlayer())
  447. client = GetZone()->GetClientBySpawn(this);
  448. if(client) {
  449. if(success_msg.find("%t") < 0xFFFFFFFF)
  450. success_msg.replace(success_msg.find("%t"), 2, victim->GetName());
  451. client->Message(CHANNEL_YOU_CAST, success_msg.c_str());
  452. }
  453. }
  454. if (effect_msg.length() > 0) {
  455. if(effect_msg.find("%t") < 0xFFFFFFFF)
  456. effect_msg.replace(effect_msg.find("%t"), 2, victim->GetName());
  457. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_msg.c_str(), victim, 50);
  458. }
  459. }
  460. else {
  461. if(victim->IsNPC())
  462. ((NPC*)victim)->AddHate(this, 5);
  463. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, name.c_str());
  464. }
  465. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  466. Entity* pet = 0;
  467. bool AddHate = false;
  468. if (victim->IsPlayer()) {
  469. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  470. AddHate = true;
  471. }
  472. else
  473. AddHate = true;
  474. if (AddHate) {
  475. pet = ((Entity*)victim)->GetPet();
  476. if (pet)
  477. pet->AddHate(this, 1);
  478. pet = ((Entity*)victim)->GetCharmedPet();
  479. if (pet)
  480. pet->AddHate(this, 1);
  481. }
  482. }
  483. return true;
  484. }
  485. // this is used exclusively by LUA, heal_type is forced lower case via boost::lower(heal_type); in the LUA Functions used by this
  486. 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){
  487. if(!target || !luaspell || !luaspell->spell)
  488. return false;
  489. if (!target->Alive())
  490. return false;
  491. if (target->GetHP() == target->GetTotalHP())
  492. return true;
  493. int32 heal_amt = 0;
  494. bool crit = false;
  495. if(high_heal < low_heal)
  496. high_heal = low_heal;
  497. if(high_heal == low_heal)
  498. heal_amt = high_heal;
  499. else
  500. heal_amt = MakeRandomInt(low_heal, high_heal);
  501. if(!no_calcs){
  502. // if spell is already active, this is a tick
  503. bool is_tick = GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell);
  504. //if is a tick and the spell has crit, force crit, else disable
  505. if(is_tick){
  506. if(luaspell->crit)
  507. crit_mod = 1;
  508. else
  509. crit_mod = 2;
  510. }
  511. if (heal_amt > 0){
  512. if(target->IsEntity())
  513. heal_amt = (int32)CalculateHealAmount((Entity*)target, (sint32)heal_amt, crit_mod, &crit);
  514. else
  515. heal_amt = (int32)CalculateHealAmount(nullptr, (sint32)heal_amt, crit_mod, &crit);
  516. }
  517. }
  518. int16 type = 0;
  519. if (heal_type == "heal") {
  520. if(crit)
  521. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  522. else
  523. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  524. //apply heal
  525. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  526. heal_amt = target->GetTotalHP() - target->GetHP();
  527. target->SetHP(target->GetHP() + heal_amt);
  528. /*
  529. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  530. target->SetHP(target->GetTotalHP());
  531. else
  532. target->SetHP(target->GetHP() + heal_amt);
  533. */
  534. }
  535. else if (heal_type == "power"){
  536. if(crit)
  537. type = HEAL_PACKET_TYPE_CRIT_MANA;
  538. else
  539. type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  540. //give power
  541. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  542. heal_amt = target->GetTotalPower() - target->GetPower();
  543. target->SetPower(GetPower() + heal_amt);
  544. /*
  545. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  546. target->SetPower(target->GetTotalPower());
  547. else
  548. target->SetPower(GetPower() + heal_amt);
  549. */
  550. }
  551. /*else if (heal_type == "Savagery"){
  552. if(crit)
  553. type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
  554. else
  555. type = HEAL_PACKET_TYPE_SAVAGERY;
  556. }
  557. else if (heal_type == "Repair"){
  558. if(crit)
  559. type = HEAL_PACKET_TYPE_CRIT_REPAIR;
  560. else
  561. type = HEAL_PACKET_TYPE_REPAIR;
  562. }*/
  563. else{ //default to heal if type cannot be determined
  564. if(crit)
  565. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  566. else
  567. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  568. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  569. heal_amt = target->GetTotalHP() - target->GetHP();
  570. target->SetHP(target->GetHP() + heal_amt);
  571. /*
  572. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  573. target->SetHP(target->GetTotalHP());
  574. else
  575. target->SetHP(target->GetHP() + heal_amt);
  576. */
  577. }
  578. target->GetZone()->TriggerCharSheetTimer();
  579. if (heal_amt > 0)
  580. GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
  581. CheckProcs(PROC_TYPE_HEALING, target);
  582. CheckProcs(PROC_TYPE_BENEFICIAL, target);
  583. if (target->IsEntity()) {
  584. int32 hate_amt = heal_amt / 2;
  585. set<int32>::iterator itr;
  586. ((Entity*)target)->MHatedBy.lock();
  587. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  588. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  589. if (spawn && spawn->IsEntity() && target != this) {
  590. ((Entity*)spawn)->AddHate(this, hate_amt);
  591. }
  592. }
  593. ((Entity*)target)->MHatedBy.unlock();
  594. }
  595. return true;
  596. }
  597. int8 Entity::DetermineHit(Spawn* victim, int8 damage_type, float ToHitBonus, bool spell){
  598. if(!victim) {
  599. return DAMAGE_PACKET_RESULT_MISS;
  600. }
  601. if(victim->GetInvulnerable()) {
  602. return DAMAGE_PACKET_RESULT_INVULNERABLE;
  603. }
  604. bool behind = false;
  605. // Monk added with Brawler to 360 degree support per KoS Prima Official eGuide Fighter: Monk, pg 138, denoted '360-Degree Avoidance!'
  606. if(!victim->IsEntity() || (!spell && victim->GetAdventureClass() != BRAWLER && victim->GetAdventureClass() != MONK && (behind = BehindTarget(victim)))) {
  607. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  608. }
  609. float bonus = ToHitBonus;
  610. Skill* skill = GetSkillByWeaponType(damage_type, true);
  611. float skillAddedByWeapon = 0.0f;
  612. if(skill)
  613. {
  614. int16 skillID = master_item_list.GetItemStatIDByName(skill->name.data);
  615. if(skillID != 0xFFFFFFFF)
  616. {
  617. MStats.lock();
  618. skillAddedByWeapon = stats[skillID];
  619. MStats.unlock();
  620. }
  621. }
  622. if (skill)
  623. bonus += (skill->current_val+skillAddedByWeapon) / 25;
  624. if (victim->IsEntity())
  625. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  626. Entity* entity_victim = (Entity*)victim;
  627. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  628. sint16 roll_chance = 100;
  629. if(skill)
  630. roll_chance -= skill->current_val / 25;
  631. if(!spell){ // melee or range attack
  632. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  633. if(skill)
  634. roll_chance -= skill->current_val / 25;
  635. if(entity_victim->IsImmune(IMMUNITY_TYPE_RIPOSTE))
  636. return DAMAGE_PACKET_RESULT_RIPOSTE;
  637. // Avoidance Instructions: https://forums.daybreakgames.com/eq2/index.php?threads/avoidance-faq.482979/
  638. /*Parry: reads as parry in the avoidance tooltip, increased by items with +parry on them
  639. Caps at 70%. For plate tanks, works in the front quadrant only, for brawlers this is 360 degrees.
  640. A small % of parries will be ripostes, which not only avoid the attack but also damage your attacker
  641. */
  642. skill = entity_victim->GetSkillByName("Parry", true);
  643. if(skill){
  644. float parryChance = entity_victim->GetInfoStruct()->get_parry();
  645. float chanceValue = (100.0f - parryChance);
  646. if(rand()%roll_chance >= chanceValue){ //successful parry
  647. /* You may only riposte things in the front quadrant.
  648. Riposte is based off of parry: a certain % of parries turn into ripostes.
  649. */
  650. 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
  651. 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.
  652. if(rand()%100 <= riposteChanceValue) {
  653. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  654. return DAMAGE_PACKET_RESULT_RIPOSTE;
  655. }
  656. }
  657. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  658. return DAMAGE_PACKET_RESULT_PARRY;
  659. }
  660. }
  661. skill = nullptr;
  662. float blockChance = 0.0f;
  663. if(victim->GetAdventureClass() == BRAWLER)
  664. skill = entity_victim->GetSkillByName("Deflection", true);
  665. blockChance = entity_victim->GetInfoStruct()->get_block();
  666. if(blockChance > 0.0f)
  667. {
  668. blockChance += (blockChance*(entity_victim->GetInfoStruct()->get_block_chance()/100.0f));
  669. float chanceValue = (100.0f - blockChance);
  670. /* Non-brawlers may only block things in the front quadrant.
  671. Riposte is based off of parry: a certain % of parries turn into ripostes.
  672. */
  673. float rnd = rand()%roll_chance;
  674. if(rnd >= chanceValue){ //successful block
  675. 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
  676. entity_victim->CheckProcs(PROC_TYPE_BLOCK, this);
  677. return (victim->GetAdventureClass() == BRAWLER) ? DAMAGE_PACKET_RESULT_DEFLECT : DAMAGE_PACKET_RESULT_BLOCK;
  678. }
  679. }
  680. }
  681. skill = entity_victim->GetSkillByName("Defense", true);
  682. float dodgeChance = entity_victim->GetInfoStruct()->get_avoidance_base();
  683. if(dodgeChance > 0.0f)
  684. {
  685. float chanceValue = (100.0f - dodgeChance);
  686. float rnd = rand()%roll_chance;
  687. if(rnd >= chanceValue){ //successful dodge
  688. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  689. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  690. }
  691. }
  692. if(rand() % roll_chance >= chance)
  693. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  694. }
  695. else{
  696. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  697. if(skill)
  698. chance -= skill->current_val / 25;
  699. if(rand()%roll_chance >= chance) {
  700. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  701. }
  702. }
  703. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  704. }
  705. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  706. float ret = 1;
  707. switch(damage_type) {
  708. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  709. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  710. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  711. Skill* skill = GetSkillByName("Defense", true);
  712. if(skill)
  713. ret += skill->current_val / 25;
  714. if(IsNPC())
  715. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  716. break;
  717. }
  718. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  719. ret += GetInfoStruct()->get_heat() / 50;
  720. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  721. break;
  722. }
  723. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  724. ret += GetInfoStruct()->get_cold() / 50;
  725. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  726. break;
  727. }
  728. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  729. ret += GetInfoStruct()->get_magic() / 50;
  730. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  731. break;
  732. }
  733. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  734. ret += GetInfoStruct()->get_mental() / 50;
  735. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  736. break;
  737. }
  738. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  739. ret += GetInfoStruct()->get_divine() / 50;
  740. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  741. break;
  742. }
  743. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  744. ret += GetInfoStruct()->get_disease() / 50;
  745. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  746. break;
  747. }
  748. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  749. ret += GetInfoStruct()->get_poison() / 50;
  750. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  751. break;
  752. }
  753. }
  754. return ret;
  755. }
  756. Skill* Entity::GetSkillByWeaponType(int8 type, bool update) {
  757. switch(type) {
  758. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  759. return GetSkillByName("Slashing", update);
  760. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  761. return GetSkillByName("Crushing", update);
  762. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  763. return GetSkillByName("Piercing", update);
  764. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  765. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  766. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  767. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  768. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  769. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  770. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  771. return GetSkillByName("Disruption", update);
  772. }
  773. return 0;
  774. }
  775. 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) {
  776. if(!victim || victim->GetHP() == 0)
  777. return false;
  778. int8 hit_result = 0;
  779. int16 blow_type = 0;
  780. sint32 damage = 0;
  781. bool crit = false;
  782. if(low_damage > high_damage)
  783. high_damage = low_damage;
  784. if(low_damage == high_damage)
  785. damage = low_damage;
  786. else
  787. damage = MakeRandomInt(low_damage, high_damage);
  788. if(!no_calcs) {
  789. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  790. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  791. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  792. //DPS mod is only applied to auto attacks
  793. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE ) {
  794. damage *= (info_struct.get_dps_multiplier());
  795. }
  796. //Potency and ability mod is only applied to spells/CAs
  797. else {
  798. damage = CalculateDamageAmount(victim, damage, type, damage_type, spell);
  799. }
  800. if(!crit_mod || crit_mod == 1){
  801. //force crit if crit_mod == 1
  802. if(crit_mod == 1)
  803. crit = true;
  804. // Crit Roll
  805. else {
  806. victim->MStats.lock();
  807. float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  808. victim->MStats.unlock();
  809. if (MakeRandomFloat(0, 100) <= chance)
  810. crit = true;
  811. }
  812. if(crit){
  813. //Apply total crit multiplier with crit bonus
  814. if(info_struct.get_crit_bonus() > 0)
  815. damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
  816. else
  817. damage *= 1.3;
  818. // Change packet type to crit
  819. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  820. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  821. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  822. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  823. }
  824. }
  825. // TODO: Mitigation equation from http://www.guildportal.com/Guild.aspx?GuildID=20881&TabID=189653&ForumID=95908&TopicID=9024250
  826. }
  827. LogWrite(MISC__TODO, 3, "TODO", "Take players armor into account\nfile: %s, func: %s, line: %i)", __FILE__, __FUNCTION__, __LINE__);
  828. bool useWards = false;
  829. if(damage <= 0){
  830. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  831. damage = 0;
  832. }
  833. else{
  834. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  835. GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this);
  836. int32 prevDmg = damage;
  837. damage = victim->CheckWards(this, damage, damage_type);
  838. if (damage < (sint64)prevDmg)
  839. useWards = true;
  840. victim->TakeDamage(damage);
  841. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  842. if (IsPlayer()) {
  843. switch (damage_type) {
  844. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  845. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  846. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  847. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  848. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  849. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  850. break;
  851. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  852. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  853. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  854. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  855. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  856. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  857. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  858. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  859. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  860. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  861. break;
  862. }
  863. }
  864. }
  865. if(victim->IsNPC() && victim->GetHP() > 0)
  866. ((Entity*)victim)->AddHate(this, damage);
  867. Entity* attacker = nullptr;
  868. if(!ignore_attacker)
  869. attacker = this;
  870. if (damage > 0) {
  871. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  872. if (IsStealthed() || IsInvis())
  873. CancelAllStealth();
  874. if (victim->IsEntity())
  875. ((Entity*)victim)->CheckInterruptSpell(this);
  876. }
  877. else if (useWards)
  878. {
  879. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  880. }
  881. if (victim->GetHP() <= 0)
  882. KillSpawn(victim, damage_type, blow_type);
  883. else {
  884. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  885. if (spell_name)
  886. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  887. else
  888. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  889. }
  890. return crit;
  891. }
  892. void Entity::AddHate(Entity* attacker, sint32 hate) {
  893. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  894. return;
  895. // If a players pet and protect self is off
  896. if (IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
  897. return;
  898. hate = attacker->CalculateHateAmount(this, hate);
  899. if (IsNPC()) {
  900. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  901. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  902. // if encounter size is 0 then add the attacker to the encounter
  903. if (((NPC*)this)->Brain()->GetEncounterSize() == 0)
  904. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  905. }
  906. if (attacker->GetThreatTransfer() && hate > 0) {
  907. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  908. if (transfer_target && transfer_target->IsEntity()) {
  909. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  910. hate -= transfered_hate;
  911. this->AddHate((Entity*)transfer_target, transfered_hate);
  912. }
  913. }
  914. // If pet is adding hate add some to the pets owner as well
  915. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  916. AddHate(((NPC*)attacker)->GetOwner(), 1);
  917. // If player and player has a pet and protect master is set add hate to the pet
  918. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
  919. // If we have a combat pet add hate to it
  920. if (((Player*)this)->GetPet())
  921. AddHate(((Player*)this)->GetPet(), 1);
  922. if (((Player*)this)->GetCharmedPet())
  923. AddHate(((Player*)this)->GetCharmedPet(), 1);
  924. }
  925. // If this spawn has a spawn group then add the attacker to the hate list of the other
  926. // group members if not already in their list
  927. if (HasSpawnGroup()) {
  928. vector<Spawn*>* group = GetSpawnGroup();
  929. vector<Spawn*>::iterator itr;
  930. for (itr = group->begin(); itr != group->end(); itr++) {
  931. if (!(*itr)->IsNPC())
  932. continue;
  933. NPC* spawn = (NPC*)(*itr);
  934. if (spawn->Brain()->GetHate(attacker) == 0)
  935. spawn->Brain()->AddHate(attacker, 1);
  936. }
  937. safe_delete(group);
  938. }
  939. }
  940. bool Entity::CheckFizzleSpell(LuaSpell* spell) {
  941. if(!spell || !rule_manager.GetGlobalRule(R_Spells, EnableFizzleSpells)->GetInt8()
  942. || spell->spell->GetSpellData()->can_fizzle == false)
  943. return false;
  944. float fizzleMaxSkill = rule_manager.GetGlobalRule(R_Spells, FizzleMaxSkill)->GetFloat();
  945. float baseFizzle = rule_manager.GetGlobalRule(R_Spells, DefaultFizzleChance)->GetFloat()/100.0f; // 10%
  946. float skillObtained = rule_manager.GetGlobalRule(R_Spells, FizzleDefaultSkill)->GetFloat(); // default of .2f so we don't go over the threshold if no skill
  947. Skill* skill = GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
  948. if(skill && spell->spell->GetSpellData()->min_class_skill_req > 0)
  949. {
  950. float skillObtained = skill->current_val / spell->spell->GetSpellData()->min_class_skill_req;
  951. if(skillObtained > fizzleMaxSkill) // 120% over the skill value
  952. {
  953. skillObtained = fizzleMaxSkill;
  954. }
  955. baseFizzle = (fizzleMaxSkill - skillObtained) * baseFizzle;
  956. float totalSuccessChance = 1.0f - baseFizzle;
  957. float randResult = MakeRandomFloat(0.0f, 1.0f);
  958. if(randResult > totalSuccessChance)
  959. return true;
  960. }
  961. return false;
  962. }
  963. bool Entity::CheckInterruptSpell(Entity* attacker) {
  964. if(!IsCasting())
  965. return false;
  966. Spell* spell = GetZone()->GetSpell(this);
  967. if(!spell || spell->GetSpellData()->interruptable == 0)
  968. return false;
  969. if(GetInfoStruct()->get_no_interrupt())
  970. return false;
  971. //originally base of 30 percent chance to continue casting if attacked
  972. //modified to 50% and added global rule, 30% was too small at starting levels
  973. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  974. Skill* skill = GetSkillByName("Focus", true);
  975. float focusSkillPts = 0.0f;
  976. MStats.lock();
  977. focusSkillPts = stats[ITEM_STAT_FOCUS];
  978. MStats.unlock();
  979. if(skill)
  980. percent += ((skill->current_val + 1 + focusSkillPts)/6);
  981. if(MakeRandomInt(1, 100) > percent) {
  982. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  983. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  984. return true;
  985. }
  986. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  987. return false;
  988. }
  989. void Entity::KillSpawn(Spawn* dead, int8 damage_type, int16 kill_blow_type) {
  990. if(!dead)
  991. return;
  992. if (IsPlayer()) {
  993. Client* client = GetZone()->GetClientBySpawn(this);
  994. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  995. if (packet) {
  996. client->QueuePacket(packet->serialize());
  997. }
  998. safe_delete(packet);
  999. ((Player*)this)->InCombat(false);
  1000. }
  1001. if (IsPlayer() && dead->IsEntity())
  1002. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  1003. //if (dead->IsEntity()) same code called in zone server
  1004. //((Entity*)dead)->InCombat(false);
  1005. /* just for sake of not knowing if we are in a read lock, write lock, or no lock
  1006. ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
  1007. */
  1008. if (dead->IsPet())
  1009. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
  1010. else if (dead->IsEntity()) {
  1011. // remove all pets for this entity
  1012. ((Entity*)dead)->DismissAllPets(false, true);
  1013. }
  1014. // If not in combat and no one in the encounter list add this killer to the list
  1015. if(dead->EngagedInCombat() == false && dead->IsNPC() && ((NPC*)dead)->Brain()->GetEncounterSize() == 0)
  1016. ((NPC*)dead)->Brain()->AddToEncounter(this);
  1017. if (IsCasting())
  1018. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  1019. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  1020. // Kill movement for the dead npc so the corpse doesn't move
  1021. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  1022. dead->ClearRunningLocations();
  1023. dead->CalculateRunningLocation(true);
  1024. GetZone()->KillSpawn(true, dead, this, true, damage_type, kill_blow_type);
  1025. }
  1026. void Entity::HandleDeathExperienceDebt(Spawn* killer)
  1027. {
  1028. if(!IsPlayer())
  1029. return;
  1030. float ruleDebt = 0.0f;
  1031. if(killer && killer->IsPlayer())
  1032. ruleDebt = rule_manager.GetGlobalRule(R_Combat, PVPDeathExperienceDebt)->GetFloat()/100.0f;
  1033. else
  1034. ruleDebt = rule_manager.GetGlobalRule(R_Combat, DeathExperienceDebt)->GetFloat()/100.0f;
  1035. if(ruleDebt > 0.0f)
  1036. {
  1037. bool groupDebt = rule_manager.GetGlobalRule(R_Combat, GroupExperienceDebt)->GetBool();
  1038. if(groupDebt && ((Player*)this)->GetGroupMemberInfo())
  1039. {
  1040. world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
  1041. PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)this)->GetGroupMemberInfo()->group_id);
  1042. if (group)
  1043. {
  1044. group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
  1045. deque<GroupMemberInfo*>* members = group->GetMembers();
  1046. int32 size = members->size();
  1047. float xpDebtPerMember = ruleDebt/(float)size;
  1048. deque<GroupMemberInfo*>::iterator itr;
  1049. for (itr = members->begin(); itr != members->end(); itr++) {
  1050. GroupMemberInfo* gmi = *itr;
  1051. if (gmi->client && gmi->client->GetPlayer()) {
  1052. gmi->client->GetPlayer()->GetInfoStruct()->set_xp_debt(gmi->client->GetPlayer()->GetInfoStruct()->get_xp_debt()+xpDebtPerMember);
  1053. gmi->client->GetPlayer()->SetCharSheetChanged(true);
  1054. }
  1055. }
  1056. group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
  1057. }
  1058. world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
  1059. }
  1060. else
  1061. {
  1062. ((Player*)this)->GetInfoStruct()->set_xp_debt(((Player*)this)->GetInfoStruct()->get_xp_debt()+ruleDebt);
  1063. ((Player*)this)->SetCharSheetChanged(true);
  1064. }
  1065. }
  1066. }
  1067. void Entity::ProcessCombat() {
  1068. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  1069. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  1070. }
  1071. void NPC::ProcessCombat() {
  1072. MBrain.writelock(__FUNCTION__, __LINE__);
  1073. // Check to see if it is time to call the AI again
  1074. if (GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  1075. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  1076. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  1077. m_brain->Think();
  1078. // Set the time for when the brain was last called
  1079. m_brain->SetLastTick(Timer::GetCurrentTime2());
  1080. }
  1081. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  1082. }
  1083. void Player::ProcessCombat() {
  1084. // if not in combat OR casting a spell OR dazed OR feared return out
  1085. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  1086. return;
  1087. //If no target delete combat_target and return out
  1088. Spawn* Target = GetZone()->GetSpawnByID(target);
  1089. if (!Target) {
  1090. combat_target = 0;
  1091. if (target > 0) {
  1092. SetTarget(0);
  1093. }
  1094. return;
  1095. }
  1096. // If is not an entity return out
  1097. if (!Target->IsEntity())
  1098. return;
  1099. // Reset combat target
  1100. combat_target = 0;
  1101. if (Target->HasTarget()) {
  1102. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1103. Spawn* secondary_target = Target->GetTarget();
  1104. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1105. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1106. combat_target = secondary_target;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // If combat_target wasn't set in the above if set it to the original target
  1112. if (!combat_target)
  1113. combat_target = Target;
  1114. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1115. if(!combat_target)
  1116. return;
  1117. float distance = 0;
  1118. distance = GetDistance(combat_target);
  1119. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1120. if (GetRangeAttack()) {
  1121. // We are doing ranged auto attacks
  1122. //check to see if we can attack the target AND the ranged weapon is ready
  1123. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1124. Item* weapon = 0;
  1125. Item* ammo = 0;
  1126. // Get the currently equiped weapon and ammo for the ranged attack
  1127. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1128. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1129. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1130. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1131. if(weapon && ammo) {
  1132. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1133. RangeAttack(combat_target, distance, weapon, ammo);
  1134. }
  1135. else {
  1136. Client* client = GetZone()->GetClientBySpawn(this);
  1137. if (client) {
  1138. // 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
  1139. if (!ammo)
  1140. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1141. if (!weapon)
  1142. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1143. }
  1144. }
  1145. }
  1146. }
  1147. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1148. // We are doing melee auto attacks and are within range
  1149. // Check to see if we can attack the target
  1150. if(AttackAllowed((Entity*)combat_target)) {
  1151. // Check to see if the primary melee weapon is ready
  1152. if(PrimaryWeaponReady()) {
  1153. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1154. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1155. MeleeAttack(combat_target, distance, true);
  1156. }
  1157. // Check to see if the secondary weapon is ready
  1158. if(SecondaryWeaponReady()) {
  1159. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1160. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1161. MeleeAttack(combat_target, distance, false);
  1162. }
  1163. }
  1164. }
  1165. }
  1166. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1167. float mod = CalculateAttackSpeedMod();
  1168. bool dual_wield = IsDualWield();
  1169. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1170. //Add 33% longer delay if dual wielding
  1171. if(dual_wield && ! ranged) {
  1172. if(primary)
  1173. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1174. else
  1175. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1176. }
  1177. else {
  1178. if(primary)
  1179. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1180. else if(ranged)
  1181. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1182. else
  1183. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1184. }
  1185. }
  1186. float Entity::CalculateAttackSpeedMod(){
  1187. float aspeed = info_struct.get_attackspeed();
  1188. if(aspeed > 0) {
  1189. if (aspeed <= 100)
  1190. return (aspeed / 100 + 1);
  1191. else if (aspeed <= 200)
  1192. return 2.25;
  1193. }
  1194. return 1;
  1195. }
  1196. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1197. if (type == 0) {
  1198. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1199. return;
  1200. }
  1201. if (!item && !spell) {
  1202. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1203. return;
  1204. }
  1205. MProcList.writelock(__FUNCTION__, __LINE__);
  1206. Proc* proc = new Proc();
  1207. proc->chance = chance;
  1208. proc->item = item;
  1209. proc->spell = spell;
  1210. proc->spellid = spell->spell->GetSpellID();
  1211. m_procList[type].push_back(proc);
  1212. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1213. }
  1214. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1215. if (!item && !spell) {
  1216. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1217. return;
  1218. }
  1219. MProcList.writelock(__FUNCTION__, __LINE__);
  1220. map<int8, vector<Proc*> >::iterator proc_itr;
  1221. vector<Proc*>::iterator itr;
  1222. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1223. itr = proc_itr->second.begin();
  1224. while (itr != proc_itr->second.end()) {
  1225. Proc* proc = *itr;
  1226. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1227. itr = proc_itr->second.erase(itr);
  1228. safe_delete(proc);
  1229. }
  1230. else
  1231. itr++;
  1232. }
  1233. }
  1234. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1235. }
  1236. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1237. lua_State* state = 0;
  1238. bool item_proc = false;
  1239. int8 num_args = 3;
  1240. if (proc->spell) {
  1241. state = proc->spell->state;
  1242. }
  1243. else if (proc->item) {
  1244. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1245. item_proc = true;
  1246. }
  1247. if (!state) {
  1248. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1249. return false;
  1250. }
  1251. lua_getglobal(state, "proc");
  1252. if (item_proc) {
  1253. num_args++;
  1254. lua_interface->SetItemValue(state, proc->item);
  1255. }
  1256. lua_interface->SetSpawnValue(state, this);
  1257. lua_interface->SetSpawnValue(state, target);
  1258. lua_interface->SetInt32Value(state, type);
  1259. /*
  1260. Add spell data from db in case of a spell proc here...
  1261. */
  1262. if (!item_proc) {
  1263. // Append spell data to the param list
  1264. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1265. for(int32 i = 0; i < data->size(); i++) {
  1266. switch(data->at(i)->type) {
  1267. case 0:{
  1268. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1269. break;
  1270. }
  1271. case 1:{
  1272. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1273. break;
  1274. }
  1275. case 2:{
  1276. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1277. break;
  1278. }
  1279. case 3:{
  1280. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1281. break;
  1282. }
  1283. default:{
  1284. 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());
  1285. return false;
  1286. }
  1287. }
  1288. num_args++;
  1289. }
  1290. }
  1291. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1292. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1293. lua_pop(state, 1);
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. void Entity::CheckProcs(int8 type, Spawn* target) {
  1299. if (type == 0) {
  1300. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1301. return;
  1302. }
  1303. float roll = MakeRandomFloat(0, 100);
  1304. vector<Proc*> tmpList;
  1305. MProcList.readlock(__FUNCTION__, __LINE__);
  1306. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1307. Proc* proc = m_procList[type].at(i);
  1308. if (roll <= proc->chance)
  1309. {
  1310. Proc* tmpProc = new Proc();
  1311. tmpProc->chance = proc->chance;
  1312. tmpProc->item = proc->item;
  1313. tmpProc->spell = proc->spell;
  1314. tmpProc->spellid = proc->spellid;
  1315. tmpList.push_back(tmpProc);
  1316. }
  1317. }
  1318. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1319. vector<Proc*>::iterator proc_itr;
  1320. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1321. Proc* tmpProc = *proc_itr;
  1322. CastProc(tmpProc, type, target);
  1323. proc_itr++;
  1324. safe_delete(tmpProc);
  1325. }
  1326. }
  1327. void Entity::ClearProcs() {
  1328. MProcList.writelock(__FUNCTION__, __LINE__);
  1329. map<int8, vector<Proc*> >::iterator proc_itr;
  1330. vector<Proc*>::iterator itr;
  1331. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1332. itr = proc_itr->second.begin();
  1333. while (itr != proc_itr->second.end()) {
  1334. safe_delete(*itr);
  1335. itr = proc_itr->second.erase(itr);
  1336. }
  1337. proc_itr->second.clear();
  1338. }
  1339. m_procList.clear();
  1340. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1341. }
  1342. sint32 Entity::CalculateHateAmount(Spawn* target, sint32 amt) {
  1343. sint32 hate = amt;
  1344. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AMOUNT);
  1345. amt = CalculateFormulaByStat(amt, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1346. amt = CalculateFormulaByStat(amt, ITEM_STAT_ABILITY_MODIFIER);
  1347. return amt;
  1348. }
  1349. sint32 Entity::CalculateHealAmount(Spawn* target, sint32 amt, int8 crit_mod, bool* crit, bool skip_crit_mod) {
  1350. amt = CalculateFormulaByStat(amt, ITEM_STAT_HEAL_AMOUNT);
  1351. amt = CalculateFormulaByStat(amt, ITEM_STAT_SPELL_AND_HEAL);
  1352. //Potency Mod
  1353. amt = CalculateFormulaByStat(amt, ITEM_STAT_POTENCY);
  1354. //Ability Mod
  1355. amt += (int32)min((int32)GetInfoStruct()->get_ability_modifier(), (int32)(amt / 2));
  1356. if(!skip_crit_mod){
  1357. if(!crit_mod || crit_mod == 1){
  1358. if(crit_mod == 1)
  1359. *crit = true;
  1360. else if(!*crit) {
  1361. // Crit Roll
  1362. float chance = (float)max((float)0, (float)GetInfoStruct()->get_crit_chance());
  1363. *crit = (MakeRandomFloat(0, 100) <= chance);
  1364. }
  1365. if(*crit){
  1366. //Apply total crit multiplier with crit bonus
  1367. amt *= ((GetInfoStruct()->get_crit_bonus() / 100) + 1.3);
  1368. }
  1369. }
  1370. }
  1371. return amt;
  1372. }
  1373. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, LuaSpell* spell) {
  1374. return CalculateDamageAmount(target, damage, base_type, damage_type, (spell && spell->spell) ? spell->spell->GetSpellData()->target_type : 0);
  1375. }
  1376. sint32 Entity::CalculateDamageAmount(Spawn* target, sint32 damage, int8 base_type, int8 damage_type, int8 target_type) {
  1377. // only spells may add spell damage item stat
  1378. if(damage_type >= DAMAGE_PACKET_DAMAGE_TYPE_HEAT && damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_POISON)
  1379. {
  1380. // https://forums.daybreakgames.com/eq2/index.php?threads/potency-and-ability-mod-what.4316/
  1381. // Spell damage model assuming 100% crit chance:
  1382. // (Spell base damage * Base damage modifier * Int bonus * Skill bonus * Potency + Ability modifier) * Crit bonus * Spell double attack
  1383. /** Spell base damage: Get all master spells
  1384. Base damage modifier: Very very rare. Available from Wizard aa Wisdom line(Brainstorm). Get it, cherish it.
  1385. 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.
  1386. 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.
  1387. Potency: A straight damage modifier, the more you can get the better. No practical cap.
  1388. 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.
  1389. 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.
  1390. 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.
  1391. Makes the spell cast twice with some limitations.
  1392. **/
  1393. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_DAMAGE);
  1394. }
  1395. if(base_type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  1396. damage = CalculateFormulaByStat(damage, ITEM_STAT_SPELL_AND_COMBAT_ART_DAMAGE);
  1397. // combat abilities only bonus
  1398. if(damage_type <= DAMAGE_PACKET_DAMAGE_TYPE_PIERCE)
  1399. damage = CalculateFormulaByStat(damage, ITEM_STAT_TAUNT_AND_COMBAT_ART_DAMAGE);
  1400. // Potency mod
  1401. damage = CalculateFormulaByStat(damage, ITEM_STAT_POTENCY);
  1402. int32 modifier = 2;
  1403. if(target_type == SPELL_TARGET_GROUP_AE || target_type == SPELL_TARGET_RAID_AE || target_type == SPELL_TARGET_OTHER_GROUP_AE)
  1404. {
  1405. modifier = 3;
  1406. }
  1407. // Ability mod can only give up to half of damage after potency
  1408. int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / modifier));
  1409. damage += mod;
  1410. return damage;
  1411. }
  1412. sint32 Entity::CalculateFormulaByStat(sint32 value, int16 stat) {
  1413. sint32 outValue = value;
  1414. MStats.lock();
  1415. std::map<int16, float>::iterator itr = stats.find(stat);
  1416. if(itr != stats.end())
  1417. outValue = (sint32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1418. MStats.unlock();
  1419. return outValue;
  1420. }
  1421. int32 Entity::CalculateFormulaByStat(int32 value, int16 stat) {
  1422. int32 outValue = value;
  1423. MStats.lock();
  1424. std::map<int16, float>::iterator itr = stats.find(stat);
  1425. if(itr != stats.end())
  1426. outValue = (int32)((float)value * ((itr->second / 100.0f) + 1.0f));
  1427. MStats.unlock();
  1428. return outValue;
  1429. }
  1430. int32 Entity::CalculateFormulaBonus(int32 value, float percent_bonus) {
  1431. return (int32)((float)value * ((percent_bonus / 100.0f) + 1.0f));
  1432. }