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 authored and PunkPun committed Aug 26, 2023
1 parent d9787b1 commit bf64339
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
34 changes: 34 additions & 0 deletions OpenRA.Mods.Common/AIUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,39 @@ public static void BotDebug(string format, params object[] args)
if (Game.Settings.Debug.BotDebug)
TextNotificationsManager.Debug(format, args);
}

public static IEnumerable<Order> ClearBlockersOrders(IEnumerable<CPos> tiles, Player owner, Actor ignoreActor = null)
{
var world = owner.World;
var adjacentTiles = Util.ExpandFootprint(tiles, true).Except(tiles)
.Where(world.Map.Contains).ToList();

var blockers = tiles.SelectMany(world.ActorMap.GetActorsAt)
.Where(a => a.Owner == owner && a.IsIdle && (ignoreActor == null || a != ignoreActor))
.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
};
}
}
}
}
35 changes: 3 additions & 32 deletions OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected virtual IEnumerable<Order> InnerOrder(World world, CPos cell, MouseInp
if (!world.CanPlaceBuilding(topLeft, ai, bi, null)
|| !bi.IsCloseEnoughToBase(world, owner, ai, topLeft))
{
foreach (var order in ClearBlockersOrders(world, topLeft))
foreach (var order in ClearBlockersOrders(topLeft))
yield return order;

Game.Sound.PlayNotification(world.Map.Rules, owner, "Speech", notification, owner.Faction.InternalName);
Expand Down Expand Up @@ -326,38 +326,9 @@ bool IOrderGenerator.HandleKeyPress(KeyInput e)

void IOrderGenerator.Deactivate() { }

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

var blockers = allTiles.SelectMany(world.ActorMap.GetActorsAt)
.Where(a => a.Owner == Queue.Actor.Owner && a.IsIdle)
.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(variants[variant].BuildingInfo.Tiles(topLeft).ToList(), Queue.Actor.Owner);
}
}
}
9 changes: 9 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,11 @@ public bool CanDeploy()
return buildingInfo == null || self.World.CanPlaceBuilding(self.Location + Info.Offset, actorInfo, buildingInfo, self);
}

IEnumerable<Order> ClearBlockersOrders(CPos topLeft)
{
return AIUtils.ClearBlockersOrders(buildingInfo.Tiles(topLeft).ToList(), self.Owner, self);
}

public Activity GetTransformActivity()
{
return new Transform(Info.IntoActor)
Expand Down Expand Up @@ -139,6 +145,9 @@ public void DeployTransform(bool queued)
{
if (!queued && !CanDeploy())
{
foreach (var order in ClearBlockersOrders(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 bf64339

Please sign in to comment.