Enemies can attack each other

This commit is contained in:
Regalis
2017-05-24 17:39:02 +03:00
parent 8b4b35e84b
commit 024094cfba
7 changed files with 104 additions and 21 deletions
@@ -53,7 +53,8 @@
</ragdoll> </ragdoll>
<ai attackhumans="100.0" attackrooms="50.0" <ai attackhumans="100.0" attackrooms="50.0"
attackweaker="50.0" attackstronger="-30.0" attackweaker="0.0" attackstronger="-50.0"
attackcooldown="15.0" attackcooldown="15.0"
combatstrength="1000"
sight="0.5" hearing="1.0"/> sight="0.5" hearing="1.0"/>
</Character> </Character>
@@ -37,5 +37,5 @@
</ragdoll> </ragdoll>
<ai attackhumans="100.0" attackrooms="50.0" attackweaker="50" attackstronger="-30" sight="0.5" hearing="1.0"/> <ai attackhumans="100.0" attackrooms="50.0" attackweaker="50" attackstronger="-30" combatstrength="1500" sight="0.5" hearing="1.0"/>
</Character> </Character>
@@ -75,6 +75,6 @@
<joint limb1="0" limb1anchor="160,50" limb2="9" limb2anchor="0,-200" lowerlimit="-50" upperlimit="20"/> <joint limb1="0" limb1anchor="160,50" limb2="9" limb2anchor="0,-200" lowerlimit="-50" upperlimit="20"/>
</ragdoll> </ragdoll>
<ai attackrooms="100.0" attackweaker="50" attackstronger="-30" sight="0.1" hearing="2.0"/> <ai attackrooms="100.0" attackweaker="50" attackstronger="-30" combatstrength="10000" sight="0.1" hearing="2.0"/>
</Character> </Character>
@@ -85,11 +85,16 @@
<joint limb1="0" limb1anchor="43,-67" limb2="9" limb2anchor="0,21" lowerlimit="-180" upperlimit="-10"/> <joint limb1="0" limb1anchor="43,-67" limb2="9" limb2anchor="0,21" lowerlimit="-180" upperlimit="-10"/>
<joint limb1="9" limb1anchor="0,-28" limb2="10" limb2anchor="-11,-46" lowerlimit="-190" upperlimit="-50"/> <joint limb1="9" limb1anchor="0,-28" limb2="10" limb2anchor="-11,-46" lowerlimit="-190" upperlimit="-50"/>
</ragdoll> </ragdoll>
<ai attackhumans="500" attackrooms="50.0" attackweaker="50" attackstronger="-30" <ai
sight="0.5" hearing="1.0" combatstrength="250"
attackcooldown="1.0"/> attackpriorithumans="500.0"
attackpriorityrooms="50.0"
attackpriorityweaker="60"
attackprioritystronger="-30"
attackcooldown="1.0"
sight="0.5"
hearing="1.0"/>
</Character> </Character>
@@ -72,5 +72,12 @@
</ragdoll> </ragdoll>
<ai attackhumans="100.0" attackrooms="50.0" attackweaker="50" attackstronger="-30" sight="0.5" hearing="1.0"/> <ai
combatstrength="400"
attackpriorithumans="100.0"
attackpriorityrooms="50.0"
attackpriorityweaker="50"
attackprioritystronger="-30"
sight="0.5"
hearing="1.0"/>
</Character> </Character>
@@ -25,6 +25,8 @@ namespace Barotrauma
//negative values = escapes targets of this type //negative values = escapes targets of this type
private float attackRooms, attackHumans, attackWeaker, attackStronger; private float attackRooms, attackHumans, attackWeaker, attackStronger;
private float combatStrength;
private SteeringManager outsideSteering, insideSteering; private SteeringManager outsideSteering, insideSteering;
private float updateTargetsTimer; private float updateTargetsTimer;
@@ -73,10 +75,12 @@ namespace Barotrauma
XElement aiElement = doc.Root.Element("ai"); XElement aiElement = doc.Root.Element("ai");
if (aiElement == null) return; if (aiElement == null) return;
attackRooms = ToolBox.GetAttributeFloat(aiElement, "attackrooms", 0.0f) / 100.0f; attackRooms = ToolBox.GetAttributeFloat(aiElement, 0.0f, "attackrooms", "attackpriorityrooms") / 100.0f;
attackHumans = ToolBox.GetAttributeFloat(aiElement, "attackhumans", 0.0f) / 100.0f; attackHumans = ToolBox.GetAttributeFloat(aiElement, 0.0f, "attackhumans", "attackpriorityhumans") / 100.0f;
attackWeaker = ToolBox.GetAttributeFloat(aiElement, "attackweaker", 0.0f) / 100.0f; attackWeaker = ToolBox.GetAttributeFloat(aiElement, 0.0f, "attackweaker", "attackpriorityweaker") / 100.0f;
attackStronger = ToolBox.GetAttributeFloat(aiElement, "attackstronger", 0.0f) / 100.0f; attackStronger = ToolBox.GetAttributeFloat(aiElement, 0.0f, "attackstronger", "attackprioritystronger") / 100.0f;
combatStrength = ToolBox.GetAttributeFloat(aiElement, "combatstrength", 1.0f);
attackCoolDown = ToolBox.GetAttributeFloat(aiElement, "attackcooldown", 5.0f); attackCoolDown = ToolBox.GetAttributeFloat(aiElement, "attackcooldown", 5.0f);
@@ -157,6 +161,11 @@ namespace Barotrauma
case AiState.Attack: case AiState.Attack:
UpdateAttack(deltaTime); UpdateAttack(deltaTime);
break; break;
case AiState.Escape:
UpdateEscape(deltaTime);
break;
default:
throw new NotImplementedException();
} }
steeringManager.Update(); steeringManager.Update();
@@ -260,6 +269,13 @@ namespace Barotrauma
} }
} }
private void UpdateEscape(float deltaTime)
{
SteeringManager.SteeringManual(deltaTime, Vector2.Normalize(SimPosition - selectedAiTarget.SimPosition));
SteeringManager.SteeringWander(1.0f);
SteeringManager.SteeringAvoid(deltaTime, 2f);
}
private void UpdateCoolDown(Vector2 attackPosition, float deltaTime) private void UpdateCoolDown(Vector2 attackPosition, float deltaTime)
{ {
coolDownTimer -= deltaTime; coolDownTimer -= deltaTime;
@@ -397,9 +413,30 @@ namespace Barotrauma
if (targetCharacter!=null) if (targetCharacter!=null)
{ {
if (attackHumans == 0.0f || targetCharacter.SpeciesName != "human") continue; if (targetCharacter.SpeciesName == "human")
{
valueModifier = attackHumans; if (attackHumans == 0.0f) continue;
valueModifier = attackHumans;
}
else
{
EnemyAIController enemy = targetCharacter.AIController as EnemyAIController;
if (enemy != null)
{
if (enemy.combatStrength > combatStrength)
{
valueModifier = attackStronger;
}
else if (enemy.combatStrength < combatStrength)
{
valueModifier = attackWeaker;
}
else
{
continue;
}
}
}
} }
else if (target.Entity!=null && attackRooms != 0.0f) else if (target.Entity!=null && attackRooms != 0.0f)
{ {
@@ -409,11 +446,13 @@ namespace Barotrauma
valueModifier = attackRooms; valueModifier = attackRooms;
} }
if (valueModifier == 0.0f) continue;
dist = Vector2.Distance(character.WorldPosition, target.WorldPosition); dist = Vector2.Distance(character.WorldPosition, target.WorldPosition);
//if the target has been within range earlier, the character will notice it more easily //if the target has been within range earlier, the character will notice it more easily
//(i.e. remember where the target was) //(i.e. remember where the target was)
if (targetMemories.ContainsKey(target)) dist *= 0.1f; if (targetMemories.ContainsKey(target)) dist *= 0.5f;
//ignore target if it's too far to see or hear //ignore target if it's too far to see or hear
if (dist > target.SightRange * sight && dist > target.SoundRange * hearing) continue; if (dist > target.SightRange * sight && dist > target.SoundRange * hearing) continue;
@@ -434,18 +473,20 @@ namespace Barotrauma
Body closestBody = Submarine.CheckVisibility(rayStart, rayEnd); Body closestBody = Submarine.CheckVisibility(rayStart, rayEnd);
Structure closestStructure = (closestBody == null) ? null : closestBody.UserData as Structure; Structure closestStructure = (closestBody == null) ? null : closestBody.UserData as Structure;
/* float targetStrength = 0.0f;
if (targetDamageable != null) if (targetDamageable != null)
{ {
valueModifier = valueModifier / targetDamageable.Health; targetStrength = targetDamageable.Health;
} }
else if (closestStructure!=null) else if (closestStructure!=null)
{ {
valueModifier = valueModifier / ((IDamageable)closestStructure).Health; targetStrength = ((IDamageable)closestStructure).Health;
} }
else else
{ {
valueModifier = valueModifier / 1000.0f; targetStrength = 1000.0f;
} }*/
;
if (selectedAiTarget == null || Math.Abs(valueModifier) > Math.Abs(targetValue)) if (selectedAiTarget == null || Math.Abs(valueModifier) > Math.Abs(targetValue))
{ {
+29
View File
@@ -162,6 +162,35 @@ namespace Barotrauma
return value; return value;
} }
public static float GetAttributeFloat(XElement element, float defaultValue, params string[] matchingAttributeName)
{
if (element == null) return defaultValue;
foreach (string name in matchingAttributeName)
{
if (element.Attribute(name) == null) continue;
float val = defaultValue;
try
{
if (!float.TryParse(element.Attribute(name).Value, NumberStyles.Float, CultureInfo.InvariantCulture, out val))
{
continue;
}
}
catch (Exception e)
{
DebugConsole.ThrowError("Error in "+element+"!", e);
continue;
}
return val;
}
return defaultValue;
}
public static float GetAttributeFloat(XElement element, string name, float defaultValue) public static float GetAttributeFloat(XElement element, string name, float defaultValue)
{ {
if (element == null || element.Attribute(name) == null) return defaultValue; if (element == null || element.Attribute(name) == null) return defaultValue;