mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-15 15:02:34 +02:00
Convert nodes to unique ptrs
This commit is contained in:
parent
3821244c95
commit
94ffd77743
@ -11,36 +11,32 @@
|
|||||||
#include <rw/debug.hpp>
|
#include <rw/debug.hpp>
|
||||||
#include <rw/types.hpp>
|
#include <rw/types.hpp>
|
||||||
|
|
||||||
AIGraph::~AIGraph() {
|
|
||||||
for (auto n : nodes) {
|
|
||||||
delete n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void AIGraph::createPathNodes(const glm::vec3& position,
|
void AIGraph::createPathNodes(const glm::vec3& position,
|
||||||
const glm::quat& rotation, PathData& path) {
|
const glm::quat& rotation, PathData& path) {
|
||||||
auto startIndex = static_cast<std::uint32_t>(nodes.size());
|
auto startIndex = static_cast<std::uint32_t>(nodes.size());
|
||||||
std::vector<AIGraphNode*> pathNodes;
|
std::vector<AIGraphNode*> pathNodes;
|
||||||
pathNodes.reserve(path.nodes.size());
|
pathNodes.reserve(path.nodes.size());
|
||||||
|
nodes.reserve(path.nodes.size());
|
||||||
|
|
||||||
for (auto n = 0u; n < path.nodes.size(); ++n) {
|
for (auto n = 0u; n < path.nodes.size(); ++n) {
|
||||||
|
bool external = false;
|
||||||
auto& node = path.nodes[n];
|
auto& node = path.nodes[n];
|
||||||
AIGraphNode* ainode = nullptr;
|
|
||||||
glm::vec3 nodePosition = position + (rotation * node.position);
|
glm::vec3 nodePosition = position + (rotation * node.position);
|
||||||
|
|
||||||
if (node.type == PathNode::EXTERNAL) {
|
if (node.type == PathNode::EXTERNAL) {
|
||||||
for (auto &realNode : externalNodes) {
|
for (auto& realNode : externalNodes) {
|
||||||
auto d = glm::distance2(realNode->position, nodePosition);
|
auto d = glm::distance2(realNode->position, nodePosition);
|
||||||
if (d < 1.f) {
|
if (d < 1.f) {
|
||||||
pathNodes.push_back(realNode);
|
pathNodes.push_back(realNode);
|
||||||
ainode = realNode;
|
external = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!external) {
|
||||||
|
auto ainode = std::make_unique<AIGraphNode>();
|
||||||
|
auto ptr = ainode.get();
|
||||||
|
|
||||||
if (ainode == nullptr) {
|
|
||||||
ainode = new AIGraphNode;
|
|
||||||
ainode->type =
|
ainode->type =
|
||||||
(path.type == PathData::PATH_PED ? AIGraphNode::Pedestrian
|
(path.type == PathData::PATH_PED ? AIGraphNode::Pedestrian
|
||||||
: AIGraphNode::Vehicle);
|
: AIGraphNode::Vehicle);
|
||||||
@ -53,15 +49,15 @@ void AIGraph::createPathNodes(const glm::vec3& position,
|
|||||||
ainode->external = node.type == PathNode::EXTERNAL;
|
ainode->external = node.type == PathNode::EXTERNAL;
|
||||||
ainode->disabled = false;
|
ainode->disabled = false;
|
||||||
|
|
||||||
pathNodes.push_back(ainode);
|
pathNodes.push_back(ptr);
|
||||||
nodes.push_back(ainode);
|
nodes.push_back(std::move(ainode));
|
||||||
|
|
||||||
if (ainode->external) {
|
if (ptr->external) {
|
||||||
externalNodes.push_back(ainode);
|
externalNodes.push_back(ptr);
|
||||||
|
|
||||||
// Determine which grid cell this node falls into
|
// Determine which grid cell this node falls into
|
||||||
float lowerCoord = -(WORLD_GRID_SIZE) / 2.f;
|
float lowerCoord = -(WORLD_GRID_SIZE) / 2.f;
|
||||||
auto gridrel = glm::vec2(ainode->position) -
|
auto gridrel = glm::vec2(ptr->position) -
|
||||||
glm::vec2(lowerCoord, lowerCoord);
|
glm::vec2(lowerCoord, lowerCoord);
|
||||||
auto gridcoord =
|
auto gridcoord =
|
||||||
glm::floor(gridrel / glm::vec2(WORLD_CELL_SIZE));
|
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.x >= WORLD_GRID_WIDTH ||
|
||||||
gridcoord.y >= WORLD_GRID_WIDTH) {
|
gridcoord.y >= WORLD_GRID_WIDTH) {
|
||||||
RW_MESSAGE("Warning: Node outside of grid at coord "
|
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);
|
auto index = static_cast<std::size_t>(
|
||||||
gridNodes[index].push_back(ainode);
|
(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 &&
|
if (path.nodes[pn].next >= 0 &&
|
||||||
static_cast<unsigned>(path.nodes[pn].next) < pathNodes.size()) {
|
static_cast<unsigned>(path.nodes[pn].next) < pathNodes.size()) {
|
||||||
auto node = pathNodes[pn];
|
auto node = pathNodes[pn];
|
||||||
@ -114,7 +111,7 @@ void AIGraph::gatherExternalNodesNear(const glm::vec3& center,
|
|||||||
}
|
}
|
||||||
auto& external = gridNodes[i];
|
auto& external = gridNodes[i];
|
||||||
copy_if(external.begin(), external.end(), back_inserter(nodes),
|
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 &&
|
return node->type == type &&
|
||||||
glm::distance2(center, node->position) <
|
glm::distance2(center, node->position) <
|
||||||
radius * radius;
|
radius * radius;
|
||||||
|
@ -14,9 +14,9 @@ struct PathData;
|
|||||||
|
|
||||||
class AIGraph {
|
class AIGraph {
|
||||||
public:
|
public:
|
||||||
~AIGraph();
|
~AIGraph() = default;
|
||||||
|
|
||||||
std::vector<AIGraphNode*> nodes;
|
std::vector<std::unique_ptr<AIGraphNode>> nodes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of external nodes, which are links between each
|
* List of external nodes, which are links between each
|
||||||
|
@ -89,7 +89,7 @@ void DefaultAIController::update(float dt) {
|
|||||||
float d = glm::distance(n->position,
|
float d = glm::distance(n->position,
|
||||||
getCharacter()->getPosition());
|
getCharacter()->getPosition());
|
||||||
if (d < mindist) {
|
if (d < mindist) {
|
||||||
node = n;
|
node = n.get();
|
||||||
mindist = d;
|
mindist = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -199,7 +199,7 @@ void DefaultAIController::update(float dt) {
|
|||||||
getCharacter()->getCurrentVehicle()->getPosition());
|
getCharacter()->getCurrentVehicle()->getPosition());
|
||||||
|
|
||||||
if (d < mindist) {
|
if (d < mindist) {
|
||||||
node = n;
|
node = n.get();
|
||||||
mindist = d;
|
mindist = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -829,7 +829,7 @@ void GameWorld::loadSpecialModel(const unsigned short index,
|
|||||||
|
|
||||||
void GameWorld::disableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
|
void GameWorld::disableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
|
||||||
const glm::vec3& max) {
|
const glm::vec3& max) {
|
||||||
for (AIGraphNode* n : aigraph.nodes) {
|
for (auto& n : aigraph.nodes) {
|
||||||
if (n->type == type) {
|
if (n->type == type) {
|
||||||
if (n->position.x >= min.x && n->position.y >= min.y &&
|
if (n->position.x >= min.x && n->position.y >= min.y &&
|
||||||
n->position.z >= min.z && n->position.x <= max.x &&
|
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,
|
void GameWorld::enableAIPaths(AIGraphNode::NodeType type, const glm::vec3& min,
|
||||||
const glm::vec3& max) {
|
const glm::vec3& max) {
|
||||||
for (AIGraphNode* n : aigraph.nodes) {
|
for (auto& n : aigraph.nodes) {
|
||||||
if (n->type == type) {
|
if (n->type == type) {
|
||||||
if (n->position.x >= min.x && n->position.y >= min.y &&
|
if (n->position.x >= min.x && n->position.y >= min.y &&
|
||||||
n->position.z >= min.z && n->position.x <= max.x &&
|
n->position.z >= min.z && n->position.x <= max.x &&
|
||||||
|
@ -594,7 +594,7 @@ void CharacterObject::setJumpSpeed(float speed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CharacterObject::resetToAINode() {
|
void CharacterObject::resetToAINode() {
|
||||||
auto nodes = engine->aigraph.nodes;
|
auto& nodes = engine->aigraph.nodes;
|
||||||
bool vehicleNode = !!getCurrentVehicle();
|
bool vehicleNode = !!getCurrentVehicle();
|
||||||
AIGraphNode* nearest = nullptr;
|
AIGraphNode* nearest = nullptr;
|
||||||
float d = std::numeric_limits<float>::max();
|
float d = std::numeric_limits<float>::max();
|
||||||
@ -607,7 +607,7 @@ void CharacterObject::resetToAINode() {
|
|||||||
|
|
||||||
float dist = glm::length(node->position - getPosition());
|
float dist = glm::length(node->position - getPosition());
|
||||||
if (dist < d) {
|
if (dist < d) {
|
||||||
nearest = node;
|
nearest = node.get();
|
||||||
d = dist;
|
d = dist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -721,7 +721,7 @@ void RWGame::renderDebugPaths(float time) {
|
|||||||
btVector3 roadColour(1.f, 0.f, 0.f);
|
btVector3 roadColour(1.f, 0.f, 0.f);
|
||||||
btVector3 pedColour(0.f, 0.f, 1.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);
|
btVector3 p(n->position.x, n->position.y, n->position.z);
|
||||||
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
|
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
|
||||||
debug.drawLine(p - btVector3(0.f, 0.f, 1.f),
|
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),
|
debug.drawLine(p - btVector3(0.f, 1.f, 0.f),
|
||||||
p + btVector3(0.f, 1.f, 0.f), col);
|
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);
|
btVector3 f(c->position.x, c->position.y, c->position.z);
|
||||||
debug.drawLine(p, f, col);
|
debug.drawLine(p, f, col);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user