From b4540db4063389b8336c88172bb0c60d6868434c Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 21 Mar 2010 17:56:08 +1300 Subject: [PATCH] Begin unfailing movement types / IsBuildable / etc --- OpenRA.FileFormats/TileSet.cs | 4 +-- OpenRA.FileFormats/Walkability.cs | 2 +- OpenRA.Game/Combat.cs | 11 ++++----- OpenRA.Game/Graphics/Minimap.cs | 2 +- OpenRA.Game/PathFinder.cs | 4 +-- OpenRA.Game/TerrainCosts.cs | 13 +++++++--- OpenRA.Game/Traits/Activities/HeliLand.cs | 4 +-- OpenRA.Game/Traits/Activities/UnloadCargo.cs | 4 +-- OpenRA.Game/Traits/Bridge.cs | 4 +-- OpenRA.Game/Traits/ConstructionYard.cs | 2 +- OpenRA.Game/Traits/Crate.cs | 4 +-- OpenRA.Game/Traits/Mobile.cs | 2 +- OpenRA.Game/Traits/ProductionSurround.cs | 2 +- OpenRA.Game/Traits/World/CrateSpawner.cs | 2 +- OpenRA.Game/WorldUtils.cs | 26 +++++++++++--------- 15 files changed, 48 insertions(+), 38 deletions(-) diff --git a/OpenRA.FileFormats/TileSet.cs b/OpenRA.FileFormats/TileSet.cs index eb6ca1d4bb..5f446fc705 100644 --- a/OpenRA.FileFormats/TileSet.cs +++ b/OpenRA.FileFormats/TileSet.cs @@ -72,7 +72,7 @@ namespace OpenRA.FileFormats string tilename = string.Format(pattern, i + 1); if (!walk.ContainsKey((ushort)(start + i))) - walk.Add((ushort)(start + i), Walkability.GetWalkability(tilename)); + walk.Add((ushort)(start + i), Walkability.GetTerrainType(tilename)); using( Stream s = FileSystem.Open( tilename + "." + suffix ) ) { @@ -99,7 +99,7 @@ namespace OpenRA.FileFormats return missingTile; } - public int GetWalkability(TileReference r) + public int GetTerrainType(TileReference r) { if (r.tile == 0xff || r.tile == 0xffff) r.image = 0; diff --git a/OpenRA.FileFormats/Walkability.cs b/OpenRA.FileFormats/Walkability.cs index 3469ab71da..44e23fa7ea 100644 --- a/OpenRA.FileFormats/Walkability.cs +++ b/OpenRA.FileFormats/Walkability.cs @@ -65,7 +65,7 @@ namespace OpenRA.FileFormats } } - public TileTemplate GetWalkability(string terrainName) + public TileTemplate GetTerrainType(string terrainName) { return walkability[terrainName]; } diff --git a/OpenRA.Game/Combat.cs b/OpenRA.Game/Combat.cs index e36cd319ec..f4ea66bbe4 100644 --- a/OpenRA.Game/Combat.cs +++ b/OpenRA.Game/Combat.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. @@ -41,15 +41,14 @@ namespace OpenRA var targetTile = ((1f / Game.CellSize) * loc.ToFloat2()).ToInt2(); - var isWater = world.IsWater(targetTile); - var hitWater = world.IsCellBuildable(targetTile, UnitMovementType.Float); - + var isWater = (Game.world.GetTerrainType(targetTile) == TerrainMovementType.Water); + if (warhead.Explosion != 0) world.AddFrameEndTask( - w => w.Add(new Explosion(w, visualLoc, warhead.Explosion, hitWater))); + w => w.Add(new Explosion(w, visualLoc, warhead.Explosion, isWater))); var impactSound = warhead.ImpactSound; - if (hitWater && warhead.WaterImpactSound != null) + if (isWater && warhead.WaterImpactSound != null) impactSound = warhead.WaterImpactSound; if (impactSound != null) Sound.Play(impactSound + ".aud"); diff --git a/OpenRA.Game/Graphics/Minimap.cs b/OpenRA.Game/Graphics/Minimap.cs index 58dcebc145..8152cba8d2 100644 --- a/OpenRA.Game/Graphics/Minimap.cs +++ b/OpenRA.Game/Graphics/Minimap.cs @@ -92,7 +92,7 @@ namespace OpenRA.Graphics for (var y = 0; y < map.MapSize; y++) for (var x = 0; x < map.MapSize; x++) terrain.SetPixel(x, y, map.IsInMap(x, y) - ? Color.FromArgb(alpha, terrainTypeColors[map.Theater].ColorForTerrainType(tileset.GetWalkability(map.MapTiles[x, y]))) + ? Color.FromArgb(alpha, terrainTypeColors[map.Theater].ColorForTerrainType(tileset.GetTerrainType(map.MapTiles[x, y]))) : shroudColor); return terrain; } diff --git a/OpenRA.Game/PathFinder.cs b/OpenRA.Game/PathFinder.cs index f4200c1472..b08eec0768 100644 --- a/OpenRA.Game/PathFinder.cs +++ b/OpenRA.Game/PathFinder.cs @@ -42,7 +42,7 @@ namespace OpenRA for( int y = 0 ; y < map.MapSize ; y++ ) for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Float; umt++ ) passableCost[(int)umt][ x, y ] = ( world.Map.IsInMap( x, y ) ) - ? (float)TerrainCosts.Cost( umt, world.TileSet.GetWalkability( world.Map.MapTiles[ x, y ] ) ) + ? (float)TerrainCosts.Cost( umt, world.TileSet.GetTerrainType( world.Map.MapTiles[ x, y ] ) ) : float.PositiveInfinity; } @@ -64,7 +64,7 @@ namespace OpenRA using( new PerfSample( "find_unit_path_multiple_src" ) ) { var tilesInRange = world.FindTilesInCircle(target, range) - .Where( t => world.IsCellBuildable( t, umt ) ); + .Where( t => world.IsPathableCell( t, umt ) ); var path = FindPath( PathSearch.FromPoints( world, tilesInRange, src, umt, false ).WithCustomBlocker(AvoidUnitsNear(src, 4))); path.Reverse(); diff --git a/OpenRA.Game/TerrainCosts.cs b/OpenRA.Game/TerrainCosts.cs index d325cfc06f..a873df9152 100644 --- a/OpenRA.Game/TerrainCosts.cs +++ b/OpenRA.Game/TerrainCosts.cs @@ -31,7 +31,7 @@ namespace OpenRA Fly = 4, } - enum TerrainMovementType : byte + public enum TerrainMovementType : byte { Clear = 0, Water = 1, @@ -50,7 +50,8 @@ namespace OpenRA { static float[][] costs = Util.MakeArray(4, a => Util.MakeArray(11, b => float.PositiveInfinity)); - + + static bool[] buildable = Util.MakeArray(11,b => false); static TerrainCosts() { for( int i = 0 ; i < 11 ; i++ ) @@ -62,9 +63,15 @@ namespace OpenRA string val = section.GetValue( ( (UnitMovementType)j ).ToString(), "0%" ); costs[j][i] = 100f / float.Parse(val.Substring(0, val.Length - 1)); } + buildable[i] = (section.GetValue("Buildable", "no") == "yes"); } } - + + public static bool Buildable(int r) + { + return buildable[r]; + } + public static float Cost( UnitMovementType unitMovementType, int r ) { return costs[ (byte)unitMovementType ][ r ]; diff --git a/OpenRA.Game/Traits/Activities/HeliLand.cs b/OpenRA.Game/Traits/Activities/HeliLand.cs index 2f5027ee2a..3818214106 100644 --- a/OpenRA.Game/Traits/Activities/HeliLand.cs +++ b/OpenRA.Game/Traits/Activities/HeliLand.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. @@ -35,7 +35,7 @@ namespace OpenRA.Traits.Activities if (unit.Altitude == 0) return NextActivity; - if (requireSpace && !self.World.IsCellBuildable(self.Location, UnitMovementType.Foot)) + if (requireSpace && !self.World.IsPathableCell(self.Location, UnitMovementType.Foot)) return this; // fail to land if no space --unit.Altitude; diff --git a/OpenRA.Game/Traits/Activities/UnloadCargo.cs b/OpenRA.Game/Traits/Activities/UnloadCargo.cs index f023ecf9eb..b6b851b288 100644 --- a/OpenRA.Game/Traits/Activities/UnloadCargo.cs +++ b/OpenRA.Game/Traits/Activities/UnloadCargo.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. @@ -36,7 +36,7 @@ namespace OpenRA.Traits.Activities for (var i = -1; i < 2; i++) for (var j = -1; j < 2; j++) if ((i != 0 || j != 0) && - self.World.IsCellBuildable(self.Location + new int2(i, j), + self.World.IsPathableCell(self.Location + new int2(i, j), UnitMovementType.Foot)) return self.Location + new int2(i, j); diff --git a/OpenRA.Game/Traits/Bridge.cs b/OpenRA.Game/Traits/Bridge.cs index 99dc12c52f..f447e5b257 100644 --- a/OpenRA.Game/Traits/Bridge.cs +++ b/OpenRA.Game/Traits/Bridge.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. @@ -97,7 +97,7 @@ namespace OpenRA.Traits var numStates = self.Info.Traits.Get().Long ? 6 : 3; for (var n = 0; n < numStates; n++) { - var stateTemplate = world.TileSet.Walkability.GetWalkability(NameFromState(template, n)); + var stateTemplate = world.TileSet.Walkability.GetTerrainType(NameFromState(template, n)); Templates.Add( stateTemplate ); TileSprites.Add(replacedTiles.ToDictionary( diff --git a/OpenRA.Game/Traits/ConstructionYard.cs b/OpenRA.Game/Traits/ConstructionYard.cs index d2e737fb14..160cca540f 100644 --- a/OpenRA.Game/Traits/ConstructionYard.cs +++ b/OpenRA.Game/Traits/ConstructionYard.cs @@ -91,7 +91,7 @@ namespace OpenRA.Traits return self.World.Map.IsInMap(a.X, a.Y) && TerrainCosts.Cost(GetMovementType(), - self.World.TileSet.GetWalkability(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; + self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; } } } diff --git a/OpenRA.Game/Traits/Crate.cs b/OpenRA.Game/Traits/Crate.cs index c3fbad0246..b9761716eb 100644 --- a/OpenRA.Game/Traits/Crate.cs +++ b/OpenRA.Game/Traits/Crate.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. @@ -56,7 +56,7 @@ namespace OpenRA.Traits this.self = self; self.World.WorldActor.traits.Get().Add(self, this); - if (self.World.IsWater(self.Location)) + if (self.World.GetTerrainType(self.Location) == TerrainMovementType.Water) self.traits.Get().anim.PlayRepeating("water"); } diff --git a/OpenRA.Game/Traits/Mobile.cs b/OpenRA.Game/Traits/Mobile.cs index 3401102b33..00854125b1 100644 --- a/OpenRA.Game/Traits/Mobile.cs +++ b/OpenRA.Game/Traits/Mobile.cs @@ -134,7 +134,7 @@ namespace OpenRA.Traits return self.World.Map.IsInMap(a.X, a.Y) && TerrainCosts.Cost(GetMovementType(), - self.World.TileSet.GetWalkability(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; + self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; } public IEnumerable GetCurrentPath() diff --git a/OpenRA.Game/Traits/ProductionSurround.cs b/OpenRA.Game/Traits/ProductionSurround.cs index 6a02267b7d..74cd072fe6 100644 --- a/OpenRA.Game/Traits/ProductionSurround.cs +++ b/OpenRA.Game/Traits/ProductionSurround.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. diff --git a/OpenRA.Game/Traits/World/CrateSpawner.cs b/OpenRA.Game/Traits/World/CrateSpawner.cs index bae94f5ec8..266bb4cafc 100644 --- a/OpenRA.Game/Traits/World/CrateSpawner.cs +++ b/OpenRA.Game/Traits/World/CrateSpawner.cs @@ -1,4 +1,4 @@ -#region Copyright & License Information +#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. diff --git a/OpenRA.Game/WorldUtils.cs b/OpenRA.Game/WorldUtils.cs index dc6c55dd66..30624280ee 100755 --- a/OpenRA.Game/WorldUtils.cs +++ b/OpenRA.Game/WorldUtils.cs @@ -30,19 +30,25 @@ namespace OpenRA { public static class WorldUtils { + public static bool IsPathableCell(this World world, int2 a, UnitMovementType umt) + { + if (world.WorldActor.traits.Get().GetBuildingAt(a) != null) return false; + if (world.WorldActor.traits.Get().GetUnitsAt(a).Any()) return false; + + return world.Map.IsInMap(a.X, a.Y) && TerrainCosts.Cost(umt, world.TileSet.GetTerrainType(world.Map.MapTiles[a.X,a.Y])) < double.PositiveInfinity; + } + public static bool IsCellBuildable(this World world, int2 a, UnitMovementType umt) { return world.IsCellBuildable(a, umt, null); } - + public static bool IsCellBuildable(this World world, int2 a, UnitMovementType umt, Actor toIgnore) { if (world.WorldActor.traits.Get().GetBuildingAt(a) != null) return false; if (world.WorldActor.traits.Get().GetUnitsAt(a).Any(b => b != toIgnore)) return false; - return world.Map.IsInMap(a.X, a.Y) && - TerrainCosts.Cost(umt, - world.TileSet.GetWalkability(world.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; + return world.Map.IsInMap(a.X, a.Y) && TerrainCosts.Buildable(world.TileSet.GetTerrainType(world.Map.MapTiles[a.X, a.Y])); } public static bool IsActorCrushableByActor(this World world, Actor a, Actor b) @@ -64,13 +70,6 @@ namespace OpenRA a.traits.WithInterface() .Any(c => c.IsCrushableBy(umt, a.Owner)); } - - public static bool IsWater(this World world, int2 a) - { - return world.Map.IsInMap(a.X, a.Y) && - TerrainCosts.Cost(UnitMovementType.Float, - world.TileSet.GetWalkability(world.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity; - } public static IEnumerable FindUnitsAtMouse(this World world, int2 mouseLocation) { @@ -127,6 +126,11 @@ namespace OpenRA .FirstOrDefault(); } + public static TerrainMovementType GetTerrainType(this World world, int2 cell) + { + return (TerrainMovementType)world.TileSet.GetTerrainType(world.Map.MapTiles[cell.X, cell.Y]); + } + public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, int2 topLeft, Actor toIgnore) { var res = world.WorldActor.traits.Get();