diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index f82b46508a..4d1ff9db93 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -8,7 +8,8 @@ using IrrKlang; using IjwFramework.Collections; using System; using IjwFramework.Types; -using OpenRa.Game.Traits; +using OpenRa.Game.Traits; +using OpenRa.Game.GameRules; namespace OpenRa.Game { @@ -185,8 +186,22 @@ namespace OpenRa.Game public static readonly Pair SovietVoices = Pair.New( new VoicePool("ackno", "affirm1", "noprob", "overout", "ritaway", "roger", "ugotit"), - new VoicePool("await1", "ready", "report1", "yessir1")); - + new VoicePool("await1", "ready", "report1", "yessir1")); + + static int2? FindAdjacentTile(Actor a, UnitMovementType umt) + { + var tiles = Footprint.Tiles(a); + var min = tiles.Aggregate(int2.Min) - new int2(1, 1); + var max = tiles.Aggregate(int2.Max) + new int2(1, 1); + + for (var j = min.Y; j <= max.Y; j++) + for (var i = min.X; i <= max.X; i++) + if (IsCellBuildable(new int2(i, j), umt)) + return new int2(i, j); + + return null; + } + public static void BuildUnit(Player player, string name) { var producerTypes = Rules.UnitInfo[name].BuiltAt; @@ -195,12 +210,29 @@ namespace OpenRa.Game && producerTypes.Contains(a.unitInfo.Name) && a.Owner == player); if (producer == null) - throw new InvalidOperationException("BuildUnit without suitable production structure!"); - - var unit = new Actor(name, (1/24f * producer.CenterLocation).ToInt2(), player); - var mobile = unit.traits.Get(); - mobile.facing = 128; - mobile.QueueActivity( new Traits.Mobile.MoveTo( unit.Location + new int2( 0, 3 ) ) ); + throw new InvalidOperationException("BuildUnit without suitable production structure!"); + + Actor unit; + + if (producerTypes.Contains("spen") || producerTypes.Contains("syrd")) + { + var space = FindAdjacentTile(producer, Rules.UnitInfo[name].WaterBound ? + UnitMovementType.Float : UnitMovementType.Wheel ); /* hackety hack */ + + if (space == null) + throw new NotImplementedException("Nowhere to place this unit."); + + unit = new Actor(name, space.Value, player); + var mobile = unit.traits.Get(); + mobile.facing = SharedRandom.Next(256); + } + else + { + unit = new Actor(name, (1 / 24f * producer.CenterLocation).ToInt2(), player); + var mobile = unit.traits.Get(); + mobile.facing = 128; + mobile.QueueActivity(new Traits.Mobile.MoveTo(unit.Location + new int2(0, 3))); + } world.AddFrameEndTask(_ => world.Add(unit)); diff --git a/OpenRa.Game/GameRules/Footprint.cs b/OpenRa.Game/GameRules/Footprint.cs index efd384b35e..3a8441a347 100644 --- a/OpenRa.Game/GameRules/Footprint.cs +++ b/OpenRa.Game/GameRules/Footprint.cs @@ -10,25 +10,38 @@ namespace OpenRa.Game.GameRules static class Footprint { public static IEnumerable Tiles( UnitInfo unitInfo, int2 position ) + { + return Tiles(unitInfo, position, true); + } + + static IEnumerable Tiles(UnitInfo unitInfo, int2 position, bool adjustForPlacement) { var buildingInfo = unitInfo as UnitInfo.BuildingInfo; var dim = buildingInfo.Dimensions; - var footprint = buildingInfo.Footprint.ToCharArray().Where( x => !char.IsWhiteSpace( x ) ); - if( buildingInfo.Bib ) + var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x)); + if (buildingInfo.Bib) { dim.Y += 1; - footprint = footprint.Concat( new char[ dim.X ] ); + footprint = footprint.Concat(new char[dim.X]); } - foreach( var tile in TilesWhere( unitInfo.Name, dim, footprint.ToArray(), a => a != '_' ) ) - yield return tile + position - AdjustForBuildingSize( buildingInfo ); + + var adjustment = adjustForPlacement ? AdjustForBuildingSize(buildingInfo) : int2.Zero; + + var tiles = TilesWhere(unitInfo.Name, dim, footprint.ToArray(), a => a != '_'); + return tiles.Select(t => t + position - adjustment); + } + + public static IEnumerable Tiles(Actor a) + { + return Tiles(a.unitInfo, a.Location, false); } public static IEnumerable UnpathableTiles( UnitInfo unitInfo, int2 position ) { var buildingInfo = unitInfo as UnitInfo.BuildingInfo; - var footprint = buildingInfo.Footprint.ToCharArray().Where( x => !char.IsWhiteSpace( x ) ).ToArray(); + var footprint = buildingInfo.Footprint.Where( x => !char.IsWhiteSpace( x ) ).ToArray(); foreach( var tile in TilesWhere( unitInfo.Name, buildingInfo.Dimensions, footprint, a => a == 'x' ) ) yield return tile + position; } @@ -38,15 +51,14 @@ namespace OpenRa.Game.GameRules if( footprint.Length != dim.X * dim.Y ) throw new InvalidOperationException( "Invalid footprint for " + name ); int index = 0; + for( int y = 0 ; y < dim.Y ; y++ ) - { for( int x = 0 ; x < dim.X ; x++ ) { if( cond( footprint[ index ] ) ) yield return new int2( x, y ); ++index; } - } } public static int2 AdjustForBuildingSize( string name )