Skip to content

Commit d988188

Browse files
committed
fix bots getting stuck in ra
1 parent a1f1b58 commit d988188

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

OpenRA.Mods.RA/HackyAI.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ public void Tick(Actor self)
256256
//Units that the ai already knows about. Any unit not on this list needs to be given a role.
257257
List<Actor> activeUnits = new List<Actor>();
258258

259-
//This is purely to identify production buildings that don't have a rally point set.
260-
List<Actor> activeProductionBuildings = new List<Actor>();
261-
262259
bool IsHumanPlayer(Player p) { return !p.IsBot && !p.NonCombatant; }
263260

264261
bool HasHumanPlayers()
@@ -288,7 +285,6 @@ void AssignRolesToIdleUnits(Actor self)
288285
activeUnits.RemoveAll(a => a.Destroyed);
289286
unitsHangingAroundTheBase.RemoveAll(a => a.Destroyed);
290287
attackForce.RemoveAll(a => a.Destroyed);
291-
activeProductionBuildings.RemoveAll(a => a.Destroyed);
292288

293289
// don't select harvesters.
294290
var newUnits = self.World.Queries.OwnedBy[p]
@@ -318,34 +314,41 @@ void AssignRolesToIdleUnits(Actor self)
318314

319315
unitsHangingAroundTheBase.Clear();
320316
}
317+
}
318+
319+
bool IsRallyPointValid(int2 x)
320+
{
321+
return world.IsCellBuildable(x, false);
321322
}
322323

323324
void SetRallyPointsForNewProductionBuildings(Actor self)
324-
{
325-
var newProdBuildings = self.World.Queries.OwnedBy[p]
326-
.Where(a => (a.TraitOrDefault<RallyPoint>() != null
327-
&& !activeProductionBuildings.Contains(a)
328-
)).ToArray();
325+
{
326+
var buildings = self.World.Queries.OwnedBy[p].WithTrait<RallyPoint>()
327+
.Where(rp => !IsRallyPointValid(rp.Trait.rallyPoint)).ToArray();
328+
329+
if (buildings.Length > 0)
330+
BotDebug("Bot {0} needs to find rallypoints for {1} buildings.",
331+
p.PlayerName, buildings.Length);
332+
329333

330-
foreach (var a in newProdBuildings)
334+
foreach (var a in buildings)
331335
{
332-
activeProductionBuildings.Add(a);
333-
int2 newRallyPoint = ChooseRallyLocationNear(a.Location);
334-
newRallyPoint.X += 4;
335-
newRallyPoint.Y += 4;
336-
world.IssueOrder(new Order("SetRallyPoint", a, false) { TargetLocation = newRallyPoint });
336+
int2 newRallyPoint = ChooseRallyLocationNear(a.Actor.Location);
337+
world.IssueOrder(new Order("SetRallyPoint", a.Actor, false) { TargetLocation = newRallyPoint });
337338
}
338339
}
339340

340341
//won't work for shipyards...
341342
int2 ChooseRallyLocationNear(int2 startPos)
342-
{
343-
Random r = new Random();
344-
foreach (var t in world.FindTilesInCircle(startPos, 8))
345-
if (world.IsCellBuildable(t, false) && t != startPos && r.Next(64) == 0)
346-
return t;
347-
348-
return startPos; // i don't know where to put it.
343+
{
344+
var possibleRallyPoints = world.FindTilesInCircle(startPos, 8).Where(x => world.IsCellBuildable(x, false)).ToArray();
345+
if (possibleRallyPoints.Length == 0)
346+
{
347+
Game.Debug("Bot Bug: No possible rallypoint near {0}", startPos);
348+
return startPos;
349+
}
350+
351+
return possibleRallyPoints.Random(random);
349352
}
350353

351354
int2? ChooseDestinationNear(Actor a, int2 desiredMoveTarget)

mods/ra/rules/system.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ Player:
6767
BuildingFractions:
6868
proc: 30%
6969
tent: 1%
70+
barr: 1%
7071
weap: 1%
7172
pbox: 5%
7273
gun: 15%
7374
ftur: 10%
74-
tsla: 15%
75+
tsla: 10%
76+
fix: 0.1%
7577
dome: 1%
7678
agun: 5%
7779
sam: 1%
7880
atek: 1%
7981
stek: 1%
80-
fix: 0.1%
8182
UnitsToBuild:
8283
1tnk: 0%
8384
2tnk: 0%

0 commit comments

Comments
 (0)