Skip to content
Permalink
Browse files
Enemy pathfinding algorithm
Pathfinding done for goblins, isn't linked to main file
  • Loading branch information
mccallu4 committed Mar 17, 2020
1 parent b20c222 commit 4e7a0d71560a0eac6e11188ad1829cade52d2365
Showing 1 changed file with 125 additions and 0 deletions.
@@ -0,0 +1,125 @@
#pragma once
#include <vector>
#include <array>
#include "mapPrototype.h"

struct coord
{
int x, y;
};

using namespace std;
class pathing {
public:
char nodePathingArray[100][100];
int mpWidth, mpHeight;
coord startNode;
coord endNode;
coord difference;
vector<coord> solution;
pathing(int mapSizeX, int mapSizeY, Dungeon holdMap, int coordXGoblin, int coordYGoblin)
{
startNode = { coordXGoblin, coordYGoblin };
for (int i = 0; i < mapSizeX; i++) //MAP GENERATION
{
for (int j = 0; j < mapSizeY; j++)
{
nodePathingArray[i][j] = holdMap.getTile(i, j);
if (nodePathingArray[i][j] == '@')
{
endNode = { i,j };
}
else if (nodePathingArray[i][j] == 'G' && (i != coordXGoblin && j != coordYGoblin))
{
map[i][j] = '#';
}
else if (nodePathingArray[i][j] == ' ')
{
nodePathingArray[i][j] = '.';
}
}
}
difference = { endNode.x - startNode.x, endNode.y - startNode.y };
for (int i = 0; i < mapSizeX; i++) //MAP GENERATION
{
if (nodePathingArray[i][0] == NULL)
{
mpWidth = i;
break;
}
}
for (int j = 0; j < mapSizeY; j++)
{
if (nodePathingArray[0][j] == NULL)
{
mpHeight = j;
break;
}
}
}
vector<coord> pathCalc()
{

bool up, left;
if (difference.x < 0 )
{
left = true;
}
else
{
left = false;
}
if (difference.y < 0)
{
up = true;
}
else
{
up = false;
}
}
bool recursiveSolve(int x, int y)
{
if (x == endNode.x && y == endNode.y)
{
return true;
}

if (!(nodePathingArray[x][y] == '#' || x < 0 || y < 0 || x > mpWidth || y > mpHeight))
{
nodePathingArray[x][y] = 'T';
solution.push_back({ x,y });
if (recursiveSolve(x + 1, y))
{
return true;
}
if (recursiveSolve(x, y + 1))
{
return true;
}
if (recursiveSolve(x - 1, y))
{
return true;
}
if (recursiveSolve(x, y - 1))
{
return true;
}
solution.pop_back();
nodePathingArray[x][y] = '.';
return false;
}
return false;
}
void printNodes()
{
for (int i = 0; i < sizeof(nodePathingArray); i++) //MAP GENERATION
{
for (int j = 0; j < sizeof(nodePathingArray); j++)
{
cout << nodePathingArray[i][j];
}
cout << endl;
}
}
};

0 comments on commit 4e7a0d7

Please sign in to comment.