浏览代码

CubeWars

/develop/cubewars
Andrew Cohen 5 年前
当前提交
d7b8cf16
共有 38 个文件被更改,包括 6023 次插入0 次删除
  1. 8
      Project/ProjectSettings/TagManager.asset
  2. 33
      config/trainer_config.yaml
  3. 8
      Project/Assets/ML-Agents/Examples/CubeWars.meta
  4. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Materials.meta
  5. 77
      Project/Assets/ML-Agents/Examples/CubeWars/Materials/lazer.mat
  6. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Materials/lazer.mat.meta
  7. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Meshes.meta
  8. 1001
      Project/Assets/ML-Agents/Examples/CubeWars/Meshes/Court.fbx
  9. 115
      Project/Assets/ML-Agents/Examples/CubeWars/Meshes/Court.fbx.meta
  10. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Prefabs.meta
  11. 1001
      Project/Assets/ML-Agents/Examples/CubeWars/Prefabs/CubeWarArea.prefab
  12. 7
      Project/Assets/ML-Agents/Examples/CubeWars/Prefabs/CubeWarArea.prefab.meta
  13. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Scenes.meta
  14. 1001
      Project/Assets/ML-Agents/Examples/CubeWars/Scenes/CubeWar.unity
  15. 7
      Project/Assets/ML-Agents/Examples/CubeWars/Scenes/CubeWar.unity.meta
  16. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts.meta
  17. 66
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarArea.cs
  18. 11
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarArea.cs.meta
  19. 50
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarSettings.cs
  20. 11
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarSettings.cs.meta
  21. 248
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs
  22. 11
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs.meta
  23. 258
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs
  24. 11
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs.meta
  25. 8
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/WarLogic.cs
  26. 11
      Project/Assets/ML-Agents/Examples/CubeWars/Scripts/WarLogic.cs.meta
  27. 8
      Project/Assets/ML-Agents/Examples/CubeWars/TFModels.meta
  28. 1001
      Project/Assets/ML-Agents/Examples/CubeWars/TFModels/LargeCubeSoldier.nn
  29. 11
      Project/Assets/ML-Agents/Examples/CubeWars/TFModels/LargeCubeSoldier.nn.meta
  30. 1001
      Project/Assets/ML-Agents/Examples/CubeWars/TFModels/SmallCubeSoldier.nn
  31. 11
      Project/Assets/ML-Agents/Examples/CubeWars/TFModels/SmallCubeSoldier.nn.meta

8
Project/ProjectSettings/TagManager.asset


- symbol_O_Goal
- purpleAgent
- purpleGoal
- StrongLargeAgent
- WeakLargeAgent
- DeadLargeAgent
- StrongSmallAgent
- WeakSmallAgent
- DeadSmallAgent
- largeLazer
- smallLazer
layers:
- Default
- TransparentFX

33
config/trainer_config.yaml


swap_steps: 50000
team_change: 100000
SmallCubeSoldier:
normalize: false
max_steps: 5.0e7
learning_rate_schedule: constant
batch_size: 2048
buffer_size: 20480
hidden_units: 512
time_horizon: 1000
num_layers: 2
self_play:
window: 50
play_against_latest_model_ratio: 0.5
save_steps: 50000
swap_steps: 150000
team_change: 200000
LargeCubeSoldier:
normalize: false
max_steps: 5.0e7
learning_rate_schedule: constant
batch_size: 2048
buffer_size: 20480
hidden_units: 512
time_horizon: 1000
num_layers: 2
self_play:
window: 50
play_against_latest_model_ratio: 0.5
save_steps: 50000
swap_steps: 17000
team_change: 200000
CrawlerStatic:
normalize: true
num_epoch: 3

8
Project/Assets/ML-Agents/Examples/CubeWars.meta


fileFormatVersion: 2
guid: 624ff327b14154c41869c6ecdcdcc167
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Materials.meta


fileFormatVersion: 2
guid: 1109fb214924545fa8c858adeb46006e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

77
Project/Assets/ML-Agents/Examples/CubeWars/Materials/lazer.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: lazer
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _ALPHABLEND_ON _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: 3000
stringTagMap:
RenderType: Transparent
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 10
- _GlossMapScale: 0
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0.132
- _Mode: 2
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 1
- _SpecularHighlights: 1
- _SrcBlend: 5
- _UVSec: 0
- _ZWrite: 0
m_Colors:
- _Color: {r: 1, g: 0, b: 0.93103456, a: 0.23529412}
- _EmissionColor: {r: 0.18910186, g: 0.040765576, b: 0.19117647, a: 1}

8
Project/Assets/ML-Agents/Examples/CubeWars/Materials/lazer.mat.meta


fileFormatVersion: 2
guid: 7fa38eb3aed104201a47be0d9a29aa3a
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Meshes.meta


fileFormatVersion: 2
guid: 58d69d036af19479fac040620908b536
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/CubeWars/Meshes/Court.fbx
文件差异内容过多而无法显示
查看文件

115
Project/Assets/ML-Agents/Examples/CubeWars/Meshes/Court.fbx.meta


fileFormatVersion: 2
guid: c334b553a3f3f4ba38ce81984b3bf2c9
ModelImporter:
serializedVersion: 23
fileIDToRecycleName:
100000: //RootNode
100002: Floor
100004: WallsOuter
400000: //RootNode
400002: Floor
400004: WallsOuter
2100000: rep_WhiteWalls
2100002: rep_Floor
2300000: Floor
2300002: WallsOuter
3300000: Floor
3300002: WallsOuter
4300000: WallsOuter
4300002: Floor
externalObjects:
- first:
type: UnityEngine:Material
assembly: UnityEngine.CoreModule
name: rep_Floor
second: {fileID: 2100000, guid: 9705566e6ddfaac4ca6e57f5c0f9b879, type: 2}
- first:
type: UnityEngine:Material
assembly: UnityEngine.CoreModule
name: rep_WhiteWalls
second: {fileID: 2100000, guid: 6a39c0407dd85684384bf0277294e9b6, type: 2}
materials:
importMaterials: 1
materialName: 0
materialSearch: 1
materialLocation: 1
animations:
legacyGenerateAnimations: 4
bakeSimulation: 0
resampleCurves: 1
optimizeGameObjects: 0
motionNodeName:
rigImportErrors:
rigImportWarnings:
animationImportErrors:
animationImportWarnings:
animationRetargetingWarnings:
animationDoRetargetingWarnings: 0
importAnimatedCustomProperties: 0
importConstraints: 0
animationCompression: 1
animationRotationError: 0.5
animationPositionError: 0.5
animationScaleError: 0.5
animationWrapMode: 0
extraExposedTransformPaths: []
extraUserProperties: []
clipAnimations: []
isReadable: 1
meshes:
lODScreenPercentages: []
globalScale: 1
meshCompression: 0
addColliders: 0
useSRGBMaterialColor: 1
importVisibility: 1
importBlendShapes: 1
importCameras: 1
importLights: 1
swapUVChannels: 0
generateSecondaryUV: 1
useFileUnits: 1
optimizeMeshForGPU: 1
keepQuads: 0
weldVertices: 1
preserveHierarchy: 0
indexFormat: 0
secondaryUVAngleDistortion: 8
secondaryUVAreaDistortion: 15.000001
secondaryUVHardAngle: 88
secondaryUVPackMargin: 4
useFileScale: 1
previousCalculatedGlobalScale: 1
hasPreviousCalculatedGlobalScale: 0
tangentSpace:
normalSmoothAngle: 60
normalImportMode: 0
tangentImportMode: 3
normalCalculationMode: 4
legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 1
blendShapeNormalImportMode: 1
normalSmoothingSource: 0
importAnimation: 1
copyAvatar: 0
humanDescription:
serializedVersion: 2
human: []
skeleton: []
armTwist: 0.5
foreArmTwist: 0.5
upperLegTwist: 0.5
legTwist: 0.5
armStretch: 0.05
legStretch: 0.05
feetSpacing: 0
rootMotionBoneName:
hasTranslationDoF: 0
hasExtraRoot: 0
skeletonHasParents: 1
lastHumanDescriptionAvatarSource: {instanceID: 0}
animationType: 0
humanoidOversampling: 1
additionalBone: 0
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Prefabs.meta


fileFormatVersion: 2
guid: 58aef9963acd34e2c8e8972485552f65
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/CubeWars/Prefabs/CubeWarArea.prefab
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/CubeWars/Prefabs/CubeWarArea.prefab.meta


fileFormatVersion: 2
guid: 507699f5e996e4b839e9b05fce9dde76
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Scenes.meta


fileFormatVersion: 2
guid: 6fffdb5c342d24f748ef245f7393e7ee
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/CubeWars/Scenes/CubeWar.unity
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/CubeWars/Scenes/CubeWar.unity.meta


fileFormatVersion: 2
guid: d45ecfde6c12c4d98a0b9764b3196abc
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Scripts.meta


fileFormatVersion: 2
guid: 746b200d236db432f89b839adb9454e7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

66
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarArea.cs


using UnityEngine;
using MLAgentsExamples;
public class CubeWarArea : Area
{
[HideInInspector]
public SmallCubeAgent[] smallAgents;
[HideInInspector]
public LargeCubeAgent[] largeAgents;
public float range;
void Start()
{
range = 1.0f;
smallAgents = GetComponentsInChildren<SmallCubeAgent>();
largeAgents = GetComponentsInChildren<LargeCubeAgent>();
}
public void AgentDied()
{
bool smallAlive = false;
foreach (var smallAgent in smallAgents)
{
if (!smallAgent.IsDead())
{
smallAlive = true;
}
}
bool largeAlive = false;
foreach (var largeAgent in largeAgents)
{
if (!largeAgent.IsDead())
{
largeAlive = true;
}
}
if (!smallAlive)
{
foreach (var smallAgent in smallAgents)
{
smallAgent.SetReward(-1.0f);
smallAgent.EndEpisode();
}
foreach (var largeAgent in largeAgents)
{
largeAgent.SetReward(1.0f);
largeAgent.EndEpisode();
}
}
else if (!largeAlive)
{
foreach (var smallAgent in smallAgents)
{
smallAgent.SetReward(1.0f);
smallAgent.EndEpisode();
}
foreach (var largeAgent in largeAgents)
{
largeAgent.SetReward(-1.0f);
largeAgent.EndEpisode();
}
}
}
}

11
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarArea.cs.meta


fileFormatVersion: 2
guid: 3870142441aee402fba1a2beb608d6e2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

50
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarSettings.cs


using System;
using UnityEngine;
using UnityEngine.UI;
using MLAgents;
using MLAgents.SideChannels;
public class CubeWarSettings : MonoBehaviour
{
[HideInInspector]
public GameObject[] agents;
[HideInInspector]
public CubeWarArea[] listArea;
public int totalScore;
public Text scoreText;
StatsSideChannel m_statsSideChannel;
// public void Awake()
// {
// Academy.Instance.OnEnvironmentReset += EnvironmentReset;
// m_statsSideChannel = Academy.Instance.GetSideChannel<StatsSideChannel>();
// }
// public void EnvironmentReset()
// {
//
// agents = GameObject.FindGameObjectsWithTag("agent");
// listArea = FindObjectsOfType<CubeWarArea>();
// foreach (var fa in listArea)
// {
// fa.ResetWarArea(agents);
// }
//
// totalScore = 0;
// }
// public void Update()
// {
// scoreText.text = $"Score: {totalScore}";
//
// // Send stats via SideChannel so that they'll appear in TensorBoard.
// // These values get averaged every summary_frequency steps, so we don't
// // need to send every Update() call.
// if ((Time.frameCount % 100)== 0)
// {
// m_statsSideChannel?.AddStat("TotalScore", totalScore);
// }
// }
}

11
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/CubeWarSettings.cs.meta


fileFormatVersion: 2
guid: 79901b5323d5747d8a2e9b3802ed4f7f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

248
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs


using UnityEngine;
using MLAgents;
using MLAgents.Sensors;
public class LargeCubeAgent : Agent
{
CubeWarSettings m_CubeWarSettings;
public GameObject area;
CubeWarArea m_MyArea;
bool m_Dead;
bool m_Shoot;
Rigidbody m_AgentRb;
float m_LaserLength;
float m_HitPoints;
// Speed of agent rotation.
public float turnSpeed;
// Speed of agent movement.
public float moveSpeed;
public Material normalMaterial;
public Material weakMaterial;
public Material deadMaterial;
public GameObject myLaser;
public override void Initialize()
{
m_AgentRb = GetComponent<Rigidbody>();
m_MyArea = area.GetComponent<CubeWarArea>();
m_CubeWarSettings = FindObjectOfType<CubeWarSettings>();
SetResetParameters();
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(System.Convert.ToInt32(m_Shoot));
sensor.AddObservation(m_HitPoints);
}
public Color32 ToColor(int hexVal)
{
var r = (byte)((hexVal >> 16) & 0xFF);
var g = (byte)((hexVal >> 8) & 0xFF);
var b = (byte)(hexVal & 0xFF);
return new Color32(r, g, b, 255);
}
public void MoveAgent(float[] act)
{
m_Shoot = false;
var dirToGo = Vector3.zero;
var rotateDir = Vector3.zero;
if (!m_Dead)
{
var shootCommand = false;
var forwardAxis = (int)act[0];
var rightAxis = (int)act[1];
var rotateAxis = (int)act[2];
var shootAxis = (int)act[3];
switch (forwardAxis)
{
case 1:
dirToGo = transform.forward;
break;
case 2:
dirToGo = -transform.forward;
break;
}
switch (rightAxis)
{
case 1:
dirToGo = transform.right;
break;
case 2:
dirToGo = -transform.right;
break;
}
switch (rotateAxis)
{
case 1:
rotateDir = -transform.up;
break;
case 2:
rotateDir = transform.up;
break;
}
switch (shootAxis)
{
case 1:
shootCommand = true;
break;
}
if (shootCommand)
{
m_Shoot = true;
dirToGo *= 0.5f;
m_AgentRb.velocity *= 0.75f;
}
else
{
transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
}
m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
}
if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
{
m_AgentRb.velocity *= 0.95f;
}
if (m_Shoot)
{
var myTransform = transform;
myLaser.transform.localScale = new Vector3(1f, 1f, m_LaserLength);
var rayDir = 125.0f * myTransform.forward;
Debug.DrawRay(myTransform.position, rayDir, Color.red, 0f, true);
RaycastHit hit;
if (Physics.SphereCast(transform.position, 7f, rayDir, out hit, 125f))
{
if (hit.collider.gameObject.CompareTag("StrongSmallAgent") || hit.collider.gameObject.CompareTag("WeakSmallAgent"))
{
hit.collider.gameObject.GetComponent<SmallCubeAgent>().HitAgent();
}
else if (hit.collider.gameObject.CompareTag("StrongLargeAgent") || hit.collider.gameObject.CompareTag("WeakLargeAgent"))
{
hit.collider.gameObject.GetComponent<LargeCubeAgent>().HealAgent();
}
}
}
else
{
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
}
}
public void HitAgent()
{
if (!m_Dead)
{
m_HitPoints -= .005f;
HealthStatus();
}
}
public void HealAgent()
{
if (m_HitPoints < 1f && !m_Dead)
{
m_HitPoints += .1f;
m_HitPoints = Mathf.Min(m_HitPoints + .1f, 1f);
HealthStatus();
}
}
public void HealthStatus()
{
if (m_HitPoints <= 1f && m_HitPoints > .5f)
{
gameObject.tag = "StrongLargeAgent";
gameObject.GetComponentInChildren<Renderer>().material = normalMaterial;
}
else if (m_HitPoints <= .5f && m_HitPoints > 0.0f)
{
gameObject.tag = "WeakLargeAgent";
gameObject.GetComponentInChildren<Renderer>().material = weakMaterial;
}
else // Dead
{
m_Dead = true;
gameObject.tag = "DeadLargeAgent";
gameObject.GetComponentInChildren<Renderer>().material = deadMaterial;
m_MyArea.AgentDied();
}
}
public override void OnActionReceived(float[] vectorAction)
{
MoveAgent(vectorAction);
}
public override float[] Heuristic()
{
var action = new float[4];
if (Input.GetKey(KeyCode.D))
{
action[2] = 2f;
}
if (Input.GetKey(KeyCode.W))
{
action[0] = 1f;
}
if (Input.GetKey(KeyCode.A))
{
action[2] = 1f;
}
if (Input.GetKey(KeyCode.S))
{
action[0] = 2f;
}
action[3] = Input.GetKey(KeyCode.Space) ? 1.0f : 0.0f;
return action;
}
public override void OnEpisodeBegin()
{
m_HitPoints = 1f;
HealthStatus();
m_Dead = false;
m_Shoot = false;
m_AgentRb.velocity = Vector3.zero;
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
transform.position = new Vector3(Random.Range(-m_MyArea.range, m_MyArea.range),
2f, Random.Range(-m_MyArea.range, m_MyArea.range))
+ area.transform.position;
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));
SetResetParameters();
}
public bool IsDead()
{
return m_Dead;
}
public void SetLaserLengths()
{
m_LaserLength = Academy.Instance.FloatProperties.GetPropertyWithDefault("laser_length", 1.0f);
}
public void SetAgentScale()
{
float agentScale = Academy.Instance.FloatProperties.GetPropertyWithDefault("agent_scale", 5.0f);
gameObject.transform.localScale = new Vector3(agentScale, agentScale, agentScale);
}
public void SetResetParameters()
{
SetLaserLengths();
SetAgentScale();
}
}

11
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/LargeCubeAgent.cs.meta


fileFormatVersion: 2
guid: 2a58b23895cfa4b609adfbe98d1a7c7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

258
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs


using UnityEngine;
using MLAgents;
using MLAgents.Sensors;
public class SmallCubeAgent : Agent
{
CubeWarSettings m_CubeWarSettings;
public GameObject area;
CubeWarArea m_MyArea;
public GameObject largeAgent;
LargeCubeAgent m_LargeAgent;
bool m_Dead;
bool m_Shoot;
Rigidbody m_AgentRb;
float m_LaserLength;
float m_HitPoints;
// Speed of agent rotation.
public float turnSpeed;
// Speed of agent movement.
public float moveSpeed;
public Material normalMaterial;
public Material weakMaterial;
public Material deadMaterial;
public GameObject myLaser;
public override void Initialize()
{
m_AgentRb = GetComponent<Rigidbody>();
m_MyArea = area.GetComponent<CubeWarArea>();
m_LargeAgent = largeAgent.GetComponent<LargeCubeAgent>();
m_CubeWarSettings = FindObjectOfType<CubeWarSettings>();
SetResetParameters();
}
public override void CollectObservations(VectorSensor sensor)
{
sensor.AddObservation(System.Convert.ToInt32(m_Shoot));
sensor.AddObservation(System.Convert.ToInt32(m_Dead));
sensor.AddObservation(m_HitPoints);
// Direction big agent is looking
Vector3 dirToSelf = transform.position - m_LargeAgent.transform.position;
float angle = Vector3.Dot(transform.forward.normalized, dirToSelf.normalized);
sensor.AddObservation(angle);
if (m_Dead)
{
AddReward(-.001f);
}
Debug.Log(angle);
}
public Color32 ToColor(int hexVal)
{
var r = (byte)((hexVal >> 16) & 0xFF);
var g = (byte)((hexVal >> 8) & 0xFF);
var b = (byte)(hexVal & 0xFF);
return new Color32(r, g, b, 255);
}
public void MoveAgent(float[] act)
{
m_Shoot = false;
var dirToGo = Vector3.zero;
var rotateDir = Vector3.zero;
if (!m_Dead)
{
var shootCommand = false;
var forwardAxis = (int)act[0];
var rightAxis = (int)act[1];
var rotateAxis = (int)act[2];
var shootAxis = (int)act[3];
switch (forwardAxis)
{
case 1:
dirToGo = transform.forward;
break;
case 2:
dirToGo = -transform.forward;
break;
}
switch (rightAxis)
{
case 1:
dirToGo = transform.right;
break;
case 2:
dirToGo = -transform.right;
break;
}
switch (rotateAxis)
{
case 1:
rotateDir = -transform.up;
break;
case 2:
rotateDir = transform.up;
break;
}
switch (shootAxis)
{
case 1:
shootCommand = true;
break;
}
if (shootCommand)
{
m_Shoot = true;
}
transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
}
if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
{
m_AgentRb.velocity *= 0.95f;
}
if (m_Shoot)
{
var myTransform = transform;
myLaser.transform.localScale = new Vector3(1f, 1f, m_LaserLength);
var rayDir = 25.0f * myTransform.forward;
Debug.DrawRay(myTransform.position, rayDir, Color.red, 0f, true);
RaycastHit hit;
if (Physics.SphereCast(transform.position, 2f, rayDir, out hit, 25f))
{
if (hit.collider.gameObject.CompareTag("StrongSmallAgent") || hit.collider.gameObject.CompareTag("WeakSmallAgent"))
{
hit.collider.gameObject.GetComponent<SmallCubeAgent>().HealAgent();
}
else if (hit.collider.gameObject.CompareTag("StrongLargeAgent") || hit.collider.gameObject.CompareTag("WeakLargeAgent"))
{
hit.collider.gameObject.GetComponent<LargeCubeAgent>().HitAgent();
}
}
}
else
{
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
}
}
public void HitAgent()
{
if (!m_Dead)
{
m_HitPoints -= .05f;
HealthStatus();
}
}
public void HealAgent()
{
if (m_HitPoints < 1f)
{
m_HitPoints += .01f;
m_HitPoints = Mathf.Min(m_HitPoints + .01f, 1f);
HealthStatus();
}
}
void HealthStatus()
{
if (m_HitPoints <= 1f && m_HitPoints > .65f)
{
gameObject.tag = "StrongSmallAgent";
gameObject.GetComponentInChildren<Renderer>().material = normalMaterial;
}
else if (m_HitPoints <= .65f && m_HitPoints > .3f)
{
gameObject.tag = "WeakSmallAgent";
gameObject.GetComponentInChildren<Renderer>().material = weakMaterial;
}
else // Dead
{
AddReward(-.1f);
m_Dead = true;
gameObject.tag = "DeadSmallAgent";
gameObject.GetComponentInChildren<Renderer>().material = deadMaterial;
m_MyArea.AgentDied();
}
}
public override void OnActionReceived(float[] vectorAction)
{
MoveAgent(vectorAction);
}
public override float[] Heuristic()
{
var action = new float[4];
if (Input.GetKey(KeyCode.D))
{
action[2] = 2f;
}
if (Input.GetKey(KeyCode.W))
{
action[0] = 1f;
}
if (Input.GetKey(KeyCode.A))
{
action[2] = 1f;
}
if (Input.GetKey(KeyCode.S))
{
action[0] = 2f;
}
action[3] = Input.GetKey(KeyCode.Space) ? 1.0f : 0.0f;
return action;
}
public override void OnEpisodeBegin()
{
m_HitPoints = 1f;
HealthStatus();
m_Dead = false;
m_Shoot = false;
m_AgentRb.velocity = Vector3.zero;
myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
float smallRange = 30f * m_MyArea.range;
transform.position = new Vector3(Random.Range(-smallRange, smallRange),
2f,Random.Range(-smallRange, smallRange))
+ area.transform.position;
transform.rotation = Quaternion.Euler(new Vector3(0f, Random.Range(0, 360)));
SetResetParameters();
}
public bool IsDead()
{
return m_Dead;
}
public void SetLaserLengths()
{
m_LaserLength = Academy.Instance.FloatProperties.GetPropertyWithDefault("laser_length", 1.0f);
}
public void SetAgentScale()
{
float agentScale = Academy.Instance.FloatProperties.GetPropertyWithDefault("agent_scale", 1.0f);
gameObject.transform.localScale = new Vector3(agentScale, agentScale, agentScale);
}
public void SetResetParameters()
{
SetLaserLengths();
SetAgentScale();
}
}

11
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/SmallCubeAgent.cs.meta


fileFormatVersion: 2
guid: 23537b7855a6444ea9d37f84c9c5ee84
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/WarLogic.cs


using UnityEngine;
// In war, resolution; in defeat, defiance; in victory, magnanimity.
public class WarLogic : MonoBehaviour
{
public bool respawn;
public CubeWarArea myArea;
}

11
Project/Assets/ML-Agents/Examples/CubeWars/Scripts/WarLogic.cs.meta


fileFormatVersion: 2
guid: 54215a6408d454345aea40fd6f357714
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Project/Assets/ML-Agents/Examples/CubeWars/TFModels.meta


fileFormatVersion: 2
guid: 4cb84562aa05c40f3b3f037350715a14
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/CubeWars/TFModels/LargeCubeSoldier.nn
文件差异内容过多而无法显示
查看文件

11
Project/Assets/ML-Agents/Examples/CubeWars/TFModels/LargeCubeSoldier.nn.meta


fileFormatVersion: 2
guid: fa881d0c4f6b44ea9880a781d0771fc9
ScriptedImporter:
fileIDToRecycleName:
11400000: main obj
11400002: model data
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 19ed1486aa27d4903b34839f37b8f69f, type: 3}

1001
Project/Assets/ML-Agents/Examples/CubeWars/TFModels/SmallCubeSoldier.nn
文件差异内容过多而无法显示
查看文件

11
Project/Assets/ML-Agents/Examples/CubeWars/TFModels/SmallCubeSoldier.nn.meta


fileFormatVersion: 2
guid: 697e3166f993b4e4cb890de8be9ec842
ScriptedImporter:
fileIDToRecycleName:
11400000: main obj
11400002: model data
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 19ed1486aa27d4903b34839f37b8f69f, type: 3}
正在加载...
取消
保存