-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScriptWorld.cpp
More file actions
244 lines (176 loc) · 6.25 KB
/
ScriptWorld.cpp
File metadata and controls
244 lines (176 loc) · 6.25 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "Pch.h"
#include "Interfaces.h"
#include "Formula.h"
#include "Actions.h"
#include "TokenPool.h"
#include "PropertyBag.h"
#include "EventHandler.h"
#include "Scriptable.h"
#include "ScriptWorld.h"
ScriptWorld::ScriptWorld (TokenPool * pool, IEngineBinder * binder)
: m_binder(binder),
m_tokens(pool)
{
assert(m_tokens != nullptr);
if (pool) {
m_magicTokenEvent = pool->AddToken("event");
m_magicTokenOther = pool->AddToken("other");
}
}
ScriptWorld::~ScriptWorld () {
for (auto & instance : m_instances)
delete instance;
}
void ScriptWorld::AddArchetype (const std::string & name, Scriptable && archetype) {
unsigned token = m_tokens->AddToken(name);
assert(m_archetypes.find(token) == m_archetypes.end());
m_archetypes.emplace(token, std::move(archetype));
}
void ScriptWorld::AddScriptable (const std::string & name, Scriptable && scriptable) {
unsigned token = m_tokens->AddToken(name);
assert(m_scriptables.find(token) == m_scriptables.end());
m_scriptables.emplace(token, std::move(scriptable));
}
void ScriptWorld::AddMagicBag (unsigned token) {
m_magicBags.emplace_back(token, TextPropertyBag(token));
}
void ScriptWorld::DispatchEvent (Scriptable * target, unsigned eventToken, const IFormulaPropertyBag * paramBag) {
// TODO - this is a giant HACK
target->GetScopes().SetWorld(this);
target->GetEvents()->TriggerHandlers(this, eventToken, target, paramBag);
}
bool ScriptWorld::DispatchEvents () {
bool dispatched = false;
TransferTimedEvents();
while (!m_eventQueue.empty()) {
dispatched = true;
std::vector<Event> tempqueue = std::move(m_eventQueue);
for (Event & e : tempqueue) {
if (e.targetToken) {
Scriptable * scriptable = GetScriptable(e.targetToken);
if (scriptable)
DispatchEvent(scriptable, e.nameToken, e.parameterBag);
}
else if (e.directTarget) {
DispatchEvent(e.directTarget, e.nameToken, e.parameterBag);
}
else {
for (auto & pair : m_scriptables) {
DispatchEvent(&pair.second, e.nameToken, e.parameterBag);
}
for (auto * scriptable : m_instances) {
DispatchEvent(scriptable, e.nameToken, e.parameterBag);
}
}
delete e.parameterBag;
}
}
return dispatched;
}
void ScriptWorld::DumpOverview () const {
std::cout << "----- Scripting world " << this << " -----\n";
std::cout << "Contains " << m_scriptables.size() << " objects and " << m_archetypes.size() << " archetypes\n";
std::cout << "Instantiated " << m_instances.size() << " objects\n";
std::cout << "----- End world -----" << std::endl;
}
Scriptable * ScriptWorld::GetArchetype (unsigned token) {
auto iter = m_archetypes.find(token);
if (iter == m_archetypes.end())
return nullptr;
return &iter->second;
}
Scriptable * ScriptWorld::GetScriptable (unsigned token) {
auto iter = m_scriptables.find(token);
if (iter == m_scriptables.end())
return nullptr;
return &iter->second;
}
TextPropertyBag * ScriptWorld::GetMagicBag (unsigned token) {
for (auto & pair : m_magicBags) {
if (pair.first == token)
return &pair.second;
}
return nullptr;
}
Scriptable * ScriptWorld::InstantiateArchetype (unsigned token, IFormulaPropertyBag * paramBag) {
Scriptable * archetype = GetArchetype(token);
if (!archetype)
return nullptr;
Scriptable * instance = archetype->Instantiate();
m_instances.push_back(instance);
instance->BindAll(m_binder, this);
QueueEvent(instance, m_tokens->AddToken("OnCreate"), paramBag);
return instance;
}
Scriptable * ScriptWorld::InstantiateArchetype (unsigned nameOfInstance, unsigned token, IFormulaPropertyBag * paramBag) {
Scriptable * archetype = GetArchetype(token);
if (!archetype)
return nullptr;
Scriptable * instance = archetype->Instantiate();
assert(m_scriptables.find(nameOfInstance) == m_scriptables.end());
instance->BindAll(m_binder, this);
m_scriptables.emplace(nameOfInstance, std::move(*instance));
delete instance;
QueueEvent(GetScriptable(nameOfInstance), m_tokens->AddToken("OnCreate"), paramBag);
return GetScriptable(nameOfInstance);
}
void ScriptWorld::QueueBroadcastEvent (const std::string & eventName) {
QueueEvent(0, eventName);
}
void ScriptWorld::QueueEvent (unsigned targetToken, const std::string & eventName) {
Event e;
e.nameToken = m_tokens->AddToken(eventName);
e.targetToken = targetToken;
e.directTarget = nullptr;
e.parameterBag = nullptr;
m_eventQueue.push_back(e);
}
void ScriptWorld::QueueEvent (Scriptable * target, unsigned eventToken, IFormulaPropertyBag * bag) {
Event e;
e.nameToken = eventToken;
e.targetToken = 0;
e.directTarget = target;
e.parameterBag = bag;
m_eventQueue.push_back(e);
}
void ScriptWorld::QueueDelayedEvent (unsigned targetToken, unsigned eventToken, IFormulaPropertyBag * paramBag, ValueT delaySeconds) {
Event e;
e.nameToken = eventToken;
e.targetToken = targetToken;
e.directTarget = nullptr;
e.parameterBag = paramBag;
e.timestamp = std::chrono::system_clock::now();
e.timestamp += std::chrono::milliseconds(long long(delaySeconds * 1000.0));
m_eventQueueTimed.push_back(e);
}
void ScriptWorld::QueueDelayedEvent (Scriptable * target, unsigned eventToken, IFormulaPropertyBag * paramBag, ValueT delaySeconds) {
Event e;
e.nameToken = eventToken;
e.targetToken = 0;
e.directTarget = target;
e.parameterBag = paramBag;
e.timestamp = std::chrono::system_clock::now();
e.timestamp += std::chrono::milliseconds(long long(delaySeconds * 1000.0));
m_eventQueueTimed.push_back(e);
}
void ScriptWorld::TransferTimedEvents () {
auto iter = std::stable_partition(m_eventQueueTimed.begin(), m_eventQueueTimed.end(), [](const Event & e) {
return (e.timestamp > std::chrono::system_clock::now());
});
m_eventQueue.insert(m_eventQueue.end(), iter, m_eventQueueTimed.end());
std::stable_sort(m_eventQueue.begin(), m_eventQueue.end(), [](const Event & a, const Event & b) {
return a.timestamp < b.timestamp;
});
m_eventQueueTimed.erase(iter, m_eventQueueTimed.end());
}
unsigned ScriptWorld::PeekTimeUntilNextEvent () const {
long long timeUntilEvent = 0xffffffff;
auto now(std::chrono::system_clock::now());
for (const auto & timedEvent : m_eventQueueTimed) {
auto delta = timedEvent.timestamp - now;
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(delta);
if (ms.count() < timeUntilEvent)
timeUntilEvent = ms.count();
}
return unsigned(timeUntilEvent);
}