From 38caadfdf0fa0fefbca0d31ff086b4705813c695 Mon Sep 17 00:00:00 2001 From: tovl Date: Thu, 19 Sep 2019 00:09:46 +0200 Subject: [PATCH] Clean up nudging code. --- OpenRA.Mods.Cnc/Traits/MadTank.cs | 2 +- OpenRA.Mods.Common/Activities/Move/Move.cs | 34 +--------- .../Scripting/Properties/MobileProperties.cs | 2 +- OpenRA.Mods.Common/Traits/Crushable.cs | 2 +- OpenRA.Mods.Common/Traits/EjectOnDeath.cs | 2 +- .../Traits/Infantry/ScaredyCat.cs | 2 +- OpenRA.Mods.Common/Traits/Mobile.cs | 64 ++++++++----------- 7 files changed, 31 insertions(+), 77 deletions(-) diff --git a/OpenRA.Mods.Cnc/Traits/MadTank.cs b/OpenRA.Mods.Cnc/Traits/MadTank.cs index 643ff7869c..56316ce32a 100644 --- a/OpenRA.Mods.Cnc/Traits/MadTank.cs +++ b/OpenRA.Mods.Cnc/Traits/MadTank.cs @@ -262,7 +262,7 @@ namespace OpenRA.Mods.Cnc.Traits }); var driverMobile = driver.TraitOrDefault(); if (driverMobile != null) - driverMobile.Nudge(driver, driver, true); + driverMobile.Nudge(driver); } } } diff --git a/OpenRA.Mods.Common/Activities/Move/Move.cs b/OpenRA.Mods.Common/Activities/Move/Move.cs index 8608128463..4e37f57b44 100644 --- a/OpenRA.Mods.Common/Activities/Move/Move.cs +++ b/OpenRA.Mods.Common/Activities/Move/Move.cs @@ -273,7 +273,7 @@ namespace OpenRA.Mods.Common.Activities else if (mobile.IsBlocking) { // If there is no way around the blocker and blocker will not move and we are blocking others, back up to let others pass. - var newCell = GetAdjacentCell(self, nextCell); + var newCell = mobile.GetAdjacentCell(nextCell); if (newCell != null) { if ((nextCell - newCell).Value.LengthSquared > 2) @@ -309,38 +309,6 @@ namespace OpenRA.Mods.Common.Activities return true; } - CPos? GetAdjacentCell(Actor self, CPos nextCell) - { - var availCells = new List(); - var notStupidCells = new List(); - for (var i = -1; i <= 1; i++) - { - for (var j = -1; j <= 1; j++) - { - var p = mobile.ToCell + new CVec(i, j); - if (mobile.CanEnterCell(p)) - availCells.Add(p); - else if (p != nextCell && p != mobile.ToCell) - notStupidCells.Add(p); - } - } - - CPos? newCell = null; - if (availCells.Count > 0) - newCell = availCells.Random(self.World.SharedRandom); - else - { - var cellInfo = notStupidCells - .SelectMany(c => self.World.ActorMap.GetActorsAt(c).Where(Mobile.Ismovable), - (c, a) => new { Cell = c, Actor = a }) - .RandomOrDefault(self.World.SharedRandom); - if (cellInfo != null) - newCell = cellInfo.Cell; - } - - return newCell; - } - public override void Cancel(Actor self, bool keepQueue = false) { if (path != null) diff --git a/OpenRA.Mods.Common/Scripting/Properties/MobileProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/MobileProperties.cs index a6446562c0..2f846b6c34 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/MobileProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/MobileProperties.cs @@ -56,7 +56,7 @@ namespace OpenRA.Mods.Common.Scripting [Desc("Leave the current position in a random direction.")] public void Scatter() { - mobile.Nudge(Self, Self, true); + mobile.Nudge(Self); } [ScriptActorPropertyActivity] diff --git a/OpenRA.Mods.Common/Traits/Crushable.cs b/OpenRA.Mods.Common/Traits/Crushable.cs index 0ef095a91b..b0457764ca 100644 --- a/OpenRA.Mods.Common/Traits/Crushable.cs +++ b/OpenRA.Mods.Common/Traits/Crushable.cs @@ -46,7 +46,7 @@ namespace OpenRA.Mods.Common.Traits var mobile = self.TraitOrDefault(); if (mobile != null && self.World.SharedRandom.Next(100) <= Info.WarnProbability) - mobile.Nudge(self, crusher, true); + mobile.Nudge(crusher); } void INotifyCrushed.OnCrush(Actor self, Actor crusher, BitSet crushClasses) diff --git a/OpenRA.Mods.Common/Traits/EjectOnDeath.cs b/OpenRA.Mods.Common/Traits/EjectOnDeath.cs index 5752cf3ec0..1ca8286e02 100644 --- a/OpenRA.Mods.Common/Traits/EjectOnDeath.cs +++ b/OpenRA.Mods.Common/Traits/EjectOnDeath.cs @@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits var pilotMobile = pilot.TraitOrDefault(); if (pilotMobile != null) - pilotMobile.Nudge(pilot, pilot, true); + pilotMobile.Nudge(pilot); }); } } diff --git a/OpenRA.Mods.Common/Traits/Infantry/ScaredyCat.cs b/OpenRA.Mods.Common/Traits/Infantry/ScaredyCat.cs index 75357007d6..b871d1c3af 100644 --- a/OpenRA.Mods.Common/Traits/Infantry/ScaredyCat.cs +++ b/OpenRA.Mods.Common/Traits/Infantry/ScaredyCat.cs @@ -76,7 +76,7 @@ namespace OpenRA.Mods.Common.Traits if (!Panicking) return; - mobile.Nudge(self, self, true); + mobile.Nudge(self); } void INotifyDamage.Damaged(Actor self, AttackInfo e) diff --git a/OpenRA.Mods.Common/Traits/Mobile.cs b/OpenRA.Mods.Common/Traits/Mobile.cs index 2cfebd2bfc..9f4bede314 100644 --- a/OpenRA.Mods.Common/Traits/Mobile.cs +++ b/OpenRA.Mods.Common/Traits/Mobile.cs @@ -330,60 +330,46 @@ namespace OpenRA.Mods.Common.Traits #region Local misc stuff - public void Nudge(Actor self, Actor nudger, bool force) + public void Nudge(Actor nudger) { if (IsTraitDisabled || IsTraitPaused || RequireForceMove) return; - // Pick an adjacent available cell. + var cell = GetAdjacentCell(nudger.Location); + if (cell != null) + self.QueueActivity(false, MoveTo(cell.Value, 0)); + } + + public CPos? GetAdjacentCell(CPos nextCell) + { var availCells = new List(); var notStupidCells = new List(); - - for (var i = -1; i < 2; i++) - for (var j = -1; j < 2; j++) - { - var p = ToCell + new CVec(i, j); - if (CanEnterCell(p)) - availCells.Add(p); - else if (p != nudger.Location && p != ToCell) - notStupidCells.Add(p); - } - - var moveTo = availCells.Any() ? availCells.Random(self.World.SharedRandom) : (CPos?)null; - - if (moveTo.HasValue) + foreach (CVec direction in CVec.Directions) { - self.QueueActivity(false, new Move(self, moveTo.Value, WDist.Zero)); - self.ShowTargetLines(); - - Log.Write("debug", "OnNudge #{0} from {1} to {2}", - self.ActorID, self.Location, moveTo.Value); + var p = ToCell + direction; + if (CanEnterCell(p)) + availCells.Add(p); + else if (p != nextCell && p != ToCell) + notStupidCells.Add(p); } + + CPos? newCell = null; + if (availCells.Count > 0) + newCell = availCells.Random(self.World.SharedRandom); else { var cellInfo = notStupidCells - .SelectMany(c => self.World.ActorMap.GetActorsAt(c).Where(Ismovable), + .SelectMany(c => self.World.ActorMap.GetActorsAt(c).Where(IsMovable), (c, a) => new { Cell = c, Actor = a }) .RandomOrDefault(self.World.SharedRandom); - if (cellInfo != null) - { - self.QueueActivity(false, new CallFunc(() => self.NotifyBlocker(cellInfo.Cell))); - self.QueueActivity(new WaitFor(() => CanEnterCell(cellInfo.Cell))); - self.QueueActivity(new Move(self, cellInfo.Cell)); - - Log.Write("debug", "OnNudge (notify next blocking actor, wait and move) #{0} from {1} to {2}", - self.ActorID, self.Location, cellInfo.Cell); - } - else - { - Log.Write("debug", "OnNudge #{0} refuses at {1}", - self.ActorID, self.Location); - } + newCell = cellInfo.Cell; } + + return newCell; } - public static bool Ismovable(Actor otherActor) + static bool IsMovable(Actor otherActor) { if (!otherActor.IsIdle) return false; @@ -857,7 +843,7 @@ namespace OpenRA.Mods.Common.Traits if (self.IsIdle) { - Nudge(self, blocking, true); + Nudge(blocking); return; } @@ -916,7 +902,7 @@ namespace OpenRA.Mods.Common.Traits self.CancelActivity(); if (order.OrderString == "Scatter") - Nudge(self, self, true); + Nudge(self); } string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)