载入中...
搜索中...
未找到
RTSCrowdSurface.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
3#include "map/Pathfinder.h"
5#include "rts/RTSSystems.h"
6namespace eve::rts {
10 std::int64_t maxChecks) try {
11 const auto fail = [](DiagnosticCode code, const char* message) {
13 Diagnostic::error(code, message, "crowdSurfaceAdapter"));
14 };
15 if (!source.valid() || source.topology() == map::NavigationTopology::Hex)
16 return fail(DiagnosticCode::PreconditionViolation, "Crowd adapter requires a captured orthogonal map grid");
17 if (!std::isfinite(geometry.cellSize) || geometry.cellSize <= 0.f || !std::isfinite(geometry.originX) ||
18 !std::isfinite(geometry.originY) || !std::isfinite(stamp.clearanceRadius) || stamp.clearanceRadius < 0.f ||
19 maxChecks < 0)
20 return fail(DiagnosticCode::InvalidArgument, "Invalid adapter geometry or work budget");
23 result.width = source.width();
24 result.height = source.height();
25 result.cellSize = geometry.cellSize;
26 result.originX = geometry.originX - geometry.cellSize * 0.5f;
27 result.originY = geometry.originY - geometry.cellSize * 0.5f;
28 result.diagonal = source.topology() == map::NavigationTopology::Ortho8;
29 result.destinationEntryCost = true;
30 result.terrainCosts.assign(source.costs().begin(), source.costs().end());
31 for (std::size_t index = 0; index < result.terrainCosts.size(); ++index) {
32 auto& cost = result.terrainCosts[index];
33 if (cost <= 0.f)
34 cost = 0.f;
35 else if (cost < 1.f || (source.enterMasks()[index] & 15) != 15 || (source.exitMasks()[index] & 15) != 15)
37 "Adapter does not support fractional costs or directional gates");
38 }
39 result.navigationCosts = result.terrainCosts;
40 const double radius = double(stamp.clearanceRadius) / geometry.cellSize;
41 std::int64_t checks = 0;
42 for (int y = 0; y < result.height; ++y)
43 for (int x = 0; x < result.width; ++x) {
44 const auto index = static_cast<std::size_t>(y * result.width + x);
45 if (result.navigationCosts[index] == 0.f || radius == 0.0) continue;
46 const double cx = x + 0.5, cy = y + 0.5;
47 if (cx < radius || cy < radius || result.width - cx < radius || result.height - cy < radius) {
48 result.navigationCosts[index] = 0.f;
49 continue;
50 }
51 const int x0 = int(std::clamp(std::floor(cx - radius), 0.0, double(result.width - 1)));
52 const int x1 = int(std::clamp(std::floor(cx + radius), 0.0, double(result.width - 1)));
53 const int y0 = int(std::clamp(std::floor(cy - radius), 0.0, double(result.height - 1)));
54 const int y1 = int(std::clamp(std::floor(cy + radius), 0.0, double(result.height - 1)));
55 bool blocked = false;
56 for (int wy = y0; wy <= y1 && !blocked; ++wy)
57 for (int wx = x0; wx <= x1; ++wx) {
58 if (checks == maxChecks)
59 return fail(DiagnosticCode::PreconditionViolation, "Clearance work budget exhausted");
60 ++checks;
61 if (result.terrainCosts[static_cast<std::size_t>(wy * result.width + wx)] != 0.f) continue;
62 const double dx = cx - std::clamp(cx, double(wx), double(wx + 1));
63 const double dy = cy - std::clamp(cy, double(wy), double(wy + 1));
64 if (dx * dx + dy * dy < radius * radius) {
65 blocked = true;
66 break;
67 }
68 }
69 if (blocked) result.navigationCosts[index] = 0.f;
70 }
72} catch (const std::exception& error) {
74 Diagnostic::error(DiagnosticCode::Failed, error.what(), "crowdSurfaceAdapter"));
75}
76} // namespace eve::rts
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
eve::resource::CostSpec cost
std::string message
DiagnosticCode code
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
glm::dvec3 dx
std::string error
Definition Package.cpp:174
Deterministic phase-one RTS systems and their ECS contracts.
std::string geometry
std::string_view source
float dy
Definition Shadow.cpp:151
Heightmap stamp
uint32_t index
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.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Immutable owning capture; independent of Pathfinder and TileLayer lifetimes.
Definition Pathfinder.h:27
Result< crowd::NavigationSurfaceSnapshot > projectCrowdSurface(const map::NavigationGridSnapshot &source, crowd::NavigationSurfaceStamp stamp, const NavigationGrid &geometry, std::int64_t maxChecks)
Upper-domain adapter; owns output and never retains map spans.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning adapter input: raw physical terrain and separately inflated navigation costs....
Definition CrowdTypes.h:70
Provenance of one immutable navigation projection.
Definition CrowdTypes.h:33
World/grid conversion used when an RTS composition consumes a canonical map Pathfinder.
Definition RTSSystems.h:265