20constexpr float kHalfWidth = 0.4f;
21constexpr float kHeight = 1.8f;
25constexpr float kStep = 0.5f;
26constexpr float kFarthest = 150.0f;
28constexpr int kSteps =
static_cast<int>(kFarthest / kStep);
31template <
typename Test>
32const Figure* Pick(
const std::vector<Figure>& figures,
const Ray& ray,
34 const Figure* picked =
nullptr;
35 float nearest = std::numeric_limits<float>::infinity();
36 for (
const Figure& figure : figures) {
40 const Vector3& at = figure.position;
42 const float height = std::max(kHeight, figure.height);
43 const float half = kHalfWidth * height / kHeight;
44 const RayCollision hit = GetRayCollisionBox(
45 ray, {.min = {at.x - half, at.y, at.z - half},
46 .max = {at.x + half, at.y + height, at.z + half}});
47 if (hit.hit && hit.distance < nearest) {
48 nearest = hit.distance;
57const Figure*
PickNpc(
const std::vector<Figure>& figures,
const Ray& ray,
59 return Pick(figures, ray, [you](
const Figure& figure) {
69 return !figure.
monster && !figure.
npc && !figure.
you && !enemy.empty() &&
70 figure.
guild == enemy;
74 uint32_t opponent, std::string_view enemy) {
75 return Pick(figures, ray, [opponent, enemy](
const Figure& figure) {
77 Enemy(figure, enemy)) &&
78 figure.
to != protocol::MOTION_DEAD;
83 return Pick(figures, ray, [](
const Figure& figure) {
return !figure.
you; });
86bool Picked(
const Figure& figure, uint32_t target, uint32_t target_player) {
88 return target != 0 && figure.
id == target;
90 return !figure.
npc && target_player != 0 && figure.
id == target_player;
94 const auto below = [&terrain, &ray](
float along) {
96 Vector3Add(ray.position, Vector3Scale(ray.direction, along));
97 return at.y < terrain.
Height(at.x, at.z);
102 for (
int step = 1; step <= kSteps; ++step) {
103 float far =
static_cast<float>(step) * kStep;
105 for (
int halving = 0; halving < 12; ++halving) {
106 const float middle = (near + far) / 2.0f;
107 (below(middle) ? far : near) = middle;
109 return Vector3Add(ray.position, Vector3Scale(ray.direction, far));
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
bool Enemy(const Figure &figure, std::string_view enemy)
const Figure * PickMonster(const std::vector< Figure > &figures, const Ray &ray, uint32_t opponent, std::string_view enemy)
std::optional< Vector3 > PickGround(const Terrain &terrain, const Ray &ray)
const Figure * PickAnyone(const std::vector< Figure > &figures, const Ray &ray)
constexpr float kTalkReach
const Figure * PickNpc(const std::vector< Figure > &figures, const Ray &ray, Vector3 you)