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:
52
OpenRA.Game/Traits/Hazardous.cs
Normal file
52
OpenRA.Game/Traits/Hazardous.cs
Normal 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"; } }
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Traits
|
||||
{
|
||||
@@ -53,11 +54,13 @@ namespace OpenRA.Traits
|
||||
public interface INotifyCapture { void OnCapture(Actor self, Actor captor); }
|
||||
public interface IAcceptSpy { void OnInfiltrate(Actor self, Actor spy); }
|
||||
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
|
||||
{
|
||||
float GetCost(int2 p, UnitMovementType umt);
|
||||
float GetSpeedModifier(int2 p, UnitMovementType umt);
|
||||
float GetCost(int2 p, Actor forActor);
|
||||
float GetSpeedModifier(int2 p, Actor forActor);
|
||||
}
|
||||
|
||||
public interface IDisable { bool Disabled { get; } }
|
||||
|
||||
@@ -149,7 +149,7 @@ namespace OpenRA.Traits
|
||||
var tt = self.World.GetTerrainType(self.Location);
|
||||
terrain = Rules.TerrainTypes[tt].GetSpeedModifier(umt)*self.World.WorldActor.traits
|
||||
.WithInterface<ICustomTerrain>()
|
||||
.Select(t => t.GetSpeedModifier(self.Location, umt))
|
||||
.Select(t => t.GetSpeedModifier(self.Location, self))
|
||||
.Product();
|
||||
}
|
||||
var modifier = self.traits
|
||||
|
||||
97
OpenRA.Game/Traits/World/AircraftInfluence.cs
Normal file
97
OpenRA.Game/Traits/World/AircraftInfluence.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
99
OpenRA.Game/Traits/World/HazardLayer.cs
Normal file
99
OpenRA.Game/Traits/World/HazardLayer.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,17 +93,21 @@ namespace OpenRA.Traits
|
||||
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)
|
||||
return 1.0f;
|
||||
|
||||
var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
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)
|
||||
return 1.0f;
|
||||
|
||||
var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
||||
return content[p.X,p.Y].type.GetCost(umt);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user