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:
@@ -176,6 +176,7 @@ namespace OpenRA
|
|||||||
|
|
||||||
[FieldLoader.Ignore] public ProjectedCellRegion ProjectedCellBounds;
|
[FieldLoader.Ignore] public ProjectedCellRegion ProjectedCellBounds;
|
||||||
[FieldLoader.Ignore] public CellRegion AllCells;
|
[FieldLoader.Ignore] public CellRegion AllCells;
|
||||||
|
public List<CPos> AllEdgeCells { get; private set; }
|
||||||
|
|
||||||
void AssertExists(string filename)
|
void AssertExists(string filename)
|
||||||
{
|
{
|
||||||
@@ -320,6 +321,8 @@ namespace OpenRA
|
|||||||
CustomTerrain = new CellLayer<byte>(this);
|
CustomTerrain = new CellLayer<byte>(this);
|
||||||
foreach (var uv in AllCells.MapCoords)
|
foreach (var uv in AllCells.MapCoords)
|
||||||
CustomTerrain[uv] = byte.MaxValue;
|
CustomTerrain[uv] = byte.MaxValue;
|
||||||
|
|
||||||
|
AllEdgeCells = UpdateEdgeCells();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializeCellProjection()
|
void InitializeCellProjection()
|
||||||
@@ -1070,20 +1073,44 @@ namespace OpenRA
|
|||||||
return edge.V == Bounds.Bottom ? unProjected.MaxBy(x => x.V) : unProjected.MinBy(x => x.V);
|
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)
|
public CPos ChooseRandomEdgeCell(MersenneTwister rand)
|
||||||
{
|
{
|
||||||
List<MPos> cells;
|
return AllEdgeCells.Random(rand);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public WDist DistanceToEdge(WPos pos, WVec dir)
|
public WDist DistanceToEdge(WPos pos, WVec dir)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using OpenRA;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
using OpenRA.Primitives;
|
using OpenRA.Primitives;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
@@ -17,62 +18,103 @@ using OpenRA.Traits;
|
|||||||
namespace OpenRA.Mods.D2k.Traits
|
namespace OpenRA.Mods.D2k.Traits
|
||||||
{
|
{
|
||||||
[Desc("Produce a unit on the closest map edge cell and move into the world.")]
|
[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); }
|
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)
|
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)
|
public override bool Produce(Actor self, ActorInfo producee, string factionVariant)
|
||||||
{
|
{
|
||||||
var location = self.World.Map.ChooseClosestEdgeCell(self.Location);
|
var aircraftInfo = producee.TraitInfoOrDefault<AircraftInfo>();
|
||||||
var pos = self.World.Map.CenterOfCell(location);
|
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
|
// If aircraft, spawn at cruise altitude
|
||||||
var aircraftInfo = producee.TraitInfoOrDefault<AircraftInfo>();
|
|
||||||
if (aircraftInfo != null)
|
if (aircraftInfo != null)
|
||||||
pos += new WVec(0, 0, aircraftInfo.CruiseAltitude.Length);
|
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 =>
|
self.World.AddFrameEndTask(w =>
|
||||||
|
{
|
||||||
|
var td = new TypeDictionary
|
||||||
{
|
{
|
||||||
var td = new TypeDictionary
|
new OwnerInit(self.Owner),
|
||||||
{
|
new LocationInit(location.Value),
|
||||||
new OwnerInit(self.Owner),
|
new CenterPositionInit(pos),
|
||||||
new LocationInit(location),
|
new FacingInit(initialFacing)
|
||||||
new CenterPositionInit(pos),
|
};
|
||||||
new FacingInit(initialFacing)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (factionVariant != null)
|
if (factionVariant != null)
|
||||||
td.Add(new FactionInit(factionVariant));
|
td.Add(new FactionInit(factionVariant));
|
||||||
|
|
||||||
var newUnit = self.World.CreateActor(producee.Name, td);
|
var newUnit = self.World.CreateActor(producee.Name, td);
|
||||||
|
|
||||||
var move = newUnit.TraitOrDefault<IMove>();
|
var move = newUnit.TraitOrDefault<IMove>();
|
||||||
if (move != null)
|
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)
|
if (!self.IsDead)
|
||||||
foreach (var t in self.TraitsImplementing<INotifyProduction>())
|
foreach (var t in self.TraitsImplementing<INotifyProduction>())
|
||||||
t.UnitProduced(self, newUnit, self.Location);
|
t.UnitProduced(self, newUnit, destination);
|
||||||
|
|
||||||
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
|
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
|
||||||
foreach (var notify in notifyOthers)
|
foreach (var notify in notifyOthers)
|
||||||
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);
|
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);
|
||||||
|
|
||||||
foreach (var t in newUnit.TraitsImplementing<INotifyBuildComplete>())
|
foreach (var t in newUnit.TraitsImplementing<INotifyBuildComplete>())
|
||||||
t.BuildingComplete(newUnit);
|
t.BuildingComplete(newUnit);
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user