Newer
Older
#pragma once
#include "memory.h"
#include "utils.h"
#include <map>
#include <memory>
#include <set>
#include <unordered_map>
#include <vector>
#ifdef ABSOLUTE
#undef ABSOLUTE
#endif
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
namespace Retro {
class GameData {
public:
bool load(const std::string& filename);
bool load(std::istream* stream);
bool save(const std::string& filename) const;
bool save(std::ostream* stream) const;
void reset();
static std::string dataPath(const std::string& hint = ".");
AddressSpace& addressSpace() { return m_mem; }
const AddressSpace& addressSpace() const { return m_mem; }
void updateRam();
void setTypes(const std::vector<DataType> types);
void setButtons(const std::vector<std::string>& names);
std::vector<std::string> buttons() const;
void setActions(const std::vector<std::vector<std::vector<std::string>>>& actions);
std::map<int, std::set<int>> validActions() const;
unsigned filterAction(unsigned) const;
Datum lookupValue(const std::string& name);
int64_t lookupValue(const std::string& name) const;
std::unordered_map<std::string, Datum> lookupAll();
std::unordered_map<std::string, int64_t> lookupAll() const;
int64_t lookupDelta(const std::string& name) const;
Variable getVariable(const std::string& name) const;
void setVariable(const std::string& name, const Variable&);
void removeVariable(const std::string& name);
std::unordered_map<std::string, Variable> listVariables() const;
size_t numVariables() const;
private:
AddressSpace m_mem;
AddressSpace m_cloneMem;
AddressSpace m_lastMem;
std::vector<DataType> m_types;
std::map<int, std::set<int>> m_actions;
std::vector<std::string> m_buttons;
std::unordered_map<std::string, Variable> m_vars;
};
class Scenario {
public:
Scenario(const GameData& data);
bool load(const std::string& filename);
bool load(std::istream* stream, const std::string& path = {});
bool save(const std::string& filename) const;
bool save(std::ostream* stream) const;
void reset();
bool loadScript(const std::string& filename, const std::string& scope);
void reloadScripts();
std::vector<std::pair<std::string, std::string>> scripts() const;
const GameData* data() const { return &m_data; }
float currentReward() const;
bool isDone() const;
void setActions(const std::vector<std::vector<std::vector<std::string>>>& actions);
std::map<int, std::set<int>> validActions() const;
unsigned filterAction(unsigned) const;
enum class Measurement {
ABSOLUTE,
DELTA
};
enum class DoneCondition {
ANY,
ALL
};
static Measurement measurement(const std::string&, Measurement);
static Operation op(const std::string&);
static std::string name(Measurement);
static std::string name(Operation);
static int64_t calculate(Measurement, Operation, int64_t reference, int64_t value, int64_t delta);
struct RewardSpec {
Measurement measurement;
Operation op;
int64_t reference;
float reward;
float penalty;
float calculate(int64_t value, int64_t delta) const;
};
struct DoneSpec {
Measurement measurement;
Operation op;
int64_t reference;
bool test(int64_t value, int64_t delta) const;
};
struct DoneNode {
std::unordered_map<std::string, DoneSpec> vars;
std::unordered_map<std::string, std::shared_ptr<DoneNode>> nodes;
DoneCondition condition = DoneCondition::ANY;
};
void setRewardVariable(const std::string& name, const RewardSpec&);
void setRewardFunction(const std::string& name, const std::string& scope = {});
void setDoneVariable(const std::string& name, const DoneSpec&);
void setDoneNode(const std::string& name, std::shared_ptr<DoneNode>);
void setDoneCondition(DoneCondition);
void setDoneFunction(const std::string& name, const std::string& scope = {});
std::unordered_map<std::string, RewardSpec> listRewardVariables() const;
std::unordered_map<std::string, DoneSpec> listDoneVariables() const;
std::unordered_map<std::string, std::shared_ptr<DoneNode>> listDoneNodes() const;
std::pair<std::string, std::string> rewardFunction() const { return m_rewardFunc; }
std::pair<std::string, std::string> doneFunction() const { return m_doneFunc; }
DoneCondition doneCondition() const { return m_doneCondition; }
private:
bool isDone(const DoneNode&) const;
const GameData& m_data;
std::string m_base;
std::vector<std::pair<std::string, std::string>> m_scripts;
std::unordered_map<std::string, RewardSpec> m_rewardVars;
RewardSpec m_rewardTime{ Measurement::DELTA };
std::pair<std::string, std::string> m_rewardFunc;
std::unordered_map<std::string, DoneSpec> m_doneVars;
std::unordered_map<std::string, std::shared_ptr<DoneNode>> m_doneNodes;
DoneCondition m_doneCondition = DoneCondition::ANY;
std::pair<std::string, std::string> m_doneFunc;
std::map<int, std::set<int>> m_actions;
};
}