Units automatically path around known hazards (eg static base defenses). Test using SAM and TRAN.

Todo: Ignore hazards when force-moving; Apply only to known enemy hazards; Implement more hazard classes

Also add a few files I forgot from previous patches
This commit is contained in:
Paul Chote
2010-06-25 13:53:37 +12:00
parent 0aeca2aadc
commit 7ac8d0cf59
15 changed files with 378 additions and 16 deletions

View File

@@ -23,8 +23,8 @@ namespace OpenRA.GameRules
public class UserSettings public class UserSettings
{ {
// Debug settings // Debug settings
public bool UnitDebug = false; public bool UnitDebug = true;
public bool PathDebug = false; public bool PathDebug = true;
public bool PerfDebug = true; public bool PerfDebug = true;
public bool IndexDebug = false; public bool IndexDebug = false;
public bool RecordSyncReports = true; public bool RecordSyncReports = true;

View File

@@ -242,6 +242,8 @@
<Compile Include="Widgets\ImageWidget.cs" /> <Compile Include="Widgets\ImageWidget.cs" />
<Compile Include="Traits\SharesCell.cs" /> <Compile Include="Traits\SharesCell.cs" />
<Compile Include="Traits\World\AircraftInfluence.cs" /> <Compile Include="Traits\World\AircraftInfluence.cs" />
<Compile Include="Traits\World\HazardLayer.cs" />
<Compile Include="Traits\Hazardous.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj"> <ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -42,7 +42,7 @@ namespace OpenRA
for( int x = 0 ; x < map.MapSize.X ; x++ ) for( int x = 0 ; x < map.MapSize.X ; x++ )
for( int y = 0 ; y < map.MapSize.Y ; y++ ) for( int y = 0 ; y < map.MapSize.Y ; y++ )
for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Fly; umt++ ) for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Fly; umt++ )
passableCost[(int)umt][ x, y ] = (umt == UnitMovementType.Fly) ? 0f : ( world.Map.IsInMap( x, y ) ) passableCost[(int)umt][ x, y ] = (umt == UnitMovementType.Fly) ? 1f : ( world.Map.IsInMap( x, y ) )
? (float)Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[x, y])] ? (float)Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[x, y])]
.GetCost(umt) .GetCost(umt)
: float.PositiveInfinity; : float.PositiveInfinity;

View File

@@ -33,7 +33,6 @@ namespace OpenRA
public CellInfo[ , ] cellInfo; public CellInfo[ , ] cellInfo;
public PriorityQueue<PathDistance> queue; public PriorityQueue<PathDistance> queue;
public Func<int2, float> heuristic; public Func<int2, float> heuristic;
public UnitMovementType umt;
Func<int2, bool> customBlock; Func<int2, bool> customBlock;
public bool checkForBlocked; public bool checkForBlocked;
public Actor ignoreBuilding; public Actor ignoreBuilding;
@@ -48,9 +47,6 @@ namespace OpenRA
world = self.World; world = self.World;
cellInfo = InitCellInfo(); cellInfo = InitCellInfo();
queue = new PriorityQueue<PathDistance>(); queue = new PriorityQueue<PathDistance>();
umt = self.traits.Get<Mobile>().GetMovementType();
buildingInfluence = world.WorldActor.traits.Get<BuildingInfluence>(); buildingInfluence = world.WorldActor.traits.Get<BuildingInfluence>();
} }
@@ -88,11 +84,13 @@ namespace OpenRA
public int2 Expand( World world, float[][ , ] passableCost ) public int2 Expand( World world, float[][ , ] passableCost )
{ {
var umt = self.traits.Get<Mobile>().GetMovementType();
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;
var thisCost = passableCost[(int)umt][p.Location.X, p.Location.Y]* var thisCost = passableCost[(int)umt][p.Location.X, p.Location.Y]*
world.WorldActor.traits.WithInterface<ICustomTerrain>().Aggregate(1f, (a, x) => a * x.GetCost(p.Location,umt)); world.WorldActor.traits.WithInterface<ICustomTerrain>().Aggregate(1f, (a, x) => a * x.GetCost(p.Location,self));
if (thisCost == float.PositiveInfinity) if (thisCost == float.PositiveInfinity)
return p.Location; return p.Location;
@@ -107,7 +105,7 @@ namespace OpenRA
var costHere = passableCost[(int)umt][newHere.X, newHere.Y]* var costHere = passableCost[(int)umt][newHere.X, newHere.Y]*
world.WorldActor.traits.WithInterface<ICustomTerrain>() world.WorldActor.traits.WithInterface<ICustomTerrain>()
.Aggregate(1f, (a, x) => a * x.GetCost(newHere,umt)); .Aggregate(1f, (a, x) => a * x.GetCost(newHere,self));
if (costHere == float.PositiveInfinity) if (costHere == float.PositiveInfinity)
continue; continue;

View File

@@ -0,0 +1,52 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Linq;
using System.Collections.Generic;
namespace OpenRA.Traits
{
class AntiAirInfo : ITraitInfo
{
public readonly float Badness = 1000f;
public object Create( ActorInitializer init ) { return new AntiAir( init.self ); }
}
class AntiAir : IProvideHazard
{
public AntiAir(Actor self)
{
self.World.WorldActor.traits.Get<HazardLayer>().Add( self, this );
}
public IEnumerable<HazardLayer.Hazard> HazardCells(Actor self)
{
var info = self.Info.Traits.Get<AntiAirInfo>();
return self.World.FindTilesInCircle(self.Location, (int)self.GetPrimaryWeapon().Range).Select(
t => new HazardLayer.Hazard(){location = t, type = "antiair", intensity = info.Badness});
}
}
class AvoidsAAInfo : TraitInfo<AvoidsAA> {}
class AvoidsAA : IAvoidHazard
{
public string Type { get { return "antiair"; } }
}
}

View File

@@ -22,6 +22,7 @@ using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
@@ -53,11 +54,13 @@ namespace OpenRA.Traits
public interface INotifyCapture { void OnCapture(Actor self, Actor captor); } public interface INotifyCapture { void OnCapture(Actor self, Actor captor); }
public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); } public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); }
public interface INotifyEnterCell { void OnEnterCell(Actor self, int2 cell); } public interface INotifyEnterCell { void OnEnterCell(Actor self, int2 cell); }
public interface IProvideHazard { IEnumerable<HazardLayer.Hazard> HazardCells(Actor self); }
public interface IAvoidHazard { string Type { get; } }
public interface ICustomTerrain public interface ICustomTerrain
{ {
float GetCost(int2 p, UnitMovementType umt); float GetCost(int2 p, Actor forActor);
float GetSpeedModifier(int2 p, UnitMovementType umt); float GetSpeedModifier(int2 p, Actor forActor);
} }
public interface IDisable { bool Disabled { get; } } public interface IDisable { bool Disabled { get; } }

View File

@@ -149,7 +149,7 @@ namespace OpenRA.Traits
var tt = self.World.GetTerrainType(self.Location); var tt = self.World.GetTerrainType(self.Location);
terrain = Rules.TerrainTypes[tt].GetSpeedModifier(umt)*self.World.WorldActor.traits terrain = Rules.TerrainTypes[tt].GetSpeedModifier(umt)*self.World.WorldActor.traits
.WithInterface<ICustomTerrain>() .WithInterface<ICustomTerrain>()
.Select(t => t.GetSpeedModifier(self.Location, umt)) .Select(t => t.GetSpeedModifier(self.Location, self))
.Product(); .Product();
} }
var modifier = self.traits var modifier = self.traits

View File

@@ -0,0 +1,97 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenRA.FileFormats;
namespace OpenRA.Traits
{
public class AircraftInfluenceInfo : ITraitInfo
{
public object Create( ActorInitializer init ) { return new AircraftInfluence( init.world ); }
}
public class AircraftInfluence : ITick
{
List<Actor>[,] influence;
Map map;
public AircraftInfluence( World world )
{
map = world.Map;
influence = new List<Actor>[world.Map.MapSize.X, world.Map.MapSize.Y];
for (int i = 0; i < world.Map.MapSize.X; i++)
for (int j = 0; j < world.Map.MapSize.Y; j++)
influence[ i, j ] = new List<Actor>();
world.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IOccupyAir>() );
}
public void Tick( Actor self )
{
SanityCheck( self );
}
[Conditional( "SANITY_CHECKS" )]
void SanityCheck( Actor self )
{
for( int x = 0 ; x < self.World.Map.MapSize.X ; x++ )
for( int y = 0 ; y < self.World.Map.MapSize.Y ; y++ )
if( influence[ x, y ] != null )
foreach (var a in influence[ x, y ])
if (!a.traits.Get<IOccupyAir>().OccupiedAirCells().Contains( new int2( x, y ) ) )
throw new InvalidOperationException( "AIM: Sanity check failed A" );
foreach( var t in self.World.Queries.WithTraitMultiple<IOccupyAir>() )
foreach( var cell in t.Trait.OccupiedAirCells() )
if (!influence[cell.X, cell.Y].Contains(t.Actor))
throw new InvalidOperationException( "AIM: Sanity check failed B" );
}
Actor[] noActors = { };
public IEnumerable<Actor> GetUnitsAt( int2 a )
{
if (!map.IsInMap(a)) return noActors;
return influence[ a.X, a.Y ];
}
public void Add( Actor self, IOccupyAir unit )
{
foreach( var c in unit.OccupiedAirCells() )
influence[c.X, c.Y].Add(self);
}
public void Remove( Actor self, IOccupyAir unit )
{
if (unit != null)
foreach (var c in unit.OccupiedAirCells())
influence[c.X, c.Y].Remove(self);
}
public void Update(Actor self, IOccupyAir unit)
{
Remove(self, unit);
if (!self.IsDead) Add(self, unit);
}
}
}

View File

@@ -0,0 +1,99 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using OpenRA.FileFormats;
namespace OpenRA.Traits
{
public class HazardLayerInfo : ITraitInfo
{
public object Create( ActorInitializer init ) { return new HazardLayer( init.world ); }
}
public class HazardLayer : ICustomTerrain
{
List<Pair<Actor, Hazard>>[,] hazards;
Map map;
public HazardLayer( World world )
{
//System.Console.WriteLine("Created HazardLayer");
map = world.Map;
hazards = new List<Pair<Actor, Hazard>>[world.Map.MapSize.X, world.Map.MapSize.Y];
for (int i = 0; i < world.Map.MapSize.X; i++)
for (int j = 0; j < world.Map.MapSize.Y; j++)
hazards[ i, j ] = new List<Pair<Actor, Hazard>>();
world.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IProvideHazard>() );
}
public float GetSpeedModifier(int2 p, Actor forActor)
{
return 1f;
}
public float GetCost(int2 p, Actor forActor)
{
//System.Console.WriteLine("GetCost for {0}", forActor.Info.Name);
var avoid = forActor.traits.WithInterface<IAvoidHazard>().Select(h => h.Type).ToList();
var intensity = hazards[p.X,p.Y].Aggregate(1f,(a,b) => a + (avoid.Contains(b.Second.type) ? b.Second.intensity : 0f));
//System.Console.WriteLine("Avoid {0} cost {1}", avoid.Aggregate("",(a,b) => a+","+b), intensity);
return intensity;
}
public void Add( Actor self, IProvideHazard hazard )
{
//System.Console.WriteLine("Adding hazard {0}", self.Info.Name);
foreach( var h in hazard.HazardCells(self) )
{
// System.Console.WriteLine("\t{0} {1} {2}", h.location, h.type, h.intensity);
hazards[h.location.X, h.location.Y].Add(Pair.New(self, h));
}
}
public void Remove( Actor self, IProvideHazard hazard )
{
if (hazard != null)
foreach (var h in hazard.HazardCells(self))
hazards[h.location.X, h.location.Y].Remove(Pair.New(self,h));
}
public void Update(Actor self, IProvideHazard hazard)
{
Remove(self, hazard);
if (!self.IsDead) Add(self, hazard);
}
public struct Hazard
{
public int2 location;
public string type;
public float intensity;
}
}
}

View File

@@ -93,17 +93,21 @@ namespace OpenRA.Traits
content[x, y].density = GetIdealDensity(x, y); content[x, y].density = GetIdealDensity(x, y);
} }
public float GetSpeedModifier(int2 p, UnitMovementType umt) public float GetSpeedModifier(int2 p, Actor forActor)
{ {
if (content[p.X,p.Y].type == null) if (content[p.X,p.Y].type == null)
return 1.0f; return 1.0f;
var umt = forActor.traits.Get<Mobile>().GetMovementType();
return content[p.X,p.Y].type.GetSpeedModifier(umt); return content[p.X,p.Y].type.GetSpeedModifier(umt);
} }
public float GetCost(int2 p,UnitMovementType umt) public float GetCost(int2 p, Actor forActor)
{ {
if (content[p.X,p.Y].type == null) if (content[p.X,p.Y].type == null)
return 1.0f; return 1.0f;
var umt = forActor.traits.Get<Mobile>().GetMovementType();
return content[p.X,p.Y].type.GetCost(umt); return content[p.X,p.Y].type.GetCost(umt);
} }

View File

@@ -0,0 +1,100 @@
#region Copyright & License Information
/*
* Copyright 2007,2009,2010 Chris Forbes, Robert Pepperell, Matthew Bowra-Dean, Paul Chote, Alli Witheford.
* This file is part of OpenRA.
*
* OpenRA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenRA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Traits
{
public class MobileAirInfo : MobileInfo
{
public readonly int CruiseAltitude = 20;
public readonly float InstabilityMagnitude = 2.0f;
public readonly int InstabilityTicks = 5;
public readonly bool LandWhenIdle = true;
public override object Create(ActorInitializer init) { return new MobileAir(init, this); }
}
public class MobileAir : Mobile, ITick, IOccupyAir
{
MobileAirInfo Info;
public MobileAir (ActorInitializer init, MobileAirInfo info)
: base(init)
{
Info = info;
}
public override void AddInfluence()
{
self.World.WorldActor.traits.Get<AircraftInfluence>().Add( self, this );
}
public override void RemoveInfluence()
{
self.World.WorldActor.traits.Get<AircraftInfluence>().Remove( self, this );
}
public override bool CanEnterCell(int2 p, Actor ignoreBuilding, bool checkTransientActors)
{
if (!checkTransientActors)
return true;
return self.World.WorldActor.traits.Get<AircraftInfluence>().GetUnitsAt(p).Count() == 0;
}
public override IEnumerable<int2> OccupiedCells()
{
// Todo: do the right thing when landed
return new int2[] {};
}
public int2 TopLeft { get { return toCell; } }
public IEnumerable<int2> OccupiedAirCells()
{
return (fromCell == toCell)
? new[] { fromCell }
: CanEnterCell(toCell)
? new[] { toCell }
: new[] { fromCell, toCell };
}
int offsetTicks = 0;
public void Tick(Actor self)
{
var unit = self.traits.Get<Unit>();
//if (unit.Altitude <= 0)
// return;
if (unit.Altitude < Info.CruiseAltitude)
unit.Altitude++;
if (--offsetTicks <= 0)
{
self.CenterLocation += Info.InstabilityMagnitude * self.World.SharedRandom.Gauss2D(5);
unit.Altitude += (int)(Info.InstabilityMagnitude * self.World.SharedRandom.Gauss1D(5));
offsetTicks = Info.InstabilityTicks;
}
}
}
}

View File

@@ -90,14 +90,17 @@ namespace OpenRA.Mods.RA
} }
} }
public float GetCost(int2 p, UnitMovementType umt) public float GetCost(int2 p, Actor forActor)
{ {
var umt = forActor.traits.Get<Mobile>().GetMovementType();
if (customTerrain[p.X, p.Y] != null) if (customTerrain[p.X, p.Y] != null)
return customTerrain[p.X,p.Y].GetCost(p,umt); return customTerrain[p.X,p.Y].GetCost(p,umt);
return 1f; return 1f;
} }
public float GetSpeedModifier(int2 p, UnitMovementType umt)
public float GetSpeedModifier(int2 p, Actor forActor)
{ {
var umt = forActor.traits.Get<Mobile>().GetMovementType();
if (customTerrain[p.X, p.Y] != null) if (customTerrain[p.X, p.Y] != null)
return customTerrain[p.X,p.Y].GetSpeedModifier(p,umt); return customTerrain[p.X,p.Y].GetSpeedModifier(p,umt);
return 1f; return 1f;

View File

@@ -519,6 +519,8 @@ SAM:
AttackTurreted: AttackTurreted:
PrimaryWeapon: Nike PrimaryWeapon: Nike
AutoTarget: AutoTarget:
AntiAir:
Badness: 10
-RenderBuilding: -RenderBuilding:
RenderRangeCircle: RenderRangeCircle:

View File

@@ -48,6 +48,7 @@ World:
BuildingInfluence: BuildingInfluence:
UnitInfluence: UnitInfluence:
AircraftInfluence: AircraftInfluence:
HazardLayer:
# BridgeLoadHook: # BridgeLoadHook:
Theater@DESERT: Theater@DESERT:
Name:Desert Name:Desert

View File

@@ -407,6 +407,7 @@ TRAN:
Sight: 8 Sight: 8
Speed: 15 Speed: 15
# Helicopter: # Helicopter:
AvoidsAA:
MobileAir: MobileAir:
MovementType: Fly MovementType: Fly
RenderUnitRotor: RenderUnitRotor: