From 0b0c0771ceb36d3aaf1c2be3bbb983cf455148b1 Mon Sep 17 00:00:00 2001 From: Koha9 Date: Mon, 10 Jul 2023 01:51:44 +0800 Subject: [PATCH] =?UTF-8?q?V3.1.3=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分离AgentController和MLAgentsController --- Assets/ML-Agents/Timers/Play_timers.json | 2 +- .../ML-Agents/Timers/StartScene_timers.json | 2 +- Assets/ML-Agents/Timers/Train_timers.json | 2 +- Assets/Scenes/Play.unity | 123 +++--- Assets/Scenes/Train.unity | 87 ++-- Assets/Script/InGame/AgentController.cs | 401 ++++++++++++++++++ Assets/Script/InGame/AgentController.cs.meta | 11 + .../Script/InGame/MLAgentsCustomController.cs | 247 +++++++++++ .../InGame/MLAgentsCustomController.cs.meta | 11 + Assets/Script/InGame/SceneBlockContainer.cs | 4 + Assets/Script/InGame/TargetController.cs | 96 ++--- UserSettings/EditorUserSettings.asset | 8 +- UserSettings/Layouts/default-2021.dwlt | 168 ++++---- 13 files changed, 931 insertions(+), 231 deletions(-) create mode 100644 Assets/Script/InGame/AgentController.cs create mode 100644 Assets/Script/InGame/AgentController.cs.meta create mode 100644 Assets/Script/InGame/MLAgentsCustomController.cs create mode 100644 Assets/Script/InGame/MLAgentsCustomController.cs.meta diff --git a/Assets/ML-Agents/Timers/Play_timers.json b/Assets/ML-Agents/Timers/Play_timers.json index 5a7c0ee..9ec53ba 100644 --- a/Assets/ML-Agents/Timers/Play_timers.json +++ b/Assets/ML-Agents/Timers/Play_timers.json @@ -1 +1 @@ -{"count":1,"self":90.10023679999999,"total":90.127127899999991,"children":{"InitializeActuators":{"count":2,"self":0.0020023,"total":0.0020023,"children":null},"InitializeSensors":{"count":2,"self":0.0014996,"total":0.0014996,"children":null},"AgentSendState":{"count":4270,"self":0.0037265999999999996,"total":0.0037265999999999996,"children":null},"DecideAction":{"count":4270,"self":0.0176603,"total":0.0176603,"children":null},"AgentAct":{"count":4270,"self":0.0009994,"total":0.0009994,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688824628","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId c86659c3-26b6-427b-94c1-0b7b989d18ca -accessToken _k9GNx_VGy_rCCWyZkrKkxzMw6bY-A2LXlE0bI3JCCw00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Play","end_time_seconds":"1688824718"}} \ No newline at end of file +{"count":1,"self":24.7677008,"total":25.0365407,"children":{"InitializeActuators":{"count":2,"self":0.0015003,"total":0.0015003,"children":null},"InitializeSensors":{"count":2,"self":0.0014996999999999999,"total":0.0014996999999999999,"children":null},"AgentSendState":{"count":1017,"self":0.007495,"total":0.0511248,"children":{"CollectObservations":{"count":1017,"self":0.0386265,"total":0.0386265,"children":null},"WriteActionMask":{"count":1017,"self":0.00099969999999999985,"total":0.00099969999999999985,"children":null},"RequestDecision":{"count":1017,"self":0.0040035999999999995,"total":0.0040035999999999995,"children":null}}},"DecideAction":{"count":1017,"self":0.0070006,"total":0.0070006,"children":null},"AgentAct":{"count":1017,"self":0.2072142,"total":0.2072142,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688924863","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId 44edc017-c2ef-4ee2-a46e-a805348063c3 -accessToken hwwekuWWOiWUQhx9nSaQ-lN3ldhffV3XBnjmGsVD5O400ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Play","end_time_seconds":"1688924888"}} \ No newline at end of file diff --git a/Assets/ML-Agents/Timers/StartScene_timers.json b/Assets/ML-Agents/Timers/StartScene_timers.json index 75e38d2..a307a30 100644 --- a/Assets/ML-Agents/Timers/StartScene_timers.json +++ b/Assets/ML-Agents/Timers/StartScene_timers.json @@ -1 +1 @@ -{"count":1,"self":8.638788,"total":8.6849955,"children":{"InitializeActuators":{"count":2,"self":0.0009998,"total":0.0009998,"children":null},"InitializeSensors":{"count":2,"self":0.0009998,"total":0.0009998,"children":null},"AgentSendState":{"count":134,"self":0.0015026,"total":0.0074966,"children":{"CollectObservations":{"count":134,"self":0.0034936,"total":0.0034936,"children":null},"WriteActionMask":{"count":134,"self":0.0004999,"total":0.0004999,"children":null},"RequestDecision":{"count":134,"self":0.0020005,"total":0.0020005,"children":null}}},"DecideAction":{"count":134,"self":0.003005,"total":0.003005,"children":null},"AgentAct":{"count":134,"self":0.0337065,"total":0.0337065,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688824177","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId c86659c3-26b6-427b-94c1-0b7b989d18ca -accessToken _k9GNx_VGy_rCCWyZkrKkxzMw6bY-A2LXlE0bI3JCCw00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"StartScene","end_time_seconds":"1688824186"}} \ No newline at end of file +{"count":1,"self":21.093688,"total":21.0986854,"children":{"InitializeActuators":{"count":2,"self":0.0009999,"total":0.0009999,"children":null},"InitializeSensors":{"count":2,"self":0.0010000999999999999,"total":0.0010000999999999999,"children":null},"AgentSendState":{"count":591,"self":0.0005004,"total":0.0005004,"children":null},"DecideAction":{"count":591,"self":0.0024974,"total":0.0024974,"children":null},"AgentAct":{"count":591,"self":0,"total":0,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688923374","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId 44edc017-c2ef-4ee2-a46e-a805348063c3 -accessToken hwwekuWWOiWUQhx9nSaQ-lN3ldhffV3XBnjmGsVD5O400ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"StartScene","end_time_seconds":"1688923395"}} \ No newline at end of file diff --git a/Assets/ML-Agents/Timers/Train_timers.json b/Assets/ML-Agents/Timers/Train_timers.json index 0f4aef3..58106d7 100644 --- a/Assets/ML-Agents/Timers/Train_timers.json +++ b/Assets/ML-Agents/Timers/Train_timers.json @@ -1 +1 @@ -{"count":1,"self":6.8957847999999995,"total":6.9429937,"children":{"InitializeActuators":{"count":2,"self":0.0009998,"total":0.0009998,"children":null},"InitializeSensors":{"count":2,"self":0.0009998,"total":0.0009998,"children":null},"AgentSendState":{"count":134,"self":0.0015026,"total":0.0074966,"children":{"CollectObservations":{"count":134,"self":0.0034936,"total":0.0034936,"children":null},"WriteActionMask":{"count":134,"self":0.0004999,"total":0.0004999,"children":null},"RequestDecision":{"count":134,"self":0.0020005,"total":0.0020005,"children":null}}},"DecideAction":{"count":134,"self":0.003005,"total":0.003005,"children":null},"AgentAct":{"count":134,"self":0.0337065,"total":0.0337065,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688824177","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId c86659c3-26b6-427b-94c1-0b7b989d18ca -accessToken _k9GNx_VGy_rCCWyZkrKkxzMw6bY-A2LXlE0bI3JCCw00ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Train","end_time_seconds":"1688824184"}} \ No newline at end of file +{"count":1,"self":25.2639312,"total":25.5526563,"children":{"InitializeActuators":{"count":2,"self":0.0015004999999999999,"total":0.0015004999999999999,"children":null},"InitializeSensors":{"count":2,"self":0.00099969999999999985,"total":0.00099969999999999985,"children":null},"AgentSendState":{"count":1054,"self":0.0034980999999999996,"total":0.045699899999999995,"children":{"CollectObservations":{"count":1054,"self":0.0368365,"total":0.0368365,"children":null},"WriteActionMask":{"count":1054,"self":0.0004996,"total":0.0004996,"children":null},"RequestDecision":{"count":1054,"self":0.0048657,"total":0.0048657,"children":null}}},"DecideAction":{"count":1054,"self":0.0070008,"total":0.0070008,"children":null},"AgentAct":{"count":1054,"self":0.2325234,"total":0.2325234,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1688924949","unity_version":"2021.3.14f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2021.3.14f1\\Editor\\Unity.exe -projectpath C:\\Users\\UCUNI\\OneDrive\\Unity\\ML-Agents\\Aimbot-ParallelEnv -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-UCUNI -hubSessionId 44edc017-c2ef-4ee2-a46e-a805348063c3 -accessToken hwwekuWWOiWUQhx9nSaQ-lN3ldhffV3XBnjmGsVD5O400ef","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"Train","end_time_seconds":"1688924974"}} \ No newline at end of file diff --git a/Assets/Scenes/Play.unity b/Assets/Scenes/Play.unity index c3d96c4..508b428 100644 --- a/Assets/Scenes/Play.unity +++ b/Assets/Scenes/Play.unity @@ -4059,11 +4059,11 @@ RectTransform: m_Father: {fileID: 55747560} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 1} - m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 250, y: -150} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} - m_Pivot: {x: 0, y: 1} + m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &413189341 MonoBehaviour: m_ObjectHideFlags: 0 @@ -4484,11 +4484,11 @@ RectTransform: m_Father: {fileID: 55747560} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 1} - m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 250, y: -150} + m_AnchorMin: {x: 1, y: 0.5} + m_AnchorMax: {x: 1, y: 0.5} + m_AnchoredPosition: {x: -250, y: 0} m_SizeDelta: {x: 0, y: 0} - m_Pivot: {x: 0, y: 1} + m_Pivot: {x: 1, y: 0.5} --- !u!114 &440411431 MonoBehaviour: m_ObjectHideFlags: 0 @@ -6412,7 +6412,8 @@ GameObject: - component: {fileID: 629870343} - component: {fileID: 629870342} - component: {fileID: 629870341} - - component: {fileID: 629870340} + - component: {fileID: 629870347} + - component: {fileID: 629870346} - component: {fileID: 629870339} - component: {fileID: 629870338} - component: {fileID: 629870344} @@ -6468,40 +6469,6 @@ MonoBehaviour: m_UseChildSensors: 1 m_UseChildActuators: 1 m_ObservableAttributeHandling: 0 ---- !u!114 &629870340 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 629870337} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 4faf6e358e53cc24582eaff8dd830f97, type: 3} - m_Name: - m_EditorClassIdentifier: - agentParameters: - maxStep: 0 - hasUpgradedFromAgentParameters: 1 - MaxStep: 0 - parameterContainerObj: {fileID: 707336050} - environmentObj: {fileID: 749443372} - enemyContainerObj: {fileID: 891195334} - sceneBlockContainerObj: {fileID: 1811162388} - environmentUIControlObj: {fileID: 156390936} - targetControllerObj: {fileID: 1692325237} - HUDObj: {fileID: 2082200184} - thisCam: {fileID: 1633276149} - moveSpeed: 9 - vX: 0 - vZ: 0 - thisMovement: {x: 0, y: 0, z: 0} - acceleration: 0.9 - mouseXSensitivity: 100 - mouseYSensitivity: 200 - yRotation: 0.1 - oneHotRayTag: 1 - defaultTPCamera: 1 --- !u!114 &629870341 MonoBehaviour: m_ObjectHideFlags: 0 @@ -6598,6 +6565,56 @@ MonoBehaviour: m_EditorClassIdentifier: isDead: 0 maxHP: 100 +--- !u!114 &629870346 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 629870337} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 427cc67643922b245b572721bea0462d, type: 3} + m_Name: + m_EditorClassIdentifier: + agentParameters: + maxStep: 0 + hasUpgradedFromAgentParameters: 1 + MaxStep: 0 + paramContainerObj: {fileID: 707336050} + targetControllerObj: {fileID: 1692325237} + environmentUIObj: {fileID: 156390936} + hudUIObj: {fileID: 2082200184} + oneHotRayTag: 1 +--- !u!114 &629870347 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 629870337} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a899ce3efe4b8384a8cc2e43cc761b71, type: 3} + m_Name: + m_EditorClassIdentifier: + parameterContainerObj: {fileID: 707336050} + environmentObj: {fileID: 749443372} + enemyContainerObj: {fileID: 891195334} + sceneBlockContainerObj: {fileID: 1811162388} + environmentUIControlObj: {fileID: 156390936} + targetControllerObj: {fileID: 1692325237} + HUDObj: {fileID: 2082200184} + thisCam: {fileID: 1633276149} + moveSpeed: 9 + vX: 0 + vZ: 0 + thisMovement: {x: 0, y: 0, z: 0} + acceleration: 0.1 + mouseXSensitivity: 100 + mouseYSensitivity: 200 + yRotation: 0.1 + defaultTPCamera: 1 --- !u!1 &636386815 GameObject: m_ObjectHideFlags: 0 @@ -7905,11 +7922,11 @@ RectTransform: m_Father: {fileID: 413189340} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 1} - m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 2, y: -0} + m_AnchorMin: {x: 0.5, y: 0.5} + m_AnchorMax: {x: 0.5, y: 0.5} + m_AnchoredPosition: {x: 2, y: 0} m_SizeDelta: {x: 0, y: 0} - m_Pivot: {x: 0, y: 1} + m_Pivot: {x: 0.5, y: 0.5} --- !u!114 &762190081 MonoBehaviour: m_ObjectHideFlags: 0 @@ -7937,7 +7954,7 @@ MonoBehaviour: m_BestFit: 0 m_MinSize: 10 m_MaxSize: 40 - m_Alignment: 0 + m_Alignment: 4 m_AlignByGeometry: 0 m_RichText: 1 m_HorizontalOverflow: 1 @@ -14121,11 +14138,11 @@ RectTransform: m_Father: {fileID: 440411430} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 1} - m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 2, y: -0} + m_AnchorMin: {x: 1, y: 0.5} + m_AnchorMax: {x: 1, y: 0.5} + m_AnchoredPosition: {x: -2, y: 0} m_SizeDelta: {x: 0, y: 0} - m_Pivot: {x: 0, y: 1} + m_Pivot: {x: 1, y: 0.5} --- !u!114 &1714055741 MonoBehaviour: m_ObjectHideFlags: 0 @@ -14153,7 +14170,7 @@ MonoBehaviour: m_BestFit: 0 m_MinSize: 10 m_MaxSize: 40 - m_Alignment: 0 + m_Alignment: 5 m_AlignByGeometry: 0 m_RichText: 1 m_HorizontalOverflow: 1 diff --git a/Assets/Scenes/Train.unity b/Assets/Scenes/Train.unity index ea86e8d..19acad1 100644 --- a/Assets/Scenes/Train.unity +++ b/Assets/Scenes/Train.unity @@ -6007,7 +6007,8 @@ GameObject: - component: {fileID: 629870343} - component: {fileID: 629870342} - component: {fileID: 629870341} - - component: {fileID: 629870340} + - component: {fileID: 629870347} + - component: {fileID: 629870346} - component: {fileID: 629870339} - component: {fileID: 629870338} - component: {fileID: 629870344} @@ -6063,40 +6064,6 @@ MonoBehaviour: m_UseChildSensors: 1 m_UseChildActuators: 1 m_ObservableAttributeHandling: 0 ---- !u!114 &629870340 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 629870337} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 4faf6e358e53cc24582eaff8dd830f97, type: 3} - m_Name: - m_EditorClassIdentifier: - agentParameters: - maxStep: 0 - hasUpgradedFromAgentParameters: 1 - MaxStep: 0 - parameterContainerObj: {fileID: 707336050} - environmentObj: {fileID: 749443372} - enemyContainerObj: {fileID: 891195334} - sceneBlockContainerObj: {fileID: 1811162388} - environmentUIControlObj: {fileID: 156390936} - targetControllerObj: {fileID: 1692325237} - HUDObj: {fileID: 2082200184} - thisCam: {fileID: 1633276149} - moveSpeed: 9 - vX: 0 - vZ: 0 - thisMovement: {x: 0, y: 0, z: 0} - acceleration: 0.9 - mouseXSensitivity: 100 - mouseYSensitivity: 200 - yRotation: 0.1 - oneHotRayTag: 1 - defaultTPCamera: 1 --- !u!114 &629870341 MonoBehaviour: m_ObjectHideFlags: 0 @@ -6193,6 +6160,56 @@ MonoBehaviour: m_EditorClassIdentifier: isDead: 0 maxHP: 100 +--- !u!114 &629870346 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 629870337} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 427cc67643922b245b572721bea0462d, type: 3} + m_Name: + m_EditorClassIdentifier: + agentParameters: + maxStep: 0 + hasUpgradedFromAgentParameters: 1 + MaxStep: 0 + paramContainerObj: {fileID: 707336050} + targetControllerObj: {fileID: 1692325237} + environmentUIObj: {fileID: 156390936} + hudUIObj: {fileID: 2082200184} + oneHotRayTag: 1 +--- !u!114 &629870347 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 629870337} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a899ce3efe4b8384a8cc2e43cc761b71, type: 3} + m_Name: + m_EditorClassIdentifier: + parameterContainerObj: {fileID: 707336050} + environmentObj: {fileID: 749443372} + enemyContainerObj: {fileID: 891195334} + sceneBlockContainerObj: {fileID: 1811162388} + environmentUIControlObj: {fileID: 156390936} + targetControllerObj: {fileID: 1692325237} + HUDObj: {fileID: 2082200184} + thisCam: {fileID: 1633276149} + moveSpeed: 9 + vX: 0 + vZ: 0 + thisMovement: {x: 0, y: 0, z: 0} + acceleration: 0.1 + mouseXSensitivity: 100 + mouseYSensitivity: 200 + yRotation: 0.1 + defaultTPCamera: 1 --- !u!1 &636386815 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Script/InGame/AgentController.cs b/Assets/Script/InGame/AgentController.cs new file mode 100644 index 0000000..b8a1d26 --- /dev/null +++ b/Assets/Script/InGame/AgentController.cs @@ -0,0 +1,401 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public class AgentController : MonoBehaviour +{ + public GameObject parameterContainerObj; + public GameObject environmentObj; + public GameObject enemyContainerObj; + public GameObject sceneBlockContainerObj; + public GameObject environmentUIControlObj; + public GameObject targetControllerObj; + public GameObject HUDObj; + public Camera thisCam; + + [Header("GetAxis() Simulate")] + public float moveSpeed = 9.0f; + + public float vX = 0f; + public float vZ = 0f; + public Vector3 thisMovement; + public float acceleration = 0.1f; // 加速度 + public float mouseXSensitivity = 100; + public float mouseYSensitivity = 200; + public float yRotation = 0.1f;//定义一个浮点类型的量,记录‘围绕’X轴旋转的角度 + + private List spinRecord = new List(); + private bool lockMouse; + private float damage; + private float fireRate; + private bool lockCameraX; + private bool lockCameraY; + + // environment + private float lastShootTime = 0.0f; + + private int enemyKillCount = 0; + private Vector3 killEnemyPosition; + public bool defaultTPCamera = true; + + [System.NonSerialized] public bool gunReadyToggle = true; + private string myTag = ""; + private float lastEnemyFacingDistance = 0f; // record last enemy facing minimum distance + private float lastTargetFacingDistance = 0f; // record last target facing minimum distance + + // scripts + private RaySensors raySensors; + + private CharacterController playerController; + private ParameterContainer paramContainer; + private SceneBlockContainer blockContainer; + private TargetController targetCon; + + private void Start() + { + // initialize scripts + paramContainer = parameterContainerObj.GetComponent(); + blockContainer = sceneBlockContainerObj.GetComponent(); + targetCon = targetControllerObj.GetComponent(); + raySensors = GetComponent(); + playerController = this.transform.GetComponent(); + + // initialize Environment parameters + lockMouse = paramContainer.lockMouse; + damage = paramContainer.damage; + fireRate = paramContainer.fireRate; + lockCameraX = paramContainer.lockCameraX; + lockCameraY = paramContainer.lockCameraY; + + // initialize remainTime + // this agent's tag + myTag = gameObject.tag; + } + + // ------------动作处理-------------- + // moveAgent 用于模拟Input.GetAxis移动 + public void MoveAgent(int vertical, int horizontal) + { + // Vector3 thisMovement; + if (horizontal != 0)//当按下按键(水平方向) + { + if (vX < moveSpeed && vX > -moveSpeed)//当前速度小于最大速度 + { + vX += (float)horizontal * acceleration;//增加加速度 + } + else + { + //防止在一瞬间切换输入时速度仍保持不变 + if ((vX * horizontal) > 0)//输入与当前速度方向同向 + { + vX = (float)horizontal * moveSpeed; //限制最大速度 + } + else + { + vX += (float)horizontal * acceleration;//增加加速度 + } + } + } + else + { + if (Math.Abs(vX) > 0.001) + { + vX -= (vX / Math.Abs(vX)) * acceleration;//减少加速度 + } + else + { + vX = 0; + } + } + + if (vertical != 0)//当按下按键(垂直方向) + { + if (vZ < moveSpeed && vZ > -moveSpeed)//当前速度小于最大速度 + { + vZ += (float)vertical * acceleration;//增加加速度 + } + else + { + if ((vZ * vertical) > 0)//输入与当前速度方向同向 + { + vZ = (float)vertical * moveSpeed; //限制最大速度 + } + else + { + vZ += (float)vertical * acceleration;//增加加速度 + } + } + } + else + { + if (Math.Abs(vZ) > 0.001) + { + vZ -= (vZ / Math.Abs(vZ)) * acceleration;//减少加速度 + } + else + { + vZ = 0; + } + } + thisMovement = (transform.forward * vZ + transform.right * vX); + //PlayerController下的.Move为实现物体运动的函数 + //Move()括号内放入一个Vector3类型的量,本例中为Player_Move + if (thisMovement.magnitude > moveSpeed) + { + thisMovement = thisMovement.normalized * moveSpeed; + } + playerController.Move(thisMovement * Time.deltaTime); + // update Key Viewer + } + + // ------------动作处理-------------- + // cameraControl 用于控制Agent视角转动 + public void CameraControl(float Mouse_X, float Mouse_Y) + { + //Mouse_X = Input.GetAxis("Mouse X") * MouseSensitivity * Time.deltaTime; + //Debug.Log(Input.GetAxis("Mouse X")); + //Mouse_Y = Input.GetAxis("Mouse Y") * MouseSensitivity * Time.deltaTime; + if (lockCameraX) + { + Mouse_X = 0; + } + if (lockCameraY) + { + Mouse_Y = 0; + } + yRotation = yRotation - Mouse_Y; + //xRotation值为正时,屏幕下移,当xRotation值为负时,屏幕上移 + //当鼠标向上滑动,Mouse_Y值为正,xRotation-Mouse_Y的值为负,xRotation总的值为负,屏幕视角向上滑动 + //当鼠标向下滑动,Mouse_Y值为负,xRotation-Mouse_Y的值为正,xRotation总的值为正,屏幕视角向下滑动 + //简单来说就是要控制鼠标滑动的方向与屏幕移动的方向要相同 + + //limit UP DOWN between -90 -> 90 + yRotation = Mathf.Clamp(yRotation, -90f, 90f); + + //相机左右旋转时,是以Y轴为中心旋转的,上下旋转时,是以X轴为中心旋转的 + transform.Rotate(Vector3.up * Mouse_X); + //Vector3.up相当于Vector3(0,1,0),CameraRotation.Rotate(Vector3.up * Mouse_X)相当于使CameraRotation对象绕y轴旋转Mouse_X个单位 + //即相机左右旋转时,是以Y轴为中心旋转的,此时Mouse_X控制着值的大小 + + //相机在上下旋转移动时,相机方向不会随着移动,类似于低头和抬头,左右移动时,相机方向会随着向左向右移动,类似于向左向右看 + //所以在控制相机向左向右旋转时,要保证和父物体一起转动 + thisCam.transform.localRotation = Quaternion.Euler(yRotation, 0, 0); + //this.transform指这个CameraRotation的位置,localRotation指的是旋转轴 + //transform.localRotation = Quaternion.Eular(x,y,z)控制旋转的时候,按照X-Y-Z轴的旋转顺规 + //即以围绕X轴旋转x度,围绕Y轴旋转y度,围绕Z轴旋转z度 + //且绕轴旋转的坐标轴是父节点本地坐标系的坐标轴 + } + + // GotKill 获得击杀时用于被呼出 + public void KillRecord(Vector3 thiskillEnemyPosition) + { + enemyKillCount += 1; + killEnemyPosition = thiskillEnemyPosition; + } + + // ballistic 射击弹道处理,并返回获得reward + private float Ballistic(int shootState) + { + Vector3 point = new Vector3(thisCam.pixelWidth / 2, thisCam.pixelHeight / 2, 0);//发射位置 + Ray ray = thisCam.ScreenPointToRay(point); + RaycastHit hit; + // Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue); + //按下鼠标左键 + if (shootState != 0 && gunReadyToggle == true) + { + lastShootTime = Time.time; + if (Physics.Raycast(ray, out hit, 100)) + { + if (hit.collider.tag != myTag && hit.collider.tag != "Wall") + { + // kill enemy + GameObject gotHitObj = hit.transform.gameObject;//获取受到Ray撞击的对象 + gotHitObj.GetComponent().ReactToHit(damage, gameObject); + shootState = 0; + return targetCon.HitEnemyReward(gotHitObj.transform.position); + } + } + if (targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Attack) + { + // while if attack mode + float targetDis = Vector3.Distance(blockContainer.thisBlock.transform.position, transform.position); + if (targetDis <= raySensors.viewDistance) + { + // Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red); + if (Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.thisBlock.transform.position) <= blockContainer.thisBlock.firebasesAreaDiameter / 2) + { + // im shooting at target but didn't hit enemy + // Debug.DrawRay(ray.origin, viewPoint-ray.origin, Color.blue); + return paramContainer.shootTargetAreaReward; + } + } + } + shootState = 0; + return paramContainer.shootReward; + } + else if (shootState != 0 && gunReadyToggle == false) + { + // shoot without ready + shootState = 0; + return paramContainer.shootWithoutReadyReward; + } + else + { + // do not shoot + shootState = 0; + return paramContainer.nonReward; + } + } + + private float FacingReward() + { + float thisReward = 0; + bool isFacingtoEnemy = false; + float enemyFacingDistance = 0f; + Ray ray = thisCam.ScreenPointToRay(new Vector3(thisCam.pixelWidth / 2, thisCam.pixelHeight / 2, 0)); + if (targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Free) + { + //free mode + RaycastHit hit; + if (Physics.Raycast(ray, out hit, 100)) + { + // facing to an enemy + if (hit.collider.tag != myTag && hit.collider.tag != "Wall") + { + thisReward = paramContainer.facingReward; + isFacingtoEnemy = true; + } + } + if (raySensors.inViewEnemies.Count > 0 && !isFacingtoEnemy) + { + // have enemy in view + List projectionDis = new List(); + foreach (GameObject thisEnemy in raySensors.inViewEnemies) + { + // for each enemy in view + Vector3 projection = Vector3.Project(thisEnemy.transform.position - transform.position, (ray.direction * 10)); + Vector3 verticalToRay = transform.position + projection - thisEnemy.transform.position; + projectionDis.Add(verticalToRay.magnitude); + // Debug.Log("enemy!" + verticalToRay.magnitude); + // Debug.DrawRay(transform.position, (ray.direction * 100), Color.cyan); + // Debug.DrawRay(transform.position, thisEnemy.transform.position - transform.position, Color.yellow); + // Debug.DrawRay(transform.position, projection, Color.blue); + // Debug.DrawRay(thisEnemy.transform.position, verticalToRay, Color.magenta); + } + enemyFacingDistance = projectionDis.Min(); + if (enemyFacingDistance <= lastEnemyFacingDistance) + { + // closing to enemy + thisReward = 1 / MathF.Sqrt(paramContainer.facingInviewEnemyDisCOEF * enemyFacingDistance + 0.00001f); + } + else + { + thisReward = 0; + } + // enemy in view Reward + lastEnemyFacingDistance = enemyFacingDistance; + if (thisReward >= paramContainer.facingReward) thisReward = paramContainer.facingReward; // limit + if (thisReward <= -paramContainer.facingReward) thisReward = -paramContainer.facingReward; // limit + // Debug.Log("ninimum = " + thisReward); + } + } + else if (targetCon.targetTypeInt == (int)SceneBlockContainer.Targets.Attack) + { + // attack mode + // Target to Agent distance + float targetDis = Vector3.Distance(blockContainer.thisBlock.transform.position, transform.position); + // center of screen between target's distance + float camCenterToTarget = Vector3.Distance(ray.origin + (ray.direction * targetDis), blockContainer.thisBlock.transform.position); + if (targetDis <= raySensors.viewDistance) + { + // Debug.DrawRay(new Vector3(0,0,0), viewPoint, Color.red); + // while center of screen between target's distance is lower than firebasesAreaDiameter + // while facing to target + if (camCenterToTarget <= blockContainer.thisBlock.firebasesAreaDiameter / 2) + { + // Debug.DrawRay(ray.origin, viewPoint-ray.origin, Color.blue); + + thisReward = paramContainer.facingReward; + } + else + { + // while not facing to target + thisReward = (lastTargetFacingDistance - camCenterToTarget) * paramContainer.facingTargetReward; + } + } + // update lastTargetFacingDistance + lastTargetFacingDistance = camCenterToTarget; + } + return thisReward; + } + + // ------------Reward-------------- + // rewardCalculate 计算本动作的Reward + public float RewardCalculate(float sceneReward, float mouseX, float movement, int shootState) + { + float epreward = 0f; + // 击杀reward判断 + if (enemyKillCount > 0) + { + for (int i = 0; i < enemyKillCount; i++) + { + // get + epreward += targetCon.KillReward(killEnemyPosition); + } + enemyKillCount = 0; + } + else + { + enemyKillCount = 0; + } + // 射击动作reward判断 + epreward += Ballistic(shootState) + sceneReward; + // facing reward + epreward += FacingReward(); + // Penalty + // spin penalty + spinRecord.Add(mouseX); + if (spinRecord.Count >= paramContainer.spinRecordMax) + { + spinRecord.RemoveAt(0); + } + float spinPenaltyReward = Math.Abs(spinRecord.ToArray().Sum() * paramContainer.spinPenalty); + if (spinPenaltyReward >= paramContainer.spinPenaltyThreshold) + { + epreward -= spinPenaltyReward; + } + else + { + epreward -= Math.Abs(mouseX) * paramContainer.mousePenalty; + } + // move penalty + if (movement != 0) + { + epreward -= paramContainer.movePenalty; + } + return epreward; + } + + public void UpdateLockMouse() + { + // lock mouse based on paramContainer lockMouse + if (lockMouse) + { + Cursor.lockState = CursorLockMode.Locked; + } + } + + public void UpdateGunState() + { + // update gun state + if ((Time.time - lastShootTime) >= fireRate) + { + gunReadyToggle = true; + } + else + { + gunReadyToggle = false; + } + } +} \ No newline at end of file diff --git a/Assets/Script/InGame/AgentController.cs.meta b/Assets/Script/InGame/AgentController.cs.meta new file mode 100644 index 0000000..443230c --- /dev/null +++ b/Assets/Script/InGame/AgentController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a899ce3efe4b8384a8cc2e43cc761b71 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Script/InGame/MLAgentsCustomController.cs b/Assets/Script/InGame/MLAgentsCustomController.cs new file mode 100644 index 0000000..27e1c5f --- /dev/null +++ b/Assets/Script/InGame/MLAgentsCustomController.cs @@ -0,0 +1,247 @@ +using System; +using Unity.MLAgents; +using Unity.MLAgents.Actuators; +using Unity.MLAgents.Sensors; +using UnityEngine; + +public class MLAgentsCustomController : Agent +{ + public GameObject paramContainerObj; + public GameObject targetControllerObj; + public GameObject environmentUIObj; + public GameObject hudUIObj; + + [Header("Env")] + public bool oneHotRayTag = true; + + // script + private AgentController agentController; + private ParameterContainer paramContainer; + private TargetController targetController; + private EnvironmentUIControl envUIController; + private HUDController hudController; + private RaySensors raySensors; + + // observation + private float[] myObserve = new float[4]; + + private float[] rayTagResult; + private float[] rayTagResultOnehot; + private float[] rayDisResult; + private float[] targetStates; + private float remainTime; + private float inAreaState; + + private int finishedState; + private int step = 0; + private int EP = 0; + + private void Start() + { + agentController = transform.GetComponent(); + raySensors = transform.GetComponent(); + paramContainer = paramContainerObj.GetComponent(); + targetController = targetControllerObj.GetComponent(); + envUIController = environmentUIObj.GetComponent(); + hudController = hudUIObj.GetComponent(); + } + + // ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS + // env开始执行初始化 + public override void OnEpisodeBegin() + { + Debug.LogWarning("GameState|START TEST!"); + step = 0; + agentController.UpdateLockMouse(); + paramContainer.ResetTimeBonusReward(); + //thisAgentObj.name = thisAgentObj.GetInstanceID().ToString(); + if (paramContainer.gameMode == 0) + { + // train mode + targetController.RollNewScene(); + } + else + { + // play mode + targetController.PlayInitialize(); + } + + // give default Reward to Reward value will be used. + if (hudController.chartOn) + { + envUIController.InitChart(); + } + raySensors.UpdateRayInfo(); // update raycast + } + + // ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS + // 观察情报 + public override void CollectObservations(VectorSensor sensor) + { + //List enemyLDisList = RaySensors.enemyLDisList;// All Enemy Lside Distances + //List enemyRDisList = RaySensors.enemyRDisList;// All Enemy Rside Distances + /**myObserve[0] = transform.localPosition.x / raySensors.viewDistance; + myObserve[1] = transform.localPosition.y / raySensors.viewDistance; + myObserve[2] = transform.localPosition.z / raySensors.viewDistance; + myObserve[3] = transform.eulerAngles.y / 360f;**/ + myObserve[0] = transform.localPosition.x; + myObserve[1] = transform.localPosition.y; + myObserve[2] = transform.localPosition.z; + myObserve[3] = transform.eulerAngles.y / 36f; + rayTagResult = raySensors.rayTagResult;// 探测用RayTag结果 float[](raySensorNum,1) + rayTagResultOnehot = raySensors.rayTagResultOneHot.ToArray(); // 探测用RayTagonehot结果 List[](raySensorNum*Tags,1) + rayDisResult = raySensors.rayDisResult; // 探测用RayDis结果 float[](raySensorNum,1) + targetStates = targetController.targetState; // (6) targettype, target x,y,z, firebasesAreaDiameter + remainTime = targetController.leftTime; + inAreaState = targetController.GetInAreaState(); + agentController.UpdateGunState(); + //float[] focusEnemyObserve = RaySensors.focusEnemyInfo;// 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z + + //sensor.AddObservation(allEnemyNum); // 敌人数量 int + sensor.AddObservation(targetStates);// (6) targettype, target x,y,z, firebasesAreaDiameter + sensor.AddObservation(inAreaState); // (1) + sensor.AddObservation(remainTime); // (1) + sensor.AddObservation(agentController.gunReadyToggle); // (1) save gun is ready? + sensor.AddObservation(myObserve); // (4)自机位置xyz+朝向 float[](4,1) + if (oneHotRayTag) + { + sensor.AddObservation(rayTagResultOnehot); // 探测用RayTag结果 float[](raySensorNum,1) + } + else + { + sensor.AddObservation(rayTagResult); + } + sensor.AddObservation(rayDisResult); // 探测用RayDis结果 float[](raySensorNum,1) + envUIController.UpdateStateText(targetStates, inAreaState, remainTime, agentController.gunReadyToggle, myObserve, rayTagResultOnehot, rayDisResult); + /*foreach(float aaa in rayDisResult) + { + Debug.Log(aaa); + } + Debug.LogWarning("------------");*/ + //sensor.AddObservation(focusEnemyObserve); // 最近的Enemy情报 float[](3,1) MinEnemyIndex,x,z + //sensor.AddObservation(raySensorNum); // raySensor数量 int + //sensor.AddObservation(remainTime); // RemainTime int + } + + // ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS + // agent 输入处理 + public override void OnActionReceived(ActionBuffers actionBuffers) + { + //获取输入 + int vertical = actionBuffers.DiscreteActions[0]; + int horizontal = actionBuffers.DiscreteActions[1]; + int mouseShoot = actionBuffers.DiscreteActions[2]; + float Mouse_X = actionBuffers.ContinuousActions[0]; + if (vertical == 2) vertical = -1; + if (horizontal == 2) horizontal = -1; + + //应用输入 + agentController.CameraControl(Mouse_X, 0); + agentController.MoveAgent(vertical, horizontal); + raySensors.UpdateRayInfo(); // update raycast + + //判断结束 + float sceneReward = 0f; + float endReward = 0f; + (finishedState, sceneReward, endReward) = targetController.CheckOverAndRewards(); + float thisRoundReward = agentController.RewardCalculate(sceneReward + endReward, Mouse_X, Math.Abs(vertical) + Math.Abs(horizontal), mouseShoot); + if (hudController.chartOn) + { + envUIController.UpdateChart(thisRoundReward); + } + else + { + envUIController.RemoveChart(); + } + //Debug.Log("reward = " + thisRoundReward); + if (finishedState != (int)TargetController.EndType.Running) + { + // Win or lose Finished + Debug.Log("Finish reward = " + thisRoundReward); + EP += 1; + string targetString = Enum.GetName(typeof(SceneBlockContainer.Targets), targetController.targetTypeInt); + switch (finishedState) + { + case (int)TargetController.EndType.Win: + Debug.LogWarning("Result|" + targetString + "|Win"); + break; + + case (int)TargetController.EndType.Lose: + Debug.LogWarning("Result|" + targetString + "|Lose"); + break; + + default: + Debug.LogWarning("TypeError"); + break; + } + SetReward(thisRoundReward); + EndEpisode(); + } + else + { + // game not over yet + step += 1; + } + SetReward(thisRoundReward); + } + + // ML-AGENTS处理-------------------------------------------------------------------------------------------ML-AGENTS + // 控制调试 + public override void Heuristic(in ActionBuffers actionsOut) + { + //-------------------BUILD + ActionSegment continuousActions = actionsOut.ContinuousActions; + ActionSegment discreteActions = actionsOut.DiscreteActions; + + if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S)) + { + discreteActions[0] = 1; + } + else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W)) + { + discreteActions[0] = -1; + } + else + { + discreteActions[0] = 0; + } + if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A)) + { + discreteActions[1] = 1; + } + else if (Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D)) + { + discreteActions[1] = -1; + } + else + { + discreteActions[1] = 0; + } + + if (Input.GetMouseButton(0)) + { + // Debug.Log("mousebuttonhit"); + discreteActions[2] = 1; + } + else + { + discreteActions[2] = 0; + } + //^^^^^^^^^^^^^^^^^^^^^discrete-Control^^^^^^^^^^^^^^^^^^^^^^ + + //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvcontinuous-Controlvvvvvvvvvvvvvvvvvvvvvv + float Mouse_X = Input.GetAxis("Mouse X") * agentController.mouseXSensitivity * Time.deltaTime; + //float Mouse_Y = Input.GetAxis("Mouse Y") * agentController.mouseYSensitivity * Time.deltaTime; + continuousActions[0] = Mouse_X; + //continuousActions[1] = nonReward; + //continuousActions[2] = shootReward; + //continuousActions[3] = shootWithoutReadyReward; + //continuousActions[4] = hitReward; + //continuousActions[5] = winReward; + //continuousActions[6] = loseReward; + //continuousActions[7] = killReward; + //continuousActions[1] = Mouse_Y; + //continuousActions[2] = timeLimit; + //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^continuous-Control^^^^^^^^^^^^^^^^^^^^^^ + } +} \ No newline at end of file diff --git a/Assets/Script/InGame/MLAgentsCustomController.cs.meta b/Assets/Script/InGame/MLAgentsCustomController.cs.meta new file mode 100644 index 0000000..653a487 --- /dev/null +++ b/Assets/Script/InGame/MLAgentsCustomController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 427cc67643922b245b572721bea0462d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Script/InGame/SceneBlockContainer.cs b/Assets/Script/InGame/SceneBlockContainer.cs index 2adeaf3..c2e95c3 100644 --- a/Assets/Script/InGame/SceneBlockContainer.cs +++ b/Assets/Script/InGame/SceneBlockContainer.cs @@ -57,6 +57,10 @@ public void CreateNewBlock(Targets targetType, int blockType, Vector3 blockPosit thisBlock.group2Tag = tag2; sceneSize = thisBlock.blockSize; break; + + default: + Debug.LogWarning("SceneBlock: CreateNewBlock: targetType not found!"); + break; } } diff --git a/Assets/Script/InGame/TargetController.cs b/Assets/Script/InGame/TargetController.cs index ca1b866..bab92be 100644 --- a/Assets/Script/InGame/TargetController.cs +++ b/Assets/Script/InGame/TargetController.cs @@ -51,7 +51,7 @@ public enum EndType private int inArea = 0; private float freeProb; - private float sceneSize; + private float sceneBlockSize; private float lastDistance; public Vector3 targetPosition; private Vector3 targetLocalPosition; @@ -142,21 +142,7 @@ public void RollNewScene() { // goto target spawn Debug.Log("GOTO THIS TARGET!"); - targetTypeInt = (int)SceneBlockContainer.Targets.Go; - int randBlockType = Random.Range(0, blockCont.goBlockPrefabs.Length); - // get choosed scene size - sceneSize = blockCont.goBlockPrefabs[randBlockType].GetComponent().blockSize; - float randX = UnityEngine.Random.Range(minEnemyAreaX + sceneSize / 2 + 1f, maxEnemyAreaX - sceneSize / 2 - 1f); - float randZ = UnityEngine.Random.Range(minEnemyAreaZ + sceneSize / 2 + 1f, maxEnemyAreaZ - sceneSize / 2 - 1f); - targetPosition = new Vector3(randX, 0f, randZ); - // Init Agent position - MoveAgentToSpwanArea(); - // init scene block - blockCont.DestroyBlock(); - blockCont.CreateNewBlock(SceneBlockContainer.Targets.Go, randBlockType, targetPosition, group1Tag, group2Tag); - enemyCont.DestroyAllEnemys(); - enemyCont.RandomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize); - blockCont.thisBlock.InitBlock(environmentObj); + SpawnSceneBlock(SceneBlockContainer.Targets.Go); // set startDistance firstRewardFlag = true; } @@ -164,21 +150,7 @@ public void RollNewScene() { // attack target spawn Debug.Log("ATTACK!"); - targetTypeInt = (int)SceneBlockContainer.Targets.Attack; - int randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length); - // get choosed scene size - sceneSize = blockCont.attackBlockPrefabs[randBlockType].GetComponent().blockSize; - float randX = UnityEngine.Random.Range(minEnemyAreaX + sceneSize / 2 + 1f, maxEnemyAreaX - sceneSize / 2 - 1f); - float randZ = UnityEngine.Random.Range(minEnemyAreaZ + sceneSize / 2 + 1f, maxEnemyAreaZ - sceneSize / 2 - 1f); - targetPosition = new Vector3(randX, 0f, randZ); - // Init Agent position - MoveAgentToSpwanArea(); - // init scene block - blockCont.DestroyBlock(); - blockCont.CreateNewBlock(SceneBlockContainer.Targets.Attack, randBlockType, targetPosition, group1Tag, group2Tag); - enemyCont.DestroyAllEnemys(); - enemyCont.RandomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize); - blockCont.thisBlock.InitBlock(environmentObj); + SpawnSceneBlock(SceneBlockContainer.Targets.Attack); // set startDistance firstRewardFlag = true; targetEnemySpawnFinish = false; @@ -187,21 +159,7 @@ public void RollNewScene() { // defence target spawn Debug.Log("DEFENCE!"); - targetTypeInt = (int)SceneBlockContainer.Targets.Defence; - int randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length); - // get choosed scene size - sceneSize = blockCont.defencePrefabs[randBlockType].GetComponent().blockSize; - float randX = UnityEngine.Random.Range(minEnemyAreaX + sceneSize / 2 + 1f, maxEnemyAreaX - sceneSize / 2 - 1f); - float randZ = UnityEngine.Random.Range(minEnemyAreaZ + sceneSize / 2 + 1f, maxEnemyAreaZ - sceneSize / 2 - 1f); - targetPosition = new Vector3(randX, 0f, randZ); - // Init Agent position - MoveAgentTo(targetPosition); - // init scene block - blockCont.DestroyBlock(); - blockCont.CreateNewBlock(SceneBlockContainer.Targets.Defence, randBlockType, targetPosition, group1Tag, group2Tag); - enemyCont.DestroyAllEnemys(); - enemyCont.RandomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize); - blockCont.thisBlock.InitBlock(environmentObj); + SpawnSceneBlock(SceneBlockContainer.Targets.Defence); // set startDistance firstRewardFlag = true; } @@ -219,18 +177,17 @@ public void RollNewScene() } // get target observation states - public void UpdateTargetStates() + private void UpdateTargetStates() { // targettype, x,y,z, firebasesAreaDiameter - if (targetTypeInt == (int)SceneBlockContainer.Targets.Free) + targetState[0] = targetTypeInt; + if (targetTypeInt == (int)SceneBlockContainer.Targets.Free || targetTypeInt == (int)SceneBlockContainer.Targets.Stay) { - targetState[0] = targetTypeInt; for (int i = 1; i < targetState.Length; i++) targetState[i] = 0f; } else { - targetState[0] = targetTypeInt; /* targetState[1] = targetPosition.x / raySensors.viewDistance; // normalization targetState[2] = targetPosition.y / raySensors.viewDistance; targetState[3] = targetPosition.z / raySensors.viewDistance;*/ @@ -243,7 +200,7 @@ public void UpdateTargetStates() } // move Agent into Agent Spawn Area - public void MoveAgentToSpwanArea() + private void MoveAgentToSpwanArea() { float randX = UnityEngine.Random.Range(minAgentAreaX, maxAgentAreaX); ; float randZ = 0f; @@ -417,8 +374,43 @@ public void MoveAgentTo(Vector3 thisPosition) 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().blockSize; + break; + case SceneBlockContainer.Targets.Attack: + randBlockType = Random.Range(0, blockCont.attackBlockPrefabs.Length); + sceneBlockSize = blockCont.attackBlockPrefabs[randBlockType].GetComponent().blockSize; + break; + case SceneBlockContainer.Targets.Defence: + // randBlockType = Random.Range(0, blockCont.defenceBlockPrefabs.Length); + // sceneBlockSize = blockCont.defenceBlockPrefabs[randBlockType].GetComponent().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 - public float GetDistanceReward(float nowDistance, int inarea) + private float GetDistanceReward(float nowDistance, int inarea) { if (firstRewardFlag) { diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index d983ba2..3d09235 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -9,14 +9,14 @@ EditorUserSettings: value: 55090405535008090b5608764626074415151c79747c74602b7c1861e4b96c6c flags: 0 RecentlyUsedSceneGuid-1: - value: 06570c0704040b0e5a575520137b5e44154f192e292d22367c2c4866b7b3376f - flags: 0 - RecentlyUsedSceneGuid-2: value: 5703025650035e5d090f087148715d4443161e2c7e2c20357b281b31b0e43060 flags: 0 - RecentlyUsedSceneGuid-3: + RecentlyUsedSceneGuid-2: value: 5453565f53020f085f5d0e72157a0844454f4c2b757d7265757a4f64b7b4313c flags: 0 + RecentlyUsedSceneGuid-3: + value: 06570c0704040b0e5a575520137b5e44154f192e292d22367c2c4866b7b3376f + flags: 0 RecentlyUsedScenePath-0: value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d flags: 0 diff --git a/UserSettings/Layouts/default-2021.dwlt b/UserSettings/Layouts/default-2021.dwlt index 8505670..ae120fa 100644 --- a/UserSettings/Layouts/default-2021.dwlt +++ b/UserSettings/Layouts/default-2021.dwlt @@ -14,16 +14,16 @@ MonoBehaviour: m_EditorClassIdentifier: m_PixelRect: serializedVersion: 2 - x: 64 - y: 43 - width: 1856 - height: 1037 + x: 637 + y: 162 + width: 1016 + height: 1021 m_ShowMode: 4 - m_Title: Hierarchy + m_Title: Game m_RootView: {fileID: 9} m_MinSize: {x: 875, y: 300} m_MaxSize: {x: 10000, y: 10000} - m_Maximized: 1 + m_Maximized: 0 --- !u!114 &2 MonoBehaviour: m_ObjectHideFlags: 52 @@ -40,9 +40,9 @@ MonoBehaviour: m_Position: serializedVersion: 2 x: 0 - y: 717 - width: 1087 - height: 270 + y: 705 + width: 593 + height: 266 m_MinSize: {x: 231, y: 271} m_MaxSize: {x: 10001, y: 10021} m_ActualView: {fileID: 15} @@ -70,12 +70,12 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 0 - width: 1087 - height: 987 + width: 593 + height: 971 m_MinSize: {x: 100, y: 200} m_MaxSize: {x: 8096, y: 16192} vertical: 1 - controlID: 52 + controlID: 108 --- !u!114 &4 MonoBehaviour: m_ObjectHideFlags: 52 @@ -92,9 +92,9 @@ MonoBehaviour: m_Position: serializedVersion: 2 x: 0 - y: 717 - width: 312 - height: 270 + y: 705 + width: 173 + height: 266 m_MinSize: {x: 102, y: 121} m_MaxSize: {x: 4002, y: 4021} m_ActualView: {fileID: 19} @@ -119,14 +119,14 @@ MonoBehaviour: - {fileID: 4} m_Position: serializedVersion: 2 - x: 1087 + x: 593 y: 0 - width: 312 - height: 987 + width: 173 + height: 971 m_MinSize: {x: 100, y: 200} m_MaxSize: {x: 8096, y: 16192} vertical: 1 - controlID: 21 + controlID: 119 --- !u!114 &6 MonoBehaviour: m_ObjectHideFlags: 52 @@ -144,10 +144,10 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 0 - width: 312 - height: 717 - m_MinSize: {x: 202, y: 221} - m_MaxSize: {x: 4002, y: 4021} + width: 173 + height: 705 + m_MinSize: {x: 200, y: 200} + m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 17} m_Panes: - {fileID: 17} @@ -173,12 +173,12 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 30 - width: 1856 - height: 987 + width: 1016 + height: 971 m_MinSize: {x: 300, y: 200} m_MaxSize: {x: 24288, y: 16192} vertical: 0 - controlID: 142 + controlID: 107 --- !u!114 &8 MonoBehaviour: m_ObjectHideFlags: 52 @@ -194,12 +194,12 @@ MonoBehaviour: m_Children: [] m_Position: serializedVersion: 2 - x: 1399 + x: 766 y: 0 - width: 457 - height: 987 - m_MinSize: {x: 276, y: 71} - m_MaxSize: {x: 4001, y: 4021} + width: 250 + height: 971 + m_MinSize: {x: 275, y: 50} + m_MaxSize: {x: 4000, y: 4000} m_ActualView: {fileID: 16} m_Panes: - {fileID: 16} @@ -225,8 +225,8 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 0 - width: 1856 - height: 1037 + width: 1016 + height: 1021 m_MinSize: {x: 875, y: 300} m_MaxSize: {x: 10000, y: 10000} m_UseTopView: 1 @@ -250,7 +250,7 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 0 - width: 1856 + width: 1016 height: 30 m_MinSize: {x: 0, y: 0} m_MaxSize: {x: 0, y: 0} @@ -271,8 +271,8 @@ MonoBehaviour: m_Position: serializedVersion: 2 x: 0 - y: 1017 - width: 1856 + y: 1001 + width: 1016 height: 20 m_MinSize: {x: 0, y: 0} m_MaxSize: {x: 0, y: 0} @@ -293,8 +293,8 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 0 - width: 1087 - height: 717 + width: 593 + height: 705 m_MinSize: {x: 201, y: 221} m_MaxSize: {x: 4001, y: 4021} m_ActualView: {fileID: 18} @@ -325,7 +325,7 @@ MonoBehaviour: serializedVersion: 2 x: 64 y: 73 - width: 1086 + width: 1082 height: 696 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: @@ -337,7 +337,7 @@ MonoBehaviour: m_ShowGizmos: 0 m_TargetDisplay: 0 m_ClearColor: {r: 0, g: 0, b: 0, a: 0} - m_TargetSize: {x: 1086, y: 675} + m_TargetSize: {x: 1082, y: 675} m_TextureFilterMode: 0 m_TextureHideFlags: 61 m_RenderIMGUI: 1 @@ -352,8 +352,8 @@ MonoBehaviour: m_VRangeLocked: 0 hZoomLockedByDefault: 0 vZoomLockedByDefault: 0 - m_HBaseRangeMin: -543 - m_HBaseRangeMax: 543 + m_HBaseRangeMin: -541 + m_HBaseRangeMax: 541 m_VBaseRangeMin: -337.5 m_VBaseRangeMax: 337.5 m_HAllowExceedBaseRangeMin: 1 @@ -373,23 +373,23 @@ MonoBehaviour: serializedVersion: 2 x: 0 y: 21 - width: 1086 + width: 1082 height: 675 m_Scale: {x: 1, y: 1} - m_Translation: {x: 543, y: 337.5} + m_Translation: {x: 541, y: 337.5} m_MarginLeft: 0 m_MarginRight: 0 m_MarginTop: 0 m_MarginBottom: 0 m_LastShownAreaInsideMargins: serializedVersion: 2 - x: -543 + x: -541 y: -337.5 - width: 1086 + width: 1082 height: 675 m_MinimalGUI: 1 m_defaultScale: 1 - m_LastWindowPixelSize: {x: 1086, y: 696} + m_LastWindowPixelSize: {x: 1082, y: 696} m_ClearInEditMode: 1 m_NoCameraWarning: 1 m_LowResolutionForAspectRatios: 01000000000000000000 @@ -443,10 +443,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 64 - y: 790 - width: 1086 - height: 249 + x: 637 + y: 897 + width: 592 + height: 245 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -464,7 +464,7 @@ MonoBehaviour: m_SkipHidden: 0 m_SearchArea: 1 m_Folders: - - Assets + - Assets/Scenes m_Globs: [] m_OriginalText: m_ViewMode: 1 @@ -477,9 +477,9 @@ MonoBehaviour: m_IsLocked: 0 m_FolderTreeState: scrollPos: {x: 0, y: 0} - m_SelectedIDs: 5a760000 - m_LastClickedID: 30298 - m_ExpandedIDs: 000000005a7600005c7600005e7600006076000062760000a076000000ca9a3b + m_SelectedIDs: 05ca9a3b + m_LastClickedID: 1000000005 + m_ExpandedIDs: 00000000ee660000f0660000f2660000f4660000f66600001467000000ca9a3b m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -507,7 +507,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 000000005a7600005c7600005e7600006076000062760000 + m_ExpandedIDs: 00000000ee660000f0660000f2660000f4660000f6660000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -538,18 +538,18 @@ MonoBehaviour: m_ExpandedInstanceIDs: c62300008a5c000000000000 m_RenameOverlay: m_UserAcceptedRename: 0 - m_Name: MLAgentsController - m_OriginalName: MLAgentsController + m_Name: + m_OriginalName: m_EditFieldRect: serializedVersion: 2 x: 0 y: 0 width: 0 height: 0 - m_UserData: 91370 + m_UserData: 0 m_IsWaitingForDelay: 0 m_IsRenaming: 0 - m_OriginalEventType: 4 + m_OriginalEventType: 11 m_IsRenamingFilename: 1 m_ClientGUIView: {fileID: 2} m_CreateAssetUtility: @@ -559,7 +559,7 @@ MonoBehaviour: m_Icon: {fileID: 0} m_ResourceFile: m_NewAssetIndexInList: -1 - m_ScrollPosition: {x: 0, y: 86} + m_ScrollPosition: {x: 0, y: 0} m_GridSize: 16 m_SkipHiddenPackages: 0 m_DirectoriesAreaWidth: 355 @@ -583,10 +583,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 1463 - y: 73 - width: 456 - height: 966 + x: 1403 + y: 192 + width: 249 + height: 950 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -624,10 +624,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 1151 - y: 73 - width: 310 - height: 696 + x: 1230 + y: 192 + width: 171 + height: 684 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -637,7 +637,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 9672ffff3a84ffff9a89fffff089ffff468affff9c8afffffe8affff548bffffb68bffff208cffff108dffff14c2ffff60c8ffff26ceffff2ccfffffe0cfffffa0d3ffffaed4ffffe2dbffff3adcffff1eddffffdae0ffff6ce6ffff76e7ffffa4e9ffff7aedffff56effffff6f1ffff6af5ffffc0f5ffff32fbfffff4ffffff6a600000cc920000deae0000c2c1000096cb0000e2ce000076d5000088d90000f6dc0000c0dd0000c0f70000c800010014040100a80a01003c110100d05f010036600100 + m_ExpandedIDs: 787bffffda7bffff22b3ffff3ab8ffff7cbdffffe6bdffff3cbeffff64c6ffff70cbffffcecbffff3cccffff9eccffff02d0ffff58d0ffff6ae5ffff40e6ffff90e6ffffe0e6ffff7aecffff18f3ffff6ef3ffffd8f3ffff32fbfffff87e00007c910000c6a3000054ca000034f100000e370100 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -681,10 +681,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 64 - y: 73 - width: 1086 - height: 696 + x: 637 + y: 192 + width: 592 + height: 684 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -919,9 +919,9 @@ MonoBehaviour: m_PlayAudio: 0 m_AudioPlay: 0 m_Position: - m_Target: {x: 54.110233, y: -123.744255, z: 92.51965} + m_Target: {x: 208.58499, y: -370.4251, z: 418.47623} speed: 2 - m_Value: {x: 54.110233, y: -123.744255, z: 92.51965} + m_Value: {x: 208.58499, y: -370.4251, z: 418.47623} m_RenderMode: 0 m_CameraMode: drawMode: 0 @@ -968,13 +968,13 @@ MonoBehaviour: m_GridAxis: 1 m_gridOpacity: 0.5 m_Rotation: - m_Target: {x: 0.45785084, y: 0.27967224, z: -0.15440024, w: 0.82959855} + m_Target: {x: 0.32789153, y: 0.21797852, z: -0.078089386, w: 0.9158536} speed: 2 - m_Value: {x: 0.4578698, y: 0.27968383, z: -0.15440664, w: 0.82963294} + m_Value: {x: 0.32790574, y: 0.21798797, z: -0.07809277, w: 0.9158933} m_Size: - m_Target: 109.824066 + m_Target: 472.939 speed: 2 - m_Value: 109.824066 + m_Value: 472.939 m_Ortho: m_Target: 0 speed: 2 @@ -1019,10 +1019,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 1151 - y: 790 - width: 310 - height: 249 + x: 1230 + y: 897 + width: 171 + height: 245 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default