Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Remove dpEntity pointers from collision checking #1529

Merged
merged 9 commits into from
Apr 5, 2024
8 changes: 4 additions & 4 deletions dGame/dComponents/BaseCombatAIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ void BaseCombatAIComponent::Update(const float deltaTime) {
m_dpEntityEnemy->SetPosition(m_Parent->GetPosition());

//Process enter events
for (auto en : m_dpEntity->GetNewObjects()) {
m_Parent->OnCollisionPhantom(en->GetObjectID());
for (const auto id : m_dpEntity->GetNewObjects()) {
m_Parent->OnCollisionPhantom(id);
}

//Process exit events
for (auto en : m_dpEntity->GetRemovedObjects()) {
m_Parent->OnCollisionLeavePhantom(en->GetObjectID());
for (const auto id : m_dpEntity->GetRemovedObjects()) {
m_Parent->OnCollisionLeavePhantom(id);
}

// Check if we should stop the tether effect
Expand Down
18 changes: 8 additions & 10 deletions dGame/dComponents/PhantomPhysicsComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ PhantomPhysicsComponent::PhantomPhysicsComponent(Entity* parent) : PhysicsCompon
//add fallback cube:
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 2.0f, 2.0f, 2.0f);
}

m_dpEntity->SetScale(m_Scale);
m_dpEntity->SetRotation(m_Rotation);
m_dpEntity->SetPosition(m_Position);
Expand Down Expand Up @@ -323,14 +323,13 @@ void PhantomPhysicsComponent::Update(float deltaTime) {
if (!m_dpEntity) return;

//Process enter events
for (auto en : m_dpEntity->GetNewObjects()) {
if (!en) continue;
ApplyCollisionEffect(en->GetObjectID(), m_EffectType, m_DirectionalMultiplier);
m_Parent->OnCollisionPhantom(en->GetObjectID());
for (const auto id : m_dpEntity->GetNewObjects()) {
ApplyCollisionEffect(id, m_EffectType, m_DirectionalMultiplier);
m_Parent->OnCollisionPhantom(id);

//If we are a respawn volume, inform the client:
if (m_IsRespawnVolume) {
auto entity = Game::entityManager->GetEntity(en->GetObjectID());
auto* const entity = Game::entityManager->GetEntity(id);

if (entity) {
GameMessages::SendPlayerReachedRespawnCheckpoint(entity, m_RespawnPos, m_RespawnRot);
Expand All @@ -341,10 +340,9 @@ void PhantomPhysicsComponent::Update(float deltaTime) {
}

//Process exit events
for (auto en : m_dpEntity->GetRemovedObjects()) {
if (!en) continue;
ApplyCollisionEffect(en->GetObjectID(), m_EffectType, 1.0f);
m_Parent->OnCollisionLeavePhantom(en->GetObjectID());
for (const auto id : m_dpEntity->GetRemovedObjects()) {
ApplyCollisionEffect(id, m_EffectType, 1.0f);
m_Parent->OnCollisionLeavePhantom(id);
}
}

Expand Down
26 changes: 13 additions & 13 deletions dGame/dComponents/ProximityMonitorComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "EntityManager.h"
#include "SimplePhysicsComponent.h"

const std::map<LWOOBJID, dpEntity*> ProximityMonitorComponent::m_EmptyObjectMap = {};
const std::unordered_set<LWOOBJID> ProximityMonitorComponent::m_EmptyObjectSet = {};

ProximityMonitorComponent::ProximityMonitorComponent(Entity* parent, int radiusSmall, int radiusLarge) : Component(parent) {
if (radiusSmall != -1 && radiusLarge != -1) {
Expand Down Expand Up @@ -38,26 +38,26 @@ void ProximityMonitorComponent::SetProximityRadius(dpEntity* entity, const std::
m_ProximitiesData.insert(std::make_pair(name, entity));
}

const std::map<LWOOBJID, dpEntity*>& ProximityMonitorComponent::GetProximityObjects(const std::string& name) {
const auto& iter = m_ProximitiesData.find(name);
const std::unordered_set<LWOOBJID>& ProximityMonitorComponent::GetProximityObjects(const std::string& name) {
const auto iter = m_ProximitiesData.find(name);

if (iter == m_ProximitiesData.end()) {
return m_EmptyObjectMap;
if (iter == m_ProximitiesData.cend()) {
return m_EmptyObjectSet;
}

return iter->second->GetCurrentlyCollidingObjects();
}

bool ProximityMonitorComponent::IsInProximity(const std::string& name, LWOOBJID objectID) {
const auto& iter = m_ProximitiesData.find(name);
const auto iter = m_ProximitiesData.find(name);

if (iter == m_ProximitiesData.end()) {
if (iter == m_ProximitiesData.cend()) {
return false;
}

const auto& collitions = iter->second->GetCurrentlyCollidingObjects();
const auto& collisions = iter->second->GetCurrentlyCollidingObjects();

return collitions.find(objectID) != collitions.end();
return collisions.contains(objectID);
}

void ProximityMonitorComponent::Update(float deltaTime) {
Expand All @@ -66,13 +66,13 @@ void ProximityMonitorComponent::Update(float deltaTime) {

prox.second->SetPosition(m_Parent->GetPosition());
//Process enter events
for (auto* en : prox.second->GetNewObjects()) {
m_Parent->OnCollisionProximity(en->GetObjectID(), prox.first, "ENTER");
for (const auto id : prox.second->GetNewObjects()) {
m_Parent->OnCollisionProximity(id, prox.first, "ENTER");
}

//Process exit events
for (auto* en : prox.second->GetRemovedObjects()) {
m_Parent->OnCollisionProximity(en->GetObjectID(), prox.first, "LEAVE");
for (const auto id : prox.second->GetRemovedObjects()) {
m_Parent->OnCollisionProximity(id, prox.first, "LEAVE");
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions dGame/dComponents/ProximityMonitorComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef PROXIMITYMONITORCOMPONENT_H
#define PROXIMITYMONITORCOMPONENT_H

#include <unordered_set>

#include "BitStream.h"
#include "Entity.h"
#include "dpWorld.h"
Expand Down Expand Up @@ -42,9 +44,9 @@ class ProximityMonitorComponent final : public Component {
/**
* Returns the last of entities that are used to check proximity, given a name
* @param name the proximity name to retrieve physics objects for
* @return a map of physics entities for this name, indexed by object ID
* @return a set of physics entity object IDs for this name
*/
const std::map<LWOOBJID, dpEntity*>& GetProximityObjects(const std::string& name);
const std::unordered_set<LWOOBJID>& GetProximityObjects(const std::string& name);

/**
* Checks if the passed object is in proximity of the named proximity sensor
Expand All @@ -70,7 +72,7 @@ class ProximityMonitorComponent final : public Component {
/**
* Default value for the proximity data
*/
static const std::map<LWOOBJID, dpEntity*> m_EmptyObjectMap;
static const std::unordered_set<LWOOBJID> m_EmptyObjectSet;
};

#endif // PROXIMITYMONITORCOMPONENT_H
17 changes: 8 additions & 9 deletions dPhysics/dpEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "dpShapeBox.h"
#include "dpGrid.h"

#include <iostream>

dpEntity::dpEntity(const LWOOBJID& objectID, dpShapeType shapeType, bool isStatic) {
m_ObjectID = objectID;
m_IsStatic = isStatic;
Expand Down Expand Up @@ -76,16 +74,17 @@ void dpEntity::CheckCollision(dpEntity* other) {
return;
}

bool wasFound = m_CurrentlyCollidingObjects.contains(other->GetObjectID());

bool isColliding = m_CollisionShape->IsColliding(other->GetShape());
const auto objId = other->GetObjectID();
const auto objItr = m_CurrentlyCollidingObjects.find(objId);
const bool wasFound = objItr != m_CurrentlyCollidingObjects.cend();
const bool isColliding = m_CollisionShape->IsColliding(other->GetShape());

if (isColliding && !wasFound) {
m_CurrentlyCollidingObjects.emplace(other->GetObjectID(), other);
m_NewObjects.push_back(other);
m_CurrentlyCollidingObjects.emplace(objId);
m_NewObjects.push_back(objId);
} else if (!isColliding && wasFound) {
m_CurrentlyCollidingObjects.erase(other->GetObjectID());
m_RemovedObjects.push_back(other);
m_CurrentlyCollidingObjects.erase(objItr);
m_RemovedObjects.push_back(objId);
}
}

Expand Down
15 changes: 8 additions & 7 deletions dPhysics/dpEntity.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "NiPoint3.h"
#include "NiQuaternion.h"
#include <vector>
#include <map>
#include <unordered_set>
#include <span>

#include "dCommonVars.h"
#include "dpCommon.h"
Expand Down Expand Up @@ -49,9 +50,9 @@ class dpEntity {
bool GetSleeping() const { return m_Sleeping; }
void SetSleeping(bool value) { m_Sleeping = value; }

const std::vector<dpEntity*>& GetNewObjects() const { return m_NewObjects; }
const std::vector<dpEntity*>& GetRemovedObjects() const { return m_RemovedObjects; }
const std::map<LWOOBJID, dpEntity*>& GetCurrentlyCollidingObjects() const { return m_CurrentlyCollidingObjects; }
const std::span<const LWOOBJID> GetNewObjects() const { return m_NewObjects; }
const std::span<const LWOOBJID> GetRemovedObjects() const { return m_RemovedObjects; }
const std::unordered_set<LWOOBJID>& GetCurrentlyCollidingObjects() const { return m_CurrentlyCollidingObjects; }

void PreUpdate() { m_NewObjects.clear(); m_RemovedObjects.clear(); }

Expand Down Expand Up @@ -80,7 +81,7 @@ class dpEntity {

bool m_IsGargantuan = false;

std::vector<dpEntity*> m_NewObjects;
std::vector<dpEntity*> m_RemovedObjects;
std::map<LWOOBJID, dpEntity*> m_CurrentlyCollidingObjects;
std::vector<LWOOBJID> m_NewObjects;
std::vector<LWOOBJID> m_RemovedObjects;
std::unordered_set<LWOOBJID> m_CurrentlyCollidingObjects;
};
2 changes: 1 addition & 1 deletion dScripts/02_server/Map/AM/WanderingVendor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void WanderingVendor::OnProximityUpdate(Entity* self, Entity* entering, std::str

const auto proxObjs = proximityMonitorComponent->GetProximityObjects("playermonitor");
bool foundPlayer = false;
for (const auto id : proxObjs | std::views::keys) {
for (const auto id : proxObjs) {
auto* entity = Game::entityManager->GetEntity(id);
if (entity && entity->IsPlayer()) {
foundPlayer = true;
Expand Down
8 changes: 4 additions & 4 deletions dScripts/ai/AG/AgBusDoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ void AgBusDoor::OnProximityUpdate(Entity* self, Entity* entering, std::string na
m_Counter = 0;
m_OuterCounter = 0;

for (const auto& pair : proximityMonitorComponent->GetProximityObjects("busDoor")) {
auto* entity = Game::entityManager->GetEntity(pair.first);
for (const auto id : proximityMonitorComponent->GetProximityObjects("busDoor")) {
const auto* const entity = Game::entityManager->GetEntity(id);
if (entity != nullptr && entity->IsPlayer()) m_Counter++;
}

for (const auto& pair : proximityMonitorComponent->GetProximityObjects("busDoorOuter")) {
auto* entity = Game::entityManager->GetEntity(pair.first);
for (const auto id : proximityMonitorComponent->GetProximityObjects("busDoorOuter")) {
const auto* const entity = Game::entityManager->GetEntity(id);
if (entity != nullptr && entity->IsPlayer()) m_OuterCounter++;
}

Expand Down
Loading