nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
wander.cc
Go to the documentation of this file.
1#include "bot/wander.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <numbers>
7#include <optional>
8#include <vector>
9
10namespace nanommo {
11
12namespace {
13
14constexpr float kReach = 170.0f;
15constexpr int kStuck = 3;
16constexpr float kThroughTheGate = 0.2f;
17
18} // namespace
19
20protocol::Input RandomDirection(std::mt19937& random) {
21 std::uniform_int_distribution<int> key(-1, 1);
22 protocol::Input input;
23 input.set_x(static_cast<float>(key(random)));
24 input.set_z(static_cast<float>(key(random)));
25 std::bernoulli_distribution shift(1.0 / 3.0);
26 input.set_run(shift(random));
27 return input;
28}
29
30std::chrono::milliseconds RandomHold(std::mt19937& random) {
31 std::uniform_int_distribution<int> ms(500, 3000);
32 return std::chrono::milliseconds(ms(random));
33}
34
35Place RandomGate(std::mt19937& random, int land) {
36 std::vector<Place> gates;
37 for (const Gate& gate : kGates) {
38 if (gate.land == land) {
39 gates.push_back(gate.place);
40 }
41 }
42 // Each land has a gate, as the way back to the land before.
43 return gates[std::uniform_int_distribution<std::size_t>(
44 0, gates.size() - 1)(random)];
45}
46
47Trip RandomTrip(std::mt19937& random, const Place& gate) {
48 std::uniform_real_distribution<float> unit(0.0f, 1.0f);
49 if (unit(random) < kThroughTheGate) {
50 // All the way to its middle: three metres short, it would stop outside.
51 return {.to_x = gate.x, .to_z = gate.z, .there = kGateRadius / 2};
52 }
53 const float angle = 2.0f * std::numbers::pi_v<float> * unit(random);
54 // The square root spreads the trips evenly over the circle's area.
55 const float distance = kReach * std::sqrt(unit(random));
56 return {.to_x = distance * std::cos(angle),
57 .to_z = distance * std::sin(angle)};
58}
59
60std::optional<protocol::Input> Travel(Trip& trip, float x, float z) {
61 const float dx = trip.to_x - x;
62 const float dz = trip.to_z - z;
63 const float distance = std::hypot(dx, dz);
64 if (distance < trip.there) {
65 return std::nullopt;
66 }
67 if (distance < trip.closest - 0.5f) {
68 trip.closest = distance;
69 trip.stuck = 0;
70 } else if (++trip.stuck >= kStuck) {
71 return std::nullopt;
72 }
73 protocol::Input input;
74 input.set_x(dx / distance);
75 input.set_z(dz / distance);
76 input.set_run(true);
77 return input;
78}
79
80std::optional<protocol::Input> StepOut(const MonsterTable& table,
81 const protocol::World& world, float x,
82 float z) {
83 for (const protocol::Monster& monster : world.monsters()) {
84 const auto kind = table.kinds.find(monster.kind());
85 if (monster.motion() != protocol::MOTION_SKILL ||
86 kind == table.kinds.end()) {
87 continue;
88 }
89 const auto skill = std::ranges::find(kind->second.skills(), monster.skill(),
90 &monsters::Skill::id);
91 if (skill == kind->second.skills().end()) {
92 continue;
93 }
94 monsters::Skill clear = *skill;
95 clear.set_size(skill->size() + kClear);
96 if (!InShape(clear, {.x = monster.x(), .z = monster.z()}, monster.yaw(),
97 {.x = x, .z = z})) {
98 continue;
99 }
100 // Away from the monster: out of a circle, and out of the far end of a
101 // cone, unless its nearer side is nearer, as a narrow one's is.
102 const float dx = x - monster.x();
103 const float dz = z - monster.z();
104 const float away = std::hypot(dx, dz);
105 float out_x = away > 0.0f ? dx / away : 1.0f;
106 float out_z = away > 0.0f ? dz / away : 0.0f;
107 if (skill->shape() == monsters::CONE && skill->arc() < 180.0f) {
108 // Along the way it's aimed, and across it, to the side the bot's on.
109 const float ahead_x = std::sin(monster.yaw());
110 const float ahead_z = std::cos(monster.yaw());
111 const float along = dx * ahead_x + dz * ahead_z;
112 const float across = dx * ahead_z - dz * ahead_x;
113 const float half =
114 skill->arc() / 2.0f * std::numbers::pi_v<float> / 180.0f;
115 const float to_side = along * std::tan(half) - std::abs(across);
116 if (to_side < skill->size() - away) {
117 const float side = across >= 0.0f ? 1.0f : -1.0f;
118 out_x = side * ahead_z;
119 out_z = -side * ahead_x;
120 }
121 }
122 protocol::Input input;
123 input.set_x(out_x);
124 input.set_z(out_z);
125 input.set_run(true);
126 return input;
127 }
128 return std::nullopt;
129}
130
131} // namespace nanommo
Definition chat.cc:9
Trip RandomTrip(std::mt19937 &random, const Place &gate)
Definition wander.cc:47
std::optional< protocol::Input > Travel(Trip &trip, float x, float z)
Definition wander.cc:60
bool InShape(const monsters::Skill &skill, Place from, float yaw, Place place)
Definition monsters.cc:172
constexpr std::array< Gate, 4 > kGates
Definition lands.h:45
Place RandomGate(std::mt19937 &random, int land)
One of the land's gates, at random, as players go through each of them.
Definition wander.cc:35
protocol::Input RandomDirection(std::mt19937 &random)
Definition wander.cc:20
std::optional< protocol::Input > StepOut(const MonsterTable &table, const protocol::World &world, float x, float z)
Definition wander.cc:80
constexpr float kClear
Definition wander.h:43
std::chrono::milliseconds RandomHold(std::mt19937 &random)
Definition wander.cc:30
constexpr float kGateRadius
In metres from the middle of a gate.
Definition lands.h:81
A ring of stones in a land, which takes players to another.
Definition lands.h:38
The kinds of monster, by id, and where they spawn.
Definition monsters.h:17
std::map< uint32_t, monsters::Kind > kinds
Definition monsters.h:18
In metres from the middle of a land.
Definition lands.h:13
float z
Definition lands.h:15
float x
Definition lands.h:14
Somewhere a bot heads for, across the island.
Definition wander.h:20
float there
How close to the end counts as there, in metres.
Definition wander.h:26
float to_z
Definition wander.h:22
float closest
Definition wander.h:23
float to_x
Definition wander.h:21