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

namespace world {

sector_index_t::sector_index_t ()
{
}

sector_index_t::sector_index_t (int64_t x_, int64_t y_)
{
	x = x_;
	y = y_;
}

bool sector_index_t::operator<(sector_index_t B) const
{
	if (x < B.x)
		return true;
	if (x > B.x)
		return false;
	if (y < B.y)
		return true;
	return false;
}

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

void world_t::generate(sector_t *sector, sector_index_t index)
{
	sector->bounds.left = index.x * SECTOR_SIZE;
	sector->bounds.top = index.y * SECTOR_SIZE;
	sector->bounds.width = SECTOR_SIZE;
	sector->bounds.height = SECTOR_SIZE;
	
	std::cout << "generating (" << index.x << ", " << index.y << ")\n";

	for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++)
	for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++) {
		ssize_t x, y;
		float noise;

		x = index.x * SECTOR_SIZE + lx;
		y = index.y * SECTOR_SIZE + ly;

		noise = perlin.get(x, y, 30.0f) * 0.6f +
		        perlin.get(x, y, 15.0f) * 0.25f +
		        perlin.get(x, y, 7.0f) * 0.1f +
		        perlin.get(x, y, 3.0f) * 0.05f;

		sector->tiles[ly * SECTOR_SIZE + lx].type =
		(noise + 1) * 100;
	}

	sector->empty = false;
}

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

	sector = &sectors[index];

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

	return sector;
}

tile_t *world_t::get_tile(ssize_t x, ssize_t y)
{
	sector_index_t index;
	sector_t *sector;
	ssize_t tx, ty;

	index.x = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx);
	index.y = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty);
	sector = get_sector(index);

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

void entity_t::link(world_t *world)
{
	// TODO
}

void entity_t::unlink(void)
{
	for (sector_t *sector : parents)
		sector->ents.erase(sector->ents.find(this));

	parents.clear();
	parent_world = nullptr;
}

} // namespace world