Clean up nudging code.

This commit is contained in:
tovl
2019-09-19 00:09:46 +02:00
committed by teinarss
parent c4d1468f62
commit 38caadfdf0
7 changed files with 31 additions and 77 deletions

View File

@@ -262,7 +262,7 @@ namespace OpenRA.Mods.Cnc.Traits
});
var driverMobile = driver.TraitOrDefault<Mobile>();
if (driverMobile != null)
driverMobile.Nudge(driver, driver, true);
driverMobile.Nudge(driver);
}
}
}

View File

@@ -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<CPos>();
var notStupidCells = new List<CPos>();
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)

View File

@@ -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]

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Mods.Common.Traits
var mobile = self.TraitOrDefault<Mobile>();
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<CrushClass> crushClasses)

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits
var pilotMobile = pilot.TraitOrDefault<Mobile>();
if (pilotMobile != null)
pilotMobile.Nudge(pilot, pilot, true);
pilotMobile.Nudge(pilot);
});
}
}

View File

@@ -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)

View File

@@ -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<CPos>();
var notStupidCells = new List<CPos>();
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)