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 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)
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#endregion
|
||||
|
||||
using System.Drawing;
|
||||
using OpenRA;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
@@ -17,62 +18,103 @@ 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
|
||||
{
|
||||
var td = new TypeDictionary
|
||||
{
|
||||
new OwnerInit(self.Owner),
|
||||
new LocationInit(location),
|
||||
new CenterPositionInit(pos),
|
||||
new FacingInit(initialFacing)
|
||||
};
|
||||
new OwnerInit(self.Owner),
|
||||
new LocationInit(location.Value),
|
||||
new CenterPositionInit(pos),
|
||||
new FacingInit(initialFacing)
|
||||
};
|
||||
|
||||
if (factionVariant != null)
|
||||
td.Add(new FactionInit(factionVariant));
|
||||
if (factionVariant != null)
|
||||
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>();
|
||||
if (move != null)
|
||||
newUnit.QueueActivity(move.MoveIntoWorld(newUnit, self.Location));
|
||||
var move = newUnit.TraitOrDefault<IMove>();
|
||||
if (move != null)
|
||||
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);
|
||||
if (!self.IsDead)
|
||||
foreach (var t in self.TraitsImplementing<INotifyProduction>())
|
||||
t.UnitProduced(self, newUnit, destination);
|
||||
|
||||
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
|
||||
foreach (var notify in notifyOthers)
|
||||
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);
|
||||
var notifyOthers = self.World.ActorsWithTrait<INotifyOtherProduction>();
|
||||
foreach (var notify in notifyOthers)
|
||||
notify.Trait.UnitProducedByOther(notify.Actor, self, newUnit);
|
||||
|
||||
foreach (var t in newUnit.TraitsImplementing<INotifyBuildComplete>())
|
||||
t.BuildingComplete(newUnit);
|
||||
});
|
||||
foreach (var t in newUnit.TraitsImplementing<INotifyBuildComplete>())
|
||||
t.BuildingComplete(newUnit);
|
||||
});
|
||||
|
||||
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