|
| 1 | +#region Copyright & License Information |
| 2 | +/* |
| 3 | + * Copyright 2007-2020 The OpenRA Developers (see AUTHORS) |
| 4 | + * This file is part of OpenRA, which is free software. It is made |
| 5 | + * available to you under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation, either version 3 of |
| 7 | + * the License, or (at your option) any later version. For more |
| 8 | + * information, see COPYING. |
| 9 | + */ |
| 10 | +#endregion |
| 11 | + |
| 12 | +using System; |
| 13 | +using System.Collections.Generic; |
| 14 | +using System.Linq; |
| 15 | +using OpenRA.Traits; |
| 16 | + |
| 17 | +namespace OpenRA.Mods.Common.Traits |
| 18 | +{ |
| 19 | + [Desc("Manages AI legacy bridge repair logic.")] |
| 20 | + public class BridgeRepairBotModuleInfo : ConditionalTraitInfo |
| 21 | + { |
| 22 | + [ActorReference] |
| 23 | + [Desc("Actor types that can repair bridges (via `RepairsBridges`).", |
| 24 | + "Leave this empty to disable the trait.")] |
| 25 | + public readonly HashSet<string> RepairActorTypes = new HashSet<string>(); |
| 26 | + |
| 27 | + [Desc("Avoid enemy actors nearby when searching for bridges in need of repair.", |
| 28 | + "Should be somewhere near the max weapon range.")] |
| 29 | + public readonly WDist EnemyAvoidanceRadius = WDist.FromCells(8); |
| 30 | + |
| 31 | + [Desc("Minimum delay (in ticks) between trying to scan for repair targets.")] |
| 32 | + public readonly int MinimumWaitDelay = 300; |
| 33 | + |
| 34 | + public override object Create(ActorInitializer init) { return new BridgeRepairBotModule(init.Self, this); } |
| 35 | + } |
| 36 | + |
| 37 | + public class BridgeRepairBotModule : ConditionalTrait<BridgeRepairBotModuleInfo>, IBotTick |
| 38 | + { |
| 39 | + readonly World world; |
| 40 | + readonly Player player; |
| 41 | + readonly Predicate<Actor> unitCannotBeOrderedOrIsIdle; |
| 42 | + readonly ActorIndex.OwnerAndNamesAndTrait<RepairsBridgesInfo> repairersIndex; |
| 43 | + |
| 44 | + // Units that the bot already knows about and has given a repair order. Any unit not on this list needs to be given a new order. |
| 45 | + readonly List<Actor> activeRepairers = new List<Actor>(); |
| 46 | + |
| 47 | + int waitDelayTicks; |
| 48 | + |
| 49 | + IBotRequestUnitProduction[] requestUnitProduction; |
| 50 | + |
| 51 | + public BridgeRepairBotModule(Actor self, BridgeRepairBotModuleInfo info) |
| 52 | + : base(info) |
| 53 | + { |
| 54 | + world = self.World; |
| 55 | + player = self.Owner; |
| 56 | + |
| 57 | + if (world.Type == WorldType.Editor) |
| 58 | + return; |
| 59 | + |
| 60 | + unitCannotBeOrderedOrIsIdle = a => a.Owner != player || a.IsDead || !a.IsInWorld || a.IsIdle; |
| 61 | + repairersIndex = new ActorIndex.OwnerAndNamesAndTrait<RepairsBridgesInfo>(world, info.RepairActorTypes, player); |
| 62 | + } |
| 63 | + |
| 64 | + protected override void TraitEnabled(Actor self) |
| 65 | + { |
| 66 | + // Avoid all AIs reevaluating assignments on the same tick, randomize their initial evaluation delay. |
| 67 | + waitDelayTicks = world.LocalRandom.Next(Info.MinimumWaitDelay); |
| 68 | + |
| 69 | + requestUnitProduction = player.PlayerActor.TraitsImplementing<IBotRequestUnitProduction>().ToArray(); |
| 70 | + } |
| 71 | + |
| 72 | + void IBotTick.BotTick(IBot bot) |
| 73 | + { |
| 74 | + if (--waitDelayTicks <= 0) |
| 75 | + { |
| 76 | + waitDelayTicks = Info.MinimumWaitDelay; |
| 77 | + QueueRepairOrders(bot); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + void QueueRepairOrders(IBot bot) |
| 82 | + { |
| 83 | + if (!Info.RepairActorTypes.Any() || player.WinState != WinState.Undefined) |
| 84 | + return; |
| 85 | + |
| 86 | + activeRepairers.RemoveAll(unitCannotBeOrderedOrIsIdle); |
| 87 | + |
| 88 | + var targetOptions = world.ActorsWithTrait<LegacyBridgeHut>().Where( |
| 89 | + b => b.Trait.BridgeDamageState == DamageState.Dead); |
| 90 | + |
| 91 | + if (!targetOptions.Any()) |
| 92 | + return; |
| 93 | + |
| 94 | + var newUnits = world.ActorsHavingTrait<IPositionable>() |
| 95 | + .Where(a => a.Owner == player && !activeRepairers.Contains(a)); |
| 96 | + |
| 97 | + var repairers = newUnits |
| 98 | + .Where(a => a.IsIdle && Info.RepairActorTypes.Contains(a.Info.Name) && a.Info.HasTraitInfo<RepairsBridgesInfo>()) |
| 99 | + .Select(a => new TraitPair<RepairsBridges>(a, a.TraitOrDefault<RepairsBridges>())) |
| 100 | + .Where(tp => tp.Trait != null) |
| 101 | + .ToArray(); |
| 102 | + |
| 103 | + foreach (var repairer in repairers) |
| 104 | + { |
| 105 | + var nearestTargets = targetOptions.OrderBy(target => (target.Actor.CenterPosition - repairer.Actor.CenterPosition).LengthSquared); |
| 106 | + foreach (var nearestTarget in nearestTargets) |
| 107 | + { |
| 108 | + if (activeRepairers.Contains(repairer.Actor)) |
| 109 | + continue; |
| 110 | + |
| 111 | + var safeTarget = SafePath(repairer.Actor, nearestTarget.Actor); |
| 112 | + if (safeTarget.Type == TargetType.Invalid) |
| 113 | + continue; |
| 114 | + |
| 115 | + bot.QueueOrder(new Order("RepairBridge", repairer.Actor, safeTarget, true)); |
| 116 | + AIUtils.BotDebug("AI ({0}): Ordered {1} to repair {2}", player.ClientIndex, repairer.Actor, nearestTarget.Actor); |
| 117 | + activeRepairers.Add(repairer.Actor); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Request a new repairer on demand. |
| 122 | + var unitBuilder = requestUnitProduction.FirstOrDefault(Exts.IsTraitEnabled); |
| 123 | + if (unitBuilder != null) |
| 124 | + { |
| 125 | + var engineers = AIUtils.CountActorByCommonName(repairersIndex); |
| 126 | + if (engineers == 0) |
| 127 | + unitBuilder.RequestUnitProduction(bot, Info.RepairActorTypes.Random(world.LocalRandom)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + Target SafePath(Actor repairer, Actor target) |
| 132 | + { |
| 133 | + var mobile = repairer.Trait<Mobile>(); |
| 134 | + var locomotor = mobile.Locomotor; |
| 135 | + |
| 136 | + if (!mobile.PathFinder.PathExistsForLocomotor(locomotor, repairer.Location, target.Location)) |
| 137 | + return Target.Invalid; |
| 138 | + |
| 139 | + var path = mobile.PathFinder.FindPathToTargetCellByPredicate( |
| 140 | + repairer, new[] { repairer.Location }, loc => true, BlockedByActor.Stationary, |
| 141 | + loc => world.FindActorsInCircle(world.Map.CenterOfCell(loc), Info.EnemyAvoidanceRadius) |
| 142 | + .Where(u => !u.IsDead && repairer.Owner.RelationshipWith(u.Owner) == PlayerRelationship.Enemy && repairer.IsTargetableBy(u)) |
| 143 | + .Sum(u => Math.Max(WDist.Zero.Length, Info.EnemyAvoidanceRadius.Length - (world.Map.CenterOfCell(loc) - u.CenterPosition).Length))); |
| 144 | + |
| 145 | + if (path.Count == 0) |
| 146 | + return Target.Invalid; |
| 147 | + |
| 148 | + return Target.FromActor(target); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments