AI combat, misc AI improvements, characters can't get stuck inside doors

This commit is contained in:
Regalis
2015-12-03 18:34:35 +02:00
parent 11857f894b
commit 5bcdfa2b56
36 changed files with 461 additions and 292 deletions
@@ -48,36 +48,54 @@ namespace Barotrauma
frame = new GUIFrame(Rectangle.Empty, Color.Black * 0.3f);
frame.Padding = new Vector4(200.0f, 100.0f, 200.0f, 100.0f);
UpdateCharacters();
//UpdateCharacters();
int x = 0, y = 150;
foreach (Order order in Order.PrefabList)
{
if (order.ItemComponentType!=null)
int buttonWidth = 130;
int spacing = 10;
int y = 250;
for (int n = 0; n<2; n++)
{
List<Order> orders = (n==0) ?
Order.PrefabList.FindAll(o => o.ItemComponentType == null) :
Order.PrefabList.FindAll(o=> o.ItemComponentType!=null);
int startX = (int)-(buttonWidth * orders.Count + spacing * (orders.Count - 1)) / 2;
int i=0;
foreach (Order order in orders)
{
var matchingItems = Item.ItemList.FindAll(i => i.components.Find(ic => ic.GetType() == order.ItemComponentType) != null);
int y2 = y;
foreach (Item it in matchingItems)
int x = startX + (buttonWidth + spacing) * (i % orders.Count);
if (order.ItemComponentType!=null)
{
var newOrder = new Order(order, it.components.Find(ic => ic.GetType() == order.ItemComponentType));
var matchingItems = Item.ItemList.FindAll(it => it.components.Find(ic => ic.GetType() == order.ItemComponentType) != null);
int y2 = y;
foreach (Item it in matchingItems)
{
var newOrder = new Order(order, it.components.Find(ic => ic.GetType() == order.ItemComponentType));
var button = new GUIButton(new Rectangle(x, y2, 150, 20), order.Name, GUI.Style, frame);
button.UserData = newOrder;
button.OnClicked = SetOrder;
y2 += 25;
var button = new GUIButton(new Rectangle(x+buttonWidth/2, y2, buttonWidth, 20), order.Name, Alignment.TopCenter, GUI.Style, frame);
button.UserData = newOrder;
button.OnClicked = SetOrder;
y2 += 25;
}
}
}
else
{
var button = new GUIButton(new Rectangle(x, y, 150, 20), order.Name, GUI.Style, frame);
button.UserData = order;
button.OnClicked = SetOrder;
else
{
var button = new GUIButton(new Rectangle(x + buttonWidth / 2, y, buttonWidth, 20), order.Name, Alignment.TopCenter, GUI.Style, frame);
button.UserData = order;
button.OnClicked = SetOrder;
}
i++;
}
x += 160;
y += 80;
}
}
public void UpdateCharacters()
@@ -97,12 +115,30 @@ namespace Barotrauma
frame.RemoveChild(child);
}
int x = 0, y = 0;
foreach (Character character in crewManager.characters)
{
if (character.IsDead) continue;
List<Character> aliveCharacters = crewManager.characters.FindAll(c => !c.IsDead);
GUIButton characterButton = new GUIButton(new Rectangle(x, y, 150, 40), "", Color.Transparent, null, frame);
int charactersPerRow = 4;
int spacing = 5;
int rows = (int)Math.Ceiling((double)aliveCharacters.Count / charactersPerRow);
int i = 0;
foreach (Character character in aliveCharacters)
{
int rowCharacterCount = Math.Min(charactersPerRow, aliveCharacters.Count);
if (aliveCharacters.Count - i < charactersPerRow-1) rowCharacterCount = aliveCharacters.Count % charactersPerRow;
// rowCharacterCount = Math.Min(rowCharacterCount, aliveCharacters.Count - i);
int startX = (int)-(150 * rowCharacterCount + spacing * (rowCharacterCount - 1)) / 2;
int x = startX + (150 + spacing) * (i % Math.Min(charactersPerRow, aliveCharacters.Count));
int y = (105 + spacing)*((int)Math.Floor((double)i / charactersPerRow));
GUIButton characterButton = new GUIButton(new Rectangle(x+75, y, 150, 40), "", Color.Black, Alignment.TopCenter, null, frame);
characterButton.UserData = character;
characterButton.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f);
@@ -113,8 +149,10 @@ namespace Barotrauma
}
else
{
characterButton.Color = Color.Black * 0.5f;
characterButton.HoverColor = Color.LightGray * 0.5f;
characterButton.SelectedColor = Color.Gold * 0.5f;
characterButton.OutlineColor = Color.LightGray * 0.8f;
}
string name = character.Info.Name.Replace(' ', '\n');
@@ -130,7 +168,7 @@ namespace Barotrauma
new GUIImage(new Rectangle(-10, -5, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, characterButton);
x += 160;
i++;
}
}
@@ -56,6 +56,11 @@ namespace Barotrauma
private float sight;
//how far the NPC can hear targets from (0.0 = deaf, 1.0 = hears every target within soundRange)
private float hearing;
public AITarget SelectedAiTarget
{
get { return selectedAiTarget; }
}
public EnemyAIController(Character c, string file) : base(c)
{
@@ -32,6 +32,8 @@ namespace Barotrauma
public override void Update(float deltaTime)
{
Character.ClearInputs();
steeringManager = Character.AnimController.CurrentHull == null ? outdoorsSteeringManager : indoorsSteeringManager;
if (updateObjectiveTimer>0.0f)
@@ -45,15 +47,14 @@ namespace Barotrauma
}
objectiveManager.DoCurrentObjective(deltaTime);
//if (Character.Controlled != null)
//{
// steeringManager.SteeringSeek(Character.Controlled.Position);
//}
Character.AnimController.IgnorePlatforms = (-Character.AnimController.TargetMovement.Y > Math.Abs(Character.AnimController.TargetMovement.X));
if (Math.Abs(Character.AnimController.TargetMovement.X) > 0.1f && !Character.AnimController.InWater)
if (Character.IsKeyDown(InputType.Aim))
{
Character.AnimController.TargetDir = Character.CursorPosition.X > Character.Position.X ? Direction.Right : Direction.Left;
}
else if (Math.Abs(Character.AnimController.TargetMovement.X) > 0.1f && !Character.AnimController.InWater)
{
Character.AnimController.TargetDir = Character.AnimController.TargetMovement.X > 0.0f ? Direction.Right : Direction.Left;
}
@@ -64,6 +65,14 @@ namespace Barotrauma
steeringManager.Update(moveSpeed);
}
public override void OnAttacked(IDamageable attacker, float amount)
{
var enemy = attacker as Character;
if (enemy == null) return;
objectiveManager.AddObjective(new AIObjectiveCombat(Character, enemy));
}
public void SetOrder(Order order, string option)
{
objectiveManager.SetOrder(order, option);
@@ -0,0 +1,132 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Barotrauma
{
class AIObjectiveCombat : AIObjective
{
const float CoolDown = 10.0f;
private Character enemy;
private AIObjectiveFindSafety escapeObjective;
float coolDownTimer;
private readonly float enemyStrength;
public AIObjectiveCombat (Character character, Character enemy)
: base(character, "")
{
this.enemy = enemy;
foreach (Limb limb in enemy.AnimController.Limbs)
{
if (limb.attack == null) continue;
enemyStrength += limb.attack.GetDamage(1.0f);
}
coolDownTimer = CoolDown;
}
protected override void Act(float deltaTime)
{
coolDownTimer -= deltaTime;
var weapon = character.Inventory.FindItem("weapon");
if (weapon==null)
{
Escape(deltaTime);
}
else
{
if (!character.SelectedItems.Contains(weapon))
{
character.Inventory.TryPutItem(weapon, 3, false);
weapon.Equip(character);
}
character.CursorPosition = enemy.Position;
character.SetInput(InputType.Aim, false, true);
Vector2 enemyDiff = Vector2.Normalize(enemy.Position - character.Position);
float weaponAngle = ((weapon.body.Dir == 1.0f) ? weapon.body.Rotation : weapon.body.Rotation - MathHelper.Pi);
Vector2 weaponDir = new Vector2((float)Math.Cos(weaponAngle), (float)Math.Sin(weaponAngle));
if (Vector2.Dot(enemyDiff, weaponDir)<0.9f) return;
List<FarseerPhysics.Dynamics.Body> ignoredBodies = new List<FarseerPhysics.Dynamics.Body>();
foreach (Limb limb in character.AnimController.Limbs)
{
ignoredBodies.Add(limb.body.FarseerBody);
}
var pickedBody = Submarine.PickBody(character.SimPosition, enemy.SimPosition, ignoredBodies);
if (pickedBody != null && pickedBody.UserData as Limb == null) return;
weapon.Use(deltaTime, character);
}
}
private void Escape(float deltaTime)
{
if (escapeObjective == null)
{
escapeObjective = new AIObjectiveFindSafety(character);
}
if (enemy.AnimController.CurrentHull == character.AnimController.CurrentHull)
{
escapeObjective.OverrideCurrentHullSafety = 0.0f;
}
else
{
escapeObjective.OverrideCurrentHullSafety = null;
}
escapeObjective.TryComplete(deltaTime);
if (Vector2.Distance(character.SimPosition, enemy.SimPosition) < 3.0f)
{
character.AIController.SteeringManager.SteeringManual(deltaTime, character.SimPosition - enemy.SimPosition);
}
else
{
coolDownTimer = CoolDown;
}
}
public override bool IsCompleted()
{
return enemy.IsDead || coolDownTimer <= 0.0f;
}
public override float GetPriority(Character character)
{
//clamp the strength to the health of this character
//(it doesn't make a difference whether the enemy does 200 or 600 damage, it's one hit kill anyway)
float enemyDanger = Math.Min(enemyStrength, character.Health) + enemy.Health / 10.0f;
EnemyAIController enemyAI = enemy.AIController as EnemyAIController;
if (enemyAI != null)
{
if (enemyAI.SelectedAiTarget == character.AiTarget) enemyDanger *= 2.0f;
}
return Math.Max(enemyDanger, 30.0f);
}
public override bool IsDuplicate(AIObjective otherObjective)
{
AIObjectiveCombat objective = otherObjective as AIObjectiveCombat;
if (objective == null) return false;
return objective.enemy == enemy;
}
}
}
@@ -17,6 +17,7 @@ namespace Barotrauma
bool isCompleted;
public bool IgnoreAlreadyContainedItems;
public AIObjectiveContainItem(Character character, string itemName, ItemContainer container)
: base (character, "")
@@ -47,14 +48,16 @@ namespace Barotrauma
var itemToContain = character.Inventory.FindItem(itemName);
if (itemToContain == null)
{
AddSubObjective(new AIObjectiveGetItem(character, itemName));
var getItem = new AIObjectiveGetItem(character, itemName);
getItem.IgnoreContainedItems = IgnoreAlreadyContainedItems;
AddSubObjective(getItem);
return;
}
if (Vector2.Distance(character.SimPosition, container.Item.SimPosition) > container.Item.PickDistance
|| !container.Item.IsInsideTrigger(character.Position))
&& !container.Item.IsInsideTrigger(character.Position))
{
AddSubObjective(new AIObjectiveGoTo(container.Item.SimPosition, character));
AddSubObjective(new AIObjectiveGoTo(container.Item, character));
return;
}
@@ -65,7 +68,9 @@ namespace Barotrauma
public override bool IsDuplicate(AIObjective otherObjective)
{
AIObjectiveContainItem objective = otherObjective as AIObjectiveContainItem;
return (objective != null);
if (objective == null) return false;
return objective.itemName == itemName && objective.container == container;
}
@@ -11,13 +11,15 @@ namespace Barotrauma
const float SearchHullInterval = 3.0f;
const float MinSafety = 50.0f;
AIObjectiveGoTo gotoObjective;
private AIObjectiveGoTo goToObjective;
private List<Hull> unreachable;
float currenthullSafety;
float searchHullTimer;
private float currenthullSafety;
private float searchHullTimer;
public float? OverrideCurrentHullSafety;
public AIObjectiveFindSafety(Character character)
: base(character, "")
@@ -27,20 +29,24 @@ namespace Barotrauma
protected override void Act(float deltaTime)
{
if (character.AnimController.CurrentHull == null || GetHullSafety(character.AnimController.CurrentHull) > MinSafety)
currenthullSafety = OverrideCurrentHullSafety == null ?
GetHullSafety(character.AnimController.CurrentHull) : (float)OverrideCurrentHullSafety;
if (character.AnimController.CurrentHull == null || currenthullSafety > MinSafety)
{
character.AIController.SteeringManager.SteeringSeek(character.AnimController.CurrentHull.SimPosition);
character.AIController.SelectTarget(null);
gotoObjective = null;
goToObjective = null;
return;
}
if (searchHullTimer>0.0f)
{
searchHullTimer -= deltaTime;
return;
//return;
}
else
{
@@ -67,37 +73,35 @@ namespace Barotrauma
if (bestHull != null)
{
var path = pathSteering.PathFinder.FindPath(character.SimPosition, bestHull.SimPosition);
if (pathSteering.CurrentPath != null && pathSteering.CurrentPath.Cost < path.Cost && !pathSteering.CurrentPath.Unreachable && gotoObjective!=null)
{
return;
}
else
{
pathSteering.SetPath(path);
}
//var path = pathSteering.PathFinder.FindPath(character.SimPosition, bestHull.SimPosition);
//if (pathSteering.CurrentPath == null || (pathSteering.CurrentPath.NextNode==null && pathSteering.CurrentPath.Cost > path.Cost) ||
// pathSteering.CurrentPath.Unreachable || goToObjective==null)
//{
//pathSteering.SetPath(path);
goToObjective = new AIObjectiveGoTo(bestHull, character);
//}
gotoObjective = new AIObjectiveGoTo(bestHull, character);
//character.AIController.SelectTarget(bestHull.AiTarget);
//haracter.AIController.SelectTarget(bestHull.AiTarget);
}
searchHullTimer = SearchHullInterval;
}
if (gotoObjective != null)
if (goToObjective != null)
{
var pathSteering = character.AIController.SteeringManager as IndoorsSteeringManager;
if (pathSteering!=null && pathSteering.CurrentPath!= null &&
pathSteering.CurrentPath.Unreachable && !unreachable.Contains(gotoObjective.Target))
pathSteering.CurrentPath.Unreachable && !unreachable.Contains(goToObjective.Target))
{
unreachable.Add(gotoObjective.Target as Hull);
unreachable.Add(goToObjective.Target as Hull);
}
goToObjective.TryComplete(deltaTime);
}
gotoObjective.TryComplete(deltaTime);
}
public override bool IsDuplicate(AIObjective otherObjective)
@@ -125,7 +129,7 @@ namespace Barotrauma
float safety = 100.0f - fireAmount;
if (waterPercentage > 30.0f) safety -= waterPercentage;
if (hull.OxygenPercentage < 30.0f) safety -= (30.0f-hull.OxygenPercentage)*3.0f;
if (hull.OxygenPercentage < 30.0f) safety -= (30.0f-hull.OxygenPercentage)*5.0f;
return safety;
}
@@ -16,11 +16,14 @@ namespace Barotrauma
private bool canBeCompleted;
public bool IgnoreContainedItems;
public override bool CanBeCompleted
{
get { return canBeCompleted; }
}
public AIObjectiveGetItem(Character character, string itemName)
: base (character, "")
{
@@ -39,7 +42,6 @@ namespace Barotrauma
{
targetItem.Pick(character, false, true);
}
return;
}
if (currSearchIndex >= Item.ItemList.Count)
@@ -47,20 +49,23 @@ namespace Barotrauma
canBeCompleted = false;
return;
}
if (Item.ItemList[currSearchIndex].HasTag(itemName) || Item.ItemList[currSearchIndex].Name == itemName)
{
targetItem = Item.ItemList[currSearchIndex];
while (targetItem.container != null)
{
targetItem = targetItem.container;
}
subObjectives.Add(new AIObjectiveGoTo(targetItem.Position, character));
}
currSearchIndex++;
if (!Item.ItemList[currSearchIndex].HasTag(itemName) && Item.ItemList[currSearchIndex].Name != itemName) return;
if (IgnoreContainedItems && Item.ItemList[currSearchIndex].container != null) return;
targetItem = Item.ItemList[currSearchIndex];
Item moveToTarget = targetItem;
while (moveToTarget.container != null)
{
moveToTarget = moveToTarget.container;
}
subObjectives.Add(new AIObjectiveGoTo(moveToTarget, character));
}
public override bool IsDuplicate(AIObjective otherObjective)
@@ -35,14 +35,15 @@ namespace Barotrauma
: base (character, "")
{
this.target = target;
this.repeat = false;
this.repeat = repeat;
}
public AIObjectiveGoTo(Vector2 targetPos, Character character)
public AIObjectiveGoTo(Vector2 simPos, Character character, bool repeat = false)
: base(character, "")
{
this.targetPos = targetPos;
this.targetPos = simPos;
this.repeat = repeat;
}
protected override void Act(float deltaTime)
@@ -8,6 +8,8 @@ namespace Barotrauma
{
class AIObjectiveIdle : AIObjective
{
const float WallAvoidDistance = 150.0f;
AITarget currentTarget;
private float newTargetTimer;
@@ -33,40 +35,30 @@ namespace Barotrauma
{
currentTarget = FindRandomTarget();
if (currentTarget!=null)
if (currentTarget != null)
{
var path = pathSteering.PathFinder.FindPath(character.SimPosition, currentTarget.SimPosition);
if (path.Cost > 200.0f)
{
return;
}
else
{
pathSteering.SetPath(path);
}
if (path.Cost > 200.0f) return;
pathSteering.SetPath(path);
}
newTargetTimer = currentTarget == null ? 5.0f : 10.0f;
}
else
{
newTargetTimer -= deltaTime;
}
if (currentTarget == null) return;
newTargetTimer -= deltaTime;
//wander randomly if reached the end of the path or the target is unreachable
if (pathSteering!=null && pathSteering.CurrentPath != null &&
(pathSteering.CurrentPath.NextNode == null || pathSteering.CurrentPath.Unreachable))
if (pathSteering==null || (pathSteering.CurrentPath != null &&
(pathSteering.CurrentPath.NextNode == null || pathSteering.CurrentPath.Unreachable)))
{
if (character.Position.X < character.AnimController.CurrentHull.Rect.X + 200.0f)
//steer away from edges of the hull
if (character.Position.X < character.AnimController.CurrentHull.Rect.X + WallAvoidDistance)
{
pathSteering.SteeringManual(deltaTime, Vector2.UnitX);
}
else if (character.Position.X > character.AnimController.CurrentHull.Rect.Right - 200.0f)
else if (character.Position.X > character.AnimController.CurrentHull.Rect.Right - WallAvoidDistance)
{
pathSteering.SteeringManual(deltaTime, -Vector2.UnitX);
}
@@ -75,8 +67,8 @@ namespace Barotrauma
return;
}
if (currentTarget == null) return;
character.AIController.SteeringManager.SteeringSeek(currentTarget.SimPosition);
}
private AITarget FindRandomTarget()
@@ -111,7 +103,7 @@ namespace Barotrauma
public override bool IsDuplicate(AIObjective otherObjective)
{
return true;
return (otherObjective as AIObjectiveIdle != null);
}
}
}
@@ -88,6 +88,9 @@ namespace Barotrauma
case "follow":
currentOrder = new AIObjectiveGoTo(Character.Controlled, character, true);
break;
case "wait":
currentOrder = new AIObjectiveGoTo(character.SimPosition, character, true);
break;
default:
if (order.TargetItem == null) return;
+4 -1
View File
@@ -27,11 +27,14 @@ namespace Barotrauma
PrefabList.Add(new Order("Follow", "Following"));
PrefabList.Add(new Order("Dismiss", "Dismissed"));
PrefabList.Add(new Order("Wait", "Wait"));
PrefabList.Add(new Order("Operate Reactor", "Operating reactor", typeof(Reactor), new string[] {"Power up", "Shutdown"}));
PrefabList.Add(new Order("Operate Railgun", "Operating railgun", typeof(Turret), new string[] { "Fire at will", "Hold fire" }));
PrefabList.Add(new Order("Dismiss", "Dismissed"));
}
private Order(string name, string doingText, Type itemComponentType, string[] parameters = null)
@@ -52,7 +52,7 @@ namespace Barotrauma
public void SteeringManual(float deltaTime, Vector2 velocity)
{
steering += velocity * deltaTime;
steering += velocity;
}
public virtual void Update(float speed = 1.0f)
@@ -12,7 +12,7 @@ namespace Barotrauma
public bool Unreachable
{
get;
private set;
set;
}
public SteeringPath(bool unreachable = false)
@@ -71,7 +71,7 @@ namespace Barotrauma
return DiffToCurrentNode();
}
Vector2 diff = DiffToCurrentNode();
if (diff == Vector2.Zero) return -host.Steering;
@@ -85,10 +85,18 @@ namespace Barotrauma
if (canOpenDoors) CheckDoorsInPath();
currentPath.CheckProgress(host.SimPosition, character.AnimController.InWater ? 1.0f : 0.6f);
float allowedDistance = character.AnimController.InWater ? 1.0f : 0.6f;
if (currentPath.CurrentNode!=null && currentPath.CurrentNode.SimPosition.Y > character.SimPosition.Y+1.0f) allowedDistance*=0.5f;
currentPath.CheckProgress(host.SimPosition, allowedDistance);
if (currentPath.CurrentNode == null) return Vector2.Zero;
//if (currentPath.CurrentNode.SimPosition.Y > character.SimPosition.Y+1.0f && character.AnimController.Stairs == null)
//{
// return currentPath.PrevNode.SimPosition - host.SimPosition;
//}
return currentPath.CurrentNode.SimPosition - host.SimPosition;
}
@@ -109,6 +117,7 @@ namespace Barotrauma
if (door.IsOpen != open)
{
var buttons = door.Item.GetConnectedComponents<Controller>();
foreach (Controller controller in buttons)
{
if (Vector2.Distance(controller.Item.SimPosition, character.SimPosition) > controller.Item.PickDistance * 2.0f) continue;
@@ -131,6 +140,8 @@ namespace Barotrauma
if (!canOpenDoors) return null;
var doorButtons = nextNode.Waypoint.ConnectedGap.ConnectedDoor.Item.GetConnectedComponents<Controller>();
if (!doorButtons.Any()) return null;
foreach (Controller button in doorButtons)
{
if (Math.Sign(button.Item.Position.X - nextNode.Waypoint.Position.X) !=
@@ -250,10 +250,13 @@ namespace Barotrauma
float shortestAngle = leg.Rotation - torso.Rotation;
if (Math.Abs(shortestAngle)<2.5f) continue;
//leg = GetLimb((i == 0) ? LimbType.LeftLeg : LimbType.RightLeg);
if (Math.Abs(shortestAngle)<2.4f) continue;
leg.body.ApplyTorque(-shortestAngle*10.0f);
leg = GetLimb((i == 0) ? LimbType.LeftThigh : LimbType.RightThigh);
leg.body.ApplyTorque(-shortestAngle * 5.0f);
// float torsoRot = MathHelper.WrapAngle(torso.Rotation);
// torsoRot = MathHelper.ToDegrees(torsoRot);
@@ -480,6 +480,13 @@ namespace Barotrauma
return keys[(int)inputType].Held;
}
public void SetInput(InputType inputType, bool hit, bool held)
{
keys[(int)inputType].Hit = hit;
keys[(int)inputType].Held = held;
}
public void ClearInput(InputType inputType)
{
keys[(int)inputType].Hit = false;