Files
OpenRA/OpenRA.Mods.Common/Traits/World/PathfinderDebugOverlay.cs
David Jiménez 54ae572303 - Introduced Unit Testing capabilities to the PathFinder trait and algorithm.
Introduced also a small Unit test project to prove it.

- Separated caching capabilities from PathFinder class to increase cohesion and maintainability.
Refactored the pathfinding algorithm by extracting methods based on responsibilities like
calculating costs and reordering functions. These changes should provide a in average a small increase in
pathfinding performance and maintainability.

- Optimized the pathfinder algorithm to reuse calculations like the
MovementCost and heuristics.

- Introduced base classes, IPathSearch and IPriorityQueue interfaces,
and restructured code to ease readability and testability

- Renamed the PathFinder related classes to more appropriate names. Made the
traits rely on the interface IPathfinder instead of concrete PathFinder
implementation.

- Massive performance improvements

- Solved error with harvesters' Heuristic

- Updated the heuristic to ease redability and adjustability. D can be
adjusted to offer best paths by decreasing and more performance by
increasing it

- Refactored the CellLayer<CellInfo> creation in its own Singleton class

- Extracted the graph abstraction onto an IGraph interface, making the
Pathfinder agnostic to the definition of world and terrain. This
abstraction can help in the future to be able to cache graphs for similar
classes and their costs, speeding up the pathfinder and being able to feed
the A* algorithm with different types of graphs like Hierarchical graphs
2015-03-03 20:11:11 +01:00

88 lines
2.4 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Required for the A* PathDebug from DeveloperMode. Attach this to the world actor.")]
public class PathfinderDebugOverlayInfo : TraitInfo<PathfinderDebugOverlay> { }
public class PathfinderDebugOverlay : IRenderOverlay, IWorldLoaded
{
Dictionary<Player, CellLayer<int>> layers;
int refreshTick;
World world;
public bool Visible;
public void WorldLoaded(World w, WorldRenderer wr)
{
world = w;
refreshTick = 0;
layers = new Dictionary<Player, CellLayer<int>>(8);
// Enabled via Cheats menu
Visible = false;
}
public void AddLayer(IEnumerable<Pair<CPos, int>> cellWeights, int maxWeight, Player pl)
{
if (maxWeight == 0) return;
CellLayer<int> layer;
if (!layers.TryGetValue(pl, out layer))
{
layer = new CellLayer<int>(world.Map);
layers.Add(pl, layer);
}
foreach (var p in cellWeights)
layer[p.First] = Math.Min(128, layer[p.First] + (maxWeight - p.Second) * 64 / maxWeight);
}
public void Render(WorldRenderer wr)
{
if (!Visible)
return;
var qr = Game.Renderer.WorldQuadRenderer;
var doDim = refreshTick - world.WorldTick <= 0;
if (doDim) refreshTick = world.WorldTick + 20;
foreach (var pair in layers)
{
var c = (pair.Key != null) ? pair.Key.Color.RGB : Color.PaleTurquoise;
var layer = pair.Value;
// Only render quads in viewing range:
foreach (var cell in wr.Viewport.VisibleCells)
{
if (layer[cell] <= 0)
continue;
var w = Math.Max(0, Math.Min(layer[cell], 128));
if (doDim)
layer[cell] = layer[cell] * 5 / 6;
// TODO: This doesn't make sense for isometric terrain
var pos = wr.World.Map.CenterOfCell(cell);
var tl = wr.ScreenPxPosition(pos - new WVec(512, 512, 0));
var br = wr.ScreenPxPosition(pos + new WVec(511, 511, 0));
qr.FillRect(RectangleF.FromLTRB(tl.X, tl.Y, br.X, br.Y), Color.FromArgb(w, c));
}
}
}
}
}