From 94ffd777437d669c94d82b7c62031c8c41319407 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sat, 8 Sep 2018 01:22:18 +0200 Subject: [PATCH] Convert nodes to unique ptrs --- rwengine/src/ai/AIGraph.cpp | 39 +++++++++++------------- rwengine/src/ai/AIGraph.hpp | 4 +-- rwengine/src/ai/DefaultAIController.cpp | 4 +-- rwengine/src/engine/GameWorld.cpp | 4 +-- rwengine/src/objects/CharacterObject.cpp | 4 +-- rwgame/RWGame.cpp | 4 +-- 6 files changed, 28 insertions(+), 31 deletions(-) diff --git a/rwengine/src/ai/AIGraph.cpp b/rwengine/src/ai/AIGraph.cpp index 63f911d4..9d8debbb 100644 --- a/rwengine/src/ai/AIGraph.cpp +++ b/rwengine/src/ai/AIGraph.cpp @@ -11,36 +11,32 @@ #include #include -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(nodes.size()); std::vector 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(); + 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((gridcoord.x * WORLD_GRID_WIDTH) + gridcoord.y); - gridNodes[index].push_back(ainode); + auto index = static_cast( + (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(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), - [¢er, &radius, &type](const AIGraphNode* node) { + [¢er, &radius, &type](const auto node) { return node->type == type && glm::distance2(center, node->position) < radius * radius; diff --git a/rwengine/src/ai/AIGraph.hpp b/rwengine/src/ai/AIGraph.hpp index 9bbe4cbe..a6f13dc3 100644 --- a/rwengine/src/ai/AIGraph.hpp +++ b/rwengine/src/ai/AIGraph.hpp @@ -14,9 +14,9 @@ struct PathData; class AIGraph { public: - ~AIGraph(); + ~AIGraph() = default; - std::vector nodes; + std::vector> nodes; /** * List of external nodes, which are links between each diff --git a/rwengine/src/ai/DefaultAIController.cpp b/rwengine/src/ai/DefaultAIController.cpp index b4543093..c443fc63 100644 --- a/rwengine/src/ai/DefaultAIController.cpp +++ b/rwengine/src/ai/DefaultAIController.cpp @@ -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; } } diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index 6e734af2..ccbc7768 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -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 && diff --git a/rwengine/src/objects/CharacterObject.cpp b/rwengine/src/objects/CharacterObject.cpp index 8f962069..6ef2192e 100644 --- a/rwengine/src/objects/CharacterObject.cpp +++ b/rwengine/src/objects/CharacterObject.cpp @@ -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::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; } } diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index e0fa068e..68f6ded1 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -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); }