Organize Entity header

Probably the third or fourth pass of this darn header...  Just keep making it better every time
Rename some functions to make more sense to a reader
Use different method for Observing/subscribing to component events
Get rid of abomination of overloading GetParentUser
This commit is contained in:
David Markowitz 2023-06-16 01:56:02 -07:00
parent 92006123b8
commit 891648288a
23 changed files with 126 additions and 102 deletions

View File

@ -55,13 +55,13 @@ void Player::SetSystemAddress(const SystemAddress& value) {
m_SystemAddress = value;
}
void Player::SetRespawnPos(const NiPoint3& position) {
void Player::SetRespawnPosition(const NiPoint3& position) {
m_respawnPos = position;
m_Character->SetRespawnPoint(dZoneManager::Instance()->GetZone()->GetWorldID(), position);
}
void Player::SetRespawnRot(const NiQuaternion& rotation) {
void Player::SetRespawnRotation(const NiQuaternion& rotation) {
m_respawnRot = rotation;
}

View File

@ -44,9 +44,9 @@ public:
void SetSystemAddress(const SystemAddress& value) override;
void SetRespawnPos(const NiPoint3& position) override;
void SetRespawnPosition(const NiPoint3& position) override;
void SetRespawnRot(const NiQuaternion& rotation) override;
void SetRespawnRotation(const NiQuaternion& rotation) override;
void SetGhostReferencePoint(const NiPoint3& value);

View File

@ -213,7 +213,7 @@ void BasicAttackBehavior::DoBehaviorCalculation(BehaviorContext* context, RakNet
bitStream->Write(armorDamageDealt);
bitStream->Write(healthDamageDealt);
bitStream->Write(targetEntity->GetIsDead());
bitStream->Write(targetEntity->IsDead());
}
bitStream->Write(successState);

View File

@ -148,7 +148,7 @@ void TacArcBehavior::Calculate(BehaviorContext* context, RakNet::BitStream* bitS
continue;
}
if (entity->GetIsDead()) continue;
if (entity->IsDead()) continue;
const auto otherPosition = entity->GetPosition();

View File

@ -180,7 +180,7 @@ void BaseCombatAIComponent::Update(const float deltaTime) {
m_SoftTimer -= deltaTime;
}
if (m_Disabled || m_ParentEntity->GetIsDead())
if (m_Disabled || m_ParentEntity->IsDead())
return;
bool stunnedThisFrame = m_Stunned;
CalculateCombat(deltaTime); // Putting this here for now

View File

@ -692,26 +692,32 @@ void DestroyableComponent::Damage(uint32_t damage, const LWOOBJID source, uint32
Smash(source, eKillType::VIOLENT, u"", skillID);
}
void DestroyableComponent::Subscribe(LWOOBJID scriptObjId, CppScripts::Script* scriptToAdd) {
m_SubscribedScripts.insert(std::make_pair(scriptObjId, scriptToAdd));
Game::logger->LogDebug("DestroyableComponent", "Added script %llu to entity %llu", scriptObjId, m_ParentEntity->GetObjectID());
void DestroyableComponent::Subscribe(CppScripts::Script* scriptToAdd) {
auto foundScript = std::find(m_SubscribedScripts.begin(), m_SubscribedScripts.end(), scriptToAdd);
if (foundScript != m_SubscribedScripts.end()) {
Game::logger->LogDebug("DestroyableComponent", "WARNING: Tried to add a script for Entity %llu but the script was already subscribed", m_ParentEntity->GetObjectID());
return;
}
m_SubscribedScripts.push_back(scriptToAdd);
Game::logger->LogDebug("DestroyableComponent", "A script has subscribed to entity %llu", m_ParentEntity->GetObjectID());
Game::logger->LogDebug("DestroyableComponent", "Number of subscribed scripts %i", m_SubscribedScripts.size());
}
void DestroyableComponent::Unsubscribe(LWOOBJID scriptObjId) {
auto foundScript = m_SubscribedScripts.find(scriptObjId);
void DestroyableComponent::Unsubscribe(CppScripts::Script* scriptToRemove) {
auto foundScript = std::find(m_SubscribedScripts.begin(), m_SubscribedScripts.end(), scriptToRemove);
if (foundScript != m_SubscribedScripts.end()) {
m_SubscribedScripts.erase(foundScript);
Game::logger->LogDebug("DestroyableComponent", "Removed script %llu from entity %llu", scriptObjId, m_ParentEntity->GetObjectID());
} else {
Game::logger->LogDebug("DestroyableComponent", "Tried to remove a script for Entity %llu but script %llu didnt exist", m_ParentEntity->GetObjectID(), scriptObjId);
Game::logger->LogDebug("DestroyableComponent", "Unsubscribed a script from entity %llu", m_ParentEntity->GetObjectID());
Game::logger->LogDebug("DestroyableComponent", "Number of subscribed scripts %i", m_SubscribedScripts.size());
return;
}
Game::logger->LogDebug("DestroyableComponent", "Number of subscribed scripts %i", m_SubscribedScripts.size());
Game::logger->LogDebug("DestroyableComponent", "WARNING: Tried to remove a script for Entity %llu but the script was not subscribed", m_ParentEntity->GetObjectID());
}
void DestroyableComponent::NotifySubscribers(Entity* attacker, uint32_t damage) {
for (auto script : m_SubscribedScripts) {
script.second->NotifyHitOrHealResult(m_ParentEntity, attacker, damage);
for (auto* script : m_SubscribedScripts) {
DluAssert(script != nullptr);
script->NotifyHitOrHealResult(m_ParentEntity, attacker, damage);
}
}
@ -827,7 +833,7 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType
std::vector<Entity*> scriptedActs = EntityManager::Instance()->GetEntitiesByComponent(eReplicaComponentType::SCRIPTED_ACTIVITY);
for (Entity* scriptEntity : scriptedActs) {
if (scriptEntity->GetObjectID() != zoneControl->GetObjectID()) { // Don't want to trigger twice on instance worlds
scriptEntity->GetScript()->OnPlayerDied(scriptEntity, m_ParentEntity);
scriptEntity->GetScript()->OnPlayerDied(scriptEntity, m_ParentEntity);
}
}
}

View File

@ -447,8 +447,8 @@ public:
*/
void NotifySubscribers(Entity* attacker, uint32_t damage);
void Subscribe(LWOOBJID scriptObjId, CppScripts::Script* scriptToAdd);
void Unsubscribe(LWOOBJID scriptObjId);
void Subscribe(CppScripts::Script* scriptToAdd);
void Unsubscribe(CppScripts::Script* scriptToRemove);
// handle hardcode mode drops
void DoHardcoreModeDrops(const LWOOBJID source);
@ -587,9 +587,9 @@ private:
std::vector<std::function<void(Entity*)>> m_OnHitCallbacks;
/**
* The list of scripts subscribed to this components actions
* Scripts that are subscribed to this component
*/
std::map<LWOOBJID, CppScripts::Script*> m_SubscribedScripts;
std::vector<CppScripts::Script*> m_SubscribedScripts;
/**
* status immunity counters

View File

@ -366,8 +366,8 @@ void PhantomPhysicsComponent::Update(float deltaTime) {
if (entity) {
GameMessages::SendPlayerReachedRespawnCheckpoint(entity, m_RespawnPos, m_RespawnRot);
entity->SetRespawnPos(m_RespawnPos);
entity->SetRespawnRot(m_RespawnRot);
entity->SetRespawnPosition(m_RespawnPos);
entity->SetRespawnRotation(m_RespawnRot);
}
}
}

View File

@ -569,31 +569,26 @@ bool Entity::operator!=(const Entity& other) const {
return !(other.m_ObjectID == m_ObjectID);
}
// Move to header
User* Entity::GetParentUser() const {
return IsPlayer() ? static_cast<const Player*>(this)->GetParentUser() : nullptr;
}
// Move to header
bool Entity::HasComponent(const eReplicaComponentType componentId) const {
return m_Components.find(componentId) != m_Components.end();
}
// Fine
void Entity::Subscribe(const LWOOBJID& scriptObjId, CppScripts::Script* scriptToAdd, const std::string& notificationName) {
void Entity::Subscribe(CppScripts::Script* scriptToAdd, const std::string& notificationName) {
Game::logger->Log("Entity", "Subscribing a script with notification %s", notificationName.c_str());
if (notificationName == "HitOrHealResult" || notificationName == "Hit") {
auto* destroyableComponent = GetComponent<DestroyableComponent>();
if (!destroyableComponent) return;
destroyableComponent->Subscribe(scriptObjId, scriptToAdd);
if (destroyableComponent) destroyableComponent->Subscribe(scriptToAdd);
}
}
// Fine
void Entity::Unsubscribe(const LWOOBJID& scriptObjId, const std::string& notificationName) {
void Entity::Unsubscribe(CppScripts::Script* scriptToRemove, const std::string& notificationName) {
Game::logger->Log("Entity", "Unsubscribing a script with notification %s", notificationName.c_str());
if (notificationName == "HitOrHealResult" || notificationName == "Hit") {
auto* destroyableComponent = GetComponent<DestroyableComponent>();
if (!destroyableComponent) return;
destroyableComponent->Unsubscribe(scriptObjId);
if (destroyableComponent) destroyableComponent->Unsubscribe(scriptToRemove);
}
}
@ -857,7 +852,7 @@ void Entity::OnCollisionPhantom(const LWOOBJID otherEntity) {
}
}
if (!other->GetIsDead()) {
if (!other->IsDead()) {
auto* combat = GetComponent<BaseCombatAIComponent>();
if (combat != nullptr) {
@ -1053,7 +1048,7 @@ void Entity::AddRebuildCompleteCallback(const std::function<void(Entity* user)>&
if (quickBuildComponent) quickBuildComponent->AddRebuildCompleteCallback(callback);
}
bool Entity::GetIsDead() const {
bool Entity::IsDead() const {
auto* dest = GetComponent<DestroyableComponent>();
return dest && dest->GetArmor() == 0 && dest->GetHealth() == 0;
}

View File

@ -19,6 +19,10 @@ namespace tinyxml2 {
class XMLDocument;
};
namespace CppScripts {
class Script;
};
class Player;
class EntityInfo;
class User;
@ -37,10 +41,6 @@ enum class eReplicaComponentType : uint32_t;
enum class eReplicaPacketType : uint8_t;
enum class eCinematicEvent : uint32_t;
namespace CppScripts {
class Script;
};
/**
* An entity in the world.
* Entities are composed of components which define their behavior.
@ -58,8 +58,15 @@ public:
void ApplyComponentWhitelist(TemplateComponents& components) const;
static const std::vector<ComponentWhitelist>& GetComponentWhitelists() { return m_ComponentWhitelists; }
// There are some very very edge cases we need to take care of with what components
// are kept and removed. Here is where we take care of those cases.
/**
* Functions used for creating and setting up an Entity.
*/
void Initialize();
/**
* There are some very very edge cases we need to take care of with what components
* are kept and removed. Here is where we take care of those cases.
*/
void ApplyComponentBlacklist(TemplateComponents& components) const;
// For adding and removing components based on LDF keys
@ -68,84 +75,100 @@ public:
// Paths have several components they could add. This function will add them.
void AddPathComponent(TemplateComponents& components) const;
/**
* Determines if we should ghost an Entity or not.
* Ghosting means we no longer serialize it to a specific player because it is out of range.
*/
void IsGhosted();
virtual void Initialize();
bool operator==(const Entity& other) const;
bool operator!=(const Entity& other) const;
/**
* Getters
*/
Character* GetCharacter() const { return m_Character; }
void SetCharacter(Character* value) { m_Character = value; }
// General Entity info
const LWOOBJID GetObjectID() const { return m_ObjectID; }
const LOT GetLOT() const { return m_TemplateID; }
eGameMasterLevel GetGMLevel() const { return m_GMLevel; }
void SetGMLevel(const eGameMasterLevel value);
Entity* GetParentEntity() const { return m_ParentEntity; }
const std::vector<std::string>& GetGroups() { return m_Groups; };
void SetGroups(const std::vector<std::string>& value) { m_Groups = value; }
Spawner* GetSpawner() const { return m_Spawner; }
LWOOBJID GetSpawnerID() const { return m_SpawnerID; }
const std::vector<LDFBaseData*>& GetSettings() const { return m_Settings; }
const std::vector<LDFBaseData*>& GetNetworkSettings() const { return m_NetworkSettings; }
bool GetIsDead() const;
bool GetPlayerReadyForUpdates() const { return m_PlayerIsReadyForUpdates; }
void SetPlayerReadyForUpdates() { m_PlayerIsReadyForUpdates = true; }
const bool GetIsGhostingCandidate() const { return m_IsGhostingCandidate; }
const int8_t GetObservers() const { return m_Observers; }
void SetObservers(const int8_t value);
const uint16_t GetNetworkId() const { return m_NetworkID; }
void SetNetworkId(const uint16_t id) { m_NetworkID = id; }
const float GetDefaultScale() const { return m_Scale; }
Entity* GetOwner() const;
void SetOwnerOverride(const LWOOBJID& value) { m_OwnerOverride = value; };
// Position related info
const NiPoint3& GetDefaultPosition() const { return m_DefaultPosition; };
const NiQuaternion& GetDefaultRotation() const { return m_DefaultRotation; };
const float GetDefaultScale() const { return m_Scale; }
const NiPoint3& GetPosition() const;
void SetPosition(const NiPoint3& position);
const NiQuaternion& GetRotation() const;
void SetRotation(const NiQuaternion& rotation);
virtual User* GetParentUser() const;
// Spawner related info
Spawner* GetSpawner() const { return m_Spawner; }
LWOOBJID GetSpawnerID() const { return m_SpawnerID; }
const std::vector<std::string>& GetGroups() { return m_Groups; };
void SetGroups(const std::vector<std::string>& value) { m_Groups = value; }
// LDF related into
const std::vector<LDFBaseData*>& GetSettings() const { return m_Settings; }
const std::vector<LDFBaseData*>& GetNetworkSettings() const { return m_NetworkSettings; }
// Networking related info
const int8_t GetObservers() const { return m_Observers; }
void SetObservers(const int8_t value);
const uint16_t GetNetworkId() const { return m_NetworkID; }
void SetNetworkId(const uint16_t id) { m_NetworkID = id; }
// Player extended info
virtual User* GetParentUser() const { return nullptr; };
virtual const SystemAddress GetSystemAddress() const { return UNASSIGNED_SYSTEM_ADDRESS; };
virtual void SetRespawnPos(const NiPoint3& position) {};
virtual void SetRespawnPosition(const NiPoint3& position) {};
virtual void SetRespawnRot(const NiQuaternion& rotation) {};
virtual void SetRespawnRotation(const NiQuaternion& rotation) {};
virtual void SetSystemAddress(const SystemAddress& value) {};
/**
* Component management
*/
eGameMasterLevel GetGMLevel() const { return m_GMLevel; }
void SetGMLevel(const eGameMasterLevel value);
bool GetPlayerReadyForUpdates() const { return m_PlayerIsReadyForUpdates; }
void SetPlayerReadyForUpdates() { m_PlayerIsReadyForUpdates = true; }
Character* GetCharacter() const { return m_Character; }
void SetCharacter(Character* value) { m_Character = value; }
// End info
bool IsDead() const;
// If you are calling this, then calling GetComponent<T>, just call GetComponent<T> and check for nullptr.
bool HasComponent(const eReplicaComponentType componentId) const;
void Subscribe(const LWOOBJID& scriptObjId, CppScripts::Script* scriptToAdd, const std::string& notificationName);
void Unsubscribe(const LWOOBJID& scriptObjId, const std::string& notificationName);
/**
* Call these when you want to observe events. Observed events should follow the following naming convention
* in scripts Notify<NotificationName>. For example, if you want to observe the "OnDeath" event, you would call
* entity->Subscribe(script, "OnDeath"). Then in your script, you would have a function called NotifyOnDeath.
*/
void Subscribe(CppScripts::Script* scriptToAdd, const std::string& notificationName);
/**
* Call this when you want to stop observing an event. The scriptToRemove will
* no longer be notified of notificationName events
*/
void Unsubscribe(CppScripts::Script* scriptToRemove, const std::string& notificationName);
// ### STOPPED HERE ###
void SetProximityRadius(const float proxRadius, const std::string& name);
void SetProximityRadius(dpEntity* entity, const std::string& name);

View File

@ -143,7 +143,7 @@ void GameMessageHandler::HandleMessage(RakNet::BitStream* inStream, const System
}
//Kill player if health == 0
if (entity->GetIsDead()) {
if (entity->IsDead()) {
entity->Smash(entity->GetObjectID());
}

View File

@ -16,7 +16,7 @@ void BaseEnemyApe::OnStartup(Entity* self) {
void BaseEnemyApe::OnDie(Entity* self, Entity* killer) {
auto* anchor = EntityManager::Instance()->GetEntity(self->GetVar<LWOOBJID>(u"QB"));
if (anchor != nullptr && !anchor->GetIsDead()) {
if (anchor != nullptr && !anchor->IsDead()) {
anchor->Smash(self->GetObjectID(), eKillType::SILENT);
}
}

View File

@ -152,13 +152,13 @@ void AmSkullkinDrill::FreezePlayer(Entity* self, Entity* player, bool bFreeze) {
auto StateChangeType = eStateChangeType::POP;
if (bFreeze) {
if (player->GetIsDead()) {
if (player->IsDead()) {
return;
}
StateChangeType = eStateChangeType::PUSH;
} else {
if (player->GetIsDead()) {
if (player->IsDead()) {
//
}
}

View File

@ -54,7 +54,7 @@ void AgSurvivalBuffStation::OnTimerDone(Entity* self, std::string timerName) {
auto team = self->GetVar<std::vector<LWOOBJID>>(u"BuilderTeam");
for (auto memberID : team) {
auto member = EntityManager::Instance()->GetEntity(memberID);
if (member != nullptr && !member->GetIsDead()) {
if (member != nullptr && !member->IsDead()) {
GameMessages::SendDropClientLoot(member, self->GetObjectID(), powerupToDrop, 0, self->GetPosition());
} else {
// If player left the team or left early erase them from the team variable.

View File

@ -312,7 +312,7 @@ bool BaseSurvivalServer::CheckAllPlayersDead() {
for (const auto& playerID : state.players) {
auto* player = EntityManager::Instance()->GetEntity(playerID);
if (player == nullptr || player->GetIsDead()) {
if (player == nullptr || player->IsDead()) {
deadPlayers++;
}
}

View File

@ -310,7 +310,7 @@ bool BaseWavesServer::CheckAllPlayersDead() {
for (const auto& playerID : state.players) {
auto* player = EntityManager::Instance()->GetEntity(playerID);
if (player == nullptr || player->GetIsDead()) {
if (player == nullptr || player->IsDead()) {
deadPlayers++;
}
}
@ -430,7 +430,7 @@ void BaseWavesServer::SpawnWave(Entity* self) {
for (const auto& playerID : state.players) {
auto* player = EntityManager::Instance()->GetEntity(playerID);
if (player && player->GetIsDead()) {
if (player && player->IsDead()) {
player->Resurrect();
}
}
@ -500,7 +500,7 @@ bool BaseWavesServer::UpdateSpawnedEnemies(Entity* self, LWOOBJID enemyID, uint3
for (const auto& playerID : state.players) {
auto* player = EntityManager::Instance()->GetEntity(playerID);
if (player != nullptr && !player->GetIsDead()) {
if (player != nullptr && !player->IsDead()) {
SetActivityValue(self, playerID, 1, currentTime);
SetActivityValue(self, playerID, 2, state.waveNumber);

View File

@ -4,7 +4,7 @@
#include "SkillComponent.h"
void CoilBackpackBase::OnFactionTriggerItemEquipped(Entity* itemOwner, LWOOBJID itemObjId) {
itemOwner->Subscribe(itemObjId, this, "HitOrHealResult");
itemOwner->Subscribe(this, "HitOrHealResult");
itemOwner->SetVar<uint8_t>(u"coilCount", 0);
}
@ -21,5 +21,5 @@ void CoilBackpackBase::NotifyHitOrHealResult(Entity* self, Entity* attacker, int
}
void CoilBackpackBase::OnFactionTriggerItemUnequipped(Entity* itemOwner, LWOOBJID itemObjId) {
itemOwner->Unsubscribe(itemObjId, "HitOrHealResult");
itemOwner->Unsubscribe(this, "HitOrHealResult");
}

View File

@ -3,7 +3,7 @@
#include "Entity.h"
void ActPlayerDeathTrigger::OnCollisionPhantom(Entity* self, Entity* target) {
if (!target->IsPlayer() || target->GetIsDead() || !target->GetPlayerReadyForUpdates()) return; //Don't kill already dead players or players not ready
if (!target->IsPlayer() || target->IsDead() || !target->GetPlayerReadyForUpdates()) return; //Don't kill already dead players or players not ready
target->Smash(self->GetObjectID(), eKillType::SILENT);
}

View File

@ -11,7 +11,7 @@ void ActSharkPlayerDeathTrigger::OnFireEventServerSide(Entity* self, Entity* sen
missionComponent->Progress(eMissionTaskType::SCRIPT, 8419);
if (sender->GetIsDead() || !sender->GetPlayerReadyForUpdates()) return; //Don't kill already dead players or players not ready
if (sender->IsDead() || !sender->GetPlayerReadyForUpdates()) return; //Don't kill already dead players or players not ready
if (sender->GetCharacter()) {
sender->Smash(self->GetObjectID(), eKillType::VIOLENT, u"big-shark-death");

View File

@ -2,7 +2,7 @@
#include "Entity.h"
void AgShipPlayerDeathTrigger::OnCollisionPhantom(Entity* self, Entity* target) {
if (target->GetLOT() == 1 && !target->GetIsDead()) {
if (target->GetLOT() == 1 && !target->IsDead()) {
target->Smash(self->GetObjectID(), eKillType::VIOLENT, u"electro-shock-death");
}
}

View File

@ -28,7 +28,7 @@ void TriggerGas::OnTimerDone(Entity* self, std::string timerName) {
if (timerName != this->m_TimerName) return;
auto players = self->GetVar<std::vector<Entity*>>(u"players");
for (auto player : players) {
if (player->GetIsDead() || !player){
if (player->IsDead() || !player){
auto position = std::find(players.begin(), players.end(), player);
if (position != players.end()) players.erase(position);
continue;

View File

@ -45,7 +45,7 @@ void PetDigBuild::OnDie(Entity* self, Entity* killer) {
return;
// If the quick build expired and the treasure was not collected, hide the treasure
if (!treasure->GetIsDead()) {
if (!treasure->IsDead()) {
treasure->Smash(self->GetObjectID(), eKillType::SILENT);
}
}

View File

@ -27,7 +27,7 @@ void NsConcertInstrument::OnRebuildNotifyState(Entity* self, eRebuildState state
}
void NsConcertInstrument::OnRebuildComplete(Entity* self, Entity* target) {
if (!target->GetIsDead()) {
if (!target->IsDead()) {
self->SetVar<LWOOBJID>(u"activePlayer", target->GetObjectID());
self->AddCallbackTimer(0.2f, [self, target]() {