74f32d14d4
commit 2ad9b5de40f128e8413f1ad5f568a4bc006d0971 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 20:11:41 2019 +0200 v0.8.9.3 commit 2d64da9d1e0d10f9ccb08d6e3fbc45d69fcda368 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 18:08:10 2019 +0200 Randomize the races and the heads in the net lobby. TODO: remember the previous heads and check that the next in line is unique (not exactly the same as the current). commit 09f74ab37692291ca070bac5dbc53eef395fb1cf Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 16:30:16 2019 +0200 AI characters can give treatment to characters who aren't unconscious, fixed AI characters attempting to treat dead characters. Closes #1083 commit a8fea2450cf4ef0b80a8fc809aee339bc39b28c6 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 16:28:23 2019 +0200 Fixed horizontal docking ports failing to find the hull prefab commit dd5a7a4f09195ecfeb82605e1e73ea22607694e7 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 16:02:52 2019 +0200 halved the hitpoints for baby moloch commit 92ae260558ece991560d83819168ef51d5267a66 Merge: 75fe087d7 8f451825e Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:44:08 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 75fe087d7263b46611fb0aad5c91c616e22ad41b Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:43:12 2019 +0200 Update the ai block in the moloch boss config file. commit caf11944294dfe66b5d302888baecbfddad7293a Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:42:42 2019 +0200 Remove the lowercase version of the moloch boss config file. commit b422feece1e620c427fa62d41c2b847b6b8837e9 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:41:40 2019 +0200 Fix molochbaby character name in the config file. commit 913762516dd17921c185de2cb912333f2e5230dd Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:41:22 2019 +0200 Include the baby moloch files in the project. commit 8f451825e95b415b1262f984d83828c379d36ea2 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:36:50 2019 +0200 Added molochbaby to the project file commit 2078307f255f432c19fcbbc0b690acf5655f3c99 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:33:08 2019 +0200 Disable creating humanoids in the wizard for now, because it can go wrong in so many ways. commit 55a931795ac5f60699033c4ff484a9a2d0ae9b7e Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 15:32:18 2019 +0200 Unfreeze when adjusting the joint scale, because the limbs are not otherwise updated to the joint positions. commit 2c92373ccbf331c545c830c9780f450b116f0359 Author: Iiro Enges <iiro@fakefish.fi> Date: Wed Feb 6 15:28:22 2019 +0200 Tinted baby moloch to differentiate from the adult version (for testing purposes) commit f05be351cdf8a6073702e988ed1861f665399048 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:26:52 2019 +0200 Fixed automatic temperature control setting turbine output above 100 if the power consumption is higher than what the reactor can generate. Caused "failed to write an event for the entity" errors in MP when the server tried to write the targetTurbineOutput using WriteRangedSingle with a maximum value of 100. Closes #1085 commit 7b737ebbea41fd5b7825a6c6373e578c1b1c91fd Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:15:59 2019 +0200 Baby moloch, doo doo doo doo doo doo commit f5d13649915ba761bbc6fe78370c607905e2d5f8 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:15:04 2019 +0200 Small fixes and tweaks to gun performance dependent on skill of user. commit 4bf61738c6da08d771dedf1c96160cc76e0b4dd7 Author: ezjamsen <ezjames.fi@gmail.com> Date: Wed Feb 6 15:14:11 2019 +0200 New staggered swarm medium difficulty event commit 3e593f46850987ba074d9b26d59d08eeeddd1d70 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:05:43 2019 +0200 Removed incorrect item name/description instructions fron EnglishVanilla.xml. Correct instructions are at the beginning of the file. commit ce33a523e440a5b5037b84ca96bebf1f977e0aa8 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 15:00:48 2019 +0200 Attempt to fix "attempted to move pull joint extremely far" errors (#895). I think they were (at least for the most part) caused by characters switching to simple physics mode and then moving a significant distance, and when the character switched back to normal animation, the pull joints were still somewhere far away and lerping them to the position of the collider failed (because the lerped position triggers the error). commit c8212e3eff6d9272c9d38b833743b0f33b3611a2 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 13:54:15 2019 +0200 Fixed nullref exception when teleporting from a sub to ruins or vice versa. Closes #1037 commit 0d812394944eb05be4291be7b56593cc4def6d0f Author: Daniel Asteljoki <daniel.asteljoki@gmail.com> Date: Wed Feb 6 13:34:58 2019 +0200 Made regular ruin walls much more durable (500 -> 5000) commit c9a4d9d6f6c9c767ecd8cf34f917db9da852f4bf Author: Daniel Asteljoki <daniel.asteljoki@gmail.com> Date: Wed Feb 6 13:34:26 2019 +0200 Rebalanced Humpback battery grid, added toggles for using backup power commit 47617a6d54f933714dffeca5fa311776e9d0523c Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:17:57 2019 +0200 Remove the lowercase variant of moloch's config file from the source control. commit 640662835684464e4a66b106b1958e5a9fde8a0c Merge: 80e91f96b 7861d00a6 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:10:00 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 80e91f96bf5f1e9f89c5a6e4695292298a802b06 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:09:37 2019 +0200 Fix a null reference exception. commit 91b16aaa7aacc658cb36325ea85944802166ef53 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:09:26 2019 +0200 Instead of having a bad sight, make the Hammerhead deaf. Add "provocative" priority tag, remove "sonar". Make slightly less aggressive towards weaker. commit a383c136f6f6915163e98c472ec7ba7f575ea479 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 13:02:00 2019 +0200 Add "provocative" tag on Moloch and Mudraptor targeting priorities. Tweak moloch priorities a bit. commit 7861d00a609f2555a19f510d72591bd0330f607e Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 12:53:37 2019 +0200 Attempt to fix/diagnose "collection was modified; enumeration operation may not execute" exceptions when using the fixhulls command (see #1011). Haven't been able to reproduce this issue. commit 1d1321c60e8815b3beaffb81bfb9c5f5c2528297 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:52:29 2019 +0200 Add "provocative" tag that can be used as a red flag for the monsters. Add the tag on the diving scooter. Also add an ai target with sound ranges. commit 8530f4c9e4b1c444687cadbf615a9ade0440262b Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:50:43 2019 +0200 Propulsion can now make sound that attracts the enemies. The sound is on when the propulsion is active. commit 291f12676e5148da1a83d4dd72071381c1ae73e3 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:49:39 2019 +0200 If a target item is held by a character, attack the character instead. commit 2aeaa9eaa51f3ed3db71f7853efef37bf5998838 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:46:27 2019 +0200 Allow to define min and max sound ranges. commit 3bf5d2383cbcc33fa862a79c06756e6d9f20a753 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 12:42:57 2019 +0200 Adjust the ai debug info. commit 369355ffef340875335c0d81d9a2c9a4bfac898b Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:40:30 2019 +0200 ContextualTutorial Update cleanup commit 101387adc1308ad87e3134ab2960d3032f725735 Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:34:07 2019 +0200 Formatting commit cc44eff21d57d93e4b0a67249af2135e5782e3c3 Merge: 9a338a2d4 33d1cd1be Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:31:54 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 9a338a2d440ff12426171fe9c3121efb934b6803 Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 12:31:50 2019 +0200 Modified: ContextualTutorial dismissal logic to use buttons instead of the press any key logic commit 33d1cd1beb61862a91bce16ebeb5090a8969cf1b Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 12:07:21 2019 +0200 Fixed spectate button staying disabled if starting a round fails (due to a missing sub file for example). Closes #1066 commit 8935825146203725759463278e7776a42d84a6a0 Merge: 2cdc96214 9fd3de63b Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 11:45:06 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 2cdc96214e2285bfa83a566ed8bc2aed4156c31d Author: EdusFF <pitkanen.eetu@gmail.com> Date: Wed Feb 6 11:44:56 2019 +0200 Added: Saving of the download size to the Workshop.Item, allows displaying the file size for non-downloaded content in SteamWorshopScreen. Closes #1052 commit 9fd3de63bc08e06a924bf5ec8a8b2938a647282f Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 11:34:38 2019 +0200 Watchmen cannot be dragged or grabbed, fixed watchmen imploding continuously if they end up outside. Closes #1072 commit eb3fca43d77e79f04e0ae80daac53b7e2c1e37f7 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 11:14:06 2019 +0200 Fixed server not sending PowerContainer events until the charge of the PowerContainer has changed by more then 100% (i.e. never). Closes #1068 commit 5b3b71bab75489714f72ce55621f6da7d5ec8d94 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 10:59:09 2019 +0200 Prevent locations from being generated too close to each other. Closes #1048 commit b032b36a50b60432f565cab0178de749585562ad Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Wed Feb 6 10:48:43 2019 +0200 Fixed a couple of errors in the translation instructions in EnglishVanilla.xml commit a31b2f257ef863f2cdd1504a3a15356f95262225 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Wed Feb 6 09:34:11 2019 +0200 Show inventory icons in the sprite editor. commit fb7a69d6179824dde0a44d27ce5143a1a25d0147 Author: Roni Kovaniemi <roni.kovaniemi@gmail.com> Date: Wed Feb 6 02:18:44 2019 +0200 Improved background smoke texture commit 888e86a0e7d6e6a70feb2b87ea16cee17adcfd76 Author: Juan Pablo Arce <juanjp600@users.noreply.github.com> Date: Tue Feb 5 11:36:06 2019 -0300 whoops strikes again commit 67e94c6bb9e59b20bc1b9f3cb796780d1c2141f7 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:32:45 2019 +0200 Adjust the min and max values for the sight range and add a todo note. commit ad0becdf175a66c963b2cbea5259b435c250ffa6 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:31:47 2019 +0200 Refactor the sound range calculation. It was way off. commit adc0aa96be5426dbb845e4138acab87c44c935ba Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 16:31:18 2019 +0200 Improve the ai target debug info. commit 9230ba0865896d5740898993ab3389659a4cbb4a Author: Juan Pablo Arce <juanjp600@users.noreply.github.com> Date: Tue Feb 5 10:53:51 2019 -0300 Attempted audio buffer queue fix commit dfbc61628de37a60acbaa208934957a9d892eb99 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 15:18:34 2019 +0200 Fixed: Rubber ducks not floating like a good duck should commit 8b8523dad99b5c1db271ac13e48975487a7c7c99 Merge: 509c67594 c0156c3d3 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 14:34:59 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 509c6759484750d44a154a7f1703f92d0d6e1692 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 14:34:53 2019 +0200 Fixed: Holdable items retaining their pushing ability even when the user is stunned or unconscious. Closes #1061 commit c0156c3d350b64309c8068f437ddd57765f0b911 Author: itchyOwl <lauri.harkanen@gmail.com> Date: Tue Feb 5 13:46:32 2019 +0200 If no limb is selected, select the limb with the most critical affliction. Re-evaluate when new affliction is received. Sort afflictions so that dmg/sec is prioritized. Implements the feature request #1033. commit 4a056c6e9b78f3d6f43574b1b9ff5493d36565b0 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 13:25:57 2019 +0200 Fixed order messages not being visible in single player if the character issuing the order has no headset. Closes #1030 commit 357b14fb4f4b1a56a04d192c17a2ce8e0ab7405e Author: ezjamsen <ezjames.fi@gmail.com> Date: Tue Feb 5 12:52:40 2019 +0200 new load tips commit 1f158760e1ed79968f10647c0a2931d2c603281e Author: ezjamsen <ezjames.fi@gmail.com> Date: Tue Feb 5 12:52:20 2019 +0200 minor formatting tweak to xml commit c53438151043174e18fbfe979ac3b3b862d24255 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 12:48:47 2019 +0200 Stop Steam auth session of a client when they disconnect. Probably fixes servers reporting incorrect player counts (#1049)? commit 2073b170e4fdd5cedd998ff9d01f37842995194b Merge: 9914631c7 9b912a7bf Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 12:43:05 2019 +0200 Merge branch 'dev' of https://github.com/Regalis11/Barotrauma into dev commit 9914631c7eb30c02913cd4ed44f7b7827169ad83 Author: Eetu <pitkanen.eetu@gmail.com> Date: Tue Feb 5 12:42:53 2019 +0200 Fixed: Fabricator allowing new items to be created when the output is not empty, resulting in wasted materials. commit 9b912a7bf16bd121bbe8b7eae20f4a253a9e1eba Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 12:33:00 2019 +0200 Fixed LOS effect not working on ruins when looking at them from inside a sub commit aeaf190c6d3b45d4d12654926acbe223dc4f5888 Author: Joonas Rikkonen <poe.regalis@gmail.com> Date: Tue Feb 5 11:57:17 2019 +0200 Pressing the radio chat hotkey doesn't deselect the chatbox. Closes #1044
1120 lines
45 KiB
C#
1120 lines
45 KiB
C#
using Barotrauma.Extensions;
|
|
using Barotrauma.Items.Components;
|
|
using FarseerPhysics;
|
|
using FarseerPhysics.Dynamics;
|
|
using Microsoft.Xna.Framework;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Xml.Linq;
|
|
using System.Linq;
|
|
|
|
namespace Barotrauma
|
|
{
|
|
partial class EnemyAIController : AIController
|
|
{
|
|
class WallTarget
|
|
{
|
|
public Vector2 Position;
|
|
public Structure Structure;
|
|
public int SectionIndex;
|
|
|
|
public WallTarget(Vector2 position, Structure structure = null, int sectionIndex = -1)
|
|
{
|
|
Position = position;
|
|
Structure = structure;
|
|
SectionIndex = sectionIndex;
|
|
}
|
|
}
|
|
|
|
|
|
public class TargetingPriority
|
|
{
|
|
public string TargetTag;
|
|
public AIState State;
|
|
public float Priority;
|
|
|
|
public TargetingPriority(XElement element)
|
|
{
|
|
TargetTag = element.GetAttributeString("tag", "").ToLowerInvariant();
|
|
Enum.TryParse(element.GetAttributeString("state", ""), out State);
|
|
Priority = element.GetAttributeFloat("priority", 0.0f);
|
|
}
|
|
|
|
public TargetingPriority(string tag, AIState state, float priority)
|
|
{
|
|
TargetTag = tag;
|
|
State = state;
|
|
Priority = priority;
|
|
}
|
|
}
|
|
|
|
private const float UpdateTargetsInterval = 1.0f;
|
|
|
|
private const float RaycastInterval = 1.0f;
|
|
|
|
private bool attackWhenProvoked;
|
|
|
|
private Dictionary<string, TargetingPriority> targetingPriorities = new Dictionary<string, TargetingPriority>();
|
|
|
|
//the preference to attack a specific type of target (-1.0 - 1.0)
|
|
//0.0 = doesn't attack targets of the type
|
|
//positive values = attacks targets of this type
|
|
//negative values = escapes targets of this type
|
|
//private float attackRooms, attackHumans, attackWeaker, attackStronger, eatDeadPriority;
|
|
|
|
//determines which characters are considered weaker/stronger
|
|
private float combatStrength;
|
|
|
|
private SteeringManager outsideSteering, insideSteering;
|
|
|
|
private float updateTargetsTimer;
|
|
|
|
private float raycastTimer;
|
|
|
|
private bool IsCoolDownRunning => attackingLimb != null && attackingLimb.attack.CoolDownTimer > 0;
|
|
|
|
private bool aggressiveBoarding;
|
|
|
|
private LatchOntoAI latchOntoAI;
|
|
|
|
//a point in a wall which the Character is currently targeting
|
|
private WallTarget wallTarget;
|
|
|
|
//the limb selected for the current attack
|
|
private Limb attackingLimb;
|
|
|
|
//flee when the health is below this value
|
|
private float fleeHealthThreshold;
|
|
|
|
private AITargetMemory selectedTargetMemory;
|
|
private float targetValue;
|
|
|
|
private float eatTimer;
|
|
|
|
private Dictionary<AITarget, AITargetMemory> targetMemories;
|
|
|
|
//the eyesight of the NPC (0.0 = blind, 1.0 = sees every target within sightRange)
|
|
private float sight;
|
|
//how far the NPC can hear targets from (0.0 = deaf, 1.0 = hears every target within soundRange)
|
|
private float hearing;
|
|
|
|
private float colliderSize;
|
|
|
|
private readonly float aggressiongreed;
|
|
private readonly float aggressionhurt;
|
|
|
|
public bool AttackHumans
|
|
{
|
|
get
|
|
{
|
|
var targetingPriority = GetTargetingPriority("human");
|
|
return targetingPriority != null && targetingPriority.State == AIState.Attack && targetingPriority.Priority > 0.0f;
|
|
}
|
|
}
|
|
|
|
public bool AttackRooms
|
|
{
|
|
get
|
|
{
|
|
var targetingPriority = GetTargetingPriority("room");
|
|
return targetingPriority != null && targetingPriority.State == AIState.Attack && targetingPriority.Priority > 0.0f;
|
|
}
|
|
}
|
|
|
|
public Limb AttackingLimb
|
|
{
|
|
get { return attackingLimb; }
|
|
}
|
|
|
|
public float CombatStrength
|
|
{
|
|
get { return combatStrength; }
|
|
}
|
|
|
|
public override bool CanEnterSubmarine
|
|
{
|
|
get
|
|
{
|
|
//can't enter a submarine when attached to something
|
|
return latchOntoAI == null || !latchOntoAI.IsAttached;
|
|
}
|
|
}
|
|
|
|
public override bool CanFlip
|
|
{
|
|
get
|
|
{
|
|
//can't flip when attached to something
|
|
return latchOntoAI == null || !latchOntoAI.IsAttached;
|
|
}
|
|
}
|
|
|
|
public EnemyAIController(Character c, string file, string seed) : base(c)
|
|
{
|
|
targetMemories = new Dictionary<AITarget, AITargetMemory>();
|
|
steeringManager = outsideSteering;
|
|
|
|
XDocument doc = XMLExtensions.TryLoadXml(file);
|
|
if (doc == null || doc.Root == null) return;
|
|
|
|
List<XElement> aiElements = new List<XElement>();
|
|
List<float> aiCommonness = new List<float>();
|
|
foreach (XElement element in doc.Root.Elements())
|
|
{
|
|
if (element.Name.ToString().ToLowerInvariant() != "ai") continue;
|
|
aiElements.Add(element);
|
|
aiCommonness.Add(element.GetAttributeFloat("commonness", 1.0f));
|
|
}
|
|
|
|
if (aiElements.Count == 0)
|
|
{
|
|
DebugConsole.ThrowError("Error in file \"" + file + "\" - no AI element found.");
|
|
outsideSteering = new SteeringManager(this);
|
|
insideSteering = new IndoorsSteeringManager(this, false, false);
|
|
return;
|
|
}
|
|
|
|
//choose a random ai element
|
|
MTRandom random = new MTRandom(ToolBox.StringToInt(seed));
|
|
XElement aiElement = aiElements.Count == 1 ?
|
|
aiElements[0] : ToolBox.SelectWeightedRandom(aiElements, aiCommonness, random);
|
|
|
|
combatStrength = aiElement.GetAttributeFloat("combatstrength", 1.0f);
|
|
attackWhenProvoked = aiElement.GetAttributeBool("attackwhenprovoked", false);
|
|
aggressiveBoarding = aiElement.GetAttributeBool("aggressiveboarding", false);
|
|
|
|
sight = aiElement.GetAttributeFloat("sight", 0.0f);
|
|
hearing = aiElement.GetAttributeFloat("hearing", 0.0f);
|
|
|
|
aggressionhurt = aiElement.GetAttributeFloat("aggressionhurt", 100f);
|
|
aggressiongreed = aiElement.GetAttributeFloat("aggressiongreed", 10f);
|
|
|
|
fleeHealthThreshold = aiElement.GetAttributeFloat("fleehealththreshold", 0.0f);
|
|
|
|
foreach (XElement subElement in aiElement.Elements())
|
|
{
|
|
switch (subElement.Name.ToString().ToLowerInvariant())
|
|
{
|
|
case "latchonto":
|
|
latchOntoAI = new LatchOntoAI(subElement, this);
|
|
break;
|
|
case "targetpriority":
|
|
targetingPriorities.Add(subElement.GetAttributeString("tag", "").ToLowerInvariant(), new TargetingPriority(subElement));
|
|
break;
|
|
}
|
|
}
|
|
|
|
bool canBreakDoors = false;
|
|
if (GetTargetingPriority("room")?.Priority > 0.0f)
|
|
{
|
|
AttackContext currentContext = Character.GetAttackContext();
|
|
foreach (Limb limb in Character.AnimController.Limbs)
|
|
{
|
|
if (limb.attack == null) { continue; }
|
|
if (!limb.attack.IsValidTarget(AttackTarget.Structure)) { continue; }
|
|
if (limb.attack.IsValidContext(currentContext) && limb.attack.StructureDamage > 0.0f)
|
|
{
|
|
canBreakDoors = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
outsideSteering = new SteeringManager(this);
|
|
insideSteering = new IndoorsSteeringManager(this, false, canBreakDoors);
|
|
steeringManager = outsideSteering;
|
|
State = AIState.Idle;
|
|
|
|
colliderSize = 0.1f;
|
|
switch (Character.AnimController.Collider.BodyShape)
|
|
{
|
|
case PhysicsBody.Shape.Capsule:
|
|
case PhysicsBody.Shape.HorizontalCapsule:
|
|
case PhysicsBody.Shape.Circle:
|
|
colliderSize = Character.AnimController.Collider.radius * 2;
|
|
break;
|
|
case PhysicsBody.Shape.Rectangle:
|
|
colliderSize = Math.Min(Character.AnimController.Collider.width, Character.AnimController.Collider.height);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public TargetingPriority GetTargetingPriority(string targetTag)
|
|
{
|
|
if (targetingPriorities.TryGetValue(targetTag, out TargetingPriority priority))
|
|
{
|
|
return priority;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public override void SelectTarget(AITarget target)
|
|
{
|
|
selectedAiTarget = target;
|
|
selectedTargetMemory = FindTargetMemory(target);
|
|
|
|
targetValue = 100.0f;
|
|
}
|
|
|
|
public override void Update(float deltaTime)
|
|
{
|
|
bool ignorePlatforms = (-Character.AnimController.TargetMovement.Y > Math.Abs(Character.AnimController.TargetMovement.X));
|
|
|
|
if (steeringManager is IndoorsSteeringManager)
|
|
{
|
|
var currPath = ((IndoorsSteeringManager)steeringManager).CurrentPath;
|
|
if (currPath != null && currPath.CurrentNode != null)
|
|
{
|
|
if (currPath.CurrentNode.SimPosition.Y < Character.AnimController.GetColliderBottom().Y)
|
|
{
|
|
ignorePlatforms = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
Character.AnimController.IgnorePlatforms = ignorePlatforms;
|
|
|
|
if (Character.AnimController is HumanoidAnimController)
|
|
{
|
|
if (Math.Abs(Character.AnimController.movement.X) > 0.1f && !Character.AnimController.InWater)
|
|
{
|
|
Character.AnimController.TargetDir = Character.AnimController.movement.X > 0.0f ? Direction.Right : Direction.Left;
|
|
}
|
|
}
|
|
|
|
if (updateTargetsTimer > 0.0)
|
|
{
|
|
updateTargetsTimer -= deltaTime;
|
|
}
|
|
else
|
|
{
|
|
TargetingPriority targetingPriority = null;
|
|
UpdateTargets(Character, out targetingPriority);
|
|
updateTargetsTimer = UpdateTargetsInterval;
|
|
|
|
if (selectedAiTarget == null)
|
|
{
|
|
State = AIState.Idle;
|
|
}
|
|
else if (Character.Health < fleeHealthThreshold)
|
|
{
|
|
State = AIState.Escape;
|
|
}
|
|
else
|
|
{
|
|
State = targetingPriority.State;
|
|
}
|
|
}
|
|
|
|
latchOntoAI?.Update(this, deltaTime);
|
|
|
|
if (selectedAiTarget != null && (selectedAiTarget.Entity == null || selectedAiTarget.Entity.Removed))
|
|
{
|
|
State = AIState.Idle;
|
|
return;
|
|
}
|
|
|
|
if (Character.Submarine == null)
|
|
{
|
|
if (steeringManager != outsideSteering) outsideSteering.Reset();
|
|
steeringManager = outsideSteering;
|
|
}
|
|
else
|
|
{
|
|
if (steeringManager != insideSteering) insideSteering.Reset();
|
|
steeringManager = insideSteering;
|
|
}
|
|
|
|
bool run = false;
|
|
switch (State)
|
|
{
|
|
case AIState.Idle:
|
|
UpdateIdle(deltaTime);
|
|
break;
|
|
case AIState.Attack:
|
|
run = !IsCoolDownRunning;
|
|
UpdateAttack(deltaTime);
|
|
break;
|
|
case AIState.Eat:
|
|
UpdateEating(deltaTime);
|
|
break;
|
|
case AIState.Escape:
|
|
run = true;
|
|
UpdateEscape(deltaTime);
|
|
break;
|
|
default:
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
// Just some debug code that makes the characters to follow the mouse cursor
|
|
//run = true;
|
|
//Vector2 mousePos = ConvertUnits.ToSimUnits(Screen.Selected.Cam.ScreenToWorld(PlayerInput.MousePosition));
|
|
//steeringManager.SteeringSeek(mousePos, Character.AnimController.GetCurrentSpeed(run));
|
|
|
|
steeringManager.Update(Character.AnimController.GetCurrentSpeed(run));
|
|
}
|
|
|
|
#region Idle
|
|
|
|
private void UpdateIdle(float deltaTime)
|
|
{
|
|
float speed = Character.AnimController.GetCurrentSpeed(false);
|
|
if (Character.Submarine == null && SimPosition.Y < ConvertUnits.ToSimUnits(Character.CharacterHealth.CrushDepth * 0.75f))
|
|
{
|
|
//steer straight up if very deep
|
|
steeringManager.SteeringManual(deltaTime, Vector2.UnitY * speed);
|
|
return;
|
|
}
|
|
|
|
if (wallTarget != null) return;
|
|
|
|
if (selectedAiTarget != null)
|
|
{
|
|
Vector2 targetSimPos = Character.Submarine == null ? ConvertUnits.ToSimUnits(selectedAiTarget.WorldPosition) : selectedAiTarget.SimPosition;
|
|
|
|
steeringManager.SteeringAvoid(deltaTime, colliderSize * 3.0f, speed);
|
|
steeringManager.SteeringSeek(targetSimPos, speed);
|
|
}
|
|
else
|
|
{
|
|
//wander around randomly
|
|
if (Character.Submarine == null)
|
|
{
|
|
steeringManager.SteeringAvoid(deltaTime, colliderSize * 5.0f, speed);
|
|
}
|
|
steeringManager.SteeringWander(speed / 2);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Escape
|
|
|
|
private void UpdateEscape(float deltaTime)
|
|
{
|
|
if (selectedAiTarget == null || selectedAiTarget.Entity == null || selectedAiTarget.Entity.Removed)
|
|
{
|
|
State = AIState.Idle;
|
|
return;
|
|
}
|
|
|
|
Vector2 escapeDir = Vector2.Normalize(SimPosition - selectedAiTarget.SimPosition);
|
|
if (!MathUtils.IsValid(escapeDir)) escapeDir = Vector2.UnitY;
|
|
float speed = Character.AnimController.GetCurrentSpeed(useMaxSpeed: true);
|
|
SteeringManager.SteeringManual(deltaTime, escapeDir * speed);
|
|
SteeringManager.SteeringWander(speed);
|
|
if (Character.CurrentHull == null)
|
|
{
|
|
SteeringManager.SteeringAvoid(deltaTime, colliderSize * 3.0f, speed);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Attack
|
|
|
|
private void UpdateAttack(float deltaTime)
|
|
{
|
|
if (selectedAiTarget == null)
|
|
{
|
|
State = AIState.Idle;
|
|
return;
|
|
}
|
|
|
|
float speed = Character.AnimController.GetCurrentSpeed(true);
|
|
|
|
selectedTargetMemory.Priority -= deltaTime * 0.1f;
|
|
|
|
Vector2 attackSimPosition = Character.Submarine == null ? ConvertUnits.ToSimUnits(selectedAiTarget.WorldPosition) : selectedAiTarget.SimPosition;
|
|
|
|
if (Character.Submarine != null && selectedAiTarget.Entity.Submarine != null && Character.Submarine != selectedAiTarget.Entity.Submarine)
|
|
{
|
|
attackSimPosition = ConvertUnits.ToSimUnits(selectedAiTarget.WorldPosition - Character.Submarine.Position);
|
|
}
|
|
|
|
if (selectedAiTarget.Entity is Item item)
|
|
{
|
|
// If the item is held by a character, attack the character instead.
|
|
var pickable = item.components.Select(c => c as Pickable).FirstOrDefault();
|
|
if (pickable != null)
|
|
{
|
|
var target = pickable.Picker?.AiTarget;
|
|
if (target != null)
|
|
{
|
|
selectedAiTarget = target;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (wallTarget != null)
|
|
{
|
|
attackSimPosition = ConvertUnits.ToSimUnits(wallTarget.Position);
|
|
if (Character.Submarine == null && selectedAiTarget.Entity?.Submarine != null) attackSimPosition += ConvertUnits.ToSimUnits(selectedAiTarget.Entity.Submarine.Position);
|
|
}
|
|
else if (selectedAiTarget.Entity is Character)
|
|
{
|
|
//target the closest limb if the target is a character
|
|
float closestDist = Vector2.DistanceSquared(selectedAiTarget.SimPosition, SimPosition) * 10.0f;
|
|
foreach (Limb limb in ((Character)selectedAiTarget.Entity).AnimController.Limbs)
|
|
{
|
|
if (limb == null) continue;
|
|
float dist = Vector2.DistanceSquared(limb.SimPosition, SimPosition) / Math.Max(limb.AttackPriority, 0.1f);
|
|
if (dist < closestDist)
|
|
{
|
|
closestDist = dist;
|
|
attackSimPosition = limb.SimPosition;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Math.Abs(Character.AnimController.movement.X) > 0.1f && !Character.AnimController.InWater)
|
|
{
|
|
Character.AnimController.TargetDir = Character.SimPosition.X < attackSimPosition.X ? Direction.Right : Direction.Left;
|
|
}
|
|
|
|
if (raycastTimer > 0.0)
|
|
{
|
|
raycastTimer -= deltaTime;
|
|
}
|
|
else
|
|
{
|
|
UpdateWallTarget();
|
|
raycastTimer = RaycastInterval;
|
|
}
|
|
|
|
if (aggressiveBoarding)
|
|
{
|
|
//targeting a wall section that can be passed through -> steer manually through the hole
|
|
if (wallTarget != null && wallTarget.SectionIndex > -1 && CanPassThroughHole(wallTarget.Structure, wallTarget.SectionIndex))
|
|
{
|
|
WallSection section = wallTarget.Structure.GetSection(wallTarget.SectionIndex);
|
|
Hull targetHull = section.gap?.FlowTargetHull;
|
|
if (targetHull != null && !section.gap.IsRoomToRoom)
|
|
{
|
|
Vector2 targetPos = wallTarget.Structure.SectionPosition(wallTarget.SectionIndex, true);
|
|
if (wallTarget.Structure.IsHorizontal)
|
|
{
|
|
targetPos.Y = targetHull.WorldRect.Y - targetHull.Rect.Height / 2;
|
|
}
|
|
else
|
|
{
|
|
targetPos.X = targetHull.WorldRect.Center.X;
|
|
}
|
|
|
|
latchOntoAI?.DeattachFromBody();
|
|
Character.AnimController.ReleaseStuckLimbs();
|
|
if (steeringManager is IndoorsSteeringManager)
|
|
{
|
|
steeringManager.SteeringManual(deltaTime, targetPos - Character.WorldPosition);
|
|
}
|
|
else
|
|
{
|
|
steeringManager.SteeringSeek(ConvertUnits.ToSimUnits(targetPos), speed);
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
else if (selectedAiTarget.Entity is Item)
|
|
{
|
|
var door = ((Item)selectedAiTarget.Entity).GetComponent<Door>();
|
|
//steer through the door manually if it's open or broken
|
|
if (door?.LinkedGap?.FlowTargetHull != null && !door.LinkedGap.IsRoomToRoom && (door.IsOpen || door.Item.Condition <= 0.0f))
|
|
{
|
|
if (door.LinkedGap.IsHorizontal)
|
|
{
|
|
if (Character.WorldPosition.Y < door.Item.WorldRect.Y && Character.WorldPosition.Y > door.Item.WorldRect.Y - door.Item.Rect.Height)
|
|
{
|
|
var velocity = Vector2.UnitX * (door.LinkedGap.FlowTargetHull.WorldPosition.X - Character.WorldPosition.X) * speed;
|
|
steeringManager.SteeringManual(deltaTime, velocity);
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (Character.WorldPosition.X < door.Item.WorldRect.X && Character.WorldPosition.X > door.Item.WorldRect.Right)
|
|
{
|
|
var velocity = Vector2.UnitY * (door.LinkedGap.FlowTargetHull.WorldPosition.Y - Character.WorldPosition.Y) * speed;
|
|
steeringManager.SteeringManual(deltaTime, velocity);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool canAttack = true;
|
|
if (IsCoolDownRunning)
|
|
{
|
|
switch (attackingLimb.attack.AfterAttack)
|
|
{
|
|
case AIBehaviorAfterAttack.Pursue:
|
|
case AIBehaviorAfterAttack.PursueIfCanAttack:
|
|
if (attackingLimb.attack.SecondaryCoolDown <= 0)
|
|
{
|
|
// No (valid) secondary cooldown defined.
|
|
if (attackingLimb.attack.AfterAttack == AIBehaviorAfterAttack.Pursue)
|
|
{
|
|
canAttack = false;
|
|
}
|
|
else
|
|
{
|
|
UpdateFallBack(attackSimPosition, deltaTime);
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (attackingLimb.attack.SecondaryCoolDownTimer <= 0)
|
|
{
|
|
// If the secondary cooldown is defined and expired, check if we can switch the attack
|
|
var previousLimb = attackingLimb;
|
|
var newLimb = GetAttackLimb(attackSimPosition, previousLimb);
|
|
if (newLimb != null)
|
|
{
|
|
attackingLimb = newLimb;
|
|
}
|
|
else
|
|
{
|
|
// No new limb was found.
|
|
if (attackingLimb.attack.AfterAttack == AIBehaviorAfterAttack.Pursue)
|
|
{
|
|
canAttack = false;
|
|
}
|
|
else
|
|
{
|
|
UpdateFallBack(attackSimPosition, deltaTime);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Cooldown not yet expired, cannot attack -> steer towards the target
|
|
canAttack = false;
|
|
}
|
|
}
|
|
break;
|
|
case AIBehaviorAfterAttack.FallBack:
|
|
default:
|
|
UpdateFallBack(attackSimPosition, deltaTime);
|
|
return;
|
|
|
|
}
|
|
}
|
|
|
|
if (attackingLimb == null)
|
|
{
|
|
attackingLimb = GetAttackLimb(attackSimPosition);
|
|
}
|
|
if (canAttack)
|
|
{
|
|
canAttack = attackingLimb != null && attackingLimb.attack.CoolDownTimer <= 0;
|
|
}
|
|
float distance = 0;
|
|
if (canAttack)
|
|
{
|
|
// Check that we can reach the target
|
|
distance = ConvertUnits.ToDisplayUnits(Vector2.Distance(attackingLimb.SimPosition, attackSimPosition));
|
|
canAttack = distance < attackingLimb.attack.Range;
|
|
}
|
|
|
|
Limb steeringLimb = Character.AnimController.MainLimb;
|
|
if (steeringLimb != null)
|
|
{
|
|
Vector2 steeringVector = attackSimPosition - steeringLimb.SimPosition;
|
|
Vector2 targetingVector = Vector2.Normalize(steeringVector) * attackingLimb.attack.Range;
|
|
// Offset the position a bit so that we don't overshoot the movement.
|
|
Vector2 steerPos = attackSimPosition + targetingVector;
|
|
steeringManager.SteeringSeek(steerPos, speed);
|
|
if (Character.CurrentHull == null)
|
|
{
|
|
SteeringManager.SteeringAvoid(deltaTime, colliderSize * 1.5f, speed);
|
|
}
|
|
|
|
if (steeringManager is IndoorsSteeringManager indoorsSteering)
|
|
{
|
|
if (indoorsSteering.CurrentPath != null && !indoorsSteering.IsPathDirty)
|
|
{
|
|
if (indoorsSteering.CurrentPath.Unreachable)
|
|
{
|
|
//wander around randomly and decrease the priority faster if no path is found
|
|
if (selectedTargetMemory != null) selectedTargetMemory.Priority -= deltaTime * 10.0f;
|
|
steeringManager.SteeringWander(speed);
|
|
}
|
|
else if (indoorsSteering.CurrentPath.Finished)
|
|
{
|
|
steeringManager.SteeringManual(deltaTime, steeringVector);
|
|
}
|
|
else if (indoorsSteering.CurrentPath.CurrentNode?.ConnectedDoor != null)
|
|
{
|
|
wallTarget = null;
|
|
selectedAiTarget = indoorsSteering.CurrentPath.CurrentNode.ConnectedDoor.Item.AiTarget;
|
|
}
|
|
else if (indoorsSteering.CurrentPath.NextNode?.ConnectedDoor != null)
|
|
{
|
|
wallTarget = null;
|
|
selectedAiTarget = indoorsSteering.CurrentPath.NextNode.ConnectedDoor.Item.AiTarget;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (canAttack)
|
|
{
|
|
UpdateLimbAttack(deltaTime, attackingLimb, attackSimPosition, distance);
|
|
}
|
|
}
|
|
|
|
private Limb GetAttackLimb(Vector2 attackSimPosition, Limb ignoredLimb = null)
|
|
{
|
|
AttackContext currentContext = Character.GetAttackContext();
|
|
var target = wallTarget != null ? wallTarget.Structure : selectedAiTarget.Entity;
|
|
var limbs = Character.AnimController.Limbs
|
|
.Where(l =>
|
|
l != ignoredLimb &&
|
|
l.attack != null &&
|
|
!l.IsSevered &&
|
|
!l.IsStuck &&
|
|
l.attack.IsValidContext(currentContext) &&
|
|
l.attack.IsValidTarget(target) &&
|
|
l.attack.Conditionals.All(c => (target is ISerializableEntity se && c.Matches(se)) || !(target is ISerializableEntity)))
|
|
.OrderByDescending(l => l.attack.Priority)
|
|
.ThenBy(l => ConvertUnits.ToDisplayUnits(Vector2.Distance(l.SimPosition, attackSimPosition)));
|
|
// TODO: priority should probably not override the distance -> use values instead of booleans
|
|
return limbs.FirstOrDefault();
|
|
}
|
|
|
|
private void UpdateWallTarget()
|
|
{
|
|
wallTarget = null;
|
|
|
|
if (Character.AnimController.CurrentHull != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//check if there's a wall between the target and the Character
|
|
Vector2 rayStart = Character.SimPosition;
|
|
Vector2 rayEnd = selectedAiTarget.SimPosition;
|
|
|
|
if (selectedAiTarget.Entity.Submarine != null && Character.Submarine == null)
|
|
{
|
|
rayStart -= ConvertUnits.ToSimUnits(selectedAiTarget.Entity.Submarine.Position);
|
|
}
|
|
|
|
Body closestBody = Submarine.CheckVisibility(rayStart, rayEnd);
|
|
if (Submarine.LastPickedFraction == 1.0f || closestBody == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Structure wall = closestBody.UserData as Structure;
|
|
if (wall?.Submarine == null)
|
|
{
|
|
return;
|
|
/*if (selectedAiTarget.Entity.Submarine != null)
|
|
{
|
|
wallTarget = new WallTarget(ConvertUnits.ToDisplayUnits(Submarine.LastPickedPosition), selectedAiTarget.Entity.Submarine);
|
|
latchOntoAI?.SetAttachTarget(closestBody, selectedAiTarget.Entity.Submarine, Submarine.LastPickedPosition);
|
|
}*/
|
|
//if (selectedAiTarget.Entity.Submarine != null && Character.Submarine == null) wallAttackPos += ConvertUnits.ToSimUnits(selectedAiTarget.Entity.Submarine.Position);
|
|
}
|
|
else
|
|
{
|
|
int sectionIndex = wall.FindSectionIndex(ConvertUnits.ToDisplayUnits(Submarine.LastPickedPosition));
|
|
int passableHoleCount = GetMinimumPassableHoleCount();
|
|
|
|
float sectionDamage = wall.SectionDamage(sectionIndex);
|
|
for (int i = sectionIndex - 2; i <= sectionIndex + 2; i++)
|
|
{
|
|
if (wall.SectionBodyDisabled(i))
|
|
{
|
|
if (aggressiveBoarding && CanPassThroughHole(wall, i)) //aggressive boarders always target holes they can pass through
|
|
{
|
|
sectionIndex = i;
|
|
break;
|
|
}
|
|
else //otherwise ignore and keep breaking other sections
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
if (wall.SectionDamage(i) > sectionDamage) sectionIndex = i;
|
|
}
|
|
|
|
Vector2 sectionPos = ConvertUnits.ToSimUnits(wall.SectionPosition(sectionIndex));
|
|
Vector2 attachTargetNormal;
|
|
if (wall.IsHorizontal)
|
|
{
|
|
attachTargetNormal = new Vector2(0.0f, Math.Sign(Character.WorldPosition.Y - wall.WorldPosition.Y));
|
|
sectionPos.Y += ConvertUnits.ToSimUnits((wall.BodyHeight <= 0.0f ? wall.Rect.Height : wall.BodyHeight) / 2) * attachTargetNormal.Y;
|
|
}
|
|
else
|
|
{
|
|
attachTargetNormal = new Vector2(Math.Sign(Character.WorldPosition.X - wall.WorldPosition.X), 0.0f);
|
|
sectionPos.X += ConvertUnits.ToSimUnits((wall.BodyWidth <= 0.0f ? wall.Rect.Width : wall.BodyWidth) / 2) * attachTargetNormal.X;
|
|
}
|
|
wallTarget = new WallTarget(ConvertUnits.ToDisplayUnits(sectionPos), wall, sectionIndex);
|
|
latchOntoAI?.SetAttachTarget(wall.Submarine.PhysicsBody.FarseerBody, wall.Submarine, sectionPos, attachTargetNormal);
|
|
}
|
|
}
|
|
|
|
public override void OnAttacked(Character attacker, AttackResult attackResult)
|
|
{
|
|
updateTargetsTimer = Math.Min(updateTargetsTimer, 0.1f);
|
|
|
|
// Reduce the cooldown so that the character can react
|
|
foreach (var limb in Character.AnimController.Limbs)
|
|
{
|
|
if (limb.attack != null)
|
|
{
|
|
limb.attack.CoolDownTimer *= 0.1f;
|
|
// secondary cooldown?
|
|
}
|
|
}
|
|
|
|
if (attackResult.Damage > 0.0f && attackWhenProvoked)
|
|
{
|
|
if (!(attacker is AICharacter) || (((AICharacter)attacker).AIController is HumanAIController))
|
|
{
|
|
targetingPriorities["human"] = new TargetingPriority("human", AIState.Attack, 100.0f);
|
|
targetingPriorities["room"] = new TargetingPriority("room", AIState.Attack, 100.0f);
|
|
}
|
|
}
|
|
|
|
latchOntoAI?.DeattachFromBody();
|
|
Character.AnimController.ReleaseStuckLimbs();
|
|
|
|
if (attacker == null || attacker.AiTarget == null) return;
|
|
AITargetMemory targetMemory = FindTargetMemory(attacker.AiTarget);
|
|
targetMemory.Priority += GetRelativeDamage(attackResult.Damage, Character.Vitality) * aggressionhurt; ;
|
|
}
|
|
|
|
// 10 dmg, 100 health -> 0.1
|
|
private float GetRelativeDamage(float dmg, float vitality) => dmg / Math.Max(vitality, 1.0f);
|
|
|
|
private void UpdateLimbAttack(float deltaTime, Limb limb, Vector2 attackPosition, float distance = -1)
|
|
{
|
|
var damageTarget = wallTarget != null ? wallTarget.Structure : selectedAiTarget.Entity as IDamageable;
|
|
if (damageTarget == null) return;
|
|
|
|
float prevHealth = damageTarget.Health;
|
|
if (limb.UpdateAttack(deltaTime, attackPosition, damageTarget, out AttackResult attackResult, distance))
|
|
{
|
|
if (damageTarget.Health > 0)
|
|
{
|
|
// Managed to hit a living/non-destroyed target. Increase the priority more if the target is low in health -> dies easily/soon
|
|
selectedTargetMemory.Priority += GetRelativeDamage(attackResult.Damage, damageTarget.Health) * aggressiongreed;
|
|
}
|
|
}
|
|
|
|
if (!limb.attack.IsRunning)
|
|
{
|
|
wallTarget = null;
|
|
}
|
|
}
|
|
|
|
private void UpdateFallBack(Vector2 attackPosition, float deltaTime)
|
|
{
|
|
float dist = Vector2.Distance(attackPosition, Character.SimPosition);
|
|
float desiredDist = colliderSize * 2.0f;
|
|
if (dist < desiredDist)
|
|
{
|
|
Vector2 attackDir = Vector2.Normalize(Character.SimPosition - attackPosition);
|
|
if (!MathUtils.IsValid(attackDir)) attackDir = Vector2.UnitY;
|
|
steeringManager.SteeringManual(deltaTime, attackDir * (1.0f - (dist / 500.0f)));
|
|
}
|
|
|
|
steeringManager.SteeringAvoid(deltaTime, colliderSize * 3.0f, Character.AnimController.GetCurrentSpeed(false));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Eat
|
|
|
|
private void UpdateEating(float deltaTime)
|
|
{
|
|
if (selectedAiTarget == null)
|
|
{
|
|
State = AIState.Idle;
|
|
return;
|
|
}
|
|
|
|
Limb mouthLimb = Array.Find(Character.AnimController.Limbs, l => l != null && l.MouthPos.HasValue);
|
|
if (mouthLimb == null) mouthLimb = Character.AnimController.GetLimb(LimbType.Head);
|
|
if (mouthLimb == null)
|
|
{
|
|
DebugConsole.ThrowError("Character \"" + Character.SpeciesName + "\" failed to eat a target (a head or a limb with a mouthpos required)");
|
|
State = AIState.Idle;
|
|
return;
|
|
}
|
|
|
|
Vector2 mouthPos = Character.AnimController.GetMouthPosition().Value;
|
|
Vector2 attackSimPosition = Character.Submarine == null ? ConvertUnits.ToSimUnits(selectedAiTarget.WorldPosition) : selectedAiTarget.SimPosition;
|
|
|
|
Vector2 limbDiff = attackSimPosition - mouthPos;
|
|
float limbDist = limbDiff.Length();
|
|
if (limbDist < 2.0f)
|
|
{
|
|
Character.SelectCharacter(selectedAiTarget.Entity as Character);
|
|
steeringManager.SteeringManual(deltaTime, limbDiff);
|
|
Character.AnimController.Collider.ApplyForce(limbDiff * mouthLimb.Mass * 50.0f, mouthPos);
|
|
}
|
|
else
|
|
{
|
|
steeringManager.SteeringSeek(attackSimPosition - (mouthPos - SimPosition), Character.AnimController.GetCurrentSpeed(useMaxSpeed: true));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Targeting
|
|
|
|
//goes through all the AItargets, evaluates how preferable it is to attack the target,
|
|
//whether the Character can see/hear the target and chooses the most preferable target within
|
|
//sight/hearing range
|
|
public void UpdateTargets(Character character, out TargetingPriority targetingPriority)
|
|
{
|
|
var prevAiTarget = selectedAiTarget;
|
|
|
|
targetingPriority = null;
|
|
selectedAiTarget = null;
|
|
selectedTargetMemory = null;
|
|
targetValue = 0.0f;
|
|
|
|
UpdateTargetMemories();
|
|
|
|
foreach (AITarget target in AITarget.List)
|
|
{
|
|
if (!target.Enabled) continue;
|
|
if (Level.Loaded != null && target.WorldPosition.Y > Level.Loaded.Size.Y)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
float valueModifier = 1.0f;
|
|
float dist = 0.0f;
|
|
|
|
Character targetCharacter = target.Entity as Character;
|
|
|
|
//ignore the aitarget if it is the Character itself
|
|
if (targetCharacter == character) continue;
|
|
|
|
string targetingTag = null;
|
|
if (targetCharacter != null)
|
|
{
|
|
if (targetCharacter.Submarine != null && Character.Submarine == null)
|
|
{
|
|
//target inside, AI outside -> we'll be attacking a wall between the characters so use the priority for attacking rooms
|
|
targetingTag = "room";
|
|
}
|
|
else if (targetCharacter.IsDead)
|
|
{
|
|
targetingTag = "dead";
|
|
}
|
|
else if (targetingPriorities.ContainsKey(targetCharacter.SpeciesName.ToLowerInvariant()))
|
|
{
|
|
targetingTag = targetCharacter.SpeciesName.ToLowerInvariant();
|
|
}
|
|
else
|
|
{
|
|
if (targetCharacter.AIController is EnemyAIController enemy)
|
|
{
|
|
if (enemy.combatStrength > combatStrength)
|
|
{
|
|
targetingTag = "stronger";
|
|
}
|
|
else if (enemy.combatStrength < combatStrength)
|
|
{
|
|
targetingTag = "weaker";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (target.Entity != null)
|
|
{
|
|
//skip the target if it's a room and the character is already inside a sub
|
|
if (character.CurrentHull != null && target.Entity is Hull) continue;
|
|
|
|
//multiply the priority of the target if it's a door from outside to inside and the AI is an aggressive boarder
|
|
Door door = null;
|
|
if (target.Entity is Item item)
|
|
{
|
|
|
|
//item inside and we're outside -> attack the hull
|
|
if (item.CurrentHull != null && character.CurrentHull == null)
|
|
{
|
|
targetingTag = "room";
|
|
}
|
|
|
|
door = item.GetComponent<Door>();
|
|
foreach (TargetingPriority prio in targetingPriorities.Values)
|
|
{
|
|
if (item.HasTag(prio.TargetTag))
|
|
{
|
|
targetingTag = prio.TargetTag;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
targetingTag = "room";
|
|
}
|
|
|
|
if (door != null)
|
|
{
|
|
//increase priority if the character is outside and an aggressive boarder, and the door is from outside to inside
|
|
if (character.CurrentHull == null && aggressiveBoarding && !door.LinkedGap.IsRoomToRoom)
|
|
{
|
|
valueModifier = door.IsOpen ? 5.0f : 3.0f;
|
|
}
|
|
else if (door.IsOpen || door.Item.Condition <= 0.0f) //ignore broken and open doors
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
IDamageable targetDamageable = target.Entity as IDamageable;
|
|
if (targetDamageable != null && targetDamageable.Health <= 0.0f) continue;
|
|
}
|
|
}
|
|
|
|
if (targetingTag == null) continue;
|
|
if (!targetingPriorities.ContainsKey(targetingTag)) continue;
|
|
|
|
valueModifier *= targetingPriorities[targetingTag].Priority;
|
|
|
|
if (valueModifier == 0.0f) continue;
|
|
|
|
dist = Vector2.Distance(character.WorldPosition, target.WorldPosition);
|
|
|
|
//if the target has been within range earlier, the character will notice it more easily
|
|
//(i.e. remember where the target was)
|
|
if (targetMemories.ContainsKey(target)) dist *= 0.5f;
|
|
|
|
//ignore target if it's too far to see or hear
|
|
if (dist > target.SightRange * sight && dist > target.SoundRange * hearing) continue;
|
|
if (!target.IsWithinSector(WorldPosition)) continue;
|
|
|
|
//if the target is very close, the distance doesn't make much difference
|
|
// -> just ignore the distance and attack whatever has the highest priority
|
|
dist = Math.Max(dist, 100.0f);
|
|
|
|
AITargetMemory targetMemory = FindTargetMemory(target);
|
|
valueModifier = valueModifier * targetMemory.Priority / (float)Math.Sqrt(dist);
|
|
|
|
if (valueModifier > targetValue)
|
|
{
|
|
selectedAiTarget = target;
|
|
selectedTargetMemory = targetMemory;
|
|
targetingPriority = targetingPriorities[targetingTag];
|
|
targetValue = valueModifier;
|
|
}
|
|
}
|
|
|
|
if (selectedAiTarget != prevAiTarget)
|
|
{
|
|
wallTarget = null;
|
|
}
|
|
}
|
|
|
|
//find the targetMemory that corresponds to some AItarget or create if there isn't one yet
|
|
private AITargetMemory FindTargetMemory(AITarget target)
|
|
{
|
|
AITargetMemory memory = null;
|
|
if (targetMemories.TryGetValue(target, out memory))
|
|
{
|
|
return memory;
|
|
}
|
|
|
|
memory = new AITargetMemory(10.0f);
|
|
targetMemories.Add(target, memory);
|
|
|
|
return memory;
|
|
}
|
|
|
|
//go through all the targetmemories and delete ones that don't
|
|
//have a corresponding AItarget or whose priority is 0.0f
|
|
private void UpdateTargetMemories()
|
|
{
|
|
List<AITarget> toBeRemoved = null;
|
|
foreach (KeyValuePair<AITarget, AITargetMemory> memory in targetMemories)
|
|
{
|
|
memory.Value.Priority += 0.1f;
|
|
if (Math.Abs(memory.Value.Priority) < 1.0f || !AITarget.List.Contains(memory.Key))
|
|
{
|
|
if (toBeRemoved == null) toBeRemoved = new List<AITarget>();
|
|
toBeRemoved.Add(memory.Key);
|
|
}
|
|
}
|
|
|
|
if (toBeRemoved != null)
|
|
{
|
|
foreach (AITarget target in toBeRemoved)
|
|
{
|
|
targetMemories.Remove(target);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
protected override void OnStateChanged(AIState from, AIState to)
|
|
{
|
|
latchOntoAI?.DeattachFromBody();
|
|
Character.AnimController.ReleaseStuckLimbs();
|
|
}
|
|
|
|
private int GetMinimumPassableHoleCount()
|
|
{
|
|
return (int)Math.Ceiling(ConvertUnits.ToDisplayUnits(colliderSize) / Structure.WallSectionSize);
|
|
}
|
|
|
|
private bool CanPassThroughHole(Structure wall, int sectionIndex)
|
|
{
|
|
int requiredHoleCount = GetMinimumPassableHoleCount();
|
|
if (!wall.SectionBodyDisabled(sectionIndex)) return false;
|
|
int holeCount = 1;
|
|
for (int j = sectionIndex - 1; j > sectionIndex - requiredHoleCount; j--)
|
|
{
|
|
if (wall.SectionBodyDisabled(j))
|
|
holeCount++;
|
|
else
|
|
break;
|
|
}
|
|
for (int j = sectionIndex + 1; j < sectionIndex + requiredHoleCount; j++)
|
|
{
|
|
if (wall.SectionBodyDisabled(j))
|
|
holeCount++;
|
|
else
|
|
break;
|
|
}
|
|
|
|
return holeCount >= requiredHoleCount;
|
|
}
|
|
}
|
|
|
|
//the "memory" of the Character
|
|
//keeps track of how preferable it is to attack a specific target
|
|
//(if the Character can't inflict much damage the target, the priority decreases
|
|
//and if the target attacks the Character, the priority increases)
|
|
class AITargetMemory
|
|
{
|
|
private float priority;
|
|
|
|
public float Priority
|
|
{
|
|
get { return priority; }
|
|
set { priority = MathHelper.Clamp(value, 1.0f, 100.0f); }
|
|
}
|
|
|
|
public AITargetMemory(float priority)
|
|
{
|
|
this.priority = priority;
|
|
}
|
|
|
|
}
|
|
}
|