Skip to content

Commit

Permalink
add support for shortest path to QTPFS
Browse files Browse the repository at this point in the history
  • Loading branch information
marcushutchings committed Apr 1, 2024
1 parent e9237e5 commit 1a9c6a7
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions rts/Sim/Misc/ModInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void CModInfo::ResetState()
description.clear();

{
preferShortestPath = false;
allowDirectionalPathing = true;
allowAircraftToLeaveMap = true;
allowAircraftToHitGround = true;
Expand Down Expand Up @@ -182,6 +183,7 @@ void CModInfo::Init(const std::string& modFileName)
// movement
const LuaTable& movementTbl = root.SubTable("movement");

preferShortestPath = movementTbl.GetBool("preferShortestPath", preferShortestPath);
allowDirectionalPathing = movementTbl.GetBool("allowDirectionalPathing", allowDirectionalPathing);
allowAircraftToLeaveMap = movementTbl.GetBool("allowAirPlanesToLeaveMap", allowAircraftToLeaveMap);
allowAircraftToHitGround = movementTbl.GetBool("allowAircraftToHitGround", allowAircraftToHitGround);
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/Misc/ModInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CModInfo
std::string description;

// Movement behaviour
bool preferShortestPath; //< if pathing system supports it, use shortest route instead of fastest route
bool allowDirectionalPathing; //< determines if ground speed going downhill != going uphill
bool allowAircraftToLeaveMap; //< determines if gunships are allowed to leave map boundaries
bool allowAircraftToHitGround; //< determines if aircraft (both types) can collide with terrain
Expand Down
4 changes: 3 additions & 1 deletion rts/Sim/MoveTypes/MoveDefHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ void MoveDefHandler::PostSimInit() {
// capabilities of the pathing system.
std::for_each(moveDefs.begin(), moveDefs.end(), [](MoveDef& md){
md.allowDirectionalPathing &= pathManager->AllowDirectionalPathing();
md.preferShortestPath &= pathManager->AllowShortestPath();
});
}

Expand Down Expand Up @@ -319,7 +320,8 @@ MoveDef::MoveDef(const LuaTable& moveDefTable): MoveDef() {

height = std::max(1, moveDefTable.GetInt("height", defaultHeight));

allowDirectionalPathing = moveDefTable.GetBool("allowDirectionalPathing", allowDirectionalPathing);
allowDirectionalPathing = moveDefTable.GetBool("allowDirectionalPathing", allowDirectionalPathing);
preferShortestPath = moveDefTable.GetBool("preferShortestPath", preferShortestPath);
}

bool MoveDef::DoRawSearch(
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/MoveTypes/MoveDefHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ struct MoveDef {
bool allowTerrainCollisions = true;
bool allowDirectionalPathing = true;
bool allowRawMovement = false;
bool preferShortestPath = true;

/// do we leave heat and avoid any left by others?
bool heatMapping = true;
Expand Down
1 change: 1 addition & 0 deletions rts/Sim/Path/IPathManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class IPathManager {
virtual std::int64_t PostFinalizeRefresh() { return 0; }

virtual bool AllowDirectionalPathing() { return false; }
virtual bool AllowShortestPath() { return false; }

/**
* returns if a path was changed after RequestPath returned its pathID
Expand Down
3 changes: 2 additions & 1 deletion rts/Sim/Path/QTPFS/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ bool QTPFS::QTNode::UpdateMoveCost(
assert(speedModSum >= 0.0f);

float speedModAvg = speedModSum / area();
moveCostAvg = (speedModAvg <= 0.001f) ? QTPFS_POSITIVE_INFINITY : (1.0f / speedModAvg);
moveCostAvg = (speedModAvg <= 0.001f) ? QTPFS_POSITIVE_INFINITY : (nl.UseShortestPath() ? 1.f : (1.f / speedModAvg));

// no node can have ZERO traversal cost
assert(moveCostAvg > 0.0f);
Expand Down Expand Up @@ -598,6 +598,7 @@ bool QTPFS::QTNode::UpdateMoveCost(
needSplit |= (AllSquaresImpassable() && xsize() > 16); // TODO: magic number for size of damage quads

wantSplit &= (xsize() > 16); // try not to split below 16 if possible.
wantSplit &= !(nl.UseShortestPath());

return (wantSplit || needSplit);
}
Expand Down
3 changes: 3 additions & 0 deletions rts/Sim/Path/QTPFS/NodeLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ void QTPFS::NodeLayer::Init(unsigned int layerNum) {

curSpeedMods.resize(xsize * zsize, 0);
curSpeedBins.resize(xsize * zsize, -1);

MoveDef* md = moveDefHandler.GetMoveDefByPathType(layerNum);
useShortestPath = md->preferShortestPath;
}

void QTPFS::NodeLayer::Clear() {
Expand Down
3 changes: 3 additions & 0 deletions rts/Sim/Path/QTPFS/NodeLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ namespace QTPFS {
INode* GetNearestNodeInArea(const SRectangle& areaToSearch, int2 referencePoint, std::vector<INode*>& openNodes);
INode* GetNodeThatEncasesPowerOfTwoArea(const SRectangle& areaToEncase);

bool UseShortestPath() { return useShortestPath; }

private:
std::vector<QTNode> poolNodes[16];
std::vector<unsigned int> nodeIndcs;
Expand Down Expand Up @@ -220,6 +222,7 @@ namespace QTPFS {

float maxRelSpeedMod = 0.0f; // TODO: Remove these?
float avgRelSpeedMod = 0.0f;
bool useShortestPath = false;
};
}

Expand Down
3 changes: 3 additions & 0 deletions rts/Sim/Path/QTPFS/PathManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <vector>

#include "Sim/Misc/ModInfo.h"
#include "Sim/Path/IPathManager.h"
#include "NodeLayer.h"
#include "PathCache.h"
Expand Down Expand Up @@ -47,6 +48,8 @@ namespace QTPFS {
bool PathUpdated(unsigned int pathID) override;
void ClearPathUpdated(unsigned int pathID) override;

bool AllowShortestPath() override { return modInfo.preferShortestPath; }

void TerrainChange(unsigned int x1, unsigned int z1, unsigned int x2, unsigned int z2, unsigned int type) override;
void Update() override;
void UpdatePath(const CSolidObject* owner, unsigned int pathID) override;
Expand Down

0 comments on commit 1a9c6a7

Please sign in to comment.