Combat & Visibility & Collisions
Converted entity materials to new fade shader Implemented visibility system Refactored per-instance entity code to run inside monobehavior on entity instances Created state machine AI for enemy ships with basic patrol and combat routines Removed legacy AI code (controller item behaviors) Added automatic trigger group assignment for matched weapons Created ship collision system Created shield VFX and colliders
This commit is contained in:
parent
8d4bd04891
commit
b53e4aec8c
204 changed files with 8182 additions and 1919 deletions
96
Assets/Scripts/ServerShared/Agents/Agent.cs
Normal file
96
Assets/Scripts/ServerShared/Agents/Agent.cs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RethinkDb.Driver.Ast;
|
||||
using UniRx;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
public class Agent
|
||||
{
|
||||
private const float FORWARD_DELTA_THRESHOLD = 20;
|
||||
private const float THRUST_DELTA_THRESHOLD = 1;
|
||||
protected BaseState _rootState;
|
||||
private BaseState _currentState;
|
||||
private VelocityLimit _velocityLimit;
|
||||
|
||||
public AgentTask Task { get; set; }
|
||||
public Ship Ship { get; }
|
||||
public ItemManager ItemManager { get; }
|
||||
public GameplaySettings Settings { get; }
|
||||
public float TopSpeed => _velocityLimit?.Limit ?? 100;
|
||||
|
||||
public Agent(Ship ship)
|
||||
{
|
||||
Ship = ship;
|
||||
ItemManager = Ship.ItemManager;
|
||||
Settings = Ship.ItemManager.GameplaySettings;
|
||||
|
||||
_rootState = _currentState = new BaseState(this);
|
||||
_velocityLimit = Ship.GetBehavior<VelocityLimit>();
|
||||
}
|
||||
|
||||
public void Transition(BaseState targetState, Action onTransition = null)
|
||||
{
|
||||
ItemManager.Log($"{Ship.Name} changing state from {_currentState.GetType().Name} to {targetState.GetType().Name}");
|
||||
_currentState.OnExitState();
|
||||
_currentState = targetState;
|
||||
onTransition?.Invoke();
|
||||
_currentState.OnEnterState();
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
_currentState.Update(delta);
|
||||
foreach (var transition in _currentState.Transitions)
|
||||
if (transition.Condition())
|
||||
{
|
||||
Transition(transition.TargetState, transition.OnTransition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Accelerate(float2 targetVelocity, bool noTurn = false)
|
||||
{
|
||||
var deltaV = targetVelocity - Ship.Velocity;
|
||||
var deltaVMag = length(deltaV);
|
||||
var deltaVDirection = normalize(deltaV);
|
||||
// If Delta V is above the threshold, direct the ship towards the delta and use only main thrusters
|
||||
if (!noTurn && deltaVMag > FORWARD_DELTA_THRESHOLD)
|
||||
{
|
||||
Ship.LookDirection = float3(deltaVDirection.x, 0, deltaVDirection.y);
|
||||
Ship.MovementDirection = float2(0, pow(dot(Ship.Direction, deltaVDirection), 2));
|
||||
}
|
||||
// If Delta V is low, direct the ship towards the target and use all thrusters
|
||||
else if(deltaVMag > THRUST_DELTA_THRESHOLD)
|
||||
{
|
||||
var right = Ship.Direction.Rotate(ItemRotation.Clockwise);
|
||||
Ship.MovementDirection = float2(dot(right, deltaVDirection), dot(Ship.Direction, deltaVDirection));
|
||||
}
|
||||
else
|
||||
{
|
||||
Ship.MovementDirection = float2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class StateTransition
|
||||
{
|
||||
public StateTransition(BaseState targetState, Func<bool> condition, Action onTransition)
|
||||
{
|
||||
TargetState = targetState;
|
||||
Condition = condition;
|
||||
OnTransition = onTransition;
|
||||
}
|
||||
|
||||
public BaseState TargetState { get; }
|
||||
public Func<bool> Condition { get; }
|
||||
public Action OnTransition { get; }
|
||||
}
|
||||
|
||||
|
|
@ -1,203 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using RethinkDb.Driver.Ast;
|
||||
using UniRx;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
public abstract class AgentBehavior
|
||||
{
|
||||
public abstract void Update(float delta);
|
||||
|
||||
protected float ThrustInput(float2 deltaV) =>
|
||||
saturate( // Input must be normalized
|
||||
pow( // Apply specificity as exponent to dot product to control how specific the direction needs to be
|
||||
saturate( // Discard negative dot product (we're facing away from desired direction)
|
||||
dot(normalize(Entity.Direction), normalize(deltaV))), // Cosine of angle between direction and deltaV
|
||||
ControllerData.ThrustSpecificity)
|
||||
* length(deltaV) * ControllerData.ThrustSensitivity);
|
||||
|
||||
protected float TurningInput(float2 deltaV) => clamp(-Entity.Direction.AngleDiff(deltaV) * ControllerData.TurningSensitivity, -1, 1);
|
||||
|
||||
protected ControllerData ControllerData;
|
||||
protected ItemManager Context;
|
||||
protected Entity Entity;
|
||||
|
||||
public AgentBehavior(ItemManager context, Entity entity, ControllerData controllerData)
|
||||
{
|
||||
Context = context;
|
||||
Entity = entity;
|
||||
ControllerData = controllerData;
|
||||
}
|
||||
}
|
||||
|
||||
public class Agent
|
||||
{
|
||||
private BaseState _rootState;
|
||||
private BaseState _currentState;
|
||||
private Dictionary<BaseState, List<StateTransition>> _stateTransitions = new Dictionary<BaseState, List<StateTransition>>();
|
||||
|
||||
public AgentTask Task { get; set; }
|
||||
public Ship Ship { get; }
|
||||
public ItemManager ItemManager { get; }
|
||||
public GameplaySettings Settings { get; }
|
||||
|
||||
public Agent(Ship ship)
|
||||
{
|
||||
Ship = ship;
|
||||
ItemManager = Ship.ItemManager;
|
||||
Settings = Ship.ItemManager.GameplaySettings;
|
||||
|
||||
_rootState = _currentState = new BaseState(this);
|
||||
|
||||
var patrolState = new PatrolOrbitsState(this);
|
||||
AddTransition(_rootState, patrolState,
|
||||
() => Task is PatrolOrbitsTask,
|
||||
() => patrolState.Task = Task as PatrolOrbitsTask);
|
||||
var patrolMoveState = new MoveToOrbitState(this);
|
||||
AddTransition(patrolState, patrolMoveState,
|
||||
() => true,
|
||||
() => patrolMoveState.Orbit = patrolState.CurrentTarget);
|
||||
AddTransition(patrolMoveState, patrolState,
|
||||
() => patrolMoveState.Distance < 10,
|
||||
() => patrolState.NextTarget());
|
||||
}
|
||||
|
||||
public void AddTransition(BaseState source, BaseState targetState, Func<bool> condition, Action onTransition = null)
|
||||
{
|
||||
if(!_stateTransitions.ContainsKey(source))
|
||||
_stateTransitions[source] = new List<StateTransition>();
|
||||
_stateTransitions[source].Add(new StateTransition(targetState, condition, onTransition));
|
||||
}
|
||||
|
||||
public void Transition(BaseState targetState, Action onTransition = null)
|
||||
{
|
||||
_currentState.OnExitState();
|
||||
_currentState = targetState;
|
||||
onTransition?.Invoke();
|
||||
_currentState.OnEnterState();
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
_currentState.Update(delta);
|
||||
foreach (var transition in _stateTransitions[_currentState])
|
||||
if (transition.Condition())
|
||||
{
|
||||
Transition(transition.TargetState, transition.OnTransition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public class StateTransition
|
||||
{
|
||||
public StateTransition(BaseState targetState, Func<bool> condition, Action onTransition)
|
||||
{
|
||||
TargetState = targetState;
|
||||
Condition = condition;
|
||||
OnTransition = onTransition;
|
||||
}
|
||||
|
||||
public BaseState TargetState { get; }
|
||||
public Func<bool> Condition { get; }
|
||||
public Action OnTransition { get; }
|
||||
}
|
||||
}
|
||||
|
||||
public class BaseState
|
||||
{
|
||||
protected Agent _agent;
|
||||
|
||||
public BaseState(Agent agent)
|
||||
{
|
||||
_agent = agent;
|
||||
}
|
||||
public virtual void Update(float delta){}
|
||||
public virtual void OnEnterState(){}
|
||||
public virtual void OnExitState(){}
|
||||
}
|
||||
|
||||
public abstract class MoveToState : BaseState
|
||||
{
|
||||
private const float FORWARD_DELTA_THRESHOLD = 20;
|
||||
private const float THRUST_DELTA_THRESHOLD = 1;
|
||||
private VelocityLimit _velocityLimit;
|
||||
protected abstract float2 TargetPosition { get; }
|
||||
public float Distance { get; private set; }
|
||||
|
||||
protected MoveToState(Agent agent) : base(agent)
|
||||
{
|
||||
_velocityLimit = agent.Ship.GetBehavior<VelocityLimit>();
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var diff = TargetPosition - _agent.Ship.Position.xz;
|
||||
var dir = normalize(diff);
|
||||
Distance = length(diff);
|
||||
|
||||
// We want to go top speed in the direction of our target
|
||||
var topSpeed = _velocityLimit?.Limit ?? 100;
|
||||
var desiredVelocity = dir * topSpeed;
|
||||
var deltaV = desiredVelocity - _agent.Ship.Velocity;
|
||||
var deltaVMag = length(deltaV);
|
||||
var deltaVDirection = normalize(deltaV);
|
||||
// If Delta V is above the threshold, direct the ship towards the delta and use only main thrusters
|
||||
if (deltaVMag > FORWARD_DELTA_THRESHOLD)
|
||||
{
|
||||
_agent.Ship.LookDirection = float3(deltaVDirection.x, 0, deltaVDirection.y);
|
||||
_agent.Ship.MovementDirection = float2(0, pow(dot(_agent.Ship.Direction, deltaVDirection), 2));
|
||||
}
|
||||
// If Delta V is low, direct the ship towards the target and use all thrusters
|
||||
else if(deltaVMag > THRUST_DELTA_THRESHOLD)
|
||||
{
|
||||
var right = _agent.Ship.Direction.Rotate(ItemRotation.Clockwise);
|
||||
_agent.Ship.LookDirection = float3(dir.x, 0, dir.y);
|
||||
_agent.Ship.MovementDirection = float2(dot(right, deltaVDirection), dot(_agent.Ship.Direction, deltaVDirection));
|
||||
}
|
||||
else
|
||||
{
|
||||
_agent.Ship.LookDirection = float3(dir.x, 0, dir.y);
|
||||
_agent.Ship.MovementDirection = float2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MoveToEntityState : MoveToState
|
||||
{
|
||||
public Entity TargetEntity { get; set; }
|
||||
protected override float2 TargetPosition => TargetEntity?.Position.xz ?? float2.zero;
|
||||
|
||||
public MoveToEntityState(Agent agent) : base(agent) { }
|
||||
}
|
||||
|
||||
public class MoveToOrbitState : MoveToState
|
||||
{
|
||||
public Guid Orbit { get; set; }
|
||||
public MoveToOrbitState(Agent agent) : base(agent) { }
|
||||
|
||||
protected override float2 TargetPosition => _agent.Ship.Zone.GetOrbitPosition(Orbit);
|
||||
}
|
||||
|
||||
public class PatrolOrbitsState : BaseState
|
||||
{
|
||||
public PatrolOrbitsTask Task;
|
||||
public Guid CurrentTarget
|
||||
{
|
||||
get => Task.Circuit[_currentTargetIndex];
|
||||
}
|
||||
private int _currentTargetIndex;
|
||||
public void NextTarget()
|
||||
{
|
||||
_currentTargetIndex++;
|
||||
_currentTargetIndex %= Task.Circuit.Length;
|
||||
}
|
||||
public PatrolOrbitsState(Agent agent) : base(agent) { }
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class Locomotion : AgentBehavior
|
||||
{
|
||||
public float2 Objective;
|
||||
private const float TangentSensitivity = 2;
|
||||
|
||||
private VelocityLimit _velocityLimit;
|
||||
private Thruster[] _thrusters;
|
||||
private float _thrust;
|
||||
|
||||
public Locomotion(ItemManager context, Entity entity, ControllerData controllerData) : base(context, entity, controllerData)
|
||||
{
|
||||
_velocityLimit = Entity.GetBehavior<VelocityLimit>();
|
||||
_thrusters = entity.GetBehaviors<Thruster>()
|
||||
.Where(t => t.Item.EquippableItem.Rotation == ItemRotation.Reversed)
|
||||
.ToArray();
|
||||
_thrust = _thrusters.Sum(t => t.Thrust);
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var diff = Objective - Entity.Position.xz;
|
||||
|
||||
// We want to go top speed in the direction of our target
|
||||
var topSpeed = _velocityLimit?.Limit ?? 100;
|
||||
float2 desiredVelocity = normalize(diff) * topSpeed;
|
||||
|
||||
var accelerationTime = length(desiredVelocity - Entity.Velocity) / (_thrust / Entity.Mass);
|
||||
var accelerationDistance = accelerationTime * length((desiredVelocity + Entity.Velocity) / 2);
|
||||
|
||||
// We do not have enough space to get to full speed, use approximate maneuvering
|
||||
// if (accelerationDistance > length(diff))
|
||||
// {
|
||||
// float2 tangential = Entity.Velocity - dot(normalize(diff), normalize(Entity.Velocity)) * Entity.Velocity;
|
||||
// float2 adjustedDirection = normalize(diff) - tangential * TangentSensitivity;
|
||||
//
|
||||
// _turning.Axis = TurningInput(adjustedDirection);
|
||||
// _thrust.Axis = 1;
|
||||
// }
|
||||
// // We have plenty of space, use exact maneuvering
|
||||
// else
|
||||
// {
|
||||
// var deltaV = desiredVelocity - Entity.Velocity;
|
||||
// _turning.Axis = TurningInput(deltaV);
|
||||
// _thrust.Axis = ThrustInput(deltaV);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c3af5050375d08242a52bdf7f7de558e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
Assets/Scripts/ServerShared/Agents/Minion.cs
Normal file
23
Assets/Scripts/ServerShared/Agents/Minion.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
|
||||
using System.Linq;
|
||||
using UniRx;
|
||||
|
||||
public class Minion : Agent
|
||||
{
|
||||
public Minion(Ship ship) : base(ship)
|
||||
{
|
||||
var patrolState = new PatrolOrbitsState(this);
|
||||
_rootState.AddTransition(patrolState,
|
||||
() => Task is PatrolOrbitsTask,
|
||||
() => patrolState.Task = Task as PatrolOrbitsTask);
|
||||
|
||||
Ship.VisibleHostiles.ObserveAdd().Where(_ => Ship.Target.Value == null).Subscribe(add => Ship.Target.Value = add.Value);
|
||||
|
||||
var combatState = new CombatState(this);
|
||||
_rootState.AddTransition(combatState,
|
||||
() => Ship.Target.Value != null, null, true, _rootState);
|
||||
|
||||
combatState.AddTransition(_rootState,
|
||||
() => Ship.Target.Value == null, null, true, _rootState);
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Agents/Minion.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Agents/Minion.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b764e85a1fbe4d68abcc77480d6523e3
|
||||
timeCreated: 1613182738
|
||||
3
Assets/Scripts/ServerShared/Agents/States.meta
Normal file
3
Assets/Scripts/ServerShared/Agents/States.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 823a891ccfdd4b13be60053a85bfc073
|
||||
timeCreated: 1613182847
|
||||
63
Assets/Scripts/ServerShared/Agents/States/BaseState.cs
Normal file
63
Assets/Scripts/ServerShared/Agents/States/BaseState.cs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class BaseState
|
||||
{
|
||||
public List<StateTransition> Transitions { get; } = new List<StateTransition>();
|
||||
protected Agent _agent;
|
||||
|
||||
|
||||
public BaseState(Agent agent)
|
||||
{
|
||||
_agent = agent;
|
||||
}
|
||||
|
||||
private static List<BaseState> _lastVisitedStates = new List<BaseState>();
|
||||
private static List<BaseState> _leafStates = new List<BaseState>();
|
||||
private static List<BaseState> _visitedStates = new List<BaseState>();
|
||||
private static HashSet<BaseState> _ignoreStates = new HashSet<BaseState>();
|
||||
public void AddTransition(BaseState targetState, Func<bool> condition, Action onTransition = null, bool includeChildren = false, params BaseState[] ignoreStates)
|
||||
{
|
||||
Transitions.Add(new StateTransition(targetState, condition, onTransition));
|
||||
|
||||
// Traverse the state graph, adding every state that is reachable from this state without traversing over ignored nodes
|
||||
if (includeChildren)
|
||||
{
|
||||
_visitedStates.Clear();
|
||||
_ignoreStates.Clear();
|
||||
if(ignoreStates!=null)
|
||||
foreach (var ignoreState in ignoreStates)
|
||||
_ignoreStates.Add(ignoreState);
|
||||
_leafStates.Clear();
|
||||
_leafStates.Add(this);
|
||||
_ignoreStates.Add(this);
|
||||
_ignoreStates.Add(targetState);
|
||||
while (_leafStates.Count > 0)
|
||||
{
|
||||
_lastVisitedStates.Clear();
|
||||
_lastVisitedStates.AddRange(_leafStates);
|
||||
_leafStates.Clear();
|
||||
foreach (var state in _lastVisitedStates)
|
||||
{
|
||||
foreach (var transition in state.Transitions)
|
||||
{
|
||||
if(!_ignoreStates.Contains(transition.TargetState))
|
||||
{
|
||||
_visitedStates.Add(transition.TargetState);
|
||||
_ignoreStates.Add(transition.TargetState);
|
||||
_leafStates.Add(transition.TargetState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach (var state in _visitedStates)
|
||||
{
|
||||
state.AddTransition(targetState, condition, onTransition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Update(float delta){}
|
||||
public virtual void OnEnterState(){}
|
||||
public virtual void OnExitState(){}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a3f09ceb47034c18b938cb8732df690b
|
||||
timeCreated: 1613182963
|
||||
150
Assets/Scripts/ServerShared/Agents/States/Combat.cs
Normal file
150
Assets/Scripts/ServerShared/Agents/States/Combat.cs
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
using float3 = Unity.Mathematics.float3;
|
||||
using float2x2 = Unity.Mathematics.float2x2;
|
||||
|
||||
public class CombatState : BaseState
|
||||
{
|
||||
private const int DPS_SAMPLE_COUNT = 32;
|
||||
private float _optimumRange;
|
||||
private readonly List<(int index, float dps)> _availableGroups = new List<(int index, float dps)>();
|
||||
private readonly List<LockWeapon> _availableLockingWeapons = new List<LockWeapon>();
|
||||
|
||||
public CombatState(Agent agent) : base(agent)
|
||||
{
|
||||
SampleDps();
|
||||
foreach (var weapon in _agent.Ship.Weapons)
|
||||
{
|
||||
weapon.Item.OnDisable += SampleDps;
|
||||
weapon.Item.OnEnable += SampleDps;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var target = _agent.Ship.Target.Value;
|
||||
if (target == null) return;
|
||||
|
||||
_availableGroups.Clear();
|
||||
_availableLockingWeapons.Clear();
|
||||
|
||||
var toTarget = target.Position - _agent.Ship.Position;
|
||||
var targetDistance = length(toTarget);
|
||||
for (var i = 0; i < _agent.Ship.TriggerGroups.Length; i++)
|
||||
{
|
||||
var group = _agent.Ship.TriggerGroups[i];
|
||||
var dps = 0f;
|
||||
foreach (var weapon in group.weapons)
|
||||
if (weapon.Item.Online &&
|
||||
weapon.MinRange < targetDistance &&
|
||||
targetDistance < weapon.Range &&
|
||||
(weapon is ConstantWeapon ||
|
||||
weapon is InstantWeapon instantWeapon && instantWeapon.CanFire))
|
||||
{
|
||||
if(weapon is LockWeapon lockWeapon)
|
||||
_availableLockingWeapons.Add(lockWeapon);
|
||||
else
|
||||
dps += weapon.RangeDamagePerSecond(targetDistance);
|
||||
}
|
||||
|
||||
if (dps > .1) _availableGroups.Add((i, dps));
|
||||
}
|
||||
|
||||
foreach(var w in _availableLockingWeapons) w.Activate();
|
||||
|
||||
var selectedGroup = -1;
|
||||
var maxDps = Single.MinValue;
|
||||
foreach (var group in _availableGroups)
|
||||
{
|
||||
if (group.dps > maxDps)
|
||||
{
|
||||
maxDps = group.dps;
|
||||
selectedGroup = group.index;
|
||||
}
|
||||
}
|
||||
|
||||
var targetRight = target.Direction.Rotate(ItemRotation.Clockwise);
|
||||
var optimumRangeDelta = abs(_optimumRange - targetDistance);
|
||||
var directionToTarget = normalize(toTarget.xz);
|
||||
var targetPortAlignment = dot(targetRight, directionToTarget);
|
||||
var targetForeAlignment = dot(-target.Direction, directionToTarget);
|
||||
var forwardness = saturate(optimumRangeDelta / _agent.Settings.AgentMaxForwardDistance) * _agent.Settings.AgentForwardLerp;
|
||||
if (targetForeAlignment > 0) forwardness = lerp(forwardness, 1, pow(targetPortAlignment, 2));
|
||||
|
||||
var movementDirection = normalize(lerp(
|
||||
directionToTarget.Rotate(targetPortAlignment > 0 ? ItemRotation.Clockwise : ItemRotation.CounterClockwise),
|
||||
targetDistance > _optimumRange ? directionToTarget : -directionToTarget, forwardness));
|
||||
|
||||
if (selectedGroup >= 0)
|
||||
{
|
||||
var testWeapon = _agent.Ship.TriggerGroups[selectedGroup].weapons.First();
|
||||
if(testWeapon.Velocity > 1)
|
||||
{
|
||||
var targetHullData = _agent.ItemManager.GetData(target.Hull) as HullData;
|
||||
var targetVelocity = float3(target.Velocity.x, 0, target.Velocity.y);
|
||||
var predictedPosition = AetheriaMath.FirstOrderIntercept(
|
||||
_agent.Ship.Position,
|
||||
float3.zero,
|
||||
testWeapon.Velocity,
|
||||
target.Position,
|
||||
targetVelocity
|
||||
);
|
||||
predictedPosition.y = _agent.Ship.Zone.GetHeight(predictedPosition.xz) + targetHullData.GridOffset;
|
||||
toTarget = normalize(predictedPosition - _agent.Ship.Position);
|
||||
}
|
||||
|
||||
var shouldFire = dot(
|
||||
_agent.Ship.HardpointTransforms[_agent.Ship.Hardpoints[testWeapon.Item.Position.x, testWeapon.Item.Position.y]].direction,
|
||||
toTarget) > .99f;
|
||||
foreach (var weapon in _agent.Ship.TriggerGroups[selectedGroup].weapons)
|
||||
{
|
||||
if (shouldFire)
|
||||
weapon.Activate();
|
||||
else if (weapon.Firing)
|
||||
weapon.Deactivate();
|
||||
}
|
||||
}
|
||||
_agent.Ship.LookDirection = toTarget;
|
||||
_agent.Accelerate(movementDirection * _agent.TopSpeed, true);//selectedGroup >= 0);
|
||||
|
||||
// Fire charged guns!
|
||||
}
|
||||
|
||||
private void SampleDps()
|
||||
{
|
||||
var minRange = Single.MaxValue;
|
||||
var maxRange = Single.MinValue;
|
||||
foreach (var weapon in _agent.Ship.Weapons)
|
||||
{
|
||||
if (weapon.Range < minRange)
|
||||
minRange = weapon.Range;
|
||||
if (weapon.Range > maxRange)
|
||||
maxRange = weapon.Range;
|
||||
}
|
||||
|
||||
var offset = _agent.ItemManager.Random.NextFloat((maxRange - minRange) / DPS_SAMPLE_COUNT);
|
||||
var optimumDPS = 0f;
|
||||
for (int i = 0; i < DPS_SAMPLE_COUNT; i++)
|
||||
{
|
||||
var range = offset + minRange + (maxRange - minRange) * ((float) i / DPS_SAMPLE_COUNT);
|
||||
float dps = 0;
|
||||
foreach (var weapon in _agent.Ship.Weapons)
|
||||
{
|
||||
if (weapon.Item.Online && weapon.MinRange < range && range < weapon.Range)
|
||||
dps += weapon.RangeDamagePerSecond(range);
|
||||
}
|
||||
|
||||
// Multiply by range raised to a (sublinear) exponent to bias DPS preference towards longer ranges
|
||||
dps *= pow(range, _agent.Settings.AgentRangeExponent);
|
||||
|
||||
if (dps > optimumDPS)
|
||||
{
|
||||
optimumDPS = dps;
|
||||
_optimumRange = range;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Agents/States/Combat.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Agents/States/Combat.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 059405941f2c482c8476a4b28196568e
|
||||
timeCreated: 1613189894
|
||||
32
Assets/Scripts/ServerShared/Agents/States/MatchVelocity.cs
Normal file
32
Assets/Scripts/ServerShared/Agents/States/MatchVelocity.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
public abstract class MatchVelocityState : BaseState
|
||||
{
|
||||
protected abstract float2 TargetVelocity { get; }
|
||||
|
||||
protected MatchVelocityState(Agent agent) : base(agent) { }
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
_agent.Accelerate(TargetVelocity);
|
||||
}
|
||||
|
||||
public float2 MatchDistanceTime
|
||||
{
|
||||
get
|
||||
{
|
||||
var velocity = length(_agent.Ship.Velocity);
|
||||
var deltaV = TargetVelocity - _agent.Ship.Velocity;
|
||||
|
||||
var stoppingTime = length(deltaV) / (_agent.Ship.ForwardThrust / _agent.Ship.Mass);
|
||||
var stoppingDistance = stoppingTime * (velocity / 2);
|
||||
|
||||
var turnaroundTime = _agent.Ship.TurnTime(deltaV);
|
||||
var turnaroundDistance = turnaroundTime * velocity;
|
||||
|
||||
return float2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b584acceb113424a8fb7d1982e27b2db
|
||||
timeCreated: 1613185253
|
||||
44
Assets/Scripts/ServerShared/Agents/States/MoveTo.cs
Normal file
44
Assets/Scripts/ServerShared/Agents/States/MoveTo.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
public abstract class MoveToState : BaseState
|
||||
{
|
||||
private VelocityLimit _velocityLimit;
|
||||
protected abstract float2 TargetPosition { get; }
|
||||
public float Distance { get; private set; }
|
||||
|
||||
protected MoveToState(Agent agent) : base(agent)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
var diff = TargetPosition - _agent.Ship.Position.xz;
|
||||
var dir = normalize(diff);
|
||||
Distance = length(diff);
|
||||
|
||||
// We want to go top speed in the direction of our target
|
||||
var desiredVelocity = dir * _agent.TopSpeed;
|
||||
_agent.Ship.LookDirection = float3(dir.x, 0, dir.y);
|
||||
_agent.Accelerate(desiredVelocity);
|
||||
}
|
||||
}
|
||||
|
||||
public class MoveToEntityState : MoveToState
|
||||
{
|
||||
public Entity TargetEntity { get; set; }
|
||||
protected override float2 TargetPosition => TargetEntity?.Position.xz ?? float2.zero;
|
||||
|
||||
public MoveToEntityState(Agent agent) : base(agent) { }
|
||||
}
|
||||
|
||||
public class MoveToOrbitState : MoveToState
|
||||
{
|
||||
public Guid Orbit { get; set; }
|
||||
public MoveToOrbitState(Agent agent) : base(agent) { }
|
||||
|
||||
protected override float2 TargetPosition => _agent.Ship.Zone.GetOrbitPosition(Orbit);
|
||||
}
|
||||
3
Assets/Scripts/ServerShared/Agents/States/MoveTo.cs.meta
Normal file
3
Assets/Scripts/ServerShared/Agents/States/MoveTo.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ddb2b91d6c3340ccadfc7c20020846e8
|
||||
timeCreated: 1613182864
|
||||
3
Assets/Scripts/ServerShared/Agents/Tasks.meta
Normal file
3
Assets/Scripts/ServerShared/Agents/Tasks.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b9d106c37ec34e29aab0c8ee52a7ed5d
|
||||
timeCreated: 1613182162
|
||||
|
|
@ -40,8 +40,3 @@ public enum TaskType
|
|||
Explore
|
||||
}
|
||||
|
||||
public class PatrolOrbitsTask : AgentTask
|
||||
{
|
||||
public override TaskType Type => TaskType.Defend;
|
||||
public Guid[] Circuit;
|
||||
}
|
||||
31
Assets/Scripts/ServerShared/Agents/Tasks/PatrolOrbits.cs
Normal file
31
Assets/Scripts/ServerShared/Agents/Tasks/PatrolOrbits.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
|
||||
public class PatrolOrbitsTask : AgentTask
|
||||
{
|
||||
public override TaskType Type => TaskType.Defend;
|
||||
public Guid[] Circuit;
|
||||
}
|
||||
|
||||
public class PatrolOrbitsState : BaseState
|
||||
{
|
||||
public PatrolOrbitsTask Task;
|
||||
public Guid CurrentTarget
|
||||
{
|
||||
get => Task.Circuit[_currentTargetIndex];
|
||||
}
|
||||
private int _currentTargetIndex;
|
||||
public void NextTarget()
|
||||
{
|
||||
_currentTargetIndex++;
|
||||
_currentTargetIndex %= Task.Circuit.Length;
|
||||
}
|
||||
|
||||
public PatrolOrbitsState(Agent agent) : base(agent)
|
||||
{
|
||||
var patrolMoveState = new MoveToOrbitState(agent);
|
||||
Transitions.Add(new StateTransition(patrolMoveState,
|
||||
() => true,
|
||||
() => patrolMoveState.Orbit = CurrentTarget));
|
||||
patrolMoveState.Transitions.Add(new StateTransition(this, () => patrolMoveState.Distance < 10, NextTarget));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dcfc94fde9bb4aa2ac3c7d88180689cb
|
||||
timeCreated: 1613182188
|
||||
|
|
@ -1,63 +0,0 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
using float2 = Unity.Mathematics.float2;
|
||||
|
||||
public class VelocityMatch : AgentBehavior
|
||||
{
|
||||
public event Action OnMatch;
|
||||
public float2 TargetVelocity;
|
||||
private Thruster _thrust;
|
||||
private const float TargetThreshold = .1f;
|
||||
|
||||
private ItemManager _context;
|
||||
|
||||
public VelocityMatch(ItemManager context, Entity entity, ControllerData controllerData) : base(context, entity, controllerData)
|
||||
{
|
||||
_context = context;
|
||||
_thrust = Entity.GetBehavior<Thruster>();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
OnMatch = null;
|
||||
}
|
||||
|
||||
public override void Update(float delta)
|
||||
{
|
||||
// if (_thrust != null && _turning != null)
|
||||
// {
|
||||
// var deltaV = TargetVelocity - Entity.Velocity;
|
||||
// if(length(deltaV) < TargetThreshold)
|
||||
// OnMatch?.Invoke();
|
||||
// _turning.Axis = TurningInput(deltaV);
|
||||
// _thrust.Axis = ThrustInput(deltaV);
|
||||
// }
|
||||
}
|
||||
|
||||
public float2 MatchDistanceTime
|
||||
{
|
||||
get
|
||||
{
|
||||
// var velocity = length(Entity.Velocity);
|
||||
// var deltaV = TargetVelocity - Entity.Velocity;
|
||||
//
|
||||
// var stoppingTime = length(deltaV) / (_thrust.Thrust / Entity.Mass);
|
||||
// var stoppingDistance = stoppingTime * (velocity / 2);
|
||||
//
|
||||
// var angleDiff = Entity.Direction.AngleDiff(deltaV);
|
||||
// var turnaroundTime = angleDiff / (_turning.Torque / Entity.Mass);
|
||||
// var turnaroundDistance = turnaroundTime * velocity;
|
||||
//
|
||||
// return float2(stoppingDistance + turnaroundDistance, stoppingTime + turnaroundTime);
|
||||
return float2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
fileFormatVersion: 2
|
||||
guid: df5185d5435658841878b6a8c9ff693b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue