Autopilot waypoint skipping, AI finds and equips diving gear when the sub is flooding, progress on AI welding, equipping items in AIObjectiveGetItem, wire node coordinate bugfixes, EntityGrid.RemoveEntity fix

This commit is contained in:
Regalis11
2015-12-23 00:10:02 +02:00
parent 63f5a501e8
commit c7e7b3909f
35 changed files with 402 additions and 257 deletions
@@ -109,7 +109,6 @@ namespace Barotrauma
}
else
{
System.Diagnostics.Debug.WriteLine("updatetargets");
UpdateTargets(Character);
updateTargetsTimer = UpdateTargetsInterval;
@@ -160,7 +159,6 @@ namespace Barotrauma
private void UpdateAttack(float deltaTime)
{
if (selectedAiTarget == null)
{
state = AiState.None;
@@ -219,9 +217,7 @@ namespace Barotrauma
{
coolDownTimer -= deltaTime;
attackingLimb = null;
//System.Diagnostics.Debug.WriteLine("cooldown");
if (selectedAiTarget.Entity is Hull ||
Vector2.Distance(attackPosition, Character.AnimController.Limbs[0].SimPosition) < ConvertUnits.ToSimUnits(500.0f))
{
@@ -238,10 +234,7 @@ namespace Barotrauma
private void GetTargetEntity()
{
targetEntity = null;
//check if there's a wall between the target and the Character
Vector2 rayStart = Character.SimPosition;
Vector2 rayEnd = selectedAiTarget.SimPosition;
@@ -329,10 +322,6 @@ namespace Barotrauma
limb.soundTimer = Limb.SoundInterval;
}
else
{
//limb.body.ApplyTorque(limb.Mass * -20.0f * Character.animController.Dir * dir);
}
Vector2 diff = attackPosition - limb.SimPosition;
if (diff.LengthSquared() > 0.00001f)
@@ -363,7 +352,7 @@ namespace Barotrauma
//sight/hearing range
public void UpdateTargets(Character character)
{
if (distanceAccumulator<5.0f && Rand.Range(1,3, false)==1)
if (distanceAccumulator<5.0f && Rand.Range(1,3)==1)
{
selectedAiTarget = null;
character.AnimController.TargetMovement = -character.AnimController.TargetMovement;
@@ -426,49 +415,30 @@ namespace Barotrauma
Body closestBody = Submarine.CheckVisibility(rayStart, rayEnd);
Structure closestStructure = (closestBody == null) ? null : closestBody.UserData as Structure;
//if (targetCharacter != null)
//{
// //if target is a Character that isn't visible, ignore
// if (closestStructure != null) continue;
// //prefer targets with low health
// valueModifier = valueModifier / targetCharacter.Health;
//}
//else
//{
if (targetDamageable != null)
{
valueModifier = valueModifier / targetDamageable.Health;
}
else if (closestStructure!=null)
{
valueModifier = valueModifier / (closestStructure as IDamageable).Health;
}
else
{
valueModifier = valueModifier / 1000.0f;
}
if (targetDamageable != null)
{
valueModifier = valueModifier / targetDamageable.Health;
}
else if (closestStructure!=null)
{
valueModifier = valueModifier / (closestStructure as IDamageable).Health;
}
else
{
valueModifier = valueModifier / 1000.0f;
}
//}
//float newTargetValue = valueModifier/dist;
if (selectedAiTarget == null || Math.Abs(valueModifier) > Math.Abs(targetValue))
{
selectedAiTarget = target;
selectedTargetMemory = targetMemory;
targetValue = valueModifier;
Debug.WriteLine(selectedAiTarget.Entity+": "+targetValue);
}
}
}
//selectedTarget = bestTarget;
//selectedTargetMemory = targetMemory;
//this.targetValue = bestTargetValue;
}
//find the targetMemory that corresponds to some AItarget or create if there isn't one yet
@@ -112,16 +112,16 @@ namespace Barotrauma
if (pathSteering == null || pathSteering.CurrentPath == null || pathSteering.CurrentPath.CurrentNode==null) return;
GUI.DrawLine(spriteBatch,
new Vector2(Character.Position.X, -Character.Position.Y),
new Vector2(pathSteering.CurrentPath.CurrentNode.Position.X, -pathSteering.CurrentPath.CurrentNode.Position.Y),
new Vector2(Character.WorldPosition.X, -Character.WorldPosition.Y),
new Vector2(pathSteering.CurrentPath.CurrentNode.Position.X+Submarine.Loaded.Position.X, -(pathSteering.CurrentPath.CurrentNode.Position.Y+Submarine.Loaded.Position.Y)),
Color.LightGreen);
for (int i = 1; i < pathSteering.CurrentPath.Nodes.Count; i++)
{
GUI.DrawLine(spriteBatch,
new Vector2(pathSteering.CurrentPath.Nodes[i].Position.X, -pathSteering.CurrentPath.Nodes[i].Position.Y),
new Vector2(pathSteering.CurrentPath.Nodes[i - 1].Position.X, -pathSteering.CurrentPath.Nodes[i-1].Position.Y),
new Vector2(pathSteering.CurrentPath.Nodes[i].Position.X + Submarine.Loaded.Position.X, -(pathSteering.CurrentPath.Nodes[i].Position.Y + Submarine.Loaded.Position.Y)),
new Vector2(pathSteering.CurrentPath.Nodes[i - 1].Position.X + Submarine.Loaded.Position.X, -(pathSteering.CurrentPath.Nodes[i-1].Position.Y + Submarine.Loaded.Position.Y)),
Color.LightGreen);
}
}
@@ -62,12 +62,12 @@ namespace Barotrauma
protected override Vector2 DoSteeringSeek(Vector2 target, float speed = 1)
{
//find a new path if one hasn't been found yet or the target is different from the current target
if (currentPath == null || Vector2.Distance(target, currentTarget)>1.0f || findPathTimer < -10.0f)
if (currentPath == null || Vector2.Distance(target, currentTarget)>1.0f || findPathTimer < -5.0f)
{
if (findPathTimer > 0.0f) return Vector2.Zero;
currentTarget = target;
currentPath = pathFinder.FindPath(host.SimPosition+Rand.Vector(0.2f), target);
currentPath = pathFinder.FindPath(host.SimPosition, target);
findPathTimer = Rand.Range(1.0f,1.2f);
@@ -22,7 +22,7 @@ namespace Barotrauma
public virtual bool CanBeCompleted
{
get { return false; }
get { return true; }
}
public string Option
@@ -51,7 +51,7 @@ namespace Barotrauma
/// <param name="character">the character who's trying to achieve the objective</param>
public void TryComplete(float deltaTime)
{
subObjectives.RemoveAll(s => s.IsCompleted());
subObjectives.RemoveAll(s => s.IsCompleted() || !s.CanBeCompleted);
foreach (AIObjective objective in subObjectives)
{
@@ -66,7 +66,7 @@ namespace Barotrauma
public void AddSubObjective(AIObjective objective)
{
if (subObjectives.Find(o => o.IsDuplicate(objective)) != null) return;
if (subObjectives.Any(o => o.IsDuplicate(objective))) return;
subObjectives.Add(objective);
}
@@ -12,13 +12,11 @@ namespace Barotrauma
private string itemName;
private ItemContainer container;
bool canBeCompleted;
bool isCompleted;
public bool IgnoreAlreadyContainedItems;
public AIObjectiveContainItem(Character character, string itemName, ItemContainer container)
: base (character, "")
{
@@ -26,13 +24,13 @@ namespace Barotrauma
this.container = container;
//check if the container has room for more items
canBeCompleted = false;
foreach (Item contained in container.inventory.Items)
{
if (contained != null) continue;
canBeCompleted = true;
break;
}
//canBeCompleted = false;
//foreach (Item contained in container.inventory.Items)
//{
// if (contained != null) continue;
// canBeCompleted = true;
// break;
//}
}
public override bool IsCompleted()
@@ -54,14 +52,28 @@ namespace Barotrauma
return;
}
if (Vector2.Distance(character.SimPosition, container.Item.SimPosition) > container.Item.PickDistance
&& !container.Item.IsInsideTrigger(character.Position))
if (container.Item.inventory == character.Inventory)
{
AddSubObjective(new AIObjectiveGoTo(container.Item, character));
return;
var containedItems = container.inventory.Items;
//if there's already something in the mask (empty oxygen tank?), drop it
var existingItem = containedItems.FirstOrDefault(i => i != null);
if (existingItem != null) existingItem.Drop(character);
character.Inventory.RemoveItem(itemToContain);
container.inventory.TryPutItem(itemToContain, null, false);
}
else
{
if (Vector2.Distance(character.SimPosition, container.Item.SimPosition) > container.Item.PickDistance
&& !container.Item.IsInsideTrigger(character.Position))
{
AddSubObjective(new AIObjectiveGoTo(container.Item, character));
return;
}
container.Combine(itemToContain);
}
container.Combine(itemToContain);
isCompleted = true;
}
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -19,6 +20,8 @@ namespace Barotrauma
private float searchHullTimer;
private AIObjective divingGearObjective;
public float? OverrideCurrentHullSafety;
public AIObjectiveFindSafety(Character character)
@@ -43,6 +46,12 @@ namespace Barotrauma
return;
}
var currentHull = character.AnimController.CurrentHull;
if (currentHull.Volume / currentHull.FullVolume > 0.5f)
{
if (!FindDivingGear(deltaTime)) return;
}
if (searchHullTimer>0.0f)
{
searchHullTimer -= deltaTime;
@@ -50,6 +59,8 @@ namespace Barotrauma
}
else
{
var pathSteering = character.AIController.SteeringManager as IndoorsSteeringManager;
Hull bestHull = null;
@@ -104,6 +115,46 @@ namespace Barotrauma
}
}
private bool FindDivingGear(float deltaTime)
{
var item = character.Inventory.FindItem("diving");
if (item == null)
{
//get a diving mask/suit first
if (!(divingGearObjective is AIObjectiveGetItem))
{
divingGearObjective = new AIObjectiveGetItem(character, "diving", true);
}
}
else
{
var containedItems = item.ContainedItems;
if (containedItems == null) return true;
//check if there's an oxygen tank in the mask
var oxygenTank = Array.Find(containedItems, i => i.Name == "Oxygen Tank" && i.Condition > 0.0f);
if (oxygenTank != null) return true;
if (!(divingGearObjective is AIObjectiveContainItem))
{
divingGearObjective = new AIObjectiveContainItem(character, "Oxygen Tank", item.GetComponent<ItemContainer>());
}
}
if (divingGearObjective != null)
{
divingGearObjective.TryComplete(deltaTime);
return divingGearObjective.IsCompleted();
}
return false;
}
public override bool IsDuplicate(AIObjective otherObjective)
{
return (otherObjective is AIObjectiveFindSafety);
@@ -128,7 +179,8 @@ namespace Barotrauma
}
float safety = 100.0f - fireAmount;
if (waterPercentage > 30.0f) safety -= waterPercentage;
if (waterPercentage > 30.0f && character.Oxygen<80.0f) safety -= waterPercentage;
if (hull.OxygenPercentage < 30.0f) safety -= (30.0f-hull.OxygenPercentage)*5.0f;
return safety;
@@ -1,4 +1,6 @@
using Microsoft.Xna.Framework;
using Barotrauma.Items.Components;
using FarseerPhysics;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,17 +10,26 @@ namespace Barotrauma
{
class AIObjectiveFixLeak : AIObjective
{
Gap leak;
private Gap leak;
public Gap Leak
{
get { return leak; }
}
public AIObjectiveFixLeak(Gap leak, Character character)
:base (character, "")
: base (character, "")
{
this.leak = leak;
}
public override float GetPriority(Character character)
{
return leak.isHorizontal ? leak.Rect.Height * leak.Open : leak.Rect.Width * leak.Open;
float leakSize = (leak.isHorizontal ? leak.Rect.Height : leak.Rect.Width) * leak.Open;
float dist = Vector2.DistanceSquared(character.SimPosition, leak.SimPosition);
dist = Math.Max(dist/1000.0f, 1.0f);
return Math.Min(leakSize/dist, 40.0f);
}
public override bool IsDuplicate(AIObjective otherObjective)
@@ -34,15 +45,51 @@ namespace Barotrauma
if (weldingTool == null)
{
subObjectives.Add(new AIObjectiveGetItem(character, "Welding Tool"));
subObjectives.Add(new AIObjectiveGetItem(character, "Welding Tool", true));
return;
}
else
{
if (Vector2.Distance(character.Position, leak.Position)>10.0f)
var containedItems = weldingTool.ContainedItems;
if (containedItems == null) return;
var fuelTank = Array.Find(containedItems, i => i.Name == "Welding Fuel Tank" && i.Condition > 0.0f);
if (fuelTank == null)
{
subObjectives.Add(new AIObjectiveGoTo(leak.Position,character));
AddSubObjective(new AIObjectiveContainItem(character, "Welding Fuel Tank", weldingTool.GetComponent<ItemContainer>()));
}
}
if (Vector2.Distance(character.Position, leak.Position) > 300.0f)
{
AddSubObjective(new AIObjectiveGoTo(ConvertUnits.ToSimUnits(GetStandPosition()), character));
}
else
{
var repairTool = weldingTool.GetComponent<RepairTool>();
if (repairTool == null) return;
AddSubObjective(new AIObjectiveOperateItem(repairTool, character, ""));
}
}
private Vector2 GetStandPosition()
{
Vector2 standPos = leak.Position;
var hull = leak.linkedTo[0];
if (hull == null) return standPos;
if (leak.isHorizontal)
{
standPos += Vector2.UnitX * Math.Sign(hull.Position.X - leak.Position.X) * leak.Rect.Width;
}
else
{
standPos += Vector2.UnitY * Math.Sign(hull.Position.Y - leak.Position.Y) * leak.Rect.Height;
}
return standPos;
}
}
}
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using Barotrauma.Items.Components;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -18,17 +19,32 @@ namespace Barotrauma
public bool IgnoreContainedItems;
private bool equip;
public override bool CanBeCompleted
{
get { return canBeCompleted; }
}
public AIObjectiveGetItem(Character character, Item targetItem, bool equip = false)
: base(character, "")
{
canBeCompleted = true;
public AIObjectiveGetItem(Character character, string itemName)
this.equip = equip;
currSearchIndex = 0;
this.targetItem = targetItem;
}
public AIObjectiveGetItem(Character character, string itemName, bool equip=false)
: base (character, "")
{
canBeCompleted = true;
this.equip = equip;
currSearchIndex = 0;
this.itemName = itemName;
@@ -40,32 +56,68 @@ namespace Barotrauma
{
if (Vector2.Distance(character.SimPosition, targetItem.SimPosition) < targetItem.PickDistance)
{
int targetSlot = -1;
if (equip)
{
var pickable = targetItem.GetComponent<Pickable>();
//check if all the slots required by the item are free
foreach (LimbSlot slots in pickable.AllowedSlots)
{
if (slots.HasFlag(LimbSlot.Any)) continue;
for (int i = 0; i<character.Inventory.Items.Length; i++)
{
//slot not needed by the item, continue
if (!slots.HasFlag(CharacterInventory.limbSlots[i])) continue;
targetSlot = i;
//slot free, continue
if (character.Inventory.Items[i] == null) continue;
//try to move the existing item to LimbSlot.Any and continue if successful
if (character.Inventory.TryPutItem(character.Inventory.Items[i], new List<LimbSlot>() { LimbSlot.Any }, false)) continue;
//if everything else fails, simply drop the existing item
character.Inventory.Items[i].Drop();
}
}
}
targetItem.Pick(character, false, true);
if (targetSlot>-1 && character.Inventory.IsInLimbSlot(targetItem, LimbSlot.Any))
{
character.Inventory.TryPutItem(targetItem, targetSlot, false);
}
}
return;
}
if (currSearchIndex >= Item.ItemList.Count)
for (int i = 0; i<10 && currSearchIndex<Item.ItemList.Count; i++)
{
canBeCompleted = false;
currSearchIndex++;
if (!Item.ItemList[currSearchIndex].HasTag(itemName) && Item.ItemList[currSearchIndex].Name != itemName) continue;
if (IgnoreContainedItems && Item.ItemList[currSearchIndex].container != null) continue;
if (Item.ItemList[currSearchIndex].inventory is CharacterInventory) continue;
targetItem = Item.ItemList[currSearchIndex];
Item moveToTarget = targetItem;
while (moveToTarget.container != null)
{
moveToTarget = moveToTarget.container;
}
subObjectives.Add(new AIObjectiveGoTo(moveToTarget, character));
return;
}
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));
if (currSearchIndex >= Item.ItemList.Count) canBeCompleted = false;
}
public override bool IsDuplicate(AIObjective otherObjective)
@@ -49,27 +49,23 @@ namespace Barotrauma
{
if (!objectives.Any()) return;
//remove completed objectives
objectives = objectives.FindAll(o => !o.IsCompleted());
//remove completed objectives and ones that can't be completed
objectives = objectives.FindAll(o => !o.IsCompleted() && o.CanBeCompleted);
//sort objectives according to priority
objectives.Sort((x, y) => y.GetPriority(character).CompareTo(x.GetPriority(character)));
if (character.AnimController.CurrentHull!=null)
foreach (Gap gap in Gap.GapList)
{
var gaps = character.AnimController.CurrentHull.FindGaps();
if (gap.IsRoomToRoom || gap.ConnectedDoor != null || gap.Open<0.1f) continue;
foreach (Gap gap in gaps)
{
if (gap.linkedTo.Count > 1) continue;
AddObjective(new AIObjectiveFixLeak(gap, character));
}
AddObjective(new AIObjectiveFixLeak(gap, character));
}
}
public void DoCurrentObjective(float deltaTime)
{
if (currentObjective != null && (!objectives.Any() || objectives[0].GetPriority(character)<OrderPriority))
if (currentObjective != null && (!objectives.Any() || objectives[0].GetPriority(character) < OrderPriority))
{
currentObjective.TryComplete(deltaTime);
return;
@@ -9,39 +9,69 @@ namespace Barotrauma
{
class AIObjectiveOperateItem : AIObjective
{
private ItemComponent targetItem;
private ItemComponent itemController;
private ItemComponent component;
private Entity operateTarget;
private bool isCompleted;
private bool canBeCompleted;
public override bool CanBeCompleted
{
get
{
return canBeCompleted;
}
}
public AIObjectiveOperateItem(ItemComponent item, Character character, string option)
public Entity OperateTarget
{
get { return operateTarget; }
}
public AIObjectiveOperateItem(ItemComponent item, Character character, string option, Entity operateTarget = null)
:base (character, option)
{
targetItem = item;
component = item;
this.operateTarget = operateTarget;
var controllers = item.Item.GetConnectedComponents<Controller>();
if (controllers.Any()) itemController = controllers[0];
if (controllers.Any()) component = controllers[0];
canBeCompleted = true;
}
protected override void Act(float deltaTime)
{
ItemComponent target = itemController == null ? targetItem: itemController;
if (Vector2.Distance(character.SimPosition, target.Item.SimPosition) < target.Item.PickDistance
|| target.Item.IsInsideTrigger(character.WorldPosition))
{
if (character.SelectedConstruction != target.Item && target.CanBeSelected)
if (component.CanBeSelected)
{
if (Vector2.Distance(character.SimPosition, component.Item.SimPosition) < component.Item.PickDistance
|| component.Item.IsInsideTrigger(character.WorldPosition))
{
target.Item.Pick(character, false, true);
if (character.SelectedConstruction != component.Item && component.CanBeSelected)
{
component.Item.Pick(character, false, true);
}
if (component.AIOperate(deltaTime, character, this)) isCompleted = true;
return;
}
if (targetItem.AIOperate(deltaTime, character, this)) isCompleted = true;
return;
AddSubObjective(new AIObjectiveGoTo(component.Item, character));
}
else
{
if (!character.Inventory.Items.Contains(component.Item))
{
AddSubObjective(new AIObjectiveGetItem(character, component.Item, true));
}
else
{
if (component.AIOperate(deltaTime, character, this)) isCompleted = true;
}
}
subObjectives.Add(new AIObjectiveGoTo(target.Item, character));
}
public override bool IsCompleted()
@@ -54,7 +84,7 @@ namespace Barotrauma
AIObjectiveOperateItem operateItem = otherObjective as AIObjectiveOperateItem;
if (operateItem == null) return false;
return (operateItem.targetItem == targetItem);
return (operateItem.component == component);
}
}
}
+1 -21
View File
@@ -18,27 +18,7 @@ namespace Barotrauma
{
get { return aiController; }
}
//public AICharacter(string file) : this(file, Vector2.Zero, null)
//{
//}
//public AICharacter(string file, Vector2 position)
// : this(file, position, null)
//{
//}
//public AICharacter(CharacterInfo characterInfo, WayPoint spawnPoint, bool isNetworkPlayer = false)
// : this(characterInfo.File, spawnPoint.SimPosition, characterInfo, isNetworkPlayer)
//{
//}
//public AICharacter(CharacterInfo characterInfo, Vector2 position, bool isNetworkPlayer = false)
// : this(characterInfo.File, position, characterInfo, isNetworkPlayer)
//{
//}
public AICharacter(string file, Vector2 position, CharacterInfo characterInfo = null, bool isNetworkPlayer = false)
: base(file, position, characterInfo, isNetworkPlayer)
{
@@ -561,7 +561,10 @@ namespace Barotrauma
}
CurrentHull = newHull;
character.Submarine = CurrentHull == null ? null : Submarine.Loaded;
UpdateCollisionCategories();
}
+7 -15
View File
@@ -866,8 +866,6 @@ namespace Barotrauma
{
if (!Enabled) return;
Submarine = AnimController.CurrentHull == null ? null : Submarine.Loaded;
obstructVisionAmount = Math.Max(obstructVisionAmount - deltaTime, 0.0f);
float dist = Vector2.Distance(cam.WorldViewCenter, WorldPosition);
@@ -1125,13 +1123,9 @@ namespace Barotrauma
{
timer += 1.0f / 60.0f;
if (Character.controlled == this)
if (controlled == this)
{
if (cam != null)
{
cam.TargetPos = ConvertUnits.ToDisplayUnits(AnimController.Limbs[0].SimPosition);
cam.OffsetAmount = 0.0f;
}
if (cam != null) cam.OffsetAmount = 0.0f;
GameMain.LightManager.AmbientLight = Color.Lerp(prevAmbientLight, darkLight, timer / dimDuration);
}
@@ -1139,7 +1133,7 @@ namespace Barotrauma
yield return CoroutineStatus.Running;
}
while (Character.Controlled == this)
while (controlled == this)
{
yield return CoroutineStatus.Running;
}
@@ -1381,7 +1375,7 @@ namespace Barotrauma
data = causeOfDeath;
if (causeOfDeath==CauseOfDeath.Pressure)
if (causeOfDeath == CauseOfDeath.Pressure)
{
Implode(true);
}
@@ -1525,13 +1519,11 @@ namespace Barotrauma
if (controlled == this) controlled = null;
if (GameMain.Client!=null && GameMain.Client.Character == this) GameMain.Client.Character = null;
if (GameMain.Client != null && GameMain.Client.Character == this) GameMain.Client.Character = null;
if (aiTarget != null)
aiTarget.Remove();
if (aiTarget != null) aiTarget.Remove();
if (AnimController!=null)
AnimController.Remove();
if (AnimController!=null) AnimController.Remove();
}
}