Reimplement ICustomTerrain with far less bs
This commit is contained in:
@@ -165,30 +165,40 @@ namespace OpenRA.Traits
|
|||||||
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
||||||
return float.PositiveInfinity;
|
return float.PositiveInfinity;
|
||||||
|
|
||||||
var type = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
// Custom terrain types don't stack: pick one
|
||||||
return (float)TerrainCost[type]*
|
var customTerrain = self.World.WorldActor.traits.WithInterface<ITerrainTypeModifier>()
|
||||||
self.World.WorldActor.traits.WithInterface<ICustomTerrain>()
|
.Where( t => t.GetTerrainType(cell) != null )
|
||||||
.Select(t => t.GetCost(cell, self))
|
.Select( t => t.GetTerrainType(cell) )
|
||||||
.Product();
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
// Todo: Hack this until i finish migrating TerrainType to a string
|
||||||
|
var type = (customTerrain != null) ? (TerrainType)Enum.Parse(typeof(TerrainType), customTerrain) : self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
||||||
|
var additionalCost = self.World.WorldActor.traits.WithInterface<ITerrainCost>()
|
||||||
|
.Select( t => t.GetTerrainCost(cell, self) ).Sum();
|
||||||
|
|
||||||
|
return TerrainCost[type] + additionalCost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual float MovementSpeedForCell(Actor self, int2 cell)
|
public virtual float MovementSpeedForCell(Actor self, int2 cell)
|
||||||
{
|
{
|
||||||
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
||||||
if( unitInfo == null || !self.World.Map.IsInMap(cell.X,cell.Y))
|
if( unitInfo == null )
|
||||||
return 0f;
|
return 0f;
|
||||||
|
|
||||||
var type = self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
// Custom terrain types don't stack: pick one
|
||||||
var terrain = TerrainSpeed[type]*self.World.WorldActor.traits
|
var customTerrain = self.World.WorldActor.traits.WithInterface<ITerrainTypeModifier>()
|
||||||
.WithInterface<ICustomTerrain>()
|
.Where( t => t.GetTerrainType(cell) != null )
|
||||||
.Select(t => t.GetSpeedModifier(self.Location, self))
|
.Select( t => t.GetTerrainType(cell) )
|
||||||
.Product();
|
.FirstOrDefault();
|
||||||
|
|
||||||
|
// Todo: Hack this until i finish migrating TerrainType to a string
|
||||||
|
var type = (customTerrain != null) ? (TerrainType)Enum.Parse(typeof(TerrainType), customTerrain) : self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[cell.X, cell.Y]);
|
||||||
|
|
||||||
var modifier = self.traits
|
var modifier = self.traits
|
||||||
.WithInterface<ISpeedModifier>()
|
.WithInterface<ISpeedModifier>()
|
||||||
.Select(t => t.GetSpeedModifier())
|
.Select(t => t.GetSpeedModifier())
|
||||||
.Product();
|
.Product();
|
||||||
return unitInfo.Speed * terrain * modifier;
|
return unitInfo.Speed * TerrainSpeed[type] * modifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<float2> GetCurrentPath(Actor self)
|
public IEnumerable<float2> GetCurrentPath(Actor self)
|
||||||
|
|||||||
@@ -57,11 +57,8 @@ namespace OpenRA.Traits
|
|||||||
public interface IProvideHazard { IEnumerable<HazardLayer.Hazard> HazardCells(Actor self); }
|
public interface IProvideHazard { IEnumerable<HazardLayer.Hazard> HazardCells(Actor self); }
|
||||||
public interface IAvoidHazard { string Type { get; } }
|
public interface IAvoidHazard { string Type { get; } }
|
||||||
|
|
||||||
public interface ICustomTerrain
|
public interface ITerrainTypeModifier { string GetTerrainType(int2 cell); }
|
||||||
{
|
public interface ITerrainCost { float GetTerrainCost(int2 cell, Actor forActor); }
|
||||||
float GetCost(int2 p, Actor forActor);
|
|
||||||
float GetSpeedModifier(int2 p, Actor forActor);
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface IDisable { bool Disabled { get; } }
|
public interface IDisable { bool Disabled { get; } }
|
||||||
|
|
||||||
|
|||||||
@@ -31,14 +31,13 @@ namespace OpenRA.Traits
|
|||||||
public object Create( ActorInitializer init ) { return new HazardLayer( init.world ); }
|
public object Create( ActorInitializer init ) { return new HazardLayer( init.world ); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class HazardLayer : ICustomTerrain
|
public class HazardLayer : ITerrainCost
|
||||||
{
|
{
|
||||||
List<Pair<Actor, Hazard>>[,] hazards;
|
List<Pair<Actor, Hazard>>[,] hazards;
|
||||||
Map map;
|
Map map;
|
||||||
|
|
||||||
public HazardLayer( World world )
|
public HazardLayer( World world )
|
||||||
{
|
{
|
||||||
//System.Console.WriteLine("Created HazardLayer");
|
|
||||||
map = world.Map;
|
map = world.Map;
|
||||||
hazards = new List<Pair<Actor, Hazard>>[world.Map.MapSize.X, world.Map.MapSize.Y];
|
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 i = 0; i < world.Map.MapSize.X; i++)
|
||||||
@@ -48,30 +47,21 @@ namespace OpenRA.Traits
|
|||||||
world.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IProvideHazard>() );
|
world.ActorRemoved += a => Remove( a, a.traits.GetOrDefault<IProvideHazard>() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float GetTerrainCost(int2 p, Actor forActor)
|
||||||
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 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);
|
var intensity = hazards[p.X,p.Y].Where(a => avoid.Contains(a.Second.type))
|
||||||
|
.Select(b => b.Second.intensity)
|
||||||
|
.Sum();
|
||||||
|
|
||||||
return intensity;
|
return intensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Add( Actor self, IProvideHazard hazard )
|
public void Add( Actor self, IProvideHazard hazard )
|
||||||
{
|
{
|
||||||
//System.Console.WriteLine("Adding hazard {0}", self.Info.Name);
|
|
||||||
|
|
||||||
foreach( var h in hazard.HazardCells(self) )
|
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));
|
hazards[h.location.X, h.location.Y].Add(Pair.New(self, h));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,12 +23,13 @@ using System.Linq;
|
|||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
using OpenRA.GameRules;
|
using OpenRA.GameRules;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
namespace OpenRA.Traits
|
namespace OpenRA.Traits
|
||||||
{
|
{
|
||||||
public class ResourceLayerInfo : TraitInfo<ResourceLayer> { }
|
public class ResourceLayerInfo : TraitInfo<ResourceLayer> { }
|
||||||
|
|
||||||
public class ResourceLayer: IRenderOverlay, ILoadWorldHook, ICustomTerrain
|
public class ResourceLayer: IRenderOverlay, ILoadWorldHook, ITerrainTypeModifier
|
||||||
{
|
{
|
||||||
SpriteRenderer sr;
|
SpriteRenderer sr;
|
||||||
World world;
|
World world;
|
||||||
@@ -93,28 +94,14 @@ namespace OpenRA.Traits
|
|||||||
content[x, y].density = GetIdealDensity(x, y);
|
content[x, y].density = GetIdealDensity(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetSpeedModifier(int2 p, Actor forActor)
|
public string GetTerrainType(int2 cell)
|
||||||
{
|
{
|
||||||
if (content[p.X,p.Y].type == null)
|
if (content[cell.X,cell.Y].type == null)
|
||||||
return 1.0f;
|
return null;
|
||||||
|
|
||||||
// Todo: Reenable based off something that isn't umt
|
return content[cell.X,cell.Y].type.info.TerrainType;
|
||||||
return 1f;
|
|
||||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
|
||||||
//return content[p.X,p.Y].type.GetSpeedModifier(umt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetCost(int2 p, Actor forActor)
|
|
||||||
{
|
|
||||||
if (content[p.X,p.Y].type == null)
|
|
||||||
return 1.0f;
|
|
||||||
|
|
||||||
// Todo: Reenable based off something that isn't umt
|
|
||||||
return 1f;
|
|
||||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
|
||||||
//return content[p.X,p.Y].type.GetCost(umt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprite[] ChooseContent(ResourceType t)
|
Sprite[] ChooseContent(ResourceType t)
|
||||||
{
|
{
|
||||||
return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)];
|
return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)];
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ namespace OpenRA.Traits
|
|||||||
|
|
||||||
public readonly int ValuePerUnit = 0;
|
public readonly int ValuePerUnit = 0;
|
||||||
public readonly string Name = null;
|
public readonly string Name = null;
|
||||||
|
public readonly string TerrainType = null;
|
||||||
public readonly string MovementTerrainType = null;
|
|
||||||
public readonly string PathingTerrainType = null;
|
|
||||||
|
|
||||||
public Sprite[][] Sprites;
|
public Sprite[][] Sprites;
|
||||||
|
|
||||||
@@ -44,32 +42,9 @@ namespace OpenRA.Traits
|
|||||||
public class ResourceType
|
public class ResourceType
|
||||||
{
|
{
|
||||||
public ResourceTypeInfo info;
|
public ResourceTypeInfo info;
|
||||||
float[] movementSpeed = new float[5];
|
|
||||||
float[] pathCost = new float[5];
|
|
||||||
|
|
||||||
public ResourceType(ResourceTypeInfo info)
|
public ResourceType(ResourceTypeInfo info)
|
||||||
{
|
{
|
||||||
for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Float; umt++ )
|
|
||||||
{
|
|
||||||
// HACK: hardcode "ore" terraintype for now
|
|
||||||
movementSpeed[(int)umt] = (info.MovementTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetSpeedModifier(umt) : 1.0f;
|
|
||||||
pathCost[(int)umt] = (info.PathingTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt)
|
|
||||||
: (info.MovementTerrainType != null) ? (float)Rules.TerrainTypes[TerrainType.Ore].GetCost(umt) : 1.0f;
|
|
||||||
}
|
|
||||||
movementSpeed[(int)UnitMovementType.Fly] = 1.0f;
|
|
||||||
pathCost[(int)UnitMovementType.Fly] = 1.0f;
|
|
||||||
|
|
||||||
this.info = info;
|
this.info = info;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetSpeedModifier(UnitMovementType umt)
|
|
||||||
{
|
|
||||||
return movementSpeed[(int)umt];
|
|
||||||
}
|
|
||||||
|
|
||||||
public float GetCost(UnitMovementType umt)
|
|
||||||
{
|
|
||||||
return pathCost[(int)umt];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,18 +62,21 @@ namespace OpenRA.Traits
|
|||||||
return self.World.WorldActor.traits.Get<AircraftInfluence>().GetUnitsAt(p).Count() == 0;
|
return self.World.WorldActor.traits.Get<AircraftInfluence>().GetUnitsAt(p).Count() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override float MovementCostForCell(Actor self, int2 cell)
|
public virtual float MovementCostForCell(Actor self, int2 cell)
|
||||||
{
|
{
|
||||||
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
if (!self.World.Map.IsInMap(cell.X,cell.Y))
|
||||||
return float.PositiveInfinity;
|
return float.PositiveInfinity;
|
||||||
|
|
||||||
return self.World.WorldActor.traits.WithInterface<ICustomTerrain>().Aggregate(1f, (a, x) => a * x.GetCost(cell,self));
|
var additionalCost = self.World.WorldActor.traits.WithInterface<ITerrainCost>()
|
||||||
|
.Select( t => t.GetTerrainCost(cell, self) ).Sum();
|
||||||
|
|
||||||
|
return additionalCost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override float MovementSpeedForCell(Actor self, int2 cell)
|
public virtual float MovementSpeedForCell(Actor self, int2 cell)
|
||||||
{
|
{
|
||||||
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
var unitInfo = self.Info.Traits.GetOrDefault<UnitInfo>();
|
||||||
if( unitInfo == null || !self.World.Map.IsInMap(cell.X,cell.Y))
|
if( unitInfo == null )
|
||||||
return 0f;
|
return 0f;
|
||||||
|
|
||||||
var modifier = self.traits
|
var modifier = self.traits
|
||||||
@@ -82,7 +85,7 @@ namespace OpenRA.Traits
|
|||||||
.Product();
|
.Product();
|
||||||
return unitInfo.Speed * modifier;
|
return unitInfo.Speed * modifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IEnumerable<int2> OccupiedCells()
|
public override IEnumerable<int2> OccupiedCells()
|
||||||
{
|
{
|
||||||
// Todo: do the right thing when landed
|
// Todo: do the right thing when landed
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System;
|
||||||
using OpenRA.FileFormats;
|
using OpenRA.FileFormats;
|
||||||
using OpenRA.GameRules;
|
using OpenRA.GameRules;
|
||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
@@ -126,14 +127,10 @@ namespace OpenRA.Mods.RA
|
|||||||
self.Health = (int)(self.GetMaxHP() * template.HP);
|
self.Health = (int)(self.GetMaxHP() * template.HP);
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetCost(int2 p, UnitMovementType umt)
|
public string GetTerrainType(int2 cell)
|
||||||
{
|
{
|
||||||
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetCost(umt);
|
// Ugly hack until terraintypes are converted from enums
|
||||||
}
|
return Enum.GetName( typeof(TerrainType), Templates[state].TerrainType[Tiles[cell]]);
|
||||||
|
|
||||||
public float GetSpeedModifier(int2 p, UnitMovementType umt)
|
|
||||||
{
|
|
||||||
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetSpeedModifier(umt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsIntact(Bridge b)
|
static bool IsIntact(Bridge b)
|
||||||
|
|||||||
@@ -27,10 +27,10 @@ namespace OpenRA.Mods.RA
|
|||||||
{
|
{
|
||||||
class BridgeLayerInfo : TraitInfo<BridgeLayer> { }
|
class BridgeLayerInfo : TraitInfo<BridgeLayer> { }
|
||||||
|
|
||||||
class BridgeLayer : ILoadWorldHook, ICustomTerrain
|
class BridgeLayer : ILoadWorldHook, ITerrainTypeModifier
|
||||||
{
|
{
|
||||||
// for tricky things like bridges.
|
// for tricky things like bridges.
|
||||||
Bridge[,] customTerrain;
|
Bridge[,] bridges;
|
||||||
|
|
||||||
void MakeBridges(World w)
|
void MakeBridges(World w)
|
||||||
{
|
{
|
||||||
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.RA
|
|||||||
ConvertBridgeToActor(w, i, j);
|
ConvertBridgeToActor(w, i, j);
|
||||||
|
|
||||||
foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
|
foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
|
||||||
b.FinalizeBridges(w, customTerrain);
|
b.FinalizeBridges(w, bridges);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConvertBridgeToActor(World w, int i, int j)
|
void ConvertBridgeToActor(World w, int i, int j)
|
||||||
@@ -84,30 +84,19 @@ namespace OpenRA.Mods.RA
|
|||||||
var br = a.traits.Get<Bridge>();
|
var br = a.traits.Get<Bridge>();
|
||||||
|
|
||||||
foreach (var t in replacedTiles.Keys)
|
foreach (var t in replacedTiles.Keys)
|
||||||
customTerrain[t.X, t.Y] = br;
|
bridges[t.X, t.Y] = br;
|
||||||
|
|
||||||
br.SetTiles(w, template, replacedTiles);
|
br.SetTiles(w, template, replacedTiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetCost(int2 p, Actor forActor)
|
public string GetTerrainType(int2 cell)
|
||||||
{
|
{
|
||||||
// Todo: Do we even need to reenable this since cost = 100% for everything?
|
if (bridges[ cell.X, cell.Y ] != null)
|
||||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
return bridges[ cell.X, cell.Y ].GetTerrainType(cell);
|
||||||
//if (customTerrain[p.X, p.Y] != null)
|
return null;
|
||||||
// return customTerrain[p.X,p.Y].GetCost(p,umt);
|
|
||||||
return 1f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetSpeedModifier(int2 p, Actor forActor)
|
|
||||||
{
|
|
||||||
// Todo: Do we even need to reenable this since cost = 100% for everything?
|
|
||||||
//var umt = forActor.traits.Get<Mobile>().GetMovementType();
|
|
||||||
//if (customTerrain[p.X, p.Y] != null)
|
|
||||||
// return customTerrain[p.X,p.Y].GetSpeedModifier(p,umt);
|
|
||||||
return 1f;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool IsBridge(World w, ushort t)
|
static bool IsBridge(World w, ushort t)
|
||||||
{
|
{
|
||||||
return w.TileSet.walk[t].Bridge != null;
|
return w.TileSet.walk[t].Bridge != null;
|
||||||
@@ -115,7 +104,7 @@ namespace OpenRA.Mods.RA
|
|||||||
|
|
||||||
public void WorldLoaded(World w)
|
public void WorldLoaded(World w)
|
||||||
{
|
{
|
||||||
customTerrain = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
|
bridges = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y];
|
||||||
MakeBridges(w);
|
MakeBridges(w);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user