-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomAgent.cs
More file actions
88 lines (77 loc) · 3.37 KB
/
Copy pathRandomAgent.cs
File metadata and controls
88 lines (77 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections;
using System.Collections.Generic;
using UnityActionAnalysis;
using UnityEngine;
namespace UnityActionAnalysis
{
public class RandomAgent : MonoBehaviour
{
public string AnalysisDatabasePath;
public string InputManagerSettingsPath;
public float ActionInterval = 0.1f;
public bool UseInstrumentationInputSimulator = false;
private ActionManager actionManager;
private InputSimulator inputSim;
void Start()
{
if (FindObjectsOfType(typeof(RandomAgent)).Length > 1)
{
Destroy(this);
return;
}
DontDestroyOnLoad(this);
if (string.IsNullOrEmpty(AnalysisDatabasePath) || string.IsNullOrEmpty(InputManagerSettingsPath))
{
throw new Exception("missing required parameters for RandomAgent");
}
actionManager = new ActionManager(AnalysisDatabasePath);
Debug.Log(actionManager.ActionCount + " game actions");
InputManagerSettings inputManagerSettings = new InputManagerSettings(InputManagerSettingsPath, InputManagerMode.KEYBOARD);
if (UseInstrumentationInputSimulator)
{
// Can use InstrInputSimulator instead of KeyboardInputSimulator to simulate inputs without simulating actual keyboard events (see README)
inputSim = new InstrInputSimulator(inputManagerSettings, this);
} else
{
inputSim = new KeyboardInputSimulator(inputManagerSettings, this);
}
StartCoroutine("AgentLoop");
}
private bool ShouldIncludeAction(GameAction action, InputConditionSet inputConds)
{
// opportunity to inspect which Input invocations are involved in the input by inspecting inputConds.
// example of skipping actions that are for a button named "Pause" or the "Escape" key (either of which are likely to be used for pausing the game)
foreach (InputCondition cond in inputConds)
{
if ( (cond is ButtonInputCondition buttonCond && buttonCond.buttonName == "Pause")
|| (cond is KeyInputCondition keyCond && keyCond.keyCode == KeyCode.Escape))
{
return false;
}
}
return true;
}
IEnumerator AgentLoop()
{
while (true)
{
Dictionary<int, GameAction> validActions = actionManager.DetermineValidActions();
List<GameAction> actionList = new List<GameAction>(validActions.Values);
if (actionList.Count > 0)
{
GameAction chosenAction = actionList[UnityEngine.Random.Range(0, actionList.Count)];
if (chosenAction.TrySolve(out InputConditionSet inputConds))
{
if (ShouldIncludeAction(chosenAction, inputConds))
{
Debug.Log("Performing action: " + string.Join(" && ", inputConds));
yield return StartCoroutine(inputSim.PerformAction(inputConds));
}
}
}
yield return new WaitForSecondsRealtime(ActionInterval);
}
}
}
}