I'm a second year college student in Video Game Developement. My teacher doesn't even know how to solve this so I'm hoping somebody here could help! Thank you in advance!
I'm developing a video game and I need pathfinding for the enemies. It's required for me to use A* pathfinding.
I decided to use unordered maps because I figured it's more efficient. I did all the code and it seeemed to be right but then I got an error from the unordered_map library:
Error C2280 'std::hash::hash(const std::hash &)': attempting to reference a deleted function. with [ _Kty=iPoint ] Game C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\unordered_map 117
This error then takes me to the unordered_map file, specifically to this line:
unordered_map() : _Mybase(_Key_compare(), allocator_type()) { // construct empty map from defaults }
My teacher says it's very weird and can't fix it. The other thing is that I decided to use unordered_maps but it's not the way the teacher told us to do it. He has no problem with me doing it differently, but that also means that I have no other reference from other classmates because they used a different method (do everything with lists).
I will put here the pathfinding class I coded:
This is PathFinding.h:
#ifndef __PATHFINDING_H__
#define __PATHFINDING_H__
#include "Module.h"
#include "Point.h"
#include "DynArray.h"
#include "List.h"
#include <unordered_map>
#include <queue>
class PathFinding : public Module
{
public:
PathFinding();
~PathFinding();
int Heuristic(iPoint, iPoint);
iPoint Path(iPoint, int);
iPoint NextMove(std::unordered_map<iPoint, iPoint>, iPoint, iPoint);
};
#endif // __PATHFINDING_H__
This is PathFinding.cpp:
#include "App.h"
#include "PathFinding.h"
#include "Player.h"
#include "List.h"
#include <unordered_map>
#include <queue>
#include "Defs.h"
#include "Log.h"
PathFinding::PathFinding() : Module() { name.Create("pathfinding"); }
PathFinding::~PathFinding(){}
int PathFinding::Heuristic(iPoint start, iPoint end)
{
return abs(start.x - end.x) + abs(start.y - end.y);
}
List<iPoint> Neighbours(iPoint current, int speed)
{
List<iPoint> list = List<iPoint>();
// Order is top, right, bottom, left
list.Add(iPoint(current.x, current.y - speed));
list.Add(iPoint(current.x + speed, current.y));
list.Add(iPoint(current.x, current.y - speed));
list.Add(iPoint(current.x - speed, current.y));
return list;
}
iPoint PathFinding::NextMove(std::unordered_map<iPoint, iPoint> cameFrom, iPoint end, iPoint start)
{
iPoint current = end;
while (cameFrom[current] != start)
{
current = cameFrom[current];
}
return current;
}
iPoint PathFinding::Path(iPoint start, int speed)
{
PriorityQueue<iPoint, double> frontier;
std::unordered_map<iPoint, iPoint> came_from;
std::unordered_map<iPoint, int> cost_so_far;
frontier.put(start, 0);
iPoint end = app->player->playerPos;
came_from[start] = start;
cost_so_far[start] = 0;
while (!frontier.empty())
{
iPoint current = frontier.get();
if (current == end)
{
break;
}
List<iPoint> neighbours = Neighbours(current, speed);
for (int i = 0; i < 4; i++)
{
double new_cost = cost_so_far[current] + speed;
iPoint next = neighbours.At(i)->data;
if (cost_so_far.find(next) == cost_so_far.end() || new_cost < cost_so_far[next])
{
cost_so_far[next] = new_cost;
double priority = new_cost + Heuristic(next, end);
frontier.put(next, priority);
came_from[next] = current;
}
}
}
return NextMove(came_from, end, start);
}
This is C++ in Visual Studio. If anyone can help I would appreciate it a lot! Thank you again!