Merge pull request #6855 from pchote/heightmaps-part-one

Add groundwork for heightmapped terrain.
This commit is contained in:
Matthias Mailänder
2014-11-05 09:54:38 +01:00
13 changed files with 6554 additions and 86 deletions

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Graphics
{
var mapX = x + b.Left;
var mapY = y + b.Top;
var type = tileset.GetTerrainInfo(mapTiles[mapX, mapY]);
var type = tileset[tileset.GetTerrainIndex(mapTiles[mapX, mapY])];
colors[y * stride + x] = type.Color.ToArgb();
}
}

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Graphics
foreach (var cell in map.Cells)
{
var tile = wr.Theater.TileSprite(map.MapTiles.Value[cell]);
var pos = wr.ScreenPosition(map.CenterOfCell(cell)) - 0.5f * tile.size;
var pos = wr.ScreenPosition(map.CenterOfCell(cell)) + tile.offset - 0.5f * tile.size;
Util.FastCreateQuad(vertices, pos, tile, terrainPalette, nv, tile.size);
nv += 4;
}

View File

@@ -21,9 +21,11 @@ namespace OpenRA.Graphics
SheetBuilder sheetBuilder;
Dictionary<ushort, Sprite[]> templates;
Sprite missingTile;
TileSet tileset;
public Theater(TileSet tileset)
{
this.tileset = tileset;
var allocated = false;
Func<Sheet> allocate = () =>
{
@@ -42,7 +44,13 @@ namespace OpenRA.Graphics
{
var allFrames = frameCache[t.Value.Image];
var frames = t.Value.Frames != null ? t.Value.Frames.Select(f => allFrames[f]).ToArray() : allFrames;
templates.Add(t.Value.Id, frames.Select(f => sheetBuilder.Add(f)).ToArray());
var sprites = frames.Select(f => sheetBuilder.Add(f));
// Ignore the offsets baked into R8 sprites
if (tileset.IgnoreTileSpriteOffsets)
sprites = sprites.Select(s => new Sprite(s.sheet, s.bounds, float2.Zero, s.channel, s.blendMode));
templates.Add(t.Value.Id, sprites.ToArray());
}
// 1x1px transparent tile
@@ -63,6 +71,35 @@ namespace OpenRA.Graphics
return template[r.Index];
}
public Rectangle TemplateBounds(TerrainTemplateInfo template, Size tileSize, TileShape tileShape)
{
Rectangle? templateRect = null;
var i = 0;
for (var y = 0; y < template.Size.Y; y++)
{
for (var x = 0; x < template.Size.X; x++)
{
var tile = new TerrainTile(template.Id, (byte)(i++));
var tileInfo = tileset.GetTileInfo(tile);
// Empty tile
if (tileInfo == null)
continue;
var sprite = TileSprite(tile);
var u = tileShape == TileShape.Rectangle ? x : (x - y) / 2f;
var v = tileShape == TileShape.Rectangle ? y : (x + y) / 2f;
var tl = new float2(u * tileSize.Width, (v - 0.5f * tileInfo.Height) * tileSize.Height) - 0.5f * sprite.size;
var rect = new Rectangle((int)(tl.X + sprite.offset.X), (int)(tl.Y + sprite.offset.Y), (int)sprite.size.X, (int)sprite.size.Y);
templateRect = templateRect.HasValue ? Rectangle.Union(templateRect.Value, rect) : rect;
}
}
return templateRect.HasValue ? templateRect.Value : Rectangle.Empty;
}
public Sheet Sheet { get { return sheetBuilder.Current; } }
}
}

View File

@@ -15,6 +15,18 @@ using System.Linq;
namespace OpenRA
{
public class TerrainTileInfo
{
[FieldLoader.Ignore]
public readonly byte TerrainType = byte.MaxValue;
public readonly byte Height;
public readonly byte RampType;
public readonly Color LeftColor;
public readonly Color RightColor;
public MiniYaml Save() { return FieldSaver.Save(this); }
}
public class TerrainTypeInfo
{
public readonly string Type;
@@ -30,8 +42,10 @@ namespace OpenRA
public MiniYaml Save() { return FieldSaver.Save(this); }
}
public class TileTemplate
public class TerrainTemplateInfo
{
static readonly string[] Fields = { "Id", "Image", "Frames", "Size", "PickAny", "Category" };
public readonly ushort Id;
public readonly string Image;
public readonly int[] Frames;
@@ -39,78 +53,78 @@ namespace OpenRA
public readonly bool PickAny;
public readonly string Category;
byte[] tiles;
TerrainTileInfo[] tileInfo;
public TileTemplate(ushort id, string image, int2 size, byte[] tiles)
public TerrainTemplateInfo(ushort id, string image, int2 size, byte[] tiles)
{
this.Id = id;
this.Image = image;
this.Size = size;
this.tiles = tiles;
}
public TileTemplate(TileSet tileSet, MiniYaml my)
public TerrainTemplateInfo(TileSet tileSet, MiniYaml my)
{
FieldLoader.Load(this, my);
tiles = LoadTiles(tileSet, my);
}
byte[] LoadTiles(TileSet tileSet, MiniYaml y)
{
var nodes = y.ToDictionary()["Tiles"].Nodes;
var nodes = my.ToDictionary()["Tiles"].Nodes;
if (!PickAny)
{
var tiles = new byte[Size.X * Size.Y];
for (var i = 0; i < tiles.Length; i++)
tiles[i] = byte.MaxValue;
tileInfo = new TerrainTileInfo[Size.X * Size.Y];
foreach (var node in nodes)
{
int key;
if (!int.TryParse(node.Key, out key) || key < 0 || key >= tiles.Length)
if (!int.TryParse(node.Key, out key) || key < 0 || key >= tileInfo.Length)
throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
tiles[key] = tileSet.GetTerrainIndex(node.Value.Value);
tileInfo[key] = LoadTileInfo(tileSet, node.Value);
}
return tiles;
}
else
{
var tiles = new byte[nodes.Count];
var i = 0;
tileInfo = new TerrainTileInfo[nodes.Count];
var i = 0;
foreach (var node in nodes)
{
int key;
if (!int.TryParse(node.Key, out key) || key != i++)
throw new InvalidDataException("Invalid tile key '{0}' on template '{1}' of tileset '{2}'.".F(node.Key, Id, tileSet.Id));
tiles[key] = tileSet.GetTerrainIndex(node.Value.Value);
tileInfo[key] = LoadTileInfo(tileSet, node.Value);
}
return tiles;
}
}
static readonly string[] Fields = { "Id", "Image", "Frames", "Size", "PickAny" };
public byte this[int index]
static TerrainTileInfo LoadTileInfo(TileSet tileSet, MiniYaml my)
{
get { return tiles[index]; }
var tile = new TerrainTileInfo();
FieldLoader.Load(tile, my);
// Terrain type must be converted from a string to an index
tile.GetType().GetField("TerrainType").SetValue(tile, tileSet.GetTerrainIndex(my.Value));
// Fall back to the terrain-type color if necessary
var overrideColor = tileSet.TerrainInfo[tile.TerrainType].Color;
if (tile.LeftColor == default(Color))
tile.GetType().GetField("LeftColor").SetValue(tile, overrideColor);
if (tile.RightColor == default(Color))
tile.GetType().GetField("RightColor").SetValue(tile, overrideColor);
return tile;
}
public TerrainTileInfo this[int index] { get { return tileInfo[index]; } }
public bool Contains(int index)
{
return index >= 0 && index < tiles.Length;
return index >= 0 && index < tileInfo.Length;
}
public int TilesCount
{
get { return tiles.Length; }
get { return tileInfo.Length; }
}
public MiniYaml Save(TileSet tileSet)
@@ -126,7 +140,7 @@ namespace OpenRA
}
root.Add(new MiniYamlNode("Tiles", null,
tiles.Select((terrainTypeIndex, templateIndex) => new MiniYamlNode(templateIndex.ToString(), tileSet[terrainTypeIndex].Type)).ToList()));
tileInfo.Select((terrainTypeIndex, templateIndex) => new MiniYamlNode(templateIndex.ToString(), terrainTypeIndex.Save())).ToList()));
return new MiniYaml(null, root);
}
@@ -134,6 +148,8 @@ namespace OpenRA
public class TileSet
{
static readonly string[] Fields = { "Name", "Id", "SheetSize", "Palette", "PlayerPalette", "Extensions", "WaterPaletteRotationBase", "EditorTemplateOrder", "IgnoreTileSpriteOffsets" };
public readonly string Name;
public readonly string Id;
public readonly int SheetSize = 512;
@@ -141,15 +157,14 @@ namespace OpenRA
public readonly string PlayerPalette;
public readonly string[] Extensions;
public readonly int WaterPaletteRotationBase = 0x60;
public readonly Dictionary<ushort, TileTemplate> Templates = new Dictionary<ushort, TileTemplate>();
public readonly Dictionary<ushort, TerrainTemplateInfo> Templates = new Dictionary<ushort, TerrainTemplateInfo>();
public readonly string[] EditorTemplateOrder;
public readonly bool IgnoreTileSpriteOffsets;
public readonly TerrainTypeInfo[] TerrainInfo;
readonly Dictionary<string, byte> terrainIndexByType = new Dictionary<string, byte>();
readonly byte defaultWalkableTerrainIndex;
static readonly string[] Fields = { "Name", "Id", "SheetSize", "Palette", "Extensions" };
public TileSet(ModData modData, string filepath)
{
var yaml = MiniYaml.DictFromFile(filepath);
@@ -162,8 +177,10 @@ namespace OpenRA
.Select(y => new TerrainTypeInfo(y))
.OrderBy(tt => tt.Type)
.ToArray();
if (TerrainInfo.Length >= byte.MaxValue)
throw new InvalidDataException("Too many terrain types.");
for (byte i = 0; i < TerrainInfo.Length; i++)
{
var tt = TerrainInfo[i].Type;
@@ -178,27 +195,29 @@ namespace OpenRA
// Templates
Templates = yaml["Templates"].ToDictionary().Values
.Select(y => new TileTemplate(this, y)).ToDictionary(t => t.Id);
.Select(y => new TerrainTemplateInfo(this, y)).ToDictionary(t => t.Id);
}
public TileSet(string name, string id, string palette, string[] extensions, TerrainTypeInfo[] terrainInfo)
{
this.Name = name;
this.Id = id;
this.Palette = palette;
this.Extensions = extensions;
this.TerrainInfo = terrainInfo;
Name = name;
Id = id;
Palette = palette;
Extensions = extensions;
TerrainInfo = terrainInfo;
if (TerrainInfo.Length >= byte.MaxValue)
throw new InvalidDataException("Too many terrain types.");
for (byte i = 0; i < terrainInfo.Length; i++)
{
var tt = terrainInfo[i].Type;
if (terrainIndexByType.ContainsKey(tt))
throw new InvalidDataException("Duplicate terrain type '{0}'.".F(tt));
terrainIndexByType.Add(tt, i);
}
defaultWalkableTerrainIndex = GetTerrainIndex("Clear");
}
@@ -207,11 +226,6 @@ namespace OpenRA
get { return TerrainInfo[index]; }
}
public int TerrainsCount
{
get { return TerrainInfo.Length; }
}
public bool TryGetTerrainIndex(string type, out byte index)
{
return terrainIndexByType.TryGetValue(type, out index);
@@ -232,14 +246,20 @@ namespace OpenRA
if (tpl.Contains(r.Index))
{
var ti = tpl[r.Index];
if (ti != byte.MaxValue)
return ti;
var tile = tpl[r.Index];
if (tile != null && tile.TerrainType != byte.MaxValue)
return tile.TerrainType;
}
return defaultWalkableTerrainIndex;
}
public TerrainTileInfo GetTileInfo(TerrainTile r)
{
var tpl = Templates[r.Type];
return tpl.Contains(r.Index) ? tpl[r.Index] : null;
}
public void Save(string filepath)
{
var root = new List<MiniYamlNode>();
@@ -263,10 +283,5 @@ namespace OpenRA
Templates.Select(t => new MiniYamlNode("Template@{0}".F(t.Value.Id), t.Value.Save(this))).ToList()));
root.WriteToFile(filepath);
}
public TerrainTypeInfo GetTerrainInfo(TerrainTile r)
{
return this[GetTerrainIndex(r)];
}
}
}

View File

@@ -134,6 +134,7 @@
<Compile Include="SpriteLoaders\TmpTDLoader.cs" />
<Compile Include="SpriteLoaders\ShpD2Loader.cs" />
<Compile Include="Widgets\Logic\SettingsLogic.cs" />
<Compile Include="Widgets\TerrainTemplatePreviewWidget.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,103 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets
{
public class TerrainTemplatePreviewWidget : Widget
{
public Func<float> GetScale = () => 1f;
public string Palette = "terrain";
readonly WorldRenderer worldRenderer;
readonly TileSet tileset;
TerrainTemplateInfo template;
Rectangle bounds;
public TerrainTemplateInfo Template
{
get
{
return template;
}
set
{
template = value;
if (template == null)
return;
var ts = Game.modData.Manifest.TileSize;
var shape = Game.modData.Manifest.TileShape;
bounds = worldRenderer.Theater.TemplateBounds(template, ts, shape);
}
}
[ObjectCreator.UseCtor]
public TerrainTemplatePreviewWidget(WorldRenderer worldRenderer, World world)
{
this.worldRenderer = worldRenderer;
tileset = world.Map.Rules.TileSets[world.Map.Tileset];
}
protected TerrainTemplatePreviewWidget(TerrainTemplatePreviewWidget other)
: base(other)
{
worldRenderer = other.worldRenderer;
tileset = other.worldRenderer.world.Map.Rules.TileSets[other.worldRenderer.world.Map.Tileset];
Template = other.Template;
GetScale = other.GetScale;
}
public override Widget Clone() { return new TerrainTemplatePreviewWidget(this); }
public override void Draw()
{
if (template == null)
return;
var ts = Game.modData.Manifest.TileSize;
var shape = Game.modData.Manifest.TileShape;
var scale = GetScale();
var sb = new Rectangle((int)(scale * bounds.X), (int)(scale * bounds.Y), (int)(scale * bounds.Width), (int)(scale * bounds.Height));
var origin = RenderOrigin + new int2((RenderBounds.Size.Width - sb.Width) / 2 - sb.X, (RenderBounds.Size.Height - sb.Height) / 2 - sb.Y);
var i = 0;
for (var y = 0; y < Template.Size.Y; y++)
{
for (var x = 0; x < Template.Size.X; x++)
{
var tile = new TerrainTile(Template.Id, (byte)(i++));
var tileInfo = tileset.GetTileInfo(tile);
// Empty tile
if (tileInfo == null)
continue;
var sprite = worldRenderer.Theater.TileSprite(tile);
var size = new float2(sprite.size.X * scale, sprite.size.Y * scale);
var u = shape == TileShape.Rectangle ? x : (x - y) / 2f;
var v = shape == TileShape.Rectangle ? y : (x + y) / 2f;
var pos = origin + scale * (new float2(u * ts.Width, (v - 0.5f * tileInfo.Height) * ts.Height) - 0.5f * sprite.size);
Game.Renderer.SpriteRenderer.DrawSprite(sprite, pos, worldRenderer.Palette(Palette), size);
}
}
}
}
}

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.RA.Buildings
readonly LaysTerrainInfo info;
readonly BuildableTerrainLayer layer;
readonly BuildingInfluence bi;
readonly TileTemplate template;
readonly TerrainTemplateInfo template;
public LaysTerrain(Actor self, LaysTerrainInfo info)
{

View File

@@ -76,7 +76,7 @@ namespace OpenRA.Mods.RA.Move
TerrainInfo[] LoadTilesetSpeeds(TileSet tileSet)
{
var info = new TerrainInfo[tileSet.TerrainsCount];
var info = new TerrainInfo[tileSet.TerrainInfo.Length];
for (var i = 0; i < info.Length; i++)
info[i] = TerrainInfo.Impassable;

View File

@@ -24,34 +24,69 @@ namespace OpenRA.Mods.TS.SpriteLoaders
{
public Size Size { get; private set; }
public Size FrameSize { get { return Size; } }
public float2 Offset { get { return float2.Zero; } }
public float2 Offset { get; private set; }
public byte[] Data { get; set; }
public bool DisableExportPadding { get { return false; } }
public TmpTSFrame(Stream s, Size size)
public TmpTSFrame(Stream s, Size size, int u, int v)
{
if (s.Position != 0)
{
Size = size;
// Ignore tile header for now
s.Position += 52;
// Skip unnecessary header data
s.Position += 20;
Data = new byte[size.Width * size.Height];
// Extra data is specified relative to the top-left of the template
var extraX = s.ReadInt32() - (u - v) * size.Width / 2;
var extraY = s.ReadInt32() - (u + v) * size.Height / 2;
var extraWidth = s.ReadInt32();
var extraHeight = s.ReadInt32();
var flags = s.ReadUInt32();
var bounds = new Rectangle(0, 0, size.Width, size.Height);
if ((flags & 0x01) != 0)
{
var extraBounds = new Rectangle(extraX, extraY, extraWidth, extraHeight);
bounds = Rectangle.Union(bounds, extraBounds);
Offset = new float2(bounds.X + 0.5f * (bounds.Width - size.Width), bounds.Y + 0.5f * (bounds.Height - size.Height));
Size = new Size(bounds.Width, bounds.Height);
}
// Skip unnecessary header data
s.Position += 12;
Data = new byte[bounds.Width * bounds.Height];
// Unpack tile data
var width = 4;
for (var i = 0; i < size.Height; i++)
for (var j = 0; j < size.Height; j++)
{
var start = i * size.Width + (size.Width - width) / 2;
for (var j = 0; j < width; j++)
Data[start + j] = s.ReadUInt8();
var start = (j - bounds.Y) * bounds.Width + (size.Width - width) / 2 - bounds.X;
for (var i = 0; i < width; i++)
Data[start + i] = s.ReadUInt8();
width += (i < size.Height / 2 - 1 ? 1 : -1) * 4;
width += (j < size.Height / 2 - 1 ? 1 : -1) * 4;
}
// Ignore Z-data for now
// Ignore extra data for now
// TODO: Load Z-data once the renderer can handle it
s.Position += size.Width * size.Height / 2;
if ((flags & 0x01) == 0)
return;
// Load extra data (cliff faces, etc)
for (var j = 0; j < extraHeight; j++)
{
var start = (j + extraY - bounds.Y) * bounds.Width + extraX - bounds.X;
for (var i = 0; i < extraWidth; i++)
{
var extra = s.ReadUInt8();
if (extra != 0)
Data[start + i] = extra;
}
}
}
else
Data = new byte[0];
@@ -96,10 +131,15 @@ namespace OpenRA.Mods.TS.SpriteLoaders
offsets[i] = s.ReadUInt32();
var tiles = new TmpTSFrame[offsets.Length];
for (var i = 0; i < offsets.Length; i++)
for (var j = 0; j < templateHeight; j++)
{
s.Position = offsets[i];
tiles[i] = new TmpTSFrame(s, size);
for (var i = 0; i < templateWidth; i++)
{
var k = j * templateWidth + i;
s.Position = offsets[k];
tiles[k] = new TmpTSFrame(s, size, i, j);
}
}
s.Position = start;

View File

@@ -91,19 +91,22 @@ namespace OpenRA.Mods.TS.UtilityCommands
continue;
s.Position = offsets[j] + 40;
/* var height = */s.ReadUInt8();
var height = s.ReadUInt8();
var terrainType = s.ReadUInt8();
/* var rampType = */s.ReadUInt8();
/* var height = */s.ReadUInt8();
var rampType = s.ReadUInt8();
if (!terrainTypes.ContainsKey(terrainType))
throw new InvalidDataException("Unknown terrain type {0} in {1}".F(terrainType, templateFilename));
Console.WriteLine("\t\t\t{0}: {1}", j, terrainTypes[terrainType]);
// Console.WriteLine("\t\t\t\tHeight: {0}", height);
// Console.WriteLine("\t\t\t\tTerrainType: {0}", terrainType);
// Console.WriteLine("\t\t\t\tRampType: {0}", rampType);
// Console.WriteLine("\t\t\t\tLeftColor: {0},{1},{2}", s.ReadUInt8(), s.ReadUInt8(), s.ReadUInt8());
// Console.WriteLine("\t\t\t\tRightColor: {0},{1},{2}", s.ReadUInt8(), s.ReadUInt8(), s.ReadUInt8());
if (height != 0)
Console.WriteLine("\t\t\t\tHeight: {0}", height);
if (rampType != 0)
Console.WriteLine("\t\t\t\tRampType: {0}", rampType);
Console.WriteLine("\t\t\t\tMinimapLeftColor: {0},{1},{2}", s.ReadUInt8(), s.ReadUInt8(), s.ReadUInt8());
Console.WriteLine("\t\t\t\tMinimapRightColor: {0},{1},{2}", s.ReadUInt8(), s.ReadUInt8(), s.ReadUInt8());
}
}
}

View File

@@ -393,7 +393,7 @@ namespace OpenRA.TilesetBuilder
tiles[idx] = tileset.GetTerrainIndex(ttype);
}
var template = new TileTemplate(
var template = new TerrainTemplateInfo(
id: cur,
image: "{0}{1:00}".F(txtTilesetName.Text, cur),
size: new int2(tp.Width, tp.Height),

View File

@@ -5,6 +5,7 @@ General:
Palette: d2k.pal
Extensions: .R8, .r8, .shp, .tmp
EditorTemplateOrder: Basic, Dune, Sand-Detail, Brick, Sand-Cliff, Sand-Smooth, Cliff-Type-Changer, Rock-Sand-Smooth, Rock-Detail, Rock-Cliff, Rock-Cliff-Rock, Rotten-Base, Dead-Worm, Ice, Ice-Detail, Rock-Cliff-Sand, Sand-Platform, Unidentified
IgnoreTileSpriteOffsets: true
Terrain:
TerrainType@Clear: # TODO: workaround for the stupid WinForms editor

File diff suppressed because it is too large Load Diff