载入中...
搜索中...
未找到
Sensing.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6#include "common/Module.h"
7#include "common/Result.h"
10#include "common/Value.h"
12
13#include <cstdint>
14#include <limits>
15#include <map>
16#include <memory>
17#include <optional>
18#include <set>
19#include <span>
20#include <string>
21#include <string_view>
22#include <unordered_map>
23#include <variant>
24#include <vector>
25
26namespace eve::spatial {
27class SpatialHash2D;
28class SpatialHash3D;
29} // namespace eve::spatial
30
31namespace eve::sensing {
32
33class ILineOfSightQuery;
34class EnvironmentQueryPlan;
35struct EnvironmentRequest;
36struct EnvironmentServices;
37struct EnvironmentQueryResult;
38
40struct Subject {
41 std::string id;
42 float x = 0, y = 0;
43 std::string faction;
44 std::set<std::string> tags;
45 std::set<std::string> visibleTo;
46 std::set<std::string> zones;
47 float z = 0.f;
48 float pathProgress = 0.f;
49 float strength = 0.f;
50};
51
53struct Candidate {
54 std::string id;
55 float x = 0, y = 0, distance = 0;
56 float z = 0.f;
57};
58
60enum class CountPolicy : std::uint8_t {
65};
66
77
80 float x = 0.f;
81 float y = 0.f;
82 float radius = 0.f;
83};
84
86struct QueryBox {
87 float minX = 0.f;
88 float minY = 0.f;
89 float maxX = 0.f;
90 float maxY = 0.f;
91};
92
99struct QueryCone {
100 float x = 0.f;
101 float y = 0.f;
102 float dirX = 1.f;
103 float dirY = 0.f;
104 float halfAngle = 0.f;
105 float range = 0.f;
106};
107
110 float x = 0, y = 0, z = 0, radius = 0;
111};
113enum class QueryAxes : std::uint8_t { XY, XZ, YZ, XYZ };
115enum class SpatialIndexKind : std::uint8_t { None, XY, XZ, YZ, XYZ };
116
118using QueryShape = std::variant<std::monostate, QueryCircle, QueryBox, QueryCone, QuerySphere>;
119
128struct QuerySpec {
130 float minRange = 0.f;
131 float maxRange = std::numeric_limits<float>::infinity();
132 std::vector<std::string> requiredTags;
133 std::vector<std::string> excludedTags;
134 std::vector<std::string> includeFactions;
135 std::vector<std::string> excludeFactions;
136 std::optional<std::string> visibleTo;
137 std::uint32_t minCount = 0;
138 std::uint32_t maxCount = std::numeric_limits<std::uint32_t>::max();
141
142 bool requireLineOfSight = false;
147};
148
151 float x = 0.f;
152 float y = 0.f;
153 std::optional<std::string> subjectId;
154 float z = 0.f;
156};
157
160 std::string id;
161 float x = 0.f;
162 float y = 0.f;
163 float distance = 0.f;
164 float score = 0.f;
165 std::string scoreReason;
166 float z = 0.f;
167 float pathProgress = 0.f;
168 float strength = 0.f;
170};
171
174public:
178 explicit CandidateQueryResult(std::vector<RankedCandidate> ranked) : ranked_(std::move(ranked)) {}
179
181 [[nodiscard]] std::span<const RankedCandidate> ranked() const noexcept { return ranked_; }
183 [[nodiscard]] std::size_t size() const noexcept { return ranked_.size(); }
184
185private:
186 std::vector<RankedCandidate> ranked_;
187};
188
191 std::vector<RankedCandidate> candidates;
192 bool usedSpatial = false;
193 std::uint32_t scanned = 0;
194};
195
198public:
203 [[nodiscard]] Result<void> upsertPosition(std::string_view id, float x, float y, float z, std::string_view faction,
204 std::string_view tagsCsv, std::string_view visibleToCsv);
206 [[nodiscard]] eve::Result<void> upsert(std::string_view id, float x, float y, std::string_view faction,
207 std::string_view tagsCsv, std::string_view visibleToCsv);
209 [[nodiscard]] eve::Result<void> remove(std::string_view id);
217 [[nodiscard]] eve::Result<void> setZones(std::string_view id, std::string_view zonesCsv);
226 [[nodiscard]] eve::Result<void> setMetrics(std::string_view id, float pathProgress, float strength);
227
236 [[nodiscard]] eve::Result<void> setSpatialIndexEnabled(bool enabled, float cellSize = 64.f);
242 [[nodiscard]] Result<void> rebuildIndex(SpatialIndexKind kind, float cellSize);
244 [[nodiscard]] SpatialIndexKind indexKind() const noexcept;
246 [[nodiscard]] bool spatialIndexEnabled() const noexcept;
247
256 [[nodiscard]] eve::Result<CandidateQueryResult> query(const QueryOrigin& origin, const QuerySpec& spec);
263 [[nodiscard]] Result<CandidateQueryResult> query(const QueryOrigin& origin, const QuerySpec& spec,
265
272 [[nodiscard]] eve::Result<CandidateCollection> collectCandidates(const QueryOrigin& origin,
273 const QuerySpec& spec) const;
274
278 [[nodiscard]] Result<EnvironmentRequest> environmentInput(const QueryOrigin& origin, const QuerySpec& spec) const;
285 [[nodiscard]] Result<EnvironmentQueryResult> executeEnvironmentQuery(const EnvironmentQueryPlan& plan,
287 const EnvironmentServices& services);
293 [[nodiscard]] Result<Value> queryPositions(float originX, float originY, float targetX, float targetY, float radius,
294 int samples, int limit, bool requireLineOfSight);
295
297 [[nodiscard]] eve::Result<int> circle(float x, float y, float radius, std::string_view requireTagsCsv,
298 std::string_view excludeTagsCsv, std::string_view includeFactionsCsv,
299 std::string_view excludeFactionsCsv, std::string_view visibleTo, int limit);
304 [[nodiscard]] eve::Result<int> circleSorted(float x, float y, float radius, std::string_view requireTagsCsv,
305 std::string_view excludeTagsCsv, std::string_view includeFactionsCsv,
306 std::string_view excludeFactionsCsv, std::string_view visibleTo,
307 int limit, std::string_view sortPolicy);
309 [[nodiscard]] eve::Result<int> box(float minX, float minY, float maxX, float maxY, std::string_view requireTagsCsv,
310 std::string_view excludeTagsCsv, std::string_view includeFactionsCsv,
311 std::string_view excludeFactionsCsv, std::string_view visibleTo, int limit);
322 [[nodiscard]] eve::Result<int> executePreset(std::string_view presetId, float originX, float originY, float dirX,
323 float dirY);
332 [[nodiscard]] eve::OptionalRef<const Candidate> resultAt(int index) const;
338 [[nodiscard]] std::string debugLastQueryJson() const;
340 std::string snapshotJson() const;
342 [[nodiscard]] eve::Result<void> restoreJson(const std::string& json);
343
345 [[nodiscard]] const std::map<std::string, Subject>& subjects() const noexcept { return subjects_; }
346
350 SensingWorld(const SensingWorld&) = delete;
355 SensingWorld& operator=(SensingWorld&&) noexcept;
356
357private:
358 [[nodiscard]] bool accepts(const Subject& subject, const QuerySpec& spec) const;
359 void publishResults(std::vector<RankedCandidate> ranked);
360 void clearSpatialIndex();
361 void rebuildSpatialIndex();
362 void indexSubject(const Subject& subject);
363 void unindexSubject(const std::string& id);
365 [[nodiscard]] bool trySpatialBroadphase(const QueryOrigin& origin, const QuerySpec& spec,
366 std::vector<const Subject*>& out) const;
367
368 std::map<std::string, Subject> subjects_;
369 std::vector<Candidate> results_;
370
371 std::unique_ptr<eve::spatial::SpatialHash2D> spatialIndex_;
372 std::unique_ptr<eve::spatial::SpatialHash3D> spatialIndex3D_;
373 SpatialIndexKind indexKind_ = SpatialIndexKind::None;
374 std::unordered_map<std::string, int> subjectSpatialIds_;
375 // Derived references into owned map nodes: stable across upserts, removed before erase,
376 // and rebuilt after restore. No borrowed external storage or worker access.
377 std::unordered_map<int, const Subject*> spatialIdSubjects_;
378 int nextSpatialId_ = 1;
379
380 struct LastQueryDebug {
381 bool usedSpatial = false;
382 std::uint32_t scanned = 0;
383 std::uint32_t accepted = 0;
384 float originX = 0.f;
385 float originY = 0.f;
386 float originZ = 0.f;
387 CoordinateSpace space = CoordinateSpace::World2D;
388 std::string shapeKind = "none";
389 std::vector<RankedCandidate> ranked;
390 };
391 LastQueryDebug lastQuery_;
392 std::string lastEnvironmentQuery_ = "null";
393};
394
399
402public:
405 Sensing();
407 ~Sensing() override;
416 [[nodiscard]] static eve::Result<SensingWorldHandleRef> newWorld();
418 [[nodiscard]] static eve::script::Borrowed<SensingWorld> resolve(SensingWorldHandleRef reference) noexcept;
420 [[nodiscard]] static eve::Result<void> release(SensingWorldHandleRef reference);
422 [[nodiscard]] static bool isStale(SensingWorldHandleRef reference) noexcept;
423
426 [[nodiscard]] int worldCount() const override;
429 [[nodiscard]] std::vector<eve::SensingWorldQuery> lastQueries() const override;
430
431private:
433};
434
442 std::string subjectId;
443 float x = 0.f;
444 float y = 0.f;
445 float distance = 0.f;
446 float score = 0.f;
447 std::string scoreReason;
448 float z = 0.f;
450};
451
457[[nodiscard]] inline std::vector<PerceptionFact> perceptionFactsFrom(const CandidateQueryResult& result) {
458 std::vector<PerceptionFact> facts;
459 facts.reserve(result.size());
460 for (const auto& candidate : result.ranked()) {
461 PerceptionFact fact;
462 fact.subjectId = candidate.id;
463 fact.x = candidate.x;
464 fact.y = candidate.y;
465 fact.z = candidate.z;
466 fact.space = candidate.space;
467 fact.distance = candidate.distance;
468 fact.score = candidate.score;
469 fact.scoreReason = candidate.scoreReason;
470 facts.push_back(std::move(fact));
471 }
472 return facts;
473}
474
475} // namespace eve::sensing
double z
double y
double x
int subject
Definition AnimSmr.cpp:163
eve::action::ActionSpatialBinding spatial
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
Vocabulary aliases for nullable and checked borrowed references.
#define EVENGINE_API_PLATFORM
Definition Export.h:107
float strength
const GltfImportRequest & request
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
const std::string * accepts
glm::dvec3 origin
Move-only, checked operation results for the common layer.
ScriptStructure out
std::string kind
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
float targetX
int limit
Definition TreeMesh.cpp:166
uint32_t index
Owning, renderer-independent dynamic values.
Runtime sensing read surface (provided by the sensing module).
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Non-owning observation of a registry-owned object.
Slot/generation registry whose objects are exclusively unique-owned.
Owning ranked result; does not assign a primary target.
Definition Sensing.h:173
std::size_t size() const noexcept
Returns the size of .
Definition Sensing.h:183
CandidateQueryResult(std::vector< RankedCandidate > ranked)
Constructs a CandidateQueryResult.
Definition Sensing.h:178
std::span< const RankedCandidate > ranked() const noexcept
Ranked.
Definition Sensing.h:181
CandidateQueryResult()=default
Constructs a CandidateQueryResult.
Immutable resolved plan, safe to reuse after definitions are edited or destroyed. @ownership Shares i...
Mirrored world positions and query composition; consumers decide gameplay actions from results.
Definition Sensing.h:197
const std::map< std::string, Subject > & subjects() const noexcept
Returns a borrowed view of stored subjects for adapters; valid until mutation.
Definition Sensing.h:345
SensingWorld(SensingWorld &&) noexcept
Sensing world.
SensingWorld(const SensingWorld &)=delete
SensingWorld & operator=(const SensingWorld &)=delete
Script factory for independent sensing worlds.
Definition Sensing.h:401
SortKey
Ordering applied to ranked query results.
Definition Sensing.h:68
std::vector< PerceptionFact > perceptionFactsFrom(const CandidateQueryResult &result)
Projects ranked query results into adapter-neutral perception facts.
Definition Sensing.h:457
CoordinateSpace
Coordinate spaces are not interchangeable, even when dimensions match.
QueryAxes
Independent distance/projection semantics; planar shapes use these two axes.
Definition Sensing.h:113
std::variant< std::monostate, QueryCircle, QueryBox, QueryCone, QuerySphere > QueryShape
Optional broad-phase shape; monostate means range-only around the origin.
Definition Sensing.h:118
CountPolicy
How candidate-count overflow relative to maxCount is handled.
Definition Sensing.h:60
@ TruncateToMax
Keep the best maxCount candidates after sorting (legacy circle/box limit).
@ FailIfOutOfRange
Fail the query when the filtered set is outside [minCount, maxCount].
SpatialIndexKind
Broad-phase implementation; selecting an index never changes query semantics.
Definition Sensing.h:115
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
bool enabled
Owning broad-phase output; collection never publishes or applies result limits.
Definition Sensing.h:190
std::vector< RankedCandidate > candidates
Definition Sensing.h:191
A deterministic candidate returned by a spatial query.
Definition Sensing.h:53
Per-execution values. No world pointers or callbacks are retained.
Borrowed providers for this synchronous execution only.
Neutral perception fact projected from a ranked sensing candidate.
Definition Sensing.h:441
CoordinateSpace space
Definition Sensing.h:449
Inclusive axis-aligned box shape for QuerySpec (World2D).
Definition Sensing.h:86
Circle shape for QuerySpec (World2D).
Definition Sensing.h:79
2D cone/sector shape for QuerySpec (World2D).
Definition Sensing.h:99
World origin used for range tests, distance sorting and LOS.
Definition Sensing.h:150
std::optional< std::string > subjectId
Definition Sensing.h:153
CoordinateSpace space
Definition Sensing.h:155
Configurable candidate query against SensingWorld.
Definition Sensing.h:128
std::vector< std::string > excludedTags
Definition Sensing.h:133
CountPolicy countPolicy
Definition Sensing.h:139
std::vector< std::string > excludeFactions
Definition Sensing.h:135
std::uint32_t minCount
Definition Sensing.h:137
EVENGINE_API_PLATFORM eve::Result< void > validate() const
Validates ranges, counts, tags and shape invariants.
Definition Sensing.cpp:242
std::vector< std::string > requiredTags
Definition Sensing.h:132
std::uint32_t maxCount
Definition Sensing.h:138
std::optional< std::string > visibleTo
Definition Sensing.h:136
std::vector< std::string > includeFactions
Definition Sensing.h:134
Sphere in actual XYZ world coordinates.
Definition Sensing.h:109
One ranked candidate produced by SensingWorld::query.
Definition Sensing.h:159
Handle domain for module-owned sensing worlds.
Definition Sensing.h:396
Mirrored gameplay facts used by candidate queries.
Definition Sensing.h:40
std::string id
Definition Sensing.h:41
std::set< std::string > tags
Definition Sensing.h:44
std::string faction
Definition Sensing.h:43
std::set< std::string > zones
Definition Sensing.h:46
std::set< std::string > visibleTo
Definition Sensing.h:45