Autopilot can avoid other subs, characters won't go outside in idle state

This commit is contained in:
Regalis
2016-07-28 21:45:35 +03:00
parent 357a853342
commit c773320a55
6 changed files with 38 additions and 21 deletions
@@ -33,12 +33,6 @@ namespace Barotrauma
get { return currentTarget; } get { return currentTarget; }
} }
public bool HasOutdoorsNodes
{
get;
private set;
}
public IndoorsSteeringManager(ISteerable host, bool canOpenDoors) public IndoorsSteeringManager(ISteerable host, bool canOpenDoors)
: base(host) : base(host)
{ {
@@ -87,8 +81,6 @@ namespace Barotrauma
currentPath = pathFinder.FindPath(pos, target); currentPath = pathFinder.FindPath(pos, target);
HasOutdoorsNodes = currentPath.Nodes.Find(n => n.CurrentHull == null) != null;
findPathTimer = Rand.Range(1.0f,1.2f); findPathTimer = Rand.Range(1.0f,1.2f);
return DiffToCurrentNode(); return DiffToCurrentNode();
@@ -112,7 +112,7 @@ namespace Barotrauma
{ {
indoorsSteering.SteeringWander(); indoorsSteering.SteeringWander();
} }
else if (getDivingGearIfNeeded && indoorsSteering.CurrentPath != null && indoorsSteering.HasOutdoorsNodes) else if (getDivingGearIfNeeded && indoorsSteering.CurrentPath != null && indoorsSteering.CurrentPath.HasOutdoorsNodes)
{ {
AddSubObjective(new AIObjectiveFindDivingGear(character, true)); AddSubObjective(new AIObjectiveFindDivingGear(character, true));
} }
@@ -54,9 +54,12 @@ namespace Barotrauma
newTargetTimer -= deltaTime; newTargetTimer -= deltaTime;
//wander randomly if reached the end of the path or the target is unreachable //wander randomly
// - if reached the end of the path
// - if the target is unreachable
// - if the path requires going outside
if (pathSteering==null || (pathSteering.CurrentPath != null && if (pathSteering==null || (pathSteering.CurrentPath != null &&
(pathSteering.CurrentPath.NextNode == null || pathSteering.CurrentPath.Unreachable))) (pathSteering.CurrentPath.NextNode == null || pathSteering.CurrentPath.Unreachable || pathSteering.CurrentPath.HasOutdoorsNodes)))
{ {
//steer away from edges of the hull //steer away from edges of the hull
if (character.AnimController.CurrentHull!=null) if (character.AnimController.CurrentHull!=null)
@@ -25,6 +25,14 @@ namespace Barotrauma
{ {
if (node == null) return; if (node == null) return;
nodes.Add(node); nodes.Add(node);
if (node.CurrentHull == null) HasOutdoorsNodes = true;
}
public bool HasOutdoorsNodes
{
get;
private set;
} }
public int CurrentIndex public int CurrentIndex
@@ -260,7 +260,7 @@ namespace Barotrauma.Tutorials
infoBox = CreateInfoFrame("The green rectangle in the middle is the submarine, and the flickering shapes outside it are the walls of an underwater cavern. " infoBox = CreateInfoFrame("The green rectangle in the middle is the submarine, and the flickering shapes outside it are the walls of an underwater cavern. "
+ "Try moving the submarine by clicking somewhere on the monitor and dragging the pointer to the direction you want to go to."); + "Try moving the submarine by clicking somewhere on the monitor and dragging the pointer to the direction you want to go to.");
while (steering.CurrTargetVelocity == Vector2.Zero && steering.CurrTargetVelocity.Length() < 50.0f) while (steering.TargetVelocity == Vector2.Zero && steering.TargetVelocity.Length() < 50.0f)
{ {
yield return CoroutineStatus.Running; yield return CoroutineStatus.Running;
} }
@@ -81,7 +81,7 @@ namespace Barotrauma.Items.Components
} }
} }
private Vector2 TargetVelocity public Vector2 TargetVelocity
{ {
get { return targetVelocity;} get { return targetVelocity;}
set set
@@ -91,12 +91,7 @@ namespace Barotrauma.Items.Components
targetVelocity.Y = MathHelper.Clamp(value.Y, -100.0f, 100.0f); targetVelocity.Y = MathHelper.Clamp(value.Y, -100.0f, 100.0f);
} }
} }
public Vector2 CurrTargetVelocity
{
get { return targetVelocity; }
}
public SteeringPath SteeringPath public SteeringPath SteeringPath
{ {
get { return steeringPath; } get { return steeringPath; }
@@ -227,7 +222,7 @@ namespace Barotrauma.Items.Components
steeringPath.CheckProgress(ConvertUnits.ToSimUnits(item.Submarine.WorldPosition), 10.0f); steeringPath.CheckProgress(ConvertUnits.ToSimUnits(item.Submarine.WorldPosition), 10.0f);
if (autopilotRayCastTimer <= 0.0f && steeringPath.NextNode != null) if (autopilotRayCastTimer <= 0.0f && steeringPath.NextNode != null)
{ {
Vector2 diff = Vector2.Normalize(ConvertUnits.ToSimUnits(steeringPath.NextNode.Position - item.Submarine.WorldPosition)); Vector2 diff = Vector2.Normalize(ConvertUnits.ToSimUnits(steeringPath.NextNode.Position - item.Submarine.WorldPosition));
bool nextVisible = true; bool nextVisible = true;
@@ -259,6 +254,25 @@ namespace Barotrauma.Items.Components
{ {
SteerTowardsPosition(steeringPath.CurrentNode.WorldPosition); SteerTowardsPosition(steeringPath.CurrentNode.WorldPosition);
} }
foreach (Submarine sub in Submarine.Loaded)
{
if (sub == item.Submarine) continue;
float thisSize = Math.Max(item.Submarine.Borders.Width, item.Submarine.Borders.Height);
float otherSize = Math.Max(sub.Borders.Width, sub.Borders.Height);
Vector2 diff = sub.WorldPosition - item.Submarine.WorldPosition;
float dist = diff == Vector2.Zero ? 0.0f : diff.Length();
if (dist > thisSize + otherSize) continue;
diff = Vector2.Normalize(diff);
TargetVelocity = Vector2.Lerp(-diff * 100.0f, TargetVelocity, (dist / (thisSize + otherSize)) - 0.5f);
}
} }
private void SteerTowardsPosition(Vector2 worldPosition) private void SteerTowardsPosition(Vector2 worldPosition)
@@ -275,7 +289,7 @@ namespace Barotrauma.Items.Components
} }
else else
{ {
targetVelocity = targetSpeed/5.0f; TargetVelocity = targetSpeed/5.0f;
} }