Subastra
Loading...
Searching...
No Matches
world.h
Go to the documentation of this file.
1#ifndef __H__ENGINE_WORLD__
2#define __H__ENGINE_WORLD__
3
4#include "../defs.h"
5#include "../list.h"
6#include "../map.h"
7#include "../vec2i.h"
8
9#include "entity.h"
10#include <string.h>
11
17
24
26
28 entity_id_t id;
29 if (world->vacant_entity_ids.size > 0) {
30 list_pop_tail(&world->vacant_entity_ids, &id);
31 return id;
32 }
33
34 id = world->_entities.size;
35
36 entity_t e;
37 memset(&e, 0, sizeof(entity_t));
39 e.self_id = id;
41
42 return id;
43}
44
47 u64 identity = entity_id_get_identity(new_id);
48
49 entity_t *ptr = list_get_ty_ptr(entity_t, &world->_entities, identity);
50
51 memset(ptr, 0, sizeof(entity_t));
53 ptr->self_id = new_id;
54 return new_id;
55}
56
58 if (!world->_entities.size)
59 return 0;
60
62
63 if (identity > world->_entities.size)
64 return 0;
65
66 entity_t *ptr = list_get_ty_ptr(entity_t, &world->_entities, identity);
67 if (ptr->self_id != id) {
68 if (ptr->kind == ENTITY_KIND_TOMBSTONE)
69 return 0;
70
71 PANIC(
72 "Ruined invariant! Entity at %016lX index %013lX is indexed by %016lX.",
74 return 0;
75 }
76
77 return ptr;
78}
79
85
88 return false;
89
90 u64 identity = entity_id_get_identity(id);
93 if (e->self_id != id)
94 // The element must have already been deleted
95 return false;
96
97 // Clear the entity and increment the generation now.
98 // This allows for any future indexing attempts against this
99 // specific entity to fail due to generation mismatch
100 memset(e, 0, sizeof(entity_t));
103 identity = entity_id_get_identity(e->self_id);
104 e->self_id = entity_id_new(gen + 1, identity);
105
107
108 return true;
109}
110
117
119 entity_iter_t ret;
120 ret.world = world;
121 ret.entity = 0;
122 ret.idx = 0;
124 return ret;
125}
126
128 entity_kind_mask_t mask) {
129 entity_iter_t ret;
130 ret.world = world;
131 ret.entity = 0;
132 ret.idx = 0;
133 ret.mask = mask;
134 return ret;
135}
136
138 while (it->idx < it->world->_entities.size) {
139 entity_t *candidate =
141 it->idx++;
142 if (candidate->kind & it->mask) {
143 it->entity = candidate;
144 return true;
145 }
146 }
147
148 it->entity = 0;
149 return false;
150}
151
153 vec2i chunk_relative,
154 vec2 chunk_local) {
156 entity_t *entity = world_get_entity(world, e);
157 entity_init_construct(entity, world->allocator, chunk_relative, chunk_local);
158 return entity;
159}
160
162 vec2 chunk_local, float scale) {
164 entity_t *entity = world_get_entity(world, e);
165
166 entity_init_camera(entity, world->allocator, chunk_relative, chunk_local,
167 scale);
168
169 return entity;
170}
171
172#endif
uint64_t u64
Definition defs.h:46
size_t sz
Definition defs.h:51
#define PANIC(msg,...)
Definition defs.h:15
uint32_t u32
Definition defs.h:45
entity_kind_t
Definition entity.h:34
@ ENTITY_KIND_TOMBSTONE
Definition entity.h:36
static void entity_init_construct(entity_t *entity, allocator_t alloc, vec2i chunk_relative, vec2 chunk_local)
Definition entity.h:62
u64 entity_id_t
Definition entity.h:11
static u64 entity_id_new(u32 gen, u64 id)
Definition entity.h:25
static u64 entity_id_get_identity(entity_id_t id)
Definition entity.h:21
static u32 entity_id_get_generation(entity_id_t id)
Definition entity.h:16
entity_kind_t entity_kind_mask_t
Definition entity.h:42
static void entity_init_camera(entity_t *entity, allocator_t alloc, vec2i chunk_relative, vec2 chunk_local, float scale)
Definition entity.h:83
#define list_get_ty_ptr(ty, ls, idx)
Definition list.h:128
#define list_init_ty(ty, ls, alloc)
Definition list.h:36
static void list_cleanup(list_t *ls)
Definition list.h:166
#define list_push_var(ls, var)
Definition list.h:83
static allocator_t allocator_new_malloc()
Wrap malloc into an generic allocator interface.
Definition memory.h:99
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
Definition world.h:111
entity_kind_mask_t mask
Definition world.h:115
entity_t * entity
Definition world.h:114
sz idx
Definition world.h:113
world_t * world
Definition world.h:112
Definition entity.h:44
entity_id_t self_id
Definition entity.h:46
entity_kind_t kind
Definition entity.h:45
Definition list.h:13
sz size
Definition list.h:17
Definition vec2.h:11
Definition vec2i.h:7
Definition world.h:12
allocator_t allocator
Definition world.h:13
list_t _entities
Definition world.h:14
list_t vacant_entity_ids
Definition world.h:15
static world_t world
Definition world.c:5
static entity_id_t world_reserve_entity_id(world_t *world)
Definition world.h:27
static entity_iter_t world_entity_iter_masked(world_t *world, entity_kind_mask_t mask)
Definition world.h:127
static entity_t * world_spawn_entity_construct(world_t *world, vec2i chunk_relative, vec2 chunk_local)
Definition world.h:152
static void world_init(world_t *world)
Definition world.h:18
static entity_id_t world_spawn_entity(world_t *world)
Definition world.h:45
static void world_cleanup(world_t *world)
Definition world.h:25
static sz world_count_entities(world_t *world)
Definition world.h:80
static entity_t * world_spawn_entity_camera(world_t *world, vec2i chunk_relative, vec2 chunk_local, float scale)
Definition world.h:161
static entity_t * world_get_entity(world_t *world, entity_id_t id)
Definition world.h:57
static bool world_destroy_entity(world_t *world, entity_id_t id)
Definition world.h:86
static bool entity_iter_next(entity_iter_t *it)
Definition world.h:137
static entity_iter_t world_entity_iter(world_t *world)
Definition world.h:118