载入中...
搜索中...
未找到
TargetingLineOfSightAdapter.cpp
浏览该文件的文档.
2
3#include "physics/World3D.h"
4
5#include <algorithm>
6#include <cmath>
7#include <string>
8#include <utility>
9
10namespace eve::physics {
11namespace {
12
13} // namespace
14
16 if (!world)
18 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "Physics LOS cannot register a null World3D"));
19 if (!world->isValid())
21 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "Physics LOS cannot register an invalid World3D"));
22
23 for (auto it = worlds_.begin(); it != worlds_.end();) {
24 auto lifetime = it->lifetime.lock();
25 if (!lifetime) {
26 it = worlds_.erase(it);
27 continue;
28 }
31 "Physics LOS already has a different active World3D"));
32 }
33 worlds_.push_back({world, world->queryLifetime_});
35}
36
38 if (!world)
40 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "Physics LOS cannot remove a null World3D"));
41 const auto before = worlds_.size();
42 std::erase_if(worlds_, [world](const RegisteredWorld& entry) { return entry.world == world; });
43 const auto code = before == worlds_.size() ? eve::StatusCode::NoOp : eve::StatusCode::Applied;
45}
46
48 const sensing::TargetLocation& to) const {
49 const auto* fromPoint = std::get_if<sensing::WorldPoint>(&from);
50 const auto* toPoint = std::get_if<sensing::WorldPoint>(&to);
51 if (!fromPoint || !toPoint || !fromPoint->isValid() || !toPoint->isValid() ||
52 fromPoint->space() != sensing::CoordinateSpace::World3D ||
53 toPoint->space() != sensing::CoordinateSpace::World3D)
55 eve::DiagnosticCode::Unsupported, "Physics LOS supports only two valid World3D points in the same space"));
56
57 if (!std::isfinite(fromPoint->x()) || !std::isfinite(fromPoint->y()) || !std::isfinite(fromPoint->z()) ||
58 !std::isfinite(toPoint->x()) || !std::isfinite(toPoint->y()) || !std::isfinite(toPoint->z()))
60 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "Physics LOS points must be finite"));
61
62 World3D* activeWorld = nullptr;
63 std::shared_ptr<const void> activeLifetime;
64 for (const RegisteredWorld& entry : worlds_) {
65 auto lifetime = entry.lifetime.lock();
66 World3D* world = entry.world;
67 if (!lifetime || !world || !world->isValid()) continue;
68 if (activeWorld)
70 eve::DiagnosticCode::Conflict, "Physics LOS has more than one valid active World3D"));
71 activeWorld = world;
72 activeLifetime = std::move(lifetime);
73 }
74 if (!activeWorld)
76 eve::Diagnostic::error(eve::DiagnosticCode::Unsupported, "Physics LOS has no valid active World3D"));
77
78 activeWorld->rayCast(fromPoint->x(), fromPoint->y(), fromPoint->z(), toPoint->x(), toPoint->y(), toPoint->z());
79 if (activeWorld->hasRayHit())
82}
83
84} // namespace eve::physics
std::string from
DiagnosticCode code
HexCoordinates to
Cell the unit walks towards on this segment.
Definition HexUnits.cpp:64
World3D * world
std::weak_ptr< const void > lifetime
Physics-to-sensing line-of-sight capability adapter.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.h:81
Result< void > removeWorld(World3D *world)
Removes a previously registered world; repeated removal is a NoOp.
Result< sensing::LineOfSightResult > query(const sensing::TargetLocation &from, const sensing::TargetLocation &to) const override
Tests the segment between two same-space locations.
Result< void > addWorld(World3D *world)
Registers a borrowed live World3D as the sole active LOS world.
Box3D rigid-body world. Script coordinates are meters (Box3D native), unlike 2D World which uses pixe...
Definition World3D.h:63
bool hasRayHit() const
Definition World3D.h:514
int rayCast(float x1, float y1, float z1, float x2, float y2, float z2)
Closest raycast from (x1,y1,z1) to (x2,y2,z2) in meters. Returns hit body id, or -1....
Definition World3D.cpp:1332
Optional physics backend for vehicle mobility and body attach.
Definition Climbing.h:36
std::variant< WorldPoint, GridPoint > TargetLocation
Either a world point or a grid point, with the tag retained.
Definition Targeting.h:198
Result of one injected line-of-sight query.
Definition Targeting.h:417