Move GetTerrainIndex/Info from WorldUtils to Map

This commit is contained in:
Pavlos Touboulidis
2014-06-13 13:57:32 +03:00
parent 0cf2d608e4
commit c282fa1077
20 changed files with 43 additions and 39 deletions

View File

@@ -182,7 +182,7 @@ namespace OpenRA.GameRules
if (!world.Map.IsInMap(cell)) if (!world.Map.IsInMap(cell))
return false; return false;
var cellInfo = world.GetTerrainInfo(cell); var cellInfo = world.Map.GetTerrainInfo(cell);
if (!ValidTargets.Intersect(cellInfo.TargetTypes).Any() if (!ValidTargets.Intersect(cellInfo.TargetTypes).Any()
|| InvalidTargets.Intersect(cellInfo.TargetTypes).Any()) || InvalidTargets.Intersect(cellInfo.TargetTypes).Any())
return false; return false;

View File

@@ -537,5 +537,18 @@ namespace OpenRA
} }
} }
} }
public int GetTerrainIndex(CPos cell)
{
var custom = CustomTerrain[cell.X, cell.Y];
var tileSet = Rules.TileSets[Tileset];
return custom != -1 ? custom : tileSet.GetTerrainIndex(MapTiles.Value[cell.X, cell.Y]);
}
public TerrainTypeInfo GetTerrainInfo(CPos cell)
{
var tileSet = Rules.TileSets[Tileset];
return tileSet[GetTerrainIndex(cell)];
}
} }
} }

View File

@@ -145,7 +145,7 @@ namespace OpenRA.Traits
if (!world.Map.IsInMap(a.X, a.Y)) if (!world.Map.IsInMap(a.X, a.Y))
return false; return false;
if (!rt.Info.AllowedTerrainTypes.Contains(world.GetTerrainInfo(a).Type)) if (!rt.Info.AllowedTerrainTypes.Contains(world.Map.GetTerrainInfo(a).Type))
return false; return false;
if (!rt.Info.AllowUnderActors && world.ActorMap.AnyUnitsAt(a)) if (!rt.Info.AllowUnderActors && world.ActorMap.AnyUnitsAt(a))

View File

@@ -88,17 +88,6 @@ namespace OpenRA
public const int MaxRange = 50; public const int MaxRange = 50;
static List<CVec>[] TilesByDistance = InitTilesByDistance(MaxRange); static List<CVec>[] TilesByDistance = InitTilesByDistance(MaxRange);
public static int GetTerrainIndex(this World world, CPos cell)
{
var custom = world.Map.CustomTerrain[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[world.GetTerrainIndex(cell)];
}
public static CPos ClampToWorld(this World world, CPos xy) public static CPos ClampToWorld(this World world, CPos xy)
{ {
var r = world.Map.Bounds; var r = world.Map.Bounds;

View File

@@ -49,7 +49,7 @@ namespace OpenRA.Mods.Cnc
foreach (var kv in self.OccupiesSpace.OccupiedCells()) foreach (var kv in self.OccupiesSpace.OccupiedCells())
{ {
totalTiles++; totalTiles++;
if (info.SafeTerrain.Contains(self.World.GetTerrainInfo(kv.First).Type)) if (info.SafeTerrain.Contains(self.World.Map.GetTerrainInfo(kv.First).Type))
safeTiles++; safeTiles++;
} }

View File

@@ -366,7 +366,7 @@ namespace OpenRA.Mods.RA.AI
case BuildingType.Refinery: case BuildingType.Refinery:
var tilesPos = world.FindTilesInCircle(baseCenter, MaxBaseDistance) var tilesPos = world.FindTilesInCircle(baseCenter, MaxBaseDistance)
.Where(a => resourceTypeIndices.Contains(world.GetTerrainIndex(new CPos(a.X, a.Y)))); .Where(a => resourceTypeIndices.Contains(Map.GetTerrainIndex(new CPos(a.X, a.Y))));
if (tilesPos.Any()) if (tilesPos.Any())
{ {
var pos = tilesPos.MinBy(a => (a.CenterPosition - baseCenter.CenterPosition).LengthSquared); var pos = tilesPos.MinBy(a => (a.CenterPosition - baseCenter.CenterPosition).LengthSquared);

View File

@@ -171,7 +171,7 @@ namespace OpenRA.Mods.RA.Air
if (self.World.ActorMap.AnyUnitsAt(cell)) if (self.World.ActorMap.AnyUnitsAt(cell))
return false; return false;
var type = self.World.GetTerrainInfo(cell).Type; var type = self.World.Map.GetTerrainInfo(cell).Type;
return info.LandableTerrainTypes.Contains(type); return info.LandableTerrainTypes.Contains(type);
} }

View File

@@ -44,6 +44,7 @@ namespace OpenRA.Mods.RA.Buildings
var centerOffset = FootprintUtils.CenterOffset(bi); var centerOffset = FootprintUtils.CenterOffset(bi);
var location = self.Location; var location = self.Location;
var rows = info.HasMinibib ? 1 : 2; var rows = info.HasMinibib ? 1 : 2;
var map = self.World.Map;
for (var i = 0; i < rows * width; i++) for (var i = 0; i < rows * width; i++)
{ {
@@ -52,7 +53,7 @@ namespace OpenRA.Mods.RA.Buildings
var cellOffset = new CVec(i % width, i / width + bibOffset); var cellOffset = new CVec(i % width, i / width + bibOffset);
// Some mods may define terrain-specific bibs // Some mods may define terrain-specific bibs
var terrain = self.World.GetTerrainInfo(location + cellOffset).Type; var terrain = map.GetTerrainInfo(location + cellOffset).Type;
var testSequence = info.Sequence + "-" + terrain; var testSequence = info.Sequence + "-" + terrain;
var sequence = anim.HasSequence(testSequence) ? testSequence : info.Sequence; var sequence = anim.HasSequence(testSequence) ? testSequence : info.Sequence;
anim.PlayFetchIndex(sequence, () => index); anim.PlayFetchIndex(sequence, () => index);

View File

@@ -48,17 +48,19 @@ namespace OpenRA.Mods.RA.Buildings
public void AddedToWorld(Actor self) public void AddedToWorld(Actor self)
{ {
var map = self.World.Map;
if (template.PickAny) if (template.PickAny)
{ {
// Fill the footprint with random variants // Fill the footprint with random variants
foreach (var c in FootprintUtils.Tiles(self)) foreach (var c in FootprintUtils.Tiles(self))
{ {
// Only place on allowed terrain types // Only place on allowed terrain types
if (!info.TerrainTypes.Contains(self.World.GetTerrainInfo(c).Type)) if (!info.TerrainTypes.Contains(map.GetTerrainInfo(c).Type))
continue; continue;
// Don't place under other buildings or custom terrain // Don't place under other buildings or custom terrain
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != -1) if (bi.GetBuildingAt(c) != self || map.CustomTerrain[c.X, c.Y] != -1)
continue; continue;
var index = Game.CosmeticRandom.Next(template.TilesCount); var index = Game.CosmeticRandom.Next(template.TilesCount);
@@ -74,11 +76,11 @@ namespace OpenRA.Mods.RA.Buildings
var c = origin + new CVec(i % template.Size.X, i / template.Size.X); var c = origin + new CVec(i % template.Size.X, i / template.Size.X);
// Only place on allowed terrain types // Only place on allowed terrain types
if (!info.TerrainTypes.Contains(self.World.GetTerrainInfo(c).Type)) if (!info.TerrainTypes.Contains(map.GetTerrainInfo(c).Type))
continue; continue;
// Don't place under other buildings or custom terrain // Don't place under other buildings or custom terrain
if (bi.GetBuildingAt(c) != self || self.World.Map.CustomTerrain[c.X, c.Y] != -1) if (bi.GetBuildingAt(c) != self || map.CustomTerrain[c.X, c.Y] != -1)
continue; continue;
layer.AddTile(c, new TileReference<ushort, byte>(template.Id, (byte)i)); layer.AddTile(c, new TileReference<ushort, byte>(template.Id, (byte)i));

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Mods.RA.Buildings
if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(a) != null) return false; if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(a) != null) return false;
if (world.ActorMap.GetUnitsAt(a).Any(b => b != toIgnore)) return false; if (world.ActorMap.GetUnitsAt(a).Any(b => b != toIgnore)) return false;
return world.Map.IsInMap(a) && bi.TerrainTypes.Contains(world.GetTerrainInfo(a).Type); return world.Map.IsInMap(a) && bi.TerrainTypes.Contains(world.Map.GetTerrainInfo(a).Type);
} }
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore) public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.RA
if (!world.Map.IsInMap(targetTile)) if (!world.Map.IsInMap(targetTile))
return; return;
var isWater = pos.Z <= 0 && world.GetTerrainInfo(targetTile).IsWater; var isWater = pos.Z <= 0 && world.Map.GetTerrainInfo(targetTile).IsWater;
var explosionType = isWater ? warhead.WaterExplosion : warhead.Explosion; var explosionType = isWater ? warhead.WaterExplosion : warhead.Explosion;
var explosionTypePalette = isWater ? warhead.WaterExplosionPalette : warhead.ExplosionPalette; var explosionTypePalette = isWater ? warhead.WaterExplosionPalette : warhead.ExplosionPalette;
@@ -63,7 +63,7 @@ namespace OpenRA.Mods.RA
// Draw the smudges: // Draw the smudges:
foreach (var sc in smudgeCells) foreach (var sc in smudgeCells)
{ {
var smudgeType = world.GetTerrainInfo(sc).AcceptsSmudgeType.FirstOrDefault(t => warhead.SmudgeType.Contains(t)); var smudgeType = world.Map.GetTerrainInfo(sc).AcceptsSmudgeType.FirstOrDefault(t => warhead.SmudgeType.Contains(t));
if (smudgeType == null) continue; if (smudgeType == null) continue;
SmudgeLayer smudgeLayer; SmudgeLayer smudgeLayer;
@@ -84,7 +84,7 @@ namespace OpenRA.Mods.RA
} }
else else
{ {
var smudgeType = world.GetTerrainInfo(targetTile).AcceptsSmudgeType.FirstOrDefault(t => warhead.SmudgeType.Contains(t)); var smudgeType = world.Map.GetTerrainInfo(targetTile).AcceptsSmudgeType.FirstOrDefault(t => warhead.SmudgeType.Contains(t));
if (smudgeType != null) if (smudgeType != null)
{ {
SmudgeLayer smudgeLayer; SmudgeLayer smudgeLayer;

View File

@@ -93,7 +93,7 @@ namespace OpenRA.Mods.RA
{ {
if (!self.World.Map.IsInMap(cell.X, cell.Y)) return false; if (!self.World.Map.IsInMap(cell.X, cell.Y)) return false;
var type = self.World.GetTerrainInfo(cell).Type; var type = self.World.Map.GetTerrainInfo(cell).Type;
if (!info.TerrainTypes.Contains(type)) if (!info.TerrainTypes.Contains(type))
return false; return false;

View File

@@ -117,7 +117,7 @@ namespace OpenRA.Mods.RA
var p = self.World.ChooseRandomCell(self.World.SharedRandom); var p = self.World.ChooseRandomCell(self.World.SharedRandom);
// Is this valid terrain? // Is this valid terrain?
var terrainType = self.World.GetTerrainInfo(p).Type; var terrainType = self.World.Map.GetTerrainInfo(p).Type;
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType)) if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType))
continue; continue;

View File

@@ -160,9 +160,8 @@ namespace OpenRA.Mods.RA.Effects
var shouldExplode = (pos.Z < 0) // Hit the ground var shouldExplode = (pos.Z < 0) // Hit the ground
|| (dist.LengthSquared < MissileCloseEnough.Range * MissileCloseEnough.Range) // Within range || (dist.LengthSquared < MissileCloseEnough.Range * MissileCloseEnough.Range) // Within range
|| (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel || (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel
|| (!info.High && world.ActorMap.GetUnitsAt(cell) || (!info.High && world.ActorMap.GetUnitsAt(cell).Any(a => a.HasTrait<IBlocksBullets>())) // Hit a wall
.Any(a => a.HasTrait<IBlocksBullets>())) // Hit a wall || (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.Map.GetTerrainInfo(cell).Type != info.BoundToTerrainType); // Hit incompatible terrain
|| (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.GetTerrainInfo(cell).Type != info.BoundToTerrainType); // Hit incompatible terrain
if (shouldExplode) if (shouldExplode)
Explode(world); Explode(world);

View File

@@ -58,7 +58,7 @@ namespace OpenRA.Mods.RA
if (!self.World.Map.IsInMap(cell.X, cell.Y)) if (!self.World.Map.IsInMap(cell.X, cell.Y))
return false; return false;
if (!info.AllowedTerrain.Contains(self.World.GetTerrainInfo(cell).Type)) if (!info.AllowedTerrain.Contains(self.World.Map.GetTerrainInfo(cell).Type))
return false; return false;
if (!checkTransientActors) if (!checkTransientActors)

View File

@@ -104,7 +104,7 @@ namespace OpenRA.Mods.RA.Move
if (!world.Map.IsInMap(cell.X, cell.Y)) if (!world.Map.IsInMap(cell.X, cell.Y))
return int.MaxValue; return int.MaxValue;
var index = world.GetTerrainIndex(cell); var index = world.Map.GetTerrainIndex(cell);
if (index == -1) if (index == -1)
return int.MaxValue; return int.MaxValue;
@@ -481,7 +481,7 @@ namespace OpenRA.Mods.RA.Move
public int MovementSpeedForCell(Actor self, CPos cell) public int MovementSpeedForCell(Actor self, CPos cell)
{ {
var index = self.World.GetTerrainIndex(cell); var index = self.World.Map.GetTerrainIndex(cell);
if (index == -1) if (index == -1)
return 0; return 0;
@@ -574,7 +574,7 @@ namespace OpenRA.Mods.RA.Move
cursor = "move"; cursor = "move";
if (self.Owner.Shroud.IsExplored(location)) if (self.Owner.Shroud.IsExplored(location))
cursor = self.World.GetTerrainInfo(location).CustomCursor ?? cursor; cursor = self.World.Map.GetTerrainInfo(location).CustomCursor ?? cursor;
if (!self.World.Map.IsInMap(location) || (self.Owner.Shroud.IsExplored(location) && if (!self.World.Map.IsInMap(location) || (self.Owner.Shroud.IsExplored(location) &&
unitType.MovementCostForCell(self.World, location) == int.MaxValue)) unitType.MovementCostForCell(self.World, location) == int.MaxValue))

View File

@@ -45,8 +45,8 @@ namespace OpenRA.Mods.RA.Render
if (self.CenterPosition.Z > 0 || move.IsMoving) if (self.CenterPosition.Z > 0 || move.IsMoving)
return false; return false;
return cargo.CurrentAdjacentCells return cargo.CurrentAdjacentCells.Any(c => self.World.Map.IsInMap(c)
.Any(c => self.World.Map.IsInMap(c) && info.OpenTerrainTypes.Contains(self.World.GetTerrainInfo(c).Type)); && info.OpenTerrainTypes.Contains(self.World.Map.GetTerrainInfo(c).Type));
} }
void Open() void Open()

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.RA.Render
public void OnLanded() public void OnLanded()
{ {
var seq = self.World.GetTerrainInfo(self.Location).IsWater ? "water" : "land"; var seq = self.World.Map.GetTerrainInfo(self.Location).IsWater ? "water" : "land";
anim.PlayRepeating(seq); anim.PlayRepeating(seq);
} }
} }

View File

@@ -82,7 +82,7 @@ namespace OpenRA.Mods.RA
if (!self.Owner.Shroud.IsExplored(a) || !self.Owner.Shroud.IsExplored(b)) if (!self.Owner.Shroud.IsExplored(a) || !self.Owner.Shroud.IsExplored(b))
return false; return false;
if (self.World.GetTerrainIndex(a) != self.World.GetTerrainIndex(b)) if (self.World.Map.GetTerrainIndex(a) != self.World.Map.GetTerrainIndex(b))
return false; return false;
} }

View File

@@ -169,7 +169,7 @@ namespace OpenRA.Mods.RA
bool CanTraverseTile(World world, CPos p) bool CanTraverseTile(World world, CPos p)
{ {
var terrainOffset = world.GetTerrainIndex(p); var terrainOffset = world.Map.GetTerrainIndex(p);
return (movementClass & (1 << terrainOffset)) > 0; return (movementClass & (1 << terrainOffset)) > 0;
} }