summaryrefslogtreecommitdiff
path: root/src/world.cpp
blob: a41b7937af842a593cac8bb8ad05b93a001f6e23 (plain)
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
#include "common.hpp"
#include <sstream>

namespace world {

sector_index_t sector_index_at(v2f_t x)
{
	return sector_index_t((x / SECTOR_SIZE).floor());
}

tile_index_t tile_index_at(v2f_t x)
{
	return sector_index_t(x.floor());
}

world_t::world_t(void)
{
	prng.seed(125);
	perlin.generate(&prng, 32);
}

// FIXME: rename
static cflags_t tiles[256] = {0};

void register_tile(uint8_t type, cflags_t cflags)
{
	tiles[type] = cflags;
}

void world_t::generate(sector_t *sector, sector_index_t index, bool partial)
{
	sector->index = index;

	sector->bounds.v[0] = (v2f_t)index * SECTOR_SIZE;
	sector->bounds.v[1] = sector->bounds.v[0] + v2f_t(SECTOR_SIZE, SECTOR_SIZE);

	sector->empty = false;
	generator(this, index, sector, generator_data);

	if (partial)
		return;

	for (coord_t ly = 0; ly < SECTOR_SIZE; ly++)
	for (coord_t lx = 0; lx < SECTOR_SIZE; lx++) {
		tile_t *tile;

		tile = sector->tiles + ly * SECTOR_SIZE + lx;
		tile->neighbors = 0;

		for (size_t i = 0; i < 8; i++) {
			tile_index_t neighbor_index;
			tile_t *neighbor;

			neighbor_index = index * SECTOR_SIZE + tile_index_t(lx, ly) + neighbor_offsets[i];
			neighbor = get_tile(neighbor_index, true);

			if (neighbor->type == tile->type)
				tile->neighbors |= (1 << i);
		}
	}
}

bool world_t::find_path(v2f_t src, v2f_t dst, cmodel_t *cmodel, entity_t *ignore,
                        std::list<v2f_t> *path)
{
	path_finder_t finder;
	rectf_t bounds;
	v2f_t cmodel_dims;

	finder.setup_nodes(src, dst, cmodel->cflags);

	cmodel_dims = cmodel->bounds.dims();

	for (size_t y = 0; y < finder.height; y++)
	for (size_t x = 0; x < finder.width; x++) {
		tile_index_t index;
		rectf_t combined;

		index = finder.base + tile_index_t(x, y);

		if (!(tiles[get_tile(index)->type] & cmodel->cflags))
			continue;

		combined[0] = v2f_t(index)  - cmodel_dims / 2;
		combined[1] = v2f_t(index) + v2f_t(1.0f, 1.0f) + cmodel_dims / 2;

		finder.eliminate_nodes(combined);
	}

	bounds = finder.bounds;
	bounds[0] -= cmodel_dims / 2;
	bounds[1] += cmodel_dims / 2;

	for (entity_t *ent : get_entities(bounds, cmodel->cflags)) {
		v2f_t center;
		v2f_t combined_dims;
		rectf_t combined;

		if (ent == ignore)
			continue;

		if (!(ent->cmodel.cflags & cmodel->cflags))
			continue;

		center = ent->cmodel.bounds.center();
		combined_dims = ent->cmodel.bounds.dims() + cmodel_dims;
		combined[0] = center - combined_dims / 2;
		combined[1] = center + combined_dims / 2;

		finder.eliminate_nodes(combined);
	}
	if (!finder.find())
		return false;

	finder.export_path(path);
	return true;
}

sector_t *world_t::get_sector(sector_index_t index, bool partial)
{
	sector_t *sector;

	sector = &sectors[index];

	if (sector->empty)
		generate(sector, index, partial);

	return sector;
}

tile_t *world_t::get_tile(tile_index_t index, bool partial)
{
	sector_index_t sector_index;
	sector_t *sector;
	int64_t tx, ty;

	sector_index[0] = divide_rmi(index[0], (int64_t)SECTOR_SIZE, &tx);
	sector_index[1] = divide_rmi(index[1], (int64_t)SECTOR_SIZE, &ty);
	sector = get_sector(sector_index, partial);

	return sector->tiles + ty * SECTOR_SIZE + tx;
}

std::list<sector_t*> world_t::get_sectors(rectf_t rect)
{
	sector_index_t base, upper;
	std::list<sector_t*> list;

	base = sector_index_at(rect.v[0]);
	upper = sector_index_at(rect.v[1]);

	for (int64_t y = base[1]; y <= upper[1]; y++)
	for (int64_t x = base[0]; x <= upper[0]; x++) {
		sector_index_t index(x, y);
		list.push_back(get_sector(index));
	}

	return list;
}

static size_t cookie = 0;

std::list<entity_t*> world_t::get_entities(rectf_t rect, cflags_t cflags)
{
	std::list<entity_t*> list;

	cookie++;

	for (sector_t *sector : get_sectors(rect))
	for (entity_t *ent : sector->ents) {
		if (ent->cookie == cookie)
			continue;

		ent->cookie = cookie;

		if (!(ent->cmodel.cflags & cflags))
			continue;

		if (!(rect && ent->cmodel.bounds))
			continue;

		list.push_back(ent);
	}

	return list;
}

std::list<entity_t*> world_t::get_render_entities(rectf_t rect)
{
	std::list<entity_t*> list;

	cookie++;

	for (sector_t *sector : get_sectors(rect))
	for (entity_t *ent : sector->ents) {
		if (ent->cookie == cookie)
			continue;

		if (!(rect && ent->render_bounds))
			continue;

		ent->cookie = cookie;

		list.push_back(ent);
	}

	return list;
}

bool world_t::test_rect(const cmodel_t *cmodel, const entity_t *ignore)
{
	cookie++;

	for (sector_t *sector : get_sectors(cmodel->bounds)) {
		rect_t<coord_t, 2> bounds;
		tile_index_t index;

		bounds[0] = (cmodel->bounds[0] - sector->bounds[0]).floor();
		bounds[1] = (cmodel->bounds[1] - sector->bounds[0]).floor();

		if (bounds[0][0] < 0)
			bounds[0][0] = 0;
		if (bounds[0][1] < 0)
			bounds[0][1] = 0;
		if (bounds[1][0] >= (coord_t)SECTOR_SIZE)
			bounds[1][0] = SECTOR_SIZE - 1;
		if (bounds[1][1] >= (coord_t)SECTOR_SIZE)
			bounds[1][1] = SECTOR_SIZE - 1;

		for (index[1] = bounds[0][1]; index[1] <= bounds[1][1]; index[1]++)
		for (index[0] = bounds[0][0]; index[0] <= bounds[1][0]; index[0]++) {
			tile_t *tile;

			tile = sector->tiles + index[1] * SECTOR_SIZE + index[0];

			if (tiles[tile->type] & cmodel->cflags)
				return true;
		}

		for (entity_t *ent : sector->ents) {
			if (ent == ignore)
				continue;

			if (ent->cookie == cookie)
				continue;

			ent->cookie = cookie;

			if (!(ent->cmodel.cflags & cmodel->cflags))
				continue;

			if (!(cmodel->bounds && ent->cmodel.bounds))
				continue;

			return true;
		}
	}

	return false;
}

static const v2f_t transforms[4] = {
	{+1, +1}, {-1, +1}, {-1, -1}, {+1, -1}
};

static const tile_index_t transforms_index[4] = {
	{+1, +1}, {-1, +1}, {-1, -1}, {+1, -1}
};

static const tile_index_t offsets_index[4] = {
	{0, 0}, {-1, 0}, {-1, -1}, {0, -1}
};

static inline size_t quadrant(v2f_t dx)
{
	if (dx[0] == 0.0f && dx[1] == 0.0f)
		return 4;

	if (dx[1] >= 0.0f) {
		if (dx[0] >= 0.0f)
			return 0;
		else
			return 1;
	} else {
		if (dx[0] >= 0.0f)
			return 3;
		else
			return 2;
	}
}

trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags)
{
	v2f_t x, dx;
	size_t quad;
	trace_t res;

	dx = end - start;

	quad = quadrant(dx);
	if (quad == 4) {
		res.hit = false;
		res.end = end;
		res.frac = 1.0f;
		return res;
	}

	start ^= transforms[quad];
	dx ^= transforms[quad];
	end ^= transforms[quad];

	x = start;

	while (1) {
		v2f_t mod, P;
		tile_index_t index;

		if (x[0] >= end[0] && x[1] >= end[1]) {
			res.hit = false;
			res.end = end ^ transforms[quad];
			res.frac = 1.0f;
			return res;
		}

		index = (tile_index_t(x.floor()) ^ transforms_index[quad]) +
		        offsets_index[quad];

		if (tiles[get_tile(index, false)->type] & cflags) {
			res.hit = true;
			res.end = x ^ transforms[quad];
			res.frac = (x - start).len() / (end - start).len();
			return res;
		}

		mod = x - x.floor();

		// Project onto x = const.
		P[0] = 1.0f - mod[0];
		P[1] = P[0] / dx[0] * dx[1];
		if (mod[1] + P[1] >= 0.0f && mod[1] + P[1] <= 1.0f)
		{
			x += P;
			x[0] = std::round(x[0]);
			continue;
		}

		// Project onto y = const.
		P[1] = 1.0f - mod[1];
		P[0] = P[1] / dx[1] * dx[0];
		x += P;
		x[1] = std::round(x[1]);
	}
}

trace_t trace_cmodel(v2f_t start, v2f_t end, const cmodel_t *cmodel)
{
	// TODO
	return {0};
}

void world_t::debug_point(sf::Vector2f point)
{
	sector_index_t index = sector_index_at(point);
	printf("sector (%zd, %zd)\n", index[0], index[1]);
}

entity_t::entity_t(int type_)
{
	type = type_;
}

entity_t::~entity_t(void)
{
}

void entity_t::link_to_sector(sector_t *sector)
{
	parents.push_back(sector);
	sector->ents.insert(this);
}

void entity_t::link(world_t *world_)
{
	rectf_t total_bounds;
	float fx, fy;
	sector_index_t base;
	float xlip, ylip;
	size_t xsecs, ysecs;

	if (world)
		unlink();

	world = world_;

	total_bounds = cmodel.bounds | render_bounds;

	fx = floor(total_bounds[0][0]);
	fy = floor(total_bounds[0][1]);

	base = sector_index_at(v2f_t(fx, fy));
	xlip = total_bounds[1][0] - (base[0] + 1) * SECTOR_SIZE;
	ylip = total_bounds[1][1] - (base[1] + 1) * SECTOR_SIZE;

	if (xlip > 0.0f)
		xsecs = ceil(xlip / SECTOR_SIZE) + 1;
	else
		xsecs = 1;

	if (ylip > 0.0f)
		ysecs = ceil(ylip / SECTOR_SIZE) + 1;
	else
		ysecs = 1;

	for (int64_t y = 0; y < (int64_t)ysecs; y++)
	for (int64_t x = 0; x < (int64_t)xsecs; x++) {
		sector_index_t index = base + sector_index_t(x, y);
		sector_t *sector;

		sector = world->get_sector(index);
		link_to_sector(sector);
	}
}

void entity_t::unlink(void)
{
	for (sector_t *sector : parents) {
		if (sector->ents.find(this) == sector->ents.end()) {
			printf("entity_t::unlink: %p should belong to %p (%" PRIi64", %" PRIi64") but it doesn't\n",
			       this, sector, sector->index[0], sector->index[1]);
			continue;
		}

		sector->ents.erase(sector->ents.find(this));
	}

	parents.clear();
	world = 0;
}

} // namespace world