|
22 | 22 | #include "moving_objects.h" |
23 | 23 | #include "doors_manager.h" |
24 | 24 |
|
25 | | -//----------------------- Event processing----------------------- |
26 | | - |
27 | | -CAI_Space::CEventCallback::CID CAI_Space::CEventCallbackStorage::RegisterCallback(CEventCallback* cb) |
28 | | -{ |
29 | | - m_lock.lock(); |
30 | | - |
31 | | - size_t i, cb_count = m_callbacks.size(); |
32 | | - |
33 | | - for (i = 0; i < cb_count; ++i) |
34 | | - { |
35 | | - if (!m_callbacks[i]) |
36 | | - { |
37 | | - break; |
38 | | - } |
39 | | - } |
40 | | - |
41 | | - if (i == cb_count) |
42 | | - { |
43 | | - m_callbacks.resize(cb_count + 1); |
44 | | - } |
45 | | - |
46 | | - m_callbacks[i].reset(cb); |
47 | | - |
48 | | - m_lock.unlock(); |
49 | | - return i; |
50 | | -} |
51 | | -bool CAI_Space::CEventCallbackStorage::UnregisterCallback(CEventCallback::CID cid) |
52 | | -{ |
53 | | - bool result = false; |
54 | | - m_lock.lock(); |
55 | | - |
56 | | - if (cid < m_callbacks.size() && m_callbacks[cid]) |
57 | | - { |
58 | | - m_callbacks[cid].reset(nullptr); |
59 | | - result = true; |
60 | | - } |
61 | | - |
62 | | - m_lock.unlock(); |
63 | | - return result; |
64 | | -} |
65 | | - |
66 | | -void CAI_Space::CEventCallbackStorage::ExecuteCallbacks() |
67 | | -{ |
68 | | - m_lock.lock(); |
69 | | - |
70 | | - for (auto& cb : m_callbacks) |
71 | | - { |
72 | | - if (cb) |
73 | | - { |
74 | | - cb->ProcessEvent(); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - m_lock.unlock(); |
79 | | -} |
80 | | - |
81 | | -CAI_Space::CEventCallback::CID CAI_Space::CNotifier::RegisterCallback(CEventCallback* cb, EEventID event_id) |
82 | | -{ |
83 | | - R_ASSERT(event_id < EVENT_COUNT); |
84 | | - return m_callbacks[event_id].RegisterCallback(cb); |
85 | | -} |
86 | | - |
87 | | -bool CAI_Space::CNotifier::UnregisterCallback(CEventCallback::CID cid, EEventID event_id) |
88 | | -{ |
89 | | - R_ASSERT(event_id < EVENT_COUNT); |
90 | | - return m_callbacks[event_id].UnregisterCallback(cid); |
91 | | -} |
92 | | - |
93 | | -void CAI_Space::CNotifier::FireEvent(EEventID event_id) |
94 | | -{ |
95 | | - R_ASSERT(event_id < EVENT_COUNT); |
96 | | - m_callbacks[event_id].ExecuteCallbacks(); |
97 | | -} |
98 | | - |
99 | | -//----------------------- Main CAI_Space stuff----------------------- |
100 | | - |
101 | 25 | static CAI_Space g_ai_space; |
102 | 26 |
|
103 | 27 | CAI_Space& CAI_Space::GetInstance() |
@@ -132,7 +56,11 @@ void CAI_Space::init() |
132 | 56 |
|
133 | 57 | CAI_Space::~CAI_Space() |
134 | 58 | { |
135 | | - m_events_notifier.FireEvent(CNotifier::EVENT_SCRIPT_ENGINE_RESET); |
| 59 | + if (GEnv.ScriptEngine != nullptr) |
| 60 | + { |
| 61 | + m_events_notifier.FireEvent(EVENT_SCRIPT_ENGINE_RESET); |
| 62 | + } |
| 63 | + |
136 | 64 | unload(); |
137 | 65 | xr_delete(GEnv.ScriptEngine); // XXX: wrapped into try..catch(...) in vanilla source |
138 | 66 | } |
@@ -211,15 +139,18 @@ void CAI_Space::RestartScriptEngine() |
211 | 139 | { |
212 | 140 | if (GEnv.ScriptEngine != nullptr) |
213 | 141 | { |
214 | | - m_events_notifier.FireEvent(CNotifier::EVENT_SCRIPT_ENGINE_RESET); |
| 142 | + m_events_notifier.FireEvent(EVENT_SCRIPT_ENGINE_RESET); |
215 | 143 | } |
216 | 144 |
|
217 | 145 | SetupScriptEngine(); |
218 | 146 | #ifdef DEBUG |
219 | 147 | get_moving_objects().clear(); |
220 | 148 | #endif // DEBUG |
221 | 149 |
|
222 | | - m_events_notifier.FireEvent(CNotifier::EVENT_SCRIPT_ENGINE_STARTED); |
| 150 | + if (GEnv.ScriptEngine != nullptr) |
| 151 | + { |
| 152 | + m_events_notifier.FireEvent(EVENT_SCRIPT_ENGINE_STARTED); |
| 153 | + } |
223 | 154 | } |
224 | 155 |
|
225 | 156 | void CAI_Space::load(LPCSTR level_name) |
@@ -266,12 +197,12 @@ void CAI_Space::set_alife(CALifeSimulator* alife_simulator) |
266 | 197 | SetGameGraph(nullptr); |
267 | 198 | } |
268 | 199 |
|
269 | | -CAI_Space::CEventCallback::CID CAI_Space::Subscribe(CEventCallback* cb, CNotifier::EEventID event_id) |
| 200 | +CEventNotifierCallback::CID CAI_Space::Subscribe(CEventNotifierCallback* cb, EEventID event_id) |
270 | 201 | { |
271 | 202 | return m_events_notifier.RegisterCallback(cb, event_id); |
272 | 203 | } |
273 | 204 |
|
274 | | -bool CAI_Space::Unsubscribe(CAI_Space::CEventCallback::CID cid, CNotifier::EEventID event_id) |
| 205 | +bool CAI_Space::Unsubscribe(CEventNotifierCallback::CID cid, EEventID event_id) |
275 | 206 | { |
276 | 207 | return m_events_notifier.UnregisterCallback(cid, event_id); |
277 | 208 | } |
0 commit comments