Skip to content

Commit

Permalink
feat(Core/Creatures): implement a sparring system (azerothcore#19824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grimdhex authored Jan 26, 2025
1 parent 2b4a6cc commit edf2959
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
9 changes: 9 additions & 0 deletions data/sql/updates/pending_db_world/rev_1724592400426298600.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--
DROP TABLE IF EXISTS `creature_sparring`;
CREATE TABLE `creature_sparring` (
`GUID` int unsigned NOT NULL,
`SparringPCT` float NOT NULL,
PRIMARY KEY (`GUID`),
FOREIGN KEY (`GUID`) REFERENCES creature(`guid`),
CONSTRAINT `creature_sparring_chk_1` CHECK (`SparringPCT` BETWEEN 0 AND 100)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
19 changes: 18 additions & 1 deletion src/server/game/Entities/Creature/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Creature::Creature(bool isWorldObject): Unit(isWorldObject), MovableMapObject(),
m_transportCheckTimer(1000), lootPickPocketRestoreTime(0), m_combatPulseTime(0), m_combatPulseDelay(0), m_reactState(REACT_AGGRESSIVE), m_defaultMovementType(IDLE_MOTION_TYPE),
m_spawnId(0), m_equipmentId(0), m_originalEquipmentId(0), m_AlreadyCallAssistance(false),
m_AlreadySearchedAssistance(false), m_regenHealth(true), m_regenPower(true), m_AI_locked(false), m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL), m_originalEntry(0), m_moveInLineOfSightDisabled(false), m_moveInLineOfSightStrictlyDisabled(false),
m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), m_detectionDistance(20.0f), m_waypointID(0), m_path_id(0), m_formation(nullptr), m_lastLeashExtensionTime(nullptr), m_cannotReachTimer(0),
m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), m_detectionDistance(20.0f),_sparringPct(0.0f), m_waypointID(0), m_path_id(0), m_formation(nullptr), m_lastLeashExtensionTime(nullptr), m_cannotReachTimer(0),
_isMissingSwimmingFlagOutOfCombat(false), m_assistanceTimer(0), _playerDamageReq(0), _damagedByPlayer(false), _isCombatMovementAllowed(true)
{
m_regenTimer = CREATURE_REGEN_INTERVAL;
Expand Down Expand Up @@ -608,6 +608,8 @@ bool Creature::UpdateEntry(uint32 Entry, const CreatureData* data, bool changele
SetCanModifyStats(true);
UpdateAllStats();

LoadSparringPct();

// checked and error show at loading templates
if (FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(cInfo->faction))
{
Expand Down Expand Up @@ -1189,6 +1191,7 @@ bool Creature::Create(ObjectGuid::LowType guidlow, Map* map, uint32 phaseMask, u
}

LoadCreaturesAddon();
LoadSparringPct();

//! Need to be called after LoadCreaturesAddon - MOVEMENTFLAG_HOVER is set there
m_positionZ += GetHoverHeight();
Expand Down Expand Up @@ -2024,6 +2027,8 @@ void Creature::setDeathState(DeathState state, bool despawn)

Motion_Initialize();
LoadCreaturesAddon(true);
LoadSparringPct();

if (GetCreatureData() && GetPhaseMask() != GetCreatureData()->phaseMask)
SetPhaseMask(GetCreatureData()->phaseMask, false);
}
Expand Down Expand Up @@ -2797,6 +2802,18 @@ bool Creature::LoadCreaturesAddon(bool reload)
return true;
}

void Creature::LoadSparringPct()
{
ObjectGuid::LowType spawnId = GetSpawnId();
auto const& sparringData = sObjectMgr->GetSparringData();

auto itr = sparringData.find(spawnId);
if (itr != sparringData.end() && !itr->second.empty())
{
_sparringPct = itr->second[0];
}
}

/// Send a message to LocalDefense channel for players opposition team in the zone
void Creature::SendZoneUnderAttackMessage(Player* attacker)
{
Expand Down
5 changes: 5 additions & 0 deletions src/server/game/Entities/Creature/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ class Creature : public Unit, public GridObject<Creature>, public MovableMapObje
void UpdateAttackPowerAndDamage(bool ranged = false) override;
void CalculateMinMaxDamage(WeaponAttackType attType, bool normalized, bool addTotalPct, float& minDamage, float& maxDamage, uint8 damageIndex) override;

void LoadSparringPct();
[[nodiscard]] float GetSparringPct() const { return _sparringPct; }

bool HasWeapon(WeaponAttackType type) const override;
bool HasWeaponForAttack(WeaponAttackType type) const override { return (Unit::HasWeaponForAttack(type) && HasWeapon(type)); }
void SetCanDualWield(bool value) override;
Expand Down Expand Up @@ -483,6 +486,8 @@ class Creature : public Unit, public GridObject<Creature>, public MovableMapObje
float m_detectionDistance;
uint16 m_LootMode; // bitmask, default LOOT_MODE_DEFAULT, determines what loot will be lootable

float _sparringPct;

[[nodiscard]] bool IsInvisibleDueToDespawn() const override;
bool CanAlwaysSee(WorldObject const* obj) const override;
bool IsAlwaysDetectableFor(WorldObject const* seer) const override;
Expand Down
33 changes: 33 additions & 0 deletions src/server/game/Entities/Unit/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,17 @@ uint32 Unit::DealDamage(Unit* attacker, Unit* victim, uint32 damage, CleanDamage
}
}

// Sparring
if (victim->CanSparringWith(attacker))
{
if (damage >= victim->GetHealth())
damage = 0;

uint32 sparringHealth = victim->GetHealth() * (victim->ToCreature()->GetSparringPct() / 100);
if (victim->GetHealth() - damage <= sparringHealth)
damage = 0;
}

if (health <= damage)
{
LOG_DEBUG("entities.unit", "DealDamage: victim just died");
Expand Down Expand Up @@ -2635,6 +2646,10 @@ void Unit::AttackerStateUpdate(Unit* victim, WeaponAttackType attType /*= BASE_A
Unit::DealDamageMods(victim, damageInfo.damages[i].damage, &damageInfo.damages[i].absorb);
}

// Related to sparring system. Allow attack animations even if there are no damages
if (victim->CanSparringWith(damageInfo.attacker))
damageInfo.HitInfo |= HITINFO_FAKE_DAMAGE;

SendAttackStateUpdate(&damageInfo);

//TriggerAurasProcOnEvent(damageInfo);
Expand Down Expand Up @@ -3954,6 +3969,24 @@ void Unit::_UpdateAutoRepeatSpell()
}
}

bool Unit::CanSparringWith(Unit const* attacker) const
{
if (!IsCreature() || IsCharmedOwnedByPlayerOrPlayer())
return false;

if (!attacker)
return false;

if (!attacker->IsCreature() || attacker->IsCharmedOwnedByPlayerOrPlayer())
return false;

if (Creature const* creature = ToCreature())
if (!creature->GetSparringPct())
return false;

return true;
}

void Unit::SetCurrentCastedSpell(Spell* pSpell)
{
ASSERT(pSpell); // nullptr may be never passed here, use InterruptSpell or InterruptNonMeleeSpells
Expand Down
2 changes: 2 additions & 0 deletions src/server/game/Entities/Unit/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,8 @@ class Unit : public WorldObject

void _UpdateAutoRepeatSpell();

bool CanSparringWith(Unit const* attacker) const; ///@brief: Check if unit is eligible for sparring damages. Work only if attacker and victim are creatures.

bool IsAlwaysVisibleFor(WorldObject const* seer) const override;
bool IsAlwaysDetectableFor(WorldObject const* seer) const override;

Expand Down
36 changes: 36 additions & 0 deletions src/server/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,42 @@ void ObjectMgr::LoadCreatures()
LOG_INFO("server.loading", " ");
}

void ObjectMgr::LoadCreatureSparring()
{
uint32 oldMSTime = getMSTime();

QueryResult result = WorldDatabase.Query("SELECT GUID, SparringPCT FROM creature_sparring");

if (!result)
{
LOG_WARN("server.loading", ">> Loaded 0 sparring data. DB table `creature_sparring` is empty.");
LOG_INFO("server.loading", " ");
return;
}

uint32 count = 0;
do
{
Field* fields = result->Fetch();

ObjectGuid::LowType spawnId = fields[0].Get<uint32>();
float sparringHealthPct = fields[1].Get<float>();

if (!sObjectMgr->GetCreatureData(spawnId))
{
LOG_ERROR("sql.sql", "Entry {} has a record in `creature_sparring` but doesn't exist in `creatures` table");
continue;
}

_creatureSparringStore[spawnId].push_back(sparringHealthPct);

++count;
} while (result->NextRow());

LOG_INFO("server.loading", ">> Loaded {} sparring data in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
LOG_INFO("server.loading", " ");
}

void ObjectMgr::AddCreatureToGrid(ObjectGuid::LowType guid, CreatureData const* data)
{
uint8 mask = data->spawnMask;
Expand Down
8 changes: 8 additions & 0 deletions src/server/game/Globals/ObjectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,8 @@ class ObjectMgr

typedef std::map<uint32, uint32> CharacterConversionMap;

typedef std::unordered_map<ObjectGuid::LowType, std::vector<float>> CreatureSparringContainer;

GameObjectTemplate const* GetGameObjectTemplate(uint32 entry);
bool IsGameObjectStaticTransport(uint32 entry);
[[nodiscard]] GameObjectTemplateContainer const* GetGameObjectTemplates() const { return &_gameObjectTemplateStore; }
Expand Down Expand Up @@ -1028,6 +1030,7 @@ class ObjectMgr
void LoadCreatureQuestItems();
void LoadTempSummons();
void LoadCreatures();
void LoadCreatureSparring();
void LoadLinkedRespawn();
bool SetCreatureLinkedRespawn(ObjectGuid::LowType guid, ObjectGuid::LowType linkedGuid);
void LoadCreatureAddons();
Expand Down Expand Up @@ -1201,6 +1204,9 @@ class ObjectMgr
if (itr == _creatureDataStore.end()) return nullptr;
return &itr->second;
}

[[nodiscard]] CreatureSparringContainer const& GetSparringData() const { return _creatureSparringStore; }

CreatureData& NewOrExistCreatureData(ObjectGuid::LowType spawnId) { return _creatureDataStore[spawnId]; }
void DeleteCreatureData(ObjectGuid::LowType spawnId);
[[nodiscard]] ObjectGuid GetLinkedRespawnGuid(ObjectGuid guid) const
Expand Down Expand Up @@ -1526,6 +1532,8 @@ class ObjectMgr
PageTextContainer _pageTextStore;
InstanceTemplateContainer _instanceTemplateStore;

CreatureSparringContainer _creatureSparringStore;

private:
void LoadScripts(ScriptsType type);
void LoadQuestRelationsHelper(QuestRelations& map, std::string const& table, bool starter, bool go);
Expand Down
3 changes: 3 additions & 0 deletions src/server/game/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,9 @@ void World::SetInitialWorldSettings()
LOG_INFO("server.loading", "Loading Creature Data...");
sObjectMgr->LoadCreatures();

LOG_INFO("server.loading", "Loading Creature sparring...");
sObjectMgr->LoadCreatureSparring();

LOG_INFO("server.loading", "Loading Temporary Summon Data...");
sObjectMgr->LoadTempSummons(); // must be after LoadCreatureTemplates() and LoadGameObjectTemplates()

Expand Down

0 comments on commit edf2959

Please sign in to comment.