From 2c50d3baa7dab2505af40bd9698fccafca922742 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 2 Mar 2010 21:55:32 +1300 Subject: [PATCH] added ResourceType; mostly works, except for growth & consumption --- OpenRA.Game/Graphics/OverlayRenderer.cs | 46 ------------------ OpenRA.Game/OpenRA.Game.csproj | 1 + OpenRA.Game/Ore.cs | 10 ---- OpenRA.Game/Traits/World/OreGrowth.cs | 7 +-- OpenRA.Game/Traits/World/ResourceLayer.cs | 59 +++++++++++++++-------- OpenRA.Game/Traits/World/ResourceType.cs | 39 +++++++++++++++ mods/cnc/system.yaml | 5 +- mods/ra/rules.yaml | 13 +++++ 8 files changed, 98 insertions(+), 82 deletions(-) create mode 100644 OpenRA.Game/Traits/World/ResourceType.cs diff --git a/OpenRA.Game/Graphics/OverlayRenderer.cs b/OpenRA.Game/Graphics/OverlayRenderer.cs index 55adb33de9..285eb8f206 100755 --- a/OpenRA.Game/Graphics/OverlayRenderer.cs +++ b/OpenRA.Game/Graphics/OverlayRenderer.cs @@ -25,22 +25,12 @@ namespace OpenRA.Graphics { class OverlayRenderer { - static string[] overlaySpriteNames = - { - "sbag", "cycl", "brik", "fenc", "wood", - "gold01", "gold02", "gold03", "gold04", - "gem01", "gem02", "gem03", "gem04", - "v12", "v13", "v14", "v15", "v16", "v17", "v18", - "fpls", "wcrate", "scrate", "barb", "sbag", - }; - static string[] smudgeSpriteNames = { "bib3", "bib2", "bib1", "sc1", "sc2", "sc3", "sc4", "sc5", "sc6", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", }; - readonly Sprite[][] overlaySprites; readonly Sprite[] smudgeSprites; SpriteRenderer spriteRenderer; @@ -51,7 +41,6 @@ namespace OpenRA.Graphics this.spriteRenderer = new SpriteRenderer( renderer, true ); this.map = map; - overlaySprites = overlaySpriteNames.Select(f => SpriteSheetBuilder.LoadAllSprites(f)).ToArray(); smudgeSprites = smudgeSpriteNames.SelectMany(f => SpriteSheetBuilder.LoadAllSprites(f)).ToArray(); } @@ -71,44 +60,9 @@ namespace OpenRA.Graphics spriteRenderer.DrawSprite(smudgeSprites[tr.smudge - 1], Game.CellSize * (float2)location, "terrain"); } - - //var o = tr.overlay; - //if (o < overlaySprites.Length) - //{ - // var location = new int2(x, y); - // var sprites = overlaySprites[o]; - // var spriteIndex = 0; - // if (Ore.overlayIsFence[o]) spriteIndex = NearbyFences(x, y); - // else if (Ore.overlayIsOre[o]) spriteIndex = map.MapTiles[x,y].density - 1; - // else if (Ore.overlayIsGems[o]) spriteIndex = map.MapTiles[x,y].density - 1; - // spriteRenderer.DrawSprite(sprites[spriteIndex], - // Game.CellSize * (float2)location, "terrain"); - //} } spriteRenderer.Flush(); } - - bool IsFence( int x, int y ) - { - var o = map.MapTiles[ x, y ].overlay; - if (o < Ore.overlayIsFence.Length) - return Ore.overlayIsFence[o]; - return false; - } - - int NearbyFences( int x, int y ) - { - int ret = 0; - if( IsFence( x, y - 1 ) ) - ret |= 1; - if( IsFence( x + 1, y ) ) - ret |= 2; - if( IsFence( x, y + 1 ) ) - ret |= 4; - if( IsFence( x - 1, y ) ) - ret |= 8; - return ret; - } } } diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index a72530894c..2d7b51d370 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -217,6 +217,7 @@ + diff --git a/OpenRA.Game/Ore.cs b/OpenRA.Game/Ore.cs index 0fe76526c3..1b26d435d8 100644 --- a/OpenRA.Game/Ore.cs +++ b/OpenRA.Game/Ore.cs @@ -101,16 +101,6 @@ namespace OpenRA ++map.MapTiles[i, j].density; } - public static void InitOreDensity( this Map map ) - { - for (int j = 0; j < 128; j++) - for (int i = 0; i < 128; i++) - { - if (map.ContainsOre(i, j)) map.MapTiles[i, j].density = map.GetOreDensity(i, j); - if (map.ContainsGem(i, j)) map.MapTiles[i, j].density = map.GetGemDensity(i, j); - } - } - static byte GetOreDensity(this Map map, int i, int j) { int sum = 0; diff --git a/OpenRA.Game/Traits/World/OreGrowth.cs b/OpenRA.Game/Traits/World/OreGrowth.cs index 3e96ca88e2..71109f7e21 100644 --- a/OpenRA.Game/Traits/World/OreGrowth.cs +++ b/OpenRA.Game/Traits/World/OreGrowth.cs @@ -30,7 +30,7 @@ namespace OpenRA.Traits public object Create(Actor self) { return new OreGrowth(); } } - class OreGrowth : ITick, ILoadWorldHook + class OreGrowth : ITick { int remainingTicks; @@ -52,10 +52,5 @@ namespace OpenRA.Traits remainingTicks = (int)(info.Interval * 60 * 25); } } - - public void WorldLoaded(World w) - { - Ore.InitOreDensity(w.Map); - } } } diff --git a/OpenRA.Game/Traits/World/ResourceLayer.cs b/OpenRA.Game/Traits/World/ResourceLayer.cs index ac7ed0b317..eefbfa8a5a 100644 --- a/OpenRA.Game/Traits/World/ResourceLayer.cs +++ b/OpenRA.Game/Traits/World/ResourceLayer.cs @@ -18,7 +18,6 @@ */ #endregion -using System; using System.Linq; using OpenRA.Graphics; @@ -26,23 +25,18 @@ namespace OpenRA.Traits { class ResourceLayerInfo : ITraitInfo { - public readonly string[] SpriteNames = { }; - public readonly int[] OverlayIndices = { }; - public readonly string Palette = "terrain"; - public object Create(Actor self) { return new ResourceLayer(self, this); } + public object Create(Actor self) { return new ResourceLayer(self); } } class ResourceLayer : IRenderOverlay, ILoadWorldHook { - ResourceLayerInfo info; - Sprite[][] sprites; - CellContents[,] content = new CellContents[128,128]; SpriteRenderer sr; - public ResourceLayer(Actor self, ResourceLayerInfo info) + public ResourceTypeInfo[] resourceTypes; + public CellContents[,] content = new CellContents[128, 128]; + + public ResourceLayer(Actor self) { - this.info = info; - sprites = info.SpriteNames.Select( f => SpriteSheetBuilder.LoadAllSprites(f)).ToArray(); sr = new SpriteRenderer( Game.renderer, true ); } @@ -55,10 +49,12 @@ namespace OpenRA.Traits for (int x = map.XOffset; x < map.XOffset + map.Width; x++) { if (!shroud.IsExplored(new int2(x, y))) continue; - if (content[x, y].contents != null) - sr.DrawSprite(content[x, y].contents[content[x, y].density], + + var c = content[x, y]; + if (c.image != null) + sr.DrawSprite(c.image[c.density], Game.CellSize * new int2(x, y), - info.Palette); + c.type.Palette); } sr.Flush(); @@ -66,22 +62,47 @@ namespace OpenRA.Traits public void WorldLoaded(World w) { + resourceTypes = w.WorldActor.Info.Traits.WithInterface().ToArray(); + foreach (var rt in resourceTypes) + rt.Sprites = rt.SpriteNames.Select(a => SpriteSheetBuilder.LoadAllSprites(a)).ToArray(); + var map = w.Map; for (int y = map.YOffset; y < map.YOffset + map.Height; y++) for (int x = map.XOffset; x < map.XOffset + map.Width; x++) - if (info.OverlayIndices.Contains(w.Map.MapTiles[x, y].overlay)) - content[x, y].contents = ChooseContent(w, w.Map.MapTiles[x, y].overlay); + { + content[x,y].type = resourceTypes.FirstOrDefault( + r => r.Overlays.Contains(w.Map.MapTiles[x, y].overlay)); + if (content[x, y].type != null) + content[x, y].image = ChooseContent(w, content[x, y].type); + } + + for (int y = map.YOffset; y < map.YOffset + map.Height; y++) + for (int x = map.XOffset; x < map.XOffset + map.Width; x++) + if (content[x, y].type != null) + content[x, y].density = (GetAdjacentCellsWith(content[x, y].type, x, y) * + content[x, y].image.Length) / 9; } - Sprite[] ChooseContent(World w, int overlay) + public Sprite[] ChooseContent(World w, ResourceTypeInfo info) { - return sprites[w.SharedRandom.Next(sprites.Length)]; + return info.Sprites[w.SharedRandom.Next(info.Sprites.Length)]; + } + + public int GetAdjacentCellsWith(ResourceTypeInfo info, int i, int j) + { + int sum = 0; + for (var u = -1; u < 2; u++) + for (var v = -1; v < 2; v++) + if (content[i+u, j+v].type == info) + ++sum; + return sum; } public struct CellContents { - public Sprite[] contents; + public ResourceTypeInfo type; + public Sprite[] image; public int density; } } diff --git a/OpenRA.Game/Traits/World/ResourceType.cs b/OpenRA.Game/Traits/World/ResourceType.cs new file mode 100644 index 0000000000..dac76cbc83 --- /dev/null +++ b/OpenRA.Game/Traits/World/ResourceType.cs @@ -0,0 +1,39 @@ +#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. + * + * OpenRA is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * OpenRA is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with OpenRA. If not, see . + */ +#endregion + +using OpenRA.Graphics; +using System.Collections.Generic; +namespace OpenRA.Traits +{ + class ResourceTypeInfo : ITraitInfo + { + public readonly int[] Overlays = { }; + public readonly string[] SpriteNames = { }; + public readonly string Palette = "terrain"; + public readonly int ValuePerUnit = 0; + public readonly string Name = null; + + public Sprite[][] Sprites; + + public object Create(Actor self) { return new ResourceType(); } + } + + class ResourceType { } +} diff --git a/mods/cnc/system.yaml b/mods/cnc/system.yaml index a20142ddf6..0dd0e95844 100644 --- a/mods/cnc/system.yaml +++ b/mods/cnc/system.yaml @@ -167,6 +167,9 @@ World: SellButton: RepairButton: ResourceLayer: - OverlayIndices: 5,6,7,8,9,10,11,12 + ResourceType@green-tib: + Overlays: 5,6,7,8,9,10,11,12 Palette: player SpriteNames: ti1,ti2,ti3,ti4,ti5,ti6,ti7,ti8,ti9,ti10,ti11,ti12 + ValuePerUnit: 30 + Name: Tiberium diff --git a/mods/ra/rules.yaml b/mods/ra/rules.yaml index 03132a1fb3..a1c1bdee32 100644 --- a/mods/ra/rules.yaml +++ b/mods/ra/rules.yaml @@ -234,6 +234,19 @@ World: WallLoadHook@barb: ActorType: barb OverlayIndices: 23 + ResourceLayer: + ResourceType@ore: + Overlays: 5,6,7,8 + Palette: terrain + SpriteNames: gold01,gold02,gold03,gold04 + ValuePerUnit: 30 + Name: Ore + ResourceType@gem: + Overlays: 9,10,11,12 + Palette: terrain + SpriteNames: gem01,gem02,gem03,gem04 + ValuePerUnit: 60 + Name: Gems MGG: GeneratesGap: