beginning of pathfinding implementation

This commit is contained in:
devZoGok
2022-07-26 09:33:07 +03:00
parent 99a65a8d0c
commit a4da044b10
8 changed files with 168 additions and 14 deletions
+18 -13
View File
@@ -5,15 +5,16 @@ set(CMAKE_BUILD_TYPE Debug)
set(BUILD_TESTS ON)
cmake_policy(SET CMP0015 NEW)
set(states activeGameState.cpp inGameAppState.cpp guiAppState.cpp)
set(util util.cpp binds.h)
set(gui tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp)
set(core gameManager.cpp main.cpp defConfigs.cpp)
set(ships vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp)
set(projectiles projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp)
set(aircraft jet.cpp demoJet.cpp missileJet.cpp)
set(fx explosion.cpp)
set(content player.cpp unitDataManager.cpp unit.cpp map.cpp ${ships} ${projectiles} ${aircraft} ${fx})
set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp)
set(UTIL util.cpp binds.h)
set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp)
set(CORE gameManager.cpp defConfigs.cpp)
set(SHIPS vessel.cpp destroyer.cpp cruiser.cpp aircraftCarrier.cpp submarine.cpp)
set(PROJECTILES projectile.cpp guidedMissile.cpp depthCharge.cpp missile.cpp shell.cpp torpedo.cpp)
set(AIRCRAFT jet.cpp demoJet.cpp missileJet.cpp)
set(FX explosion.cpp)
set(CONTENT player.cpp unitDataManager.cpp unit.cpp pathfinder.cpp map.cpp ${SHIPS} ${PROJECTILES} ${AIRCRAFT} ${FX})
set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL})
set(SFML_DIR external/SFML)
set(VB01_DIR external/vb01)
@@ -23,7 +24,7 @@ set(GAME_BASE_DIR external/gameBase)
set(GLFW_DIR ${VB01_DIR}/external/glfw/)
set(GAME_NAME battleship)
add_executable(${GAME_NAME} ${states} ${gui} ${core} ${content} ${util})
add_executable(${GAME_NAME} main.cpp ${GAME_SRC})
target_include_directories(${GAME_NAME} PUBLIC ${FREETYPE_DIR}/include)
@@ -67,8 +68,12 @@ if(BUILD_TESTS)
link_directories(C:/Program\ Files\ \(x86\)/cppunit/lib)
endif()
set(TEST_SRC testMain.cpp ${GAME_SRC})
set(TEST_SRC testMain.cpp pathfinderTest.cpp ${GAME_SRC})
set(TEST_NAME battleshipTests)
add_executable(battleshipTests ${TEST_SRC})
target_link_libraries(battleshipTests ${DEPS} cppunit)
add_executable(${TEST_NAME} ${TEST_SRC})
target_include_directories(${TEST_NAME} PUBLIC ${VB01_DIR})
target_include_directories(${TEST_NAME} PUBLIC ${VB01_GUI_DIR})
target_include_directories(${TEST_NAME} PUBLIC ${GAME_BASE_DIR})
target_link_libraries(${TEST_NAME} ${DEPS} cppunit)
endif()
+30 -1
View File
@@ -125,9 +125,12 @@ namespace battleship{
for(int i = 0; i < numCellsByDim[0]; i++){
cells[i] = new GraphNode*[numCellsByDim[1]];
for(int j = 0; j < numCellsByDim[1]; j++){
for(int j = 0; j < numCellsByDim[1]; j++)
cells[i][j] = new GraphNode[numCellsByDim[2]];
}
for(int i = 0; i < numCellsByDim[0]; i++){
for(int j = 0; j < numCellsByDim[1]; j++){
for(int k = 0; k < numCellsByDim[2]; k++){
GraphNode::Type type = GraphNode::SEA;
@@ -138,6 +141,32 @@ namespace battleship{
}
}
}
int numCells = numCellsByDim[0] * numCellsByDim[1] * numCellsByDim[2];
weights = new int*[numCells];
for(int i = 0; i < numCells; i++){
weights[i] = new int[numCells];
int x1 = i / numCellsByDim[2] / numCellsByDim[1];
int y1 = i / numCellsByDim[2] % numCellsByDim[1];
int z1 = i % numCellsByDim[2];
for(int j = 0; j < numCells; j++){
int x2 = j / numCellsByDim[2] / numCellsByDim[1];
int y2 = j / numCellsByDim[2] % numCellsByDim[1];
int z2 = j % numCellsByDim[2];
int weight;
if(abs(x1 - x2) == 1 || abs(y1 - y2) == 1 || abs(z1 - z2) == 1)
weight = 1;
else if(x1 == x2 && y1 == y2 && z1 == z2)
weight = 0;
else
weight = -1;
weights[i][j] = weight;
}
}
}
void Map::unload() {}
+1
View File
@@ -31,6 +31,7 @@ namespace battleship{
std::string mapName;
float cellSize = 1;
GraphNode*** cells = nullptr;
int **weights = nullptr;
void loadSkybox(gameBase::LuaManager*);
void loadTerrain(gameBase::LuaManager*);
+28
View File
@@ -0,0 +1,28 @@
#include "pathfinder.h"
namespace battleship{
using namespace std;
static Pathfinder *pathfinder = nullptr;
Pathfinder* Pathfinder::getSingleton(){
if(!pathfinder)
pathfinder = new Pathfinder();
return pathfinder;
}
Pathfinder::Pathfinder(){
}
vector<int> Pathfinder::findPath(int **weights, int size, int source, int sink){
int pathWeights[size];
pathWeights[0] = 0;
for(int i = 1; i < size; i++)
pathWeights[i] = -1;
vector<int> path;
return path;
}
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef PATHFINDER_H
#define PATHFINDER_H
#include <vector>
namespace battleship{
class Pathfinder{
public:
static Pathfinder* getSingleton();
std::vector<int> findPath(int**, int, int, int);
private:
Pathfinder();
};
}
#endif
+46
View File
@@ -0,0 +1,46 @@
#include "pathfinderTest.h"
#include "pathfinder.h"
#include <vector>
namespace battleship{
using namespace std;
void PathfinderTest::testFindPath(){
int size = 7;
int w[size][size] = {
{0, 2, 4, -1, -1, -1, -1},
{-1, 0, 1, 9, 13, -1, -1},
{-1, 2, 0, -1, 4, 5, -1},
{-1, -1, -1, 0, -1, -1, 1},
{1, -1, -1, 1, 0, -1, 3},
{-1, -1, -1, -1, 9, 0, 2},
{-1, -1, -1, -1, -1, -1, 0}
};
int **weights = new int*[size];
for(int i = 0; i < size; i++){
weights[i] = new int[size];
for(int j = 0; j < size; j++)
weights[i][j] = w[i][j];
}
vector<int> path = pathfinder->findPath(weights, size, 0, size - 1);
CPPUNIT_ASSERT(path.size() == 6);
int sumPathWeights = 0;
for(int p : path)
sumPathWeights += p;
CPPUNIT_ASSERT(sumPathWeights == 9);
}
void PathfinderTest::setUp(){
pathfinder = Pathfinder::getSingleton();
}
void PathfinderTest::tearDown(){
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef PATHFINDER_TEST_H
#define PATHFINDER_TEST_H
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
namespace battleship{
class Pathfinder;
class PathfinderTest : public CppUnit::TestFixture{
CPPUNIT_TEST_SUITE(PathfinderTest);
CPPUNIT_TEST(testFindPath);
CPPUNIT_TEST_SUITE_END();
public:
PathfinderTest(){}
void testFindPath();
void setUp();
void tearDown();
private:
Pathfinder *pathfinder = nullptr;
};
}
#endif
+4
View File
@@ -3,10 +3,14 @@
#include <cppunit/TestCaller.h>
#include <cppunit/ui/text/TestRunner.h>
#include "pathfinderTest.h"
using namespace CppUnit;
using namespace battleship;
int main(){
TextUi::TestRunner runner;
runner.addTest(PathfinderTest::suite());
runner.run();
return 0;
}