to new git
This commit is contained in:
@ -1,108 +1,170 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using Unity.MLAgents.Actuators;
|
||||
using Unity.MLAgents.Sensors;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(MovementController))]
|
||||
[RequireComponent(typeof(MovementController),typeof(BufferSensor))]
|
||||
public class NPC : Agent, ICharacter
|
||||
{
|
||||
[HideInInspector]
|
||||
public Character AgentCharacter;
|
||||
private Character AgentCharacter;
|
||||
public CharacterCondition Condition;
|
||||
private FlagZone flagZone;
|
||||
private FlagZone flagZone = null;
|
||||
|
||||
public NPC_BaseState NPC_State { get; private set; }
|
||||
public INpcBaseState NpcState { get; private set; }
|
||||
public INpcBaseBodyState NpcBodyState { get; private set; }
|
||||
|
||||
public Character GetCharacter => AgentCharacter;
|
||||
|
||||
private NPC_DirectPointState DirectState;
|
||||
private NPC_InCoverState CoverState;
|
||||
private NPC_RunningState RunningState;
|
||||
private NpcDirectPointState DirectState;
|
||||
private NpcInCoverState CoverState;
|
||||
private NpcRunningState RunningState;
|
||||
|
||||
private NpcStandingState StandingState;
|
||||
private NpcCrouchingState CrouchingState;
|
||||
|
||||
private MovementController moveController;
|
||||
private BufferSensorComponent bufferSensor;
|
||||
|
||||
private Dictionary<int, NavPoint> navPointIdDict;
|
||||
|
||||
#region UnityEvents and ML
|
||||
private void Awake()
|
||||
{
|
||||
DirectState = new NPC_DirectPointState();
|
||||
CoverState = new NPC_InCoverState();
|
||||
RunningState = new NPC_RunningState();
|
||||
NPC_State = DirectState;
|
||||
DirectState = new NpcDirectPointState();
|
||||
CoverState = new NpcInCoverState();
|
||||
RunningState = new NpcRunningState();
|
||||
NpcState = DirectState;
|
||||
|
||||
CrouchingState = new NpcCrouchingState();
|
||||
StandingState = new NpcStandingState();
|
||||
NpcBodyState = StandingState;
|
||||
|
||||
AgentCharacter = new Character();
|
||||
Condition = AgentCharacter.Condition;
|
||||
|
||||
moveController = gameObject.GetComponent<MovementController>();
|
||||
bufferSensor = gameObject.GetComponent<BufferSensorComponent>();
|
||||
}
|
||||
|
||||
|
||||
public void ResetCharacter()
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
if (flagZone == null)
|
||||
Debug.LogError("Flag Is Not Setted");
|
||||
|
||||
navPointIdDict = MapManager.IDToNavPoint;
|
||||
if (navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Condition = new CharacterCondition();
|
||||
EndEpisode();
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
}
|
||||
|
||||
public override void OnEpisodeBegin()
|
||||
{
|
||||
NPC_State = DirectState;
|
||||
{
|
||||
NpcState = DirectState;
|
||||
flagZone = GameObject.FindObjectOfType<FlagZone>();
|
||||
}
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
var candidates = moveController.getPointsCandidate();
|
||||
var candidates = moveController.GetPointsCandidate();
|
||||
|
||||
sensor.AddObservation(Condition.HealthPoints);
|
||||
sensor.AddObservation(Condition.ArmourPoints);
|
||||
sensor.AddObservation(Condition.Ammunition);
|
||||
sensor.AddObservation((int)NPC_State.State);
|
||||
sensor.AddObservation((!flagZone.isNotOccup).ToInt());
|
||||
//common sensors
|
||||
sensor.AddObservation(GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
NpcBodyState.GetPointToHit(gameObject)).ToInt());
|
||||
sensor.AddObservation(AgentCharacter.LastTimeHit);
|
||||
sensor.AddObservation((!flagZone.IsNotOccup).ToInt());
|
||||
sensor.AddObservation(Condition.GetHealthPointsInQuantile());
|
||||
sensor.AddObservation(Condition.GetArmourPointsInQuantile());
|
||||
sensor.AddObservation(candidates.Count);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||
|
||||
sensor.AddObservation(moveController.PointStartID);
|
||||
sensor.AddObservation(moveController.PointEndID);
|
||||
//state sensors
|
||||
sensor.AddObservation((int)NpcState.State);
|
||||
sensor.AddObservation((int)NpcBodyState.State);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(navPointIdDict[moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(moveController.FlagDistance);
|
||||
|
||||
//point sensors
|
||||
foreach (var point in candidates)
|
||||
{
|
||||
Debug.Log((float)moveController.CurrentNavPoint.PointId);
|
||||
|
||||
bufferSensor.AppendObservation(new float[] {
|
||||
//1 position in navpointId
|
||||
(float)moveController.CurrentNavPoint.PointId,
|
||||
//2 distance to flag
|
||||
moveController.FlagDistance,
|
||||
//3 death count in point
|
||||
moveController.CurrentNavPoint.DeathAttr,
|
||||
point.DeathAttr,
|
||||
(int)point.navType,
|
||||
//4 flagEnemyDistance
|
||||
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position).ToInt(),
|
||||
//5 EnemyVsNavPointDistance
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team).ToInt()
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public override void Heuristic(in ActionBuffers actionsOut)
|
||||
{
|
||||
var discreteActionsOut = actionsOut.DiscreteActions;
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
discreteActionsOut[0] = 1;
|
||||
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team.GetOppositeTeam()).ToInt(),
|
||||
//6 Have been seen by enemy in this point
|
||||
GameManager.IsHaveSeenByEnemy(AgentCharacter.Team.GetOppositeTeam(),
|
||||
point.Position).ToInt()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnActionReceived(ActionBuffers actions)
|
||||
{
|
||||
if (actions.DiscreteActions[0] == 1)
|
||||
var result = actions.DiscreteActions;
|
||||
if (result[0] == 0)
|
||||
{
|
||||
moveController.MoveToRandomPoint();
|
||||
NPC_State = RunningState;
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Cover)
|
||||
return;
|
||||
NpcState = CoverState;
|
||||
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: Peek(); break;
|
||||
case 1: Cover(); break;
|
||||
case 3: Peek(); moveController.GoToNextNavPoint(navPointIdDict[result[2]]); break;
|
||||
case 4: NpcState = DirectState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
if (result[0] == 1)
|
||||
{
|
||||
if (navPointIdDict[moveController.PointStartID].navType != NavPointType.Direction)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.GoToNextNavPoint(navPointIdDict[result[2]]);
|
||||
NpcState = RunningState; break;
|
||||
case 1: NpcState = DirectState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
if (result[0] == 2)
|
||||
{
|
||||
if (moveController.PointStartID == moveController.PointEndID && moveController.PointEndID != -1)
|
||||
return;
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: moveController.StopOnPath(); NpcState = DirectState; break;
|
||||
case 1: moveController.ReturnToStartPoint(); NpcState = RunningState; break;
|
||||
default: throw new ArgumentException("Undefined Action recieved");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public event Action<object> OnKilledEvent;
|
||||
public event Action<NpcBodyState> OnChangePosition;
|
||||
private void Peek()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Standing);
|
||||
NpcBodyState = StandingState;
|
||||
}
|
||||
|
||||
private void Cover()
|
||||
{
|
||||
OnChangePosition?.Invoke(global::NpcBodyState.Crouching);
|
||||
NpcBodyState = CrouchingState;
|
||||
}
|
||||
|
||||
public event Action<int, Team> OnDamageRecieved;
|
||||
public void GetDamage(float damage)
|
||||
{
|
||||
AgentCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
@ -111,13 +173,17 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
if (Condition.HealthPoints < 0)
|
||||
{
|
||||
OnKilledEvent?.Invoke(this);
|
||||
moveController.CurrentNavPoint.DeathAttr += 1;
|
||||
MapManager.AddDeathAttributeToPoints(moveController.PointStartID, moveController.PointEndID,
|
||||
moveController.DistanceToGo, moveController.RemainingDistance);
|
||||
var pos = gameObject.transform.position;
|
||||
var id = moveController.PointStartID;
|
||||
CharacterFactory.Instance.ReSpawn(this, ref pos, ref id);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
public void ResetCharacter()
|
||||
{
|
||||
Debug.LogWarning("Pooled object was destroyed");
|
||||
Condition.Reset();
|
||||
EndEpisode();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user