551 lines
20 KiB
C#
551 lines
20 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class TargetController : MonoBehaviour
|
|
{
|
|
public GameObject environmentObj;
|
|
public GameObject agentObj;
|
|
public GameObject HUDObj;
|
|
public GameObject sceneBlockContainerObj;
|
|
public GameObject enemyContainerObj;
|
|
public GameObject parameterContainerObj;
|
|
public GameObject environmentUIObj;
|
|
public GameObject worldUIObj;
|
|
|
|
// area
|
|
public GameObject edgeUp;
|
|
public GameObject edgeDown;
|
|
public GameObject edgeLeft;
|
|
public GameObject edgeRight;
|
|
public GameObject edgeAgent_Enemy;
|
|
|
|
//group
|
|
public string group1Tag = "Player";
|
|
public string group2Tag = "Enemy";
|
|
|
|
public float minEnemyAreaX;
|
|
[System.NonSerialized] public float maxEnemyAreaX;
|
|
[System.NonSerialized] public float minEnemyAreaZ;
|
|
[System.NonSerialized] public float maxEnemyAreaZ;
|
|
[System.NonSerialized] public float minAgentAreaX;
|
|
[System.NonSerialized] public float maxAgentAreaX;
|
|
[System.NonSerialized] public float minAgentAreaZ;
|
|
[System.NonSerialized] public float maxAgentAreaZ;
|
|
[System.NonSerialized] public float startTime = 0f;
|
|
[System.NonSerialized] public float leftTime = 0f;
|
|
|
|
[SerializeField, Range(0f, 1f)] public float attackProb = 0.2f;
|
|
[SerializeField, Range(0f, 1f)] public float gotoProb = 0.2f;
|
|
[SerializeField, Range(0f, 1f)] public float defenceProb = 0.2f;
|
|
|
|
[System.NonSerialized] public int targetTypeInt;
|
|
public float[] targetState = new float[6];
|
|
|
|
public enum EndType
|
|
{ Win, Lose, Running, Num };
|
|
|
|
[System.NonSerialized] public int targetNum = 0;
|
|
private Dictionary<int, float[]> oneHotRarget = new Dictionary<int, float[]>();
|
|
|
|
private int inArea = 0;
|
|
private float freeProb;
|
|
private float sceneBlockSize;
|
|
private float lastDistance;
|
|
public Vector3 targetPosition;
|
|
private Vector3 targetLocalPosition;
|
|
private bool firstRewardFlag = true;
|
|
private bool targetEnemySpawnFinish = false;
|
|
private SceneBlockContainer blockCont;
|
|
private EnemyContainer enemyCont;
|
|
private EnvironmentUIControl envUICon;
|
|
private ParameterContainer paramCon;
|
|
private CharacterController agentCharaCon;
|
|
private WorldUIController worldUICon;
|
|
private HUDController hudCon;
|
|
private RaySensors raySensors;
|
|
|
|
// start scene datas 0=train 1=play
|
|
private int gamemode;
|
|
|
|
// Start is called before the first frame update
|
|
private void Start()
|
|
{
|
|
blockCont = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
|
envUICon = environmentUIObj.GetComponent<EnvironmentUIControl>();
|
|
enemyCont = enemyContainerObj.GetComponent<EnemyContainer>();
|
|
agentCharaCon = agentObj.GetComponent<CharacterController>();
|
|
paramCon = parameterContainerObj.GetComponent<ParameterContainer>();
|
|
worldUICon = worldUIObj.GetComponent<WorldUIController>();
|
|
hudCon = HUDObj.GetComponent<HUDController>();
|
|
raySensors = agentObj.GetComponent<RaySensors>();
|
|
|
|
// get parameter from ParameterContainer
|
|
gamemode = paramCon.gameMode;
|
|
attackProb = paramCon.attackProb;
|
|
gotoProb = paramCon.gotoProb;
|
|
defenceProb = paramCon.defenceProb;
|
|
|
|
// initialize spawn area
|
|
minEnemyAreaX = edgeLeft.transform.localPosition.x + 1.0f;
|
|
maxEnemyAreaX = edgeRight.transform.localPosition.x - 1.0f;
|
|
minEnemyAreaZ = edgeAgent_Enemy.transform.localPosition.z + 1.0f;
|
|
maxEnemyAreaZ = edgeUp.transform.localPosition.z - 1.0f;
|
|
|
|
minAgentAreaX = edgeLeft.transform.localPosition.x + 1.0f;
|
|
maxAgentAreaX = edgeRight.transform.localPosition.x - 1.0f;
|
|
minAgentAreaZ = edgeDown.transform.localPosition.z + 1.0f;
|
|
maxAgentAreaZ = edgeAgent_Enemy.transform.localPosition.z - 1.0f;
|
|
|
|
freeProb = 1 - attackProb - gotoProb - defenceProb;
|
|
targetNum = (int)SceneBlockContainer.Targets.Num;
|
|
if (freeProb < 0)
|
|
{
|
|
Debug.LogError("target percentage wrong");
|
|
}
|
|
|
|
// initialize a simple fake onehot encoder.
|
|
for (int i = 0; i < targetNum; i++)
|
|
{
|
|
float[] onehotList = new float[targetNum];
|
|
for (int j = 0; j < targetNum; j++)
|
|
{
|
|
onehotList[j] = 0;
|
|
}
|
|
onehotList[i] = 1;
|
|
oneHotRarget.Add(i, onehotList);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// if gamemode is play, then time will keep paramCon.timeLimit
|
|
if (gamemode == 1)
|
|
{
|
|
leftTime = paramCon.timeLimit;
|
|
// print out time
|
|
Debug.Log("Playing Time: " + leftTime);
|
|
}
|
|
else
|
|
{
|
|
leftTime = paramCon.timeLimit - Time.time + startTime;
|
|
}
|
|
}
|
|
|
|
public void RollNewScene()
|
|
{
|
|
startTime = Time.time;// Reset StartTime as now time
|
|
leftTime = paramCon.timeLimit - Time.time + startTime;
|
|
float randTargetType = UnityEngine.Random.Range(0f, 1f);
|
|
if (randTargetType <= gotoProb)
|
|
{
|
|
// goto target spawn
|
|
Debug.Log("GOTO THIS TARGET!");
|
|
SpawnSceneBlock(SceneBlockContainer.Targets.Go);
|
|
// set startDistance
|
|
firstRewardFlag = true;
|
|
}
|
|
else if (randTargetType > gotoProb && randTargetType <= gotoProb + attackProb)
|
|
{
|
|
// attack target spawn
|
|
Debug.Log("ATTACK!");
|
|
SpawnSceneBlock(SceneBlockContainer.Targets.Attack);
|
|
// set startDistance
|
|
firstRewardFlag = true;
|
|
targetEnemySpawnFinish = false;
|
|
}
|
|
else if (randTargetType > gotoProb + attackProb && randTargetType <= gotoProb + attackProb + defenceProb)
|
|
{
|
|
// defence target spawn
|
|
Debug.Log("DEFENCE!");
|
|
SpawnSceneBlock(SceneBlockContainer.Targets.Defence);
|
|
// set startDistance
|
|
firstRewardFlag = true;
|
|
}
|
|
else
|
|
{
|
|
//Debug.Log("Free");
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Free;
|
|
enemyCont.DestroyAllEnemys();
|
|
enemyCont.RandomInitEnemys(hudCon.enemyNum);
|
|
MoveAgentToSpwanArea();
|
|
blockCont.DestroyBlock();
|
|
}
|
|
UpdateTargetStates();
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
}
|
|
|
|
// get target observation states
|
|
private void UpdateTargetStates()
|
|
{
|
|
// targettype, x,y,z, firebasesAreaDiameter
|
|
targetState[0] = targetTypeInt;
|
|
if (targetTypeInt == (int)SceneBlockContainer.Targets.Free || targetTypeInt == (int)SceneBlockContainer.Targets.Stay)
|
|
{
|
|
for (int i = 1; i < targetState.Length; i++)
|
|
targetState[i] = 0f;
|
|
}
|
|
else
|
|
{
|
|
/* targetState[1] = targetPosition.x / raySensors.viewDistance; // normalization
|
|
targetState[2] = targetPosition.y / raySensors.viewDistance;
|
|
targetState[3] = targetPosition.z / raySensors.viewDistance;*/
|
|
targetState[1] = targetPosition.x; // normalization
|
|
targetState[2] = targetPosition.y;
|
|
targetState[3] = targetPosition.z;
|
|
targetState[4] = blockCont.thisBlock.firebasesAreaDiameter / raySensors.viewDistance;
|
|
targetState[5] = blockCont.thisBlock.belongRatio;
|
|
}
|
|
}
|
|
|
|
// move Agent into Agent Spawn Area
|
|
private void MoveAgentToSpwanArea()
|
|
{
|
|
float randX = UnityEngine.Random.Range(minAgentAreaX, maxAgentAreaX); ;
|
|
float randZ = 0f;
|
|
if (paramCon.spawnAgentInAllMap)
|
|
{
|
|
// spawn agent in all around map
|
|
randZ = UnityEngine.Random.Range(minAgentAreaZ, maxEnemyAreaZ);
|
|
}
|
|
else
|
|
{
|
|
// spawn agent in only agent spawn area
|
|
randZ = UnityEngine.Random.Range(minAgentAreaZ, maxAgentAreaZ);
|
|
}
|
|
|
|
int Y = 1;
|
|
Vector3 initAgentLoc = new Vector3(randX, Y, randZ);
|
|
MoveAgentTo(initAgentLoc);
|
|
}
|
|
|
|
// move Agent to this position
|
|
public void MoveAgentTo(Vector3 thisPosition)
|
|
{
|
|
// while using transform.localPosition to move character
|
|
// u should turn off character Controller or it won't work
|
|
agentCharaCon.enabled = false;
|
|
agentObj.transform.localPosition = thisPosition;
|
|
agentCharaCon.enabled = true;
|
|
}
|
|
|
|
// check over and get rewards
|
|
// 1 = success,2 = overtime,0 = notover
|
|
public (int, float, float) CheckOverAndRewards()
|
|
{
|
|
int endTypeInt = 0;
|
|
float thisReward = 0;
|
|
float endReward = 0;
|
|
float nowDistance = 0f;
|
|
switch (targetTypeInt)
|
|
{
|
|
case (int)SceneBlockContainer.Targets.Go:
|
|
// goto
|
|
(nowDistance, inArea) = blockCont.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
|
envUICon.UpdateTargetGauge(blockCont.thisBlock.firebasesBelong, blockCont.thisBlock.belongMaxPoint);
|
|
float areaTargetReward = GetDistanceReward(nowDistance, inArea);
|
|
//if(inArea != 0)
|
|
if (blockCont.thisBlock.firebasesBelong >= blockCont.thisBlock.belongMaxPoint)
|
|
{
|
|
// win
|
|
// let the area belongs to me
|
|
thisReward = areaTargetReward;
|
|
endReward = paramCon.goWinReward;
|
|
//thisReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
|
endTypeInt = (int)EndType.Win;
|
|
}
|
|
else if (leftTime <= 0)
|
|
{
|
|
// time out lose
|
|
thisReward = areaTargetReward;
|
|
endReward = paramCon.loseReward;
|
|
endTypeInt = (int)EndType.Lose;
|
|
}
|
|
else
|
|
{
|
|
// keep on keeping on!
|
|
thisReward = areaTargetReward;
|
|
endReward = 0;
|
|
endTypeInt = (int)EndType.Running;
|
|
}
|
|
break;
|
|
|
|
case (int)SceneBlockContainer.Targets.Attack:
|
|
// attack
|
|
(nowDistance, inArea) = blockCont.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
|
envUICon.UpdateTargetGauge(blockCont.thisBlock.firebasesBelong, blockCont.thisBlock.belongMaxPoint);
|
|
if (blockCont.thisBlock.GetInAreaNumber(group2Tag) <= 0 && targetEnemySpawnFinish)
|
|
{
|
|
Debug.Log(blockCont.thisBlock.GetInAreaNumber(group2Tag));
|
|
// win
|
|
// let the area belongs to me and kill every enmy in this area.
|
|
thisReward = 0;
|
|
endReward = paramCon.attackWinReward;
|
|
//thisReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
|
endTypeInt = (int)EndType.Win;
|
|
targetEnemySpawnFinish = false;
|
|
}
|
|
else if (leftTime <= 0 && targetEnemySpawnFinish)
|
|
{
|
|
// time out lose
|
|
thisReward = 0;
|
|
endReward = paramCon.loseReward;
|
|
//thisReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
|
endTypeInt = (int)EndType.Lose;
|
|
targetEnemySpawnFinish = false;
|
|
}
|
|
else
|
|
{
|
|
// keep on keeping on!
|
|
// thisReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
|
thisReward = 0;
|
|
endReward = 0;
|
|
targetEnemySpawnFinish = true;
|
|
endTypeInt = (int)EndType.Running;
|
|
}
|
|
break;
|
|
|
|
case (int)SceneBlockContainer.Targets.Defence:
|
|
//defence
|
|
// !!! DIDN't FINISH!!!
|
|
(nowDistance, inArea) = blockCont.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
|
envUICon.UpdateTargetGauge(blockCont.thisBlock.firebasesBelong, blockCont.thisBlock.belongMaxPoint);
|
|
if (leftTime <= 0 && blockCont.thisBlock.firebasesBelong >= 0f)
|
|
{
|
|
// win
|
|
// time over and the area still mine
|
|
thisReward = paramCon.defenceWinReward;
|
|
//thisReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
|
endTypeInt = (int)EndType.Win;
|
|
}
|
|
else if (blockCont.thisBlock.firebasesBelong <= blockCont.thisBlock.belongMaxPoint)
|
|
{
|
|
// lost area lose
|
|
thisReward = paramCon.loseReward;
|
|
//thisReward = (paramCon.inAreaReward * inArea) + getSceneReward(nowDistance);
|
|
endTypeInt = (int)EndType.Lose;
|
|
}
|
|
else
|
|
{
|
|
// keep on keeping on!
|
|
// thisReward = (paramCon.inAreaReward * inArea) + getDistanceReward(nowDistance);
|
|
endTypeInt = (int)EndType.Running;
|
|
}
|
|
break;
|
|
|
|
case (int)SceneBlockContainer.Targets.Stay:
|
|
// Stay
|
|
// endless
|
|
thisReward = 0;
|
|
endReward = 0;
|
|
endTypeInt = (int)EndType.Running;
|
|
break;
|
|
|
|
default:
|
|
//free kill
|
|
if (enemyContainerObj.transform.childCount <= 0)
|
|
{
|
|
// win
|
|
// thisReward = paramCon.winReward + (paramCon.timeBonusPerSecReward * leftTime);
|
|
thisReward = 0;
|
|
endReward = paramCon.freeWinReward;
|
|
endTypeInt = (int)EndType.Win;
|
|
}
|
|
else if (leftTime <= 0)
|
|
{
|
|
// lose
|
|
//thisReward = paramCon.loseReward;
|
|
thisReward = 0;
|
|
endReward = paramCon.loseReward;
|
|
endTypeInt = (int)EndType.Lose;
|
|
}
|
|
else
|
|
{
|
|
// keep on keeping on!
|
|
thisReward = 0;
|
|
endReward = 0;
|
|
endTypeInt = (int)EndType.Running;
|
|
}
|
|
break;
|
|
}
|
|
envUICon.ShowResult(endTypeInt);
|
|
worldUICon.UpdateChart(targetTypeInt, endTypeInt);
|
|
return (endTypeInt, thisReward, endReward);
|
|
}
|
|
|
|
// initialize scene block by target type
|
|
private void SpawnSceneBlock(SceneBlockContainer.Targets thisTargetType)
|
|
{
|
|
int randBlockType = 0;
|
|
switch (thisTargetType)
|
|
{
|
|
case SceneBlockContainer.Targets.Go:
|
|
randBlockType = Random.Range(0, blockCont.goBlockPrefabs.Length);
|
|
sceneBlockSize = blockCont.goBlockPrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
|
|
break;
|
|
case SceneBlockContainer.Targets.Attack:
|
|
randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length);
|
|
sceneBlockSize = blockCont.attackBlockPrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
|
|
break;
|
|
case SceneBlockContainer.Targets.Defence:
|
|
// randBlockType = Random.Range(0, blockCont.defenceBlockPrefabs.Length);
|
|
// sceneBlockSize = blockCont.defenceBlockPrefabs[randBlockType].GetComponent<SceneBlock>().blockSize;
|
|
Debug.LogWarning("Defence Block Not Ready");
|
|
break;
|
|
default:
|
|
Debug.LogWarning("TargetController: InitializeSceneBlock: Wrong TargetType");
|
|
break;
|
|
}
|
|
float randX = UnityEngine.Random.Range(minEnemyAreaX + sceneBlockSize / 2 + 1f, maxEnemyAreaX - sceneBlockSize / 2 - 1f);
|
|
float randZ = UnityEngine.Random.Range(minEnemyAreaZ + sceneBlockSize / 2 + 1f, maxEnemyAreaZ - sceneBlockSize / 2 - 1f);
|
|
targetPosition = new Vector3(randX, 0, randZ);
|
|
|
|
// init scene block
|
|
blockCont.DestroyBlock();
|
|
blockCont.CreateNewBlock(thisTargetType, randBlockType, targetPosition, group1Tag, group2Tag);
|
|
enemyCont.DestroyAllEnemys();
|
|
enemyCont.RandomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneBlockSize);
|
|
blockCont.thisBlock.InitBlock(environmentObj);
|
|
}
|
|
|
|
// caulculate sceneReward if close to target then get great reward
|
|
private float GetDistanceReward(float nowDistance, int inarea)
|
|
{
|
|
if (firstRewardFlag)
|
|
{
|
|
// first distance record
|
|
(lastDistance, _) = blockCont.GetAgentTargetDistanceAndInside(agentObj.transform.position);
|
|
firstRewardFlag = false;
|
|
}
|
|
float thisSceneReward = 0f;
|
|
if (inarea != 0)
|
|
{
|
|
// in area
|
|
thisSceneReward = paramCon.inAreaReward;
|
|
}
|
|
else
|
|
{
|
|
// out of area
|
|
// thisSceneReward = paramCon.distanceReward * Math.Clamp(lastDistance - nowDistance, 0, 100);
|
|
thisSceneReward = paramCon.distanceReward * (lastDistance - nowDistance);
|
|
}
|
|
lastDistance = nowDistance;
|
|
return thisSceneReward;
|
|
}
|
|
|
|
// calculate kill reward base on killed enemy's position
|
|
public float KillReward(Vector3 enemyPosition)
|
|
{
|
|
float thisKillReward = 0f;
|
|
if (targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
|
|
{
|
|
// attack mode
|
|
(_, int isInArea) = blockCont.thisBlock.GetDistInArea(enemyPosition);
|
|
if (isInArea == 1)
|
|
{
|
|
// kill in area enemy
|
|
thisKillReward = paramCon.killTargetEnemyReward;
|
|
}
|
|
else
|
|
{
|
|
thisKillReward = paramCon.killReward;
|
|
}
|
|
}
|
|
else if (targetTypeInt == (int)SceneBlockContainer.Targets.Free)
|
|
{
|
|
// free mode hit
|
|
thisKillReward = paramCon.killTargetEnemyReward;
|
|
}
|
|
else
|
|
{
|
|
// goto & defence
|
|
thisKillReward = paramCon.killReward;
|
|
}
|
|
return thisKillReward;
|
|
}
|
|
|
|
// calculate hit reward base on killed enemy's position and now mode
|
|
public float HitEnemyReward(Vector3 enemyPosition)
|
|
{
|
|
float thisHitReward = 0f;
|
|
if (targetTypeInt == (int)SceneBlockContainer.Targets.Attack)
|
|
{
|
|
// attack mode
|
|
(_, int isInArea) = blockCont.thisBlock.GetDistInArea(enemyPosition);
|
|
if (isInArea == 1)
|
|
{
|
|
// hit in area enemy
|
|
thisHitReward = paramCon.hitTargetReward;
|
|
}
|
|
else
|
|
{
|
|
// hit not in area enemy
|
|
thisHitReward = paramCon.hitReward;
|
|
}
|
|
}
|
|
else if (targetTypeInt == (int)SceneBlockContainer.Targets.Free)
|
|
{
|
|
// free mode hit
|
|
thisHitReward = paramCon.hitTargetReward;
|
|
}
|
|
else
|
|
{
|
|
// goto & defence
|
|
thisHitReward = paramCon.hitReward;
|
|
}
|
|
return thisHitReward;
|
|
}
|
|
|
|
// get in area state
|
|
public int GetInAreaState()
|
|
{
|
|
if (targetTypeInt == (int)SceneBlockContainer.Targets.Go)
|
|
{
|
|
return inArea;
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// Play Mode method
|
|
// Initialize Play mode
|
|
public void PlayInitialize()
|
|
{
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Stay;
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
MoveAgentToSpwanArea();
|
|
enemyCont.DestroyAllEnemys();
|
|
blockCont.DestroyBlock();
|
|
}
|
|
|
|
// change to attack mode
|
|
public void AttackModeChange()
|
|
{
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Attack;
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
}
|
|
|
|
// change to free mode
|
|
public void FreeModeChange()
|
|
{
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Free;
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
}
|
|
|
|
// change to goto mode
|
|
public void GotoModeChange()
|
|
{
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Go;
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
}
|
|
|
|
// change to stay mode
|
|
public void StayModeChange()
|
|
{
|
|
targetTypeInt = (int)SceneBlockContainer.Targets.Stay;
|
|
envUICon.UpdateTargetType(targetTypeInt);
|
|
}
|
|
} |