Combat.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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.get_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()->get_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()->get_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.get_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. int8 class1_ = GetInfoStruct()->get_class1();
  389. if(class1_ == COMMONER ||
  390. class1_ == FIGHTER ||
  391. class1_ == WARRIOR ||
  392. class1_ == GUARDIAN ||
  393. class1_ == BERSERKER ||
  394. class1_ == BRAWLER ||
  395. class1_ == MONK ||
  396. class1_ == BRUISER ||
  397. class1_ == CRUSADER ||
  398. class1_ == SHADOWKNIGHT ||
  399. class1_ == PALADIN ||
  400. class1_ == SCOUT ||
  401. class1_ == ROGUE ||
  402. class1_ == SWASHBUCKLER ||
  403. class1_ == BRIGAND ||
  404. class1_ == BARD ||
  405. class1_ == TROUBADOR ||
  406. class1_ == DIRGE ||
  407. class1_ == PREDATOR ||
  408. class1_ == RANGER ||
  409. class1_ == ASSASSIN ||
  410. class1_ == ANIMALIST ||
  411. class1_ == BEASTLORD ||
  412. class1_ == SHAPER ||
  413. class1_ == CHANNELER) //note: it would probably be better to add a column to the spells table for "starts autoattack".
  414. {
  415. if (victim->IsNPC())
  416. ((NPC*)victim)->AddHate(this, 5);
  417. else
  418. InCombat(true);
  419. }
  420. }
  421. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  422. Entity* pet = 0;
  423. bool AddHate = false;
  424. if (victim->IsPlayer()) {
  425. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  426. AddHate = true;
  427. }
  428. else
  429. AddHate = true;
  430. if (AddHate) {
  431. pet = ((Entity*)victim)->GetPet();
  432. if (pet)
  433. pet->AddHate(this, 1);
  434. pet = ((Entity*)victim)->GetCharmedPet();
  435. if (pet)
  436. pet->AddHate(this, 1);
  437. }
  438. }
  439. return true;
  440. }
  441. bool Entity::ProcAttack(Spawn* victim, int8 damage_type, int32 low_damage, int32 high_damage, string name, string success_msg, string effect_msg) {
  442. int8 hit_result = DetermineHit(victim, damage_type, 0, true);
  443. if (hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
  444. DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, name.c_str());
  445. if (success_msg.length() > 0) {
  446. Client* client = 0;
  447. if(IsPlayer())
  448. client = GetZone()->GetClientBySpawn(this);
  449. if(client) {
  450. if(success_msg.find("%t") < 0xFFFFFFFF)
  451. success_msg.replace(success_msg.find("%t"), 2, victim->GetName());
  452. client->Message(CHANNEL_YOU_CAST, success_msg.c_str());
  453. }
  454. }
  455. if (effect_msg.length() > 0) {
  456. if(effect_msg.find("%t") < 0xFFFFFFFF)
  457. effect_msg.replace(effect_msg.find("%t"), 2, victim->GetName());
  458. GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_msg.c_str(), victim, 50);
  459. }
  460. }
  461. else {
  462. if(victim->IsNPC())
  463. ((NPC*)victim)->AddHate(this, 5);
  464. GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, name.c_str());
  465. }
  466. if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
  467. Entity* pet = 0;
  468. bool AddHate = false;
  469. if (victim->IsPlayer()) {
  470. if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
  471. AddHate = true;
  472. }
  473. else
  474. AddHate = true;
  475. if (AddHate) {
  476. pet = ((Entity*)victim)->GetPet();
  477. if (pet)
  478. pet->AddHate(this, 1);
  479. pet = ((Entity*)victim)->GetCharmedPet();
  480. if (pet)
  481. pet->AddHate(this, 1);
  482. }
  483. }
  484. return true;
  485. }
  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. //int32 base_roll = heal_amt;
  513. //potency mod
  514. heal_amt *= (stats[ITEM_STAT_POTENCY] / 100 + 1);
  515. //primary stat mod, insert forula here when done
  516. //heal_amt += base_roll * (GetPrimaryStat()
  517. //Ability Modifier can only be up to half of base roll + potency and primary stat bonus
  518. heal_amt += (int32)min(info_struct.get_ability_modifier(), (float)(heal_amt / 2));
  519. }
  520. if(!crit_mod || crit_mod == 1){
  521. if(crit_mod == 1)
  522. crit = true;
  523. else {
  524. // Crit Roll
  525. float chance = max((float)0, info_struct.get_crit_chance());
  526. crit = (MakeRandomFloat(0, 100) <= chance);
  527. }
  528. if(crit){
  529. //Apply total crit multiplier with crit bonus
  530. heal_amt *= (info_struct.get_crit_bonus() / 100) + 1.3;
  531. if(luaspell->spell)
  532. luaspell->crit = true;
  533. }
  534. }
  535. }
  536. int16 type = 0;
  537. if (heal_type == "Heal") {
  538. if(crit)
  539. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  540. else
  541. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  542. //apply heal
  543. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  544. heal_amt = target->GetTotalHP() - target->GetHP();
  545. target->SetHP(target->GetHP() + heal_amt);
  546. /*
  547. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  548. target->SetHP(target->GetTotalHP());
  549. else
  550. target->SetHP(target->GetHP() + heal_amt);
  551. */
  552. }
  553. else if (heal_type == "Power"){
  554. if(crit)
  555. type = HEAL_PACKET_TYPE_CRIT_MANA;
  556. else
  557. type = HEAL_PACKET_TYPE_SIMPLE_MANA;
  558. //give power
  559. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  560. heal_amt = target->GetTotalPower() - target->GetPower();
  561. target->SetPower(GetPower() + heal_amt);
  562. /*
  563. if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
  564. target->SetPower(target->GetTotalPower());
  565. else
  566. target->SetPower(GetPower() + heal_amt);
  567. */
  568. }
  569. /*else if (heal_type == "Savagery"){
  570. if(crit)
  571. type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
  572. else
  573. type = HEAL_PACKET_TYPE_SAVAGERY;
  574. }
  575. else if (heal_type == "Repair"){
  576. if(crit)
  577. type = HEAL_PACKET_TYPE_CRIT_REPAIR;
  578. else
  579. type = HEAL_PACKET_TYPE_REPAIR;
  580. }*/
  581. else{ //default to heal if type cannot be determined
  582. if(crit)
  583. type = HEAL_PACKET_TYPE_CRIT_HEAL;
  584. else
  585. type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
  586. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  587. heal_amt = target->GetTotalHP() - target->GetHP();
  588. target->SetHP(target->GetHP() + heal_amt);
  589. /*
  590. if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
  591. target->SetHP(target->GetTotalHP());
  592. else
  593. target->SetHP(target->GetHP() + heal_amt);
  594. */
  595. }
  596. target->GetZone()->TriggerCharSheetTimer();
  597. if (heal_amt > 0)
  598. GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
  599. CheckProcs(PROC_TYPE_HEALING, target);
  600. CheckProcs(PROC_TYPE_BENEFICIAL, target);
  601. if (target->IsEntity()) {
  602. int32 hate_amt = heal_amt / 2;
  603. set<int32>::iterator itr;
  604. for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
  605. Spawn* spawn = GetZone()->GetSpawnByID(*itr);
  606. if (spawn && spawn->IsEntity()) {
  607. ((Entity*)spawn)->AddHate(this, hate_amt);
  608. }
  609. }
  610. }
  611. return true;
  612. }
  613. int8 Entity::DetermineHit(Spawn* victim, int8 damage_type, float ToHitBonus, bool spell){
  614. if(!victim) {
  615. return DAMAGE_PACKET_RESULT_MISS;
  616. }
  617. if(victim->GetInvulnerable()) {
  618. return DAMAGE_PACKET_RESULT_INVULNERABLE;
  619. }
  620. if(!victim->IsEntity() || (!spell && BehindTarget(victim))) {
  621. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  622. }
  623. float bonus = ToHitBonus;
  624. Skill* skill = GetSkillByWeaponType(damage_type, true);
  625. if (skill)
  626. bonus += skill->current_val / 25;
  627. if (victim->IsEntity())
  628. bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
  629. Entity* entity_victim = (Entity*)victim;
  630. float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
  631. sint16 roll_chance = 100;
  632. if(skill)
  633. roll_chance -= skill->current_val / 25;
  634. if(!spell){ // melee or range attack
  635. skill = GetSkillByName("Offense", true); //add this skill for NPCs
  636. if(skill)
  637. roll_chance -= skill->current_val / 25;
  638. skill = entity_victim->GetSkillByName("Defense", true);
  639. if (skill)
  640. chance -= skill->current_val / 25;
  641. if(rand()%roll_chance >= (chance - entity_victim->GetAgi()/50)){
  642. entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
  643. return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
  644. }
  645. if(rand() % roll_chance >= chance)
  646. return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
  647. skill = entity_victim->GetSkillByName("Parry", true);
  648. if(skill){
  649. if(rand()%roll_chance >= (chance - 5 - skill->current_val/25)){ //successful parry
  650. if(rand()%100 <= 20) {
  651. entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
  652. return DAMAGE_PACKET_RESULT_RIPOSTE;
  653. }
  654. entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
  655. return DAMAGE_PACKET_RESULT_PARRY;
  656. }
  657. }
  658. skill = entity_victim->GetSkillByName("Deflection", true);
  659. if(skill){
  660. if(rand()%100 >= (chance - skill->current_val/25)) { //successfully deflected
  661. return DAMAGE_PACKET_RESULT_DEFLECT;
  662. }
  663. }
  664. }
  665. else{
  666. skill = entity_victim->GetSkillByName("Spell Avoidance", true);
  667. if(skill)
  668. chance -= skill->current_val / 25;
  669. if(rand()%roll_chance >= chance) {
  670. return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
  671. }
  672. }
  673. return DAMAGE_PACKET_RESULT_SUCCESSFUL;
  674. }
  675. float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
  676. float ret = 1;
  677. switch(damage_type) {
  678. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  679. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  680. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
  681. Skill* skill = GetSkillByName("Defense", true);
  682. if(skill)
  683. ret += skill->current_val / 25;
  684. if(IsNPC())
  685. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
  686. break;
  687. }
  688. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
  689. ret += GetInfoStruct()->get_heat() / 50;
  690. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
  691. break;
  692. }
  693. case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
  694. ret += GetInfoStruct()->get_cold() / 50;
  695. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
  696. break;
  697. }
  698. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
  699. ret += GetInfoStruct()->get_magic() / 50;
  700. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
  701. break;
  702. }
  703. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
  704. ret += GetInfoStruct()->get_mental() / 50;
  705. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
  706. break;
  707. }
  708. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
  709. ret += GetInfoStruct()->get_divine() / 50;
  710. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
  711. break;
  712. }
  713. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
  714. ret += GetInfoStruct()->get_disease() / 50;
  715. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
  716. break;
  717. }
  718. case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
  719. ret += GetInfoStruct()->get_poison() / 50;
  720. LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
  721. break;
  722. }
  723. }
  724. return ret;
  725. }
  726. Skill* Entity::GetSkillByWeaponType(int8 type, bool update) {
  727. switch(type) {
  728. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
  729. return GetSkillByName("Slashing", update);
  730. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
  731. return GetSkillByName("Crushing", update);
  732. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
  733. return GetSkillByName("Piercing", update);
  734. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  735. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  736. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  737. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  738. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  739. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  740. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  741. return GetSkillByName("Disruption", update);
  742. }
  743. return 0;
  744. }
  745. 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) {
  746. if(!victim || victim->GetHP() == 0)
  747. return false;
  748. int8 hit_result = 0;
  749. int16 blow_type = 0;
  750. sint32 damage = 0;
  751. bool crit = false;
  752. if(low_damage > high_damage)
  753. high_damage = low_damage;
  754. if(low_damage == high_damage)
  755. damage = low_damage;
  756. else
  757. damage = MakeRandomInt(low_damage, high_damage);
  758. if(!no_calcs) {
  759. //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
  760. //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
  761. //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
  762. //DPS mod is only applied to auto attacks
  763. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE ) {
  764. damage *= (info_struct.get_dps_multiplier());
  765. }
  766. //Potency and ability mod is only applied to spells/CAs
  767. else {
  768. // Potency mod
  769. damage *= ((stats[ITEM_STAT_POTENCY] / 100) + 1);
  770. // Ability mod can only give up to half of damage after potency
  771. int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / 2));
  772. damage += mod;
  773. }
  774. if(!crit_mod || crit_mod == 1){
  775. //force crit if crit_mod == 1
  776. if(crit_mod == 1)
  777. crit = true;
  778. // Crit Roll
  779. else {
  780. float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
  781. if (MakeRandomFloat(0, 100) <= chance)
  782. crit = true;
  783. }
  784. if(crit){
  785. //Apply total crit multiplier with crit bonus
  786. if(info_struct.get_crit_bonus() > 0)
  787. damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
  788. else
  789. damage *= 1.3;
  790. // Change packet type to crit
  791. if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
  792. type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
  793. else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
  794. type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
  795. }
  796. }
  797. // TODO: Mitigation equation from http://www.guildportal.com/Guild.aspx?GuildID=20881&TabID=189653&ForumID=95908&TopicID=9024250
  798. }
  799. LogWrite(MISC__TODO, 3, "TODO", "Take players armor into account\nfile: %s, func: %s, line: %i)", __FILE__, __FUNCTION__, __LINE__);
  800. bool useWards = false;
  801. if(damage <= 0){
  802. hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
  803. damage = 0;
  804. }
  805. else{
  806. hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
  807. GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this);
  808. int32 prevDmg = damage;
  809. damage = victim->CheckWards(this, damage, damage_type);
  810. if (damage < prevDmg)
  811. useWards = true;
  812. victim->TakeDamage(damage);
  813. victim->CheckProcs(PROC_TYPE_DAMAGED, this);
  814. if (IsPlayer()) {
  815. switch (damage_type) {
  816. case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
  817. case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
  818. case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
  819. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
  820. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
  821. victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
  822. break;
  823. case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
  824. case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
  825. case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
  826. case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
  827. case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
  828. case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
  829. case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
  830. if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
  831. ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
  832. victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
  833. break;
  834. }
  835. }
  836. }
  837. if(victim->IsNPC() && victim->GetHP() > 0)
  838. ((Entity*)victim)->AddHate(this, damage);
  839. Entity* attacker = nullptr;
  840. if(!ignore_attacker)
  841. attacker = this;
  842. if (damage > 0) {
  843. GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
  844. if (IsStealthed() || IsInvis())
  845. CancelAllStealth();
  846. if (victim->IsEntity())
  847. ((Entity*)victim)->CheckInterruptSpell(this);
  848. }
  849. else if (useWards)
  850. {
  851. GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
  852. }
  853. if (victim->GetHP() <= 0)
  854. KillSpawn(victim, damage_type, blow_type);
  855. else {
  856. victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
  857. if (spell_name)
  858. victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
  859. else
  860. victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
  861. }
  862. return crit;
  863. }
  864. void Entity::AddHate(Entity* attacker, sint32 hate) {
  865. if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
  866. return;
  867. // If a players pet and protect self is off
  868. if (IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
  869. return;
  870. if (IsNPC()) {
  871. LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
  872. ((NPC*)this)->Brain()->AddHate(attacker, hate);
  873. // if encounter size is 0 then add the attacker to the encounter
  874. if (((NPC*)this)->Brain()->GetEncounterSize() == 0)
  875. ((NPC*)this)->Brain()->AddToEncounter(attacker);
  876. }
  877. if (attacker->GetThreatTransfer() && hate > 0) {
  878. Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
  879. if (transfer_target && transfer_target->IsEntity()) {
  880. sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
  881. hate -= transfered_hate;
  882. this->AddHate((Entity*)transfer_target, transfered_hate);
  883. }
  884. }
  885. // If pet is adding hate add some to the pets owner as well
  886. if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
  887. AddHate(((NPC*)attacker)->GetOwner(), 1);
  888. // If player and player has a pet and protect master is set add hate to the pet
  889. if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
  890. // If we have a combat pet add hate to it
  891. if (((Player*)this)->GetPet())
  892. AddHate(((Player*)this)->GetPet(), 1);
  893. if (((Player*)this)->GetCharmedPet())
  894. AddHate(((Player*)this)->GetCharmedPet(), 1);
  895. }
  896. // If this spawn has a spawn group then add the attacker to the hate list of the other
  897. // group members if not already in their list
  898. if (HasSpawnGroup()) {
  899. vector<Spawn*>* group = GetSpawnGroup();
  900. vector<Spawn*>::iterator itr;
  901. for (itr = group->begin(); itr != group->end(); itr++) {
  902. if (!(*itr)->IsNPC())
  903. continue;
  904. NPC* spawn = (NPC*)(*itr);
  905. if (spawn->Brain()->GetHate(attacker) == 0)
  906. spawn->Brain()->AddHate(attacker, 1);
  907. }
  908. safe_delete(group);
  909. }
  910. }
  911. bool Entity::CheckInterruptSpell(Entity* attacker) {
  912. if(!IsCasting())
  913. return false;
  914. Spell* spell = GetZone()->GetSpell(this);
  915. if(!spell || spell->GetSpellData()->interruptable == 0)
  916. return false;
  917. //originally base of 30 percent chance to continue casting if attacked
  918. //modified to 50% and added global rule, 30% was too small at starting levels
  919. int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
  920. Skill* skill = GetSkillByName("Focus", true);
  921. if(skill)
  922. percent += ((skill->current_val + 1)/6);
  923. if(MakeRandomInt(1, 100) > percent) {
  924. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  925. GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
  926. return true;
  927. }
  928. LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
  929. return false;
  930. }
  931. void Entity::KillSpawn(Spawn* dead, int8 damage_type, int16 kill_blow_type) {
  932. if(!dead)
  933. return;
  934. if (IsPlayer()) {
  935. Client* client = GetZone()->GetClientBySpawn(this);
  936. PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
  937. if (packet) {
  938. client->QueuePacket(packet->serialize());
  939. }
  940. safe_delete(packet);
  941. ((Player*)this)->InCombat(false);
  942. }
  943. if (IsPlayer() && dead->IsEntity())
  944. GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
  945. //if (dead->IsEntity()) same code called in zone server
  946. //((Entity*)dead)->InCombat(false);
  947. /* just for sake of not knowing if we are in a read lock, write lock, or no lock
  948. ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
  949. */
  950. if (dead->IsPet())
  951. ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
  952. else if (dead->IsEntity()) {
  953. // remove all pets for this entity
  954. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetPet(), false, true);
  955. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCharmedPet(), false, true);
  956. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetDeityPet(), false, true);
  957. ((Entity*)dead)->DismissPet((NPC*)((Entity*)dead)->GetCosmeticPet(), false, true);
  958. }
  959. // If not in combat and no one in the encounter list add this killer to the list
  960. if(dead->EngagedInCombat() == false && dead->IsNPC() && ((NPC*)dead)->Brain()->GetEncounterSize() == 0)
  961. ((NPC*)dead)->Brain()->AddToEncounter(this);
  962. if (IsCasting())
  963. GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
  964. LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
  965. // Kill movement for the dead npc so the corpse doesn't move
  966. GetZone()->movementMgr->StopNavigation((Entity*)dead);
  967. dead->ClearRunningLocations();
  968. dead->CalculateRunningLocation(true);
  969. GetZone()->KillSpawn(true, dead, this, true, damage_type, kill_blow_type);
  970. }
  971. void Entity::ProcessCombat() {
  972. // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
  973. // and a player will use the Player::ProcessCombat() version, leave this function blank.
  974. }
  975. void NPC::ProcessCombat() {
  976. MBrain.writelock(__FUNCTION__, __LINE__);
  977. // Check to see if it is time to call the AI again
  978. if (GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
  979. // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
  980. //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
  981. m_brain->Think();
  982. // Set the time for when the brain was last called
  983. m_brain->SetLastTick(Timer::GetCurrentTime2());
  984. }
  985. MBrain.releasewritelock(__FUNCTION__, __LINE__);
  986. }
  987. void Player::ProcessCombat() {
  988. // if not in combat OR casting a spell OR dazed OR feared return out
  989. if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
  990. return;
  991. //If no target delete combat_target and return out
  992. Spawn* Target = GetZone()->GetSpawnByID(target);
  993. if (!Target) {
  994. combat_target = 0;
  995. if (target > 0) {
  996. SetTarget(0);
  997. }
  998. return;
  999. }
  1000. // If is not an entity return out
  1001. if (!Target->IsEntity())
  1002. return;
  1003. // Reset combat target
  1004. combat_target = 0;
  1005. if (Target->HasTarget()) {
  1006. if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner()->IsPlayer())){
  1007. Spawn* secondary_target = Target->GetTarget();
  1008. if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
  1009. if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
  1010. combat_target = secondary_target;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. // If combat_target wasn't set in the above if set it to the original target
  1016. if (!combat_target)
  1017. combat_target = Target;
  1018. // this if may not be required as at the min combat_target will be Target, which we already check at the begining
  1019. if(!combat_target)
  1020. return;
  1021. float distance = 0;
  1022. distance = GetDistance(combat_target);
  1023. // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
  1024. if (GetRangeAttack()) {
  1025. // We are doing ranged auto attacks
  1026. //check to see if we can attack the target AND the ranged weapon is ready
  1027. if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
  1028. Item* weapon = 0;
  1029. Item* ammo = 0;
  1030. // Get the currently equiped weapon and ammo for the ranged attack
  1031. weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
  1032. ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
  1033. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
  1034. // If weapon and ammo are both valid perform the ranged attack else send a message to the client
  1035. if(weapon && ammo) {
  1036. LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
  1037. RangeAttack(combat_target, distance, weapon, ammo);
  1038. }
  1039. else {
  1040. Client* client = GetZone()->GetClientBySpawn(this);
  1041. if (client) {
  1042. // 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
  1043. if (!ammo)
  1044. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
  1045. if (!weapon)
  1046. client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
  1047. }
  1048. }
  1049. }
  1050. }
  1051. else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
  1052. // We are doing melee auto attacks and are within range
  1053. // Check to see if we can attack the target
  1054. if(AttackAllowed((Entity*)combat_target)) {
  1055. // Check to see if the primary melee weapon is ready
  1056. if(PrimaryWeaponReady()) {
  1057. // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
  1058. SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
  1059. MeleeAttack(combat_target, distance, true);
  1060. }
  1061. // Check to see if the secondary weapon is ready
  1062. if(SecondaryWeaponReady()) {
  1063. // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
  1064. SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
  1065. MeleeAttack(combat_target, distance, false);
  1066. }
  1067. }
  1068. }
  1069. }
  1070. void Entity::SetAttackDelay(bool primary, bool ranged) {
  1071. float mod = CalculateAttackSpeedMod();
  1072. bool dual_wield = IsDualWield();
  1073. //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
  1074. //Add 33% longer delay if dual wielding
  1075. if(dual_wield && ! ranged) {
  1076. if(primary)
  1077. SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
  1078. else
  1079. SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
  1080. }
  1081. else {
  1082. if(primary)
  1083. SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
  1084. else if(ranged)
  1085. SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
  1086. else
  1087. SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
  1088. }
  1089. }
  1090. float Entity::CalculateAttackSpeedMod(){
  1091. float aspeed = info_struct.get_attackspeed();
  1092. if(aspeed > 0) {
  1093. if (aspeed <= 100)
  1094. return (aspeed / 100 + 1);
  1095. else if (aspeed <= 200)
  1096. return 2.25;
  1097. }
  1098. return 1;
  1099. }
  1100. void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
  1101. if (type == 0) {
  1102. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
  1103. return;
  1104. }
  1105. if (!item && !spell) {
  1106. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
  1107. return;
  1108. }
  1109. MProcList.writelock(__FUNCTION__, __LINE__);
  1110. Proc* proc = new Proc();
  1111. proc->chance = chance;
  1112. proc->item = item;
  1113. proc->spell = spell;
  1114. proc->spellid = spell->spell->GetSpellID();
  1115. m_procList[type].push_back(proc);
  1116. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1117. }
  1118. void Entity::RemoveProc(Item* item, LuaSpell* spell) {
  1119. if (!item && !spell) {
  1120. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
  1121. return;
  1122. }
  1123. MProcList.writelock(__FUNCTION__, __LINE__);
  1124. map<int8, vector<Proc*> >::iterator proc_itr;
  1125. vector<Proc*>::iterator itr;
  1126. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1127. itr = proc_itr->second.begin();
  1128. while (itr != proc_itr->second.end()) {
  1129. Proc* proc = *itr;
  1130. if ((item && proc->item == item) || (spell && proc->spell == spell)) {
  1131. itr = proc_itr->second.erase(itr);
  1132. safe_delete(proc);
  1133. }
  1134. else
  1135. itr++;
  1136. }
  1137. }
  1138. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1139. }
  1140. bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
  1141. lua_State* state = 0;
  1142. bool item_proc = false;
  1143. int8 num_args = 3;
  1144. if (proc->spell) {
  1145. state = proc->spell->state;
  1146. }
  1147. else if (proc->item) {
  1148. state = lua_interface->GetItemScript(proc->item->GetItemScript());
  1149. item_proc = true;
  1150. }
  1151. if (!state) {
  1152. LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
  1153. return false;
  1154. }
  1155. lua_getglobal(state, "proc");
  1156. if (item_proc) {
  1157. num_args++;
  1158. lua_interface->SetItemValue(state, proc->item);
  1159. }
  1160. lua_interface->SetSpawnValue(state, this);
  1161. lua_interface->SetSpawnValue(state, target);
  1162. lua_interface->SetInt32Value(state, type);
  1163. /*
  1164. Add spell data from db in case of a spell proc here...
  1165. */
  1166. if (!item_proc) {
  1167. // Append spell data to the param list
  1168. vector<LUAData*>* data = proc->spell->spell->GetLUAData();
  1169. for(int32 i = 0; i < data->size(); i++) {
  1170. switch(data->at(i)->type) {
  1171. case 0:{
  1172. lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
  1173. break;
  1174. }
  1175. case 1:{
  1176. lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
  1177. break;
  1178. }
  1179. case 2:{
  1180. lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
  1181. break;
  1182. }
  1183. case 3:{
  1184. lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
  1185. break;
  1186. }
  1187. default:{
  1188. 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());
  1189. return false;
  1190. }
  1191. }
  1192. num_args++;
  1193. }
  1194. }
  1195. if (lua_pcall(state, num_args, 0, 0) != 0) {
  1196. LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
  1197. lua_pop(state, 1);
  1198. return false;
  1199. }
  1200. return true;
  1201. }
  1202. void Entity::CheckProcs(int8 type, Spawn* target) {
  1203. if (type == 0) {
  1204. LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
  1205. return;
  1206. }
  1207. float roll = MakeRandomFloat(0, 100);
  1208. vector<Proc*> tmpList;
  1209. MProcList.readlock(__FUNCTION__, __LINE__);
  1210. for (int8 i = 0; i < m_procList[type].size(); i++) {
  1211. Proc* proc = m_procList[type].at(i);
  1212. if (roll <= proc->chance)
  1213. {
  1214. Proc* tmpProc = new Proc();
  1215. tmpProc->chance = proc->chance;
  1216. tmpProc->item = proc->item;
  1217. tmpProc->spell = proc->spell;
  1218. tmpProc->spellid = proc->spellid;
  1219. tmpList.push_back(tmpProc);
  1220. }
  1221. }
  1222. MProcList.releasereadlock(__FUNCTION__, __LINE__);
  1223. vector<Proc*>::iterator proc_itr;
  1224. for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
  1225. Proc* tmpProc = *proc_itr;
  1226. CastProc(tmpProc, type, target);
  1227. proc_itr++;
  1228. safe_delete(tmpProc);
  1229. }
  1230. }
  1231. void Entity::ClearProcs() {
  1232. MProcList.writelock(__FUNCTION__, __LINE__);
  1233. map<int8, vector<Proc*> >::iterator proc_itr;
  1234. vector<Proc*>::iterator itr;
  1235. for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
  1236. itr = proc_itr->second.begin();
  1237. while (itr != proc_itr->second.end()) {
  1238. safe_delete(*itr);
  1239. itr = proc_itr->second.erase(itr);
  1240. }
  1241. proc_itr->second.clear();
  1242. }
  1243. m_procList.clear();
  1244. MProcList.releasewritelock(__FUNCTION__, __LINE__);
  1245. }