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
|
#include "common.hpp"
namespace world {
sector_index_t sector_index_at(v2f_t x)
{
return sector_index_t(floor(x[0] / SECTOR_SIZE),
floor(x[1] / SECTOR_SIZE));
}
world_t::world_t(void)
{
prng.seed(124);
perlin.generate(&prng, 32);
}
void world_t::generate_tile(tile_t *tile, tile_index_t x)
{
float waterlevel, height;
waterlevel = perlin.get(x, 1000.0f) * 0.3f +
perlin.get(x, 500.0f) * 0.1f;
height = perlin.get(x, 60.0f) * 0.6f +
perlin.get(x, 30.0f) * 0.25f +
perlin.get(x, 14.0f) * 0.1f +
perlin.get(x, 6.0f) * 0.05f;
if (height < waterlevel - 0.2f)
tile->type = -1;
else if (height < waterlevel)
tile->type = 0;
else if (height < waterlevel + 0.05f)
tile->type = 1;
else {
if (perlin.get(x, 5.0f) > 0.0f)
tile->type = 3;
else
tile->type = 2;
}
}
void world_t::generate(sector_t *sector, sector_index_t index)
{
sector->index = index;
sector->bounds.left = index[0] * SECTOR_SIZE;
sector->bounds.top = index[1] * SECTOR_SIZE;
sector->bounds.width = SECTOR_SIZE;
sector->bounds.height = SECTOR_SIZE;
std::cout << "generating " << index << "\n";
for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++)
for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++)
generate_tile(sector->tiles + ly * SECTOR_SIZE + lx,
tile_index_t(index[0] * SECTOR_SIZE + lx,
index[1] * SECTOR_SIZE + ly));
sector->empty = false;
}
sector_t *world_t::get_sector(sector_index_t index)
{
sector_t *sector;
sector = §ors[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[0] = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx);
index[1] = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty);
sector = get_sector(index);
return sector->tiles + ty * SECTOR_SIZE + tx;
}
std::list<sector_t*> world_t::get_sectors(sf::FloatRect rect)
{
sector_index_t base, upper;
std::list<sector_t*> list;
base = sector_index_at(v2f_t(rect.left, rect.top));
upper = sector_index_at(v2f_t(rect.left + rect.width, rect.top + rect.height));
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;
}
std::list<entity_t*> world_t::get_render_entities(sf::FloatRect rect)
{
static size_t cookie = 0;
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.intersects(ent->render_bounds))
continue;
ent->cookie = cookie;
list.push_back(ent);
}
return list;
}
std::list<entity_t*> world_t::get_entities(sf::FloatRect rect)
{
static size_t cookie = 0;
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.intersects(ent->bounds))
continue;
ent->cookie = cookie;
list.push_back(ent);
}
return list;
}
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]);
}
void entity_t::link_to_sector(sector_t *sector)
{
parents.push_back(sector);
sector->ents.insert(this);
}
void entity_t::link(world_t *world)
{
//sf::FloatRect total_bounds;
float fx, fy;
sector_index_t base;
float xlip, ylip;
size_t xsecs, ysecs;
// TODO
//total.bounds.left = std::min(bounds.left, render_bounds.left);
//total.bounds.top = std::min(bounds.top, render_bounds.top);
//total.bounds.width = std::max(bounds.width, render_bounds.width);
//total.bounds.height = std::max(bounds.height, render_bounds.height);
fx = floor(bounds.left);
fy = floor(bounds.top);
base = sector_index_at(v2f_t(fx, fy));
xlip = bounds.left + bounds.width - (base[0] + 1) * SECTOR_SIZE;
ylip = bounds.top + bounds.height - (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 = sector_index_at(v2f_t(base[0] + x, base[1] + y));
sector_t *sector;
sector = world->get_sector(index);
link_to_sector(sector);
}
}
void entity_t::unlink(void)
{
for (sector_t *sector : parents)
sector->ents.erase(sector->ents.find(this));
parents.clear();
parent_world = nullptr;
}
} // namespace world
|