Added QuadRenderer to render shaded/colored quads. Added DebugOverlay to World to show A* bidi search cost per cell.
This commit is contained in:
@@ -12,6 +12,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Support;
|
||||
using OpenRA.Traits;
|
||||
|
||||
@@ -125,21 +126,43 @@ namespace OpenRA.Mods.RA.Move
|
||||
{
|
||||
using (fromSrc)
|
||||
using (fromDest)
|
||||
{
|
||||
List<CPos> path = null;
|
||||
|
||||
while (!fromSrc.queue.Empty && !fromDest.queue.Empty)
|
||||
{
|
||||
/* make some progress on the first search */
|
||||
var p = fromSrc.Expand(world);
|
||||
|
||||
if (fromDest.cellInfo[p.X, p.Y].Seen && fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity)
|
||||
return MakeBidiPath(fromSrc, fromDest, p);
|
||||
if (fromDest.cellInfo[p.X, p.Y].Seen &&
|
||||
fromDest.cellInfo[p.X, p.Y].MinCost < float.PositiveInfinity)
|
||||
{
|
||||
path = MakeBidiPath(fromSrc, fromDest, p);
|
||||
break;
|
||||
}
|
||||
|
||||
/* make some progress on the second search */
|
||||
var q = fromDest.Expand(world);
|
||||
|
||||
if (fromSrc.cellInfo[q.X, q.Y].Seen && fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity)
|
||||
return MakeBidiPath(fromSrc, fromDest, q);
|
||||
if (fromSrc.cellInfo[q.X, q.Y].Seen &&
|
||||
fromSrc.cellInfo[q.X, q.Y].MinCost < float.PositiveInfinity)
|
||||
{
|
||||
path = MakeBidiPath(fromSrc, fromDest, q);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var dbg = world.WorldActor.TraitOrDefault<DebugOverlay>();
|
||||
if (dbg != null)
|
||||
{
|
||||
dbg.AddLayer(fromSrc.considered.Select(p => new Pair<CPos, int>(p, fromSrc.cellInfo[p.X, p.Y].MinCost)), fromSrc.maxCost);
|
||||
dbg.AddLayer(fromDest.considered.Select(p => new Pair<CPos, int>(p, fromDest.cellInfo[p.X, p.Y].MinCost)), fromDest.maxCost);
|
||||
}
|
||||
|
||||
if (path != null)
|
||||
return path;
|
||||
}
|
||||
|
||||
return new List<CPos>(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,8 @@ namespace OpenRA.Mods.RA.Move
|
||||
public bool checkForBlocked;
|
||||
public Actor ignoreBuilding;
|
||||
public bool inReverse;
|
||||
|
||||
public HashSet<CPos> considered;
|
||||
public int maxCost;
|
||||
MobileInfo mobileInfo;
|
||||
Actor self;
|
||||
public Player owner { get { return self.Owner; } }
|
||||
@@ -39,6 +40,8 @@ namespace OpenRA.Mods.RA.Move
|
||||
this.self = self;
|
||||
customCost = null;
|
||||
queue = new PriorityQueue<PathDistance>();
|
||||
considered = new HashSet<CPos>();
|
||||
maxCost = 0;
|
||||
}
|
||||
|
||||
public PathSearch InReverse()
|
||||
@@ -108,14 +111,20 @@ namespace OpenRA.Mods.RA.Move
|
||||
return p.Location;
|
||||
}
|
||||
|
||||
// This current cell is ok; check all immediate directions:
|
||||
considered.Add(p.Location);
|
||||
|
||||
foreach( CVec d in directions )
|
||||
{
|
||||
CPos newHere = p.Location + d;
|
||||
|
||||
if (!world.Map.IsInMap(newHere.X, newHere.Y)) continue;
|
||||
// Is this direction flat-out unusable or already seen?
|
||||
if (!world.Map.IsInMap(newHere.X, newHere.Y))
|
||||
continue;
|
||||
if (cellInfo[newHere.X, newHere.Y].Seen)
|
||||
continue;
|
||||
|
||||
// Now we may seriously consider this direction using heuristics:
|
||||
var costHere = mobileInfo.MovementCostForCell(world, newHere);
|
||||
|
||||
if (costHere == int.MaxValue)
|
||||
@@ -134,8 +143,12 @@ namespace OpenRA.Mods.RA.Move
|
||||
int cellCost = costHere;
|
||||
if (d.X * d.Y != 0) cellCost = (cellCost * 34) / 24;
|
||||
|
||||
int userCost = 0;
|
||||
if (customCost != null)
|
||||
cellCost += customCost(newHere);
|
||||
{
|
||||
userCost = customCost(newHere);
|
||||
cellCost += userCost;
|
||||
}
|
||||
|
||||
// directional bonuses for smoother flow!
|
||||
if (LaneBias != 0)
|
||||
@@ -151,6 +164,7 @@ namespace OpenRA.Mods.RA.Move
|
||||
|
||||
int newCost = cellInfo[p.Location.X, p.Location.Y].MinCost + cellCost;
|
||||
|
||||
// Cost is even higher; next direction:
|
||||
if (newCost >= cellInfo[newHere.X, newHere.Y].MinCost)
|
||||
continue;
|
||||
|
||||
@@ -158,7 +172,11 @@ namespace OpenRA.Mods.RA.Move
|
||||
cellInfo[newHere.X, newHere.Y].MinCost = newCost;
|
||||
|
||||
queue.Add(new PathDistance(newCost + est, newHere));
|
||||
|
||||
if (newCost > maxCost) maxCost = newCost;
|
||||
considered.Add(newHere);
|
||||
}
|
||||
|
||||
return p.Location;
|
||||
}
|
||||
|
||||
|
||||
@@ -403,6 +403,7 @@
|
||||
<Compile Include="Widgets\WorldCommandWidget.cs" />
|
||||
<Compile Include="Widgets\WorldTooltipWidget.cs" />
|
||||
<Compile Include="World\ChooseBuildTabOnSelect.cs" />
|
||||
<Compile Include="World\DebugOverlay.cs" />
|
||||
<Compile Include="World\ResourceClaim.cs" />
|
||||
<Compile Include="World\ResourceClaimLayer.cs" />
|
||||
<Compile Include="World\PlayMusicOnMapLoad.cs" />
|
||||
|
||||
60
OpenRA.Mods.RA/World/DebugOverlay.cs
Normal file
60
OpenRA.Mods.RA/World/DebugOverlay.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
class DebugOverlayInfo : Traits.TraitInfo<DebugOverlay>
|
||||
{
|
||||
}
|
||||
|
||||
class DebugOverlay : IRenderOverlay, IWorldLoaded
|
||||
{
|
||||
int[,] layer;
|
||||
int refreshTick;
|
||||
World world;
|
||||
|
||||
public void WorldLoaded(World w)
|
||||
{
|
||||
this.world = w;
|
||||
this.layer = new int[w.Map.MapSize.X, w.Map.MapSize.Y];
|
||||
this.refreshTick = 0;
|
||||
}
|
||||
|
||||
public void AddLayer(IEnumerable<Pair<CPos, int>> cellWeights, int maxWeight)
|
||||
{
|
||||
if (maxWeight == 0) return;
|
||||
|
||||
foreach (var p in cellWeights)
|
||||
layer[p.First.X, p.First.Y] = layer[p.First.X, p.First.Y] / 2 + (p.Second * 128 / maxWeight);
|
||||
}
|
||||
|
||||
public void Render(WorldRenderer wr)
|
||||
{
|
||||
var qr = Game.Renderer.WorldQuadRenderer;
|
||||
bool doSwap = refreshTick - world.FrameNumber <= 0;
|
||||
if (doSwap) refreshTick = world.FrameNumber + 20;
|
||||
|
||||
for (int j = world.Map.Bounds.Top; j <= world.Map.Bounds.Bottom; ++j)
|
||||
for (int i = world.Map.Bounds.Left; i <= world.Map.Bounds.Right; ++i)
|
||||
{
|
||||
if (!world.Map.IsInMap(i, j)) continue;
|
||||
|
||||
var cell = new CPos(i, j);
|
||||
var pix = cell.ToPPos();
|
||||
|
||||
var w = Math.Max(0, Math.Min(layer[i, j], 224));
|
||||
if (doSwap)
|
||||
{
|
||||
layer[i, j] = layer[i, j] * 4 / 5;
|
||||
}
|
||||
qr.FillRect(new RectangleF(pix.X, pix.Y, Game.CellSize, Game.CellSize), Color.FromArgb(w, Color.White));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user