Combat.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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 <math.h>
  30. extern Classes classes;
  31. extern ConfigReader configReader;
  32. extern MasterSkillList master_skill_list;
  33. extern RuleManager rule_manager;
  34. extern LuaInterface* lua_interface;
  35. /* ******************************************************************************
  36. DamageSpawn() - Damage equation
  37. MeleeAttack() - Melee auto attacks
  38. RangeAttack() - Range auto attacks
  39. DetermineHit() - ToHit chance as well as defender parry / dodge / block / riposte
  40. CheckInterruptSpell() - Interrupt equations
  41. No mitigation equations yet
  42. ****************************************************************************** */
  43. /* New Combat code */
  44. bool Entity::PrimaryWeaponReady() {
  45. //Can only be ready if no ranged timer
  46. if (GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) {
  47. if (GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool Entity::SecondaryWeaponReady() {
  53. //Can only be ready if no ranged timer
  54. // if(IsDualWield() && (GetPrimaryLastAttackTime()
  55. if (IsDualWield() && (GetSecondaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay())))) {
  56. if(GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
  57. return true;
  58. }
  59. return false;
  60. }
  61. bool Entity::RangeWeaponReady() {
  62. //Ranged can only be ready if no other attack timers are active
  63. if(GetRangeLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))) {
  64. if((GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) && (GetSecondaryLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay()))){
  65. if(!IsPlayer() || ((Player*)this)->GetRangeAttack()) {
  66. return true;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. bool Entity::AttackAllowed(Entity* target, float distance, bool range_attack) {
  73. Entity* attacker = this;
  74. Client* client = 0;
  75. if(!target || IsMezzedOrStunned() || IsDazed()) {
  76. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: no target, mezzed, stunned or dazed");
  77. return false;
  78. }
  79. if (IsPlayer())
  80. client = GetZone()->GetClientBySpawn(this);
  81. if (IsPet())
  82. attacker = ((NPC*)this)->GetOwner();
  83. if (target->IsNPC() && ((NPC*)target)->IsPet()){
  84. if (((NPC*)target)->GetOwner())
  85. target = ((NPC*)target)->GetOwner();
  86. }
  87. if (attacker == target) {
  88. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: attacker tried to attack himself or his pet.");
  89. return false;
  90. }
  91. if (IsPlayer() && target->GetAttackable() == 0) {
  92. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is not attackable");
  93. return false;
  94. }
  95. if (IsPlayer() && target->IsBot()) {
  96. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: players are not allowed to attack bots");
  97. return false;
  98. }
  99. if (attacker->IsPlayer() && target->IsPlayer())
  100. {
  101. bool pvp_allowed = rule_manager.GetGlobalRule(R_PVP, AllowPVP)->GetBool();
  102. if (!pvp_allowed) {
  103. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp is not allowed");
  104. return false;
  105. }
  106. else
  107. {
  108. sint32 pvpLevelRange = rule_manager.GetGlobalRule(R_PVP, LevelRange)->GetSInt32();
  109. int32 attackerLevel = attacker->GetLevel();
  110. int32 defenderLevel = target->GetLevel();
  111. if ((sint32)abs((sint32)attackerLevel - (sint32)defenderLevel) > pvpLevelRange)
  112. {
  113. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp range of %i exceeded abs(%i-%i).", pvpLevelRange, attackerLevel, defenderLevel);
  114. return false;
  115. }
  116. }
  117. }
  118. if (target->GetHP() <= 0) {
  119. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is dead");
  120. return false;
  121. }
  122. if(range_attack && distance != 0) {
  123. Item* weapon = 0;
  124. Item* ammo = 0;
  125. if(attacker->IsPlayer()) {
  126. weapon = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  127. ammo = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  128. }
  129. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  130. // Distance is less then min weapon range
  131. if(distance < weapon->ranged_info->range_low) {
  132. if (client)
  133. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too close! Move back!");
  134. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target to close");
  135. return false;
  136. }
  137. // Distance is greater then max weapon range
  138. if (distance > (weapon->ranged_info->range_high + ammo->thrown_info->range)) {
  139. if (client)
  140. client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too far away! Move closer!");
  141. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target is to far");
  142. return false;
  143. }
  144. }
  145. }
  146. else if (distance != 0) {
  147. if(distance >= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  148. LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: distance is beyond melee range");
  149. return false;
  150. }
  151. }
  152. LogWrite(MISC__TODO, 3, "TODO", "Add more AttackAllowed calculations\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
  153. return true;
  154. }
  155. void Entity::MeleeAttack(Spawn* victim, float distance, bool primary, bool multi_attack) {
  156. if(!victim)
  157. return;
  158. int8 damage_type = 0;
  159. int32 min_damage = 0;
  160. int32 max_damage = 0;
  161. if(primary) {
  162. damage_type = GetPrimaryWeaponType();
  163. min_damage = GetPrimaryWeaponMinDamage();
  164. max_damage = GetPrimaryWeaponMaxDamage();
  165. }
  166. else {
  167. damage_type = GetSecondaryWeaponType();
  168. min_damage = GetSecondaryWeaponMinDamage();
  169. max_damage = GetSecondaryWeaponMaxDamage();
  170. }
  171. if (IsStealthed() || IsInvis())
  172. CancelAllStealth();
  173. int8 hit_result = DetermineHit(victim, damage_type, 0, false);
  174. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL){
  175. /*if(GetAdventureClass() == MONK){
  176. max_damage*=3;
  177. crit_chance = GetLevel()/4+5;
  178. }
  179. else if(GetAdventureClass() == BRUISER){
  180. min_damage = GetLevel();
  181. max_damage*=3;
  182. crit_chance = GetLevel()/3+5;
  183. }
  184. if(rand()%100 <=crit_chance){
  185. max_damage*= 2;
  186. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG, damage_type, min_damage, max_damage, 0);
  187. }
  188. else*/
  189. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, damage_type, min_damage, max_damage, 0);
  190. if (!multi_attack) {
  191. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  192. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  193. }
  194. }
  195. else{
  196. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, hit_result, damage_type, 0, 0);
  197. if(hit_result == DAMAGE_PACKET_RESULT_RIPOSTE && victim->IsEntity())
  198. ((Entity*)victim)->MeleeAttack(this, distance, true);
  199. }
  200. //Multi Attack roll
  201. if(!multi_attack){
  202. float multi_attack = info_struct.multi_attack;
  203. if(multi_attack > 0){
  204. float chance = multi_attack;
  205. if (multi_attack > 100){
  206. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  207. chance = (multi_attack - (floor((float) ((multi_attack / 100) * 100))));
  208. while(automatic_multi > 0){
  209. MeleeAttack(victim, 100, primary, true);
  210. automatic_multi--;
  211. }
  212. }
  213. if (MakeRandomFloat(0, 100) <= chance)
  214. MeleeAttack(victim, 100, primary, true);
  215. }
  216. }
  217. //Apply attack speed mods
  218. if(!multi_attack)
  219. SetAttackDelay(primary);
  220. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  221. ((NPC*)victim)->AddHate(this, 50);
  222. }
  223. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  224. Entity* pet = 0;
  225. bool AddHate = false;
  226. if (victim->IsPlayer()) {
  227. if (((Player*)victim)->GetInfoStruct()->pet_behavior & 1)
  228. AddHate = true;
  229. }
  230. else
  231. AddHate = true;
  232. if (AddHate) {
  233. pet = ((Entity*)victim)->GetPet();
  234. if (pet)
  235. pet->AddHate(this, 1);
  236. pet = ((Entity*)victim)->GetCharmedPet();
  237. if (pet)
  238. pet->AddHate(this, 1);
  239. }
  240. }
  241. }
  242. void Entity::RangeAttack(Spawn* victim, float distance, Item* weapon, Item* ammo, bool multi_attack) {
  243. if(!victim)
  244. return;
  245. if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
  246. if(weapon->ranged_info->range_low <= distance && (weapon->ranged_info->range_high + ammo->thrown_info->range) >= distance) {
  247. int8 hit_result = DetermineHit(victim, ammo->thrown_info->damage_type, ammo->thrown_info->hit_bonus, false);
  248. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  249. 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);
  250. if (!multi_attack) {
  251. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  252. CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
  253. }
  254. }
  255. else
  256. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, hit_result, ammo->thrown_info->damage_type, 0, 0);
  257. // If is a player subtract ammo
  258. if (IsPlayer()) {
  259. if (ammo->details.count > 1) {
  260. ammo->details.count -= 1;
  261. ammo->save_needed = true;
  262. }
  263. else
  264. ((Player*)this)->equipment_list.RemoveItem(ammo->details.slot_id, true);
  265. Client* client = GetZone()->GetClientBySpawn(this);
  266. EQ2Packet* outapp = ((Player*)this)->GetEquipmentList()->serialize(client->GetVersion());
  267. if(outapp)
  268. client->QueuePacket(outapp);
  269. }
  270. if(victim->IsNPC() && victim->EngagedInCombat() == false) {
  271. ((NPC*)victim)->AddHate(this, 50);
  272. }
  273. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  274. Entity* pet = 0;
  275. bool AddHate = false;
  276. if (victim->IsPlayer()) {
  277. if (((Player*)victim)->GetInfoStruct()->pet_behavior & 1)
  278. AddHate = true;
  279. }
  280. else
  281. AddHate = true;
  282. if (AddHate) {
  283. pet = ((Entity*)victim)->GetPet();
  284. if (pet)
  285. pet->AddHate(this, 1);
  286. pet = ((Entity*)victim)->GetCharmedPet();
  287. if (pet)
  288. pet->AddHate(this, 1);
  289. }
  290. }
  291. // Check Ranged attack proc
  292. CheckProcs(PROC_TYPE_RANGED_ATTACK, victim);
  293. // Check Ranged defence proc
  294. if (victim->IsEntity())
  295. ((Entity*)victim)->CheckProcs(PROC_TYPE_RANGED_DEFENSE, this);
  296. SetRangeLastAttackTime(Timer::GetCurrentTime2());
  297. }
  298. }
  299. //Multi Attack roll
  300. if(!multi_attack){
  301. float multi_attack = info_struct.multi_attack;
  302. if(multi_attack > 0){
  303. float chance = multi_attack;
  304. if (multi_attack > 100){
  305. int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
  306. chance = (multi_attack - (floor((float)(multi_attack / 100) * 100)));
  307. while(automatic_multi > 0){
  308. RangeAttack(victim, 100, weapon, ammo, true);
  309. automatic_multi--;
  310. }
  311. }
  312. if (MakeRandomFloat(0, 100) <= chance)
  313. RangeAttack(victim, 100, weapon, ammo, true);
  314. }
  315. }
  316. //Apply attack speed mods
  317. if(!multi_attack)
  318. SetAttackDelay(false, true);
  319. }
  320. bool Entity::SpellAttack(Spawn* victim, float distance, LuaSpell* luaspell, int8 damage_type, int32 low_damage, int32 high_damage, int8 crit_mod, bool no_calcs){
  321. if(!victim || !luaspell || !luaspell->spell)
  322. return false;
  323. Spell* spell = luaspell->spell;
  324. float bonus = 0;
  325. Skill* skill = nullptr;
  326. if(spell->GetSpellData()->resistibility > 0)
  327. bonus -= (1 - spell->GetSpellData()->resistibility)*100;
  328. skill = master_skill_list.GetSkill(spell->GetSpellData()->mastery_skill);
  329. if(skill){
  330. skill = GetSkillByName(skill->name.data.c_str(), true);
  331. if(skill)
  332. bonus += skill->current_val / 25;
  333. }
  334. int8 hit_result = 0;
  335. bool is_tick = false; // if spell is already active, this is a tick
  336. if (GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell)){
  337. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  338. is_tick = true;
  339. }
  340. else if(spell->GetSpellData()->type == SPELL_BOOK_TYPE_COMBAT_ART)
  341. hit_result = DetermineHit(victim, damage_type, 0, false);
  342. else
  343. hit_result = DetermineHit(victim, damage_type, 0, true);
  344. if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  345. luaspell->last_spellattack_hit = true;
  346. //If this spell is a tick and has already crit, force the tick to crit
  347. if(is_tick){
  348. if(luaspell->crit)
  349. crit_mod = 1;
  350. else
  351. crit_mod = 2;
  352. }
  353. if(DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, spell->GetName(), crit_mod, is_tick, no_calcs) && !luaspell->crit)
  354. luaspell->crit = true;
  355. CheckProcs(PROC_TYPE_OFFENSIVE, victim);
  356. CheckProcs(PROC_TYPE_MAGICAL_OFFENSIVE, victim);
  357. if(spell->GetSpellData()->success_message.length() > 0){
  358. Client* client = nullptr;
  359. if(IsPlayer())
  360. client = GetZone()->GetClientBySpawn(this);
  361. if(client){
  362. string success_message = spell->GetSpellData()->success_message;
  363. if(success_message.find("%t") < 0xFFFFFFFF)
  364. success_message.replace(success_message.find("%t"), 2, victim->GetName());
  365. client->Message(CHANNEL_YOU_CAST, success_message.c_str());
  366. //commented out the following line as it was causing a duplicate message EmemJR 5/4/2019
  367. //GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  368. }
  369. }
  370. if(spell->GetSpellData()->effect_message.length() > 0){
  371. string effect_message = spell->GetSpellData()->effect_message;
  372. if(effect_message.find("%t") < 0xFFFFFFFF)
  373. effect_message.replace(effect_message.find("%t"), 2, victim->GetName());
  374. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_message.c_str(), victim, 50);
  375. }
  376. }
  377. else {
  378. if(hit_result == DAMAGE_PACKET_RESULT_RESIST)
  379. luaspell->resisted = true;
  380. if(victim->IsNPC())
  381. ((NPC*)victim)->AddHate(this, 5);
  382. luaspell->last_spellattack_hit = false;
  383. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
  384. }
  385. if(EngagedInCombat() == false)
  386. {
  387. 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__);
  388. if(GetInfoStruct()->class1 == COMMONER ||
  389. GetInfoStruct()->class1 == FIGHTER ||
  390. GetInfoStruct()->class1 == WARRIOR ||
  391. GetInfoStruct()->class1 == GUARDIAN ||
  392. GetInfoStruct()->class1 == BERSERKER ||
  393. GetInfoStruct()->class1 == BRAWLER ||
  394. GetInfoStruct()->class1 == MONK ||
  395. GetInfoStruct()->class1 == BRUISER ||
  396. GetInfoStruct()->class1 == CRUSADER ||
  397. GetInfoStruct()->class1 == SHADOWKNIGHT ||
  398. GetInfoStruct()->class1 == PALADIN ||
  399. GetInfoStruct()->class1 == SCOUT ||
  400. GetInfoStruct()->class1 == ROGUE ||
  401. GetInfoStruct()->class1 == SWASHBUCKLER ||
  402. GetInfoStruct()->class1 == BRIGAND ||
  403. GetInfoStruct()->class1 == BARD ||
  404. GetInfoStruct()->class1 == TROUBADOR ||
  405. GetInfoStruct()->class1 == DIRGE ||
  406. GetInfoStruct()->class1 == PREDATOR ||
  407. GetInfoStruct()->class1 == RANGER ||
  408. GetInfoStruct()->class1 == ASSASSIN ||
  409. GetInfoStruct()->class1 == ANIMALIST ||
  410. GetInfoStruct()->class1 == BEASTLORD ||
  411. GetInfoStruct()->class1 == SHAPER ||
  412. GetInfoStruct()->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()->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()->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. 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){
  486. if(!target || !luaspell || !luaspell->spell)
  487. return false;
  488. if (!target->Alive())
  489. return false;
  490. if (target->GetHP() == target->GetTotalHP())
  491. return true;
  492. int32 heal_amt = 0;
  493. bool crit = false;
  494. if(high_heal < low_heal)
  495. high_heal = low_heal;
  496. if(high_heal == low_heal)
  497. heal_amt = high_heal;
  498. else
  499. heal_amt = MakeRandomInt(low_heal, high_heal);
  500. if(!no_calcs){
  501. // if spell is already active, this is a tick
  502. bool is_tick = GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell);
  503. //if is a tick and the spell has crit, force crit, else disable
  504. if(is_tick){
  505. if(luaspell->crit)
  506. crit_mod = 1;
  507. else
  508. crit_mod = 2;
  509. }
  510. if (heal_amt > 0){
  511. //int32 base_roll = heal_amt;
  512. //potency mod
  513. heal_amt *= (stats[ITEM_STAT_POTENCY] / 100 + 1);
  514. //primary stat mod, insert forula here when done
  515. //heal_amt += base_roll * (GetPrimaryStat()
  516. //Ability Modifier can only be up to half of base roll + potency and primary stat bonus
  517. heal_amt += (int32)min(info_struct.ability_modifier, (float)(heal_amt / 2));
  518. }
  519. if(!crit_mod || crit_mod == 1){
  520. if(crit_mod == 1)
  521. crit = true;
  522. else {
  523. // Crit Roll
  524. float chance = max((float)0, info_struct.crit_chance);
  525. crit = (MakeRandomFloat(0, 100) <= chance);
  526. }
  527. if(crit){
  528. //Apply total crit multiplier with crit bonus
  529. heal_amt *= (info_struct.crit_bonus / 100) + 1.3;
  530. if(luaspell->spell)
  531. luaspell->crit = true;
  532. }
  533. }
  534. }
  535. int16 type = 0;
  536. if (heal_type == "Heal") {
  537. if(crit)
  538. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  539. else
  540. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  541. //apply heal
  542. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  543. heal_amt = target->GetTotalHP() - target->GetHP();
  544. target->SetHP(target->GetHP() + heal_amt);
  545. /*
  546. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  547. target->SetHP(target->GetTotalHP());
  548. else
  549. target->SetHP(target->GetHP() + heal_amt);
  550. */
  551. }
  552. else if (heal_type == "Power"){
  553. if(crit)
  554. type = HEAL_PACKET_TYPE_CRIT_MANA;
  555. else
  556. type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  557. //give power
  558. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  559. heal_amt = target->GetTotalPower() - target->GetPower();
  560. target->SetPower(GetPower() + heal_amt);
  561. /*
  562. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  563. target->SetPower(target->GetTotalPower());
  564. else
  565. target->SetPower(GetPower() + heal_amt);
  566. */
  567. }
  568. /*else if (heal_type == "Savagery"){
  569. if(crit)
  570. type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
  571. else
  572. type = HEAL_PACKET_TYPE_SAVAGERY;
  573. }
  574. else if (heal_type == "Repair"){
  575. if(crit)
  576. type = HEAL_PACKET_TYPE_CRIT_REPAIR;
  577. else
  578. type = HEAL_PACKET_TYPE_REPAIR;
  579. }*/
  580. else{ //default to heal if type cannot be determined
  581. if(crit)
  582. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  583. else
  584. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  585. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  586. heal_amt = target->GetTotalHP() - target->GetHP();
  587. target->SetHP(target->GetHP() + heal_amt);
  588. /*
  589. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  590. target->SetHP(target->GetTotalHP());
  591. else
  592. target->SetHP(target->GetHP() + heal_amt);
  593. */
  594. }
  595. target->GetZone()->TriggerCharSheetTimer();
  596. if (heal_amt > 0)
  597. GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
  598. CheckProcs(PROC_TYPE_HEALING, target);
  599. CheckProcs(PROC_TYPE_BENEFICIAL, target);
  600. if (target->IsEntity()) {
  601. int32 hate_amt = heal_amt / 2;
  602. set<int32>::iterator itr;
  603. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  604. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  605. if (spawn && spawn->IsEntity()) {
  606. ((Entity*)spawn)->AddHate(this, hate_amt);
  607. }
  608. }
  609. }
  610. return true;
  611. }
  612. int8 Entity::DetermineHit(Spawn* victim, int8 damage_type, float ToHitBonus, bool spell){
  613. if(!victim) {
  614. return DAMAGE_PACKET_RESULT_MISS;
  615. }
  616. if(victim->GetInvulnerable()) {
  617. return DAMAGE_PACKET_RESULT_INVULNERABLE;
  618. }
  619. if(!victim->IsEntity() || (!spell && BehindTarget(victim))) {
  620. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  621. }
  622. float bonus = ToHitBonus;
  623. Skill* skill = GetSkillByWeaponType(damage_type, true);
  624. if (skill)
  625. bonus += skill->current_val / 25;
  626. if (victim->IsEntity())
  627. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  628. Entity* entity_victim = (Entity*)victim;
  629. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  630. sint16 roll_chance = 100;
  631. if(skill)
  632. roll_chance -= skill->current_val / 25;
  633. if(!spell){ // melee or range attack
  634. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  635. if(skill)
  636. roll_chance -= skill->current_val / 25;
  637. skill = entity_victim->GetSkillByName("Defense", true);
  638. if (skill)
  639. chance -= skill->current_val / 25;
  640. if(rand()%roll_chance >= (chance - entity_victim->GetAgi()/50)){
  641. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  642. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  643. }
  644. if(rand() % roll_chance >= chance)
  645. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  646. skill = entity_victim->GetSkillByName("Parry", true);
  647. if(skill){
  648. if(rand()%roll_chance >= (chance - 5 - skill->current_val/25)){ //successful parry
  649. if(rand()%100 <= 20) {
  650. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  651. return DAMAGE_PACKET_RESULT_RIPOSTE;
  652. }
  653. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  654. return DAMAGE_PACKET_RESULT_PARRY;
  655. }
  656. }
  657. skill = entity_victim->GetSkillByName("Deflection", true);
  658. if(skill){
  659. if(rand()%100 >= (chance - skill->current_val/25)) { //successfully deflected
  660. return DAMAGE_PACKET_RESULT_DEFLECT;
  661. }
  662. }
  663. }
  664. else{
  665. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  666. if(skill)
  667. chance -= skill->current_val / 25;
  668. if(rand()%roll_chance >= chance) {
  669. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  670. }
  671. }
  672. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  673. }
  674. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  675. float ret = 1;
  676. switch(damage_type) {
  677. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  678. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  679. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  680. Skill* skill = GetSkillByName("Defense", true);
  681. if(skill)
  682. ret += skill->current_val / 25;
  683. if(IsNPC())
  684. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  685. break;
  686. }
  687. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  688. ret += GetInfoStruct()->heat / 50;
  689. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  690. break;
  691. }
  692. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  693. ret += GetInfoStruct()->cold / 50;
  694. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  695. break;
  696. }
  697. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  698. ret += GetInfoStruct()->magic / 50;
  699. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  700. break;
  701. }
  702. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  703. ret += GetInfoStruct()->mental / 50;
  704. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  705. break;
  706. }
  707. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  708. ret += GetInfoStruct()->divine / 50;
  709. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  710. break;
  711. }
  712. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  713. ret += GetInfoStruct()->disease / 50;
  714. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  715. break;
  716. }
  717. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  718. ret += GetInfoStruct()->poison / 50;
  719. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  720. break;
  721. }
  722. }
  723. return ret;
  724. }
  725. Skill* Entity::GetSkillByWeaponType(int8 type, bool update) {
  726. switch(type) {
  727. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  728. return GetSkillByName("Slashing", update);
  729. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  730. return GetSkillByName("Crushing", update);
  731. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  732. return GetSkillByName("Piercing", update);
  733. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  734. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  735. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  736. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  737. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  738. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  739. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  740. return GetSkillByName("Disruption", update);
  741. }
  742. return 0;
  743. }
  744. 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) {
  745. if(!victim || victim->GetHP() == 0)
  746. return false;
  747. int8 hit_result = 0;
  748. int16 blow_type = 0;
  749. sint32 damage = 0;
  750. bool crit = false;
  751. if(low_damage > high_damage)
  752. high_damage = low_damage;
  753. if(low_damage == high_damage)
  754. damage = low_damage;
  755. else
  756. damage = MakeRandomInt(low_damage, high_damage);
  757. if(!no_calcs) {
  758. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  759. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  760. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  761. //DPS mod is only applied to auto attacks
  762. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE ) {
  763. damage *= (info_struct.dps_multiplier);
  764. }
  765. //Potency and ability mod is only applied to spells/CAs
  766. else {
  767. // Potency mod
  768. damage *= ((stats[ITEM_STAT_POTENCY] / 100) + 1);
  769. // Ability mod can only give up to half of damage after potency
  770. int32 mod = (int32)min(info_struct.ability_modifier, (float)(damage / 2));
  771. damage += mod;
  772. }
  773. if(!crit_mod || crit_mod == 1){
  774. //force crit if crit_mod == 1
  775. if(crit_mod == 1)
  776. crit = true;
  777. // Crit Roll
  778. else {
  779. float chance = max((float)0, (info_struct.crit_chance - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  780. if (MakeRandomFloat(0, 100) <= chance)
  781. crit = true;
  782. }
  783. if(crit){
  784. //Apply total crit multiplier with crit bonus
  785. if(info_struct.crit_bonus > 0)
  786. damage *= (1.3 + (info_struct.crit_bonus / 100));
  787. else
  788. damage *= 1.3;
  789. // Change packet type to crit
  790. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  791. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  792. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  793. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  794. }
  795. }
  796. // TODO: Mitigation equation from http://www.guildportal.com/Guild.aspx?GuildID=20881&TabID=189653&ForumID=95908&TopicID=9024250
  797. }
  798. LogWrite(MISC__TODO, 3, "TODO", "Take players armor into account\nfile: %s, func: %s, line: %i)", __FILE__, __FUNCTION__, __LINE__);
  799. bool useWards = false;
  800. if(damage <= 0){
  801. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  802. damage = 0;
  803. }
  804. else{
  805. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  806. GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this);
  807. int32 prevDmg = damage;
  808. damage = victim->CheckWards(this, damage, damage_type);
  809. if (damage < prevDmg)
  810. useWards = true;
  811. victim->TakeDamage(damage);
  812. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  813. if (IsPlayer()) {
  814. switch (damage_type) {
  815. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  816. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  817. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  818. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  819. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  820. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  821. break;
  822. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  823. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  824. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  825. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  826. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  827. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  828. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  829. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  830. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  831. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  832. break;
  833. }
  834. }
  835. }
  836. if(victim->IsNPC() && victim->GetHP() > 0)
  837. ((Entity*)victim)->AddHate(this, damage);
  838. Entity* attacker = nullptr;
  839. if(!ignore_attacker)
  840. attacker = this;
  841. if (damage > 0) {
  842. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  843. if (IsStealthed() || IsInvis())
  844. CancelAllStealth();
  845. if (victim->IsEntity())
  846. ((Entity*)victim)->CheckInterruptSpell(this);
  847. }
  848. else if (useWards)
  849. {
  850. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  851. }
  852. if (victim->GetHP() <= 0)
  853. KillSpawn(victim, damage_type, blow_type);
  854. else {
  855. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  856. if (spell_name)
  857. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  858. else
  859. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  860. }
  861. return crit;
  862. }
  863. void Entity::AddHate(Entity* attacker, sint32 hate) {
  864. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  865. return;
  866. // If a players pet and protect self is off
  867. if (IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->pet_behavior & 2) == 0))
  868. return;
  869. if (IsNPC()) {
  870. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  871. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  872. // if encounter size is 0 then add the attacker to the encounter
  873. if (((NPC*)this)->Brain()->GetEncounterSize() == 0)
  874. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  875. }
  876. if (attacker->GetThreatTransfer() && hate > 0) {
  877. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  878. if (transfer_target && transfer_target->IsEntity()) {
  879. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  880. hate -= transfered_hate;
  881. this->AddHate((Entity*)transfer_target, transfered_hate);
  882. }
  883. }
  884. // If pet is adding hate add some to the pets owner as well
  885. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  886. AddHate(((NPC*)attacker)->GetOwner(), 1);
  887. // If player and player has a pet and protect master is set add hate to the pet
  888. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->pet_behavior & 1)) {
  889. // If we have a combat pet add hate to it
  890. if (((Player*)this)->GetPet())
  891. AddHate(((Player*)this)->GetPet(), 1);
  892. if (((Player*)this)->GetCharmedPet())
  893. AddHate(((Player*)this)->GetCharmedPet(), 1);
  894. }
  895. // If this spawn has a spawn group then add the attacker to the hate list of the other
  896. // group members if not already in their list
  897. if (HasSpawnGroup()) {
  898. vector<Spawn*>* group = GetSpawnGroup();
  899. vector<Spawn*>::iterator itr;
  900. for (itr = group->begin(); itr != group->end(); itr++) {
  901. if (!(*itr)->IsNPC())
  902. continue;
  903. NPC* spawn = (NPC*)(*itr);
  904. if (spawn->Brain()->GetHate(attacker) == 0)
  905. spawn->Brain()->AddHate(attacker, 1);
  906. }
  907. safe_delete(group);
  908. }
  909. }
  910. bool Entity::CheckInterruptSpell(Entity* attacker) {
  911. if(!IsCasting())
  912. return false;
  913. Spell* spell = GetZone()->GetSpell(this);
  914. if(!spell || spell->GetSpellData()->interruptable == 0)
  915. return false;
  916. //originally base of 30 percent chance to continue casting if attacked
  917. //modified to 50% and added global rule, 30% was too small at starting levels
  918. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  919. Skill* skill = GetSkillByName("Focus", true);
  920. if(skill)
  921. percent += ((skill->current_val + 1)/6);
  922. if(MakeRandomInt(1, 100) > percent) {
  923. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  924. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  925. return true;
  926. }
  927. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  928. return false;
  929. }
  930. void Entity::KillSpawn(Spawn* dead, int8 damage_type, int16 kill_blow_type) {
  931. if(!dead)
  932. return;
  933. if (IsPlayer()) {
  934. Client* client = GetZone()->GetClientBySpawn(this);
  935. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  936. if (packet) {
  937. client->QueuePacket(packet->serialize());
  938. }
  939. safe_delete(packet);
  940. ((Player*)this)->InCombat(false);
  941. }
  942. if (IsPlayer() && dead->IsEntity())
  943. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  944. //if (dead->IsEntity()) same code called in zone server
  945. //((Entity*)dead)->InCombat(false);
  946. if (dead->IsPet())
  947. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true);
  948. else if (dead->IsEntity()) {
  949. // remove all pets for this entity
  950. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetPet());
  951. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCharmedPet());
  952. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetDeityPet());
  953. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCosmeticPet());
  954. }
  955. // If not in combat and no one in the encounter list add this killer to the list
  956. if(dead->EngagedInCombat() == false && dead->IsNPC() && ((NPC*)dead)->Brain()->GetEncounterSize() == 0)
  957. ((NPC*)dead)->Brain()->AddToEncounter(this);
  958. if (IsCasting())
  959. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  960. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  961. // Kill movement for the dead npc so the corpse doesn't move
  962. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  963. dead->ClearRunningLocations();
  964. dead->CalculateRunningLocation(true);
  965. GetZone()->KillSpawn(true, dead, this, true, damage_type, kill_blow_type);
  966. }
  967. void Entity::ProcessCombat() {
  968. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  969. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  970. }
  971. void NPC::ProcessCombat() {
  972. MBrain.writelock(__FUNCTION__, __LINE__);
  973. // Check to see if it is time to call the AI again
  974. if (GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  975. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  976. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  977. m_brain->Think();
  978. // Set the time for when the brain was last called
  979. m_brain->SetLastTick(Timer::GetCurrentTime2());
  980. }
  981. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  982. }
  983. void Player::ProcessCombat() {
  984. // if not in combat OR casting a spell OR dazed OR feared return out
  985. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  986. return;
  987. //If no target delete combat_target and return out
  988. Spawn* Target = GetZone()->GetSpawnByID(target);
  989. if (!Target) {
  990. combat_target = 0;
  991. if (target > 0) {
  992. SetTarget(0);
  993. }
  994. return;
  995. }
  996. // If is not an entity return out
  997. if (!Target->IsEntity())
  998. return;
  999. // Reset combat target
  1000. combat_target = 0;
  1001. if (Target->HasTarget()) {
  1002. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1003. Spawn* secondary_target = Target->GetTarget();
  1004. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1005. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1006. combat_target = secondary_target;
  1007. }
  1008. }
  1009. }
  1010. }
  1011. // If combat_target wasn't set in the above if set it to the original target
  1012. if (!combat_target)
  1013. combat_target = Target;
  1014. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1015. if(!combat_target)
  1016. return;
  1017. float distance = 0;
  1018. distance = GetDistance(combat_target);
  1019. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1020. if (GetRangeAttack()) {
  1021. // We are doing ranged auto attacks
  1022. //check to see if we can attack the target AND the ranged weapon is ready
  1023. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1024. Item* weapon = 0;
  1025. Item* ammo = 0;
  1026. // Get the currently equiped weapon and ammo for the ranged attack
  1027. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1028. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1029. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1030. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1031. if(weapon && ammo) {
  1032. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1033. RangeAttack(combat_target, distance, weapon, ammo);
  1034. }
  1035. else {
  1036. Client* client = GetZone()->GetClientBySpawn(this);
  1037. if (client) {
  1038. // 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
  1039. if (!ammo)
  1040. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1041. if (!weapon)
  1042. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1043. }
  1044. }
  1045. }
  1046. }
  1047. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1048. // We are doing melee auto attacks and are within range
  1049. // Check to see if we can attack the target
  1050. if(AttackAllowed((Entity*)combat_target)) {
  1051. // Check to see if the primary melee weapon is ready
  1052. if(PrimaryWeaponReady()) {
  1053. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1054. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1055. MeleeAttack(combat_target, distance, true);
  1056. }
  1057. // Check to see if the secondary weapon is ready
  1058. if(SecondaryWeaponReady()) {
  1059. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1060. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1061. MeleeAttack(combat_target, distance, false);
  1062. }
  1063. }
  1064. }
  1065. }
  1066. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1067. float mod = CalculateAttackSpeedMod();
  1068. bool dual_wield = IsDualWield();
  1069. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1070. //Add 33% longer delay if dual wielding
  1071. if(dual_wield && ! ranged) {
  1072. if(primary)
  1073. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1074. else
  1075. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1076. }
  1077. else {
  1078. if(primary)
  1079. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1080. else if(ranged)
  1081. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1082. else
  1083. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1084. }
  1085. }
  1086. float Entity::CalculateAttackSpeedMod(){
  1087. float aspeed = info_struct.attackspeed;
  1088. if(aspeed > 0) {
  1089. if (aspeed <= 100)
  1090. return (aspeed / 100 + 1);
  1091. else if (aspeed <= 200)
  1092. return 2.25;
  1093. }
  1094. return 1;
  1095. }
  1096. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1097. if (type == 0) {
  1098. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1099. return;
  1100. }
  1101. if (!item && !spell) {
  1102. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1103. return;
  1104. }
  1105. MProcList.writelock(__FUNCTION__, __LINE__);
  1106. Proc* proc = new Proc();
  1107. proc->chance = chance;
  1108. proc->item = item;
  1109. proc->spell = spell;
  1110. proc->spellid = spell->spell->GetSpellID();
  1111. m_procList[type].push_back(proc);
  1112. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1113. }
  1114. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1115. if (!item && !spell) {
  1116. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1117. return;
  1118. }
  1119. MProcList.writelock(__FUNCTION__, __LINE__);
  1120. map<int8, vector<Proc*> >::iterator proc_itr;
  1121. vector<Proc*>::iterator itr;
  1122. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1123. itr = proc_itr->second.begin();
  1124. while (itr != proc_itr->second.end()) {
  1125. Proc* proc = *itr;
  1126. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1127. itr = proc_itr->second.erase(itr);
  1128. safe_delete(proc);
  1129. }
  1130. else
  1131. itr++;
  1132. }
  1133. }
  1134. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1135. }
  1136. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1137. lua_State* state = 0;
  1138. bool item_proc = false;
  1139. int8 num_args = 3;
  1140. if (proc->spell) {
  1141. state = proc->spell->state;
  1142. }
  1143. else if (proc->item) {
  1144. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1145. item_proc = true;
  1146. }
  1147. if (!state) {
  1148. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1149. return false;
  1150. }
  1151. lua_getglobal(state, "proc");
  1152. if (item_proc) {
  1153. num_args++;
  1154. lua_interface->SetItemValue(state, proc->item);
  1155. }
  1156. lua_interface->SetSpawnValue(state, this);
  1157. lua_interface->SetSpawnValue(state, target);
  1158. lua_interface->SetInt32Value(state, type);
  1159. /*
  1160. Add spell data from db in case of a spell proc here...
  1161. */
  1162. if (!item_proc) {
  1163. // Append spell data to the param list
  1164. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1165. for(int32 i = 0; i < data->size(); i++) {
  1166. switch(data->at(i)->type) {
  1167. case 0:{
  1168. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1169. break;
  1170. }
  1171. case 1:{
  1172. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1173. break;
  1174. }
  1175. case 2:{
  1176. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1177. break;
  1178. }
  1179. case 3:{
  1180. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1181. break;
  1182. }
  1183. default:{
  1184. 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());
  1185. return false;
  1186. }
  1187. }
  1188. num_args++;
  1189. }
  1190. }
  1191. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1192. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1193. lua_pop(state, 1);
  1194. return false;
  1195. }
  1196. return true;
  1197. }
  1198. void Entity::CheckProcs(int8 type, Spawn* target) {
  1199. if (type == 0) {
  1200. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1201. return;
  1202. }
  1203. float roll = MakeRandomFloat(0, 100);
  1204. vector<Proc*> tmpList;
  1205. MProcList.readlock(__FUNCTION__, __LINE__);
  1206. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1207. Proc* proc = m_procList[type].at(i);
  1208. if (roll <= proc->chance)
  1209. {
  1210. Proc* tmpProc = new Proc();
  1211. tmpProc->chance = proc->chance;
  1212. tmpProc->item = proc->item;
  1213. tmpProc->spell = proc->spell;
  1214. tmpProc->spellid = proc->spellid;
  1215. tmpList.push_back(tmpProc);
  1216. }
  1217. }
  1218. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1219. vector<Proc*>::iterator proc_itr;
  1220. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1221. Proc* tmpProc = *proc_itr;
  1222. CastProc(tmpProc, type, target);
  1223. proc_itr++;
  1224. safe_delete(tmpProc);
  1225. }
  1226. }
  1227. void Entity::ClearProcs() {
  1228. MProcList.writelock(__FUNCTION__, __LINE__);
  1229. map<int8, vector<Proc*> >::iterator proc_itr;
  1230. vector<Proc*>::iterator itr;
  1231. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1232. itr = proc_itr->second.begin();
  1233. while (itr != proc_itr->second.end()) {
  1234. safe_delete(*itr);
  1235. itr = proc_itr->second.erase(itr);
  1236. }
  1237. proc_itr->second.clear();
  1238. }
  1239. m_procList.clear();
  1240. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1241. }