1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-01 16:32:31 +01:00
openrw/tests/test_TrafficDirector.cpp

170 lines
5.2 KiB
C++
Raw Normal View History

#include <boost/test/unit_test.hpp>
#include "test_Globals.hpp"
#include <ai/AIGraph.hpp>
2018-12-10 01:51:05 +01:00
#include <ai/AIGraphNode.hpp>
2016-09-09 22:13:22 +02:00
#include <ai/TrafficDirector.hpp>
2018-12-10 01:51:05 +01:00
#include <data/PathData.hpp>
#include <objects/CharacterObject.hpp>
2016-09-09 22:13:22 +02:00
#include <objects/InstanceObject.hpp>
#include <render/ViewCamera.hpp>
2018-12-10 01:51:05 +01:00
bool operator!=(const ai::AIGraphNode* lhs, const glm::vec3& rhs) {
2016-09-09 22:13:22 +02:00
return lhs->position != rhs;
}
2018-12-10 01:51:05 +01:00
std::ostream& operator<<(std::ostream& os, const ai::AIGraphNode* yt) {
2016-09-09 22:13:22 +02:00
os << glm::to_string(yt->position);
return os;
}
BOOST_AUTO_TEST_SUITE(TrafficDirectorTests, DATA_TEST_PREDICATE)
2016-09-09 22:13:22 +02:00
BOOST_AUTO_TEST_CASE(test_available_nodes) {
2018-12-10 01:51:05 +01:00
ai::AIGraph graph;
2016-09-09 22:13:22 +02:00
PathData path{PathData::PATH_PED,
0,
"",
{
{PathNode::EXTERNAL, 1, {10.f, 10.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, 2, {10.f, 0.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, 3, {10.f, -10.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, 4, {0.f, -10.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, 5, {-10.f, -10.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, 6, {-10.f, 0.f, 0.f}, 1.f, 0, 0},
{PathNode::EXTERNAL, -1, {-10.f, 10.f, 0.f}, 1.f, 0, 0},
}};
graph.createPathNodes(glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f}, path);
2016-09-09 22:13:22 +02:00
2018-12-10 01:51:05 +01:00
ai::TrafficDirector director(&graph, Global::get().e);
2016-09-09 22:13:22 +02:00
ViewCamera testCamera(glm::vec3(-5.f, -5.f, 0.f));
auto open =
2018-12-10 01:51:05 +01:00
director.findAvailableNodes(ai::NodeType::Pedestrian, testCamera, 10.f);
2016-09-09 22:13:22 +02:00
std::vector<glm::vec3> expected{
{0.f, -10.f, 0.f}, {-10.f, -10.f, 0.f}, {-10.f, 0.f, 0.f}};
BOOST_REQUIRE(expected.size() == 3);
BOOST_ASSERT(expected.size() == open.size());
for (auto& v : expected) {
2018-12-10 01:51:05 +01:00
BOOST_CHECK(std::find_if(open.begin(), open.end(), [v](ai::AIGraphNode* n) {
2016-09-09 22:13:22 +02:00
return n->position == v;
}) != open.end());
}
}
2016-09-09 22:13:22 +02:00
BOOST_AUTO_TEST_CASE(test_node_not_blocking) {
2018-12-10 01:51:05 +01:00
ai::AIGraph graph;
2016-09-09 22:13:22 +02:00
PathData path{PathData::PATH_PED,
0,
"",
{
{PathNode::EXTERNAL, 1, {10.f, 10.f, 0.f}, 1.f, 0, 0},
}};
graph.createPathNodes(glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f}, path);
2016-09-09 22:13:22 +02:00
2018-12-10 01:51:05 +01:00
ai::TrafficDirector director(&graph, Global::get().e);
2016-09-09 22:13:22 +02:00
// Create something that isn't a pedestrian
InstanceObject* box =
Global::get().e->createInstance(1337, glm::vec3(10.f, 10.f, 0.f));
{
2018-12-10 01:51:05 +01:00
auto open = director.findAvailableNodes(ai::NodeType::Pedestrian,
2016-09-09 22:13:22 +02:00
glm::vec3(5.f, 5.f, 0.f), 10.f);
BOOST_CHECK(open.size() == 1);
}
Global::get().e->destroyObject(box);
}
2016-09-09 22:13:22 +02:00
BOOST_AUTO_TEST_CASE(test_node_blocking) {
2018-12-10 01:51:05 +01:00
ai::AIGraph graph;
2016-09-09 22:13:22 +02:00
PathData path{PathData::PATH_PED,
0,
"",
{
{PathNode::EXTERNAL, 1, {10.f, 10.f, 0.f}, 1.f, 0, 0},
}};
graph.createPathNodes(glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f}, path);
2016-09-09 22:13:22 +02:00
2018-12-10 01:51:05 +01:00
ai::TrafficDirector director(&graph, Global::get().e);
2016-09-09 22:13:22 +02:00
// create something that should block the spawn point
CharacterObject* ped =
Global::get().e->createPedestrian(1, glm::vec3(10.f, 10.f, 0.f));
{
2018-12-10 01:51:05 +01:00
auto open = director.findAvailableNodes(ai::NodeType::Pedestrian,
2016-09-09 22:13:22 +02:00
glm::vec3(5.f, 5.f, 0.f), 10.f);
BOOST_CHECK(open.size() == 0);
}
Global::get().e->destroyObject(ped);
}
2016-09-09 22:13:22 +02:00
BOOST_AUTO_TEST_CASE(test_node_density) {
2018-12-10 01:51:05 +01:00
ai::AIGraph graph;
2016-09-09 22:13:22 +02:00
PathData path{PathData::PATH_PED,
0,
"",
{
{PathNode::EXTERNAL, 1, {10.f, 10.f, 0.f}, 1.f, 0, 0},
}};
graph.createPathNodes(glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f}, path);
2016-09-09 22:13:22 +02:00
2018-12-10 01:51:05 +01:00
ai::TrafficDirector director(&graph, Global::get().e);
2016-09-09 22:13:22 +02:00
CharacterObject* ped =
Global::get().e->createPedestrian(1, glm::vec3(5.f, 5.f, 0.f));
{
2018-12-10 01:51:05 +01:00
director.setDensity(ai::NodeType::Pedestrian, 1.f);
auto open = director.findAvailableNodes(ai::NodeType::Pedestrian,
2016-09-09 22:13:22 +02:00
glm::vec3(5.f, 5.f, 0.f), 10.f);
BOOST_CHECK(open.size() == 0);
}
{
2018-12-10 01:51:05 +01:00
director.setDensity(ai::NodeType::Pedestrian, 2.f);
auto open = director.findAvailableNodes(ai::NodeType::Pedestrian,
2016-09-09 22:13:22 +02:00
glm::vec3(5.f, 5.f, 0.f), 10.f);
BOOST_CHECK(open.size() == 1);
}
Global::get().e->destroyObject(ped);
}
2016-09-09 22:13:22 +02:00
BOOST_AUTO_TEST_CASE(test_create_traffic) {
2018-12-10 01:51:05 +01:00
ai::AIGraph graph;
2016-09-09 22:13:22 +02:00
PathData path{PathData::PATH_PED,
0,
"",
{
{PathNode::EXTERNAL, 1, {10.f, 10.f, 0.f}, 1.f, 0, 0},
}};
graph.createPathNodes(glm::vec3(), glm::quat{1.0f,0.0f,0.0f,0.0f}, path);
2016-09-09 22:13:22 +02:00
2018-12-10 01:51:05 +01:00
ai::TrafficDirector director(&graph, Global::get().e);
2016-09-09 22:13:22 +02:00
auto created = director.populateNearby(glm::vec3(0.f, 0.f, 0.f), 20.f);
BOOST_CHECK(created.size() == 1);
// Global::get().e->destroyObject(created[0]);
}
BOOST_AUTO_TEST_SUITE_END()