2008-06-01 17:55:31 +03:00
|
|
|
#ifndef CPathfinder_H
|
|
|
|
#define CPathfinder_H
|
2007-11-19 00:58:28 +02:00
|
|
|
#include "global.h"
|
2007-08-07 16:49:15 +03:00
|
|
|
#include <vector>
|
2008-06-01 17:55:31 +03:00
|
|
|
#include <queue>
|
|
|
|
#include <math.h>
|
2007-10-27 22:38:48 +03:00
|
|
|
class CGHeroInstance;
|
2008-06-01 17:55:31 +03:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class Coordinate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int z;
|
|
|
|
float g; //Distance from goal
|
|
|
|
int h; //Movement cost
|
|
|
|
|
|
|
|
Coordinate() : x(NULL), y(NULL), z(NULL), g(NULL), h(NULL) {}
|
|
|
|
Coordinate(int3 xyz) : x(xyz.x), y(xyz.y), z(xyz.z){}
|
|
|
|
Coordinate(int xCor, int yCor, int zCor) : x(xCor), y(yCor), z(zCor){}
|
|
|
|
Coordinate(int xCor, int yCor, int zCor, int dist, int penalty) : x(xCor), y(yCor), z(zCor), g(dist), h(penalty){}
|
|
|
|
void operator=(const Coordinate& other);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Class used in priority queue to determine order.
|
|
|
|
class Compare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool operator()(const vector<Coordinate>& a, const vector<Coordinate>& b);
|
|
|
|
};
|
|
|
|
|
|
|
|
//Included for old format
|
2007-08-07 16:49:15 +03:00
|
|
|
struct CPathNode
|
|
|
|
{
|
|
|
|
bool accesible; //true if a hero can be on this node
|
|
|
|
int dist; //distance from the first node of searching; -1 is infinity
|
|
|
|
CPathNode * theNodeBefore;
|
2007-09-22 04:16:31 +03:00
|
|
|
int3 coord; //coordiantes
|
2007-08-12 20:48:05 +03:00
|
|
|
bool visited;
|
2007-08-07 16:49:15 +03:00
|
|
|
};
|
|
|
|
|
2008-06-01 17:55:31 +03:00
|
|
|
//Included for old format
|
2007-08-07 16:49:15 +03:00
|
|
|
struct CPath
|
|
|
|
{
|
2007-08-17 20:42:21 +03:00
|
|
|
std::vector<CPathNode> nodes; //just get node by node
|
2007-09-22 04:16:31 +03:00
|
|
|
|
|
|
|
int3 startPos(); // start point
|
|
|
|
int3 endPos(); //destination point
|
2007-08-07 16:49:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class CPathfinder
|
|
|
|
{
|
|
|
|
private:
|
2008-06-01 17:55:31 +03:00
|
|
|
boost::logic::tribool blockLandSea; //true - blocks sea, false - blocks land, indeterminate - allows all
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does the actual path calculation. Don't call this directly, call GetPath instead.
|
|
|
|
*/
|
|
|
|
vector<Coordinate>* CalcPath();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determines if the given node exists in the closed list, returns true if it does. This is
|
|
|
|
* used to ensure you dont check the same node twice.
|
|
|
|
*/
|
|
|
|
bool ExistsInClosed(Coordinate node);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds the neighbors of the current node to the open cue so they can be considered in the
|
|
|
|
* path creation. If the node has a cost (f = g + h) less than zero, it isn't added to Open.
|
|
|
|
*/
|
|
|
|
void AddNeighbors(vector<Coordinate>* node);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculates the movement cost of the node. Returns -1 if it is impossible to travel on.
|
|
|
|
*/
|
|
|
|
void CalcH(Coordinate* node);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculates distance from node to end node.
|
|
|
|
*/
|
|
|
|
void CalcG(Coordinate* node);
|
|
|
|
|
2007-08-07 16:49:15 +03:00
|
|
|
public:
|
2008-06-01 17:55:31 +03:00
|
|
|
//Contains nodes to be searched
|
|
|
|
priority_queue < vector<Coordinate>, vector<vector<Coordinate>>, Compare> Open;
|
|
|
|
|
|
|
|
//History of nodes you have been to before
|
|
|
|
vector<Coordinate> Closed;
|
|
|
|
|
|
|
|
//The start, or source position.
|
|
|
|
Coordinate Start;
|
|
|
|
|
|
|
|
//The end, or destination position
|
|
|
|
Coordinate End;
|
|
|
|
|
|
|
|
//A reference to the Hero.
|
|
|
|
const CGHeroInstance* Hero;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does basic input checking and setup for the path calculation.
|
|
|
|
*/
|
|
|
|
vector<Coordinate>* GetPath(const CGHeroInstance* hero);
|
|
|
|
vector<Coordinate>* GetPath(Coordinate start, Coordinate end, const CGHeroInstance* hero);
|
|
|
|
|
|
|
|
//Convert to oldformat
|
|
|
|
CPath* ConvertToOldFormat(vector<Coordinate>* p);
|
2007-10-05 21:16:22 +03:00
|
|
|
|
|
|
|
static void convertPath(CPath * path, unsigned int mode); //mode=0 -> from 'manifest' to 'object'
|
2008-06-01 17:55:31 +03:00
|
|
|
|
2007-08-07 16:49:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //CPATHFINDER_H
|