1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 17:19:46 +02:00

Convert nodes to unique ptrs

This commit is contained in:
Filip Gawin 2018-09-08 01:22:18 +02:00 committed by Daniel Evans
parent 3821244c95
commit 94ffd77743
6 changed files with 28 additions and 31 deletions

View File

@ -11,36 +11,32 @@
#include <rw/debug.hpp>
#include <rw/types.hpp>
AIGraph::~AIGraph() {
for (auto n : nodes) {
delete n;
}
}
void AIGraph::createPathNodes(const glm::vec3& position,
const glm::quat& rotation, PathData& path) {
auto startIndex = static_cast<std::uint32_t>(nodes.size());
std::vector<AIGraphNode*> pathNodes;
pathNodes.reserve(path.nodes.size());
nodes.reserve(path.nodes.size());
for (auto n = 0u; n < path.nodes.size(); ++n) {
bool external = false;
auto& node = path.nodes[n];
AIGraphNode* ainode = nullptr;
glm::vec3 nodePosition = position + (rotation * node.position);
if (node.type == PathNode::EXTERNAL) {
for (auto &realNode : externalNodes) {
for (auto& realNode : externalNodes) {
auto d = glm::distance2(realNode->position, nodePosition);
if (d < 1.f) {
pathNodes.push_back(realNode);
ainode = realNode;
external = true;
break;
}
}
}
if (!external) {
auto ainode = std::make_unique<AIGraphNode>();
auto ptr = ainode.get();
if (ainode == nullptr) {
ainode = new AIGraphNode;
ainode->type =
(path.type == PathData::PATH_PED ? AIGraphNode::Pedestrian
: AIGraphNode::Vehicle);
@ -53,15 +49,15 @@ void AIGraph::createPathNodes(const glm::vec3& position,
ainode->external = node.type == PathNode::EXTERNAL;
ainode->disabled = false;
pathNodes.push_back(ainode);
nodes.push_back(ainode);
pathNodes.push_back(ptr);
nodes.push_back(std::move(ainode));
if (ainode->external) {
externalNodes.push_back(ainode);
if (ptr->external) {
externalNodes.push_back(ptr);
// Determine which grid cell this node falls into
float lowerCoord = -(WORLD_GRID_SIZE) / 2.f;
auto gridrel = glm::vec2(ainode->position) -
auto gridrel = glm::vec2(ptr->position) -
glm::vec2(lowerCoord, lowerCoord);
auto gridcoord =
glm::floor(gridrel / glm::vec2(WORLD_CELL_SIZE));
@ -69,15 +65,16 @@ void AIGraph::createPathNodes(const glm::vec3& position,
gridcoord.x >= WORLD_GRID_WIDTH ||
gridcoord.y >= WORLD_GRID_WIDTH) {
RW_MESSAGE("Warning: Node outside of grid at coord "
<< gridcoord.x << " " << gridcoord.y);
<< gridcoord.x << " " << gridcoord.y);
}
auto index = static_cast<std::size_t>((gridcoord.x * WORLD_GRID_WIDTH) + gridcoord.y);
gridNodes[index].push_back(ainode);
auto index = static_cast<std::size_t>(
(gridcoord.x * WORLD_GRID_WIDTH) + gridcoord.y);
gridNodes[index].push_back(ptr);
}
}
}
for (size_t pn = 0; pn < path.nodes.size(); ++pn) {
for (auto pn = 0u; pn < path.nodes.size(); ++pn) {
if (path.nodes[pn].next >= 0 &&
static_cast<unsigned>(path.nodes[pn].next) < pathNodes.size()) {
auto node = pathNodes[pn];
@ -114,7 +111,7 @@ void AIGraph::gatherExternalNodesNear(const glm::vec3& center,
}
auto& external = gridNodes[i];
copy_if(external.begin(), external.end(), back_inserter(nodes),
[&center, &radius, &type](const AIGraphNode* node) {
[&center, &radius, &type](const auto node) {
return node->type == type &&
glm::distance2(center, node->position) <
radius * radius;

View File

@ -14,9 +14,9 @@ struct PathData;
class AIGraph {
public:
~AIGraph();
~AIGraph() = default;
std::vector<AIGraphNode*> nodes;
std::vector<std::unique_ptr<AIGraphNode>> nodes;
/**
* List of external nodes, which are links between each

View File

@ -89,7 +89,7 @@ void DefaultAIController::update(float dt) {
float d = glm::distance(n->position,
getCharacter()->getPosition());
if (d < mindist) {
node = n;
node = n.get();
mindist = d;
}
}
@ -199,7 +199,7 @@ void DefaultAIController::update(float dt) {
getCharacter()->getCurrentVehicle()->getPosition());
if (d < mindist) {
node = n;
node = n.get();
mindist = d;
}
}

View File

@ -829,7 +829,7 @@ void GameWorld::loadSpecialModel(const unsigned short index,
void GameWorld::disableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
const glm::vec3& max) {
for (AIGraphNode* n : aigraph.nodes) {
for (auto& n : aigraph.nodes) {
if (n->type == type) {
if (n->position.x >= min.x && n->position.y >= min.y &&
n->position.z >= min.z && n->position.x <= max.x &&
@ -842,7 +842,7 @@ void GameWorld::disableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
void GameWorld::enableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
const glm::vec3& max) {
for (AIGraphNode* n : aigraph.nodes) {
for (auto& n : aigraph.nodes) {
if (n->type == type) {
if (n->position.x >= min.x && n->position.y >= min.y &&
n->position.z >= min.z && n->position.x <= max.x &&

View File

@ -594,7 +594,7 @@ void CharacterObject::setJumpSpeed(float speed) {
}
void CharacterObject::resetToAINode() {
auto nodes = engine->aigraph.nodes;
auto& nodes = engine->aigraph.nodes;
bool vehicleNode = !!getCurrentVehicle();
AIGraphNode* nearest = nullptr;
float d = std::numeric_limits<float>::max();
@ -607,7 +607,7 @@ void CharacterObject::resetToAINode() {
float dist = glm::length(node->position - getPosition());
if (dist < d) {
nearest = node;
nearest = node.get();
d = dist;
}
}

View File

@ -721,7 +721,7 @@ void RWGame::renderDebugPaths(float time) {
btVector3 roadColour(1.f, 0.f, 0.f);
btVector3 pedColour(0.f, 0.f, 1.f);
for (AIGraphNode* n : world->aigraph.nodes) {
for (const auto& n : world->aigraph.nodes) {
btVector3 p(n->position.x, n->position.y, n->position.z);
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
debug.drawLine(p - btVector3(0.f, 0.f, 1.f),
@ -731,7 +731,7 @@ void RWGame::renderDebugPaths(float time) {
debug.drawLine(p - btVector3(0.f, 1.f, 0.f),
p + btVector3(0.f, 1.f, 0.f), col);
for (AIGraphNode* c : n->connections) {
for (const auto& c : n->connections) {
btVector3 f(c->position.x, c->position.y, c->position.z);
debug.drawLine(p, f, col);
}