From b21e1adc2d442645eea443e1b041b4a47fbff677 Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 12 Jan 2010 19:31:06 +1300 Subject: [PATCH] Fixing uses of legacyInfo.WaterBound --- OpenRa.Game/Combat.cs | 2 +- OpenRa.Game/Traits/Buildable.cs | 1 + OpenRa.Game/Traits/Building.cs | 2 +- OpenRa.Game/Traits/Production.cs | 4 +- OpenRa.Game/Traits/ProductionQueue.cs | 2 +- OpenRa.Game/Traits/ProductionSurround.cs | 4 +- OpenRa.Game/Traits/TraitsInterfaces.cs | 2 +- RulesConverter/MiniYamlExts.cs | 100 +++++++++++++++++++++++ 8 files changed, 109 insertions(+), 8 deletions(-) create mode 100755 RulesConverter/MiniYamlExts.cs diff --git a/OpenRa.Game/Combat.cs b/OpenRa.Game/Combat.cs index 2147aaceb3..7cd6c5cfa8 100644 --- a/OpenRa.Game/Combat.cs +++ b/OpenRa.Game/Combat.cs @@ -68,7 +68,7 @@ namespace OpenRa.Game if (unit != null && unit.Altitude > 0) return projectile.AA; - if (projectile.UnderWater && !target.LegacyInfo.WaterBound) + if (projectile.UnderWater && !target.Info.Traits.Get().WaterBound) return false; return projectile.AG; diff --git a/OpenRa.Game/Traits/Buildable.cs b/OpenRa.Game/Traits/Buildable.cs index 273fdf1d97..5a420533e6 100755 --- a/OpenRa.Game/Traits/Buildable.cs +++ b/OpenRa.Game/Traits/Buildable.cs @@ -10,6 +10,7 @@ namespace OpenRa.Game.Traits public readonly int TechLevel = -1; public readonly string Tab = null; public readonly string[] Prerequisites = { }; + public readonly string[] BuiltAt = { }; public readonly Race[] Owner = { }; public readonly int Cost = 0; public readonly string Description = ""; diff --git a/OpenRa.Game/Traits/Building.cs b/OpenRa.Game/Traits/Building.cs index 82a990aea9..b4816bb971 100644 --- a/OpenRa.Game/Traits/Building.cs +++ b/OpenRa.Game/Traits/Building.cs @@ -16,6 +16,7 @@ namespace OpenRa.Game.Traits public readonly bool Crewed = false; // replace with trait? public readonly int InitialFacing = 128; public readonly int Sight = 0; + public readonly bool WaterBound = false; } class BuildingInfo : OwnedActorInfo, ITraitInfo @@ -30,7 +31,6 @@ namespace OpenRa.Game.Traits public readonly string Footprint = "x"; public readonly string[] Produces = { }; // does this go somewhere else? public readonly int2 Dimensions = new int2(1, 1); - public readonly bool WaterBound = false; public readonly bool Unsellable = false; public object Create(Actor self) { return new Building(self); } diff --git a/OpenRa.Game/Traits/Production.cs b/OpenRa.Game/Traits/Production.cs index 0f86a7b41e..794b7e4479 100755 --- a/OpenRa.Game/Traits/Production.cs +++ b/OpenRa.Game/Traits/Production.cs @@ -19,7 +19,7 @@ namespace OpenRa.Game.Traits public Production( Actor self ) { } - public virtual int2? CreationLocation( Actor self, LegacyUnitInfo producee ) + public virtual int2? CreationLocation( Actor self, NewUnitInfo producee ) { return ( 1 / 24f * self.CenterLocation ).ToInt2(); } @@ -29,7 +29,7 @@ namespace OpenRa.Game.Traits return newUnit.Info.Traits.GetOrDefault().InitialFacing; } - public bool Produce( Actor self, LegacyUnitInfo producee ) + public bool Produce( Actor self, NewUnitInfo producee ) { var location = CreationLocation( self, producee ); if( location == null || Game.UnitInfluence.GetUnitsAt( location.Value ).Any() ) diff --git a/OpenRa.Game/Traits/ProductionQueue.cs b/OpenRa.Game/Traits/ProductionQueue.cs index b5945c76ff..559f78fc81 100755 --- a/OpenRa.Game/Traits/ProductionQueue.cs +++ b/OpenRa.Game/Traits/ProductionQueue.cs @@ -126,7 +126,7 @@ namespace OpenRa.Game.Traits public void BuildUnit( string name ) { - var newUnitType = Rules.UnitInfo[ name ]; + var newUnitType = Rules.NewUnitInfo[ name ]; var producerTypes = Rules.TechTree.UnitBuiltAt( newUnitType ); Actor producer = null; diff --git a/OpenRa.Game/Traits/ProductionSurround.cs b/OpenRa.Game/Traits/ProductionSurround.cs index fb81449bb1..0bf3ec8066 100644 --- a/OpenRa.Game/Traits/ProductionSurround.cs +++ b/OpenRa.Game/Traits/ProductionSurround.cs @@ -26,9 +26,9 @@ namespace OpenRa.Game.Traits return null; } - public override int2? CreationLocation(Actor self, LegacyUnitInfo producee) + public override int2? CreationLocation(Actor self, NewUnitInfo producee) { - return FindAdjacentTile(self, producee.WaterBound ? + return FindAdjacentTile(self, producee.Traits.Get().WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel); /* hackety hack */ } diff --git a/OpenRa.Game/Traits/TraitsInterfaces.cs b/OpenRa.Game/Traits/TraitsInterfaces.cs index 1b0b8d7fc7..6a1e6aa0a6 100644 --- a/OpenRa.Game/Traits/TraitsInterfaces.cs +++ b/OpenRa.Game/Traits/TraitsInterfaces.cs @@ -25,7 +25,7 @@ namespace OpenRa.Game.Traits interface IProducer { - bool Produce( Actor self, LegacyUnitInfo producee ); + bool Produce( Actor self, NewUnitInfo producee ); void SetPrimaryProducer(Actor self, bool isPrimary); } interface IOccupySpace { IEnumerable OccupiedCells(); } diff --git a/RulesConverter/MiniYamlExts.cs b/RulesConverter/MiniYamlExts.cs new file mode 100755 index 0000000000..415e024a25 --- /dev/null +++ b/RulesConverter/MiniYamlExts.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using OpenRa.FileFormats; + +namespace RulesConverter +{ + using MiniYamlNodes = Dictionary; + using System.IO; + + static class MiniYamlExts + { + public static void WriteToFile( this MiniYamlNodes y, string filename ) + { + File.WriteAllLines( filename, y.ToLines( true ).ToArray() ); + } + + public static IEnumerable ToLines( this MiniYamlNodes y, bool lowest ) + { + foreach( var kv in y ) + { + foreach( var line in kv.Value.ToLines( kv.Key ) ) + yield return line; + if( lowest ) + yield return ""; + } + } + + public static IEnumerable ToLines( this MiniYaml y, string name ) + { + yield return name + ": " + y.Value; + foreach( var line in y.Nodes.ToLines( false ) ) + yield return "\t" + line; + } + + public static void OptimizeInherits( this MiniYamlNodes y, MiniYamlNodes baseYaml ) + { + foreach( var key in y.Keys.ToList() ) + { + var node = y[ key ]; + MiniYaml inherits; + node.Nodes.TryGetValue( "Inherits", out inherits ); + if( inherits == null || string.IsNullOrEmpty( inherits.Value ) ) + continue; + + MiniYaml parent; + baseYaml.TryGetValue( inherits.Value, out parent ); + if( parent == null ) + continue; + + y[ key ] = Diff( node, parent ); + if( y[ key ] == null ) + y.Remove( key ); + } + } + + public static MiniYamlNodes Diff( MiniYamlNodes a, MiniYamlNodes b ) + { + if( a.Count == 0 && b.Count == 0 ) + return null; + if( b.Count == 0 ) + return a; + if( a.Count == 0 ) + throw new NotImplementedException( "parent has key not in child" ); + + var ret = new MiniYamlNodes(); + + var keys = a.Keys.Union( b.Keys ).ToList(); + + foreach( var key in keys ) + { + MiniYaml aa, bb; + a.TryGetValue( key, out aa ); + b.TryGetValue( key, out bb ); + var diff = Diff( aa, bb ); + if( diff != null ) + ret.Add( key, diff ); + } + + if( ret.Count == 0 ) return null; + return ret; + } + + public static MiniYaml Diff( MiniYaml a, MiniYaml b ) + { + if( a == null && b == null ) + throw new InvalidOperationException( "can't happen" ); + else if( a == null ) + throw new NotImplementedException( "parent has key not in child" ); + else if( b == null ) + return a; + + var diff = Diff( a.Nodes, b.Nodes ); + if( diff == null ) + return null; + return new MiniYaml( a.Value, diff ); + } + } +}