Skip to content

Commit

Permalink
Automatically move blockers when transform deploying.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mailaender committed Aug 25, 2023
1 parent df53473 commit 45b914a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
25 changes: 25 additions & 0 deletions OpenRA.Mods.Common/AIUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,30 @@ public static void BotDebug(string format, params object[] args)
if (Game.Settings.Debug.BotDebug)
TextNotificationsManager.Debug(format, args);
}

public static IEnumerable<Order> ClearBlockersOrders(World world, IEnumerable<TraitPair<IMove>> blockers, List<CPos> adjacentTiles)
{
foreach (var blocker in blockers)
{
CPos moveCell;
if (blocker.Trait is Mobile mobile)
{
var availableCells = adjacentTiles.Where(t => mobile.CanEnterCell(t)).ToList();
if (availableCells.Count == 0)
continue;

moveCell = blocker.Actor.ClosestCell(availableCells);
}
else if (blocker.Trait is Aircraft)
moveCell = blocker.Actor.Location;
else
continue;

yield return new Order("Move", blocker.Actor, Target.FromCell(world, moveCell), false)
{
SuppressVisualFeedback = true
};
}
}
}
}
22 changes: 1 addition & 21 deletions OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,27 +337,7 @@ IEnumerable<Order> ClearBlockersOrders(World world, CPos topLeft)
.Select(a => new TraitPair<IMove>(a, a.TraitOrDefault<IMove>()))
.Where(x => x.Trait != null);

foreach (var blocker in blockers)
{
CPos moveCell;
if (blocker.Trait is Mobile mobile)
{
var availableCells = adjacentTiles.Where(t => mobile.CanEnterCell(t)).ToList();
if (availableCells.Count == 0)
continue;

moveCell = blocker.Actor.ClosestCell(availableCells);
}
else if (blocker.Trait is Aircraft)
moveCell = blocker.Actor.Location;
else
continue;

yield return new Order("Move", blocker.Actor, Target.FromCell(world, moveCell), false)
{
SuppressVisualFeedback = true
};
}
return AIUtils.ClearBlockersOrders(world, blockers, adjacentTiles);
}
}
}
18 changes: 18 additions & 0 deletions OpenRA.Mods.Common/Traits/Transforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
Expand Down Expand Up @@ -97,6 +98,20 @@ public bool CanDeploy()
return buildingInfo == null || self.World.CanPlaceBuilding(self.Location + Info.Offset, actorInfo, buildingInfo, self);
}

IEnumerable<Order> ClearBlockersOrders(World world, CPos topLeft)
{
var allTiles = buildingInfo.Tiles(topLeft).ToList();
var adjacentTiles = Util.ExpandFootprint(allTiles, true).Except(allTiles)
.Where(world.Map.Contains).ToList();

var blockers = allTiles.SelectMany(world.ActorMap.GetActorsAt)
.Where(a => a.Owner == self.Owner && a != self && a.IsIdle)
.Select(a => new TraitPair<IMove>(a, a.TraitOrDefault<IMove>()))
.Where(x => x.Trait != null);

return AIUtils.ClearBlockersOrders(world, blockers, adjacentTiles);
}

public Activity GetTransformActivity()
{
return new Transform(Info.IntoActor)
Expand Down Expand Up @@ -139,6 +154,9 @@ public void DeployTransform(bool queued)
{
if (!queued && !CanDeploy())
{
foreach (var order in ClearBlockersOrders(self.World, self.Location + Info.Offset))
self.World.IssueOrder(order);

// Only play the "Cannot deploy here" audio
// for non-queued orders
foreach (var s in Info.NoTransformSounds)
Expand Down

0 comments on commit 45b914a

Please sign in to comment.