9
3

Combat.cpp 44 KB

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