mirror of
https://github.com/bensuperpc/astar.git
synced 2024-10-18 14:13:24 +02:00
212 lines
8.5 KiB
C++
212 lines
8.5 KiB
C++
#include <array> // std::array
|
|
#include <chrono> // std::chrono::system_clock
|
|
#include <cmath> // std::abs
|
|
#include <cstdint> // std::uint32_t
|
|
#include <iostream> // std::cout, std::endl
|
|
#include <map> // std::map
|
|
#include <memory> // std::unique_ptr
|
|
#include <random> // std::random_device, std::mt19937, std::uniform_int_distribution
|
|
#include <vector> // std::vector
|
|
|
|
#include "astar/astar.hpp"
|
|
#include "generator/generator.hpp"
|
|
|
|
#include "raylib.h"
|
|
|
|
#define RAYGUI_IMPLEMENTATION
|
|
extern "C" {
|
|
#include "src/raygui.h"
|
|
}
|
|
|
|
auto main() -> int {
|
|
// Set log level for Raylib
|
|
SetTraceLogLevel(LOG_WARNING);
|
|
|
|
const int screenWidth = 1920;
|
|
const int screenHeight = 1080;
|
|
|
|
const int mapWidth = 192;
|
|
const int mapHeight = 108;
|
|
|
|
const uint32_t targetFPS = 120;
|
|
|
|
const uint32_t ImageUpdatePerSecond = 30;
|
|
|
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT);
|
|
InitWindow(screenWidth, screenHeight, "Path finder by Bensuperpc");
|
|
|
|
SetTargetFPS(targetFPS);
|
|
|
|
float lacunarity = 1.6f;
|
|
float octaves = 6;
|
|
float gain = 3.5f;
|
|
float frequency = 1.7f;
|
|
float weighted_strength = 0.034f;
|
|
float multiplier = 118;
|
|
|
|
Generator generator_2(-972960945);
|
|
generator_2.setLacunarity(lacunarity);
|
|
generator_2.setOctaves((uint32_t)octaves);
|
|
generator_2.setGain(gain);
|
|
generator_2.setFrequency(frequency);
|
|
generator_2.setWeightedStrength(0.0f);
|
|
generator_2.setMultiplier((uint32_t)multiplier);
|
|
|
|
AStar::AStar<uint32_t, true> pathFinder;
|
|
pathFinder.setWorldSize({mapWidth, mapHeight});
|
|
pathFinder.setHeuristic(AStar::Heuristic::manhattan);
|
|
pathFinder.setDiagonalMovement(true);
|
|
|
|
size_t pathSize = 0;
|
|
|
|
std::vector<uint32_t> heightmap;
|
|
|
|
heightmap = generator_2.generate2dMeightmap(0, 0, 0, mapWidth, 0, mapHeight);
|
|
|
|
std::vector<uint8_t> blocks = std::vector<uint8_t>(mapWidth * mapHeight, 0);
|
|
|
|
uint64_t framesCounter = 0;
|
|
|
|
bool needUpdate = true;
|
|
|
|
while (!WindowShouldClose()) {
|
|
framesCounter++;
|
|
if (IsKeyPressed(KEY_S)) {
|
|
const std::string filename = "screenshot_" + std::to_string(std::chrono::system_clock::now().time_since_epoch().count()) + ".png";
|
|
TakeScreenshot(filename.c_str());
|
|
}
|
|
if (IsKeyPressed(KEY_R)) {
|
|
generator_2.randomizeSeed();
|
|
needUpdate = true;
|
|
}
|
|
|
|
if (framesCounter % (targetFPS / ImageUpdatePerSecond) == 0) {
|
|
if (needUpdate) {
|
|
needUpdate = false;
|
|
generator_2.setLacunarity(lacunarity);
|
|
generator_2.setOctaves((uint32_t)octaves);
|
|
generator_2.setGain(gain);
|
|
generator_2.setFrequency(frequency);
|
|
generator_2.setWeightedStrength(weighted_strength);
|
|
generator_2.setMultiplier((uint32_t)multiplier);
|
|
|
|
pathFinder.clear();
|
|
|
|
heightmap = generator_2.generate2dMeightmap(0, 0, 0, screenWidth, 0, screenHeight);
|
|
|
|
for (uint64_t x = 0; x < mapWidth; x++) {
|
|
for (uint64_t y = 0; y < mapHeight; y++) {
|
|
uint64_t index = x + y * mapWidth;
|
|
uint8_t value = static_cast<uint8_t>(heightmap[index]);
|
|
|
|
if (value < 128) {
|
|
blocks[index] = 0;
|
|
} else {
|
|
blocks[index] = 1;
|
|
pathFinder.addObstacle({static_cast<int32_t>(x), static_cast<int32_t>(y)});
|
|
}
|
|
}
|
|
}
|
|
blocks[0] = 0;
|
|
pathFinder.removeObstacle({0, 0});
|
|
blocks[mapWidth * mapHeight - 1] = 0;
|
|
pathFinder.removeObstacle({mapWidth - 1, mapHeight - 1});
|
|
|
|
pathFinder.setHeuristic(AStar::Heuristic::euclidean);
|
|
|
|
std::function<void(const AStar::Node<uint32_t>* node)> debugCurrentNode = [&](const AStar::Node<uint32_t>* node) {
|
|
uint64_t index = node->pos.x + node->pos.y * mapWidth;
|
|
blocks[index] = 3;
|
|
};
|
|
|
|
pathFinder.setDebugCurrentNode(debugCurrentNode);
|
|
|
|
std::function<void(const AStar::Node<uint32_t>* node)> debugOpenNode = [&](const AStar::Node<uint32_t>* node) {
|
|
uint64_t index = node->pos.x + node->pos.y * mapWidth;
|
|
blocks[index] = 4;
|
|
};
|
|
|
|
pathFinder.setDebugOpenNode(debugOpenNode);
|
|
|
|
auto start1 = std::chrono::high_resolution_clock::now();
|
|
auto path = pathFinder.findPath({0, 0}, {mapWidth - 1, mapHeight - 1});
|
|
auto stop1 = std::chrono::high_resolution_clock::now();
|
|
if (path.empty()) {
|
|
std::cout << "Path not found" << std::endl;
|
|
}
|
|
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(stop1 - start1);
|
|
std::cout << "Path search: " << duration1.count() << " microseconds" << std::endl;
|
|
|
|
pathSize = path.size();
|
|
for (auto& i : path) {
|
|
uint64_t index = i.x + i.y * mapWidth;
|
|
blocks[index] = 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
ClearBackground(RAYWHITE);
|
|
BeginDrawing();
|
|
|
|
// Draw white if blocks[index] == 0 else black
|
|
int size = 10;
|
|
for (uint64_t x = 0; x < mapWidth; x++) {
|
|
for (uint64_t y = 0; y < mapHeight; y++) {
|
|
uint64_t index = x + y * mapWidth;
|
|
if (blocks[index] == 0) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, WHITE);
|
|
} else if (blocks[index] == 1) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, BLACK);
|
|
} else if (blocks[index] == 2) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, RED);
|
|
} else if (blocks[index] == 3) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, GREEN);
|
|
} else if (blocks[index] == 4) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, BLUE);
|
|
} else if (blocks[index] == 5) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, YELLOW);
|
|
} else if (blocks[index] == 6) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, ORANGE);
|
|
} else if (blocks[index] == 7) {
|
|
DrawRectangle(static_cast<float>(x * size), static_cast<float>(y * size), size, size, PURPLE);
|
|
}
|
|
}
|
|
}
|
|
|
|
// display FPS
|
|
DrawRectangle(screenWidth - 90, 10, 80, 20, Fade(SKYBLUE, 0.95f));
|
|
DrawText(TextFormat("FPS: %02d", GetFPS()), screenWidth - 80, 15, 15, DARKGRAY);
|
|
|
|
DrawRectangle(0, 0, 275, 200, Fade(SKYBLUE, 0.95f));
|
|
DrawRectangleLines(0, 0, 275, 200, BLUE);
|
|
GuiSlider((Rectangle){70, 10, 165, 20}, "Lacunarity", TextFormat("%2.3f", lacunarity), &lacunarity, -0.0f, 5.0f);
|
|
GuiSlider((Rectangle){70, 40, 165, 20}, "Octaves", TextFormat("%2.3f", octaves), &octaves, 1, 12);
|
|
GuiSlider((Rectangle){70, 70, 165, 20}, "Gain", TextFormat("%2.3f", gain), &gain, -0.0f, 16.0f);
|
|
GuiSlider((Rectangle){70, 100, 165, 20}, "Frequency", TextFormat("%2.3f", frequency), &frequency, -0.0f, 10.0f);
|
|
GuiSlider((Rectangle){70, 130, 165, 20}, "Weight", TextFormat("%2.3f", weighted_strength), &weighted_strength, -5.0f, 5.0f);
|
|
GuiSlider((Rectangle){70, 160, 165, 20}, "Multiplier", TextFormat("%2.3f", multiplier), &multiplier, 1, 512);
|
|
|
|
// display info each color for each heuristic
|
|
DrawRectangle(0, 200, 275, 190, Fade(SKYBLUE, 0.95f));
|
|
DrawRectangleLines(0, 200, 275, 190, BLUE);
|
|
|
|
std::string pathText = "Path: " + std::to_string(pathSize);
|
|
DrawRectangle(10, 210, 20, 20, RED);
|
|
DrawText(pathText.c_str(), 40, 210, 20, DARKGRAY);
|
|
|
|
std::string testedNodesText = "Tested nodes";
|
|
DrawRectangle(10, 240, 20, 20, GREEN);
|
|
DrawText(testedNodesText.c_str(), 40, 240, 20, DARKGRAY);
|
|
|
|
std::string openNodesText = "Open nodes";
|
|
DrawRectangle(10, 270, 20, 20, BLUE);
|
|
DrawText(openNodesText.c_str(), 40, 270, 20, DARKGRAY);
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
|
|
return 0;
|
|
}
|