Skip to content

Commit

Permalink
AI: Add automatic remount and speed change using MountEntry
Browse files Browse the repository at this point in the history
Cant add it to addon cos addon does displayId mounting
  • Loading branch information
killerwife committed Nov 7, 2024
1 parent f431038 commit 877d607
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/game/AI/BaseAI/CreatureAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ void CreatureAI::DamageTaken(Unit* dealer, uint32& damage, DamageEffectType dama
}
}

void CreatureAI::JustReachedHome()
{
if (m_dismountOnAggro)
if (CreatureInfo const* mountInfo = m_creature->GetMountInfo())
m_creature->Mount(Creature::ChooseDisplayId(mountInfo));
}

void CreatureAI::SetDeathPrevention(bool state)
{
if (state)
Expand Down
1 change: 1 addition & 0 deletions src/game/AI/BaseAI/CreatureAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CreatureAI : public UnitAI
virtual void AttackStart(Unit* who) override;
virtual void DamageTaken(Unit* dealer, uint32& damage, DamageEffectType damageType, SpellEntry const* spellInfo) override;
virtual void JustPreventedDeath(Unit* /*attacker*/) {}
virtual void JustReachedHome() override;

void DoFakeDeath(uint32 spellId = 0);
void SetDeathPrevention(bool state);
Expand Down
6 changes: 1 addition & 5 deletions src/game/AI/EventAI/CreatureEventAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,11 +1162,7 @@ bool CreatureEventAI::ProcessAction(CreatureEventAI_Action const& action, uint32
// set model based on entry from creature_template
if (action.mount.creatureId)
{
if (CreatureInfo const* cInfo = ObjectMgr::GetCreatureTemplate(action.mount.creatureId))
{
uint32 display_id = Creature::ChooseDisplayId(cInfo);
m_creature->Mount(display_id);
}
m_creature->MountEntry(action.mount.creatureId);
}
// if no param1, then use value from param2 (modelId)
else
Expand Down
30 changes: 14 additions & 16 deletions src/game/DBScripts/ScriptMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2377,27 +2377,25 @@ bool ScriptAction::ExecuteDbscriptCommand(WorldObject* pSource, WorldObject* pTa
break;

Creature* creatureSource = static_cast<Creature*>(pSource);
if (!m_script->mount.creatureOrModelEntry)
if (m_script->mount.speedChange) // new flow
{
creatureSource->Unmount();
if (m_script->mount.speedChange)
{
creatureSource->SetBaseRunSpeed(creatureSource->GetCreatureInfo()->SpeedRun);
creatureSource->UpdateSpeed(MOVE_RUN, true);
}
if (m_script->mount.creatureOrModelEntry)
creatureSource->MountEntry(m_script->mount.creatureOrModelEntry);
else
creatureSource->UnmountEntry();
}
else if (m_script->data_flags & SCRIPT_FLAG_COMMAND_ADDITIONAL)
creatureSource->Mount(m_script->mount.creatureOrModelEntry);
else
{
CreatureInfo const* ci = ObjectMgr::GetCreatureTemplate(m_script->mount.creatureOrModelEntry);
uint32 display_id = Creature::ChooseDisplayId(ci);

creatureSource->Mount(display_id);
if (m_script->mount.speedChange)
if (!m_script->mount.creatureOrModelEntry)
creatureSource->Unmount();
else if (m_script->data_flags & SCRIPT_FLAG_COMMAND_ADDITIONAL)
creatureSource->Mount(m_script->mount.creatureOrModelEntry);
else
{
creatureSource->SetBaseRunSpeed(ci->SpeedRun);
creatureSource->UpdateSpeed(MOVE_RUN, true);
CreatureInfo const* ci = ObjectMgr::GetCreatureTemplate(m_script->mount.creatureOrModelEntry);
uint32 display_id = Creature::ChooseDisplayId(ci);

creatureSource->Mount(display_id);
}
}

Expand Down
28 changes: 25 additions & 3 deletions src/game/Entities/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Creature::Creature(CreatureSubtype subtype) : Unit(),
m_settings(this),
m_countSpawns(false),
m_creatureGroup(nullptr), m_imposedCooldown(false),
m_creatureInfo(nullptr)
m_creatureInfo(nullptr), m_mountInfo(nullptr)
{
m_valuesCount = UNIT_END;

Expand Down Expand Up @@ -1581,6 +1581,21 @@ void Creature::ClearCreatureGroup()
m_creatureGroup = nullptr;
}

void Creature::SetMountInfo(CreatureInfo const* info)
{
if (info)
{
if (info->SpeedRun)
SetBaseRunSpeed(info->SpeedRun);
}
else if (GetCreatureInfo()->SpeedRun)
SetBaseRunSpeed(GetCreatureInfo()->SpeedRun);
else
UpdateModelData();

m_mountInfo = info;
}

bool Creature::CreateFromProto(uint32 dbGuid, uint32 guidlow, CreatureInfo const* cinfo, const CreatureData* data /*=nullptr*/, GameEventCreatureData const* eventData /*=nullptr*/)
{
m_originalEntry = cinfo->Entry;
Expand Down Expand Up @@ -2845,8 +2860,15 @@ void Creature::SetBaseWalkSpeed(float speed)
void Creature::SetBaseRunSpeed(float speed, bool force)
{
float newSpeed = speed;
if (!force && m_creatureInfo->SpeedRun) // Creature template should still override
newSpeed = m_creatureInfo->SpeedRun;
if (!force)
{
if (m_mountInfo && m_mountInfo->SpeedRun) // mount precedes everything
newSpeed = m_mountInfo->SpeedRun;
else if (m_creatureInfo->SpeedRun) // Creature template should still override
newSpeed = m_creatureInfo->SpeedRun;
else if (m_modelRunSpeed > 0)
newSpeed = m_modelRunSpeed;
}

if (newSpeed != m_baseSpeedRun)
{
Expand Down
8 changes: 8 additions & 0 deletions src/game/Entities/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,11 @@ class Creature : public Unit
ObjectGuid GetKillerGuid() const { return m_killer; }
void SetKillerGuid(ObjectGuid guid) { m_killer = guid; }

CreatureInfo const* GetMountInfo() const override{ return m_mountInfo; }
void SetMountInfo(CreatureInfo const* info) override;

void SetModelRunSpeed(float runSpeed) override { m_modelRunSpeed = runSpeed; }

protected:
bool CreateFromProto(uint32 dbGuid, uint32 guidlow, CreatureInfo const* cinfo, const CreatureData* data = nullptr, GameEventCreatureData const* eventData = nullptr);
bool InitEntry(uint32 Entry, const CreatureData* data = nullptr, GameEventCreatureData const* eventData = nullptr);
Expand Down Expand Up @@ -980,6 +985,9 @@ class Creature : public Unit
private:
GridReference<Creature> m_gridRef;
CreatureInfo const* m_creatureInfo; // in heroic mode can different from sObjectMgr::GetCreatureTemplate(GetEntry())

CreatureInfo const* m_mountInfo;
float m_modelRunSpeed;
};

class ForcedDespawnDelayEvent : public BasicEvent
Expand Down
31 changes: 30 additions & 1 deletion src/game/Entities/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8070,6 +8070,22 @@ float Unit::GetPPMProcChance(uint32 WeaponSpeed, float PPM) const
return WeaponSpeed * PPM / 600.0f; // result is chance in percents (probability = Speed_in_sec * (PPM / 60))
}

bool Unit::MountEntry(uint32 templateEntry, const Aura* aura)
{
CreatureInfo const* ci = ObjectMgr::GetCreatureTemplate(templateEntry);
uint32 display_id = Creature::ChooseDisplayId(ci);

SetMountInfo(ci);

return Mount(display_id, aura);
}

bool Unit::UnmountEntry(const Aura* aura)
{
SetMountInfo(nullptr);
return Unmount(aura);
}

bool Unit::Mount(uint32 displayid, const Aura* aura/* = nullptr*/)
{
// Custom mount (non-aura such as taxi or command) overwrites aura mounts
Expand All @@ -8084,6 +8100,12 @@ bool Unit::Mount(uint32 displayid, const Aura* aura/* = nullptr*/)

if (aura)
SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_MOUNT);

if (GetMountInfo())
{
SetBaseRunSpeed(1.f); // overriden inside
UpdateSpeed(MOVE_RUN, true);
}
return true;
}

Expand All @@ -8110,6 +8132,12 @@ bool Unit::Unmount(const Aura* aura/* = nullptr*/)
SendMessageToSet(data, true);
}

if (GetMountInfo())
{
SetBaseRunSpeed(1.f); // overriden inside
UpdateSpeed(MOVE_RUN, true);
}

return true;
}

Expand Down Expand Up @@ -8757,7 +8785,7 @@ void Unit::UpdateSpeed(UnitMoveType mtype, bool forced, float ratio)
if (slow)
speed *= (100.0f + slow) / 100.0f;

if (GetTypeId() == TYPEID_UNIT)
if (IsCreature())
{
switch (mtype)
{
Expand Down Expand Up @@ -10652,6 +10680,7 @@ void Unit::UpdateModelData()
SetFloatValue(UNIT_FIELD_COMBATREACH, GetObjectScale() * modelInfo->combat_reach);

SetBaseWalkSpeed(modelInfo->SpeedWalk);
SetModelRunSpeed(modelInfo->SpeedRun);
SetBaseRunSpeed(modelInfo->SpeedRun, false);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/game/Entities/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,8 @@ class Unit : public WorldObject

bool IsMounted() const { return HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_MOUNT); } // not used with creature non-aura mounts
uint32 GetMountID() const { return GetUInt32Value(UNIT_FIELD_MOUNTDISPLAYID); }
bool MountEntry(uint32 templateEntry, const Aura* aura = nullptr);
bool UnmountEntry(const Aura* aura = nullptr);
virtual bool Mount(uint32 displayid, const Aura* aura = nullptr);
virtual bool Unmount(const Aura* aura = nullptr);

Expand Down Expand Up @@ -2504,6 +2506,10 @@ class Unit : public WorldObject
virtual bool IsNoWeaponSkillGain() const { return false; }
virtual bool IsPreventingDeath() const { return false; }

virtual CreatureInfo const* GetMountInfo() const { return nullptr; } // TODO: Meant to be used by players during taxi
virtual void SetMountInfo(CreatureInfo const* info) {} // does nothing for base unit
virtual void SetModelRunSpeed(float runSpeed) {} // does nothing for base unit

protected:
bool MeetsSelectAttackingRequirement(Unit* target, SpellEntry const* spellInfo, uint32 selectFlags, SelectAttackingTargetParams params, int32 unitConditionId) const;

Expand Down

0 comments on commit 877d607

Please sign in to comment.