summaryrefslogtreecommitdiff
path: root/src/world.cpp
blob: b5aeea2a635a30927384eec827cfec580fc124db (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
#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;
}

void sector_t::generate(sector_index_t index)
{
	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;

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

		tiles[ly * SECTOR_SIZE + lx].type = (x ^ y) * 151;
	}

	empty = false;
}

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

	sector = &sectors[index];

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

	return sector;
}

// divide and round to minus infinity
static ssize_t divide_rmi(ssize_t x, ssize_t y, size_t *rem)
{
	ssize_t rv;

	if (x >= 0) {
		*rem = x % y;
		return x / y;
	}

	rv = (x + 1) / y - 1;
	*rem = x - rv * y;
	return rv;
}

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

	index.x = divide_rmi(x, SECTOR_SIZE, &tx);
	index.y = divide_rmi(y, SECTOR_SIZE, &ty);
	printf("get_tile(%zd, %zd)\n", x, y);
	printf("\tsector is (%zd, %zd)\n", index.x, index.y);
	sector = get_sector(index);

	printf("\ttile is (%zd, %zd)\n", tx, ty);
	return sector->tiles + ty * SECTOR_SIZE + tx;
}

}