rules-based terrain cost, part 1

This commit is contained in:
Chris Forbes
2010-04-02 15:14:58 +13:00
parent 45f9ec2f7e
commit 0596b08f2c
25 changed files with 235 additions and 239 deletions

View File

@@ -34,6 +34,7 @@ namespace OpenRA
public static Dictionary<string, ActorInfo> Info;
public static Dictionary<string, WeaponInfo> Weapons;
public static Dictionary<string, VoiceInfo> Voices;
public static Dictionary<TerrainType, TerrainCost> TerrainTypes;
public static void LoadRules(string map, Manifest m)
{
@@ -48,6 +49,8 @@ namespace OpenRA
Info = LoadYamlRules(m.Rules, (k, y) => new ActorInfo(k.Key.ToLowerInvariant(), k.Value, y));
Weapons = LoadYamlRules(m.Weapons, (k, _) => new WeaponInfo(k.Key.ToLowerInvariant(), k.Value));
Voices = LoadYamlRules(m.Voices, (k, _) => new VoiceInfo(k.Value));
TerrainTypes = LoadYamlRules(m.Terrain, (k, _) => new TerrainCost(k.Value))
.ToDictionary(kv => (TerrainType)Enum.Parse(typeof(TerrainType), kv.Key, true), kv => kv.Value);
TechTree = new TechTree();
}

View File

@@ -18,10 +18,11 @@
*/
#endregion
using OpenRA.Graphics;
using System;
using OpenRA.FileFormats;
using OpenRA.Graphics;
namespace OpenRA
namespace OpenRA.GameRules
{
public enum UnitMovementType : byte
{
@@ -32,35 +33,26 @@ namespace OpenRA
Fly = 4,
}
static class TerrainCosts
public class TerrainCost
{
static float[][] costs = Util.MakeArray<float[]>(4,
a => Util.MakeArray<float>(11, b => float.PositiveInfinity));
static bool[] buildable = Util.MakeArray<bool>(11,b => false);
static TerrainCosts()
public readonly bool Buildable = true;
public readonly float Foot = 0, Track = 0, Wheel = 0, Float = 0;
public readonly bool AcceptSmudge = true;
public TerrainCost(MiniYaml y) { FieldLoader.Load(this, y); }
public float GetCost(UnitMovementType umt)
{
for( int i = 0 ; i < 11 ; i++ )
switch (umt) /* todo: make this nice */
{
if( i == 4 ) continue;
var section = Rules.AllRules.GetSection( ( (TerrainType)i ).ToString() );
for( int j = 0 ; j < 4 ; j++ )
{
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");
case UnitMovementType.Fly: return 1;
case UnitMovementType.Foot: return 1 / Foot;
case UnitMovementType.Wheel: return 1 / Wheel;
case UnitMovementType.Track: return 1 / Track;
case UnitMovementType.Float: return 1 / Float;
default:
throw new InvalidOperationException("wtf?");
}
}
public static bool Buildable(TerrainType r)
{
return buildable[(int)r];
}
public static float Cost( UnitMovementType unitMovementType, TerrainType r )
{
return costs[ (byte)unitMovementType ][ (int)r ];
}
}
}

View File

@@ -188,7 +188,7 @@
<Compile Include="Graphics\Sprite.cs" />
<Compile Include="Graphics\SpriteRenderer.cs" />
<Compile Include="Graphics\SpriteSheetBuilder.cs" />
<Compile Include="TerrainCosts.cs" />
<Compile Include="GameRules\TerrainCost.cs" />
<Compile Include="Graphics\TerrainRenderer.cs" />
<Compile Include="Traits\Activities\Harvest.cs" />
<Compile Include="Traits\Activities\Move.cs" />

View File

@@ -24,6 +24,7 @@ using System.Diagnostics;
using System.Linq;
using OpenRA.Support;
using OpenRA.Traits;
using OpenRA.GameRules;
namespace OpenRA
{
@@ -38,11 +39,12 @@ namespace OpenRA
var map = world.Map;
for (var umt = UnitMovementType.Foot; umt <= UnitMovementType.Float; umt++)
passableCost[(int)umt] = new float[map.MapSize, map.MapSize];
for( int x = 0 ; x < map.MapSize ; x++ )
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.GetTerrainType( world.Map.MapTiles[ x, y ] ) )
for (int x = 0; x < map.MapSize; x++)
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)Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[x, y])]
.GetCost(umt)
: float.PositiveInfinity;
}

View File

@@ -23,6 +23,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits;
using OpenRA.GameRules;
namespace OpenRA
{

View File

@@ -19,6 +19,7 @@
#endregion
using System.Linq;
using OpenRA.GameRules;
namespace OpenRA.Traits.Activities
{

View File

@@ -18,6 +18,7 @@
*/
#endregion
using OpenRA.GameRules;
namespace OpenRA.Traits.Activities
{
class HeliLand : IActivity

View File

@@ -19,6 +19,7 @@
#endregion
using System.Linq;
using OpenRA.GameRules;
namespace OpenRA.Traits.Activities
{

View File

@@ -23,6 +23,7 @@ using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.GameRules;
namespace OpenRA.Traits
{
@@ -128,10 +129,7 @@ namespace OpenRA.Traits
public float GetCost(int2 p, UnitMovementType umt)
{
// just use the standard walkability from templates.ini. no hackery.
return TerrainCosts.Cost(umt,
Templates[state].TerrainType[Tiles[p]]);
return Rules.TerrainTypes[Templates[state].TerrainType[Tiles[p]]].GetCost(umt);
}
static bool IsIntact(Bridge b)

View File

@@ -20,6 +20,7 @@
using System.Collections.Generic;
using OpenRA.Traits.Activities;
using OpenRA.GameRules;
namespace OpenRA.Traits
{

View File

@@ -19,6 +19,7 @@
#endregion
using OpenRA.Traits.Activities;
using OpenRA.GameRules;
namespace OpenRA.Traits
{
@@ -66,10 +67,7 @@ namespace OpenRA.Traits
}
// HACK: This should make reference to an MCV actor, and use of its Mobile trait
public UnitMovementType GetMovementType()
{
return UnitMovementType.Wheel;
}
public UnitMovementType GetMovementType() { return UnitMovementType.Wheel; }
public bool CanEnterCell(int2 a)
{
@@ -88,10 +86,10 @@ namespace OpenRA.Traits
}
if (!crushable) return false;
return self.World.Map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(GetMovementType(),
self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
Rules.TerrainTypes[self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])]
.GetCost(GetMovementType()) < float.PositiveInfinity;
}
}
}

View File

@@ -21,6 +21,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.GameRules;
/*
* Crates left to implement:
@@ -75,24 +76,15 @@ namespace OpenRA.Traits
n -= s.Second;
}
public bool IsPathableCrush(UnitMovementType umt, Player player)
{
return true;
}
public bool IsCrushableBy(UnitMovementType umt, Player player)
{
return true;
}
public bool IsPathableCrush(UnitMovementType umt, Player player) { return true; }
public bool IsCrushableBy(UnitMovementType umt, Player player) { return true; }
public IEnumerable<int2> OccupiedCells() { yield return self.Location; }
public void Tick(Actor self)
{
if (++ticks >= self.Info.Traits.Get<CrateInfo>().Lifetime * 25)
{
self.World.AddFrameEndTask(w => w.Remove(self));
}
}
}
}

View File

@@ -20,6 +20,7 @@
using System;
using OpenRA.Traits.Activities;
using OpenRA.GameRules;
namespace OpenRA.Traits
{

View File

@@ -20,6 +20,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
namespace OpenRA.Traits
{
@@ -131,10 +132,10 @@ namespace OpenRA.Traits
}
if (!crushable) return false;
return self.World.Map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(GetMovementType(),
self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
Rules.TerrainTypes[self.World.TileSet.GetTerrainType(self.World.Map.MapTiles[a.X, a.Y])]
.GetCost(GetMovementType()) < float.PositiveInfinity;
}
public IEnumerable<int2> GetCurrentPath()

View File

@@ -21,6 +21,7 @@
using System;
using System.Linq;
using OpenRA.Traits.Activities;
using OpenRA.GameRules;
namespace OpenRA.Traits
{

View File

@@ -18,6 +18,7 @@
*/
#endregion
using OpenRA.GameRules;
namespace OpenRA.Traits
{
class SquishByTankInfo : ITraitInfo

View File

@@ -34,8 +34,10 @@ namespace OpenRA
{
if (world.WorldActor.traits.Get<BuildingInfluence>().GetBuildingAt(a) != null) return false;
if (world.WorldActor.traits.Get<UnitInfluence>().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;
return world.Map.IsInMap(a.X, a.Y) &&
Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[a.X, a.Y])]
.GetCost(umt) < float.PositiveInfinity;
}
public static bool IsCellBuildable(this World world, int2 a, bool waterBound)
@@ -50,8 +52,10 @@ namespace OpenRA
if (waterBound)
return world.Map.IsInMap(a.X,a.Y) && GetTerrainType(world,a) == TerrainType.Water;
return world.Map.IsInMap(a.X, a.Y) && TerrainCosts.Buildable(world.TileSet.GetTerrainType(world.Map.MapTiles[a.X, a.Y]));
return world.Map.IsInMap(a.X, a.Y) &&
Rules.TerrainTypes[world.TileSet.GetTerrainType(world.Map.MapTiles[a.X, a.Y])]
.Buildable;
}
public static bool IsActorCrushableByActor(this World world, Actor a, Actor b)