-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtod_map_builder_fc.hpp
More file actions
101 lines (81 loc) · 2.54 KB
/
tod_map_builder_fc.hpp
File metadata and controls
101 lines (81 loc) · 2.54 KB
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
#pragma once
#include <cstdint>
#include <functional>
#include <set>
#include <stdexcept>
#include <tuple>
#include "tod_map.hpp"
#include "tod_rand_fc.hpp"
using tod_map_fc = tod_map<15*2+3, 6*2+3>;
class tod_map_builder_fc
{
using progress_fn_t = std::function<void(const tod_map_fc&)>;
progress_fn_t progress_fn;
public:
tod_map_builder_fc() {
progress_fn = [](auto&) {};
}
tod_map_fc build(int floor) {
if (1 <= floor && floor <= 59) {
auto [s1, s2] = to_seed(floor);
tod_rand_fc rng(s1, s2);
return build(rng);
} else {
tod_rand_fc_floor60 rng;
return build(rng);
}
}
template <class RNG>
tod_map_fc build(RNG& rng) {
tod_map_fc map;
for (int y = 2; y < map.height(); y += 2)
for (int x = 2; x < map.width(); x += 2)
build_wall(map, x, y, rng);
return map;
}
void progress(progress_fn_t fn) {
progress_fn = fn ? fn : [](auto&) {};
}
private:
static std::tuple<uint8_t, uint8_t> to_seed(int floor) {
auto x = floor * 3 + 3;
auto y = floor * 3 + 5;
return { x, y };
}
template <class RNG>
void build_wall(tod_map_fc& map, int x, int y, RNG& rng) {
std::set<std::tuple<int, int>> history;
while (map.get(x, y) == Block::None) {
map.set(x, y, Block::Piller);
history.insert({x, y});
progress_fn(map);
// Is it closed in history?
if (history.contains(move(x, y, 0, 2)) &&
history.contains(move(x, y, 1, 2)) &&
history.contains(move(x, y, 2, 2)) &&
history.contains(move(x, y, 3, 2))) {
break;
}
auto direction = rng() % 4;
auto [nx, ny] = move(x, y, direction, 2);
while (history.contains({nx, ny})) {
direction = rng() % 4;
std::tie(nx, ny) = move(x, y, direction, 2);
}
auto [wx, wy] = move(x, y, direction, 1);
map.set(wx, wy, Block::Wall);
progress_fn(map);
x = nx;
y = ny;
}
}
static std::tuple<int, int> move(int x, int y, int direction, int length) {
switch (direction) {
case 0: return { x, y - length };
case 1: return { x, y + length };
case 2: return { x - length, y };
case 3: return { x + length, y };
}
throw std::invalid_argument("invalid direction");
}
};