Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rubenwardy
Created February 3, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubenwardy/77b3c5651415a050143bff3b6bc31446 to your computer and use it in GitHub Desktop.
Save rubenwardy/77b3c5651415a050143bff3b6bc31446 to your computer and use it in GitHub Desktop.
#include "tests.hpp"
#include "../world//AStarPathfinder.hpp"
namespace {
void initWorld(World &world, content::DefMan &def) {
{
WorldChunk *chunk = world.getOrCreateChunk({0, 0, 0});
for (auto &cid : chunk->terrain) {
cid = def.getCidFromName("grass");
}
chunk->tiles[2 + 4 * CHUNK_SIZE].cid = def.getCidFromName("wall");
chunk->tiles[3 + 4 * CHUNK_SIZE].cid = def.getCidFromName("wall");
chunk->tiles[4 + 4 * CHUNK_SIZE].cid = def.getCidFromName("wall");
}
{
WorldChunk *chunk = world.getOrCreateChunk({0, 0, 1});
chunk->terrain[2 + 4 * CHUNK_SIZE] = def.getCidFromName("grass");
chunk->terrain[3 + 4 * CHUNK_SIZE] = def.getCidFromName("grass");
chunk->terrain[4 + 4 * CHUNK_SIZE] = def.getCidFromName("grass");
}
{
WorldChunk *chunk = world.getOrCreateChunk({1, 0, 0});
for (auto &cid : chunk->terrain) {
cid = def.getCidFromName("grass");
}
for (int y = 0; y < 16; y++) {
for (int x = 8; x < 16; x++) {
chunk->tiles[x + y * CHUNK_SIZE].cid =
def.getCidFromName("dirt");
}
}
}
{
WorldChunk *chunk = world.getOrCreateChunk({1, 0, 1});
for (int y = 0; y < 16; y++) {
for (int x = 8; x < 16; x++) {
chunk->terrain[x + y * CHUNK_SIZE] =
def.getCidFromName("grass");
}
}
}
}
} // namespace
Test(AStarPathfinderStraightLineTest) {
content::DefMan def{};
def.defineDefault();
World world(&def);
initWorld(world, def);
AStarPathfinder pathfinder(&world);
std::vector<V3s> path;
TEST(pathfinder.findPath(path, {3, 3, 0}, {6, 3, 0}));
std::vector<V3s> expected = {{3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {6, 3, 0}};
TEST(path.size() == expected.size());
for (int i = 0; i < path.size(); i++) {
TEST(path[i] == expected[i]);
}
return true;
}
Test(AStarPathfinderObstacleTest) {
content::DefMan def{};
def.defineDefault();
World world(&def);
initWorld(world, def);
AStarPathfinder pathfinder(&world);
std::vector<V3s> path;
TEST(pathfinder.findPath(path, {3, 3, 0}, {3, 5, 0}));
std::vector<V3s> expected = {{3, 3, 0}, {4, 3, 0}, {5, 3, 0}, {5, 4, 0},
{5, 5, 0}, {4, 5, 0}, {3, 5, 0}};
TEST(path.size() == expected.size());
for (int i = 0; i < path.size(); i++) {
TEST(path[i] == expected[i]);
}
return true;
}
Test(AStarPathfinderAscendTest) {
content::DefMan def{};
def.defineDefault();
World world(&def);
initWorld(world, def);
AStarPathfinder pathfinder(&world);
std::vector<V3s> path;
TEST(pathfinder.findPath(path, {17, 3, 0}, {30, 3, 1}));
std::vector<V3s> expected = {{17, 3, 0}, {18, 3, 0}, {19, 3, 0}, {20, 3, 0},
{21, 3, 0}, {22, 3, 0}, {23, 3, 0}, {24, 3, 1}, {25, 3, 1},
{26, 3, 1}, {27, 3, 1}, {28, 3, 1}, {29, 3, 1}, {30, 3, 1}};
TEST(path.size() == expected.size());
for (int i = 0; i < path.size(); i++) {
TEST(path[i] == expected[i]);
}
return true;
}
Test(AStarPathfinderFallTest) {
content::DefMan def{};
def.defineDefault();
World world(&def);
initWorld(world, def);
AStarPathfinder pathfinder(&world);
std::vector<V3s> path;
TEST(pathfinder.findPath(path, {3, 4, 1}, {0, 4, 0}));
std::vector<V3s> expected = {{3, 4, 1}, {2, 4, 1}, {1, 4, 0}, {0, 4, 0}};
TEST(path.size() == expected.size());
for (int i = 0; i < path.size(); i++) {
TEST(path[i] == expected[i]);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment