PathSearch patched to support ICustomTerrain

This commit is contained in:
Chris Forbes
2010-01-19 10:32:03 +13:00
parent 76a9e293b2
commit f0b26e8ee2
3 changed files with 23 additions and 25 deletions

View File

@@ -14,7 +14,6 @@ namespace OpenRa
public UnitMovementType umt; public UnitMovementType umt;
Func<int2, bool> customBlock; Func<int2, bool> customBlock;
public bool checkForBlocked; public bool checkForBlocked;
public bool ignoreTerrain;
public Actor ignoreBuilding; public Actor ignoreBuilding;
public PathSearch() public PathSearch()
@@ -40,9 +39,8 @@ namespace OpenRa
var p = queue.Pop(); var p = queue.Pop();
cellInfo[ p.Location.X, p.Location.Y ].Seen = true; cellInfo[ p.Location.X, p.Location.Y ].Seen = true;
if (!ignoreTerrain) if (passableCost[(int)umt][p.Location.X, p.Location.Y] == float.PositiveInfinity)
if (passableCost[(int)umt][p.Location.X, p.Location.Y] == float.PositiveInfinity) return p.Location;
return p.Location;
foreach( int2 d in Util.directions ) foreach( int2 d in Util.directions )
{ {
@@ -51,17 +49,18 @@ namespace OpenRa
if (!Game.world.Map.IsInMap(newHere.X, newHere.Y)) continue; if (!Game.world.Map.IsInMap(newHere.X, newHere.Y)) continue;
if( cellInfo[ newHere.X, newHere.Y ].Seen ) if( cellInfo[ newHere.X, newHere.Y ].Seen )
continue; continue;
if (!ignoreTerrain) var custom = Game.world.customTerrain[newHere.X, newHere.Y];
{ if (custom != null
if (passableCost[(int)umt][newHere.X, newHere.Y] == float.PositiveInfinity) && custom.GetCost(newHere, umt) == float.PositiveInfinity)
continue; continue;
if (!Game.world.BuildingInfluence.CanMoveHere(newHere) && if (passableCost[(int)umt][newHere.X, newHere.Y] == float.PositiveInfinity)
Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding) continue;
continue; if (!Game.world.BuildingInfluence.CanMoveHere(newHere) &&
if (Game.world.Map.IsOverlaySolid(newHere)) Game.world.BuildingInfluence.GetBuildingAt(newHere) != ignoreBuilding)
continue; continue;
} if (Game.world.Map.IsOverlaySolid(newHere))
continue;
// Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units // Replicate real-ra behavior of not being able to enter a cell if there is a mixture of crushable and uncrushable units
if (checkForBlocked && (Game.world.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.world.IsActorPathableToCrush(a, umt)))) if (checkForBlocked && (Game.world.UnitInfluence.GetUnitsAt(newHere).Any(a => !Game.world.IsActorPathableToCrush(a, umt))))
@@ -69,14 +68,15 @@ namespace OpenRa
if (customBlock != null && customBlock(newHere)) if (customBlock != null && customBlock(newHere))
continue; continue;
var est = heuristic( newHere ); var est = heuristic( newHere );
if( est == float.PositiveInfinity ) if( est == float.PositiveInfinity )
continue; continue;
float cellCost = ( ( d.X * d.Y != 0 ) ? 1.414213563f : 1.0f ) * float cellCost = ((d.X * d.Y != 0) ? 1.414213563f : 1.0f) *
(ignoreTerrain ? 1 : passableCost[ (int)umt ][ newHere.X, newHere.Y ]); (custom != null
? custom.GetCost(newHere, umt) :
passableCost[(int)umt][newHere.X, newHere.Y]);
float newCost = cellInfo[ p.Location.X, p.Location.Y ].MinCost + cellCost; float newCost = cellInfo[ p.Location.X, p.Location.Y ].MinCost + cellCost;
if( newCost >= cellInfo[ newHere.X, newHere.Y ].MinCost ) if( newCost >= cellInfo[ newHere.X, newHere.Y ].MinCost )

View File

@@ -9,12 +9,12 @@ using System.Drawing;
namespace OpenRa.Traits namespace OpenRa.Traits
{ {
class BridgeInfo : ITraitInfo class BridgeInfo : OwnedActorInfo, ITraitInfo
{ {
public object Create(Actor self) { return new Bridge(self); } public object Create(Actor self) { return new Bridge(self); }
} }
class Bridge : IRender, ITick, ICustomTerrain class Bridge : IRender, ICustomTerrain
{ {
Dictionary<int2, int> Tiles; Dictionary<int2, int> Tiles;
TileTemplate Template; TileTemplate Template;
@@ -34,8 +34,6 @@ namespace OpenRa.Traits
yield return new Renderable(t.Value, Game.CellSize * t.Key, PaletteType.Gold); yield return new Renderable(t.Value, Game.CellSize * t.Key, PaletteType.Gold);
} }
public void Tick(Actor self) {}
public void SetTiles(TileTemplate template, Dictionary<int2, int> replacedTiles) public void SetTiles(TileTemplate template, Dictionary<int2, int> replacedTiles)
{ {
Template = template; Template = template;
@@ -49,10 +47,10 @@ namespace OpenRa.Traits
a => Sprites[new TileReference { tile = (ushort)template.Index, image = (byte)a.Value }]); a => Sprites[new TileReference { tile = (ushort)template.Index, image = (byte)a.Value }]);
} }
public double GetCost(int2 p, UnitMovementType umt) public float GetCost(int2 p, UnitMovementType umt)
{ {
var origTile = Tiles[p]; // if this explodes, then SetTiles did something horribly wrong. var origTile = Tiles[p]; // if this explodes, then SetTiles did something horribly wrong.
return 1.0; return float.PositiveInfinity;
} }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace OpenRa.Traits
public interface IAcceptThief { void OnSteal(Actor self, Actor thief); } public interface IAcceptThief { void OnSteal(Actor self, Actor thief); }
public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); } public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); }
public interface ICustomTerrain { double GetCost(int2 p, UnitMovementType umt); } public interface ICustomTerrain { float GetCost(int2 p, UnitMovementType umt); }
interface IProducer interface IProducer
{ {