From 4e2e108076c7b12cadcbbfba62d74e2ec69aca68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=98=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD?= Date: Wed, 21 Aug 2024 15:00:17 +0200 Subject: [PATCH] feat: change death logic --- Client/Domain/ValueObjects/VitalStats.cs | 11 +++++---- L2BotCore/Domain/Entities/Hero.h | 6 ++++- L2BotCore/Domain/Entities/NPC.h | 6 ++++- L2BotCore/Domain/Entities/Player.h | 6 ++++- L2BotCore/Domain/ValueObjects/VitalStats.h | 24 +++++++++++++++---- .../Interlude/Repositories/HeroRepository.h | 1 + .../Interlude/Repositories/NPCRepository.h | 3 +++ .../Interlude/Repositories/PlayerRepository.h | 19 +++++++++++++++ 8 files changed, 64 insertions(+), 12 deletions(-) diff --git a/Client/Domain/ValueObjects/VitalStats.cs b/Client/Domain/ValueObjects/VitalStats.cs index c69a28c..feaf345 100644 --- a/Client/Domain/ValueObjects/VitalStats.cs +++ b/Client/Domain/ValueObjects/VitalStats.cs @@ -11,13 +11,15 @@ namespace Client.Domain.ValueObjects private uint maxMp; private uint cp; private uint maxCp; + private bool isDead; - public uint Hp { get => hp; set { if (value != hp) { hp = value; OnPropertyChanged(); OnPropertyChanged("MaxHp"); OnPropertyChanged("IsDead"); } } } - public uint MaxHp { get => Math.Max(hp, maxHp); set { if (value != maxHp) { maxHp = value; OnPropertyChanged(); OnPropertyChanged("IsDead"); } } } + public uint Hp { get => hp; set { if (value != hp) { hp = value; OnPropertyChanged(); OnPropertyChanged("MaxHp"); } } } + public uint MaxHp { get => Math.Max(hp, maxHp); set { if (value != maxHp) { maxHp = value; OnPropertyChanged(); } } } public uint Mp { get => mp; set { if (value != mp) { mp = value; OnPropertyChanged(); } } } public uint MaxMp { get => maxMp; set { if (value != maxMp) { maxMp = value; OnPropertyChanged(); } } } public uint Cp { get => cp; set { if (value != cp) { cp = value; OnPropertyChanged(); } } } public uint MaxCp { get => maxCp; set { if (value != maxCp) { maxCp = value; OnPropertyChanged(); } } } + public bool IsDead { get => isDead; set { if (value != isDead) { isDead = value; OnPropertyChanged(); } } } public double HpPercent { @@ -41,9 +43,7 @@ namespace Client.Domain.ValueObjects } } - public bool IsDead => MaxHp > 0 && hp == 0; - - public VitalStats(uint hp, uint maxHp, uint mp, uint maxMp, uint cp, uint maxCp) + public VitalStats(uint hp, uint maxHp, uint mp, uint maxMp, uint cp, uint maxCp, bool isDead) { this.hp = hp; this.maxHp = maxHp; @@ -51,6 +51,7 @@ namespace Client.Domain.ValueObjects this.maxMp = maxMp; this.cp = cp; this.maxCp = maxCp; + this.isDead = isDead; } } } diff --git a/L2BotCore/Domain/Entities/Hero.h b/L2BotCore/Domain/Entities/Hero.h index 60ca22b..2eecbd2 100644 --- a/L2BotCore/Domain/Entities/Hero.h +++ b/L2BotCore/Domain/Entities/Hero.h @@ -31,7 +31,7 @@ namespace L2Bot::Domain::Entities WorldObject::Update(transform); m_FullName = fullName; - m_VitalStats = vitalStats; + m_VitalStats.LoadFromOther(vitalStats); m_Phenotype = phenotype; m_ExperienceInfo = experienceInfo; m_PermanentStats = permanentStats; @@ -93,6 +93,10 @@ namespace L2Bot::Domain::Entities { m_AttackerIds.clear(); } + void MarkAsDead() + { + m_VitalStats.MarkAsDead(); + } const std::vector BuildSerializationNodes() const override { diff --git a/L2BotCore/Domain/Entities/NPC.h b/L2BotCore/Domain/Entities/NPC.h index 1f3c522..90e26fd 100644 --- a/L2BotCore/Domain/Entities/NPC.h +++ b/L2BotCore/Domain/Entities/NPC.h @@ -28,7 +28,7 @@ namespace L2Bot::Domain::Entities m_IsHostile = isHostile; m_NpcId = npcId; m_FullName = fullName; - m_VitalStats = vitalStats; + m_VitalStats.LoadFromOther(vitalStats); } const size_t GetHash() const override { @@ -45,6 +45,10 @@ namespace L2Bot::Domain::Entities { return "npc"; } + void MarkAsDead() + { + m_VitalStats.MarkAsDead(); + } const std::vector BuildSerializationNodes() const override { diff --git a/L2BotCore/Domain/Entities/Player.h b/L2BotCore/Domain/Entities/Player.h index 48aa2cf..87e8849 100644 --- a/L2BotCore/Domain/Entities/Player.h +++ b/L2BotCore/Domain/Entities/Player.h @@ -19,7 +19,7 @@ namespace L2Bot::Domain::Entities m_FullName = fullName; m_Phenotype = phenotype; - m_VitalStats = vitalStats; + m_VitalStats.LoadFromOther(vitalStats); } const size_t GetHash() const override { @@ -34,6 +34,10 @@ namespace L2Bot::Domain::Entities { return "player"; } + void MarkAsDead() + { + m_VitalStats.MarkAsDead(); + } const std::vector BuildSerializationNodes() const override { diff --git a/L2BotCore/Domain/ValueObjects/VitalStats.h b/L2BotCore/Domain/ValueObjects/VitalStats.h index 9fd9294..e58e93e 100644 --- a/L2BotCore/Domain/ValueObjects/VitalStats.h +++ b/L2BotCore/Domain/ValueObjects/VitalStats.h @@ -10,9 +10,9 @@ namespace L2Bot::Domain::ValueObjects class VitalStats : public Serializers::Serializable, public Entities::Hashable { public: - const bool IsAlive() const + const bool IsDead() const { - return m_MaxHp <= 0 || m_Hp > 0; + return m_IsDead || (m_MaxHp > 0 && m_Hp < 0); } const uint32_t GetMaxHp() const { @@ -46,9 +46,19 @@ namespace L2Bot::Domain::ValueObjects std::hash{}(m_MaxMp), std::hash{}(m_Mp), std::hash{}(m_MaxCp), - std::hash{}(m_Cp) + std::hash{}(m_Cp), + std::hash{}(m_IsDead) }); } + void LoadFromOther(VitalStats other) + { + m_MaxHp = other.m_MaxHp; + m_Hp = other.m_Hp; + m_MaxMp = other.m_MaxMp; + m_Mp = other.m_Mp; + m_MaxCp = other.m_MaxCp; + m_Cp = other.m_Cp; + } const std::vector BuildSerializationNodes() const override { @@ -59,9 +69,14 @@ namespace L2Bot::Domain::ValueObjects { L"maxMp", std::to_wstring(m_MaxMp) }, { L"mp", std::to_wstring(m_Mp) }, { L"maxCp", std::to_wstring(m_MaxCp) }, - { L"cp", std::to_wstring(m_Cp) } + { L"cp", std::to_wstring(m_Cp) }, + { L"isDead", std::to_wstring(m_IsDead) } }; } + void MarkAsDead() + { + m_IsDead = true;; + } VitalStats( uint32_t maxHp, @@ -90,5 +105,6 @@ namespace L2Bot::Domain::ValueObjects uint32_t m_Mp = 0; uint32_t m_MaxCp = 0; uint32_t m_Cp = 0; + uint32_t m_IsDead = false; }; } diff --git a/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h b/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h index cb0b2e3..841799b 100644 --- a/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h +++ b/L2BotDll/Versions/Interlude/Repositories/HeroRepository.h @@ -83,6 +83,7 @@ namespace Interlude { Services::ServiceLocator::GetInstance().GetLogger()->App(L"{} died", m_Hero->GetFullName().GetNickname()); m_Hero->ClearAttackers(); + m_Hero->MarkAsDead(); } else { diff --git a/L2BotDll/Versions/Interlude/Repositories/NPCRepository.h b/L2BotDll/Versions/Interlude/Repositories/NPCRepository.h index 5aaf6d7..15c3afa 100644 --- a/L2BotDll/Versions/Interlude/Repositories/NPCRepository.h +++ b/L2BotDll/Versions/Interlude/Repositories/NPCRepository.h @@ -111,6 +111,9 @@ namespace Interlude m_Spoiled[casted.GetCreatureId()] = Enums::SpoilStateEnum::none; } } + if (m_Npcs.find(casted.GetCreatureId()) != m_Npcs.end()) { + m_Npcs[casted.GetCreatureId()]->MarkAsDead(); + } } } diff --git a/L2BotDll/Versions/Interlude/Repositories/PlayerRepository.h b/L2BotDll/Versions/Interlude/Repositories/PlayerRepository.h index 8452b56..d21bae5 100644 --- a/L2BotDll/Versions/Interlude/Repositories/PlayerRepository.h +++ b/L2BotDll/Versions/Interlude/Repositories/PlayerRepository.h @@ -46,6 +46,13 @@ namespace Interlude m_Players.clear(); } + void Init() override + { + Services::ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(Events::CreatureDiedEvent::name, [this](const Events::Event& evt) { + OnCreatureDied(evt); + }); + } + PlayerRepository(const NetworkHandlerWrapper& networkHandler, const PlayerFactory& factory, const uint16_t radius) : m_NetworkHandler(networkHandler), m_Factory(factory), @@ -57,6 +64,18 @@ namespace Interlude PlayerRepository() = delete; virtual ~PlayerRepository() = default; + void OnCreatureDied(const Events::Event& evt) + { + std::shared_lock(m_Mutex); + if (evt.GetName() == Events::CreatureDiedEvent::name) + { + const auto casted = static_cast(evt); + if (m_Players.find(casted.GetCreatureId()) != m_Players.end()) { + m_Players[casted.GetCreatureId()]->MarkAsDead(); + } + } + } + private: const PlayerFactory& m_Factory; const NetworkHandlerWrapper& m_NetworkHandler;