Merge pull request #10851 from obrakmann/prod-from-map-edge_ground-units-fixes

Fix ProductionFromMapEdge to work properly with ground units
This commit is contained in:
abcdefg30
2016-03-12 20:37:02 +01:00
2 changed files with 111 additions and 42 deletions

View File

@@ -176,6 +176,7 @@ namespace OpenRA
[FieldLoader.Ignore] public ProjectedCellRegion ProjectedCellBounds;
[FieldLoader.Ignore] public CellRegion AllCells;
public List<CPos> AllEdgeCells { get; private set; }
void AssertExists(string filename)
{
@@ -320,6 +321,8 @@ namespace OpenRA
CustomTerrain = new CellLayer<byte>(this);
foreach (var uv in AllCells.MapCoords)
CustomTerrain[uv] = byte.MaxValue;
AllEdgeCells = UpdateEdgeCells();
}
void InitializeCellProjection()
@@ -1070,20 +1073,44 @@ namespace OpenRA
return edge.V == Bounds.Bottom ? unProjected.MaxBy(x => x.V) : unProjected.MinBy(x => x.V);
}
public CPos ChooseClosestMatchingEdgeCell(CPos cell, Func<CPos, bool> match)
{
return AllEdgeCells.OrderBy(c => (cell - c).Length).FirstOrDefault(c => match(c));
}
List<CPos> UpdateEdgeCells()
{
var edgeCells = new List<CPos>();
var unProjected = new List<MPos>();
var bottom = Bounds.Bottom - 1;
for (var u = Bounds.Left; u < Bounds.Right; u++)
{
unProjected = Unproject(new PPos(u, Bounds.Top));
if (unProjected.Any())
edgeCells.Add(unProjected.MinBy(x => x.V).ToCPos(Grid.Type));
unProjected = Unproject(new PPos(u, bottom));
if (unProjected.Any())
edgeCells.Add(unProjected.MaxBy(x => x.V).ToCPos(Grid.Type));
}
for (var v = Bounds.Top; v < Bounds.Bottom; v++)
{
unProjected = Unproject(new PPos(Bounds.Left, v));
if (unProjected.Any())
edgeCells.Add((v == bottom ? unProjected.MaxBy(x => x.V) : unProjected.MinBy(x => x.V)).ToCPos(Grid.Type));
unProjected = Unproject(new PPos(Bounds.Right - 1, v));
if (unProjected.Any())
edgeCells.Add((v == bottom ? unProjected.MaxBy(x => x.V) : unProjected.MinBy(x => x.V)).ToCPos(Grid.Type));
}
return edgeCells;
}
public CPos ChooseRandomEdgeCell(MersenneTwister rand)
{
List<MPos> cells;
do
{
var isU = rand.Next(2) == 0;
var edge = rand.Next(2) == 0;
var u = isU ? rand.Next(Bounds.Left, Bounds.Right) : (edge ? Bounds.Left : Bounds.Right);
var v = !isU ? rand.Next(Bounds.Top, Bounds.Bottom) : (edge ? Bounds.Top : Bounds.Bottom);
cells = Unproject(new PPos(u, v));
} while (!cells.Any());
return cells.Random(rand).ToCPos(Grid.Type);
return AllEdgeCells.Random(rand);
}
public WDist DistanceToEdge(WPos pos, WVec dir)

View File

@@ -10,6 +10,7 @@
#endregion
using System.Drawing;
using OpenRA;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
@@ -17,34 +18,67 @@ using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Produce a unit on the closest map edge cell and move into the world.")]
class ProductionFromMapEdgeInfo : ProductionInfo
class ProductionFromMapEdgeInfo : ProductionInfo, UsesInit<ProductionSpawnLocationInit>
{
public override object Create(ActorInitializer init) { return new ProductionFromMapEdge(init, this); }
}
class ProductionFromMapEdge : Production
class ProductionFromMapEdge : Production, INotifyCreated
{
readonly CPos? spawnLocation;
readonly DomainIndex domainIndex;
RallyPoint rp;
public ProductionFromMapEdge(ActorInitializer init, ProductionInfo info)
: base(init, info) { }
: base(init, info)
{
domainIndex = init.Self.World.WorldActor.Trait<DomainIndex>();
if (init.Contains<ProductionSpawnLocationInit>())
spawnLocation = init.Get<ProductionSpawnLocationInit, CPos>();
}
void INotifyCreated.Created(Actor self)
{
rp = self.TraitOrDefault<RallyPoint>();
}
public override bool Produce(Actor self, ActorInfo producee, string factionVariant)
{
var location = self.World.Map.ChooseClosestEdgeCell(self.Location);
var pos = self.World.Map.CenterOfCell(location);
var aircraftInfo = producee.TraitInfoOrDefault<AircraftInfo>();
var mobileInfo = producee.TraitInfoOrDefault<MobileInfo>();
var passable = mobileInfo != null ? (uint)mobileInfo.GetMovementClass(self.World.TileSet) : 0;
var destination = rp != null ? rp.Location : self.Location;
var location = spawnLocation;
if (!location.HasValue)
{
if (aircraftInfo != null)
location = self.World.Map.ChooseClosestEdgeCell(self.Location);
if (mobileInfo != null)
location = self.World.Map.ChooseClosestMatchingEdgeCell(self.Location,
c => mobileInfo.CanEnterCell(self.World, null, c) && domainIndex.IsPassable(c, destination, passable));
}
// No suitable spawn location could be found, so production has failed.
if (!location.HasValue)
return false;
var pos = self.World.Map.CenterOfCell(location.Value);
// If aircraft, spawn at cruise altitude
var aircraftInfo = producee.TraitInfoOrDefault<AircraftInfo>();
if (aircraftInfo != null)
pos += new WVec(0, 0, aircraftInfo.CruiseAltitude.Length);
var initialFacing = self.World.Map.FacingBetween(location, self.Location, 0);
var initialFacing = self.World.Map.FacingBetween(location.Value, destination, 0);
self.World.AddFrameEndTask(w =>
{
var td = new TypeDictionary
{
new OwnerInit(self.Owner),
new LocationInit(location),
new LocationInit(location.Value),
new CenterPositionInit(pos),
new FacingInit(initialFacing)
};
@@ -56,13 +90,13 @@ namespace OpenRA.Mods.D2k.Traits
var move = newUnit.TraitOrDefault<IMove>();
if (move != null)
newUnit.QueueActivity(move.MoveIntoWorld(newUnit, self.Location));
newUnit.QueueActivity(move.MoveTo(destination, 2));
newUnit.SetTargetLine(Target.FromCell(self.World, self.Location), Color.Green, false);
newUnit.SetTargetLine(Target.FromCell(self.World, destination), Color.Green, false);
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
t.UnitProduced(self, newUnit, self.Location);
t.UnitProduced(self, newUnit, destination);
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
foreach (var notify in notifyOthers)
@@ -75,4 +109,12 @@ namespace OpenRA.Mods.D2k.Traits
return true;
}
}
public class ProductionSpawnLocationInit : IActorInit<CPos>
{
[FieldFromYamlKey] readonly CPos value = CPos.Zero;
public ProductionSpawnLocationInit() { }
public ProductionSpawnLocationInit(CPos init) { value = init; }
public CPos Value(World world) { return value; }
}
}