Fixes? and side channels.
This commit is contained in:
@ -30,8 +30,7 @@ namespace Animators.Leonid_Animator.Bot
|
||||
public void UpdateAnimatorValues()
|
||||
{
|
||||
var movementDir = _movementController.Velocity;
|
||||
|
||||
_myAnimatorHandler.UpdateAnimatorValues(movementDir.y, movementDir.x,
|
||||
_myAnimatorHandler.UpdateAnimatorValues( Mathf.Clamp01(movementDir.magnitude), 0,
|
||||
false, _npc.NpcBodyState is NpcCrouchingState, _npc.IsFiring);
|
||||
}
|
||||
}
|
||||
|
@ -29,11 +29,6 @@ public class CharacterFactory : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
print(Player == null);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
var attcNum = SettingsReader.Instance.GetSettings.NumOfAttackers;
|
||||
@ -69,8 +64,7 @@ public class CharacterFactory : MonoBehaviour
|
||||
Quaternion.identity);
|
||||
ApplyMaterial(team, gameobject);
|
||||
gameobject.SetActive(true);
|
||||
gameObject.tag = team == Team.Attackers ? "Attacker" : "Defender";
|
||||
|
||||
gameobject.tag = team == Team.Attackers ? "Attacker" : "Defender";
|
||||
if (typeAi == TypeAI.HumanAI)
|
||||
{
|
||||
print("added player to list");
|
||||
|
@ -6,6 +6,7 @@ using UnityEngine.AI;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
[RequireComponent(typeof(NPC))]
|
||||
public class MovementController : MonoBehaviour
|
||||
{
|
||||
private Transform _firePointTransform;
|
||||
@ -21,12 +22,13 @@ public class MovementController : MonoBehaviour
|
||||
public float RemainingDistance => navMeshAgent.remainingDistance;
|
||||
public Vector3 Velocity => navMeshAgent.velocity;
|
||||
private Dictionary<int, NavPoint> _idNavPointDict;
|
||||
|
||||
private NPC _myNpc;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
navMeshAgent.speed = SettingsReader.Instance.GetSettings.MovementSpeed;
|
||||
_idNavPointDict = MapManager.Instance.IDToNavPoint;
|
||||
_myNpc = GetComponent<NPC>();
|
||||
_firePointTransform = transform.GetChild(0);
|
||||
InvokeRepeating(nameof(UpdateFlagPosition), 0, UpdateFlagPositionDelay);
|
||||
InvokeRepeating(nameof(ReachedDestination), 0, UpdateReachedDestinationDelay);
|
||||
@ -38,11 +40,25 @@ public class MovementController : MonoBehaviour
|
||||
CancelInvoke(nameof(ReachedDestination));
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
print($"{_myNpc.NpcState.ToString()}, {_myNpc.GetCharacter.Team}");
|
||||
if (Velocity.magnitude > 0)
|
||||
_myNpc.ChangeBaseState(NpcEnumState.InRunning);
|
||||
else
|
||||
{
|
||||
_myNpc.ChangeBaseState(_idNavPointDict[PointStartID].navType == NavPointType.Cover
|
||||
? NpcEnumState.InCover
|
||||
: NpcEnumState.InDirectPoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateFlagPosition()
|
||||
{
|
||||
FlagDistance = (flag.transform.position - gameObject.transform.position).magnitude;
|
||||
}
|
||||
|
||||
[Obsolete]
|
||||
public void MoveToRandomPoint()
|
||||
{
|
||||
GoToNextNavPoint(MapManager.Instance.NavPoints[Random.Range(0, MapManager.Instance.NavPoints.Count)]);
|
||||
@ -79,7 +95,9 @@ public class MovementController : MonoBehaviour
|
||||
|
||||
public void ReachedDestination()
|
||||
{
|
||||
if ((navMeshAgent.isStopped == false) && (navMeshAgent.velocity.magnitude < 0.1))
|
||||
if (navMeshAgent.remainingDistance < float.Epsilon)
|
||||
{
|
||||
PointStartID = PointEndID;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Animators.Leonid_Animator;
|
||||
using Animators.Leonid_Animator.Bot;
|
||||
using Unity.MLAgents;
|
||||
using Unity.MLAgents.Actuators;
|
||||
@ -15,9 +13,8 @@ using UnityEngine;
|
||||
public class NPC : Agent, ICharacter
|
||||
{
|
||||
private CharacterCondition _condition;
|
||||
private FlagZone _flagZone = null;
|
||||
|
||||
private INpcBaseState NpcState { get; set; }
|
||||
private FlagZone _flagZone;
|
||||
public INpcBaseState NpcState { get; set; }
|
||||
public INpcBaseBodyState NpcBodyState { get; private set; }
|
||||
|
||||
[field: HideInInspector]
|
||||
@ -38,6 +35,22 @@ public class NPC : Agent, ICharacter
|
||||
private Dictionary<int, NavPoint> _navPointIdDict;
|
||||
|
||||
public bool IsFiring => _assistant.fireAnimation;
|
||||
|
||||
public void ChangeBaseState(NpcEnumState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case NpcEnumState.InCover:
|
||||
NpcState = _coverState;
|
||||
break;
|
||||
case NpcEnumState.InRunning:
|
||||
NpcState = _runningState;
|
||||
break;
|
||||
case NpcEnumState.InDirectPoint:
|
||||
NpcState = _directState;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#region UnityEvents and ML
|
||||
private void Awake()
|
||||
@ -89,7 +102,6 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
public override void CollectObservations(VectorSensor sensor)
|
||||
{
|
||||
Debug.Log("Collect observations called!");
|
||||
_navPointIdDict = MapManager.Instance.IDToNavPoint;
|
||||
if (_navPointIdDict is null)
|
||||
Debug.LogError("Cant Find Nav Point Dictionary");
|
||||
@ -109,8 +121,8 @@ public class NPC : Agent, ICharacter
|
||||
sensor.AddObservation((int)NpcState.State);
|
||||
sensor.AddObservation((int)NpcBodyState.State);
|
||||
sensor.AddObservation(GameManager.IsEnemyNearby(gameObject.transform.position, GetCharacter.Team));
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointStartID].DeathAttr);
|
||||
sensor.AddObservation(_navPointIdDict[_moveController.PointEndID].DeathAttr);
|
||||
//sensor.AddObservation(_navPointIdDict[_moveController.PointStartID].DeathAttr);
|
||||
//sensor.AddObservation(_navPointIdDict[_moveController.PointEndID].DeathAttr);
|
||||
sensor.AddObservation(_moveController.FlagDistance);
|
||||
|
||||
//point sensors
|
||||
@ -134,7 +146,6 @@ public class NPC : Agent, ICharacter
|
||||
public override void OnActionReceived(ActionBuffers actions)
|
||||
{
|
||||
var result = actions.DiscreteActions;
|
||||
print(result[0]);
|
||||
if (result[0] == 0)
|
||||
{
|
||||
if (_navPointIdDict[_moveController.PointStartID].navType != NavPointType.Cover)
|
||||
@ -159,7 +170,7 @@ public class NPC : Agent, ICharacter
|
||||
switch (result[1])
|
||||
{
|
||||
case 0: _moveController.GoToNextNavPoint(_navPointIdDict[result[2]]);
|
||||
NpcState = _runningState; Debug.Log("Go to point " + result[2]);break;
|
||||
NpcState = _runningState; break;
|
||||
case 1: NpcState = _directState; break;
|
||||
case 2: break;
|
||||
case 3: break;
|
||||
@ -180,6 +191,7 @@ public class NPC : Agent, ICharacter
|
||||
default: throw new ArgumentException("Undefined Action received");
|
||||
}
|
||||
}
|
||||
AddReward(-0.001f);
|
||||
}
|
||||
#endregion
|
||||
|
||||
@ -204,6 +216,7 @@ public class NPC : Agent, ICharacter
|
||||
_condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - _condition.ArmourPoints * 0.5f)));
|
||||
_condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
OnDamageReceived?.Invoke(damage, GetCharacter.Team);
|
||||
AddReward(-0.03f);
|
||||
if (_condition.HealthPoints < 1)
|
||||
{
|
||||
Die();
|
||||
@ -212,6 +225,7 @@ public class NPC : Agent, ICharacter
|
||||
|
||||
private void Die()
|
||||
{
|
||||
AddReward(-0.2f);
|
||||
OnDeathEvent?.Invoke(true);
|
||||
MapManager.AddDeathAttributeToPoints(_moveController.PointStartID, _moveController.PointEndID,
|
||||
_moveController.DistanceToGo, _moveController.RemainingDistance);
|
||||
|
@ -26,8 +26,8 @@ public class Player : MonoBehaviour, ICharacter
|
||||
PlayerCharacter.LastTimeHit = TimeManager.Instance.CurrentTime;
|
||||
Condition.GiveHealth(-Mathf.RoundToInt(damage * (1 - Condition.ArmourPoints * 0.5f)));
|
||||
Condition.GiveArmour(-Mathf.RoundToInt(Mathf.Sqrt(damage) * 5));
|
||||
|
||||
if (Condition.HealthPoints < 1)
|
||||
print(Condition.HealthPoints);
|
||||
if (Condition.HealthPoints < 10)
|
||||
Die();
|
||||
}
|
||||
|
||||
|
@ -119,10 +119,14 @@ public class GameManager : MonoBehaviour
|
||||
{
|
||||
case Team.Attackers:
|
||||
Debug.Log("Attackers Win");
|
||||
_attackersTeam.AddGroupReward(1f);
|
||||
_defendersTeam.AddGroupReward(-1f);
|
||||
ResetScene();
|
||||
break;
|
||||
case Team.Defenders:
|
||||
Debug.Log("Defenders Win");
|
||||
_defendersTeam.AddGroupReward(1f);
|
||||
_attackersTeam.AddGroupReward(-1f);
|
||||
ResetScene();
|
||||
break;
|
||||
}
|
||||
@ -130,6 +134,8 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
private void TimeOut()
|
||||
{
|
||||
_attackersTeam.AddGroupReward(-1f);
|
||||
_defendersTeam.AddGroupReward(-1f);
|
||||
ResetScene();
|
||||
}
|
||||
|
||||
|
@ -7,13 +7,11 @@ public class GlobalEventManager
|
||||
public static void SendCaptureFlag(Team team)
|
||||
{
|
||||
OnCaptureFlag?.Invoke(team);
|
||||
OnCaptureFlag = null;
|
||||
}
|
||||
|
||||
public static event Action OnTimeLeft;
|
||||
public static void SendTimeout()
|
||||
{
|
||||
OnTimeLeft?.Invoke();
|
||||
OnTimeLeft = null;
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ public class MapManager : MonoBehaviour
|
||||
var startNavPoint = _instance.IDToNavPoint[startPoint];
|
||||
var endNavPoint = _instance.IDToNavPoint[endPoint];
|
||||
float coef;
|
||||
if (allDistance != 0)
|
||||
if (Mathf.Abs(allDistance) > float.Epsilon)
|
||||
{
|
||||
coef = remainingDistance / allDistance;
|
||||
startNavPoint.DeathAttr += 1 - coef;
|
||||
|
@ -1,8 +1,8 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class FlagZone : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
public float TimeStayAttackers { get; private set; }
|
||||
public float TimeStayDefenders { get; private set; }
|
||||
private int occupDefenders;
|
||||
@ -13,7 +13,6 @@ public class FlagZone : MonoBehaviour
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
timeForWin = SettingsReader.Instance.GetSettings.TimeToWin;
|
||||
TimeStayAttackers = 0;
|
||||
TimeStayDefenders = 0;
|
||||
@ -30,8 +29,12 @@ public class FlagZone : MonoBehaviour
|
||||
case "Attacker":
|
||||
occupAttackers++;
|
||||
break;
|
||||
default:
|
||||
print(other.tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
switch (other.tag)
|
||||
@ -50,19 +53,32 @@ public class FlagZone : MonoBehaviour
|
||||
{
|
||||
TimeStayAttackers = 0;
|
||||
TimeStayDefenders = 0;
|
||||
return;
|
||||
}
|
||||
else if (occupAttackers > 0)
|
||||
{
|
||||
TimeStayAttackers += Time.deltaTime;
|
||||
GameManager._attackersTeam.AddGroupReward(Time.deltaTime* 0.1f);
|
||||
GameManager._defendersTeam.AddGroupReward(-Time.deltaTime* 0.1f);
|
||||
if (TimeStayAttackers > timeForWin)
|
||||
{
|
||||
GlobalEventManager.SendCaptureFlag(Team.Attackers);
|
||||
NullifyFlagCapture();
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (occupDefenders > 0)
|
||||
{
|
||||
TimeStayDefenders += Time.deltaTime;
|
||||
if (TimeStayDefenders > timeForWin)
|
||||
{
|
||||
GlobalEventManager.SendCaptureFlag(Team.Defenders);
|
||||
NullifyFlagCapture();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void NullifyFlagCapture()
|
||||
{
|
||||
TimeStayAttackers = 0;
|
||||
TimeStayDefenders = 0;
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ public class Settings : ScriptableObject
|
||||
public float DoDamageChanceInCover;
|
||||
|
||||
public int RifleDamage;
|
||||
public int RateOfFire;
|
||||
public float RateOfFire;
|
||||
|
||||
public float CrouchingCoefficient;
|
||||
|
||||
|
@ -1,19 +1,29 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using Unity.MLAgents.SideChannels;
|
||||
using UnityEngine;
|
||||
|
||||
public class Logger
|
||||
namespace Statistics
|
||||
{
|
||||
private const string Directory = "/Logs/";
|
||||
private const string BaseName = "Log#";
|
||||
|
||||
public static void SaveLog<T>(T objToSerialize)
|
||||
public class LoggerSideChannel : SideChannel
|
||||
{
|
||||
var dir = Application.persistentDataPath + Directory;
|
||||
if (!System.IO.Directory.Exists(dir))
|
||||
System.IO.Directory.CreateDirectory(dir);
|
||||
public LoggerSideChannel()
|
||||
{
|
||||
ChannelId = new Guid("621f0a70-4f87-11ea-a6bf-784f4387d1f7");
|
||||
}
|
||||
|
||||
var logName = BaseName + (System.IO.Directory.GetFiles(dir).Length + 1).ToString();
|
||||
var json = JsonUtility.ToJson(objToSerialize);
|
||||
File.WriteAllText(dir + logName, json);
|
||||
public void SendLog(Log log)
|
||||
{
|
||||
using (var logOut = new OutgoingMessage())
|
||||
{
|
||||
logOut.WriteString(JsonUtility.ToJson(log));
|
||||
QueueMessageToSend(logOut);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMessageReceived(IncomingMessage msg)
|
||||
{
|
||||
var receivedString = msg.ReadString();
|
||||
Debug.Log("From Python : " + receivedString);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using Statistics;
|
||||
using UnityEngine;
|
||||
|
||||
internal class Log
|
||||
public class Log
|
||||
{
|
||||
public int damageTakenByDefs = 0;
|
||||
public int damageTakenByAtc = 0;
|
||||
@ -13,7 +15,8 @@ internal class Log
|
||||
|
||||
public class StatisticManager : MonoBehaviour
|
||||
{
|
||||
private Log log = new Log();
|
||||
private Log _log = new Log();
|
||||
private LoggerSideChannel _myLoggerSideChannel;
|
||||
private void Awake()
|
||||
{
|
||||
foreach (var npc in GameObject.FindObjectsOfType<NPC>())
|
||||
@ -21,31 +24,32 @@ public class StatisticManager : MonoBehaviour
|
||||
|
||||
GlobalEventManager.OnCaptureFlag += RegisterWin;
|
||||
GlobalEventManager.OnTimeLeft += RegisterTimeOut;
|
||||
GameManager.OnResetScene += SendMessage;
|
||||
}
|
||||
|
||||
private void RegisterDamage(int damage, Team team)
|
||||
{
|
||||
if (team == Team.Attackers)
|
||||
log.damageTakenByAtc += damage;
|
||||
_log.damageTakenByAtc += damage;
|
||||
else
|
||||
log.damageTakenByDefs += damage;
|
||||
_log.damageTakenByDefs += damage;
|
||||
}
|
||||
|
||||
private void RegisterWin(Team team)
|
||||
{
|
||||
if (team == Team.Attackers)
|
||||
log.AtcWin += 1;
|
||||
_log.AtcWin += 1;
|
||||
else
|
||||
log.DefWin += 1;
|
||||
_log.DefWin += 1;
|
||||
}
|
||||
|
||||
private void RegisterTimeOut()
|
||||
{
|
||||
log.TimeOuts += 1;
|
||||
_log.TimeOuts += 1;
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
private void SendMessage()
|
||||
{
|
||||
Logger.SaveLog<Log>(log);
|
||||
_myLoggerSideChannel.SendLog(_log);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ public class AimAssistant : MonoBehaviour
|
||||
[HideInInspector] public GameObject enemy;
|
||||
private Shooting _shooting;
|
||||
public bool isFiring = false;
|
||||
private ICharacter _myNpc;
|
||||
private NPC _myNpc;
|
||||
[SerializeField] private float lookSpeed = 400f;
|
||||
private Transform _myTransform;
|
||||
private float _fireCountdown;
|
||||
@ -39,7 +39,11 @@ public class AimAssistant : MonoBehaviour
|
||||
{
|
||||
enemy = enemies[new System.Random().Next(enemies.Count)];
|
||||
var character = enemy.GetComponent<ICharacter>();
|
||||
character.OnDeathEvent += _ => isFiring = false;
|
||||
character.OnDeathEvent += _ =>
|
||||
{
|
||||
isFiring = false;
|
||||
_myNpc.AddReward(0.2f);
|
||||
};
|
||||
isFiring = true;
|
||||
}
|
||||
}
|
||||
@ -88,7 +92,7 @@ public class AimAssistant : MonoBehaviour
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_shooting.Shoot();
|
||||
_shooting.BotShoot();
|
||||
yield return new WaitForSeconds(_fireCountdown);
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,31 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class Shooting : MonoBehaviour
|
||||
{
|
||||
public GameObject raycast;
|
||||
|
||||
public GameObject firePoint;
|
||||
//[SerializeField] private ParticleSystem projectilePrefab;
|
||||
|
||||
public ParticleSystem projectilePrefab;
|
||||
|
||||
|
||||
private float hSliderValue = 0.1f;
|
||||
private float _fireCountdown = 0.1f;
|
||||
|
||||
public GameObject gun;
|
||||
public AudioSource audioSource;
|
||||
private NPC _myNpc;
|
||||
private MovementController _moveCtrl;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
TryGetComponent(out _moveCtrl);
|
||||
TryGetComponent(out _myNpc);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
@ -30,6 +40,7 @@ public class Shooting : MonoBehaviour
|
||||
_fireCountdown += hSliderValue;
|
||||
PlayerShoot();
|
||||
}
|
||||
|
||||
_fireCountdown -= Time.deltaTime;
|
||||
}
|
||||
|
||||
@ -37,7 +48,7 @@ public class Shooting : MonoBehaviour
|
||||
{
|
||||
audioSource.Play();
|
||||
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
raycast.transform.forward, out var hit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
@ -47,18 +58,33 @@ public class Shooting : MonoBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Shoot()
|
||||
|
||||
public void BotShoot()
|
||||
{
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
if (Physics.Raycast(raycast.transform.position,
|
||||
raycast.transform.forward, out var hit,
|
||||
SettingsReader.Instance.GetSettings.ViewDistance))
|
||||
{
|
||||
if (hit.transform.TryGetComponent<NPC>(out var target))
|
||||
if (hit.transform.TryGetComponent<ICharacter>(out var target))
|
||||
{
|
||||
audioSource.Play();
|
||||
Instantiate(projectilePrefab, firePoint.transform.position, firePoint.transform.rotation);
|
||||
var mySpeed = _moveCtrl.Velocity.magnitude;
|
||||
var enemySpeed = 0f;
|
||||
var inCover = false;
|
||||
if (target.GetCharacter.TypeAi == TypeAI.HumanAI)
|
||||
enemySpeed = hit.rigidbody.velocity.magnitude;
|
||||
else
|
||||
{
|
||||
enemySpeed = hit.collider.GetComponent<MovementController>().Velocity.magnitude;
|
||||
inCover = hit.collider.GetComponent<NPC>().NpcState.InCover;
|
||||
}
|
||||
|
||||
var hitChance = (1 - 0.5 * mySpeed) * (1 - 0.5 * enemySpeed + 0.5*inCover.ToInt()) / 1.5f;
|
||||
if (!(UnityEngine.Random.Range(0f, 1f) < hitChance)) return;
|
||||
_myNpc.AddReward(0.05f);
|
||||
target.GetDamage(SettingsReader.Instance.GetSettings.RifleDamage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user