diff --git a/OpenRA.Mods.RA/Bridge.cs b/OpenRA.Mods.RA/Bridge.cs index 4d78e3a479..717a5ae1fe 100644 --- a/OpenRA.Mods.RA/Bridge.cs +++ b/OpenRA.Mods.RA/Bridge.cs @@ -33,121 +33,119 @@ namespace OpenRA.Mods.RA { public readonly bool Long = false; - public readonly string Template = null; - public readonly string DamagedTemplate = null; - public readonly string DestroyedTemplate = null; + public readonly ushort Template; + public readonly float DamagedThreshold = 0.5f; + public readonly ushort DamagedTemplate; + public readonly ushort DestroyedTemplate; // For long bridges - public readonly string DestroyedPlusNorthTemplate = null; - public readonly string DestroyedPlusSouthTemplate = null; - public readonly string DestroyedPlusBothTemplate = null; + public readonly ushort DestroyedPlusNorthTemplate; + public readonly ushort DestroyedPlusSouthTemplate; + public readonly ushort DestroyedPlusBothTemplate; public readonly bool UseAlternateNames = false; public readonly int[] NorthOffset = null; public readonly int[] SouthOffset = null; - public object Create(ActorInitializer init) { return new Bridge(init.self); } + public object Create(ActorInitializer init) { return new Bridge(init.self, this); } + + public IEnumerable Templates + { get { + + if (Template != 0) + yield return Template; + + if (DamagedTemplate != 0) + yield return DamagedTemplate; + + if (DestroyedTemplate != 0) + yield return DestroyedTemplate; + + if (DestroyedPlusNorthTemplate != 0) + yield return DestroyedPlusNorthTemplate; + + if (DestroyedPlusSouthTemplate != 0) + yield return DestroyedPlusSouthTemplate; + + if (DestroyedPlusBothTemplate != 0) + yield return DestroyedPlusBothTemplate; + } } } - class Bridge //: IRender, INotifyDamage + class Bridge: IRender, INotifyDamage { - Dictionary Tiles; - List> TileSprites = new List>(); - List Templates = new List(); - Actor self; - int state; - Bridge northNeighbour, southNeighbour; - - public Bridge(Actor self) { this.self = self; self.RemoveOnDeath = false; } - static string cachedTileset; static Cache, Sprite> sprites; - public IEnumerable Render(Actor self) + Dictionary> TileSprites = new Dictionary>(); + Dictionary Templates = new Dictionary(); + ushort currentTemplate; + + Actor self; + BridgeInfo info; + Bridge northNeighbour, southNeighbour; + + public Bridge(Actor self, BridgeInfo info) { - foreach (var t in TileSprites[state]) - yield return new Renderable(t.Value, Game.CellSize * t.Key, "terrain"); + this.self = self; + self.RemoveOnDeath = false; + this.info = info; } - public void FinalizeBridges(World world, Bridge[,] bridges) + public void Create(ushort template, Dictionary subtiles) { - // go looking for our neighbors, if this is a long bridge. - var info = self.Info.Traits.Get(); - if (info.NorthOffset != null) - northNeighbour = GetNeighbor(world, info.NorthOffset, bridges); - if (info.SouthOffset != null) - southNeighbour = GetNeighbor(world, info.SouthOffset, bridges); - } - - public Bridge GetNeighbor(World world, int[] offset, Bridge[,] bridges) - { - if (offset == null) return null; - var pos = self.Location + new int2(offset[0], offset[1]); - if (!world.Map.IsInMap(pos.X, pos.Y)) return null; - return bridges[pos.X, pos.Y]; - } - - - public int StateFromTemplate(TileTemplate t) - { - /* - var info = self.Info.Traits.Get(); - if (info.UseAlternateNames) + currentTemplate = template; + if (template == info.DamagedTemplate) + self.Health = (int)(info.DamagedThreshold*self.GetMaxHP()); + else if (template != info.Template) + self.Health = 0; + + // Create a new cache to store the tile data + if (cachedTileset != self.World.Map.Tileset) { - if (t.Name.EndsWith("d")) return 2; - if (t.Name.EndsWith("h")) return 1; - return 0; - } - else - return t.Name[t.Name.Length - 1] - 'a'; - */ - return 0; - } - - public string NameFromState(TileTemplate t, int state) - { - /*var info = self.Info.Traits.Get(); - if (info.UseAlternateNames) - return t.Bridge + new[] { "", "h", "d" }[state]; - else - return t.Bridge + (char)(state + 'a');*/ - return ""; - } - - public void SetTiles(World world, TileTemplate template, Dictionary replacedTiles) - { - /* - Tiles = replacedTiles; - state = StateFromTemplate(template); - - if (cachedTileset != world.Map.Tileset) - { - cachedTileset = world.Map.Tileset; + cachedTileset = self.World.Map.Tileset; sprites = new Cache, Sprite>( - x => SheetBuilder.SharedInstance.Add(world.TileSet.GetBytes(x), + x => SheetBuilder.SharedInstance.Add(self.World.TileSet.GetBytes(x), new Size(Game.CellSize, Game.CellSize))); } - - var numStates = self.Info.Traits.Get().Long ? 6 : 3; - for (var n = 0; n < numStates; n++) + + // Cache templates and tiles for the different states + foreach (var t in info.Templates) { - var stateTemplate = world.TileSet.Walkability.GetTileTemplate(NameFromState(template, n)); - Templates.Add( stateTemplate ); - - TileSprites.Add(replacedTiles.ToDictionary( + Templates.Add(t,self.World.TileSet.Templates[t]); + TileSprites.Add(t,subtiles.ToDictionary( a => a.Key, - a => sprites[new TileReference((ushort)stateTemplate.Index, (byte)a.Value)])); + a => sprites[new TileReference(t, (byte)a.Value)])); } - - self.Health = (int)(self.GetMaxHP() * template.HP); - */ } - + public string GetTerrainType(int2 cell) { - return ""; - // Ugly hack until terraintypes are converted from enums - //return Enum.GetName( typeof(TerrainType), Templates[state].TerrainType[Tiles[cell]]); + var dx = cell - self.Location; + var index = dx.X + Templates[currentTemplate].Size.X*dx.Y; + return self.World.TileSet.GetTerrainType(new TileReference(currentTemplate,(byte)index)); + } + + public void LinkNeighbouringBridges(World world, BridgeLayer bridges) + { + // go looking for our neighbors if this is a long bridge. + var info = self.Info.Traits.Get(); + if (info.NorthOffset != null) + northNeighbour = GetNeighbor(info.NorthOffset, bridges); + if (info.SouthOffset != null) + southNeighbour = GetNeighbor(info.SouthOffset, bridges); + } + + public Bridge GetNeighbor(int[] offset, BridgeLayer bridges) + { + if (offset == null) return null; + return bridges.GetBridge(self.Location + new int2(offset[0], offset[1])); + } + + public IEnumerable Render(Actor self) + { + foreach (var t in TileSprites[currentTemplate]) + yield return new Renderable(t.Value, Game.CellSize * t.Key, "terrain"); } static bool IsIntact(Bridge b) @@ -162,7 +160,7 @@ namespace OpenRA.Mods.RA void UpdateState() { - var ds = self.GetDamageState(); + /*var ds = self.GetDamageState(); if (!self.Info.Traits.Get().Long) { state = (int)ds; @@ -176,16 +174,17 @@ namespace OpenRA.Mods.RA if (waterToNorth) { state = 4; return; } if (waterToSouth) { state = 3; return; } state = (int)ds; + */ } public void Damaged(Actor self, AttackInfo e) { - if (e.DamageStateChanged) + /*if (e.DamageStateChanged) { UpdateState(); if (northNeighbour != null) northNeighbour.UpdateState(); if (southNeighbour != null) southNeighbour.UpdateState(); - } + }*/ } } } diff --git a/OpenRA.Mods.RA/BridgeLayer.cs b/OpenRA.Mods.RA/BridgeLayer.cs index 3205bd7f02..8e0ea9e4a8 100644 --- a/OpenRA.Mods.RA/BridgeLayer.cs +++ b/OpenRA.Mods.RA/BridgeLayer.cs @@ -25,89 +25,108 @@ using OpenRA.Traits; namespace OpenRA.Mods.RA { - class BridgeLayerInfo : TraitInfo { } + class BridgeLayerInfo : ITraitInfo + { + public readonly string[] Bridges = {"br1", "br2", "br3", "bridge1", "bridge2"}; + public object Create(ActorInitializer init) { return new BridgeLayer(init.self, this); } + } class BridgeLayer : ILoadWorldHook, ITerrainTypeModifier { // for tricky things like bridges. - Bridge[,] bridges; - - void MakeBridges(World w) + Bridge[,] Bridges; + + readonly BridgeLayerInfo Info; + readonly World world; + public BridgeLayer(Actor self, BridgeLayerInfo Info) { + this.Info = Info; + this.world = self.World; + } + + static Dictionary BridgeTypes; + + public void WorldLoaded(World w) + { + Bridges = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y]; + BridgeTypes = new Dictionary(); + + // Build a list of templates that should be overlayed with bridges + foreach(var bridge in Info.Bridges) + { + var bi = Rules.Info[bridge].Traits.Get(); + foreach (var template in bi.Templates) + { + BridgeTypes.Add(template, bridge); + Log.Write("debug", "Adding template {0} for bridge {1}", template, bridge); + } + } + + // Loop through the map looking for templates to overlay var tl = w.Map.TopLeft; var br = w.Map.BottomRight; for (int i = tl.X; i < br.X; i++) for (int j = tl.Y; j < br.Y; j++) - if (IsBridge(w, w.Map.MapTiles[i, j].type)) - ConvertBridgeToActor(w, i, j); - + if (BridgeTypes.Keys.Contains(w.Map.MapTiles[i, j].type)) + ConvertBridgeToActor(w, i, j); + + // Link adjacent (long)-bridges so that artwork is updated correctly foreach (var b in w.Actors.SelectMany(a => a.traits.WithInterface())) - b.FinalizeBridges(w, bridges); + b.LinkNeighbouringBridges(w,this); } void ConvertBridgeToActor(World w, int i, int j) { Log.Write("debug", "Converting bridge at {0} {1}", i, j); - /* + + // This cell already has a bridge overlaying it from a previous iteration + if (Bridges[i,j] != null) + return; + + // Correlate the tile "image" aka subtile with its position to find the template origin var tile = w.Map.MapTiles[i, j].type; var image = w.Map.MapTiles[i, j].image; - var template = w.TileSet.walk[tile]; - - // base position of the tile + var template = w.TileSet.Templates[tile]; var ni = i - image % template.Size.X; var nj = j - image / template.Size.X; - - var replacedTiles = new Dictionary(); + + // Create a new actor for this bridge and keep track of which subtiles this bridge includes + var bridge = w.CreateActor(BridgeTypes[tile], new int2(ni, nj), w.WorldActor.Owner).traits.Get(); + Dictionary subTiles = new Dictionary(); + + // Loop through the cells on the bridge template; mark each cell that is part + // of the bridge and add to the bridges array for (var x = ni; x < ni + template.Size.X; x++) for (var y = nj; y < nj + template.Size.Y; y++) { - var n = (x - ni) + template.Size.X * (y - nj); - if (!template.TerrainType.ContainsKey(n)) continue; - - if (w.Map.IsInMap(x, y)) - if (w.Map.MapTiles[x, y].type == tile - && w.Map.MapTiles[x, y].index == n) - { - // stash it - replacedTiles[new int2(x, y)] = w.Map.MapTiles[x, y].index; - // remove the tile from the actual map - w.Map.MapTiles[x, y].type = 0xfffe; - w.Map.MapTiles[x, y].index = 0; - w.Map.MapTiles[x, y].image = 0; - } + // This isn't the bridge we're looking for + if (!w.Map.IsInMap(x, y) || w.Map.MapTiles[x, y].type != tile) + continue; + + Log.Write("debug", "Adding tile {0} {1} for type {2}", x,y,tile); + + subTiles.Add(new int2(x,y),w.Map.MapTiles[x, y].image); + Bridges[x,y] = bridge; } - - if (replacedTiles.Any()) - { - var a = w.CreateActor(template.Bridge, new int2(ni, nj), w.WorldActor.Owner); - var br = a.traits.Get(); - - foreach (var t in replacedTiles.Keys) - bridges[t.X, t.Y] = br; - - br.SetTiles(w, template, replacedTiles); - } - */ + + bridge.Create(tile, subTiles); } public string GetTerrainType(int2 cell) { - /*if (bridges[ cell.X, cell.Y ] != null) - return bridges[ cell.X, cell.Y ].GetTerrainType(cell);*/ + if (Bridges[ cell.X, cell.Y ] != null) + return Bridges[ cell.X, cell.Y ].GetTerrainType(cell); return null; } - - static bool IsBridge(World w, ushort t) + + // Used to check for neighbouring bridges + public Bridge GetBridge(int2 cell) { - return false; - //return w.TileSet.walk[t].Bridge != null; - } - - public void WorldLoaded(World w) - { - bridges = new Bridge[w.Map.MapSize.X, w.Map.MapSize.Y]; - MakeBridges(w); + if (!world.Map.IsInMap(cell.X, cell.Y)) + return null; + + return Bridges[ cell.X, cell.Y ]; } } } diff --git a/mods/ra/civilian.yaml b/mods/ra/civilian.yaml index a1d3dd4de3..db276828f9 100644 --- a/mods/ra/civilian.yaml +++ b/mods/ra/civilian.yaml @@ -392,34 +392,23 @@ MISS: Bib: BR1: + Inherits: ^Bridge Bridge: Template: 235 DamagedTemplate: 236 DestroyedTemplate: 237 SouthOffset: 0,2 - Category: Building - Selectable: - BelowUnits: - Building: - Footprint: ____ ____ - Dimensions: 4,2 - HP: 1000 BR2: + Inherits: ^Bridge Bridge: Template: 238 DamagedTemplate: 239 DestroyedTemplate: 240 NorthOffset: 3,0 - Category: Building - Selectable: - BelowUnits: - Building: - Footprint: ____ ____ - Dimensions: 4,2 - HP: 1000 BR3: + Inherits: ^Bridge Bridge: Long: yes Template: 241 @@ -430,38 +419,28 @@ BR3: DestroyedPlusBothTemplate: 246 NorthOffset: 2,0 SouthOffset: 0,1 - Category: Building - Selectable: - BelowUnits: - Building: - Footprint: ____ ____ - Dimensions: 4,2 - HP: 1000 BRIDGE1: + Inherits: ^Bridge Bridge: Template: 131 DamagedTemplate: 378 DestroyedTemplate: 132 UseAlternateNames: yes - Category: Building - Selectable: - BelowUnits: Building: Footprint: _____ _____ _____ Dimensions: 5,3 - HP: 1000 - + Selectable: + Bounds: 120,48 BRIDGE2: + Inherits: ^Bridge Bridge: Template: 133 DamagedTemplate: 379 DestroyedTemplate: 134 UseAlternateNames: yes - Category: Building - Selectable: - BelowUnits: Building: Footprint: _____ _____ Dimensions: 5,2 - HP: 1000 \ No newline at end of file + Selectable: + Bounds: 120,48 \ No newline at end of file diff --git a/mods/ra/defaults.yaml b/mods/ra/defaults.yaml index ee21e3debc..10964cecad 100644 --- a/mods/ra/defaults.yaml +++ b/mods/ra/defaults.yaml @@ -137,3 +137,15 @@ HiddenUnderFog: RevealsShroud: Burns: + +^Bridge: + Category: Building + Valued: + Description: Bridge + Selectable: + Bounds: 96,48 + BelowUnits: + Building: + Footprint: ____ ____ + Dimensions: 4,2 + HP: 1000 \ No newline at end of file