Change terrain type from string based dictionaries to arrays
This commit is contained in:
@@ -32,6 +32,8 @@ namespace OpenRA.Graphics
|
||||
var bitmapData = terrain.LockBits(terrain.Bounds(),
|
||||
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
|
||||
var mapTiles = map.MapTiles.Value;
|
||||
|
||||
unsafe
|
||||
{
|
||||
int* c = (int*)bitmapData.Scan0;
|
||||
@@ -41,11 +43,9 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
var mapX = x + map.Bounds.Left;
|
||||
var mapY = y + map.Bounds.Top;
|
||||
var type = tileset.GetTerrainType(map.MapTiles.Value[mapX, mapY]);
|
||||
if (!tileset.Terrain.ContainsKey(type))
|
||||
throw new InvalidDataException("Tileset {0} lacks terraintype {1}".F(tileset.Id, type));
|
||||
var type = tileset.GetTerrainInfo(mapTiles[mapX, mapY]);
|
||||
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[type].Color.ToArgb();
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = type.Color.ToArgb();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ namespace OpenRA.Graphics
|
||||
if (res == null)
|
||||
continue;
|
||||
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = tileset.Terrain[res].Color.ToArgb();
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = tileset[tileset.GetTerrainIndex(res)].Color.ToArgb();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,9 +107,9 @@ namespace OpenRA.Graphics
|
||||
var mapX = x + map.Bounds.Left;
|
||||
var mapY = y + map.Bounds.Top;
|
||||
var custom = map.CustomTerrain[mapX, mapY];
|
||||
if (custom == null)
|
||||
if (custom == -1)
|
||||
continue;
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet.Terrain[custom].Color.ToArgb();
|
||||
*(c + (y * bitmapData.Stride >> 2) + x) = world.TileSet[custom].Color.ToArgb();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ namespace OpenRA
|
||||
|
||||
[FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles;
|
||||
[FieldLoader.Ignore] public Lazy<TileReference<byte, byte>[,]> MapResources;
|
||||
[FieldLoader.Ignore] public string[,] CustomTerrain;
|
||||
[FieldLoader.Ignore] public int[,] CustomTerrain;
|
||||
|
||||
[FieldLoader.Ignore] Lazy<Ruleset> rules;
|
||||
public Ruleset Rules { get { return rules != null ? rules.Value : null; } }
|
||||
@@ -228,7 +228,10 @@ namespace OpenRA
|
||||
NotificationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Notifications");
|
||||
TranslationDefinitions = MiniYaml.NodesOrEmpty(yaml, "Translations");
|
||||
|
||||
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||
CustomTerrain = new int[MapSize.X, MapSize.Y];
|
||||
for (var x = 0; x < MapSize.X; x++)
|
||||
for (var y = 0; y < MapSize.Y; y++)
|
||||
CustomTerrain[x, y] = -1;
|
||||
|
||||
MapTiles = Exts.Lazy(() => LoadMapTiles());
|
||||
MapResources = Exts.Lazy(() => LoadResourceTiles());
|
||||
@@ -529,7 +532,7 @@ namespace OpenRA
|
||||
var template = tileset.Templates[tr.Type];
|
||||
if (!template.PickAny)
|
||||
continue;
|
||||
tr.Index = (byte)r.Next(0, template.Tiles.Count);
|
||||
tr.Index = (byte)r.Next(0, template.TilesCount);
|
||||
MapTiles.Value[i, j] = tr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using OpenRA.Graphics;
|
||||
@@ -41,29 +42,81 @@ namespace OpenRA
|
||||
public readonly bool PickAny;
|
||||
public readonly string Category;
|
||||
|
||||
[FieldLoader.LoadUsing("LoadTiles")]
|
||||
public readonly Dictionary<byte, string> Tiles = new Dictionary<byte, string>();
|
||||
int[] tiles;
|
||||
|
||||
public TileTemplate() { }
|
||||
public TileTemplate(MiniYaml my) { FieldLoader.Load(this, my); }
|
||||
|
||||
public TileTemplate(ushort id, string image, int2 size)
|
||||
public TileTemplate(ushort id, string image, int2 size, int[] tiles)
|
||||
{
|
||||
this.Id = id;
|
||||
this.Image = image;
|
||||
this.Size = size;
|
||||
this.tiles = tiles;
|
||||
}
|
||||
|
||||
static object LoadTiles(MiniYaml y)
|
||||
public TileTemplate(TileSet tileSet, MiniYaml my)
|
||||
{
|
||||
return y.ToDictionary()["Tiles"].ToDictionary(
|
||||
name => byte.Parse(name),
|
||||
my => my.Value);
|
||||
FieldLoader.Load(this, my);
|
||||
|
||||
tiles = LoadTiles(tileSet, my);
|
||||
}
|
||||
|
||||
int[] LoadTiles(TileSet tileSet, MiniYaml y)
|
||||
{
|
||||
var nodes = y.ToDictionary()["Tiles"].Nodes;
|
||||
|
||||
if (!PickAny)
|
||||
{
|
||||
var tiles = new int[Size.X * Size.Y];
|
||||
|
||||
for (var i = 0; i < tiles.Length; i++)
|
||||
tiles[i] = -1;
|
||||
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
int key;
|
||||
if (!int.TryParse(node.Key, out key) || key < 0 || key >= tiles.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);
|
||||
}
|
||||
|
||||
return tiles;
|
||||
}
|
||||
else
|
||||
{
|
||||
var tiles = new int[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);
|
||||
}
|
||||
|
||||
return tiles;
|
||||
}
|
||||
}
|
||||
|
||||
static readonly string[] Fields = { "Id", "Image", "Frames", "Size", "PickAny" };
|
||||
|
||||
public MiniYaml Save()
|
||||
public int this[int index]
|
||||
{
|
||||
get { return tiles[index]; }
|
||||
}
|
||||
|
||||
public bool Contains(int index)
|
||||
{
|
||||
return index >= 0 && index < tiles.Length;
|
||||
}
|
||||
|
||||
public int TilesCount
|
||||
{
|
||||
get { return tiles.Length; }
|
||||
}
|
||||
|
||||
public MiniYaml Save(TileSet tileSet)
|
||||
{
|
||||
var root = new List<MiniYamlNode>();
|
||||
foreach (var field in Fields)
|
||||
@@ -76,7 +129,7 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
root.Add(new MiniYamlNode("Tiles", null,
|
||||
Tiles.Select(x => new MiniYamlNode(x.Key.ToString(), x.Value)).ToList()));
|
||||
tiles.Select((terrainTypeIndex, templateIndex) => new MiniYamlNode(templateIndex.ToString(), tileSet[terrainTypeIndex].Type)).ToList()));
|
||||
|
||||
return new MiniYaml(null, root);
|
||||
}
|
||||
@@ -91,10 +144,13 @@ namespace OpenRA
|
||||
public readonly string PlayerPalette;
|
||||
public readonly string[] Extensions;
|
||||
public readonly int WaterPaletteRotationBase = 0x60;
|
||||
public readonly Dictionary<string, TerrainTypeInfo> Terrain = new Dictionary<string, TerrainTypeInfo>();
|
||||
public readonly Dictionary<ushort, TileTemplate> Templates = new Dictionary<ushort, TileTemplate>();
|
||||
public readonly string[] EditorTemplateOrder;
|
||||
|
||||
readonly TerrainTypeInfo[] terrainInfo;
|
||||
readonly Dictionary<string, int> terrainIndexByType = new Dictionary<string, int>();
|
||||
readonly int defaultWalkableTerrainIndex;
|
||||
|
||||
static readonly string[] Fields = { "Name", "Id", "SheetSize", "Palette", "Extensions" };
|
||||
|
||||
public TileSet(ModData modData, string filepath)
|
||||
@@ -105,20 +161,83 @@ namespace OpenRA
|
||||
FieldLoader.Load(this, yaml["General"]);
|
||||
|
||||
// TerrainTypes
|
||||
Terrain = yaml["Terrain"].ToDictionary().Values
|
||||
.Select(y => new TerrainTypeInfo(y)).ToDictionary(t => t.Type);
|
||||
terrainInfo = yaml["Terrain"].ToDictionary().Values
|
||||
.Select(y => new TerrainTypeInfo(y))
|
||||
.OrderBy(tt => tt.Type)
|
||||
.ToArray();
|
||||
for (var i = 0; i < terrainInfo.Length; i++)
|
||||
{
|
||||
var tt = terrainInfo[i].Type;
|
||||
|
||||
if (terrainIndexByType.ContainsKey(tt))
|
||||
throw new InvalidDataException("Duplicate terrain type '{0}' in '{1}'.".F(tt, filepath));
|
||||
|
||||
terrainIndexByType.Add(tt, i);
|
||||
}
|
||||
|
||||
defaultWalkableTerrainIndex = GetTerrainIndex("Clear");
|
||||
|
||||
// Templates
|
||||
Templates = yaml["Templates"].ToDictionary().Values
|
||||
.Select(y => new TileTemplate(y)).ToDictionary(t => t.Id);
|
||||
.Select(y => new TileTemplate(this, y)).ToDictionary(t => t.Id);
|
||||
}
|
||||
|
||||
public TileSet(string name, string id, string palette, string[] extensions)
|
||||
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;
|
||||
|
||||
for (var 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");
|
||||
}
|
||||
|
||||
public TerrainTypeInfo this[int index]
|
||||
{
|
||||
get { return terrainInfo[index]; }
|
||||
}
|
||||
|
||||
public int TerrainsCount
|
||||
{
|
||||
get { return terrainInfo.Length; }
|
||||
}
|
||||
|
||||
public bool TryGetTerrainIndex(string type, out int index)
|
||||
{
|
||||
return terrainIndexByType.TryGetValue(type, out index);
|
||||
}
|
||||
|
||||
public int GetTerrainIndex(string type)
|
||||
{
|
||||
int index;
|
||||
if (terrainIndexByType.TryGetValue(type, out index))
|
||||
return index;
|
||||
|
||||
throw new InvalidDataException("Tileset '{0}' lacks terrain type '{1}'".F(Id, type));
|
||||
}
|
||||
|
||||
public int GetTerrainIndex(TileReference<ushort, byte> r)
|
||||
{
|
||||
var tpl = Templates[r.Type];
|
||||
|
||||
if (tpl.Contains(r.Index))
|
||||
{
|
||||
var ti = tpl[r.Index];
|
||||
if (ti != -1)
|
||||
return ti;
|
||||
}
|
||||
|
||||
return defaultWalkableTerrainIndex;
|
||||
}
|
||||
|
||||
public void Save(string filepath)
|
||||
@@ -138,21 +257,16 @@ namespace OpenRA
|
||||
root.Add(new MiniYamlNode("General", null, gen));
|
||||
|
||||
root.Add(new MiniYamlNode("Terrain", null,
|
||||
Terrain.Select(t => new MiniYamlNode("TerrainType@{0}".F(t.Value.Type), t.Value.Save())).ToList()));
|
||||
terrainInfo.Select(t => new MiniYamlNode("TerrainType@{0}".F(t.Type), t.Save())).ToList()));
|
||||
|
||||
root.Add(new MiniYamlNode("Templates", null,
|
||||
Templates.Select(t => new MiniYamlNode("Template@{0}".F(t.Value.Id), t.Value.Save())).ToList()));
|
||||
Templates.Select(t => new MiniYamlNode("Template@{0}".F(t.Value.Id), t.Value.Save(this))).ToList()));
|
||||
root.WriteToFile(filepath);
|
||||
}
|
||||
|
||||
public string GetTerrainType(TileReference<ushort, byte> r)
|
||||
public TerrainTypeInfo GetTerrainInfo(TileReference<ushort, byte> r)
|
||||
{
|
||||
var tt = Templates[r.Type].Tiles;
|
||||
string ret;
|
||||
if (!tt.TryGetValue(r.Index, out ret))
|
||||
return "Clear"; // Default walkable
|
||||
|
||||
return ret;
|
||||
return terrainInfo[GetTerrainIndex(r)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ namespace OpenRA.Traits
|
||||
|
||||
CellContents CreateResourceCell(ResourceType t, CPos p)
|
||||
{
|
||||
world.Map.CustomTerrain[p.X, p.Y] = t.Info.TerrainType;
|
||||
world.Map.CustomTerrain[p.X, p.Y] = world.TileSet.GetTerrainIndex(t.Info.TerrainType);
|
||||
return new CellContents
|
||||
{
|
||||
Type = t,
|
||||
@@ -194,7 +194,7 @@ namespace OpenRA.Traits
|
||||
if (--content[p.X, p.Y].Density < 0)
|
||||
{
|
||||
content[p.X, p.Y] = EmptyCell;
|
||||
world.Map.CustomTerrain[p.X, p.Y] = null;
|
||||
world.Map.CustomTerrain[p.X, p.Y] = -1;
|
||||
}
|
||||
|
||||
if (!dirty.Contains(p))
|
||||
@@ -211,7 +211,7 @@ namespace OpenRA.Traits
|
||||
|
||||
// Clear cell
|
||||
content[p.X, p.Y] = EmptyCell;
|
||||
world.Map.CustomTerrain[p.X, p.Y] = null;
|
||||
world.Map.CustomTerrain[p.X, p.Y] = -1;
|
||||
|
||||
if (!dirty.Contains(p))
|
||||
dirty.Add(p);
|
||||
|
||||
@@ -88,15 +88,15 @@ namespace OpenRA
|
||||
public const int MaxRange = 50;
|
||||
static List<CVec>[] TilesByDistance = InitTilesByDistance(MaxRange);
|
||||
|
||||
public static string GetTerrainType(this World world, CPos cell)
|
||||
public static int GetTerrainIndex(this World world, CPos cell)
|
||||
{
|
||||
var custom = world.Map.CustomTerrain[cell.X, cell.Y];
|
||||
return custom ?? world.TileSet.GetTerrainType(world.Map.MapTiles.Value[cell.X, cell.Y]);
|
||||
return custom != -1 ? custom : world.TileSet.GetTerrainIndex(world.Map.MapTiles.Value[cell.X, cell.Y]);
|
||||
}
|
||||
|
||||
public static TerrainTypeInfo GetTerrainInfo(this World world, CPos cell)
|
||||
{
|
||||
return world.TileSet.Terrain[world.GetTerrainType(cell)];
|
||||
return world.TileSet[world.GetTerrainIndex(cell)];
|
||||
}
|
||||
|
||||
public static CPos ClampToWorld(this World world, CPos xy)
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace OpenRA.Mods.Cnc
|
||||
foreach (var kv in self.OccupiesSpace.OccupiedCells())
|
||||
{
|
||||
totalTiles++;
|
||||
if (info.SafeTerrain.Contains(self.World.GetTerrainType(kv.First)))
|
||||
if (info.SafeTerrain.Contains(self.World.GetTerrainInfo(kv.First).Type))
|
||||
safeTiles++;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace OpenRA.Mods.RA.AI
|
||||
BuildingInfo rallypointTestBuilding;
|
||||
internal readonly HackyAIInfo Info;
|
||||
|
||||
string[] resourceTypes;
|
||||
HashSet<int> resourceTypeIndices;
|
||||
|
||||
RushFuzzy rushFuzzy = new RushFuzzy();
|
||||
|
||||
@@ -152,8 +152,10 @@ namespace OpenRA.Mods.RA.AI
|
||||
|
||||
random = new MersenneTwister((int)p.PlayerActor.ActorID);
|
||||
|
||||
resourceTypes = Map.Rules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>()
|
||||
.Select(t => t.TerrainType).ToArray();
|
||||
resourceTypeIndices = new HashSet<int>(
|
||||
Map.Rules.Actors["world"].Traits
|
||||
.WithInterface<ResourceTypeInfo>()
|
||||
.Select(t => world.TileSet.GetTerrainIndex(t.TerrainType)));
|
||||
}
|
||||
|
||||
static int GetPowerProvidedBy(ActorInfo building)
|
||||
@@ -364,7 +366,7 @@ namespace OpenRA.Mods.RA.AI
|
||||
|
||||
case BuildingType.Refinery:
|
||||
var tilesPos = world.FindTilesInCircle(baseCenter, MaxBaseDistance)
|
||||
.Where(a => resourceTypes.Contains(world.GetTerrainType(new CPos(a.X, a.Y))));
|
||||
.Where(a => resourceTypeIndices.Contains(world.GetTerrainIndex(new CPos(a.X, a.Y))));
|
||||
if (tilesPos.Any())
|
||||
{
|
||||
var pos = tilesPos.MinBy(a => (a.CenterPosition - baseCenter.CenterPosition).LengthSquared);
|
||||
|
||||
@@ -171,7 +171,7 @@ namespace OpenRA.Mods.RA.Air
|
||||
if (self.World.ActorMap.AnyUnitsAt(cell))
|
||||
return false;
|
||||
|
||||
var type = self.World.GetTerrainType(cell);
|
||||
var type = self.World.GetTerrainInfo(cell).Type;
|
||||
return info.LandableTerrainTypes.Contains(type);
|
||||
}
|
||||
|
||||
|
||||
@@ -94,11 +94,11 @@ namespace OpenRA.Mods.RA
|
||||
self.World.Map.CustomTerrain[c.X, c.Y] = GetTerrainType(c);
|
||||
}
|
||||
|
||||
string GetTerrainType(CPos cell)
|
||||
int GetTerrainType(CPos cell)
|
||||
{
|
||||
var dx = cell - self.Location;
|
||||
var index = dx.X + self.World.TileSet.Templates[template].Size.X * dx.Y;
|
||||
return self.World.TileSet.GetTerrainType(new TileReference<ushort, byte>(template, (byte)index));
|
||||
return self.World.TileSet.GetTerrainIndex(new TileReference<ushort, byte>(template, (byte)index));
|
||||
}
|
||||
|
||||
public void LinkNeighbouringBridges(World world, BridgeLayer bridges)
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace OpenRA.Mods.RA.Buildings
|
||||
var cellOffset = new CVec(i % width, i / width + bibOffset);
|
||||
|
||||
// Some mods may define terrain-specific bibs
|
||||
var terrain = self.World.GetTerrainType(location + cellOffset);
|
||||
var terrain = self.World.GetTerrainInfo(location + cellOffset).Type;
|
||||
var testSequence = info.Sequence + "-" + terrain;
|
||||
var sequence = anim.HasSequence(testSequence) ? testSequence : info.Sequence;
|
||||
anim.PlayFetchIndex(sequence, () => index);
|
||||
|
||||
@@ -54,34 +54,34 @@ namespace OpenRA.Mods.RA.Buildings
|
||||
foreach (var c in FootprintUtils.Tiles(self))
|
||||
{
|
||||
// Only place on allowed terrain types
|
||||
if (!info.TerrainTypes.Contains(self.World.GetTerrainType(c)))
|
||||
if (!info.TerrainTypes.Contains(self.World.GetTerrainInfo(c).Type))
|
||||
continue;
|
||||
|
||||
// Don't place under other buildings or custom terrain
|
||||
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != null)
|
||||
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != -1)
|
||||
continue;
|
||||
|
||||
var index = template.Tiles.Keys.Random(Game.CosmeticRandom);
|
||||
layer.AddTile(c, new TileReference<ushort, byte>(template.Id, index));
|
||||
var index = Game.CosmeticRandom.Next(template.TilesCount);
|
||||
layer.AddTile(c, new TileReference<ushort, byte>(template.Id, (byte)index));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var origin = self.Location + info.Offset;
|
||||
foreach (var i in template.Tiles.Keys)
|
||||
for (var i = 0; i < template.TilesCount; i++)
|
||||
{
|
||||
var c = origin + new CVec(i % template.Size.X, i / template.Size.X);
|
||||
|
||||
// Only place on allowed terrain types
|
||||
if (!info.TerrainTypes.Contains(self.World.GetTerrainType(c)))
|
||||
if (!info.TerrainTypes.Contains(self.World.GetTerrainInfo(c).Type))
|
||||
continue;
|
||||
|
||||
// Don't place under other buildings or custom terrain
|
||||
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != null)
|
||||
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != -1)
|
||||
continue;
|
||||
|
||||
layer.AddTile(c, new TileReference<ushort, byte>(template.Id, i));
|
||||
layer.AddTile(c, new TileReference<ushort, byte>(template.Id, (byte)i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace OpenRA.Mods.RA.Buildings
|
||||
if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(a) != null) return false;
|
||||
if (world.ActorMap.GetUnitsAt(a).Any(b => b != toIgnore)) return false;
|
||||
|
||||
return world.Map.IsInMap(a) && bi.TerrainTypes.Contains(world.GetTerrainType(a));
|
||||
return world.Map.IsInMap(a) && bi.TerrainTypes.Contains(world.GetTerrainInfo(a).Type);
|
||||
}
|
||||
|
||||
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace OpenRA.Mods.RA
|
||||
{
|
||||
if (!self.World.Map.IsInMap(cell.X, cell.Y)) return false;
|
||||
|
||||
var type = self.World.GetTerrainType(cell);
|
||||
var type = self.World.GetTerrainInfo(cell).Type;
|
||||
if (!info.TerrainTypes.Contains(type))
|
||||
return false;
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ namespace OpenRA.Mods.RA
|
||||
var p = self.World.ChooseRandomCell(self.World.SharedRandom);
|
||||
|
||||
// Is this valid terrain?
|
||||
var terrainType = self.World.GetTerrainType(p);
|
||||
var terrainType = self.World.GetTerrainInfo(p).Type;
|
||||
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType))
|
||||
continue;
|
||||
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace OpenRA.Mods.RA.Effects
|
||||
|| (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel
|
||||
|| (!info.High && world.ActorMap.GetUnitsAt(cell)
|
||||
.Any(a => a.HasTrait<IBlocksBullets>())) // Hit a wall
|
||||
|| (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.GetTerrainType(cell) != info.BoundToTerrainType); // Hit incompatible terrain
|
||||
|| (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.GetTerrainInfo(cell).Type != info.BoundToTerrainType); // Hit incompatible terrain
|
||||
|
||||
if (shouldExplode)
|
||||
Explode(world);
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace OpenRA.Mods.RA
|
||||
if (!self.World.Map.IsInMap(cell.X, cell.Y))
|
||||
return false;
|
||||
|
||||
if (!info.AllowedTerrain.Contains(self.World.GetTerrainType(cell)))
|
||||
if (!info.AllowedTerrain.Contains(self.World.GetTerrainInfo(cell).Type))
|
||||
return false;
|
||||
|
||||
if (!checkTransientActors)
|
||||
|
||||
@@ -48,16 +48,55 @@ namespace OpenRA.Mods.RA.Move
|
||||
var cost = nodesDict.ContainsKey("PathingCost")
|
||||
? FieldLoader.GetValue<int>("cost", nodesDict["PathingCost"].Value)
|
||||
: (int)(10000 / speed);
|
||||
ret.Add(t.Key, new TerrainInfo { Speed = speed, Cost = cost });
|
||||
ret.Add(t.Key, new TerrainInfo(speed, cost));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
TerrainInfo[] LoadTilesetSpeeds(TileSet tileSet)
|
||||
{
|
||||
var info = new TerrainInfo[tileSet.TerrainsCount];
|
||||
for (var i = 0; i < info.Length; i++)
|
||||
info[i] = TerrainInfo.Impassable;
|
||||
|
||||
foreach (var kvp in TerrainSpeeds)
|
||||
{
|
||||
int index;
|
||||
if (tileSet.TryGetTerrainIndex(kvp.Key, out index))
|
||||
info[index] = kvp.Value;
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
public class TerrainInfo
|
||||
{
|
||||
public int Cost = int.MaxValue;
|
||||
public decimal Speed = 0;
|
||||
public static readonly TerrainInfo Impassable = new TerrainInfo();
|
||||
|
||||
public readonly int Cost;
|
||||
public readonly decimal Speed;
|
||||
|
||||
public TerrainInfo()
|
||||
{
|
||||
Cost = int.MaxValue;
|
||||
Speed = 0;
|
||||
}
|
||||
|
||||
public TerrainInfo(decimal speed, int cost)
|
||||
{
|
||||
Speed = speed;
|
||||
Cost = cost;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly Cache<TileSet, TerrainInfo[]> TilesetTerrainInfo;
|
||||
public readonly Cache<TileSet, int> TilesetMovementClass;
|
||||
|
||||
public MobileInfo()
|
||||
{
|
||||
TilesetTerrainInfo = new Cache<TileSet, TerrainInfo[]>(LoadTilesetSpeeds);
|
||||
TilesetMovementClass = new Cache<TileSet, int>(CalculateTilesetMovementClass);
|
||||
}
|
||||
|
||||
public int MovementCostForCell(World world, CPos cell)
|
||||
@@ -65,20 +104,22 @@ namespace OpenRA.Mods.RA.Move
|
||||
if (!world.Map.IsInMap(cell.X, cell.Y))
|
||||
return int.MaxValue;
|
||||
|
||||
var type = world.GetTerrainType(cell);
|
||||
if (!TerrainSpeeds.ContainsKey(type))
|
||||
var index = world.GetTerrainIndex(cell);
|
||||
if (index == -1)
|
||||
return int.MaxValue;
|
||||
|
||||
return TerrainSpeeds[type].Cost;
|
||||
return TilesetTerrainInfo[world.TileSet][index].Cost;
|
||||
}
|
||||
|
||||
public int CalculateTilesetMovementClass(TileSet tileset)
|
||||
{
|
||||
/* collect our ability to cross *all* terraintypes, in a bitvector */
|
||||
return TilesetTerrainInfo[tileset].Select(ti => ti.Cost < int.MaxValue).ToBits();
|
||||
}
|
||||
|
||||
public int GetMovementClass(TileSet tileset)
|
||||
{
|
||||
/* collect our ability to cross *all* terraintypes, in a bitvector */
|
||||
var passability = tileset.Terrain.OrderBy(t => t.Key)
|
||||
.Select(t => TerrainSpeeds.ContainsKey(t.Key) && TerrainSpeeds[t.Key].Cost < int.MaxValue);
|
||||
|
||||
return passability.ToBits();
|
||||
return TilesetMovementClass[tileset];
|
||||
}
|
||||
|
||||
public static readonly Dictionary<SubCell, WVec> SubCellOffsets = new Dictionary<SubCell, WVec>()
|
||||
@@ -440,12 +481,15 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
public int MovementSpeedForCell(Actor self, CPos cell)
|
||||
{
|
||||
var type = self.World.GetTerrainType(cell);
|
||||
|
||||
if (!Info.TerrainSpeeds.ContainsKey(type))
|
||||
var index = self.World.GetTerrainIndex(cell);
|
||||
if (index == -1)
|
||||
return 0;
|
||||
|
||||
decimal speed = Info.Speed * Info.TerrainSpeeds[type].Speed;
|
||||
var speed = Info.TilesetTerrainInfo[self.World.TileSet][index].Speed;
|
||||
if (speed == decimal.Zero)
|
||||
return 0;
|
||||
|
||||
speed *= Info.Speed;
|
||||
foreach (var t in self.TraitsImplementing<ISpeedModifier>())
|
||||
speed *= t.GetSpeedModifier();
|
||||
return (int)(speed / 100);
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
public RadarColorFromTerrain(Actor self, string terrain)
|
||||
{
|
||||
c = self.World.TileSet.Terrain[terrain].Color;
|
||||
c = self.World.TileSet[self.World.TileSet.GetTerrainIndex(terrain)].Color;
|
||||
}
|
||||
|
||||
public bool VisibleOnRadar(Actor self) { return true; }
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace OpenRA.Mods.RA.Render
|
||||
return false;
|
||||
|
||||
return cargo.CurrentAdjacentCells
|
||||
.Any(c => self.World.Map.IsInMap(c) && info.OpenTerrainTypes.Contains(self.World.GetTerrainType(c)));
|
||||
.Any(c => self.World.Map.IsInMap(c) && info.OpenTerrainTypes.Contains(self.World.GetTerrainInfo(c).Type));
|
||||
}
|
||||
|
||||
void Open()
|
||||
|
||||
@@ -71,36 +71,18 @@ namespace OpenRA.Mods.RA
|
||||
var range = ((ChronoshiftPowerInfo)Info).Range;
|
||||
var sourceTiles = self.World.FindTilesInCircle(xy, range);
|
||||
var destTiles = self.World.FindTilesInCircle(sourceLocation, range);
|
||||
var sourceTerrain = new List<string>();
|
||||
var destTerrain = new List<string>();
|
||||
|
||||
int j = 0;
|
||||
foreach (var t in sourceTiles)
|
||||
using (var se = sourceTiles.GetEnumerator())
|
||||
using (var de = destTiles.GetEnumerator())
|
||||
while (se.MoveNext() && de.MoveNext())
|
||||
{
|
||||
j = j + 1;
|
||||
if (!self.Owner.Shroud.IsExplored(t))
|
||||
return false;
|
||||
sourceTerrain.Add(self.World.GetTerrainType(t));
|
||||
}
|
||||
var a = se.Current;
|
||||
var b = de.Current;
|
||||
|
||||
j = 0;
|
||||
foreach (var t in destTiles)
|
||||
{
|
||||
j = j + 1;
|
||||
if (!self.Owner.Shroud.IsExplored(t))
|
||||
if (!self.Owner.Shroud.IsExplored(a) || !self.Owner.Shroud.IsExplored(b))
|
||||
return false;
|
||||
|
||||
self.World.GetTerrainType(t);
|
||||
destTerrain.Add(self.World.GetTerrainType(t));
|
||||
}
|
||||
|
||||
// HACK but I don't want to write a comparison function
|
||||
if (sourceTerrain.Count != destTerrain.Count)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < sourceTerrain.Count; i++)
|
||||
{
|
||||
if (!sourceTerrain[i].Equals(destTerrain[i]))
|
||||
if (self.World.GetTerrainIndex(a) != self.World.GetTerrainIndex(b))
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
public void AddTile(CPos cell, TileReference<ushort, byte> tile)
|
||||
{
|
||||
map.CustomTerrain[cell.X, cell.Y] = tileset.GetTerrainType(tile);
|
||||
map.CustomTerrain[cell.X, cell.Y] = tileset.GetTerrainIndex(tile);
|
||||
|
||||
// Terrain tiles define their origin at the topleft
|
||||
var s = theater.TileSprite(tile);
|
||||
|
||||
@@ -59,10 +59,6 @@ namespace OpenRA.Mods.RA
|
||||
int[,] domains;
|
||||
Dictionary<int, HashSet<int>> transientConnections;
|
||||
|
||||
// Each terrain has an offset corresponding to its location in a
|
||||
// movement class bitmask. This caches each offset.
|
||||
Dictionary<string, int> terrainOffsets;
|
||||
|
||||
public MovementClassDomainIndex(World world, uint movementClass)
|
||||
{
|
||||
bounds = world.Map.Bounds;
|
||||
@@ -70,14 +66,6 @@ namespace OpenRA.Mods.RA
|
||||
domains = new int[(bounds.Width + bounds.X), (bounds.Height + bounds.Y)];
|
||||
transientConnections = new Dictionary<int, HashSet<int>>();
|
||||
|
||||
terrainOffsets = new Dictionary<string, int>();
|
||||
var terrains = world.TileSet.Terrain.OrderBy(t => t.Key).ToList();
|
||||
foreach (var terrain in terrains)
|
||||
{
|
||||
var terrainOffset = terrains.FindIndex(x => x.Key == terrain.Key);
|
||||
terrainOffsets[terrain.Key] = terrainOffset;
|
||||
}
|
||||
|
||||
BuildDomains(world);
|
||||
}
|
||||
|
||||
@@ -181,8 +169,7 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
bool CanTraverseTile(World world, CPos p)
|
||||
{
|
||||
var currentTileType = WorldUtils.GetTerrainType(world, p);
|
||||
var terrainOffset = terrainOffsets[currentTileType];
|
||||
var terrainOffset = world.GetTerrainIndex(p);
|
||||
return (movementClass & (1 << terrainOffset)) > 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -367,7 +367,8 @@ namespace OpenRA.TilesetBuilder
|
||||
name: tilesetName,
|
||||
id: tilesetID.ToUpper(),
|
||||
palette: tilesetPalette.ToLower(),
|
||||
extensions: new string[] { ext[0], ext[1] }
|
||||
extensions: new string[] { ext[0], ext[1] },
|
||||
terrainInfo: TerrainType
|
||||
);
|
||||
|
||||
// List of files to add to the mix file
|
||||
@@ -381,30 +382,25 @@ namespace OpenRA.TilesetBuilder
|
||||
foreach (var t in surface1.Templates)
|
||||
fileList.Add(ExportTemplate(t, surface1.Templates.IndexOf(t), tileset.Extensions.First(), dir));
|
||||
|
||||
// Add the terraintypes
|
||||
foreach (var tt in TerrainType)
|
||||
{
|
||||
tileset.Terrain.Add(tt.Type, tt);
|
||||
}
|
||||
|
||||
// Add the templates
|
||||
ushort cur = 0;
|
||||
foreach (var tp in surface1.Templates)
|
||||
{
|
||||
var tiles = new int[tp.Width * tp.Height];
|
||||
foreach (var t in tp.Cells)
|
||||
{
|
||||
string ttype = TerrainType[surface1.TerrainTypes[t.Key.X, t.Key.Y]].Type;
|
||||
var idx = (t.Key.X - tp.Left) + tp.Width * (t.Key.Y - tp.Top);
|
||||
tiles[idx] = tileset.GetTerrainIndex(ttype);
|
||||
}
|
||||
|
||||
var template = new TileTemplate(
|
||||
id: cur,
|
||||
image: "{0}{1:00}".F(txtTilesetName.Text, cur),
|
||||
size: new int2(tp.Width, tp.Height)
|
||||
size: new int2(tp.Width, tp.Height),
|
||||
tiles: tiles
|
||||
);
|
||||
|
||||
foreach (var t in tp.Cells)
|
||||
{
|
||||
string ttype = "Clear";
|
||||
ttype = TerrainType[surface1.TerrainTypes[t.Key.X, t.Key.Y]].Type;
|
||||
var idx = (t.Key.X - tp.Left) + tp.Width * (t.Key.Y - tp.Top);
|
||||
template.Tiles.Add((byte)idx, ttype);
|
||||
}
|
||||
|
||||
tileset.Templates.Add(cur, template);
|
||||
cur++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user