123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575 |
- /*
- EQ2Emulator: Everquest II Server Emulator
- Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
- This file is part of EQ2Emulator.
- EQ2Emulator is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- EQ2Emulator is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "Combat.h"
- #include "client.h"
- #include "../common/ConfigReader.h"
- #include "classes.h"
- #include "../common/debug.h"
- #include "../common/Log.h"
- #include "zoneserver.h"
- #include "Skills.h"
- #include "classes.h"
- #include "World.h"
- #include "LuaInterface.h"
- #include "Rules/Rules.h"
- #include "SpellProcess.h"
- #include "World.h"
- #include <math.h>
- extern Classes classes;
- extern ConfigReader configReader;
- extern MasterSkillList master_skill_list;
- extern RuleManager rule_manager;
- extern LuaInterface* lua_interface;
- extern World world;
- /* ******************************************************************************
- DamageSpawn() - Damage equation
- MeleeAttack() - Melee auto attacks
- RangeAttack() - Range auto attacks
- DetermineHit() - ToHit chance as well as defender parry / dodge / block / riposte
- CheckInterruptSpell() - Interrupt equations
- No mitigation equations yet
- ****************************************************************************** */
- /* New Combat code */
- bool Entity::PrimaryWeaponReady() {
- //Can only be ready if no ranged timer
- if (GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) {
- if (GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
- return true;
- }
- return false;
- }
- bool Entity::SecondaryWeaponReady() {
- //Can only be ready if no ranged timer
- // if(IsDualWield() && (GetPrimaryLastAttackTime()
- if (IsDualWield() && (GetSecondaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay())))) {
- if(GetRangeLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))
- return true;
- }
- return false;
- }
- bool Entity::RangeWeaponReady() {
- //Ranged can only be ready if no other attack timers are active
- if(GetRangeLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetRangeLastAttackTime() + GetRangeAttackDelay()))) {
- if((GetPrimaryLastAttackTime() == 0 || (Timer::GetCurrentTime2() >= (GetPrimaryLastAttackTime() + GetPrimaryAttackDelay()))) && (GetSecondaryLastAttackTime() == 0 || Timer::GetCurrentTime2() >= (GetSecondaryLastAttackTime() + GetSecondaryAttackDelay()))){
- if(!IsPlayer() || ((Player*)this)->GetRangeAttack()) {
- return true;
- }
- }
- }
- return false;
- }
- bool Entity::AttackAllowed(Entity* target, float distance, bool range_attack) {
- Entity* attacker = this;
- Client* client = 0;
- if(!target || IsMezzedOrStunned() || IsDazed()) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: no target, mezzed, stunned or dazed");
- return false;
- }
- if (IsPlayer())
- client = GetZone()->GetClientBySpawn(this);
- if (IsPet())
- attacker = ((NPC*)this)->GetOwner();
- if (target->IsNPC() && ((NPC*)target)->IsPet()){
- if (((NPC*)target)->GetOwner())
- target = ((NPC*)target)->GetOwner();
- }
- if (attacker == target) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: attacker tried to attack himself or his pet.");
- return false;
- }
- if (IsPlayer() && target->GetAttackable() == 0) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is not attackable");
- return false;
- }
- if (IsPlayer() && target->IsBot()) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: players are not allowed to attack bots");
- return false;
- }
- if (attacker->IsPlayer() && target->IsPlayer())
- {
- bool pvp_allowed = rule_manager.GetGlobalRule(R_PVP, AllowPVP)->GetBool();
- if (!pvp_allowed) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp is not allowed");
- return false;
- }
- else
- {
- sint32 pvpLevelRange = rule_manager.GetGlobalRule(R_PVP, LevelRange)->GetSInt32();
- int32 attackerLevel = attacker->GetLevel();
- int32 defenderLevel = target->GetLevel();
- if ((sint32)abs((sint32)attackerLevel - (sint32)defenderLevel) > pvpLevelRange)
- {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: pvp range of %i exceeded abs(%i-%i).", pvpLevelRange, attackerLevel, defenderLevel);
- return false;
- }
- }
- }
- if (target->GetHP() <= 0) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: target is dead");
- return false;
- }
- if(range_attack && distance != 0) {
- Item* weapon = 0;
- Item* ammo = 0;
- if(attacker->IsPlayer()) {
- weapon = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
- ammo = ((Player*)attacker)->GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
- }
- if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
- // Distance is less then min weapon range
- if(distance < weapon->ranged_info->range_low) {
- if (client)
- client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too close! Move back!");
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target to close");
- return false;
- }
- // Distance is greater then max weapon range
- if (distance > (weapon->ranged_info->range_high + ammo->thrown_info->range)) {
- if (client)
- client->SimpleMessage(CHANNEL_GENERAL_COMBAT, "Your target is too far away! Move closer!");
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: range attack, target is to far");
- return false;
- }
- }
- }
- else if (distance != 0) {
- if(distance >= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
- LogWrite(COMBAT__DEBUG, 3, "AttackAllowed", "Failed to attack: distance is beyond melee range");
- return false;
- }
- }
- LogWrite(MISC__TODO, 3, "TODO", "Add more AttackAllowed calculations\n\t(%s, function: %s, line #: %i)", __FILE__, __FUNCTION__, __LINE__);
- return true;
- }
- void Entity::MeleeAttack(Spawn* victim, float distance, bool primary, bool multi_attack) {
- if(!victim)
- return;
- int8 damage_type = 0;
- int32 min_damage = 0;
- int32 max_damage = 0;
- if(primary) {
- damage_type = GetPrimaryWeaponType();
- min_damage = GetPrimaryWeaponMinDamage();
- max_damage = GetPrimaryWeaponMaxDamage();
- }
- else {
- damage_type = GetSecondaryWeaponType();
- min_damage = GetSecondaryWeaponMinDamage();
- max_damage = GetSecondaryWeaponMaxDamage();
- }
- if (IsStealthed() || IsInvis())
- CancelAllStealth();
-
- int8 hit_result = DetermineHit(victim, damage_type, 0, false);
- if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL){
- /*if(GetAdventureClass() == MONK){
- max_damage*=3;
- crit_chance = GetLevel()/4+5;
- }
- else if(GetAdventureClass() == BRUISER){
- min_damage = GetLevel();
- max_damage*=3;
- crit_chance = GetLevel()/3+5;
- }
- if(rand()%100 <=crit_chance){
- max_damage*= 2;
- DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG, damage_type, min_damage, max_damage, 0);
- }
- else*/
- DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, damage_type, min_damage, max_damage, 0);
- if (!multi_attack) {
- CheckProcs(PROC_TYPE_OFFENSIVE, victim);
- CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
- }
- }
- else{
- GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, hit_result, damage_type, 0, 0);
- if(hit_result == DAMAGE_PACKET_RESULT_RIPOSTE && victim->IsEntity())
- ((Entity*)victim)->MeleeAttack(this, distance, true);
- }
- //Multi Attack roll
- if(!multi_attack){
- float multi_attack = info_struct.get_multi_attack();
- if(multi_attack > 0){
- float chance = multi_attack;
- if (multi_attack > 100){
- int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
- chance = (multi_attack - (floor((float) ((multi_attack / 100) * 100))));
- while(automatic_multi > 0){
- MeleeAttack(victim, 100, primary, true);
- automatic_multi--;
- }
- }
- if (MakeRandomFloat(0, 100) <= chance)
- MeleeAttack(victim, 100, primary, true);
- }
- }
- //Apply attack speed mods
- if(!multi_attack)
- SetAttackDelay(primary);
- if(victim->IsNPC() && victim->EngagedInCombat() == false) {
- ((NPC*)victim)->AddHate(this, 50);
- }
- if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
- Entity* pet = 0;
- bool AddHate = false;
- if (victim->IsPlayer()) {
- if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
- AddHate = true;
- }
- else
- AddHate = true;
-
- if (AddHate) {
- pet = ((Entity*)victim)->GetPet();
- if (pet)
- pet->AddHate(this, 1);
- pet = ((Entity*)victim)->GetCharmedPet();
- if (pet)
- pet->AddHate(this, 1);
- }
- }
- }
- void Entity::RangeAttack(Spawn* victim, float distance, Item* weapon, Item* ammo, bool multi_attack) {
- if(!victim)
- return;
- if(weapon && weapon->IsRanged() && ammo && ammo->IsAmmo() && ammo->IsThrown()) {
- if(weapon->ranged_info->range_low <= distance && (weapon->ranged_info->range_high + ammo->thrown_info->range) >= distance) {
- int8 hit_result = DetermineHit(victim, ammo->thrown_info->damage_type, ammo->thrown_info->hit_bonus, false);
- if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
- 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);
- if (!multi_attack) {
- CheckProcs(PROC_TYPE_OFFENSIVE, victim);
- CheckProcs(PROC_TYPE_PHYSICAL_OFFENSIVE, victim);
- }
- }
- else
- GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_RANGE_DAMAGE, hit_result, ammo->thrown_info->damage_type, 0, 0);
- // If is a player subtract ammo
- if (IsPlayer()) {
- if (ammo->details.count > 1) {
- ammo->details.count -= 1;
- ammo->save_needed = true;
- }
- else
- ((Player*)this)->equipment_list.RemoveItem(ammo->details.slot_id, true);
- Client* client = GetZone()->GetClientBySpawn(this);
- EQ2Packet* outapp = ((Player*)this)->GetEquipmentList()->serialize(client->GetVersion(), (Player*)this);
- if(outapp)
- client->QueuePacket(outapp);
- }
- if(victim->IsNPC() && victim->EngagedInCombat() == false) {
- ((NPC*)victim)->AddHate(this, 50);
- }
- if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
- Entity* pet = 0;
- bool AddHate = false;
- if (victim->IsPlayer()) {
- if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
- AddHate = true;
- }
- else
- AddHate = true;
- if (AddHate) {
- pet = ((Entity*)victim)->GetPet();
- if (pet)
- pet->AddHate(this, 1);
- pet = ((Entity*)victim)->GetCharmedPet();
- if (pet)
- pet->AddHate(this, 1);
- }
- }
- // Check Ranged attack proc
- CheckProcs(PROC_TYPE_RANGED_ATTACK, victim);
- // Check Ranged defence proc
- if (victim->IsEntity())
- ((Entity*)victim)->CheckProcs(PROC_TYPE_RANGED_DEFENSE, this);
- SetRangeLastAttackTime(Timer::GetCurrentTime2());
- }
- }
- //Multi Attack roll
- if(!multi_attack){
- float multi_attack = info_struct.get_multi_attack();
- if(multi_attack > 0){
- float chance = multi_attack;
- if (multi_attack > 100){
- int8 automatic_multi = (int8)floor((float)(multi_attack / 100));
- chance = (multi_attack - (floor((float)(multi_attack / 100) * 100)));
- while(automatic_multi > 0){
- RangeAttack(victim, 100, weapon, ammo, true);
- automatic_multi--;
- }
- }
- if (MakeRandomFloat(0, 100) <= chance)
- RangeAttack(victim, 100, weapon, ammo, true);
- }
- }
- //Apply attack speed mods
- if(!multi_attack)
- SetAttackDelay(false, true);
- }
- bool Entity::SpellAttack(Spawn* victim, float distance, LuaSpell* luaspell, int8 damage_type, int32 low_damage, int32 high_damage, int8 crit_mod, bool no_calcs){
- if(!victim || !luaspell || !luaspell->spell)
- return false;
- Spell* spell = luaspell->spell;
- float bonus = 0;
- Skill* skill = nullptr;
- if(spell->GetSpellData()->resistibility > 0)
- bonus -= (1 - spell->GetSpellData()->resistibility)*100;
- skill = GetSkillByID(spell->GetSpellData()->mastery_skill, false);
- if(skill)
- bonus += skill->current_val / 25;
- int8 hit_result = 0;
- bool is_tick = false; // if spell is already active, this is a tick
- if (GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell)){
- hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
- is_tick = true;
- }
- else if(spell->GetSpellData()->type == SPELL_BOOK_TYPE_COMBAT_ART)
- hit_result = DetermineHit(victim, damage_type, 0, false);
- else
- hit_result = DetermineHit(victim, damage_type, 0, true);
-
- if(hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
- luaspell->last_spellattack_hit = true;
- //If this spell is a tick and has already crit, force the tick to crit
- if(is_tick){
- if(luaspell->crit)
- crit_mod = 1;
- else
- crit_mod = 2;
- }
- 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)
- luaspell->crit = true;
- CheckProcs(PROC_TYPE_OFFENSIVE, victim);
- CheckProcs(PROC_TYPE_MAGICAL_OFFENSIVE, victim);
- if(spell->GetSpellData()->success_message.length() > 0){
- Client* client = nullptr;
- if(IsPlayer())
- client = GetZone()->GetClientBySpawn(this);
- if(client){
- string success_message = spell->GetSpellData()->success_message;
- if(success_message.find("%t") < 0xFFFFFFFF)
- success_message.replace(success_message.find("%t"), 2, victim->GetName());
- client->Message(CHANNEL_YOU_CAST, success_message.c_str());
- //commented out the following line as it was causing a duplicate message EmemJR 5/4/2019
- //GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
- }
- }
- if(spell->GetSpellData()->effect_message.length() > 0){
- string effect_message = spell->GetSpellData()->effect_message;
- if(effect_message.find("%t") < 0xFFFFFFFF)
- effect_message.replace(effect_message.find("%t"), 2, victim->GetName());
- GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_message.c_str(), victim, 50);
- }
- }
- else {
- if(hit_result == DAMAGE_PACKET_RESULT_RESIST)
- luaspell->resisted = true;
- if(victim->IsNPC())
- ((NPC*)victim)->AddHate(this, 5);
- luaspell->last_spellattack_hit = false;
- GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, spell->GetName());
- }
- if(EngagedInCombat() == false)
- {
- 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__);
- int8 class1_ = GetInfoStruct()->get_class1();
- if(class1_ == COMMONER ||
- class1_ == FIGHTER ||
- class1_ == WARRIOR ||
- class1_ == GUARDIAN ||
- class1_ == BERSERKER ||
- class1_ == BRAWLER ||
- class1_ == MONK ||
- class1_ == BRUISER ||
- class1_ == CRUSADER ||
- class1_ == SHADOWKNIGHT ||
- class1_ == PALADIN ||
- class1_ == SCOUT ||
- class1_ == ROGUE ||
- class1_ == SWASHBUCKLER ||
- class1_ == BRIGAND ||
- class1_ == BARD ||
- class1_ == TROUBADOR ||
- class1_ == DIRGE ||
- class1_ == PREDATOR ||
- class1_ == RANGER ||
- class1_ == ASSASSIN ||
- class1_ == ANIMALIST ||
- class1_ == BEASTLORD ||
- class1_ == SHAPER ||
- class1_ == CHANNELER) //note: it would probably be better to add a column to the spells table for "starts autoattack".
- {
- if (victim->IsNPC())
- ((NPC*)victim)->AddHate(this, 5);
- else
- InCombat(true);
- }
- }
- if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
- Entity* pet = 0;
- bool AddHate = false;
- if (victim->IsPlayer()) {
- if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
- AddHate = true;
- }
- else
- AddHate = true;
- if (AddHate) {
- pet = ((Entity*)victim)->GetPet();
- if (pet)
- pet->AddHate(this, 1);
- pet = ((Entity*)victim)->GetCharmedPet();
- if (pet)
- pet->AddHate(this, 1);
- }
- }
- return true;
- }
- bool Entity::ProcAttack(Spawn* victim, int8 damage_type, int32 low_damage, int32 high_damage, string name, string success_msg, string effect_msg) {
- int8 hit_result = DetermineHit(victim, damage_type, 0, true);
- if (hit_result == DAMAGE_PACKET_RESULT_SUCCESSFUL) {
- DamageSpawn((Entity*)victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, damage_type, low_damage, high_damage, name.c_str());
- if (success_msg.length() > 0) {
- Client* client = 0;
- if(IsPlayer())
- client = GetZone()->GetClientBySpawn(this);
- if(client) {
- if(success_msg.find("%t") < 0xFFFFFFFF)
- success_msg.replace(success_msg.find("%t"), 2, victim->GetName());
- client->Message(CHANNEL_YOU_CAST, success_msg.c_str());
- }
- }
- if (effect_msg.length() > 0) {
- if(effect_msg.find("%t") < 0xFFFFFFFF)
- effect_msg.replace(effect_msg.find("%t"), 2, victim->GetName());
- GetZone()->SimpleMessage(CHANNEL_SPELLS, effect_msg.c_str(), victim, 50);
- }
- }
- else {
- if(victim->IsNPC())
- ((NPC*)victim)->AddHate(this, 5);
- GetZone()->SendDamagePacket(this, victim, DAMAGE_PACKET_TYPE_SPELL_DAMAGE, hit_result, damage_type, 0, name.c_str());
- }
-
- if (victim->IsEntity() && victim->GetHP() > 0 && ((Entity*)victim)->HasPet()) {
- Entity* pet = 0;
- bool AddHate = false;
- if (victim->IsPlayer()) {
- if (((Player*)victim)->GetInfoStruct()->get_pet_behavior() & 1)
- AddHate = true;
- }
- else
- AddHate = true;
- if (AddHate) {
- pet = ((Entity*)victim)->GetPet();
- if (pet)
- pet->AddHate(this, 1);
- pet = ((Entity*)victim)->GetCharmedPet();
- if (pet)
- pet->AddHate(this, 1);
- }
- }
- return true;
- }
- // this is used exclusively by LUA, heal_type is forced lower case via boost::lower(heal_type); in the LUA Functions used by this
- 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){
- if(!target || !luaspell || !luaspell->spell)
- return false;
- if (!target->Alive())
- return false;
- if (target->GetHP() == target->GetTotalHP())
- return true;
- int32 heal_amt = 0;
- bool crit = false;
- if(high_heal < low_heal)
- high_heal = low_heal;
- if(high_heal == low_heal)
- heal_amt = high_heal;
- else
- heal_amt = MakeRandomInt(low_heal, high_heal);
- if(!no_calcs){
- // if spell is already active, this is a tick
- bool is_tick = GetZone()->GetSpellProcess()->GetActiveSpells()->count(luaspell);
- //if is a tick and the spell has crit, force crit, else disable
- if(is_tick){
- if(luaspell->crit)
- crit_mod = 1;
- else
- crit_mod = 2;
- }
-
- if (heal_amt > 0){
- //int32 base_roll = heal_amt;
- //potency mod
- MStats.lock();
- heal_amt *= (stats[ITEM_STAT_POTENCY] / 100 + 1);
- MStats.unlock();
- //primary stat mod, insert forula here when done
- //heal_amt += base_roll * (GetPrimaryStat()
-
- //Ability Modifier can only be up to half of base roll + potency and primary stat bonus
- heal_amt += (int32)min(info_struct.get_ability_modifier(), (float)(heal_amt / 2));
- }
- if(!crit_mod || crit_mod == 1){
- if(crit_mod == 1)
- crit = true;
- else {
- // Crit Roll
- float chance = max((float)0, info_struct.get_crit_chance());
- crit = (MakeRandomFloat(0, 100) <= chance);
- }
- if(crit){
- //Apply total crit multiplier with crit bonus
- heal_amt *= (info_struct.get_crit_bonus() / 100) + 1.3;
- if(luaspell->spell)
- luaspell->crit = true;
- }
- }
- }
- int16 type = 0;
- if (heal_type == "heal") {
- if(crit)
- type = HEAL_PACKET_TYPE_CRIT_HEAL;
- else
- type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
- //apply heal
-
- if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
- heal_amt = target->GetTotalHP() - target->GetHP();
- target->SetHP(target->GetHP() + heal_amt);
- /*
- if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
- target->SetHP(target->GetTotalHP());
- else
- target->SetHP(target->GetHP() + heal_amt);
- */
- }
- else if (heal_type == "power"){
- if(crit)
- type = HEAL_PACKET_TYPE_CRIT_MANA;
- else
- type = HEAL_PACKET_TYPE_SIMPLE_MANA;
- //give power
- if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
- heal_amt = target->GetTotalPower() - target->GetPower();
- target->SetPower(GetPower() + heal_amt);
- /*
- if (target->GetPower() + (sint32)heal_amt > target->GetTotalPower())
- target->SetPower(target->GetTotalPower());
- else
- target->SetPower(GetPower() + heal_amt);
- */
- }
- /*else if (heal_type == "Savagery"){
- if(crit)
- type = HEAL_PACKET_TYPE_CRIT_SAVAGERY;
- else
- type = HEAL_PACKET_TYPE_SAVAGERY;
- }
- else if (heal_type == "Repair"){
- if(crit)
- type = HEAL_PACKET_TYPE_CRIT_REPAIR;
- else
- type = HEAL_PACKET_TYPE_REPAIR;
- }*/
- else{ //default to heal if type cannot be determined
- if(crit)
- type = HEAL_PACKET_TYPE_CRIT_HEAL;
- else
- type = HEAL_PACKET_TYPE_SIMPLE_HEAL;
- if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
- heal_amt = target->GetTotalHP() - target->GetHP();
- target->SetHP(target->GetHP() + heal_amt);
- /*
- if (target->GetHP() + (sint32)heal_amt > target->GetTotalHP())
- target->SetHP(target->GetTotalHP());
- else
- target->SetHP(target->GetHP() + heal_amt);
- */
- }
- target->GetZone()->TriggerCharSheetTimer();
- if (heal_amt > 0)
- GetZone()->SendHealPacket(this, target, type, heal_amt, custom_spell_name.length() > 0 ? (char*)custom_spell_name.c_str() : luaspell->spell->GetName());
- CheckProcs(PROC_TYPE_HEALING, target);
- CheckProcs(PROC_TYPE_BENEFICIAL, target);
- if (target->IsEntity()) {
- int32 hate_amt = heal_amt / 2;
- set<int32>::iterator itr;
- ((Entity*)target)->MHatedBy.lock();
- for (itr = ((Entity*)target)->HatedBy.begin(); itr != ((Entity*)target)->HatedBy.end(); itr++) {
- Spawn* spawn = GetZone()->GetSpawnByID(*itr);
- if (spawn && spawn->IsEntity() && target != this) {
- ((Entity*)spawn)->AddHate(this, hate_amt);
- }
- }
- ((Entity*)target)->MHatedBy.unlock();
- }
- return true;
- }
- int8 Entity::DetermineHit(Spawn* victim, int8 damage_type, float ToHitBonus, bool spell){
- if(!victim) {
- return DAMAGE_PACKET_RESULT_MISS;
- }
- if(victim->GetInvulnerable()) {
- return DAMAGE_PACKET_RESULT_INVULNERABLE;
- }
- bool behind = false;
- // Monk added with Brawler to 360 degree support per KoS Prima Official eGuide Fighter: Monk, pg 138, denoted '360-Degree Avoidance!'
- if(!victim->IsEntity() || (!spell && victim->GetAdventureClass() != BRAWLER && victim->GetAdventureClass() != MONK && (behind = BehindTarget(victim)))) {
- return DAMAGE_PACKET_RESULT_SUCCESSFUL;
- }
- float bonus = ToHitBonus;
- Skill* skill = GetSkillByWeaponType(damage_type, true);
- float skillAddedByWeapon = 0.0f;
- if(skill)
- {
- int16 skillID = master_item_list.GetItemStatIDByName(skill->name.data);
- if(skillID != 0xFFFFFFFF)
- {
- MStats.lock();
- skillAddedByWeapon = stats[skillID];
- MStats.unlock();
- }
- }
-
- if (skill)
- bonus += (skill->current_val+skillAddedByWeapon) / 25;
- if (victim->IsEntity())
- bonus -= ((Entity*)victim)->GetDamageTypeResistPercentage(damage_type);
- Entity* entity_victim = (Entity*)victim;
- float chance = 80 + bonus; //80% base chance that the victim will get hit (plus bonus)
- sint16 roll_chance = 100;
- if(skill)
- roll_chance -= skill->current_val / 25;
- if(!spell){ // melee or range attack
- skill = GetSkillByName("Offense", true); //add this skill for NPCs
- if(skill)
- roll_chance -= skill->current_val / 25;
- if(entity_victim->IsImmune(IMMUNITY_TYPE_RIPOSTE))
- return DAMAGE_PACKET_RESULT_RIPOSTE;
- // Avoidance Instructions: https://forums.daybreakgames.com/eq2/index.php?threads/avoidance-faq.482979/
- /*Parry: reads as parry in the avoidance tooltip, increased by items with +parry on them
- Caps at 70%. For plate tanks, works in the front quadrant only, for brawlers this is 360 degrees.
- A small % of parries will be ripostes, which not only avoid the attack but also damage your attacker
- */
- skill = entity_victim->GetSkillByName("Parry", true);
- if(skill){
- float parryChance = entity_victim->GetInfoStruct()->get_parry();
- float chanceValue = (100.0f - parryChance);
- if(rand()%roll_chance >= chanceValue){ //successful parry
- /* You may only riposte things in the front quadrant.
- Riposte is based off of parry: a certain % of parries turn into ripostes.
- */
- if(!behind && victim->InFrontSpawn((Spawn*)this, victim->GetX(), victim->GetZ())) { // if the attacker is not behind the victim, and the victim is facing the attacker (in front of spawn) then we can riposte
- float riposteChanceValue = parryChance / 7.0f; // Riposte is based off of parry: a certain % of parries turn into ripostes. Unknown what the value is divided by, 7 to make it 10% even.
- if(rand()%100 <= riposteChanceValue) {
- entity_victim->CheckProcs(PROC_TYPE_RIPOSTE, this);
- return DAMAGE_PACKET_RESULT_RIPOSTE;
- }
- }
- entity_victim->CheckProcs(PROC_TYPE_PARRY, this);
- return DAMAGE_PACKET_RESULT_PARRY;
- }
- }
- skill = nullptr;
-
- float blockChance = 0.0f;
- if(victim->GetAdventureClass() == BRAWLER)
- skill = entity_victim->GetSkillByName("Deflection", true);
- blockChance = entity_victim->GetInfoStruct()->get_block();
-
- if(blockChance > 0.0f)
- {
- blockChance += (blockChance*(entity_victim->GetInfoStruct()->get_block_chance()/100.0f));
- float chanceValue = (100.0f - blockChance);
- /* Non-brawlers may only block things in the front quadrant.
- Riposte is based off of parry: a certain % of parries turn into ripostes.
- */
- float rnd = rand()%roll_chance;
- if(rnd >= chanceValue){ //successful block
- if((victim->GetAdventureClass() == BRAWLER || victim->GetAdventureClass() == MONK) || (!behind && victim->InFrontSpawn((Spawn*)this, victim->GetX(), victim->GetZ()))) { // if the attacker is not behind the victim, and the victim is facing the attacker (in front of spawn) then we can block
- entity_victim->CheckProcs(PROC_TYPE_BLOCK, this);
- return (victim->GetAdventureClass() == BRAWLER) ? DAMAGE_PACKET_RESULT_DEFLECT : DAMAGE_PACKET_RESULT_BLOCK;
- }
- }
- }
- skill = entity_victim->GetSkillByName("Defense", true);
- float dodgeChance = entity_victim->GetInfoStruct()->get_avoidance_base();
- if(dodgeChance > 0.0f)
- {
- float chanceValue = (100.0f - dodgeChance);
- float rnd = rand()%roll_chance;
- if(rnd >= chanceValue){ //successful dodge
- entity_victim->CheckProcs(PROC_TYPE_EVADE, this);
- return DAMAGE_PACKET_RESULT_DODGE;//successfully dodged
- }
- }
- if(rand() % roll_chance >= chance)
- return DAMAGE_PACKET_RESULT_MISS; //successfully avoided
- }
- else{
- skill = entity_victim->GetSkillByName("Spell Avoidance", true);
- if(skill)
- chance -= skill->current_val / 25;
- if(rand()%roll_chance >= chance) {
- return DAMAGE_PACKET_RESULT_RESIST; //successfully resisted
- }
- }
- return DAMAGE_PACKET_RESULT_SUCCESSFUL;
- }
- float Entity::GetDamageTypeResistPercentage(int8 damage_type) {
- float ret = 1;
- switch(damage_type) {
- case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
- case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
- case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: {
- Skill* skill = GetSkillByName("Defense", true);
- if(skill)
- ret += skill->current_val / 25;
- if(IsNPC())
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Crush/Pierce/Slash (%i)", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_HEAT: {
- ret += GetInfoStruct()->get_heat() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Heat (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_COLD: {
- ret += GetInfoStruct()->get_cold() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Cold (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC: {
- ret += GetInfoStruct()->get_magic() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Magic (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL: {
- ret += GetInfoStruct()->get_mental() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Mental (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE: {
- ret += GetInfoStruct()->get_divine() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Divine (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE: {
- ret += GetInfoStruct()->get_disease() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Disease (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- case DAMAGE_PACKET_DAMAGE_TYPE_POISON: {
- ret += GetInfoStruct()->get_poison() / 50;
- LogWrite(COMBAT__DEBUG, 3, "Combat", "DamageType: Poison (%i), Amt: %.2f", damage_type, ret);
- break;
- }
- }
- return ret;
- }
- Skill* Entity::GetSkillByWeaponType(int8 type, bool update) {
- switch(type) {
- case DAMAGE_PACKET_DAMAGE_TYPE_SLASH: // slash
- return GetSkillByName("Slashing", update);
- case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH: // crush
- return GetSkillByName("Crushing", update);
- case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE: // pierce
- return GetSkillByName("Piercing", update);
- case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
- case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
- case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
- case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
- case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
- case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
- case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
- return GetSkillByName("Disruption", update);
- }
- return 0;
- }
- 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) {
- if(!victim || victim->GetHP() == 0)
- return false;
- int8 hit_result = 0;
- int16 blow_type = 0;
- sint32 damage = 0;
- bool crit = false;
- if(low_damage > high_damage)
- high_damage = low_damage;
- if(low_damage == high_damage)
- damage = low_damage;
- else
- damage = MakeRandomInt(low_damage, high_damage);
- if(!no_calcs) {
- //this can be simplified by taking out the / 2, but I wanted the damage to be more consistent
- //damage = (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2)) + (rand()%((int)(high_damage/2-low_damage/2) + low_damage/2));
- //damage = (rand()%((int)(high_damage-low_damage) + low_damage)) + (rand()%((int)(high_damage-low_damage) + low_damage));
- //DPS mod is only applied to auto attacks
- if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE || type == DAMAGE_PACKET_TYPE_RANGE_DAMAGE ) {
- damage *= (info_struct.get_dps_multiplier());
- }
- //Potency and ability mod is only applied to spells/CAs
- else {
- // Potency mod
- MStats.lock();
- damage *= ((stats[ITEM_STAT_POTENCY] / 100) + 1);
- MStats.unlock();
- // Ability mod can only give up to half of damage after potency
- int32 mod = (int32)min(info_struct.get_ability_modifier(), (float)(damage / 2));
- damage += mod;
- }
- if(!crit_mod || crit_mod == 1){
- //force crit if crit_mod == 1
- if(crit_mod == 1)
- crit = true;
- // Crit Roll
- else {
- victim->MStats.lock();
- float chance = max((float)0, (info_struct.get_crit_chance() - victim->stats[ITEM_STAT_CRITAVOIDANCE]));
- victim->MStats.unlock();
- if (MakeRandomFloat(0, 100) <= chance)
- crit = true;
- }
- if(crit){
- //Apply total crit multiplier with crit bonus
- if(info_struct.get_crit_bonus() > 0)
- damage *= (1.3 + (info_struct.get_crit_bonus() / 100));
- else
- damage *= 1.3;
- // Change packet type to crit
- if (type == DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE)
- type = DAMAGE_PACKET_TYPE_SIMPLE_CRIT_DMG;
- else if (type == DAMAGE_PACKET_TYPE_SPELL_DAMAGE)
- type = DAMAGE_PACKET_TYPE_SPELL_CRIT_DMG;
- }
- }
- // TODO: Mitigation equation from http://www.guildportal.com/Guild.aspx?GuildID=20881&TabID=189653&ForumID=95908&TopicID=9024250
-
- }
- LogWrite(MISC__TODO, 3, "TODO", "Take players armor into account\nfile: %s, func: %s, line: %i)", __FILE__, __FUNCTION__, __LINE__);
- bool useWards = false;
- if(damage <= 0){
- hit_result = DAMAGE_PACKET_RESULT_NO_DAMAGE;
- damage = 0;
- }
- else{
- hit_result = DAMAGE_PACKET_RESULT_SUCCESSFUL;
- GetZone()->CallSpawnScript(victim, SPAWN_SCRIPT_HEALTHCHANGED, this);
- int32 prevDmg = damage;
- damage = victim->CheckWards(this, damage, damage_type);
- if (damage < (sint64)prevDmg)
- useWards = true;
- victim->TakeDamage(damage);
- victim->CheckProcs(PROC_TYPE_DAMAGED, this);
- if (IsPlayer()) {
- switch (damage_type) {
- case DAMAGE_PACKET_DAMAGE_TYPE_SLASH:
- case DAMAGE_PACKET_DAMAGE_TYPE_CRUSH:
- case DAMAGE_PACKET_DAMAGE_TYPE_PIERCE:
- if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MELEE_HIT) < damage)
- ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MELEE_HIT, damage, true);
- victim->CheckProcs(PROC_TYPE_DAMAGED_MELEE, this);
- break;
- case DAMAGE_PACKET_DAMAGE_TYPE_HEAT:
- case DAMAGE_PACKET_DAMAGE_TYPE_COLD:
- case DAMAGE_PACKET_DAMAGE_TYPE_MAGIC:
- case DAMAGE_PACKET_DAMAGE_TYPE_MENTAL:
- case DAMAGE_PACKET_DAMAGE_TYPE_DIVINE:
- case DAMAGE_PACKET_DAMAGE_TYPE_DISEASE:
- case DAMAGE_PACKET_DAMAGE_TYPE_POISON:
- if (((Player*)this)->GetPlayerStatisticValue(STAT_PLAYER_HIGHEST_MAGIC_HIT) < damage)
- ((Player*)this)->UpdatePlayerStatistic(STAT_PLAYER_HIGHEST_MAGIC_HIT, damage, true);
- victim->CheckProcs(PROC_TYPE_DAMAGED_MAGIC, this);
- break;
- }
- }
- }
- if(victim->IsNPC() && victim->GetHP() > 0)
- ((Entity*)victim)->AddHate(this, damage);
- Entity* attacker = nullptr;
- if(!ignore_attacker)
- attacker = this;
- if (damage > 0) {
- GetZone()->SendDamagePacket(attacker, victim, type, hit_result, damage_type, damage, spell_name);
- if (IsStealthed() || IsInvis())
- CancelAllStealth();
- if (victim->IsEntity())
- ((Entity*)victim)->CheckInterruptSpell(this);
- }
- else if (useWards)
- {
- GetZone()->SendDamagePacket(attacker, victim, DAMAGE_PACKET_TYPE_SIMPLE_DAMAGE, DAMAGE_PACKET_RESULT_NO_DAMAGE, damage_type, 0, spell_name);
- }
- if (victim->GetHP() <= 0)
- KillSpawn(victim, damage_type, blow_type);
- else {
- victim->CheckProcs(PROC_TYPE_DEFENSIVE, this);
- if (spell_name)
- victim->CheckProcs(PROC_TYPE_MAGICAL_DEFENSIVE, this);
- else
- victim->CheckProcs(PROC_TYPE_PHYSICAL_DEFENSIVE, this);
- }
-
- return crit;
- }
- void Entity::AddHate(Entity* attacker, sint32 hate) {
- if(!attacker || GetHP() <= 0 || attacker->GetHP() <= 0)
- return;
- // If a players pet and protect self is off
- if (IsPet() && ((NPC*)this)->GetOwner()->IsPlayer() && ((((Player*)((NPC*)this)->GetOwner())->GetInfoStruct()->get_pet_behavior() & 2) == 0))
- return;
- if (IsNPC()) {
- LogWrite(COMBAT__DEBUG, 3, "Combat", "Add NPC_AI Hate: Victim '%s', Attacker '%s', Hate: %i", GetName(), attacker->GetName(), hate);
- ((NPC*)this)->Brain()->AddHate(attacker, hate);
- // if encounter size is 0 then add the attacker to the encounter
- if (((NPC*)this)->Brain()->GetEncounterSize() == 0)
- ((NPC*)this)->Brain()->AddToEncounter(attacker);
- }
- if (attacker->GetThreatTransfer() && hate > 0) {
- Spawn* transfer_target = (Entity*)GetZone()->GetSpawnByID(attacker->GetThreatTransfer()->Target);
- if (transfer_target && transfer_target->IsEntity()) {
- sint32 transfered_hate = hate * (attacker->GetThreatTransfer()->Amount / 100);
- hate -= transfered_hate;
- this->AddHate((Entity*)transfer_target, transfered_hate);
- }
- }
- // If pet is adding hate add some to the pets owner as well
- if (attacker->IsNPC() && ((NPC*)attacker)->IsPet())
- AddHate(((NPC*)attacker)->GetOwner(), 1);
- // If player and player has a pet and protect master is set add hate to the pet
- if (IsPlayer() && HasPet() && (((Player*)this)->GetInfoStruct()->get_pet_behavior() & 1)) {
- // If we have a combat pet add hate to it
- if (((Player*)this)->GetPet())
- AddHate(((Player*)this)->GetPet(), 1);
- if (((Player*)this)->GetCharmedPet())
- AddHate(((Player*)this)->GetCharmedPet(), 1);
- }
- // If this spawn has a spawn group then add the attacker to the hate list of the other
- // group members if not already in their list
- if (HasSpawnGroup()) {
- vector<Spawn*>* group = GetSpawnGroup();
- vector<Spawn*>::iterator itr;
- for (itr = group->begin(); itr != group->end(); itr++) {
- if (!(*itr)->IsNPC())
- continue;
- NPC* spawn = (NPC*)(*itr);
- if (spawn->Brain()->GetHate(attacker) == 0)
- spawn->Brain()->AddHate(attacker, 1);
- }
- safe_delete(group);
- }
- }
- bool Entity::CheckFizzleSpell(LuaSpell* spell) {
- if(!spell || !rule_manager.GetGlobalRule(R_Spells, EnableFizzleSpells)->GetInt8()
- || spell->spell->GetSpellData()->can_fizzle == false)
- return false;
- float fizzleMaxSkill = rule_manager.GetGlobalRule(R_Spells, FizzleMaxSkill)->GetFloat();
- float baseFizzle = rule_manager.GetGlobalRule(R_Spells, DefaultFizzleChance)->GetFloat()/100.0f; // 10%
- float skillObtained = rule_manager.GetGlobalRule(R_Spells, FizzleDefaultSkill)->GetFloat(); // default of .2f so we don't go over the threshold if no skill
- Skill* skill = GetSkillByID(spell->spell->GetSpellData()->mastery_skill, false);
- if(skill && spell->spell->GetSpellData()->min_class_skill_req > 0)
- {
- float skillObtained = skill->current_val / spell->spell->GetSpellData()->min_class_skill_req;
- if(skillObtained > fizzleMaxSkill) // 120% over the skill value
- {
- skillObtained = fizzleMaxSkill;
- }
-
- baseFizzle = (fizzleMaxSkill - skillObtained) * baseFizzle;
- float totalSuccessChance = 1.0f - baseFizzle;
- float randResult = MakeRandomFloat(0.0f, 1.0f);
- if(randResult > totalSuccessChance)
- return true;
- }
- return false;
- }
- bool Entity::CheckInterruptSpell(Entity* attacker) {
- if(!IsCasting())
- return false;
- Spell* spell = GetZone()->GetSpell(this);
- if(!spell || spell->GetSpellData()->interruptable == 0)
- return false;
- if(GetInfoStruct()->get_no_interrupt())
- return false;
-
- //originally base of 30 percent chance to continue casting if attacked
- //modified to 50% and added global rule, 30% was too small at starting levels
- int8 percent = rule_manager.GetGlobalRule(R_Spells, NoInterruptBaseChance)->GetInt32();
- Skill* skill = GetSkillByName("Focus", true);
- float focusSkillPts = 0.0f;
- MStats.lock();
- focusSkillPts = stats[ITEM_STAT_FOCUS];
- MStats.unlock();
- if(skill)
- percent += ((skill->current_val + 1 + focusSkillPts)/6);
- if(MakeRandomInt(1, 100) > percent) {
- LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' interrupted spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
- GetZone()->Interrupted(this, attacker, SPELL_ERROR_INTERRUPTED);
- return true;
- }
- LogWrite(COMBAT__DEBUG, 0, "Combat", "'%s' failed to interrupt spell for '%s': %i%%", attacker->GetName(), GetName(), percent);
- return false;
- }
- void Entity::KillSpawn(Spawn* dead, int8 damage_type, int16 kill_blow_type) {
- if(!dead)
- return;
- if (IsPlayer()) {
- Client* client = GetZone()->GetClientBySpawn(this);
- PacketStruct* packet = configReader.getStruct("WS_EnterCombat", client->GetVersion());
- if (packet) {
- client->QueuePacket(packet->serialize());
- }
- safe_delete(packet);
- ((Player*)this)->InCombat(false);
- }
- if (IsPlayer() && dead->IsEntity())
- GetZone()->GetSpellProcess()->KillHOBySpawnID(dead->GetID());
- //if (dead->IsEntity()) same code called in zone server
- //((Entity*)dead)->InCombat(false);
-
- /* just for sake of not knowing if we are in a read lock, write lock, or no lock
- ** say spawnlist is locked (DismissPet arg 3 true), which means RemoveSpawn will remove the id from the spawn_list outside of the potential lock
- */
- if (dead->IsPet())
- ((NPC*)dead)->GetOwner()->DismissPet((NPC*)dead, true, true);
- else if (dead->IsEntity()) {
- // remove all pets for this entity
- ((Entity*)dead)->DismissAllPets(false, true);
- }
- // If not in combat and no one in the encounter list add this killer to the list
- if(dead->EngagedInCombat() == false && dead->IsNPC() && ((NPC*)dead)->Brain()->GetEncounterSize() == 0)
- ((NPC*)dead)->Brain()->AddToEncounter(this);
- if (IsCasting())
- GetZone()->Interrupted(this, dead, SPELL_ERROR_NOT_ALIVE);
- LogWrite(COMBAT__DEBUG, 3, "Combat", "Killing '%s'", dead->GetName());
- // Kill movement for the dead npc so the corpse doesn't move
- GetZone()->movementMgr->StopNavigation((Entity*)dead);
- dead->ClearRunningLocations();
- dead->CalculateRunningLocation(true);
- GetZone()->KillSpawn(true, dead, this, true, damage_type, kill_blow_type);
- }
- void Entity::HandleDeathExperienceDebt(Spawn* killer)
- {
- if(!IsPlayer())
- return;
- float ruleDebt = 0.0f;
-
- if(killer && killer->IsPlayer())
- ruleDebt = rule_manager.GetGlobalRule(R_Combat, PVPDeathExperienceDebt)->GetFloat()/100.0f;
- else
- ruleDebt = rule_manager.GetGlobalRule(R_Combat, DeathExperienceDebt)->GetFloat()/100.0f;
- if(ruleDebt > 0.0f)
- {
- bool groupDebt = rule_manager.GetGlobalRule(R_Combat, GroupExperienceDebt)->GetBool();
- if(groupDebt && ((Player*)this)->GetGroupMemberInfo())
- {
- world.GetGroupManager()->GroupLock(__FUNCTION__, __LINE__);
- PlayerGroup* group = world.GetGroupManager()->GetGroup(((Player*)this)->GetGroupMemberInfo()->group_id);
- if (group)
- {
- group->MGroupMembers.readlock(__FUNCTION__, __LINE__);
- deque<GroupMemberInfo*>* members = group->GetMembers();
- int32 size = members->size();
- float xpDebtPerMember = ruleDebt/(float)size;
- deque<GroupMemberInfo*>::iterator itr;
- for (itr = members->begin(); itr != members->end(); itr++) {
- GroupMemberInfo* gmi = *itr;
- if (gmi->client && gmi->client->GetPlayer()) {
- gmi->client->GetPlayer()->GetInfoStruct()->set_xp_debt(gmi->client->GetPlayer()->GetInfoStruct()->get_xp_debt()+xpDebtPerMember);
- gmi->client->GetPlayer()->SetCharSheetChanged(true);
- }
- }
- group->MGroupMembers.releasereadlock(__FUNCTION__, __LINE__);
- }
- world.GetGroupManager()->ReleaseGroupLock(__FUNCTION__, __LINE__);
- }
- else
- {
- ((Player*)this)->GetInfoStruct()->set_xp_debt(((Player*)this)->GetInfoStruct()->get_xp_debt()+ruleDebt);
- ((Player*)this)->SetCharSheetChanged(true);
- }
- }
- }
- void Entity::ProcessCombat() {
- // This is a virtual function so when a NPC calls this it will use the NPC::ProcessCombat() version
- // and a player will use the Player::ProcessCombat() version, leave this function blank.
- }
- void NPC::ProcessCombat() {
- MBrain.writelock(__FUNCTION__, __LINE__);
- // Check to see if it is time to call the AI again
- if (GetHP() > 0 && Timer::GetCurrentTime2() >= (m_brain->LastTick() + m_brain->Tick())) {
- // Probably never want to use the following log, will spam the console for every NPC in a zone 4 times a second
- //LogWrite(NPC_AI__DEBUG, 9, "NPC_AI", "%s is thinking...", GetName());
- m_brain->Think();
- // Set the time for when the brain was last called
- m_brain->SetLastTick(Timer::GetCurrentTime2());
- }
- MBrain.releasewritelock(__FUNCTION__, __LINE__);
- }
- void Player::ProcessCombat() {
- // if not in combat OR casting a spell OR dazed OR feared return out
- if (!EngagedInCombat() || IsCasting() || IsDazed() || IsFeared())
- return;
- //If no target delete combat_target and return out
- Spawn* Target = GetZone()->GetSpawnByID(target);
- if (!Target) {
- combat_target = 0;
- if (target > 0) {
- SetTarget(0);
- }
- return;
- }
- // If is not an entity return out
- if (!Target->IsEntity())
- return;
- // Reset combat target
- combat_target = 0;
- if (Target->HasTarget()) {
- if (Target->IsPlayer() || (Target->IsNPC() && Target->IsPet() && ((NPC*)Target)->GetOwner()->IsPlayer())){
- Spawn* secondary_target = Target->GetTarget();
- if (secondary_target->IsNPC() && secondary_target->appearance.attackable) {
- if (!secondary_target->IsPet() || (secondary_target->IsPet() && ((NPC*)secondary_target)->GetOwner()->IsNPC())) {
- combat_target = secondary_target;
- }
- }
- }
- }
-
- // If combat_target wasn't set in the above if set it to the original target
- if (!combat_target)
- combat_target = Target;
-
- // this if may not be required as at the min combat_target will be Target, which we already check at the begining
- if(!combat_target)
- return;
- float distance = 0;
- distance = GetDistance(combat_target);
- // Check to see if we are doing ranged auto attacks if not check to see if we are in melee range
- if (GetRangeAttack()) {
- // We are doing ranged auto attacks
-
- //check to see if we can attack the target AND the ranged weapon is ready
- if(AttackAllowed((Entity*)combat_target, distance, true) && RangeWeaponReady()) {
- Item* weapon = 0;
- Item* ammo = 0;
- // Get the currently equiped weapon and ammo for the ranged attack
- weapon = GetEquipmentList()->GetItem(EQ2_RANGE_SLOT);
- ammo = GetEquipmentList()->GetItem(EQ2_AMMO_SLOT);
- LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon '%s', Ammo '%s'", ( weapon )? weapon->name.c_str() : "None", ( ammo ) ? ammo->name.c_str() : "None");
- // If weapon and ammo are both valid perform the ranged attack else send a message to the client
- if(weapon && ammo) {
- LogWrite(COMBAT__DEBUG, 1, "Combat", "Weapon: Primary, Fighter: '%s', Target: '%s', Distance: %.2f", GetName(), combat_target->GetName(), distance);
- RangeAttack(combat_target, distance, weapon, ammo);
- }
- else {
- Client* client = GetZone()->GetClientBySpawn(this);
- if (client) {
- // 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
- if (!ammo)
- client->SimpleMessage(CHANNEL_COLOR_YELLOW, "Out of ammo.");
- if (!weapon)
- client->SimpleMessage(CHANNEL_COLOR_YELLOW, "No ranged weapon found.");
-
- }
- }
- }
- }
- else if(distance <= rule_manager.GetGlobalRule(R_Combat, MaxCombatRange)->GetFloat()) {
- // We are doing melee auto attacks and are within range
- // Check to see if we can attack the target
- if(AttackAllowed((Entity*)combat_target)) {
- // Check to see if the primary melee weapon is ready
- if(PrimaryWeaponReady()) {
- // Set the time of the last melee attack with the primary weapon and perform the melee attack with primary weapon
- SetPrimaryLastAttackTime(Timer::GetCurrentTime2());
- MeleeAttack(combat_target, distance, true);
- }
- // Check to see if the secondary weapon is ready
- if(SecondaryWeaponReady()) {
- // set the time of the last melee attack with the secondary weapon and perform the melee attack with the secondary weapon
- SetSecondaryLastAttackTime(Timer::GetCurrentTime2());
- MeleeAttack(combat_target, distance, false);
- }
- }
- }
- }
- void Entity::SetAttackDelay(bool primary, bool ranged) {
- float mod = CalculateAttackSpeedMod();
- bool dual_wield = IsDualWield();
- //Note: Capping all attack speed increases at 125% normal speed (from function CalculateAttackSpeedMod())
- //Add 33% longer delay if dual wielding
- if(dual_wield && ! ranged) {
- if(primary)
- SetPrimaryAttackDelay((GetPrimaryWeaponDelay() * 1.33) / mod);
- else
- SetSecondaryAttackDelay((GetSecondaryWeaponDelay() * 1.33) / mod);
- }
- else {
- if(primary)
- SetPrimaryAttackDelay(GetPrimaryWeaponDelay() / mod);
- else if(ranged)
- SetRangeAttackDelay(GetRangeWeaponDelay() / mod);
- else
- SetSecondaryAttackDelay(GetSecondaryWeaponDelay() / mod);
- }
- }
- float Entity::CalculateAttackSpeedMod(){
- float aspeed = info_struct.get_attackspeed();
-
- if(aspeed > 0) {
- if (aspeed <= 100)
- return (aspeed / 100 + 1);
- else if (aspeed <= 200)
- return 2.25;
- }
- return 1;
- }
- void Entity::AddProc(int8 type, float chance, Item* item, LuaSpell* spell) {
- if (type == 0) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc called with an invalid type.");
- return;
- }
- if (!item && !spell) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::AddProc must have a valid item or spell.");
- return;
- }
- MProcList.writelock(__FUNCTION__, __LINE__);
- Proc* proc = new Proc();
- proc->chance = chance;
- proc->item = item;
- proc->spell = spell;
- proc->spellid = spell->spell->GetSpellID();
- m_procList[type].push_back(proc);
- MProcList.releasewritelock(__FUNCTION__, __LINE__);
- }
- void Entity::RemoveProc(Item* item, LuaSpell* spell) {
- if (!item && !spell) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::RemoveProc must have a valid item or spell.");
- return;
- }
- MProcList.writelock(__FUNCTION__, __LINE__);
- map<int8, vector<Proc*> >::iterator proc_itr;
- vector<Proc*>::iterator itr;
- for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
- itr = proc_itr->second.begin();
- while (itr != proc_itr->second.end()) {
- Proc* proc = *itr;
- if ((item && proc->item == item) || (spell && proc->spell == spell)) {
- itr = proc_itr->second.erase(itr);
- safe_delete(proc);
- }
- else
- itr++;
- }
- }
- MProcList.releasewritelock(__FUNCTION__, __LINE__);
- }
- bool Entity::CastProc(Proc* proc, int8 type, Spawn* target) {
- lua_State* state = 0;
- bool item_proc = false;
- int8 num_args = 3;
- if (proc->spell) {
- state = proc->spell->state;
- }
- else if (proc->item) {
- state = lua_interface->GetItemScript(proc->item->GetItemScript());
- item_proc = true;
- }
-
- if (!state) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "No valid lua_State* found");
- return false;
- }
- lua_getglobal(state, "proc");
- if (item_proc) {
- num_args++;
- lua_interface->SetItemValue(state, proc->item);
- }
- lua_interface->SetSpawnValue(state, this);
- lua_interface->SetSpawnValue(state, target);
- lua_interface->SetInt32Value(state, type);
- /*
- Add spell data from db in case of a spell proc here...
- */
- if (!item_proc) {
- // Append spell data to the param list
- vector<LUAData*>* data = proc->spell->spell->GetLUAData();
- for(int32 i = 0; i < data->size(); i++) {
- switch(data->at(i)->type) {
- case 0:{
- lua_interface->SetSInt32Value(proc->spell->state, data->at(i)->int_value);
- break;
- }
- case 1:{
- lua_interface->SetFloatValue(proc->spell->state, data->at(i)->float_value);
- break;
- }
- case 2:{
- lua_interface->SetBooleanValue(proc->spell->state, data->at(i)->bool_value);
- break;
- }
- case 3:{
- lua_interface->SetStringValue(proc->spell->state, data->at(i)->string_value.c_str());
- break;
- }
- default:{
- 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());
- return false;
- }
- }
- num_args++;
- }
- }
- if (lua_pcall(state, num_args, 0, 0) != 0) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "Unable to call the proc function for spell %i tier %i", proc->spell->spell->GetSpellID(), proc->spell->spell->GetSpellTier());
- lua_pop(state, 1);
- return false;
- }
- return true;
- }
- void Entity::CheckProcs(int8 type, Spawn* target) {
- if (type == 0) {
- LogWrite(COMBAT__ERROR, 0, "Proc", "Entity::CheckProcs called with an invalid type.");
- return;
- }
- float roll = MakeRandomFloat(0, 100);
- vector<Proc*> tmpList;
- MProcList.readlock(__FUNCTION__, __LINE__);
- for (int8 i = 0; i < m_procList[type].size(); i++) {
- Proc* proc = m_procList[type].at(i);
- if (roll <= proc->chance)
- {
- Proc* tmpProc = new Proc();
- tmpProc->chance = proc->chance;
- tmpProc->item = proc->item;
- tmpProc->spell = proc->spell;
- tmpProc->spellid = proc->spellid;
- tmpList.push_back(tmpProc);
- }
- }
- MProcList.releasereadlock(__FUNCTION__, __LINE__);
- vector<Proc*>::iterator proc_itr;
- for (proc_itr = tmpList.begin(); proc_itr != tmpList.end();) {
- Proc* tmpProc = *proc_itr;
- CastProc(tmpProc, type, target);
- proc_itr++;
- safe_delete(tmpProc);
- }
- }
- void Entity::ClearProcs() {
- MProcList.writelock(__FUNCTION__, __LINE__);
- map<int8, vector<Proc*> >::iterator proc_itr;
- vector<Proc*>::iterator itr;
- for (proc_itr = m_procList.begin(); proc_itr != m_procList.end(); proc_itr++) {
- itr = proc_itr->second.begin();
- while (itr != proc_itr->second.end()) {
- safe_delete(*itr);
- itr = proc_itr->second.erase(itr);
- }
- proc_itr->second.clear();
- }
- m_procList.clear();
- MProcList.releasewritelock(__FUNCTION__, __LINE__);
- }
|