update scripts

This commit is contained in:
2022-04-25 16:23:25 +07:00
parent e1d0bbc1eb
commit 290f5515b7
13 changed files with 120 additions and 47 deletions

View File

@ -21,6 +21,17 @@ public class CharacterCondition
OnChangeHealthEvent?.Invoke(value);
}
}
public int GetHealthPointsInQuantile()
{
if (health < 25)
return 0;
else if (health < 50)
return 1;
else if (health < 75)
return 2;
else return 3;
}
private int armour;
public int ArmourPoints
{
@ -51,9 +62,9 @@ public class CharacterCondition
public CharacterCondition()
{
var settings = SettingsReader.Instance.GetSettings;
ammo = settings.maxAmmo;
health = settings.maxHealth;
armour = settings.maxArmour;
ammo = settings.MaxAmmo;
health = settings.MaxHealth;
armour = settings.MaxArmour;
}
public void GiveHealth(int health) => HealthPoints = Mathf.Clamp(health + HealthPoints, 0, 100);

View File

@ -2,16 +2,31 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Threading.Tasks;
[RequireComponent(typeof(NavMeshAgent))]
public class MovementController : MonoBehaviour
{
public NavPoint currentPosition { get; set; }
public NavPoint CurrentNavPoint { get; set; }
public float FlagDistance { get; private set; }
private GameObject flag;
private const float updateFlagPositionDelay = 5;
[SerializeField] private NavMeshAgent navMeshAgent;
private void Start()
{
navMeshAgent.speed = SettingsReader.Instance.GetSettings.movementSpeed;
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
InvokeRepeating(nameof(UpdateFlagPosition), 0, updateFlagPositionDelay);
}
private void OnDestroy()
{
CancelInvoke(nameof(UpdateFlagPosition));
}
private void UpdateFlagPosition()
{
FlagDistance = (flag.transform.position - gameObject.transform.position).magnitude;
}
public void MoveToRandomPoint()
@ -23,7 +38,7 @@ public class MovementController : MonoBehaviour
public List<NavPoint> getPointsCandidate()
{
return MapManager.navPoints
.Where(point => (currentPosition.position - point.position).magnitude < SettingsReader.Instance.GetSettings.movementSpeed)
.Where(point => (CurrentNavPoint.position - point.position).magnitude < SettingsReader.Instance.GetSettings.MovementSpeed)
.ToList();
}

View File

@ -3,7 +3,6 @@ using UnityEngine;
using Unity.MLAgents;
using Unity.MLAgents.Sensors;
using Unity.MLAgents.Actuators;
using System.Collections.Generic;
[RequireComponent(typeof(MovementController))]
public class NPC : Agent, ICharacter
@ -11,6 +10,7 @@ public class NPC : Agent, ICharacter
[HideInInspector]
public Character AgentCharacter;
public CharacterCondition Condition;
private FlagZone flagZone;
public NPC_BaseState NPC_State { get; private set; }
@ -47,29 +47,38 @@ public class NPC : Agent, ICharacter
public override void OnEpisodeBegin()
{
NPC_State = DirectState;
flagZone = GameObject.FindObjectOfType<FlagZone>();
}
public override void CollectObservations(VectorSensor sensor)
{
var candidates = moveController.getPointsCandidate();
sensor.AddObservation(Condition.HealthPoints);
sensor.AddObservation(Condition.ArmourPoints);
sensor.AddObservation(Condition.Ammunition);
sensor.AddObservation((int)NPC_State.State);
var candidates = moveController.getPointsCandidate();
sensor.AddObservation((!flagZone.isNotOccup).ToInt());
sensor.AddObservation(AgentCharacter.LastTimeHit);
sensor.AddObservation(Condition.GetHealthPointsInQuantile());
sensor.AddObservation(candidates.Count);
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, AgentCharacter.Team));
foreach (var point in candidates)
{
Debug.Log((float)moveController.CurrentNavPoint.PointId);
bufferSensor.AppendObservation(new float[] {
//1 position in navpointId
(float)moveController.currentPosition.PointId,
(float)moveController.CurrentNavPoint.PointId,
//2 distance to flag
moveController.currentPosition.FlagDistance,
moveController.FlagDistance,
//3 death count in point
moveController.currentPosition.DeathAttr,
moveController.CurrentNavPoint.DeathAttr,
//4 flagEnemyDistance
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position)==true?1:0,
GameManager.IsCloserToFlagFromNextNavPoint(point, transform.position).ToInt(),
//5 EnemyVsNavPointDistance
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team)==true?1:0
GameManager.IsCloserToEnemyThanToNextNavPoint(point,transform.position, AgentCharacter.Team).ToInt()
});
}
@ -103,7 +112,7 @@ public class NPC : Agent, ICharacter
if (Condition.HealthPoints < 0)
{
OnKilledEvent?.Invoke(this);
moveController.currentPosition.DeathAttr += 1;
moveController.CurrentNavPoint.DeathAttr += 1;
}
}