local player buildings influence map, incrementally updated.

This commit is contained in:
Chris Forbes
2009-10-13 18:13:22 +13:00
parent e255a48645
commit 0f0b4230c7
6 changed files with 81 additions and 25 deletions

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Game.GameRules;
namespace OpenRa.Game
{
class BuildingInfluenceMap
{
Actor[,] influence = new Actor[128, 128];
public BuildingInfluenceMap(World world, Player player)
{
world.ActorAdded +=
a => { if (a.traits.Contains<Traits.Building>() && a.Owner == player) AddInfluence(a); };
world.ActorRemoved +=
a => { if (a.traits.Contains<Traits.Building>() && a.Owner == player) RemoveInfluence(a); };
}
void AddInfluence(Actor a)
{
foreach (var t in Footprint.Tiles(a.unitInfo.Name, a.Location))
if (IsValid(t))
influence[t.X, t.Y] = a;
}
void RemoveInfluence(Actor a)
{
foreach (var t in Footprint.Tiles(a.unitInfo.Name, a.Location))
if (IsValid(t))
influence[t.X, t.Y] = null;
}
bool IsValid(int2 t)
{
return !(t.X < 0 || t.Y < 0 || t.X >= 128 || t.Y >= 128);
}
public Actor this[int2 cell] { get { return IsValid(cell) ? influence[cell.X, cell.Y] : null; } }
}
}

View File

@@ -23,7 +23,8 @@ namespace OpenRa.Game
public readonly Dictionary<int, Player> players = new Dictionary<int, Player>();
public Player LocalPlayer { get { return players[localPlayerIndex]; } }
public Player LocalPlayer { get { return players[localPlayerIndex]; } }
public BuildingInfluenceMap LocalPlayerBuildings;
public Game(string mapName, Renderer renderer, int2 clientSize)
{
@@ -46,7 +47,9 @@ namespace OpenRa.Game
pathFinder = new PathFinder(map, terrain.tileSet);
network = new Network();
network = new Network();
LocalPlayerBuildings = new BuildingInfluenceMap(world, LocalPlayer);
controller = new Controller(this); // CAREFUL THERES AN UGLY HIDDEN DEPENDENCY HERE STILL
worldRenderer = new WorldRenderer(renderer, this);
@@ -62,11 +65,13 @@ namespace OpenRa.Game
public bool IsCellBuildable(int2 a)
{
if (LocalPlayerBuildings[a] != null) return false;
a += map.Offset;
return map.IsInMap(a.X, a.Y) &&
TerrainCosts.Cost(UnitMovementType.Wheel,
terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
TerrainCosts.Cost(UnitMovementType.Wheel,
terrain.tileSet.GetWalkability(map.MapTiles[a.X, a.Y])) < double.PositiveInfinity;
}
public IEnumerable<Actor> FindUnits(float2 a, float2 b)

View File

@@ -35,5 +35,23 @@ namespace OpenRa.Game.GameRules
buildingFootprints = buildings
.ToDictionary(a => a.Name, a => a.Pat);
}
public static IEnumerable<int2> Tiles(string name, int2 position)
{
var footprint = Rules.Footprint.GetFootprint(name);
var j = 0;
foreach (var row in footprint)
{
var i = 0;
foreach (var c in row)
{
if (c != '_')
yield return position + new int2(i, j);
++i;
}
++j;
}
}
}
}

View File

@@ -81,6 +81,7 @@
<Compile Include="Graphics\LineRenderer.cs" />
<Compile Include="Graphics\OverlayRenderer.cs" />
<Compile Include="Graphics\WorldRenderer.cs" />
<Compile Include="BuildingInfluenceMap.cs" />
<Compile Include="IOrderGenerator.cs" />
<Compile Include="TechTree\Item.cs" />
<Compile Include="Network\Packet.cs" />

View File

@@ -1,6 +1,7 @@
using System.Drawing;
using OpenRa.Game.Graphics;
using System;
using OpenRa.Game.GameRules;
namespace OpenRa.Game
{
@@ -36,22 +37,8 @@ namespace OpenRa.Game
if (!hasOverlay)
return;
var footprint = Rules.Footprint.GetFootprint(name);
var j = 0;
foreach (var row in footprint)
{
var i = 0;
foreach (var c in row)
{
if (c != '_')
spriteRenderer.DrawSprite(
game.IsCellBuildable(position + new int2(i, j))
? buildOk : buildBlocked,
24 * (position + new int2(i, j)), 0);
++i;
}
++j;
}
foreach (var t in Footprint.Tiles(name,position))
spriteRenderer.DrawSprite(game.IsCellBuildable(t) ? buildOk : buildBlocked, 24 * t, 0);
spriteRenderer.Flush();
}

View File

@@ -13,11 +13,14 @@ namespace OpenRa.Game
int lastTime = Environment.TickCount;
const int timestep = 40;
public World(Game game) { this.game = game; }
public void Add(Actor a) { actors.Add(a); }
public void Remove( Actor a ) { actors.Remove( a ); }
public void AddFrameEndTask( Action<World> a ) { frameEndActions.Add( a ); }
public World(Game game) { this.game = game; }
public void Add(Actor a) { actors.Add(a); ActorAdded(a); }
public void Remove(Actor a) { actors.Remove(a); ActorRemoved(a); }
public void AddFrameEndTask( Action<World> a ) { frameEndActions.Add( a ); }
public event Action<Actor> ActorAdded = _ => { };
public event Action<Actor> ActorRemoved = _ => { };
public void Update()
{