- some code cleanup related to supporting multiple path finders

This commit is contained in:
Mark Vejvoda 2010-07-21 19:17:45 +00:00
parent bca03b0c0c
commit 74b6fc8139
5 changed files with 53 additions and 8 deletions

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest (www.glest.org) // This file is part of Glest (www.glest.org)
// //
// Copyright (C) 2001-2008 Martio Figueroa // Copyright (C) 2001-2008 Martiño Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published
@ -68,7 +68,8 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos){
if(dynamic_cast<UnitPathBasic *>(path) != NULL) { if(dynamic_cast<UnitPathBasic *>(path) != NULL) {
if(!path->isEmpty()) { if(!path->isEmpty()) {
//route cache //route cache
Vec2i pos= path->pop(); UnitPathBasic *basicPath = dynamic_cast<UnitPathBasic *>(path);
Vec2i pos= basicPath->pop();
if(map->canMove(unit, unit->getPos(), pos)) { if(map->canMove(unit, unit->getPos(), pos)) {
unit->setTargetPos(pos); unit->setTargetPos(pos);
return tsOnTheWay; return tsOnTheWay;
@ -81,12 +82,15 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos){
//route cache //route cache
Vec2i pos= advPath->peek(); Vec2i pos= advPath->peek();
if(map->canMove(unit, unit->getPos(), pos)){ if(map->canMove(unit, unit->getPos(), pos)){
path->pop(); advPath->pop();
unit->setTargetPos(pos); unit->setTargetPos(pos);
return tsOnTheWay; return tsOnTheWay;
} }
} }
} }
else {
throw runtime_error("unsupported or missing path finder detected!");
}
} }
//route cache miss //route cache miss
@ -101,7 +105,8 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos){
case tsOnTheWay: case tsOnTheWay:
{ {
if(dynamic_cast<UnitPathBasic *>(path) != NULL) { if(dynamic_cast<UnitPathBasic *>(path) != NULL) {
Vec2i pos= path->pop(); UnitPathBasic *basicPath = dynamic_cast<UnitPathBasic *>(path);
Vec2i pos= basicPath->pop();
if(map->canMove(unit, unit->getPos(), pos)) { if(map->canMove(unit, unit->getPos(), pos)) {
unit->setTargetPos(pos); unit->setTargetPos(pos);
} }
@ -122,6 +127,9 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos){
return tsBlocked; return tsBlocked;
} }
} }
else {
throw runtime_error("unsupported or missing path finder detected!");
}
} }
break; break;
} }

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest (www.glest.org) // This file is part of Glest (www.glest.org)
// //
// Copyright (C) 2001-2008 Martio Figueroa // Copyright (C) 2001-2008 Martiño Figueroa
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published // the terms of the GNU General Public License as published

View File

@ -404,6 +404,9 @@ bool RoutePlanner::refinePath(Unit *unit) {
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
assert(!wpPath.empty()); assert(!wpPath.empty());
@ -443,6 +446,9 @@ void RoutePlanner::smoothPath(Unit *unit) {
PF_TRACE(); PF_TRACE();
UnitPathInterface *path = unit->getPath(); UnitPathInterface *path = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(path); UnitPath *advPath = dynamic_cast<UnitPath *>(path);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
if (advPath->size() < 3) { if (advPath->size() < 3) {
return; return;
@ -517,6 +523,9 @@ TravelState RoutePlanner::doRouteCache(Unit *unit) {
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
WaypointPath &wpPath = *unit->getWaypointPath(); WaypointPath &wpPath = *unit->getWaypointPath();
@ -554,6 +563,9 @@ TravelState RoutePlanner::doQuickPathSearch(Unit *unit, const Vec2i &target) {
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
// IF_DEBUG_EDITION( clearOpenClosed(unit->getPos(), target); ) // IF_DEBUG_EDITION( clearOpenClosed(unit->getPos(), target); )
@ -584,6 +596,10 @@ TravelState RoutePlanner::findAerialPath(Unit *unit, const Vec2i &targetPos) {
AnnotatedMap *aMap = world->getCartographer()->getMasterMap(); AnnotatedMap *aMap = world->getCartographer()->getMasterMap();
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
PosGoal goal(targetPos); PosGoal goal(targetPos);
MoveCost cost(unit, aMap); MoveCost cost(unit, aMap);
@ -624,6 +640,10 @@ TravelState RoutePlanner::findPathToLocation(Unit *unit, const Vec2i &finalPos)
PF_TRACE(); PF_TRACE();
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
WaypointPath &wpPath = *unit->getWaypointPath(); WaypointPath &wpPath = *unit->getWaypointPath();
@ -737,6 +757,10 @@ TravelState RoutePlanner::customGoalSearch(PMap1Goal &goal, Unit *unit, const Ve
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
WaypointPath &wpPath = *unit->getWaypointPath(); WaypointPath &wpPath = *unit->getWaypointPath();
const Vec2i &start = unit->getPos(); const Vec2i &start = unit->getPos();
@ -775,6 +799,10 @@ TravelState RoutePlanner::findPathToGoal(Unit *unit, PMap1Goal &goal, const Vec2
PF_TRACE(); PF_TRACE();
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
WaypointPath &wpPath = *unit->getWaypointPath(); WaypointPath &wpPath = *unit->getWaypointPath();
@ -848,6 +876,10 @@ bool RoutePlanner::repairPath(Unit *unit) {
PF_TRACE(); PF_TRACE();
UnitPathInterface *unitpath = unit->getPath(); UnitPathInterface *unitpath = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath); UnitPath *advPath = dynamic_cast<UnitPath *>(unitpath);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
UnitPath &path = *advPath; UnitPath &path = *advPath;
WaypointPath &wpPath = *unit->getWaypointPath(); WaypointPath &wpPath = *unit->getWaypointPath();

View File

@ -1,7 +1,7 @@
// ============================================================== // ==============================================================
// This file is part of Glest (www.glest.org) // This file is part of Glest (www.glest.org)
// //
// Copyright (C) 2001-2008 Martio Figueroa // Copyright (C) 2001-2008 Martiño Figueroa
// 2009-2010 James McCulloch // 2009-2010 James McCulloch
// //
// You can redistribute this code and/or modify it under // You can redistribute this code and/or modify it under
@ -189,6 +189,10 @@ private:
bool attemptMove(Unit *unit) const { bool attemptMove(Unit *unit) const {
UnitPathInterface *path = unit->getPath(); UnitPathInterface *path = unit->getPath();
UnitPath *advPath = dynamic_cast<UnitPath *>(path); UnitPath *advPath = dynamic_cast<UnitPath *>(path);
if(advPath == NULL) {
throw runtime_error("Invalid or NULL unit path pointer!");
}
assert(advPath->isEmpty() == false); assert(advPath->isEmpty() == false);
Vec2i pos = advPath->peek(); Vec2i pos = advPath->peek();
if (isLegalMove(unit, pos)) { if (isLegalMove(unit, pos)) {

View File

@ -105,7 +105,7 @@ public:
virtual void clear() = 0; virtual void clear() = 0;
virtual void incBlockCount() = 0; virtual void incBlockCount() = 0;
virtual void push(const Vec2i &path) = 0; virtual void push(const Vec2i &path) = 0;
virtual Vec2i pop() = 0; //virtual Vec2i pop() = 0;
virtual std::string toString() const = 0; virtual std::string toString() const = 0;
}; };
@ -160,7 +160,8 @@ public:
#else #else
// new style, for the new RoutePlanner // new style, for the new RoutePlanner
Vec2i peek() {return front();} /**< peek at the next position */ Vec2i peek() {return front();} /**< peek at the next position */
virtual Vec2i pop() { Vec2i p= front(); erase(begin()); return p; } /**< pop the next position off the path */ //virtual Vec2i pop() { Vec2i p= front(); erase(begin()); return p; } /**< pop the next position off the path */
void pop() { erase(begin()); } /**< pop the next position off the path */
#endif #endif
int getBlockCount() const { return blockCount; } int getBlockCount() const { return blockCount; }