Skip to content

Commit

Permalink
Implementation of map format interfaces IMap, IMapTiles, IMapResource…
Browse files Browse the repository at this point in the history
…s, and IMapElevation
  • Loading branch information
AspectInteractive2 committed Mar 18, 2023
1 parent 16b6c77 commit 9c1d13a
Show file tree
Hide file tree
Showing 161 changed files with 1,722 additions and 1,538 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Game/CPos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override string ToString()
return X + "," + Y + "," + Layer;
}

public MPos ToMPos(Map map)
public MPos ToMPos(IMap map)
{
return ToMPos(map.Grid.Type);
}
Expand Down
10 changes: 6 additions & 4 deletions OpenRA.Game/GameRules/WeaponInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public bool IsValidTarget(BitSet<TargetableType> targetTypes)
/// <summary>Checks if the weapon is valid against (can target) the target.</summary>
public bool IsValidAgainst(in Target target, World world, Actor firedBy)
{
var map = world.Map;

if (target.Type == TargetType.Actor)
return IsValidAgainst(target.Actor, firedBy);

Expand All @@ -188,15 +190,15 @@ public bool IsValidAgainst(in Target target, World world, Actor firedBy)

if (target.Type == TargetType.Terrain)
{
var dat = world.Map.DistanceAboveTerrain(target.CenterPosition);
var dat = map.DistanceAboveTerrain(target.CenterPosition);
if (dat > AirThreshold)
return IsValidTarget(TargetTypeAir);

var cell = world.Map.CellContaining(target.CenterPosition);
if (!world.Map.Contains(cell))
var cell = map.CellContaining(target.CenterPosition);
if (!map.Contains(cell))
return false;

var cellInfo = world.Map.GetTerrainInfo(cell);
var cellInfo = map.GetTerrainInfo(cell);
if (!IsValidTarget(cellInfo.TargetTypes))
return false;

Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Game/Graphics/TerrainSpriteLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class TerrainSpriteLayer : IDisposable
readonly bool restrictToBounds;

readonly WorldRenderer worldRenderer;
readonly Map map;
readonly IMap map;

readonly PaletteReference[] palettes;

Expand Down Expand Up @@ -88,7 +88,7 @@ public void Update(CPos cell, Sprite sprite, PaletteReference palette, float sca
var xyz = float3.Zero;
if (sprite != null)
{
var cellOrigin = map.CenterOfCell(cell) - new WVec(0, 0, map.Grid.Ramps[map.Ramp[cell]].CenterHeightOffset);
var cellOrigin = map.CenterOfCell(cell) - new WVec(0, 0, map.Grid.Ramps[((IMapElevation)map).Ramp[cell]].CenterHeightOffset);
xyz = worldRenderer.Screen3DPosition(cellOrigin) + scale * (sprite.Offset - 0.5f * sprite.Size);
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public void Update(MPos uv, Sprite sprite, PaletteReference palette, in float3 p
}

// The vertex buffer does not have geometry for cells outside the map
if (!map.Tiles.Contains(uv))
if (!((IMapTiles)map).Tiles.Contains(uv))
return;

var offset = rowStride * uv.V + 6 * uv.U;
Expand Down
5 changes: 3 additions & 2 deletions OpenRA.Game/Graphics/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public ScrollDirection GetBlockedDirections()
return ret;
}

public Viewport(WorldRenderer wr, Map map)
public Viewport(WorldRenderer wr, IMap map)
{
worldRenderer = wr;
var grid = Game.ModData.Manifest.Get<MapGrid>();
Expand Down Expand Up @@ -242,6 +242,7 @@ public CPos ViewToWorld(int2 view)
{
var world = worldRenderer.Viewport.ViewToWorldPx(view);
var map = worldRenderer.World.Map;
var mapRamp = ((IMapElevation)map).Ramp;
var candidates = CandidateMouseoverCells(world).ToList();

foreach (var uv in candidates)
Expand All @@ -251,7 +252,7 @@ public CPos ViewToWorld(int2 view)
var s = worldRenderer.ScreenPxPosition(p);
if (Math.Abs(s.X - world.X) <= tileSize.Width && Math.Abs(s.Y - world.Y) <= tileSize.Height)
{
var ramp = map.Grid.Ramps[map.Ramp.Contains(uv) ? map.Ramp[uv] : 0];
var ramp = map.Grid.Ramps[mapRamp.Contains(uv) ? mapRamp[uv] : 0];
var pos = map.CenterOfCell(uv.ToCPos(map)) - new WVec(0, 0, ramp.CenterHeightOffset);
var screen = ramp.Corners.Select(c => worldRenderer.ScreenPxPosition(pos + c)).ToArray();
if (screen.PolygonContains(world))
Expand Down
5 changes: 3 additions & 2 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public sealed class WorldRenderer : IDisposable
internal WorldRenderer(ModData modData, World world)
{
World = world;
TileSize = World.Map.Grid.TileSize;
TileScale = World.Map.Grid.Type == MapGridType.RectangularIsometric ? 1448 : 1024;
var map = World.Map;
TileSize = map.Grid.TileSize;
TileScale = map.Grid.Type == MapGridType.RectangularIsometric ? 1448 : 1024;
Viewport = new Viewport(this, world.Map);

createPaletteReference = CreatePaletteReference;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/MPos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MPos Clamp(Rectangle r)

public override string ToString() { return U + "," + V; }

public CPos ToCPos(Map map)
public CPos ToCPos(IMap map)
{
return ToCPos(map.Grid.Type);
}
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Map/CellLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public sealed class CellLayer<T> : CellLayerBase<T>
{
public event Action<CPos> CellEntryChanged = null;

public CellLayer(Map map)
public CellLayer(IMap map)
: base(map) { }

public CellLayer(MapGridType gridType, Size size)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Map/CellLayerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class CellLayerBase<T> : IEnumerable<T>
protected readonly T[] Entries;
protected readonly Rectangle Bounds;

public CellLayerBase(Map map)
public CellLayerBase(IMap map)
: this(map.Grid.Type, new Size(map.MapSize.X, map.MapSize.Y)) { }

public CellLayerBase(MapGridType gridType, Size size)
Expand Down
78 changes: 78 additions & 0 deletions OpenRA.Game/Map/IMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits;

namespace OpenRA
{
public interface IMap : IDisposable
{
Dictionary<CPos, TerrainTile> ReplacedInvalidTerrainTiles { get; }
MapGrid Grid { get; }
int2 MapSize { get; }
string Tileset { get; }
Ruleset Rules { get; }
SequenceSet Sequences { get; }
CellRegion AllCells { get; }
List<CPos> AllEdgeCells { get; }
WDist CellHeightStep { get; }
CellLayer<byte> CustomTerrain { get; }
WPos ProjectedBottomRight { get; }
PPos[] ProjectedCells { get; }
WPos ProjectedTopLeft { get; }
Rectangle Bounds { get; }
int MapFormat { get; }
byte GetTerrainIndex(CPos cell);
TerrainTypeInfo GetTerrainInfo(CPos cell);
void NewSize(Size size, ITerrainInfo terrainInfo);
WVec Offset(CVec delta, int dz);
WAngle FacingBetween(CPos cell, CPos towards, WAngle fallbackfacing);
IEnumerable<CPos> FindTilesInAnnulus(CPos center, int minRange, int maxRange, bool allowOutsideBounds = false);
IEnumerable<CPos> FindTilesInCircle(CPos center, int maxRange, bool allowOutsideBounds = false);
CPos CellContaining(WPos pos);
PPos ProjectedCellCovering(WPos pos);
WDist DistanceAboveTerrain(WPos pos);
WPos CenterOfCell(CPos cell);
WPos CenterOfSubCell(CPos cell, SubCell subCell);
CPos ChooseClosestEdgeCell(CPos cell);
MPos ChooseClosestEdgeCell(MPos uv);
CPos ChooseClosestMatchingEdgeCell(CPos cell, Func<CPos, bool> match);
CPos ChooseRandomCell(MersenneTwister rand);
CPos ChooseRandomEdgeCell(MersenneTwister rand);
CPos Clamp(CPos cell);
MPos Clamp(MPos uv);
PPos Clamp(PPos puv);
bool Contains(CPos cell);
bool Contains(MPos uv);
bool Contains(PPos puv);
WDist DistanceToEdge(WPos pos, in WVec dir);
PPos[] ProjectedCellsCovering(MPos uv);

event Action<CPos> CellProjectionChanged;
byte ProjectedHeight(PPos puv);
void Resize(int width, int height);
void SetBounds(PPos tl, PPos br);
WRot TerrainOrientation(CPos cell);
List<MPos> Unproject(PPos puv);
(Color Left, Color Right) GetTerrainColorPair(MPos uv);
byte[] SavePreview();
}

public interface IMapElevation : IMap
{
CellLayer<byte> Ramp { get; }
CellLayer<byte> Height { get; }
}

public interface IMapTiles : IMap
{
CellLayer<TerrainTile> Tiles { get; }
}

public interface IMapResource : IMap
{
CellLayer<ResourceTile> Resources { get; }
}
}

0 comments on commit 9c1d13a

Please sign in to comment.