12#include <unordered_map>
13#include <unordered_set>
20constexpr float kPi = 3.14159265358979323846f;
21constexpr float kExploredMask = 0.35f;
24enum class RadiusMetric : uint8_t { Euclidean, Chebyshev, Manhattan };
25enum class Mode : uint8_t { Grid2D, Heightmap, Volume };
26enum class Topology : uint8_t { Ortho,
Hex };
27enum class CellState : uint8_t {
Unknown = 0, Explored = 1, Visible = 2 };
29Algorithm parseAlgorithm(
const std::string &
name, Algorithm fallback) {
30 if (
name ==
"shadowcast")
return Algorithm::Shadowcast;
31 if (
name ==
"raycast")
return Algorithm::Raycast;
32 if (
name ==
"permissive")
return Algorithm::Permissive;
33 if (
name ==
"rectangle")
return Algorithm::Rectangle;
37std::string algorithmName(Algorithm
a) {
39 case Algorithm::Raycast:
41 case Algorithm::Permissive:
43 case Algorithm::Rectangle:
45 case Algorithm::Shadowcast:
51Mode parseMode(
const std::string &
name, Mode fallback) {
52 if (
name ==
"grid2d")
return Mode::Grid2D;
53 if (
name ==
"heightmap")
return Mode::Heightmap;
54 if (
name ==
"volume")
return Mode::Volume;
58std::string modeName(Mode
m) {
70RadiusMetric parseMetric(
const std::string &
name, RadiusMetric fallback) {
71 if (
name ==
"euclidean")
return RadiusMetric::Euclidean;
72 if (
name ==
"chebyshev")
return RadiusMetric::Chebyshev;
73 if (
name ==
"manhattan")
return RadiusMetric::Manhattan;
77std::string metricName(RadiusMetric
m) {
79 case RadiusMetric::Euclidean:
81 case RadiusMetric::Chebyshev:
83 case RadiusMetric::Manhattan:
89std::string topologyName(Topology
t) {
return t == Topology::Hex ?
"hex" :
"ortho"; }
91void offsetToCube(
int x,
int y,
int &
q,
int &
r,
int &
s) {
93 q =
x - (
y - (
y & 1)) / 2;
98void cubeToOffset(
int q,
int r,
int &
x,
int &
y) {
100 x =
q + (
r - (
r & 1)) / 2;
103int cubeDistance(
int q0,
int r0,
int s0,
int q1,
int r1,
int s1) {
104 return std::max({std::abs(q0 - q1), std::abs(r0 - r1), std::abs(s0 - s1)});
107int hexDistance(
int x0,
int y0,
int x1,
int y1) {
108 int q0, r0, s0, q1, r1, s1;
109 offsetToCube(x0, y0, q0, r0, s0);
110 offsetToCube(x1, y1, q1, r1, s1);
111 return cubeDistance(q0, r0, s0, q1, r1, s1);
114bool inRadiusOrtho(RadiusMetric metric,
int dx,
int dy,
int radius) {
115 if (
radius < 0)
return false;
116 const int adx = std::abs(
dx);
117 const int ady = std::abs(
dy);
119 case RadiusMetric::Chebyshev:
120 return std::max(adx, ady) <=
radius;
121 case RadiusMetric::Manhattan:
122 return adx + ady <=
radius;
123 case RadiusMetric::Euclidean:
129bool inRadius3Ortho(RadiusMetric metric,
int dx,
int dy,
int dz,
int radius) {
130 if (
radius < 0)
return false;
131 const int adx = std::abs(
dx);
132 const int ady = std::abs(
dy);
133 const int adz = std::abs(
dz);
135 case RadiusMetric::Chebyshev:
136 return std::max({adx, ady, adz}) <=
radius;
137 case RadiusMetric::Manhattan:
138 return adx + ady + adz <=
radius;
139 case RadiusMetric::Euclidean:
141 return adx * adx + ady * ady + adz * adz <=
radius *
radius;
145float angleDiffDeg(
float a,
float b) {
147 while (
d > 180.f)
d -= 360.f;
148 while (
d < -180.f)
d += 360.f;
152bool inCone(
int ox,
int oy,
int x,
int y,
bool useCone,
float facingDeg,
float halfAngleDeg) {
153 if (!useCone || halfAngleDeg >= 180.f)
return true;
154 if (
x ==
ox &&
y ==
oy)
return true;
155 const float ang = std::atan2(
float(
y -
oy),
float(
x -
ox)) * (180.f / kPi);
156 return std::fabs(angleDiffDeg(
ang, facingDeg)) <= halfAngleDeg;
159uint8_t stateToMaskByte(CellState
s) {
161 case CellState::Visible:
163 case CellState::Explored:
164 return uint8_t(kExploredMask * 255.f + 0.5f);
165 case CellState::Unknown:
171float stateToMaskValue(CellState
s) {
173 case CellState::Visible:
175 case CellState::Explored:
176 return kExploredMask;
177 case CellState::Unknown:
183float normalizeAngle(
float a) {
184 while (
a <= -kPi)
a += 2.f * kPi;
185 while (
a > kPi)
a -= 2.f * kPi;
189constexpr int kMultXX[8] = {1, 0, 0, -1, -1, 0, 0, 1};
190constexpr int kMultXY[8] = {0, 1, -1, 0, 0, -1, 1, 0};
191constexpr int kMultYX[8] = {0, 1, 1, 0, 0, -1, -1, 0};
192constexpr int kMultYY[8] = {1, 0, 0, 1, -1, 0, 0, -1};
194constexpr int kCubeDirs[6][3] = {{1, -1, 0}, {1, 0, -1}, {0, 1, -1}, {-1, 1, 0}, {-1, 0, 1}, {0, -1, 1}};
227 RadiusMetric
metric = RadiusMetric::Euclidean;
264 return std::max(0,
r.radius + bonus);
274 return hexDistance(
ox,
oy,
x,
y) + std::abs(
z - oz) <=
radius;
281 const auto o =
layer->config()->orientation;
292 state.assign(
n, CellState::Unknown);
301 auto cfg =
layer->config();
302 resize(cfg->mapW, cfg->mapH, 1);
303 if (
mode == Mode::Volume)
mode = Mode::Grid2D;
310 auto cfg =
layer->config();
312 auto tileset =
layer->tileset();
314 resize(cfg->mapW, cfg->mapH, 1);
318 for (
int i = 0; i <
n; ++i) {
319 const uint32_t gid = (i < int(
tiles->gids.size())) ?
tileGid(
tiles->gids[
size_t(i)]) : 0
u;
324 std::find_if(tileset->visuals.begin(), tileset->visuals.end(),
326 if (visual != tileset->visuals.end())
isOpaque =
isOpaque || visual->opaque;
354 if (idx < 0 || idx >=
int(
state.size()))
return;
355 if (
state[
size_t(
idx)] != CellState::Visible) {
356 state[size_t(
idx)] = CellState::Visible;
374 state[size_t(
idx)] = CellState::Explored;
381 std::fill(
state.begin(),
state.end(), CellState::Unknown);
396 bool losBresenham2(
int x0,
int y0,
int x1,
int y1,
int zSlice,
bool applyHeight)
const {
397 int dx = std::abs(x1 - x0);
398 int dy = std::abs(y1 - y0);
399 const int sx = x0 < x1 ? 1 : -1;
400 const int sy = y0 < y1 ? 1 : -1;
407 if (
x == x1 &&
y == y1)
return true;
408 const int e2 = 2 * err;
419 const bool atEnd = (
nx == x1 &&
ny == y1);
421 if (applyHeight &&
mode == Mode::Heightmap && !atEnd) {
434 bool losHexCube(
int x0,
int y0,
int x1,
int y1,
int zSlice,
bool applyHeight)
const {
435 int q0, r0, s0, q1, r1, s1;
436 offsetToCube(x0, y0, q0, r0, s0);
437 offsetToCube(x1, y1, q1, r1, s1);
438 const int n = cubeDistance(q0, r0, s0, q1, r1, s1);
439 if (
n == 0)
return true;
441 for (
int i = 1; i <=
n; ++i) {
442 const float t = float(i) / float(
n);
443 const float qf = float(q0) + (float(q1) - float(q0)) *
t;
444 const float rf = float(r0) + (float(r1) - float(r0)) *
t;
445 const float sf = float(s0) + (float(s1) - float(s0)) *
t;
447 int rq = int(std::lround(qf));
448 int rr = int(std::lround(rf));
449 int rs = int(std::lround(sf));
450 const float qdiff = std::fabs(rq - qf);
451 const float rdiff = std::fabs(rr - rf);
452 const float sdiff = std::fabs(rs - sf);
453 if (qdiff > rdiff && qdiff > sdiff) rq = -rr - rs;
454 else if (rdiff > sdiff) rr = -rq - rs;
458 cubeToOffset(rq, rr,
x,
y);
459 const bool atEnd = (i ==
n);
461 if (applyHeight &&
mode == Mode::Heightmap && !atEnd) {
468 bool los2(
int x0,
int y0,
int x1,
int y1,
int zSlice,
bool applyHeight)
const {
469 if (
topology == Topology::Hex)
return losHexCube(x0, y0, x1, y1, zSlice, applyHeight);
475 return los2(
ox,
oy, tx, ty, 0,
true);
478 void tryMark2(
int ox,
int oy,
int x,
int y,
int zSlice,
bool useCone,
float facingDeg,
479 float halfAngleDeg,
int radius) {
482 if (!inCone(
ox,
oy,
x,
y, useCone, facingDeg, halfAngleDeg))
return;
489 int xy,
int yx,
int yy,
int zSlice,
bool useCone,
float facingDeg,
490 float halfAngleDeg) {
491 if (startSlope < endSlope)
return;
492 float newStart = 0.f;
493 for (
int j = row; j <=
radius; ++j) {
496 bool blocked =
false;
499 const int mapX =
ox +
dx * xx +
dy * xy;
500 const int mapY =
oy +
dx * yx +
dy * yy;
501 const float leftSlope = (float(
dx) - 0.5f) / (
float(
dy) + 0.5f);
502 const float rightSlope = (float(
dx) + 0.5f) / (
float(
dy) - 0.5f);
503 if (startSlope < rightSlope)
continue;
504 if (endSlope > leftSlope)
break;
508 newStart = rightSlope;
512 startSlope = newStart;
515 castLight(
ox,
oy,
radius, j + 1, startSlope, leftSlope, xx, xy, yx, yy, zSlice,
516 useCone, facingDeg, halfAngleDeg);
517 newStart = rightSlope;
527 for (
int oct = 0; oct < 8; ++oct) {
528 castLight(
r.x,
r.y,
radius, 1, 1.f, 0.f, kMultXX[oct], kMultXY[oct], kMultYX[oct],
529 kMultYY[oct], zSlice,
r.useCone,
r.facingDeg,
r.halfAngleDeg);
534 const float px = float(
x) + ((
y & 1) ? 0.5f : 0.f);
535 const float py = float(
y) * 0.86602540378f;
536 const float opx = float(
ox) + ((
oy & 1) ? 0.5f : 0.f);
537 const float opy = float(
oy) * 0.86602540378f;
538 return std::atan2(
py - opy,
px - opx);
543 float mid = normalizeAngle(0.5f * (a0 + a1));
545 if (std::fabs(a1 - a0) > kPi)
mid = normalizeAngle(
mid + kPi);
546 for (
const auto &sh : shadows) {
547 float s = sh.start, e = sh.end;
549 if (
mid >=
s &&
mid <= e)
return true;
551 if (
mid >=
s ||
mid <= e)
return true;
557 void addAngleShadow(std::vector<AngleShadow> &shadows,
float a0,
float a1)
const {
558 a0 = normalizeAngle(a0);
559 a1 = normalizeAngle(a1);
564 float diff = normalizeAngle(
end -
start);
565 if (diff < 0.f) std::swap(
start,
end);
572 std::vector<AngleShadow> shadows;
576 offsetToCube(
r.x,
r.y, cq, cr, cs);
578 for (
int ring = 1; ring <=
radius; ++ring) {
580 int q = cq + kCubeDirs[4][0] * ring;
581 int rr = cr + kCubeDirs[4][1] * ring;
582 int s = cs + kCubeDirs[4][2] * ring;
583 for (
int side = 0; side < 6; ++side) {
586 cubeToOffset(
q, rr,
x,
y);
588 inCone(
r.x,
r.y,
x,
y,
r.useCone,
r.facingDeg,
r.halfAngleDeg)) {
591 (ring <= 0) ? kPi : (0.55f / float(ring));
592 const float a0 = normalizeAngle(
ang - half);
593 const float a1 = normalizeAngle(
ang + half);
602 q += kCubeDirs[side][0];
603 rr += kCubeDirs[side][1];
604 s += kCubeDirs[side][2];
616 void castRayLine(
int ox,
int oy,
int tx,
int ty,
int zSlice,
bool useCone,
float facingDeg,
617 float halfAngleDeg,
int radius) {
620 int q0, r0, s0, q1, r1, s1;
621 offsetToCube(
ox,
oy, q0, r0, s0);
622 offsetToCube(tx, ty, q1, r1, s1);
623 const int n = std::max(1, cubeDistance(q0, r0, s0, q1, r1, s1));
624 for (
int i = 0; i <=
n; ++i) {
625 const float t = float(i) / float(
n);
626 int rq = int(std::lround(
float(q0) + (
float(q1) -
float(q0)) *
t));
627 int rr = int(std::lround(
float(r0) + (
float(r1) -
float(r0)) *
t));
631 cubeToOffset(rq, rr,
x,
y);
637 int dx = std::abs(tx -
ox);
638 int dy = std::abs(ty -
oy);
639 const int sx =
ox < tx ? 1 : -1;
640 const int sy =
oy < ty ? 1 : -1;
646 if (
x == tx &&
y == ty)
break;
648 const int e2 = 2 * err;
665 offsetToCube(
r.x,
r.y, cq, cr, cs);
666 for (
int ring = 1; ring <=
radius; ++ring) {
667 int q = cq + kCubeDirs[4][0] * ring;
668 int rr = cr + kCubeDirs[4][1] * ring;
669 for (
int side = 0; side < 6; ++side) {
672 cubeToOffset(
q, rr,
x,
y);
675 q += kCubeDirs[side][0];
676 rr += kCubeDirs[side][1];
699 if (
dx == 0 &&
dy == 0)
continue;
700 const int tx =
r.x +
dx;
701 const int ty =
r.y +
dy;
704 if (!inCone(
r.x,
r.y, tx, ty,
r.useCone,
r.facingDeg,
r.halfAngleDeg))
continue;
705 bool see =
los2(
r.x,
r.y, tx, ty, zSlice,
mode == Mode::Heightmap);
707 const int sx = (
dx > 0) ? -1 : (
dx < 0 ? 1 : 0);
708 const int sy = (
dy > 0) ? -1 : (
dy < 0 ? 1 : 0);
709 const int cands[3][2] = {{tx +
sx, ty}, {tx, ty +
sy}, {tx +
sx, ty +
sy}};
710 for (
auto &
c : cands) {
713 if (ax == tx && ay == ty)
continue;
717 if (
los2(
r.x,
r.y, ax, ay, zSlice,
mode == Mode::Heightmap)) {
731 const int yMin = std::max(0,
oy -
radius);
734 const int bh = yMax - yMin + 1;
735 std::vector<uint8_t>
used(
size_t(bw * bh), 0);
736 auto usedAt = [&](
int x,
int y) -> uint8_t & {
737 return used[size_t((
y - yMin) * bw + (
x -
xMin))];
740 std::vector<Rect> rects;
741 for (
int y = yMin;
y <= yMax; ++
y) {
743 if (usedAt(
x,
y))
continue;
753 while (grow && y1 + 1 <= yMax) {
754 for (
int xx =
x; xx <= x1; ++xx) {
763 for (
int yy =
y; yy <= y1; ++yy)
764 for (
int xx =
x; xx <= x1; ++xx) usedAt(xx, yy) = 1;
765 rects.push_back(
Rect{
x,
y, x1, y1});
773 std::vector<uint8_t> lit(
size_t(
width *
height), 0);
774 auto litAt = [&](
int x,
int y) -> uint8_t & {
return lit[size_t(
index2(
x,
y))]; };
779 if (!inCone(
r.x,
r.y,
x,
y,
r.useCone,
r.facingDeg,
r.halfAngleDeg))
continue;
786 const float ox = float(
r.x) + 0.5f;
787 const float oy = float(
r.y) + 0.5f;
788 std::sort(rects.begin(), rects.end(), [&](
const Rect &
a,
const Rect &
b) {
789 const int acx = (a.x0 + a.x1) / 2;
790 const int acy = (a.y0 + a.y1) / 2;
791 const int bcx = (b.x0 + b.x1) / 2;
792 const int bcy = (b.y0 + b.y1) / 2;
793 return inRadius2(r.x, r.y, acx, acy, radius) &&
794 (std::abs(acx - r.x) + std::abs(acy - r.y)) <
795 (std::abs(bcx - r.x) + std::abs(bcy - r.y));
798 for (
const auto &rc : rects) {
801 {float(rc.x0), float(rc.y0)},
802 {float(rc.x1) + 1.f, float(rc.y0)},
803 {float(rc.x0), float(rc.y1) + 1.f},
804 {float(rc.x1) + 1.f, float(rc.y1) + 1.f},
807 for (
int i = 0; i < 4; ++i) {
811 for (
int i = 1; i < 4; ++i) {
812 const float dMin = normalizeAngle(
angles[i] - aMin);
813 const float dMax = normalizeAngle(
angles[i] - aMax);
814 if (dMin < 0.f) aMin =
angles[i];
815 if (dMax > 0.f) aMax =
angles[i];
817 const float nearest2 =
818 float(std::min({(rc.x0 -
r.x) * (rc.x0 -
r.x) + (rc.y0 -
r.y) * (rc.y0 -
r.y),
819 (rc.x1 -
r.x) * (rc.x1 -
r.x) + (rc.y0 -
r.y) * (rc.y0 -
r.y),
820 (rc.x0 -
r.x) * (rc.x0 -
r.x) + (rc.y1 -
r.y) * (rc.y1 -
r.y),
821 (rc.x1 -
r.x) * (rc.x1 -
r.x) + (rc.y1 -
r.y) * (rc.y1 -
r.y)}));
825 if (!litAt(
x,
y))
continue;
827 if (
x >= rc.x0 && x <= rc.x1 && y >= rc.y0 &&
y <= rc.y1)
continue;
828 const float d2 = float((
x -
r.x) * (
x -
r.x) + (
y -
r.y) * (
y -
r.y));
829 if (d2 <= nearest2 + 0.01f)
continue;
830 const float ang = std::atan2(
float(
y) + 0.5f -
oy,
float(
x) + 0.5f -
ox);
832 if (aMin <= aMax)
inside = (
ang >= aMin &&
ang <= aMax);
835 if (normalizeAngle(aMax - aMin) < 0.f) {
836 inside = normalizeAngle(
ang - aMin) >= 0.f ||
837 normalizeAngle(aMax -
ang) >= 0.f;
839 const float da = normalizeAngle(
ang - aMin);
840 const float span = normalizeAngle(aMax - aMin);
841 inside = da >= 0.f && da <= span;
856 if (
mode != Mode::Volume)
return;
861 if (
state[
size_t(
idx)] != CellState::Visible)
continue;
865 if (z < 0 || z >=
depth)
break;
876 if (!
r.enabled)
return;
879 if (
mode == Mode::Volume) {
886 case Algorithm::Raycast:
889 case Algorithm::Permissive:
892 case Algorithm::Rectangle:
895 case Algorithm::Shadowcast:
910 std::none_of(
opaque.begin(),
opaque.end(), [](
auto value) { return value != 0; })) {
926Fov &
Fov::operator=(
Fov &&) noexcept = default;
934 impl_->layer =
nullptr;
935 if (impl_->mode == Mode::Volume) impl_->mode = Mode::Grid2D;
940 impl_->layer =
nullptr;
941 impl_->mode = Mode::Volume;
950 const Mode next = parseMode(
name, impl_->mode);
952 if (next != Mode::Volume && impl_->depth != 1) {
953 impl_->resize(impl_->width, impl_->height, 1);
961 impl_->algorithm = parseAlgorithm(
name, impl_->algorithm);
968 impl_->metric = parseMetric(
name, impl_->metric);
975 if (
name ==
"auto") {
976 impl_->topologyManual =
false;
977 impl_->applyAutoTopologyFromLayer();
978 if (!impl_->layer) impl_->topology = Topology::Ortho;
979 }
else if (
name ==
"hex" ||
name ==
"hexagonal" ||
name ==
"staggered") {
980 impl_->topologyManual =
true;
981 impl_->topology = Topology::Hex;
982 }
else if (
name ==
"ortho" ||
name ==
"orthogonal") {
983 impl_->topologyManual =
true;
984 impl_->topology = Topology::Ortho;
992 impl_->cornerPeek = enable;
999 impl_->opaqueGids.insert(uint32_t(gid));
1000 if (impl_->layer) impl_->syncFromLayer();
1001 else impl_->dirty =
true;
1005 if (gid < 0)
return;
1006 impl_->opaqueGids.erase(uint32_t(gid));
1007 if (impl_->layer) impl_->syncFromLayer();
1008 else impl_->dirty =
true;
1012 impl_->opaqueGids.clear();
1013 if (impl_->layer) impl_->syncFromLayer();
1014 else impl_->dirty =
true;
1018 impl_->blockEmpty = enable;
1019 if (impl_->layer) impl_->syncFromLayer();
1020 else impl_->dirty =
true;
1026 if (!impl_->inBounds2(
x,
y))
return;
1027 if (impl_->mode == Mode::Volume) {
1028 if (!impl_->inBounds3(
x,
y, 0))
return;
1029 impl_->opaque[size_t(impl_->index3(
x,
y, 0))] = opaque ? 1u : 0
u;
1031 impl_->opaque[size_t(impl_->index2(
x,
y))] = opaque ? 1u : 0
u;
1033 impl_->dirty =
true;
1037 if (impl_->mode == Mode::Volume)
return impl_->cellOpaque3(
x,
y, 0);
1038 return impl_->cellOpaque2(
x,
y);
1042 if (!impl_->inBounds3(
x,
y,
z))
return;
1043 impl_->opaque[size_t(impl_->index3(
x,
y,
z))] = opaque ? 1u : 0
u;
1044 impl_->dirty =
true;
1052 if (!impl_->inBounds2(
x,
y))
return;
1053 impl_->elevation[size_t(impl_->index2(
x,
y))] = elev;
1054 impl_->dirty =
true;
1060 impl_->cliffBlock = delta;
1061 impl_->dirty =
true;
1066 impl_->eyeOffset =
offset;
1067 impl_->dirty =
true;
1072 impl_->verticalRange =
range;
1073 impl_->dirty =
true;
1081 r.
id = impl_->nextRevealerId;
1086 const auto index = impl_->revealers.size();
1087 impl_->revealers.push_back(
r);
1089 impl_->revealerIndices.emplace(
r.id,
index);
1091 impl_->revealers.pop_back();
1094 ++impl_->nextRevealerId;
1095 impl_->dirty =
true;
1100 const auto found = impl_->revealerIndices.find(
id);
1101 if (
found != impl_->revealerIndices.end()) {
1103 impl_->revealers.erase(impl_->revealers.begin() +
static_cast<std::ptrdiff_t
>(
index));
1104 impl_->revealerIndices.erase(
found);
1105 for (std::size_t i =
index; i < impl_->revealers.size(); ++i)
1106 impl_->revealerIndices[impl_->revealers[i].id] = i;
1108 impl_->dirty =
true;
1112 impl_->revealers.clear();
1113 impl_->revealerIndices.clear();
1114 impl_->dirty =
true;
1122 if (
auto *
r = impl_->findRevealer(
id)) {
1123 if (
r->x ==
x &&
r->y ==
y &&
r->z ==
z)
return;
1127 impl_->dirty =
true;
1132 if (
auto *
r = impl_->findRevealer(
id)) {
1133 if (
r->radius ==
radius)
return;
1135 impl_->dirty =
true;
1140 if (
auto *
r = impl_->findRevealer(
id)) {
1142 r->facingDeg = facingDeg;
1143 r->halfAngleDeg = std::max(0.f, halfAngleDeg);
1144 impl_->dirty =
true;
1149 if (
auto *
r = impl_->findRevealer(
id)) {
1151 r->halfAngleDeg = 180.f;
1152 impl_->dirty =
true;
1157 if (
auto *
r = impl_->findRevealer(
id)) {
1159 impl_->dirty =
true;
1166 if (
auto *
r = impl_->findRevealer(
id)) {
1167 r->perception = perception;
1168 impl_->dirty =
true;
1173 if (
const auto *
r = impl_->findRevealerConst(
id))
return r->perception;
1178 impl_->perceptionRadiusScale =
scale;
1179 impl_->dirty =
true;
1187 if (
const auto *
r = impl_->findRevealerConst(
id))
return impl_->effectiveRadiusOf(*
r);
1192 if (impl_->mode == Mode::Volume)
return canDetect3(revealerId,
x,
y, 0, targetStealth);
1193 const auto *
r = impl_->findRevealerConst(revealerId);
1194 if (!
r || !
r->enabled)
return false;
1196 return (
r->perception + impl_->detectionMargin) >= targetStealth;
1200 const auto *
r = impl_->findRevealerConst(revealerId);
1201 if (!
r || !
r->enabled)
return false;
1203 return (
r->perception + impl_->detectionMargin) >= targetStealth;
1214 if (impl_->mode == Mode::Volume)
return isVisible3(
x,
y, 0);
1215 if (!impl_->inBounds2(
x,
y))
return false;
1216 return impl_->state[size_t(impl_->index2(
x,
y))] == CellState::Visible;
1220 if (impl_->mode == Mode::Volume)
return isExplored3(
x,
y, 0);
1221 if (!impl_->inBounds2(
x,
y))
return false;
1222 const auto s = impl_->state[size_t(impl_->index2(
x,
y))];
1223 return s == CellState::Explored ||
s == CellState::Visible;
1227 if (!impl_->inBounds3(
x,
y,
z))
return false;
1228 return impl_->state[size_t(impl_->index3(
x,
y,
z))] == CellState::Visible;
1232 if (!impl_->inBounds3(
x,
y,
z))
return false;
1233 const auto s = impl_->state[size_t(impl_->index3(
x,
y,
z))];
1234 return s == CellState::Explored ||
s == CellState::Visible;
1238 if (impl_->mode == Mode::Volume)
return getState3(
x,
y, 0);
1239 if (!impl_->inBounds2(
x,
y))
return "unknown";
1240 switch (impl_->state[
size_t(impl_->index2(
x,
y))]) {
1241 case CellState::Visible:
1243 case CellState::Explored:
1251 if (!impl_->inBounds3(
x,
y,
z))
return "unknown";
1252 switch (impl_->state[
size_t(impl_->index3(
x,
y,
z))]) {
1253 case CellState::Visible:
1255 case CellState::Explored:
1265 impl_->resetVisibleKeepExplored();
1266 impl_->dirty =
true;
1272 result.height = impl_->height;
1273 result.depth = impl_->depth;
1274 result.states.reserve(impl_->state.size());
1275 for (
const auto state : impl_->state)
result.states.push_back(stateToMaskByte(
state));
1280 if (snapshotValue.
width != impl_->width || snapshotValue.
height != impl_->height ||
1281 snapshotValue.
depth != impl_->depth || snapshotValue.
states.size() != impl_->state.size())
1286 impl_->state[
index] =
value == 0 ? CellState::Unknown :
1287 value >= 192 ? CellState::Visible : CellState::Explored;
1289 impl_->visibleList.clear();
1291 if (impl_->state[
index] == CellState::Visible) impl_->visibleList.push_back(
static_cast<int>(
index));
1292 impl_->dirty =
true;
1298 if (!impl_->inBounds2(
x,
y))
return 0.f;
1299 return stateToMaskValue(impl_->state[
size_t(impl_->index2(
x,
y))]);
1304 if (!impl_->inBounds2(
x,
y))
return 0;
1305 return int(stateToMaskByte(impl_->state[
size_t(impl_->index2(
x,
y))]));
1309 if (!impl_->inBounds3(
x,
y,
z))
return 0.f;
1310 return stateToMaskValue(impl_->state[
size_t(impl_->index3(
x,
y,
z))]);
1314 if (!impl_->inBounds3(
x,
y,
z))
return 0;
1315 return int(stateToMaskByte(impl_->state[
size_t(impl_->index3(
x,
y,
z))]));
1321 if (impl_->width <= 0 || impl_->height <= 0) {
1325 if (impl_->mode == Mode::Volume) {
1326 if (sliceZ < 0 || sliceZ >= impl_->depth)
return false;
1330 out.resize(
size_t(impl_->width * impl_->height));
1331 for (
int y = 0;
y < impl_->height; ++
y) {
1332 for (
int x = 0;
x < impl_->width; ++
x) {
1333 CellState
s = (impl_->mode == Mode::Volume)
1334 ? impl_->state[
size_t(impl_->index3(
x,
y, sliceZ))]
1335 : impl_->state[size_t(impl_->index2(
x,
y))];
1336 out[size_t(impl_->index2(
x,
y))] = stateToMaskByte(
s);
1347 if (!gfx)
return nullptr;
1348 std::vector<uint8_t> r8;
1350 std::vector<uint8_t> rgba(
size_t(impl_->width * impl_->height * 4));
1351 for (
size_t i = 0; i < r8.size(); ++i) {
1352 const uint8_t
v = r8[i];
1353 rgba[i * 4 + 0] =
v;
1354 rgba[i * 4 + 1] =
v;
1355 rgba[i * 4 + 2] =
v;
1356 rgba[i * 4 + 3] =
v;
1358 return gfx->
newTexture(impl_->width, impl_->height, rgba.data());
std::vector< double > angles
std::array< double, 10 > q
std::array< float, 3 > scale
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
std::vector< WfcTile > tiles
std::vector< char > inside
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
virtual Texture * newTexture(int width, int height, const uint8_t *rgba, bool repeatU=false, bool repeatV=false)=0
Creates a texture. @ownership Caller deletes unless documented otherwise.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Dynamic field-of-view / fog-of-war facade. Phase A: 2D shadowcast + multi-revealer + explored memory....
void blockOpaqueGid(int gid)
bool canDetect(int revealerId, int x, int y, float targetStealth) const
float getElevation(int x, int y) const
bool isExplored(int x, int y) const
bool isOpaque(int x, int y) const
int getVerticalRange() const
void bindLayer(TileLayer *layer)
std::string getRadiusMetric() const
void setAlgorithm(const std::string &name)
"shadowcast" | "raycast" | "permissive" | "rectangle"
float getDetectionMargin() const
float getEyeOffset() const
void setRadiusMetric(const std::string &name)
"euclidean" | "chebyshev" | "manhattan" — ignored when topology is hex (cube distance).
float getCliffBlock() const
void setRevealerEnabled(int id, bool enabled)
void setOpaque3(int x, int y, int z, bool opaque)
bool isVisible3(int x, int y, int z) const
void setDetectionMargin(float margin)
void setEyeOffset(float offset)
bool isVisible(int x, int y) const
int getEffectiveRadius(int id) const
void setSize(int width, int height)
bool fillMaskR8(std::vector< uint8_t > &out) const
Result< void > restore(const Snapshot &snapshot)
Restore exact cell states atomically.
void setRevealerPosition(int id, int x, int y)
float getRevealerPerception(int id) const
float getMaskValue3(int x, int y, int z) const
void setBlockEmpty(bool enable)
std::string getMode() const
int addRevealer(int x, int y, int radius)
std::string getState3(int x, int y, int z) const
float getMaskValue(int x, int y) const
bool isExplored3(int x, int y, int z) const
int getRevealerCount() const
graphics::Texture * buildMaskTextureSlice(graphics::Graphics *gfx, int sliceZ) const
void setCliffBlock(float delta)
void setElevation(int x, int y, float elev)
Snapshot snapshot() const
Capture exact unknown/explored/visible cell states without revealers.
std::string getTopology() const
bool fillMaskR8Slice(std::vector< uint8_t > &out, int sliceZ) const
int addRevealer3(int x, int y, int z, int radius)
std::string getAlgorithm() const
void setRevealerRadius(int id, int radius)
void setRevealerPosition3(int id, int x, int y, int z)
void setRevealerPerception(int id, float perception)
Soft RPG hooks (no hard dependency on rpg module). effectiveRadius = radius + floor(perception * perc...
void setRevealerFacing(int id, float facingDeg, float halfAngleDeg)
void removeRevealer(int id)
bool getCornerPeek() const
void clearRevealerFacing(int id)
void setPerceptionRadiusScale(float scale)
void setMode(const std::string &name)
"grid2d" (default) | "heightmap" | "volume"
void unblockOpaqueGid(int gid)
int getMaskByte(int x, int y) const
void setVerticalRange(int range)
bool canDetect3(int revealerId, int x, int y, int z, float targetStealth) const
float getPerceptionRadiusScale() const
bool isOpaque3(int x, int y, int z) const
bool getBlockEmpty() const
std::string getState(int x, int y) const
void setCornerPeek(bool enable)
void setOpaque(int x, int y, bool opaque)
graphics::Texture * buildMaskTexture(graphics::Graphics *gfx) const
Upload current 2D / slice mask as RGBA8 Texture (R=G=B=A=mask byte). Caller owns the returned Texture...
void setTopology(const std::string &name)
"ortho" (default) | "hex" | "auto" (hex if bound layer is hex/staggered).
void setVolumeSize(int width, int height, int depth)
int getMaskByte3(int x, int y, int z) const
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
放置世界:格子占用(多通道)+ 地形语义 + 已放置建筑实例。 行为由 PlacementSystem 提供;本类暴露便于脚本绑定的薄封装方法。 坐标换算统一走 eve::grid(支持 rectang...
uint32_t tileGid(uint32_t raw)
Strip Tiled flip / rotate flags; keep low 28 bits.
float hexCellAngle(int ox, int oy, int x, int y) const
void computeShadowcast(const Revealer &r, int zSlice, int radius)
void computeRaycast(const Revealer &r, int zSlice, int radius)
std::vector< uint8_t > opaque
void computePermissive(const Revealer &r, int zSlice, int radius)
void computeOrthoShadowcast(const Revealer &r, int zSlice, int radius)
void computeRectangle(const Revealer &r, int zSlice, int radius)
const Revealer * findRevealerConst(int id) const
void markVisibleIdx(int idx)
std::vector< CellState > state
void markVisible2(int x, int y)
int index2(int x, int y) const
void castRayLine(int ox, int oy, int tx, int ty, int zSlice, bool useCone, float facingDeg, float halfAngleDeg, int radius)
bool unobstructedHeightmap
bool inBounds3(int x, int y, int z) const
bool passesHeightToTarget(int ox, int oy, int tx, int ty) const
bool cellOpaque3(int x, int y, int z) const
void markVisible3(int x, int y, int z)
std::unordered_map< int, std::size_t > revealerIndices
int effectiveVerticalRange() const
void resetVisibleKeepExplored()
std::vector< float > elevation
float perceptionRadiusScale
void castLight(int ox, int oy, int radius, int row, float startSlope, float endSlope, int xx, int xy, int yx, int yy, int zSlice, bool useCone, float facingDeg, float halfAngleDeg)
std::vector< Revealer > revealers
bool cellOpaque2(int x, int y) const
std::vector< Rect > buildOpaqueRects(int ox, int oy, int radius, int zSlice) const
bool inRadius3(int ox, int oy, int oz, int x, int y, int z, int radius) const
void resize(int w, int h, int d)
bool angleFullyCovered(float a0, float a1, const std::vector< AngleShadow > &shadows) const
void computeHexShadowcast(const Revealer &r, int zSlice, int radius)
void applyAutoTopologyFromLayer()
void addAngleShadow(std::vector< AngleShadow > &shadows, float a0, float a1) const
Revealer * findRevealer(int id)
std::unordered_set< uint32_t > opaqueGids
bool los2(int x0, int y0, int x1, int y1, int zSlice, bool applyHeight) const
void bindLayer(TileLayer *l)
bool losBresenham2(int x0, int y0, int x1, int y1, int zSlice, bool applyHeight) const
std::vector< int > visibleList
bool losHexCube(int x0, int y0, int x1, int y1, int zSlice, bool applyHeight) const
float elevAt(int x, int y) const
void tryMark2(int ox, int oy, int x, int y, int zSlice, bool useCone, float facingDeg, float halfAngleDeg, int radius)
int index3(int x, int y, int z) const
bool inBounds2(int x, int y) const
int effectiveRadiusOf(const Revealer &r) const
bool inRadius2(int ox, int oy, int x, int y, int radius) const
void computeRevealer(const Revealer &r)
void extendVertical(const Revealer &r, int zSlice, int radius)
bool cellOpaqueOnSlice(int x, int y, int zSlice) const
Owning visibility-memory snapshot for the current grid/volume dimensions.
std::vector< uint8_t > states
Per-GID visual metadata emitted by a project-defined asset pipeline.