moved helper functions out of Game, into WorldUtils

This commit is contained in:
Bob
2010-01-18 02:46:22 +13:00
parent 6f0c2c5a5d
commit ab1abee843
31 changed files with 176 additions and 163 deletions

View File

@@ -208,130 +208,9 @@ namespace OpenRa
renderer.PaletteTexture = palette.Texture;
}
public static bool IsCellBuildable(int2 a, UnitMovementType umt)
{
return IsCellBuildable(a, umt, null);
}
public static bool IsCellBuildable(int2 a, UnitMovementType umt, Actor toIgnore)
{
if (Game.world.BuildingInfluence.GetBuildingAt(a) != null) return false;
if (Game.world.UnitInfluence.GetUnitsAt(a).Any(b => b != toIgnore)) return false;
return Game.world.Map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(umt,
Game.world.TileSet.GetWalkability(Game.world.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
}
public static bool IsActorCrushableByActor(Actor a, Actor b)
{
return IsActorCrushableByMovementType(a, b.traits.GetOrDefault<IMovement>().GetMovementType());
}
public static bool IsActorPathableToCrush(Actor a, UnitMovementType umt)
{
return a != null &&
a.traits.WithInterface<ICrushable>()
.Any(c => c.IsPathableCrush(umt, a.Owner));
}
public static bool IsActorCrushableByMovementType(Actor a, UnitMovementType umt)
{
return a != null &&
a.traits.WithInterface<ICrushable>()
.Any(c => c.IsCrushableBy(umt, a.Owner));
}
public static bool IsWater(int2 a)
{
return Game.world.Map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(UnitMovementType.Float,
Game.world.TileSet.GetWalkability(Game.world.Map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
}
public static IEnumerable<Actor> FindUnits(float2 a, float2 b)
{
var min = float2.Min(a, b);
var max = float2.Max(a, b);
var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
return world.Actors
.Where(x => x.GetBounds(true).IntersectsWith(rect));
}
public static IEnumerable<Actor> FindUnitsInCircle(float2 a, float r)
{
var min = a - new float2(r, r);
var max = a + new float2(r, r);
var rect = new RectangleF(min.X, min.Y, max.X - min.X, max.Y - min.Y);
var inBox = world.Actors.Where(x => x.GetBounds(false).IntersectsWith(rect));
return inBox.Where(x => (x.CenterLocation - a).LengthSquared < r * r);
}
public static IEnumerable<int2> FindTilesInCircle(int2 a, int r)
{
var min = a - new int2(r, r);
var max = a + new int2(r, r);
if (min.X < 0) min.X = 0;
if (min.Y < 0) min.Y = 0;
if (max.X > 127) max.X = 127;
if (max.Y > 127) max.Y = 127;
for (var j = min.Y; j <= max.Y; j++)
for (var i = min.X; i <= max.X; i++)
if (r * r >= (new int2(i, j) - a).LengthSquared)
yield return new int2(i, j);
}
public static IEnumerable<Actor> SelectActorsInBox(float2 a, float2 b)
{
return FindUnits(a, b)
.Where( x => x.traits.Contains<Selectable>() )
.GroupBy(x => (x.Owner == LocalPlayer) ? x.Info.Traits.Get<SelectableInfo>().Priority : 0)
.OrderByDescending(g => g.Key)
.Select( g => g.AsEnumerable() )
.DefaultIfEmpty( new Actor[] {} )
.FirstOrDefault();
}
public static Random SharedRandom = new Random(0); /* for things that require sync */
public static Random CosmeticRandom = new Random(); /* for things that are just fluff */
public static bool CanPlaceBuilding(string name, BuildingInfo building, int2 xy, Actor toIgnore, bool adjust)
{
return !Footprint.Tiles(name, building, xy, adjust).Any(
t => !Game.world.Map.IsInMap(t.X, t.Y) || Game.world.Map.ContainsResource(t) || !Game.IsCellBuildable(t,
building.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel,
toIgnore));
}
public static bool IsCloseEnoughToBase(Player p, string buildingName, BuildingInfo bi, int2 position)
{
var maxDistance = bi.Adjacent + 1;
var search = new PathSearch()
{
heuristic = loc =>
{
var b = Game.world.BuildingInfluence.GetBuildingAt(loc);
if (b != null && b.Owner == p && b.Info.Traits.Get<BuildingInfo>().BaseNormal) return 0;
if ((loc - position).Length > maxDistance)
return float.PositiveInfinity; /* not quite right */
return 1;
},
checkForBlocked = false,
ignoreTerrain = true,
};
foreach (var t in Footprint.Tiles(buildingName, bi, position)) search.AddInitialCell(t);
return Game.world.PathFinder.FindPath(search).Count != 0;
}
public static void SyncLobbyInfo(string data)
{
var session = new Session();