nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
picking.cc
Go to the documentation of this file.
1#include "client/picking.h"
2
3#include <raymath.h>
4
5#include <algorithm>
6#include <cmath>
7#include <cstdint>
8#include <limits>
9#include <optional>
10#include <string_view>
11#include <vector>
12
13#include "content/npcs.h"
14
15namespace nanommo {
16namespace {
17
18// A box around a person standing there, in metres: as wide as their arms
19// hang, and as tall as them.
20constexpr float kHalfWidth = 0.4f;
21constexpr float kHeight = 1.8f;
22
23// For the ground: half a metre at a time, up to as far as the fog lets anyone
24// see.
25constexpr float kStep = 0.5f;
26constexpr float kFarthest = 150.0f;
27// Counted in whole steps, as a float that's added to drifts.
28constexpr int kSteps = static_cast<int>(kFarthest / kStep);
29
30// The nearest of the figures along the ray that the test lets through.
31template <typename Test>
32const Figure* Pick(const std::vector<Figure>& figures, const Ray& ray,
33 Test test) {
34 const Figure* picked = nullptr;
35 float nearest = std::numeric_limits<float>::infinity();
36 for (const Figure& figure : figures) {
37 if (!test(figure)) {
38 continue;
39 }
40 const Vector3& at = figure.position;
41 // A monster taller than a person, such as a boss, is as much wider.
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;
49 picked = &figure;
50 }
51 }
52 return picked;
53}
54
55} // namespace
56
57const Figure* PickNpc(const std::vector<Figure>& figures, const Ray& ray,
58 Vector3 you) {
59 return Pick(figures, ray, [you](const Figure& figure) {
60 return figure.npc &&
61 (figure.talks || !figure.teaches.empty() || !figure.quests.empty() ||
62 figure.faction != 0) &&
63 std::hypot(figure.position.x - you.x, figure.position.z - you.z) <=
65 });
66}
67
68bool Enemy(const Figure& figure, std::string_view enemy) {
69 return !figure.monster && !figure.npc && !figure.you && !enemy.empty() &&
70 figure.guild == enemy;
71}
72
73const Figure* PickMonster(const std::vector<Figure>& figures, const Ray& ray,
74 uint32_t opponent, std::string_view enemy) {
75 return Pick(figures, ray, [opponent, enemy](const Figure& figure) {
76 return (figure.monster || Picked(figure, 0, opponent) ||
77 Enemy(figure, enemy)) &&
78 figure.to != protocol::MOTION_DEAD;
79 });
80}
81
82const Figure* PickAnyone(const std::vector<Figure>& figures, const Ray& ray) {
83 return Pick(figures, ray, [](const Figure& figure) { return !figure.you; });
84}
85
86bool Picked(const Figure& figure, uint32_t target, uint32_t target_player) {
87 if (figure.monster) {
88 return target != 0 && figure.id == target;
89 }
90 return !figure.npc && target_player != 0 && figure.id == target_player;
91}
92
93std::optional<Vector3> PickGround(const Terrain& terrain, const Ray& ray) {
94 const auto below = [&terrain, &ray](float along) {
95 const Vector3 at =
96 Vector3Add(ray.position, Vector3Scale(ray.direction, along));
97 return at.y < terrain.Height(at.x, at.z);
98 };
99 // In steps along the ray, until one's under the ground, then halving the
100 // last step, back and forth, to where it went under.
101 float near = 0.0f;
102 for (int step = 1; step <= kSteps; ++step) {
103 float far = static_cast<float>(step) * kStep;
104 if (below(far)) {
105 for (int halving = 0; halving < 12; ++halving) {
106 const float middle = (near + far) / 2.0f;
107 (below(middle) ? far : near) = middle;
108 }
109 return Vector3Add(ray.position, Vector3Scale(ray.direction, far));
110 }
111 near = far;
112 }
113 return std::nullopt;
114}
115
116} // namespace nanommo
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
Definition chat.cc:9
bool Picked(const Figure &figure, uint32_t target, uint32_t target_player)
Definition picking.cc:86
bool Enemy(const Figure &figure, std::string_view enemy)
Definition picking.cc:68
const Figure * PickMonster(const std::vector< Figure > &figures, const Ray &ray, uint32_t opponent, std::string_view enemy)
Definition picking.cc:73
std::optional< Vector3 > PickGround(const Terrain &terrain, const Ray &ray)
Definition picking.cc:93
const Figure * PickAnyone(const std::vector< Figure > &figures, const Ray &ray)
Definition picking.cc:82
constexpr float kTalkReach
Definition npcs.h:17
const Figure * PickNpc(const std::vector< Figure > &figures, const Ray &ray, Vector3 you)
Definition picking.cc:57
uint32_t id
Definition animator.h:21
std::vector< uint32_t > quests
The quests an NPC gives, by their ids in the quests file.
Definition animator.h:44
std::vector< uint32_t > teaches
The skills an NPC teaches, by their ids, when it's a trainer.
Definition animator.h:42
Vector3 position
Definition animator.h:63
std::string guild
A player's guild, by its name, or empty.
Definition animator.h:24
bool talks
Whether the player can talk to it, by clicking it.
Definition animator.h:40
protocol::Motion to
Definition animator.h:77
uint32_t faction
Definition animator.h:27