-
Notifications
You must be signed in to change notification settings - Fork 4
/
Permissive.cpp
206 lines (165 loc) · 5.32 KB
/
Permissive.cpp
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
#include <cmath>
#include "Doryen/FOV/Permissive.hpp"
using namespace Doryen;
void
Doryen::Permissive::operator()(Doryen::Map& _map, int playerX, int playerY, int maxRadius, bool ligthWalls, int fovType)
{
source = { static_cast<int16_t>(playerX), static_cast<int16_t>(playerY) };
rangeLimit = maxRadius == 0 ? -1 : maxRadius;
map.resetFieldView();
for (int q = 0; q < 4; ++q)
{
quadrant.x = static_cast<std::int16_t>(q == 0 or q == 3 ? 1 : -1);
quadrant.y = static_cast<std::int16_t>(q < 2 ? 1 : -1);
computeQuadrant();
}
}
void Doryen::Permissive::computeQuadrant()
{
const std::int16_t INFINITY_16 = INT16_MAX;
std::vector<Bump> steepBumps{};
std::vector<Bump> shallowBumps{};
LinkedList<Field> activeFields{};
Field field;
field.steep = {{ 1, 0 },
{ 0, INFINITY_16 }};
field.shallow = {{ 0, 1 },
{ INFINITY_16, 0 }};
activeFields.push_back(field);
Offset dest{};
actIsBlocked(dest);
for (int i = 1; i < INFINITY_16 and activeFields.size() not_eq 0; ++i)
{
// Reference to current element
LinkedListNode<Field> current = activeFields.begin();
for (int j = 0; j <= i; ++j)
{
dest.x = static_cast<std::int16_t>(i - j);
dest.y = static_cast<std::int16_t>(j);
current = visitSquare(dest, current, steepBumps, shallowBumps, activeFields);
}
}
}
bool Doryen::Permissive::actIsBlocked(const Doryen::Permissive::Offset& pos)
{
if (rangeLimit >= 0 and getDistance(std::max(pos.x, pos.y), std::min(pos.x, pos.y)) > rangeLimit)
{
return true;
}
else
{
const std::int32_t x = pos.x * quadrant.x + source.x;
const std::int32_t y = pos.y * quadrant.y + source.y;
map.setVisibleFieldView(x, y);
return not map.isTransparent(x, y);
}
}
std::int32_t Doryen::Permissive::getDistance(std::int32_t a, std::int32_t b)
{
if (not(a >= 0))
{
a = 0;
}
if (not(b >= 0))
{
b = 0;
}
return std::sqrt(std::pow(a, 2) + std::pow(b, 2));
}
void Permissive::addSteepBump(const Permissive::Offset& point,
Permissive::LinkedListNode<Permissive::Field> currentField,
std::vector<Bump>& steepBumps)
{
Field value = *currentField;
value.steep.far = point;
value.steepBump = { point, std::make_shared<Bump>(value.steepBump) };
steepBumps.push_back(value.steepBump);
std::shared_ptr<Bump> currentBump = std::make_shared<Bump>(value.shallowBump);
// Now look through the list of shallow bumps and see if any of them are below the line.
for (; currentBump not_eq nullptr; currentBump = currentBump->parent)
{
if (value.steep.isBelow(currentBump->position))
{
value.steep.near = currentBump->position;
}
}
*currentField = value;
}
void Permissive::addShallowBump(const Permissive::Offset& point,
Permissive::LinkedListNode<Permissive::Field> currentField,
std::vector<Bump>& shallowBumps)
{
Field value = *currentField;
value.shallow.far = point;
value.shallowBump = { point, std::make_shared<Bump>(value.shallowBump) };
shallowBumps.push_back(value.shallowBump);
std::shared_ptr<Bump> currentBump = std::make_shared<Bump>(value.steepBump);
while (currentBump not_eq nullptr)
{
if (value.shallow.isAbove(currentBump->position))
{
value.shallow.near = currentBump->position;
}
currentBump = currentBump->parent;
}
*currentField = value;
}
Permissive::LinkedListNode<Permissive::Field>
Permissive::checkField(const Permissive::LinkedListNode<Permissive::Field> currentField,
Permissive::LinkedList<Permissive::Field>& activeFields)
{
LinkedListNode <Field> result = currentField;
if (currentField->shallow.doesContain(currentField->steep.near) and
currentField->shallow.doesContain(currentField->steep.far) and
(currentField->shallow.doesContain({ 0, 1 }) or
currentField->shallow.doesContain({ 1, 0 })))
{
result = std::next(currentField, 1);
activeFields.erase(currentField);
}
return result;
}
Permissive::LinkedListNode<Permissive::Field>
Permissive::visitSquare(const Permissive::Offset& dest,
Permissive::LinkedListNode<Field> currentField,
std::vector<Bump>& steepBumps, std::vector<Bump>& shallowBumps,
Permissive::LinkedList<Field>& activeFields)
{
const Offset topLeft{ dest.x, static_cast<int16_t>(dest.y + 1) };
const Offset bottomRight{ static_cast<int16_t>(dest.x + 1), dest.y };
while (currentField not_eq activeFields.end() and currentField->steep.isBelowOrContains(bottomRight))
{
// Advance to next element
std::advance(currentField, 1);
}
if (currentField == activeFields.end() or currentField->shallow.isAboveOrContains(topLeft) or
not actIsBlocked(dest))
{
return currentField;
}
if (currentField->shallow.isAbove(bottomRight) and currentField->steep.isBelow(topLeft))
{
LinkedListNode <Field> next = std::next(currentField, 1);
activeFields.erase(currentField);
return next;
}
else if (currentField->shallow.isAbove(bottomRight))
{
addShallowBump(topLeft, currentField, shallowBumps);
return checkField(currentField, activeFields);
}
else if (currentField->steep.isBelow(topLeft))
{
addSteepBump(bottomRight, currentField, steepBumps);
return checkField(currentField, activeFields);
}
else
{
LinkedListNode <Field> steeper = currentField;
LinkedListNode <Field> shallower = activeFields.insert(currentField, *currentField);
addSteepBump(bottomRight, shallower, steepBumps);
checkField(shallower, activeFields);
addShallowBump(topLeft, steeper, shallowBumps);
return checkField(steeper, activeFields);
}
}