1
0
forked from 0ad/0ad
0ad/source/simulation/AStarEngine.h
Matei 44fe226dd2 # Housekeeping and pathfinder enhancements / optimization when dealing with ranged actions.
- Modified Xeromyces to no longer automatically convert element and
attribute names to lowercase, so that we can have camelCase names. We
should now be able to convert all the multi-word entity properties, like
pass_through_allies, to camelCase, like passThroughAllies, which is more
consistent with the rest of our JavaScript naming conventions. To
support the existing code that assumes lowercase element names, I made
the getElementID and getAttributeID methods (used in the EL and AT
macros) ignore case, and I changed any code that directly accessed
element names to use the right case. CEntityTemplate now converts
Names_LikeThis to names_likeThis (changing each separate "word" in the
name to camelCase). Changed the version letter in XMB filenames from A
to B to support this without requiring people to delete old XMBs.

- Enhanced the pathfinder's handling of contact paths, resulting in a
very large speedup for actions like attacking, construction, etc. The
problem was that the pathfinder used to not count a given state as the
goal unless it was exactly coincident with the target location. This is
fine when you order a unit to go exactly to a certain spot, but if
you're ordering a unit to build, gather or attack something, then the
target tile is impassable (because your target is there) and therefore
the pathfinder never declares a state final. As a result, the pathfinder
tries hundreds of extra tiles in case there is a long path that gets to
the goal, and after failing to find any path that reaches the goal, it
gives you one to the closest point it got to. To fix it, I made the
pathfinder take into account a radius around the goal in which it's OK
to be, which depends on the size of the target unit and the range of
your action.

This was SVN commit r4186.
2006-08-01 03:41:21 +00:00

175 lines
4.5 KiB
C++

#ifndef __ASTAR_ENGINE_H__
#define __ASTAR_ENGINE_H__
#include "ps/Vector2D.h"
#include "ps/Player.h"
#include "lib/types.h"
#include <queue>
class AStarNode
{
public:
float f, g, h;
AStarNode* parent;
CVector2D coord;
bool operator <(const AStarNode& rhs) const { return f<rhs.f; }
bool equals(const AStarNode& rhs) const
{
return ( coord.x==rhs.coord.x ) && ( coord.y==rhs.coord.y );
}
};
class AStarGoalBase;
class AStarGoalLowLevel;
struct AStarNodeComp
{
bool operator()(const AStarNode* n1, const AStarNode* n2) const
{
return (*n2) < (*n1);
}
};
class CVector2D_hash_compare
{
public:
static const size_t bucket_size = 4;
static const size_t min_buckets = 16;
size_t operator() (const CVector2D& Key) const
{
return Key.x + Key.y*1024;
}
bool operator() (const CVector2D& _Key1, const CVector2D& _Key2) const
{
return (_Key1.x < _Key2.x) || (_Key1.x==_Key2.x && _Key1.y < _Key2.y);
}
};
typedef STL_HASH_MAP<CVector2D, AStarNode*, CVector2D_hash_compare> ASNodeHashMap;
class PriQueue
: public std::priority_queue<AStarNode*, std::vector<AStarNode*>, AStarNodeComp>
{
public:
// Promote a node in the PQ, or if it doesn't exist, add it
void promote(AStarNode* node);
void clear();
};
typedef unsigned char AStarNodeFlag;
class CAStarEngine
{
public:
CAStarEngine();
CAStarEngine(AStarGoalBase* goal);
virtual ~CAStarEngine();
void setGoal(AStarGoalBase* goal) { mGoal = goal; }
bool findPath( const CVector2D& src, const CVector2D& dest, CPlayer* player=0, float radius=0.0f );
std::vector<CVector2D> getLastPath();
// The maximum number of nodes that will be expanded before failure is declared
void setSearchLimit( int limit );
protected:
AStarGoalBase* mGoal;
private:
int mSearchLimit;
bool mSolved;
std::vector<CVector2D> mPath;
AStarNodeFlag *mFlags;
long mFlagArraySize;
ASNodeHashMap mClosed;
PriQueue mOpen;
std::vector<AStarNode*> freeNodes;
std::vector<AStarNode*> usedNodes;
// addToOpen will promote if the node already is in Open
void addToOpen( AStarNode* );
AStarNode* removeBestOpenNode();
void addToClosed( AStarNode* );
void removeFromClosed( AStarNode* );
AStarNode* getFromClosed( const CVector2D& );
void clearOpen();
void clearClosed();
void constructPath( AStarNode* );
AStarNode* getFreeASNode();
void cleanup();
inline AStarNodeFlag* GetFlag(const CVector2D&);
inline bool GetIsClear(AStarNodeFlag*);
inline bool GetIsClosed(AStarNodeFlag*);
inline bool GetIsOpen(AStarNodeFlag*);
inline void SetClosedFlag(AStarNodeFlag*);
inline void ClearClosedFlag(AStarNodeFlag*);
inline void SetOpenFlag(AStarNodeFlag*);
inline void ClearOpenFlag(AStarNodeFlag*);
inline bool GetIsPassable(AStarNodeFlag*);
inline bool GetIsBlocked(AStarNodeFlag*);
inline void SetPassableFlag(AStarNodeFlag*);
inline void SetBlockedFlag(AStarNodeFlag*);
};
/**
* An A* goal consists of a destination tile and a radius within which a
* unit must get to the destination. The radius is necessary because for
* actions on a target unit, like attacking or building, the destination
* tile is obstructed by that unit and what we really want is not to get
* to that tile but to get close enough to perform our action.
**/
class AStarGoalBase
{
public:
AStarGoalBase() {}
virtual void setDestination( const CVector2D& ) = 0;
virtual void setRadius( float r ) = 0;
virtual float distanceToGoal( const CVector2D& ) = 0;
virtual bool atGoal( const CVector2D& ) = 0;
virtual float getTileCost( const CVector2D&, const CVector2D& ) = 0;
virtual bool isPassable( const CVector2D&, CPlayer* player=0 ) = 0;
virtual std::vector<CVector2D> getNeighbors( const CVector2D&, CPlayer* player=0 ) = 0;
virtual CVector2D getCoord( const CVector2D& ) = 0;
virtual CVector2D getTile( const CVector2D& ) = 0;
virtual float getRadius() = 0;
};
class AStarGoalLowLevel : public AStarGoalBase
{
public:
AStarGoalLowLevel(): radius(0.0f) {}
void setDestination( const CVector2D& dest );
void setRadius( float r );
float distanceToGoal( const CVector2D& loc );
bool atGoal( const CVector2D& loc );
float getTileCost( const CVector2D& loc1, const CVector2D& loc2 );
bool isPassable( const CVector2D& loc, CPlayer* player=0 );
std::vector<CVector2D> getNeighbors( const CVector2D& loc, CPlayer* player=0 );
CVector2D getCoord( const CVector2D& loc);
CVector2D getTile( const CVector2D& loc);
float getRadius();
private:
CVector2D coord;
float radius;
};
class CAStarEngineLowLevel : public CAStarEngine
{
public:
CAStarEngineLowLevel()
{
mGoal = new AStarGoalLowLevel;
}
virtual ~CAStarEngineLowLevel()
{
delete mGoal;
}
};
#endif