Skip to content

Commit

Permalink
Removed IMapCell, moved IMap to DefaultMap and removed interface on M…
Browse files Browse the repository at this point in the history
…ap.cs, and added interfaces for Height, Ramp, and Tiles properties.
  • Loading branch information
AspectInteractive2 committed Jan 30, 2023
1 parent 265dce0 commit 892f19a
Show file tree
Hide file tree
Showing 244 changed files with 1,115 additions and 1,221 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(IMapCell map)
public MPos ToMPos(IMap map)
{
return ToMPos(map.Grid.Type);
}
Expand Down
10 changes: 5 additions & 5 deletions OpenRA.Game/GameRules/WeaponInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ 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 mapCell = (IMapCell)world.Map;
var map = world.Map;

if (target.Type == TargetType.Actor)
return IsValidAgainst(target.Actor, firedBy);
Expand All @@ -190,15 +190,15 @@ public bool IsValidAgainst(in Target target, World world, Actor firedBy)

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

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

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

Expand Down
13 changes: 5 additions & 8 deletions OpenRA.Game/Graphics/TerrainSpriteLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ public void Update(CPos cell, ISpriteSequence sequence, PaletteReference palette

public void Update(CPos cell, Sprite sprite, PaletteReference palette, float scale = 1f, float alpha = 1f, bool ignoreTint = false)
{
var mapCell = (IMapCell)map;

var xyz = float3.Zero;
if (sprite != null)
{
var cellOrigin = mapCell.CenterOfCell(cell) - new WVec(0, 0, mapCell.Grid.Ramps[mapCell.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);
}

Update(cell.ToMPos(mapCell.Grid.Type), sprite, palette, xyz, scale, alpha, ignoreTint);
Update(cell.ToMPos(map.Grid.Type), sprite, palette, xyz, scale, alpha, ignoreTint);
}

void UpdateTint(MPos uv)
Expand All @@ -115,9 +113,8 @@ void UpdateTint(MPos uv)
// This is done by sampling the lighting the corners of the sprite, even though those pixels are
// transparent for isometric tiles
var tl = worldRenderer.TerrainLighting;
var mapCell = (IMapCell)map;
var pos = mapCell.CenterOfCell(uv.ToCPos((IMapCell)map));
var step = mapCell.Grid.Type == MapGridType.RectangularIsometric ? 724 : 512;
var pos = map.CenterOfCell(uv.ToCPos(map));
var step = map.Grid.Type == MapGridType.RectangularIsometric ? 724 : 512;
var weights = new[]
{
tl.TintAt(pos + new WVec(-step, -step, 0)),
Expand Down Expand Up @@ -180,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 (!((IMapCell)map).Tiles.Contains(uv))
if (!((IMapTiles)map).Tiles.Contains(uv))
return;

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

public Viewport(WorldRenderer wr, IMapCell map)
public Viewport(WorldRenderer wr, IMap map)
{
worldRenderer = wr;
var grid = Game.ModData.Manifest.Get<MapGrid>();
Expand All @@ -166,7 +166,7 @@ public Viewport(WorldRenderer wr, IMapCell map)
// The full map is visible in the editor
var width = map.MapSize.X * grid.TileSize.Width;
var height = map.MapSize.Y * grid.TileSize.Height;
if (((IMapCell)wr.World.Map).Grid.Type == MapGridType.RectangularIsometric)
if (wr.World.Map.Grid.Type == MapGridType.RectangularIsometric)
height /= 2;

mapBounds = new Rectangle(0, 0, width, height);
Expand Down Expand Up @@ -260,7 +260,8 @@ void UpdateViewportZooms(bool resetCurrentZoom = true)
public CPos ViewToWorld(int2 view)
{
var world = worldRenderer.Viewport.ViewToWorldPx(view);
var map = (IMapCell)worldRenderer.World.Map;
var map = worldRenderer.World.Map;
var mapRamp = ((IMapElevation)map).Ramp;
var candidates = CandidateMouseoverCells(world).ToList();

foreach (var uv in candidates)
Expand All @@ -270,7 +271,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 All @@ -294,18 +295,18 @@ public CPos ViewToWorld(int2 view)
}

// Something is very wrong, but lets return something that isn't completely bogus and hope the caller can recover
return ((IMapCell)worldRenderer.World.Map).CellContaining(worldRenderer.ProjectedPosition(ViewToWorldPx(view)));
return worldRenderer.World.Map.CellContaining(worldRenderer.ProjectedPosition(ViewToWorldPx(view)));
}

/// <summary> Returns an unfiltered list of all cells that could potentially contain the mouse cursor</summary>
IEnumerable<MPos> CandidateMouseoverCells(int2 world)
{
var mapCell = (IMapCell)worldRenderer.World.Map;
var map = worldRenderer.World.Map;
var minPos = worldRenderer.ProjectedPosition(world);

// Find all the cells that could potentially have been clicked
var a = mapCell.CellContaining(minPos - new WVec(1024, 0, 0)).ToMPos(mapCell.Grid.Type);
var b = mapCell.CellContaining(minPos + new WVec(512, 512 * mapCell.Grid.MaximumTerrainHeight, 0)).ToMPos(mapCell.Grid.Type);
var a = map.CellContaining(minPos - new WVec(1024, 0, 0)).ToMPos(map.Grid.Type);
var b = map.CellContaining(minPos + new WVec(512, 512 * map.Grid.MaximumTerrainHeight, 0)).ToMPos(map.Grid.Type);

for (var v = b.V; v >= a.V; v--)
for (var u = b.U; u >= a.U; u--)
Expand Down Expand Up @@ -347,7 +348,7 @@ public Rectangle GetScissorBounds(bool insideBounds)
{
// Visible rectangle in world coordinates (expanded to the corners of the cells)
var bounds = insideBounds ? VisibleCellsInsideBounds : AllVisibleCells;
var map = (IMapCell)worldRenderer.World.Map;
var map = worldRenderer.World.Map;
var ctl = map.CenterOfCell(((MPos)bounds.TopLeft).ToCPos(map)) - new WVec(512, 512, 0);
var cbr = map.CenterOfCell(((MPos)bounds.BottomRight).ToCPos(map)) + new WVec(512, 512, 0);

Expand All @@ -362,7 +363,7 @@ public Rectangle GetScissorBounds(bool insideBounds)

ProjectedCellRegion CalculateVisibleCells(bool insideBounds)
{
var map = (IMapCell)worldRenderer.World.Map;
var map = worldRenderer.World.Map;

// Calculate the projected cell position at the corners of the visible area
var tl = (PPos)map.CellContaining(worldRenderer.ProjectedPosition(TopLeft)).ToMPos(map);
Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public sealed class WorldRenderer : IDisposable
internal WorldRenderer(ModData modData, World world)
{
World = world;
var mapCell = (IMapCell)World.Map;
TileSize = mapCell.Grid.TileSize;
TileScale = mapCell.Grid.Type == MapGridType.RectangularIsometric ? 1448 : 1024;
Viewport = new Viewport(this, (IMapCell)world.Map);
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(IMapCell 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(IMapCell 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(IMapCell 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
95 changes: 63 additions & 32 deletions OpenRA.Game/Map/IMap.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,76 @@
using System;
using System.Collections.Generic;
using System.IO;
using OpenRA.FileSystem;
using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits;

namespace OpenRA
{
public interface IMap
{
short InvalidCachedTerrainIndex { get; }
string RequiresMod { get; set; }
string Title { get; }
string Author { get; }
bool LockPreview { get; }
Dictionary<CPos, TerrainTile> ReplacedInvalidTerrainTiles { get; }
MapGrid Grid { get; }
int2 MapSize { get; }
string Tileset { get; }
Ruleset Rules { 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; }
MapVisibility Visibility { get; }
string[] Categories { get; }
string[] Translations { 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);

int2 MapSize { get; }
List<MiniYamlNode> PlayerDefinitions { get; set; }
List<MiniYamlNode> ActorDefinitions { get; set; }
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();
}

MiniYaml RuleDefinitions { get; }
MiniYaml SequenceDefinitions { get; }
MiniYaml ModelSequenceDefinitions { get; }
MiniYaml WeaponDefinitions { get; }
MiniYaml VoiceDefinitions { get; }
MiniYaml MusicDefinitions { get; }
MiniYaml NotificationDefinitions { get; }
bool InvalidCustomRules { get; }
Exception InvalidCustomRulesException { get; }
IReadOnlyPackage Package { get; }
Ruleset Rules { get; }
string Uid { get; }
bool Exists(string filename);
bool IsExternalModFile(string filename);
Stream Open(string filename);
void Save(IReadWritePackage package);
string Translate(string key, IDictionary<string, object> args = null);
bool TryGetPackageContaining(string path, out IReadOnlyPackage package, out string filename);
bool TryOpen(string filename, out Stream s);
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; }
}
}
57 changes: 0 additions & 57 deletions OpenRA.Game/Map/IMapCell.cs

This file was deleted.

13 changes: 0 additions & 13 deletions OpenRA.Game/Map/IMapResource.cs

This file was deleted.

0 comments on commit 892f19a

Please sign in to comment.