Update PPO class,add python human control
Python: Update PPO class add python human control Unity: add FP/TP choose button
This commit is contained in:
@@ -21,6 +21,8 @@ public class AgentWithGun : Agent
|
||||
public Camera thisCam;
|
||||
public CharacterController PlayerController;
|
||||
public GameObject enemyPrefab;
|
||||
public GameObject cameraChangerOBJ;
|
||||
|
||||
|
||||
[Header("Rewards")]
|
||||
[Tooltip("Nothing happened reward")]
|
||||
@@ -41,7 +43,7 @@ public class AgentWithGun : Agent
|
||||
[Header("Env")]
|
||||
public bool lockMouse = false;
|
||||
public float Damage = 50; // damage to enemy
|
||||
public float fireRate = 0.5f;
|
||||
public float fireRate = 0.5f;
|
||||
public int enemyNum = 3;
|
||||
public int timeLimit = 30;
|
||||
public bool lockCameraX = false;
|
||||
@@ -76,12 +78,15 @@ public class AgentWithGun : Agent
|
||||
private string LoadDirTime;
|
||||
private float LoadDirDateF;
|
||||
private float loadDirTimeF;
|
||||
public bool defaultTPCamera = true;
|
||||
private StartSeneData DataTransfer;
|
||||
private UIController UICon;
|
||||
private HistoryRecorder HistoryRec;
|
||||
private RaySensors rayScript;
|
||||
private CameraChange camChanger;
|
||||
|
||||
[System.NonSerialized]public float nonReward;
|
||||
|
||||
[System.NonSerialized] public float nonReward;
|
||||
[System.NonSerialized] public float shootReward;
|
||||
[System.NonSerialized] public float shootWithoutReadyReward;
|
||||
[System.NonSerialized] public float hitReward;
|
||||
@@ -118,6 +123,8 @@ public class AgentWithGun : Agent
|
||||
killRewardDefault = DataTransfer.killReward;
|
||||
winRewardDefault = DataTransfer.winReward;
|
||||
loseRewardDefault = DataTransfer.loseReward;
|
||||
lockMouse = DataTransfer.lockMouse;
|
||||
defaultTPCamera = DataTransfer.defaultTPCamera;
|
||||
|
||||
// change Decision Period & Take Actions Between Decisions
|
||||
transform.GetComponent<DecisionRequester>().DecisionPeriod = DataTransfer.DecisionPeriod;
|
||||
@@ -156,6 +163,7 @@ public class AgentWithGun : Agent
|
||||
UICon = transform.GetComponent<UIController>();
|
||||
HistoryRec = transform.GetComponent<HistoryRecorder>();
|
||||
rayScript = GetComponent<RaySensors>();
|
||||
camChanger = cameraChangerOBJ.GetComponent<CameraChange>();
|
||||
|
||||
// give default Reward to Reward value will be used.
|
||||
nonReward = nonRewardDefault;
|
||||
@@ -167,6 +175,15 @@ public class AgentWithGun : Agent
|
||||
killReward = killRewardDefault;
|
||||
//initialize remainTime
|
||||
remainTime = (int)(timeLimit - Time.time + startTime);
|
||||
// change default camera view
|
||||
if (defaultTPCamera)
|
||||
{
|
||||
camChanger.ShowTPSView();
|
||||
}
|
||||
else
|
||||
{
|
||||
camChanger.ShowFPSView();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,7 +193,7 @@ public class AgentWithGun : Agent
|
||||
//Debug.Log(RaySensors.rayTagResult[0]);
|
||||
}*/
|
||||
/* ----------此Update用于debug,Build前删除或注释掉!----------*/
|
||||
|
||||
|
||||
// --------------初始化---------------
|
||||
// randomInitEnemys随机生成enemy
|
||||
public void randomInitEnemys(int EnemyNum)
|
||||
@@ -203,27 +220,9 @@ public class AgentWithGun : Agent
|
||||
|
||||
// ------------动作处理--------------
|
||||
// moveAgent 用于模拟Input.GetAxis移动
|
||||
public void moveAgent(int kW, int kS,int kA,int kD)
|
||||
public void moveAgent(int vertical, int horizontal)
|
||||
{
|
||||
Vector3 thisMovement;
|
||||
int horizontal = 0;
|
||||
int vertical = 0;
|
||||
if (kW==1 && kS != 1)
|
||||
{
|
||||
vertical = 1;
|
||||
}
|
||||
else if (kS==1 && kW!=1)
|
||||
{
|
||||
vertical = -1;
|
||||
}
|
||||
if (kD==1 && kA!=1)
|
||||
{
|
||||
horizontal = 1;
|
||||
}
|
||||
else if (kA ==1 && kD!=1)
|
||||
{
|
||||
horizontal = -1;
|
||||
}
|
||||
|
||||
if (horizontal != 0)//当按下按键(水平方向)
|
||||
{
|
||||
@@ -295,7 +294,7 @@ public class AgentWithGun : Agent
|
||||
|
||||
// ------------动作处理--------------
|
||||
// cameraControl 用于控制Agent视角转动
|
||||
public void cameraControl(float Mouse_X,float Mouse_Y)
|
||||
public void cameraControl(float Mouse_X, float Mouse_Y)
|
||||
{
|
||||
//Mouse_X = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime;
|
||||
//Debug.Log(Input.GetAxis("Mouse X"));
|
||||
@@ -359,11 +358,11 @@ public class AgentWithGun : Agent
|
||||
RaycastHit hit;
|
||||
Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue);
|
||||
bool isGunReady = gunReady();
|
||||
UICon.updateShootKeyViewer(shoot,isGunReady);
|
||||
UICon.updateShootKeyViewer(shoot, isGunReady);
|
||||
//按下鼠标左键
|
||||
if (shoot != 0 && isGunReady == true)
|
||||
{
|
||||
|
||||
|
||||
lastShootTime = Time.time;
|
||||
if (Physics.Raycast(ray, out hit, 100))
|
||||
{
|
||||
@@ -420,12 +419,12 @@ public class AgentWithGun : Agent
|
||||
{
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
if(EnemyGameObjs.Length <= 1)
|
||||
if (EnemyGameObjs.Length <= 1)
|
||||
{
|
||||
//成功击杀所有Enemy
|
||||
return 1;
|
||||
}
|
||||
else if(Time.time - startTime >= timeLimit)
|
||||
else if (Time.time - startTime >= timeLimit)
|
||||
{
|
||||
//超时失败
|
||||
return 2;
|
||||
@@ -477,9 +476,9 @@ public class AgentWithGun : Agent
|
||||
{
|
||||
float epreward = 0f;
|
||||
// 击杀reward判断
|
||||
if(enemyKillCount > 0)
|
||||
if (enemyKillCount > 0)
|
||||
{
|
||||
for(int i = 0;i < enemyKillCount; i++)
|
||||
for (int i = 0; i < enemyKillCount; i++)
|
||||
{
|
||||
epreward += killReward;
|
||||
}
|
||||
@@ -506,7 +505,7 @@ public class AgentWithGun : Agent
|
||||
}
|
||||
if (lockMouse)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked; // 隐藏并且锁定鼠标
|
||||
Cursor.lockState = CursorLockMode.Locked; // hide and lock the mouse
|
||||
}
|
||||
//iniCharts();
|
||||
thisAgentObj.name = thisAgentObj.GetInstanceID().ToString();
|
||||
@@ -549,35 +548,26 @@ public class AgentWithGun : Agent
|
||||
public override void OnActionReceived(ActionBuffers actionBuffers)
|
||||
{
|
||||
//获取输入
|
||||
int kW = actionBuffers.DiscreteActions[0];
|
||||
int kS = actionBuffers.DiscreteActions[1];
|
||||
int kA = actionBuffers.DiscreteActions[2];
|
||||
int kD = actionBuffers.DiscreteActions[3];
|
||||
int mouseShoot = actionBuffers.DiscreteActions[4];
|
||||
int vertical = actionBuffers.DiscreteActions[0];
|
||||
int horizontal = actionBuffers.DiscreteActions[1];
|
||||
int mouseShoot = actionBuffers.DiscreteActions[2];
|
||||
float Mouse_X = actionBuffers.ContinuousActions[0];
|
||||
//float Mouse_Y = actionBuffers.ContinuousActions[1];
|
||||
//int timeLimitControl = (int)actionBuffers.ContinuousActions[2];
|
||||
//float nonRewardIn = actionBuffers.ContinuousActions[1];
|
||||
//float shootRewardIn = actionBuffers.ContinuousActions[2];
|
||||
//float shootWithoutReadyRewardIn = actionBuffers.ContinuousActions[3];
|
||||
//float hitRewardIn = actionBuffers.ContinuousActions[4];
|
||||
//float winRewardIn = actionBuffers.ContinuousActions[5];
|
||||
// loseRewardIn = actionBuffers.ContinuousActions[6];
|
||||
//float killRewardIn = actionBuffers.ContinuousActions[7];
|
||||
//Rewards Update
|
||||
if (vertical == 2) vertical = -1;
|
||||
if (horizontal == 2) horizontal = -1;
|
||||
remainTime = (int)(timeLimit - Time.time + startTime);
|
||||
|
||||
//应用输入
|
||||
shoot = mouseShoot;
|
||||
HistoryRec.realTimeKeyCounter(kW, kS, kA, kD, shoot);
|
||||
HistoryRec.realTimeKeyCounter(vertical, horizontal, shoot);
|
||||
(int kWCount, int kSCount, int kACount, int kDCount, int shootCount) = HistoryRec.getKeyCount();
|
||||
UICon.updateRemainTime(remainTime);
|
||||
UICon.updateWASDKeyViewer(kW, kS, kA, kD);
|
||||
UICon.updateRemainEnemy(enemyNum);
|
||||
UICon.updateWASDKeyViewer(vertical, horizontal);
|
||||
UICon.updateKeyCounterChart(kWCount, kSCount, kACount, kDCount, shootCount);
|
||||
UICon.updateMouseMovementViewer(Mouse_X);
|
||||
UICon.updateRewardViewer(nonReward, shootReward, shootWithoutReadyReward, hitReward, winReward, loseReward, killReward);
|
||||
cameraControl(Mouse_X, 0);
|
||||
moveAgent(kW, kS, kA, kD);
|
||||
moveAgent(vertical, horizontal);
|
||||
float thisRoundReward = rewardCalculate();
|
||||
|
||||
//判断结束
|
||||
@@ -595,7 +585,7 @@ public class AgentWithGun : Agent
|
||||
Debug.Log("reward = " + winReward);
|
||||
EndEpisode();
|
||||
}
|
||||
else if(finished == 2)
|
||||
else if (finished == 2)
|
||||
{
|
||||
//Lose Finished
|
||||
HistoryRec.addRealTimeReward(loseReward);
|
||||
@@ -628,37 +618,45 @@ public class AgentWithGun : Agent
|
||||
ActionSegment<float> continuousActions = actionsOut.ContinuousActions;
|
||||
ActionSegment<int> discreteActions = actionsOut.DiscreteActions;
|
||||
|
||||
int kW = 0;
|
||||
int kS = 0;
|
||||
int kA = 0;
|
||||
int kD = 0;
|
||||
if (Input.GetKey(KeyCode.W))
|
||||
int vertical = 0;
|
||||
int horizontal = 0;
|
||||
if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))
|
||||
{
|
||||
kW = 1;
|
||||
vertical = 1;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.S))
|
||||
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
|
||||
{
|
||||
kS = 1;
|
||||
vertical = -1;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.A))
|
||||
else
|
||||
{
|
||||
kA = 1;
|
||||
vertical = 0;
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D))
|
||||
if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
|
||||
{
|
||||
kD = 1;
|
||||
horizontal = 1;
|
||||
}
|
||||
else if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))
|
||||
{
|
||||
horizontal = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
horizontal = 0;
|
||||
}
|
||||
discreteActions[0] = kW;
|
||||
discreteActions[1] = kS;
|
||||
discreteActions[2] = kA;
|
||||
discreteActions[3] = kD;
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
// Debug.Log("mousebuttonhit");
|
||||
shoot = 1;
|
||||
}
|
||||
discreteActions[4] = shoot;
|
||||
else
|
||||
{
|
||||
shoot = 0;
|
||||
}
|
||||
discreteActions[0] = vertical;
|
||||
discreteActions[1] = horizontal;
|
||||
discreteActions[2] = shoot;
|
||||
//^^^^^^^^^^^^^^^^^^^^^discrete-Control^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvcontinuous-Controlvvvvvvvvvvvvvvvvvvvvvv
|
||||
|
||||
@@ -31,24 +31,24 @@ public class HistoryRecorder : MonoBehaviour
|
||||
{
|
||||
EPTotalShootCount.Add(TotalShootCount);
|
||||
}
|
||||
public void realTimeKeyCounter(int kW, int kS, int kA, int kD, int shoot)
|
||||
public void realTimeKeyCounter(int vertical, int horizontal, int shoot)
|
||||
{
|
||||
if (kW == 1)
|
||||
if (vertical == 1)
|
||||
{
|
||||
realTimeWKeyCount += 1;
|
||||
}
|
||||
if (kS == 1)
|
||||
else if (vertical == -1)
|
||||
{
|
||||
realTimeSKeyCount += 1;
|
||||
}
|
||||
if (kA == 1)
|
||||
{
|
||||
realTimeAKeyCount += 1;
|
||||
}
|
||||
if (kD == 1)
|
||||
if (horizontal == 1)
|
||||
{
|
||||
realTimeDKeyCount += 1;
|
||||
}
|
||||
else if (horizontal == -1)
|
||||
{
|
||||
realTimeAKeyCount += 1;
|
||||
}
|
||||
if (shoot == 1)
|
||||
{
|
||||
realTimeShootCount += 1;
|
||||
|
||||
@@ -69,39 +69,37 @@ public class UIController : MonoBehaviour
|
||||
}
|
||||
|
||||
//------------Key Viewer----------
|
||||
public void updateWASDKeyViewer(int kW,int kS,int kA,int kD)
|
||||
public void updateWASDKeyViewer(int vertical,int horizontal)
|
||||
{
|
||||
if (kW == 1)
|
||||
if (vertical == 1)
|
||||
{
|
||||
upText.color = Color.red;
|
||||
downText.color = Color.black;
|
||||
}
|
||||
else
|
||||
{
|
||||
upText.color = Color.black;
|
||||
}
|
||||
if (kS == 1)
|
||||
else if (vertical == -1)
|
||||
{
|
||||
downText.color = Color.red;
|
||||
upText.color = Color.black;
|
||||
}
|
||||
else
|
||||
{
|
||||
downText.color = Color.black;
|
||||
upText.color = Color.black;
|
||||
}
|
||||
if(kA == 1)
|
||||
{
|
||||
leftText.color = Color.red;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftText.color = Color.black;
|
||||
}
|
||||
if( kD == 1)
|
||||
if (horizontal == 1)
|
||||
{
|
||||
rightText.color = Color.red;
|
||||
leftText.color = Color.black;
|
||||
}
|
||||
else if (horizontal == -1)
|
||||
{
|
||||
leftText.color = Color.red;
|
||||
rightText.color = Color.black;
|
||||
}
|
||||
else
|
||||
{
|
||||
rightText.color = Color.black;
|
||||
downText.color = Color.black;
|
||||
upText.color = Color.black;
|
||||
}
|
||||
}
|
||||
public void updateShootKeyViewer(int shoot,bool isGunReady)
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class gameFlowController : MonoBehaviour
|
||||
{
|
||||
public GameObject Agent;
|
||||
AgentWithGun agentWithGun;
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
agentWithGun = Agent.GetComponent<AgentWithGun>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKey(KeyCode.Escape))
|
||||
{
|
||||
Application.Quit();
|
||||
}
|
||||
if (Input.GetKey(KeyCode.L))
|
||||
{
|
||||
agentWithGun.lockMouse = !agentWithGun.lockMouse;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9a8fb4d12d4b8fc4784f3e142e7fdcf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -19,6 +19,21 @@ public class EnvArgsChanger : MonoBehaviour
|
||||
public Text DecisionPeriodDataText;
|
||||
public Toggle TakeActionsBetweenDecisionsToggle;
|
||||
|
||||
[Header("Lock Mouse")]
|
||||
public Toggle LockMouseToggle;
|
||||
|
||||
[Header("Default Camera")]
|
||||
public Toggle FPToggle;
|
||||
public Text FPText;
|
||||
public Toggle TPToggle;
|
||||
public Text TPText;
|
||||
|
||||
private StartSeneData startSeneData;
|
||||
private void Start()
|
||||
{
|
||||
startSeneData = DataTransfer.GetComponent<StartSeneData>();
|
||||
}
|
||||
|
||||
|
||||
public void onEnemynumValueChanged()
|
||||
{
|
||||
@@ -30,7 +45,7 @@ public class EnvArgsChanger : MonoBehaviour
|
||||
else
|
||||
{
|
||||
EnemyNumText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().EnemyNum = Math.Abs(int.Parse(EnemyNumInput.GetComponent<InputField>().text));
|
||||
startSeneData.EnemyNum = Math.Abs(int.Parse(EnemyNumInput.GetComponent<InputField>().text));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,19 +59,48 @@ public class EnvArgsChanger : MonoBehaviour
|
||||
else
|
||||
{
|
||||
TimeLimText.color = Color.yellow;
|
||||
DataTransfer.GetComponent<StartSeneData>().Timelim = Math.Abs(int.Parse(TimelimInput.GetComponent<InputField>().text));
|
||||
startSeneData.Timelim = Math.Abs(int.Parse(TimelimInput.GetComponent<InputField>().text));
|
||||
}
|
||||
}
|
||||
|
||||
public void onDPSlideValueChanged()
|
||||
{
|
||||
// DecisionPeriod(DP) value Control
|
||||
DataTransfer.GetComponent<StartSeneData>().DecisionPeriod = (int)(DecisionPeriodSlide.GetComponent<Slider>().value);
|
||||
DecisionPeriodDataText.text = DataTransfer.GetComponent<StartSeneData>().DecisionPeriod.ToString();
|
||||
startSeneData.DecisionPeriod = (int)(DecisionPeriodSlide.GetComponent<Slider>().value);
|
||||
DecisionPeriodDataText.text = startSeneData.DecisionPeriod.ToString();
|
||||
}
|
||||
public void onABDToggleChanged()
|
||||
{
|
||||
// Actions Between Decisions(ABD) Toggle Control
|
||||
DataTransfer.GetComponent<StartSeneData>().ActionsBetweenDecisions = TakeActionsBetweenDecisionsToggle.isOn;
|
||||
startSeneData.ActionsBetweenDecisions = TakeActionsBetweenDecisionsToggle.isOn;
|
||||
}
|
||||
public void onLockMouseToggleChanged()
|
||||
{
|
||||
// lock mouse or not
|
||||
startSeneData.lockMouse = LockMouseToggle.isOn;
|
||||
}
|
||||
public void onTPCamToggleChanged()
|
||||
{
|
||||
startSeneData.defaultTPCamera = true;
|
||||
|
||||
FPToggle.interactable = true;
|
||||
FPToggle.SetIsOnWithoutNotify(false);
|
||||
FPText.color = Color.gray;
|
||||
|
||||
TPToggle.SetIsOnWithoutNotify(true);
|
||||
TPToggle.interactable = false;
|
||||
TPText.color = Color.green;
|
||||
}
|
||||
public void onFPCameToggleChanged()
|
||||
{
|
||||
startSeneData.defaultTPCamera = false;
|
||||
|
||||
TPToggle.interactable = true;
|
||||
TPToggle.SetIsOnWithoutNotify(false);
|
||||
TPText.color = Color.gray;
|
||||
|
||||
FPToggle.SetIsOnWithoutNotify(true);
|
||||
FPToggle.interactable = false;
|
||||
FPText.color = Color.green;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ public class StartSeneData : MonoBehaviour
|
||||
public float killRewardDefault = 10.0f;
|
||||
public float winRewardDefault = 20.0f;
|
||||
public float loseRewardDefault = -10.0f;
|
||||
public bool lockMouse = false;
|
||||
public bool defaultTPCamera = true;
|
||||
|
||||
// LoadDir
|
||||
[System.NonSerialized]public string LoadDirDate = "0";
|
||||
|
||||
Reference in New Issue
Block a user