Created
February 25, 2017 23:04
-
-
Save lukaspj/d74b90333a86a2858f7fb891ea8a74ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Torque3D; | |
using Torque3D.Engine.Util.Enums; | |
using Torque3D.Util; | |
namespace GameAI { | |
public class AIClass { | |
public static PlayerAction sLastAction = PlayerAction.None; | |
private static Random rand = new Random(); | |
private static EnemySpottedData enemySpotted = new EnemySpottedData(); | |
private static SimplePlayerData a; | |
[ConsoleFunction] | |
public static PlayerAction AIThink1(FeatureVector vector) { | |
PlayerAction action; | |
enemySpotted.Update(vector); | |
if (vector.TicksSinceObservedEnemy < 10) { | |
action = ReactToEnemy(vector); | |
} | |
else if (vector.TicksSinceObservedEnemy < 100) { | |
enemySpotted.ResetFOVPosition(); | |
action = SearchForEnemy(vector); | |
} else if (vector.TicksSinceDamage < 180) { | |
action = PlayerAction.TurnLeft; | |
} | |
else { | |
action = Discover(vector); | |
} | |
sLastAction = action; | |
return action; | |
} | |
private static PlayerAction Discover(FeatureVector vector) { | |
PlayerAction action; | |
if (vector.DistanceToObstacleLeft < 2.0f || vector.DistanceToObstacleRight < 2.0f) { | |
if (sLastAction == PlayerAction.TurnRight | |
|| sLastAction == PlayerAction.TurnLeft) { | |
action = sLastAction; | |
} else { | |
action = rand.NextDouble() > 0.5 ? PlayerAction.TurnRight : PlayerAction.TurnLeft; | |
} | |
} | |
else { | |
int dec = (vector.TickCount / 32) % 4; | |
Random tmpRandom = new Random(dec); | |
double r = tmpRandom.NextDouble(); | |
if (r < 0.5) { | |
action = PlayerAction.MoveForward; | |
} else if(r < 0.75) { | |
action = PlayerAction.TurnLeft; | |
} else { | |
action = PlayerAction.TurnRight; | |
} | |
} | |
return action; | |
} | |
private static PlayerAction SearchForEnemy(FeatureVector vector) { | |
return PlayerAction.MoveForward; | |
} | |
private static PlayerAction ReactToEnemy(FeatureVector vector) { | |
PlayerAction action; | |
if (vector.DamageProb > 0.80) { | |
if (vector.ShootDelay == 0) { | |
action = PlayerAction.Shoot; | |
} else { | |
action = Dodge(vector); | |
} | |
} | |
else if (vector.TicksSinceObservedEnemy == 0 && enemySpotted.EnemyFOVPosition == EnemySpottedData.EnemyFOVPos.Unknown) { | |
action = enemySpotted.DetermineEnemyPosition(vector); | |
} | |
else if (enemySpotted.LastEnemyFOVPosition == EnemySpottedData.EnemyFOVPos.Left) action = PlayerAction.TurnLeft; | |
else action = PlayerAction.TurnRight; | |
return action; | |
} | |
private static PlayerAction Dodge(FeatureVector vector) { | |
return PlayerAction.Prepare; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Torque3D.Engine.Util.Enums; | |
using Torque3D.Util; | |
namespace GameAI { | |
internal class EnemySpottedData { | |
private float distanceToObstacleLeftSinceEnemySeen; | |
private float distanceToObstacleRightSinceEnemySeen; | |
private float rotSinceEnemySeen; | |
public enum EnemyFOVPos { | |
Unknown, Left, Right | |
} | |
public EnemyFOVPos EnemyFOVPosition = EnemyFOVPos.Unknown; | |
public EnemyFOVPos LastEnemyFOVPosition = EnemyFOVPos.Unknown; | |
public enum EnemySpottedState | |
{ | |
NoEnemySpotted, | |
EnemyInFOV, | |
EnemyNotInFOV, | |
DeterminingEnemyPositionOfFOV | |
} | |
public EnemySpottedState State = EnemySpottedState.NoEnemySpotted; | |
public void Update(FeatureVector vector) { | |
if (State == EnemySpottedState.DeterminingEnemyPositionOfFOV) return; | |
if (vector.TicksSinceObservedEnemy == 0) { | |
distanceToObstacleLeftSinceEnemySeen = vector.DistanceToObstacleLeft; | |
distanceToObstacleRightSinceEnemySeen = vector.DistanceToObstacleRight; | |
rotSinceEnemySeen = 0.0f; | |
State = EnemySpottedState.EnemyInFOV; | |
} | |
else { | |
rotSinceEnemySeen += vector.DeltaRot; | |
State = EnemySpottedState.EnemyNotInFOV; | |
} | |
} | |
public PlayerAction DetermineEnemyPosition(FeatureVector vector) { | |
if (State != EnemySpottedState.DeterminingEnemyPositionOfFOV) { | |
State = EnemySpottedState.DeterminingEnemyPositionOfFOV; | |
return PlayerAction.Prepare; | |
} | |
if (AIClass.sLastAction == PlayerAction.Prepare) { | |
return PlayerAction.TurnLeft; | |
} | |
PlayerAction action; | |
if (vector.DeltaDamageProb > 0) { | |
EnemyFOVPosition = EnemyFOVPos.Left; | |
action = PlayerAction.TurnLeft; | |
} else { | |
EnemyFOVPosition = EnemyFOVPos.Right; | |
action = PlayerAction.TurnRight; | |
} | |
LastEnemyFOVPosition = EnemyFOVPosition; | |
State = EnemySpottedState.EnemyInFOV; | |
return action; | |
} | |
public void ResetFOVPosition() { | |
EnemyFOVPosition = EnemyFOVPos.Unknown; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment