1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-26 12:22:41 +01:00

Improve TrafficDirector performance with character list

This commit is contained in:
Daniel Evans 2015-04-05 18:45:03 +01:00
parent 5b065615f3
commit 861f36bdde
3 changed files with 20 additions and 23 deletions

View File

@ -189,6 +189,8 @@ public:
*/
std::set<GameObject*> objects;
std::set<GameObject*> characters;
/**
* Map of Model Names to Instances
*/

View File

@ -4,11 +4,12 @@
#include <engine/GameWorld.hpp>
#include <engine/GameObject.hpp>
#include <objects/CharacterObject.hpp>
#include <core/Logger.hpp>
#include <glm/gtx/string_cast.hpp>
TrafficDirector::TrafficDirector(AIGraph* graph, GameWorld* world)
: graph( graph ), world( world ), pedDensity(1.f), carDensity(1.f),
TrafficDirector::TrafficDirector(AIGraph* g, GameWorld* w)
: graph( g ), world( w ), pedDensity(1.f), carDensity(1.f),
maximumPedestrians(20), maximumCars(10)
{
@ -21,22 +22,24 @@ std::vector< AIGraphNode* > TrafficDirector::findAvailableNodes(AIGraphNode::Nod
for ( AIGraphNode* node : graph->externalNodes )
{
if ( node->type != type ) continue;
if ( glm::distance( near, node->position ) < radius )
if ( glm::distance2( near, node->position ) < radius*radius )
{
available.push_back( node );
}
}
float density = type == AIGraphNode::Vehicle ? carDensity : pedDensity;
float minDist = (10.f / density) * (10.f / density);
// Determine if anything in the open set is blocked
for ( auto it = available.begin(); it != available.end(); )
{
float minDist = 10.f / density;
bool blocked = false;
for ( auto obj : world->objects )
for ( auto obj : world->characters )
{
// Sanity check
if ( obj->type() != GameObject::Character ) continue;
if ( glm::distance( (*it)->position, obj->getPosition() ) <= minDist )
if ( glm::distance2( (*it)->position, obj->getPosition() ) <= minDist )
{
blocked = true;
break;
@ -52,7 +55,7 @@ std::vector< AIGraphNode* > TrafficDirector::findAvailableNodes(AIGraphNode::Nod
it++;
}
}
return available;
}
@ -71,21 +74,7 @@ void TrafficDirector::setDensity(AIGraphNode::NodeType type, float density)
std::vector<GameObject*> TrafficDirector::populateNearby(const glm::vec3& center, float radius, int maxSpawn)
{
// TODO this should be optimised, GameWorld needs to keep seperate class lists.
int availablePeds = maximumPedestrians, availableCars = maximumCars;
for(auto& go : world->objects)
{
switch( go->type() )
{
case GameObject::Character:
availablePeds--;
break;
case GameObject::Vehicle:
availableCars--;
break;
default: break;
}
}
int availablePeds = maximumPedestrians - world->characters.size();
std::vector<GameObject*> created;

View File

@ -449,6 +449,7 @@ CharacterObject* GameWorld::createPedestrian(const uint16_t id, const glm::vec3
if(m && m->resource) {
auto ped = new CharacterObject( this, pos, rot, m, pt );
objects.insert(ped);
characters.insert(ped);
new DefaultAIController(ped);
return ped;
}
@ -463,6 +464,11 @@ void GameWorld::destroyObject(GameObject* object)
delete object;
objects.erase(iterator);
}
auto cit = characters.find(object);
if( cit != characters.end() ) {
characters.erase(cit);
}
}
void GameWorld::destroyObjectQueued(GameObject *object)