Nebula
Loading...
Searching...
No Matches
world.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "core/refcounted.h"
11#include "entity.h"
12#include "entitypool.h"
13#include "component.h"
15#include "util/queue.h"
16#include "category.h"
17#include "processorid.h"
18#include "processor.h"
20#include "frameevent.h"
21#include "util/blob.h"
22
23namespace MemDb { class Database; }
24
25namespace Game
26{
27
28class PackedLevel;
29
31template <typename COMPONENT_TYPE>
32ComponentId RegisterType(ComponentRegisterInfo<COMPONENT_TYPE> info = {});
33
34//------------------------------------------------------------------------------
38{
40 TemplateId templateId = TemplateId::Invalid();
42 bool immediate = false;
43};
44
45//------------------------------------------------------------------------------
48class World
49{
50public:
51 // Generally, only the game server should create worlds
52 World(uint32_t hash);
53 ~World();
54
56 Entity CreateEntity(bool immediate = true);
60 void DeleteEntity(Entity entity);
62 bool IsValid(Entity entity);
64 bool HasInstance(Entity entity);
68 bool HasComponent(Entity entity, ComponentId component);
70 template <typename TYPE>
71 bool HasComponent(Entity entity);
75 template <typename TYPE>
76 void RemoveComponent(Entity entity);
78 void RemoveComponent(Entity entity, ComponentId component);
80 template <typename TYPE>
81 void SetComponent(Entity entity, TYPE const& value);
83 template <typename TYPE>
84 TYPE GetComponent(Entity entity);
86 template <typename TYPE>
87 TYPE* AddComponent(Entity entity);
89 void* AddComponent(Entity entity, ComponentId component);
92
94 void SetComponentValue(Entity entity, ComponentId component, void* value, uint64_t size);
95
97 void ReinitializeComponent(Entity entity, ComponentId component, void* value, uint64_t size);
98
100 Dataset Query(Filter filter);
104
109
112
114 void MarkAsModified(Game::Entity entity);
115
119 void UnloadLevel(PackedLevel* level);
120
122 void ExportLevel(Util::String const& path);
123
124 // -- Internal methods -- Use with caution! --
125
127 static void Override(World* src, World* dst);
131 void DeallocateEntity(Entity entity);
133 MemDb::RowId AllocateInstance(Entity entity, MemDb::TableId table, Util::Blob const* const data = nullptr);
139 void DeallocateInstance(MemDb::TableId table, MemDb::RowId instance);
141 void DeallocateInstance(Entity entity);
143 void Defragment(MemDb::TableId tableId);
145 void* GetInstanceBuffer(MemDb::TableId const tableId, uint16_t partitionId, ComponentId const component);
147 void* GetColumnData(MemDb::TableId const tableId, uint16_t partitionId, MemDb::ColumnIndex const column);
152
153 void RenderDebug();
154
155private:
156 friend class GameServer;
157 friend class BlueprintManager;
158 friend class PackedLevel;
159
181
182 // These functions are called from game server
183 void Start();
184 void BeginFrame();
185 void SimFrame();
186 void EndFrame();
187 void OnLoad();
188 void OnSave();
189 void ManageEntities();
190 void Reset();
191 void PrefilterProcessors();
193 bool Prefiltered() const;
195 void ClearDecayBuffers();
196
198
201
204
205 MemDb::RowId Migrate(Entity entity, MemDb::TableId newTable);
206 void Migrate(
207 Util::Array<Entity> const& entities,
208 MemDb::TableId fromTable,
209 MemDb::TableId newTable,
211 );
212
213 void DecayComponent(ComponentId component, MemDb::TableId tableId, MemDb::ColumnIndex column, MemDb::RowId instance);
214
216
219
229 uint32_t hash;
240
243
245 bool cacheValid = false;
246
249
251};
252
253//------------------------------------------------------------------------------
256template <typename COMPONENT_TYPE>
259{
260 uint32_t componentFlags = 0;
261 componentFlags |= (uint32_t)COMPONENTFLAG_DECAY * (uint32_t)info.decay;
262
263 ComponentInterface* cInterface = new ComponentInterface(
264 COMPONENT_TYPE::Traits::name,
265 COMPONENT_TYPE(),
266 componentFlags
267 );
268 cInterface->Init = reinterpret_cast<ComponentInterface::ComponentInitFunc>(info.OnInit);
272 return cid;
273}
274
275//------------------------------------------------------------------------------
278template <typename TYPE>
279inline void
280World::SetComponent(Entity entity, TYPE const& value)
281{
282#if NEBULA_DEBUG
283 n_assert2(
284 !this->pipeline.IsRunningAsync(),
285 "Settings components in arbitrary entities while executing an async processor is currently not supported!"
286 );
287#endif
288
289#if NEBULA_DEBUG
290 n_assert2(
292 "SetComponent: Provided value's type is not the correct size for the given ComponentId."
293 );
294#endif
295 EntityMapping mapping = this->GetEntityMapping(entity);
296 TYPE* ptr = (TYPE*)this->GetInstanceBuffer(mapping.table, mapping.instance.partition, GetComponentId<TYPE>());
297 *(ptr + mapping.instance.index) = value;
298}
299
300//------------------------------------------------------------------------------
303template <typename TYPE>
304inline TYPE
306{
307#if NEBULA_DEBUG
308 n_assert2(
309 !this->pipeline.IsRunningAsync(), "Getting components from entities while executing an async processor is currently not supported!"
310 );
311#endif
312
313#if NEBULA_DEBUG
314 n_assert2(
316 "GetComponent: Provided value's type is not the correct size for the given ComponentId."
317 );
318#endif
319 EntityMapping mapping = this->GetEntityMapping(entity);
320 TYPE* ptr = (TYPE*)this->GetInstanceBuffer(mapping.table, mapping.instance.partition, GetComponentId<TYPE>());
321 return *(ptr + mapping.instance.index);
322}
323
324//------------------------------------------------------------------------------
327template <typename TYPE>
328inline void
330{
331#if NEBULA_DEBUG
332 n_assert2(
333 !this->pipeline.IsRunningAsync(),
334 "Removing components from entities while executing an async processor is currently not supported!"
335 );
336#endif
338 .entity = entity,
339 .componentId = Game::GetComponentId<TYPE>(),
340 };
341 this->removeComponentQueue.Append(cmd);
342}
343
344//------------------------------------------------------------------------------
347template <typename TYPE>
348inline bool
350{
351 return this->HasComponent(entity, Game::GetComponentId<TYPE>());
352}
353
354//------------------------------------------------------------------------------
357template <typename TYPE>
358inline TYPE*
360{
361 /*
362 We could possibly support this by either having thread local allocators and
363 staged queues and gathering after the processors have finished (must clear
364 allocators by issuing a separate job for this at the end of the frame), or by
365 just introducing a simple mutex.
366 */
367#if NEBULA_DEBUG
368 n_assert2(!this->pipeline.IsRunningAsync(), "Adding component to entities while in an async processor is currently not supported!");
369#endif
371#if NEBULA_DEBUG
372 n_assert(MemDb::AttributeRegistry::TypeSize(id) == sizeof(TYPE));
373 //n_assert(!this->HasComponent<TYPE>(entity));
374#endif
375 TYPE* data = this->componentStageAllocator.Alloc<TYPE>();
376 const void* defaultValue = MemDb::AttributeRegistry::DefaultValue(id);
377 Memory::Copy(defaultValue, data, sizeof(TYPE));
378
380 .entity = entity,
381 .componentId = id,
382 .dataSize = sizeof(TYPE),
383 .data = data,
384 };
385 this->addStagedQueue.Append(cmd);
386
388 ComponentInterface* cInterface;
389 cInterface = static_cast<ComponentInterface*>(attr);
390
391 if (cInterface->Init != nullptr)
392 {
393 // run initialization function if it exists.
394 cInterface->Init(this, entity, data);
395 }
396
397 return data;
398}
399
403
404template<> void World::SetComponent(Entity entity, Game::Position const&);
405template<> void World::SetComponent(Entity entity, Game::Orientation const&);
406template<> void World::SetComponent(Entity entity, Game::Scale const&);
407
408
409} // namespace Game
Loads the 'data:tables/blueprint.json' file and subsequently sets up categories based on the blueprin...
Definition blueprintmanager.h:31
static void Register(ComponentId component, DrawFunc)
Definition componentinspection.cc:55
These are registered to the attribute registry so that we can add more functionality to attributes.
Definition component.h:75
void(*)(Game::World *, Game::Entity, void *) ComponentInitFunc
Definition component.h:90
ComponentInitFunc Init
Definition component.h:91
static void Register(ComponentId component)
Definition componentserialization.h:70
Generation pool.
Definition entitypool.h:22
Definition frameevent.h:108
bool IsRunningAsync()
check if the pipeline is currently executing an async frame event batch
Definition frameevent.cc:453
The game server setups and runs the game world.
Definition gameserver.h:45
Definition level.h:23
Definition world.h:49
PackedLevel * PreloadLevel(Util::String const &path)
preload a level that can be instantiated
Definition world.cc:80
void MoveInstance(MemDb::Table::Partition *partition, MemDb::RowId from, MemDb::RowId to)
Definition world.cc:1305
Dataset Query(Filter filter)
Query the entity database using specified filter set. This does NOT wait for resources to be availabl...
Definition world.cc:1669
void AddStagedComponentsToEntity(Entity entity, AddStagedComponentCommand *cmds, SizeT numCmds)
Definition world.cc:855
void DeallocateEntity(Entity entity)
Deallocate an entity id. Use this with caution!
Definition world.cc:386
bool componentInitializationEnabled
Disable if initialization of components is not required (ex. when running as editor db)
Definition world.h:151
EntityPool pool
used to allocate entity ids for this world
Definition world.h:221
Util::Array< RemoveComponentCommand > removeComponentQueue
Definition world.h:239
MemDb::RowId AllocateInstance(Entity entity, MemDb::TableId table, Util::Blob const *const data=nullptr)
Allocate an entity instance in a table. Use this with caution!
Definition world.cc:1078
ComponentDecayBuffer const GetDecayBuffer(ComponentId component)
Get a decay buffer for the given component.
Definition world.cc:714
Ptr< MemDb::Database > db
contains all entity instances
Definition world.h:227
Util::HashTable< BlueprintId, MemDb::TableId > blueprintToTableMap
maps from blueprint to a table that has the same signature
Definition world.h:231
void InitializeAllComponents(Entity entity, MemDb::TableId tableId, MemDb::RowId row)
Run OnInit on all components. Use with caution, since they can only be initialized once and the funct...
Definition world.cc:1120
bool HasInstance(Entity entity)
Check if an entity has an instance. It might be valid, but not have received an instance just after i...
Definition world.cc:748
TYPE GetComponent(Entity entity)
Get an entitys component.
Definition world.h:305
void PrefilterProcessors()
Definition world.cc:463
World(uint32_t hash)
Definition world.cc:40
void ClearDecayBuffers()
Clears all decay buffers. This is called by the game server automatically.
Definition world.cc:726
void ExecuteAddComponentCommands()
dispatches all staged components to be added to entities
Definition world.cc:788
Entity CreateEntity(bool immediate=true)
Create a new empty entity.
Definition world.cc:541
void RenderDebug()
Definition world.cc:1416
bool cacheValid
set to true if the caches for the frame pipeline is valid
Definition world.h:245
Util::Queue< AllocateInstanceCommand > allocQueue
Definition world.h:233
uint32_t hash
world hash
Definition world.h:229
bool HasComponent(Entity entity, ComponentId component)
Check if entity has a specific component. (SLOW!)
Definition world.cc:769
void SetComponentValue(Entity entity, ComponentId component, void *value, uint64_t size)
Set the value of a component by providing a pointer and type size.
Definition world.cc:1359
void Reset()
Definition world.cc:522
void SimFrame()
Definition world.cc:419
void DeleteEntity(Entity entity)
Delete entity.
Definition world.cc:595
void ReinitializeComponent(Entity entity, ComponentId component, void *value, uint64_t size)
Set the value of a component by providing a pointer and type size, then reinitialize the component.
Definition world.cc:1377
void UnloadLevel(PackedLevel *level)
unload a preloaded level
Definition world.cc:197
~World()
Definition world.cc:71
FramePipeline pipeline
the frame pipeline for this world
Definition world.h:248
bool IsValid(Entity entity)
Check if an entity ID is still valid.
Definition world.cc:739
SizeT GetNumInstances(MemDb::TableId tid)
Get total number of instances in an entity table.
Definition world.cc:779
void * GetColumnData(MemDb::TableId const tableId, uint16_t partitionId, MemDb::ColumnIndex const column)
Get a pointer to the first instance of a column in a partition of an entity table....
Definition world.cc:1000
MemDb::TableId CreateEntityTable(EntityTableCreateInfo const &info)
Create a table in the entity database that has a specific set of components.
Definition world.cc:1019
void DecayComponent(ComponentId component, MemDb::TableId tableId, MemDb::ColumnIndex column, MemDb::RowId instance)
Definition world.cc:617
void ExportLevel(Util::String const &path)
Export the world as a level.
Definition world.cc:206
EntityMapping GetEntityMapping(Entity entity)
Returns the entity mapping of an entity.
Definition world.cc:758
void RemoveComponentsFromEntity(Entity entity, RemoveComponentCommand *cmds, SizeT numCmds)
Definition world.cc:924
void RemoveComponent(Entity entity)
Remove a component from an entity.
Definition world.h:329
void ExecuteRemoveComponentCommands()
Definition world.cc:822
Util::Array< EntityMapping > entityMap
maps entity index to table+row pair
Definition world.h:225
FramePipeline & GetFramePipeline()
Get the frame pipeline.
Definition world.cc:1644
void BeginFrame()
Definition world.cc:409
void Start()
Definition world.cc:397
void EndFrame()
Definition world.cc:429
Ptr< MemDb::Database > GetDatabase()
Get the entity database. Be careful when directly modifying the database, as some information is only...
Definition world.cc:532
void Defragment(MemDb::TableId tableId)
Defragment an entity table.
Definition world.cc:1335
void DeallocateInstance(MemDb::TableId table, MemDb::RowId instance)
Deallocate an entity instance. Use this with caution!
Definition world.cc:1209
MemDb::RowId Migrate(Entity entity, MemDb::TableId newTable)
Definition world.cc:1245
static void Override(World *src, World *dst)
copies and overrides dst with src. This is extremely destructive - make sure you understand the impli...
Definition world.cc:1603
MemDb::RowId GetInstance(Entity entity)
Get instance of entity.
Definition world.cc:1010
void ManageEntities()
Definition world.cc:482
TYPE * AddComponent(Entity entity)
Create a component. This queues the component in a command buffer to be added later.
Definition world.h:359
bool Prefiltered() const
Check if the database is fully prefiltered.
Definition world.cc:473
Util::Queue< DeallocInstanceCommand > deallocQueue
Definition world.h:235
Entity AllocateEntity()
Allocate an entity id. Use this with caution!
Definition world.cc:371
void * GetInstanceBuffer(MemDb::TableId const tableId, uint16_t partitionId, ComponentId const component)
Get a pointer to the first instance of a component in a partition of an entity table....
Definition world.cc:982
void OnSave()
Definition world.cc:454
Util::Array< AddStagedComponentCommand > addStagedQueue
Definition world.h:237
Memory::ArenaAllocator< 4096_KB > componentStageAllocator
allocator for staged components
Definition world.h:242
void OnLoad()
Definition world.cc:445
SizeT numEntities
Number of entities alive.
Definition world.h:223
MemDb::TableId defaultTableId
Definition world.h:250
void MarkAsModified(Game::Entity entity)
Mark an entity as modified in its table.
Definition world.cc:1653
void SetComponent(Entity entity, TYPE const &value)
Set the value of an entitys component.
Definition world.h:280
Definition attribute.h:29
static AttributeId Register(Util::StringAtom name, TYPE defaultValue, uint32_t flags=0)
register a type (templated)
Definition attributeregistry.h:99
static void const *const DefaultValue(AttributeId descriptor)
get attribute default value pointer
Definition attributeregistry.h:268
static SizeT TypeSize(AttributeId descriptor)
get type size by attribute id
Definition attributeregistry.h:246
static Attribute * GetAttribute(AttributeId descriptor)
get attribute description by id
Definition attributeregistry.h:228
Definition table.h:169
Allocates memory in chunks.
Definition arenaallocator.h:36
T * Alloc()
allocate new object, and calls constructor, but beware because this allocator does not run the destru...
Definition arenaallocator.h:186
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:60
The Util::Blob class encapsulates a chunk of raw memory into a C++ object which can be copied,...
Definition blob.h:22
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Organizes key/value pairs by a hash code.
Definition hashtable.h:42
Nebula's queue class (a FIFO container).
Definition queue.h:28
#define n_assert2(exp, msg)
Definition debug.h:51
#define n_assert(exp)
Definition debug.h:50
Definition orientation.h:7
ComponentId RegisterType(ComponentRegisterInfo< COMPONENT_TYPE > info={})
Register a component type.
Definition world.h:258
MemDb::AttributeId ComponentId
Definition componentid.h:15
@ COMPONENTFLAG_DECAY
Component will decay.
Definition component.h:38
uint32_t Filter
Opaque filter identifier.
Definition filter.h:26
void ComponentDrawFuncT(ComponentId component, void *data, bool *commit)
Definition componentinspection.h:71
ComponentId GetComponentId()
Returns a component id, based on template type.
Definition component.h:120
Attribute.
Definition attribute.h:26
void Copy(const void *from, void *to, size_t numBytes)
Copy a chunk of memory (note the argument order is different from memcpy()!!!)
Definition osxmemory.cc:213
Definition category.h:17
Contains data for components flagged with COMPONENTFLAG_DECAY, that has been deleted this frame.
Definition component.h:47
Used for registering component types to the game world.
Definition component.h:61
OnInitFunc OnInit
initialization function to run for the component, or nullptr if not needed.
Definition component.h:67
bool decay
Set to true if the component should end up in the decay buffer before being completely destroyed.
Definition component.h:65
A dataset that contains views into category tables.
Definition dataset.h:23
Definition world.h:38
TemplateId templateId
template to instantiate.
Definition world.h:40
bool immediate
set if the entity should be instantiated immediately or deferred until end of frame.
Definition world.h:42
An entity is essentially just an Id with some utility functions attached.
Definition entity.h:31
Maps an entity to a table and instance id.
Definition entity.h:81
MemDb::RowId instance
Definition entity.h:83
MemDb::TableId table
Definition entity.h:82
Definition category.h:21
Definition orientation.h:10
Definition position.h:10
Definition scale.h:10
Definition category.h:18
ComponentId componentId
Definition world.h:172
SizeT dataSize
Definition world.h:173
void * data
Definition world.h:174
Game::Entity entity
Definition world.h:171
Game::Entity entity
Definition world.h:162
TemplateId tid
Definition world.h:163
Game::Entity entity
Definition world.h:167
ComponentId componentId
Definition world.h:179
Entity entity
Definition world.h:178
Definition attributeid.h:19
column id
Definition tableid.h:38
row identifier
Definition tableid.h:18
uint16_t partition
Definition tableid.h:19
uint16_t index
Definition tableid.h:20
Table identifier.
Definition tableid.h:14
Nebula's universal string class.
Definition string.h:50
int SizeT
Definition types.h:49