summaryrefslogtreecommitdiff
path: root/world.cpp
blob: 5cfd8bf9fbf417fde9f264404649e2231217ea7e (plain) (blame)
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
#include "world.hpp"
extern "C" {
#include "memory.h"
#include "plat.h"
}

#include <string.h>

static int component_sizes[max_components];

void Pool::init(Arena* a, Component_Mask m) {
	int i, coff;
	next = 0;
	size = 0;
	mask = m;
	count = 0;
	for (i = 0, coff = 0; i < max_components; i++) {
		if (m & ((Component_Mask)1 << i)) {
			offsets[i] = coff;
			coff += component_sizes[i];
		}
	}
	size = coff;
	data = arena_alloc(a, size * pool_cap);
}

void* Pool::add(Arena* a, Entity_Id eid) {
	int idx;
	if (next)
		return next->add(a, eid);
	if (count >= pool_cap) {
		next = (Pool*)arena_alloc(a, sizeof *next);
		next->init(a, mask);
		return next->add(a, eid);
	}
	idx = count++;
	entities[idx] = eid;
	mapping[entity_index(eid)] = idx;
	return &((char*)data)[idx * size];
}

void Pool::remove(Entity_Id eid) {
	int eind = entity_index(eid);
	int end = count - 1;
	assert(eid == entities[mapping[eind]]);
	memmove(
		(char*)data + eind * size,
		(char*)data + end * size,
		size
	);
	entities[eind] = entities[count];
	mapping[end] = eind;
	count = end;
}

void* Pool::get(Entity_Id eid, int cid) {
	int eind = entity_index(eid);
	char* e = &((char*)data)[mapping[eind] * size];
	assert(eid == entities[mapping[eind]]);
	return &e[offsets[cid]];
}

void* Pool::get(Entity_Id eid) {
	int eind = entity_index(eid);
	void* e = &((char*)data)[mapping[eind] * size];
	assert(eid == entities[mapping[eind]]);
	return e;
}

int get_new_component_id(int size) {
  static int id = 0;
  int i = id++;
  component_sizes[i] = size;
  return i;
}

int entity_version(Entity_Id e) {
	return e & 0xff;
}

int entity_index(Entity_Id e) {
	return (e >> 8) & 0xffffff;
}

Entity_Id entity_id(int index, int version) {
	return ((index & 0xffffff) << 8) | (version & 0xff);
}

void World::init(Arena* a) {
	int i;
	arena = a;
	count = 0;
	free_count = 0;
	for (i = 0; i < max_entities; i++)
		versions[i] = 1;
	for (i = 0; i < max_pools; i++)
		pools[i].mask = 0;
}

uint64_t World::hash_mask(Component_Mask m) {
	uint64_t h = (uint64_t)m;
	h = (h ^ (h >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
	h = (h ^ (h >> 27)) * UINT64_C(0x94d049bb133111eb);
	h = (h ^ (h >> 31));
	return h;
}

Pool& World::get_pool(Component_Mask m) {
	int i, idx;
	idx = (int)(hash_mask(m) % max_pools);
	for (i = 0; i < max_pools; i++) {
		Pool& p = pools[idx];
		if (!p.mask) {
			p.init(arena, m);
			return p;
		} else if (p.mask == m)
			return p;
		idx++;
		idx %= max_pools;
	}
	assert(0);
	return pools[0];
}

Entity_Id World::create_entity() {
	Entity_Id e;
	int ind;
	assert(count < max_entities);
	if (free_count)
		ind = entity_index(freelist[--free_count]);
	else
		ind = count++;
	e = entity_id(ind, versions[ind]);
	masks[ind] = 0;
	entities[ind] = e;
	return e;
}

void* World::add(Entity_Id eid, Component_Mask m) {
	int eind = entity_index(eid);
	int i;
	assert(entity_version(eid) == versions[eind]);
	Component_Mask om = masks[eind];
	Component_Mask tm = om | m;
	assert(om != tm);
	Pool& p = get_pool(tm);
	p.add(arena, eid);
	if (om) {
		Pool& op = get_pool(om);
		for (i = 0; i < max_components; i++) {
			if (om & ((Component_Mask)1 << i)) {
				memcpy(
					p.get(eid, i),
					op.get(eid, i),
					component_sizes[i]
				);
			}
		}
		op.remove(eid);
	}
	masks[eind] = tm;
	return p.get(eid);
}

void* World::get(Entity_Id eid, int cid) {
	int eind = entity_index(eid);
	Pool& p = get_pool(masks[eind]);
	assert(versions[eind] == entity_version(eid));
	assert(p.entities[p.mapping[eind]] == eid);
	return p.get(eid, cid);
}

void World::remove(Entity_Id eid, int cid) {
	int eind = entity_index(eid), i;
	Component_Mask om = masks[eind];
	Component_Mask nm = om & ~((Component_Mask)1 << cid);
	Pool& s = get_pool(om);
	if (nm) {
		Pool& d = get_pool(nm);
		for (i = 0; i < max_components; i++) {
			if (nm & ((uint64_t)1 << i)) {
				memcpy(
					d.get(eid, i),
					s.get(eid, i),
					component_sizes[i]
				);
			}
		}
		s.remove(eid);
	}
	s.remove(eid);
	masks[eind] = nm;
}

void World::destroy(Entity_Id e) {
	int eind = entity_index(e);
	Component_Mask m = masks[eind];
	Pool& p = get_pool(m);
	p.remove(e);
	assert(free_count < max_entities);
	freelist[free_count++] = e;
	versions[entity_index(e)]++;
}