-
Notifications
You must be signed in to change notification settings - Fork 26
/
DaedalusGameState.h
448 lines (399 loc) · 17.3 KB
/
DaedalusGameState.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#pragma once
#include <list>
#include <vector>
#include "DATFile.h"
#include "DaedalusStdlib.h"
#include <utils/staticReferencedAllocator.h>
namespace ZenLib
{
namespace Daedalus
{
class DaedalusVM;
namespace GameState
{
constexpr int MAX_NUM_MISC = 1024;
constexpr int MAX_NUM_NPCS = 12000;
constexpr int MAX_NUM_ITEMS = 12000;
constexpr int MAX_NUM_MISSIONS = 512;
constexpr int MAX_NUM_FOCUS = MAX_NUM_MISC;
constexpr int MAX_NUM_ITEMREACT = MAX_NUM_MISC;
constexpr int MAX_NUM_INFO = 16000;
constexpr int MAX_NUM_MENU = MAX_NUM_MISC;
constexpr int MAX_NUM_MENUITEM = MAX_NUM_MISC;
constexpr int MAX_NUM_SFX = 4096; // G2 has 1700
constexpr int MAX_NUM_PFX = 1024;
constexpr int MAX_NUM_MUSICTHEME = 512;
template <class T>
constexpr size_t MAX_NUM();
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Npc>()
{
return MAX_NUM_NPCS;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Item>()
{
return MAX_NUM_ITEMS;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Mission>()
{
return MAX_NUM_MISSIONS;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Focus>()
{
return MAX_NUM_FOCUS;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_ItemReact>()
{
return MAX_NUM_ITEMREACT;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Info>()
{
return MAX_NUM_INFO;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Menu>()
{
return MAX_NUM_MENU;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_Menu_Item>()
{
return MAX_NUM_MENUITEM;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_SFX>()
{
return MAX_NUM_SFX;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_ParticleFX>()
{
return MAX_NUM_PFX;
}
template <>
constexpr size_t MAX_NUM<Daedalus::GEngineClasses::C_MusicTheme>()
{
return MAX_NUM_MUSICTHEME;
}
template <typename C_Class>
using CHandle = typename ZMemory::StaticReferencedAllocator<C_Class, MAX_NUM<C_Class>()>::Handle;
typedef CHandle<Daedalus::GEngineClasses::C_Npc> NpcHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Item> ItemHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Mission> MissionHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Focus> FocusHandle;
typedef CHandle<Daedalus::GEngineClasses::C_ItemReact> ItemReactHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Info> InfoHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Menu> MenuHandle;
typedef CHandle<Daedalus::GEngineClasses::C_Menu_Item> MenuItemHandle;
typedef CHandle<Daedalus::GEngineClasses::C_SFX> SfxHandle;
typedef CHandle<Daedalus::GEngineClasses::C_ParticleFX> PfxHandle;
typedef CHandle<Daedalus::GEngineClasses::C_MusicTheme> MusicThemeHandle;
struct LogTopic
{
enum ELogStatus
{
// TODO: Find the actual values for this
LS_Running = 1,
LS_Success = 2,
LS_Failed = 3,
LS_Obsolete = 4
};
enum ESection
{
// TODO: Find the actual values for this!
LT_Mission = 0,
LT_Note = 1
};
std::vector<std::string> entries;
ELogStatus status;
ESection section;
};
struct RegisteredObjects
{
template <class T, size_t n = MAX_NUM<T>()>
using CAllocator = ZMemory::StaticReferencedAllocator<T, n>;
CAllocator<GEngineClasses::C_Npc> NPCs;
CAllocator<GEngineClasses::C_Item> items;
CAllocator<GEngineClasses::C_ItemReact> itemReacts;
CAllocator<GEngineClasses::C_Mission> missions;
CAllocator<GEngineClasses::C_Focus> focuses;
CAllocator<GEngineClasses::C_Info> infos;
CAllocator<GEngineClasses::C_Menu> menus;
CAllocator<GEngineClasses::C_Menu_Item> menuItems;
CAllocator<GEngineClasses::C_SFX> sfx;
CAllocator<GEngineClasses::C_ParticleFX> pfx;
CAllocator<GEngineClasses::C_MusicTheme> musicThemes;
/**
* generic getter for the Engine containers NPCs, items, ...
* @tparam C_Class i.e. C_Npc C_Item, ...
*/
template <class C_Class, size_t n = MAX_NUM<C_Class>()>
CAllocator<C_Class, n>& get()
{
C_Class::missing_template_function_spezialization;
};
};
/**
* getter spezializations
*/
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Npc>&
RegisteredObjects::get<GEngineClasses::C_Npc, MAX_NUM<GEngineClasses::C_Npc>()>()
{
return NPCs;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Item>&
RegisteredObjects::get<GEngineClasses::C_Item, MAX_NUM<GEngineClasses::C_Item>()>()
{
return items;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Mission>&
RegisteredObjects::get<GEngineClasses::C_Mission, MAX_NUM<GEngineClasses::C_Mission>()>()
{
return missions;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Focus>&
RegisteredObjects::get<GEngineClasses::C_Focus, MAX_NUM<GEngineClasses::C_Focus>()>()
{
return focuses;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_ItemReact>&
RegisteredObjects::get<GEngineClasses::C_ItemReact, MAX_NUM<GEngineClasses::C_ItemReact>()>()
{
return itemReacts;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Info>&
RegisteredObjects::get<GEngineClasses::C_Info, MAX_NUM<GEngineClasses::C_Info>()>()
{
return infos;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Menu>&
RegisteredObjects::get<GEngineClasses::C_Menu, MAX_NUM<GEngineClasses::C_Menu>()>()
{
return menus;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_Menu_Item>&
RegisteredObjects::get<GEngineClasses::C_Menu_Item, MAX_NUM<GEngineClasses::C_Menu_Item>()>()
{
return menuItems;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_SFX>&
RegisteredObjects::get<GEngineClasses::C_SFX, MAX_NUM<GEngineClasses::C_SFX>()>()
{
return sfx;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_ParticleFX>&
RegisteredObjects::get<GEngineClasses::C_ParticleFX, MAX_NUM<GEngineClasses::C_ParticleFX>()>()
{
return pfx;
};
template <>
inline RegisteredObjects::CAllocator<GEngineClasses::C_MusicTheme>&
RegisteredObjects::get<GEngineClasses::C_MusicTheme, MAX_NUM<GEngineClasses::C_MusicTheme>()>()
{
return musicThemes;
};
/**
* Class holding the current engine-side gamestate of a daedalus-VM
*/
class DaedalusGameState
{
public:
DaedalusGameState(Daedalus::DaedalusVM& vm);
/**
* @brief registers the games externals
*/
void registerExternals();
struct GameExternals
{
// These will all be executed with the content already created
std::function<void(NpcHandle, std::string)> wld_insertnpc;
std::function<void(NpcHandle)> post_wld_insertnpc;
std::function<void(NpcHandle)> wld_removenpc;
std::function<void(ItemHandle)> wld_insertitem;
std::function<void(ItemHandle, NpcHandle)> createinvitem;
std::function<int(void)> wld_GetDay;
std::function<void(std::string)> log_createtopic;
std::function<void(std::string)> log_settopicstatus;
std::function<void(std::string, std::string)> log_addentry;
};
void setGameExternals(const GameExternals& ext)
{
m_GameExternals = ext;
}
/**
* Creates a new instance of the given item first and then adds it to the given NPC
* @param itemSymbol Symbol to create the instance of
* @param npc NPC to give the item to
* @param count How many to give
* @return Newly created handle or an old one, if the item was stackable
*/
ItemHandle createInventoryItem(size_t itemSymbol, NpcHandle npc, unsigned int count = 1);
/**
* Adds the given item to the inventory of the given NPC
* Note: If there already is an instance of this item and it is stackable, the old instance will
* be returned and the instance given in 'item' will be removed!
* @return Handle to the item now in the inventory or an old one, if the item was stackable
*/
ItemHandle addItemToInventory(ItemHandle item, NpcHandle npc);
/**
* Removes one of the items matching the given instance from the NPCs inventory
* Note: Handle will not be valid anymore if this returns true!
* @return True, if such an instance was found and removed, false otherwise
*/
bool removeInventoryItem(size_t itemSymbol, NpcHandle npc, unsigned int count = 1);
/**
* Removes the data behind the given item handle
* @param item Item to remove
*/
void removeItem(ItemHandle item);
void removeNPC(NpcHandle npc);
void removeMenu(MenuHandle menu);
void removeMenuItem(MenuItemHandle menuItem);
/**
* Creates a new NPC-Instance, just as Wld_InsertNPC was called from script
* @param instance Instance-Index of the NPC to create
* @param waypoint Waypoint to place the newly created npc on
* @return Handle to the script-instance created
*/
NpcHandle insertNPC(size_t instance, const std::string& waypoint);
NpcHandle insertNPC(const std::string& instance, const std::string& waypoint);
/**
* Creates a new item instance and initializes it (does not position it)
* @param instance Instance to create
* @return Handle to the created instance
*/
ItemHandle insertItem(size_t instance);
ItemHandle insertItem(const std::string& instance);
SfxHandle insertSFX(size_t instance);
SfxHandle insertSFX(const std::string& instance);
MusicThemeHandle insertMusicTheme(size_t instance);
MusicThemeHandle insertMusicTheme(const std::string& instance);
/**
* Creates scripting relevant objects
*/
template <typename C_Class>
CHandle<C_Class> create();
NpcHandle createNPC();
ItemHandle createItem();
ItemReactHandle createItemReact();
MissionHandle createMission();
InfoHandle createInfo();
FocusHandle createFocus();
MenuHandle createMenu();
MenuItemHandle createMenuItem();
SfxHandle createSfx();
PfxHandle createPfx();
MusicThemeHandle createMusicTheme();
/**
* Accessors
*/
template <typename C_Class>
inline C_Class& get(CHandle<C_Class> h)
{
return m_RegisteredObjects.get<C_Class>().getElement(h);
};
Daedalus::GEngineClasses::C_Npc& getNpc(NpcHandle h)
{
return get<Daedalus::GEngineClasses::C_Npc>(h);
};
Daedalus::GEngineClasses::C_Item& getItem(ItemHandle h)
{
return get<Daedalus::GEngineClasses::C_Item>(h);
};
Daedalus::GEngineClasses::C_ItemReact& getItemReact(ItemReactHandle h)
{
return get<Daedalus::GEngineClasses::C_ItemReact>(h);
};
Daedalus::GEngineClasses::C_Mission& getMission(MissionHandle h)
{
return get<Daedalus::GEngineClasses::C_Mission>(h);
};
Daedalus::GEngineClasses::C_Focus& getFocus(FocusHandle h)
{
return get<Daedalus::GEngineClasses::C_Focus>(h);
};
Daedalus::GEngineClasses::C_Info& getInfo(InfoHandle h)
{
return get<Daedalus::GEngineClasses::C_Info>(h);
};
Daedalus::GEngineClasses::C_Menu& getMenu(MenuHandle h)
{
return get<Daedalus::GEngineClasses::C_Menu>(h);
};
Daedalus::GEngineClasses::C_Menu_Item& getMenuItem(MenuItemHandle h)
{
return get<Daedalus::GEngineClasses::C_Menu_Item>(h);
};
Daedalus::GEngineClasses::C_SFX& getSfx(SfxHandle h)
{
return get<Daedalus::GEngineClasses::C_SFX>(h);
};
Daedalus::GEngineClasses::C_ParticleFX& getPfx(PfxHandle h)
{
return get<Daedalus::GEngineClasses::C_ParticleFX>(h);
};
Daedalus::GEngineClasses::C_MusicTheme& getMusicTheme(MusicThemeHandle h)
{
return get<Daedalus::GEngineClasses::C_MusicTheme>(h);
};
Daedalus::GEngineClasses::Instance* getByClass(ZMemory::BigHandle h, EInstanceClass instClass);
RegisteredObjects& getRegisteredObjects()
{
return m_RegisteredObjects;
}
const std::list<ItemHandle>& getInventoryOf(NpcHandle npc)
{
return m_NpcInventories[npc];
}
/**
* @param callback Function to be called right after an instance was created
*/
void setOnInstanceCreatedCallback(const std::function<void(ZMemory::BigHandle, EInstanceClass)>& callback)
{
m_OnInstanceCreated = callback;
}
/**
* @param callback Function to be called right before the actual instance gets removed
*/
void setOnInstanceRemovedCallback(const std::function<void(ZMemory::BigHandle, EInstanceClass)>& callback)
{
m_OnInstanceRemoved = callback;
}
private:
RegisteredObjects m_RegisteredObjects;
/**
* Daedalus-VM this is running on
*/
Daedalus::DaedalusVM& m_VM;
/**
* Inventories by npc handle
*/
std::map<NpcHandle, std::list<ItemHandle>> m_NpcInventories;
/**
* External-callback set by the user
*/
GameExternals m_GameExternals;
/**
* Function called when an instance was created/removed
*/
std::function<void(ZMemory::BigHandle, EInstanceClass)> m_OnInstanceCreated;
std::function<void(ZMemory::BigHandle, EInstanceClass)> m_OnInstanceRemoved;
};
} // namespace GameState
} // namespace Daedalus
} // namespace ZenLib