nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
quests.cc
Go to the documentation of this file.
1#include "bot/quests.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <limits>
7#include <optional>
8
9namespace nanommo {
10
11namespace {
12
13/// The NPC here who gives the quest, and takes it back, if there is one.
14const protocol::Npc* Giver(const quests::Quest& quest,
15 const protocol::World& seen) {
16 const auto found =
17 std::ranges::find(seen.npcs(), quest.giver(), &protocol::Npc::kind);
18 return found != seen.npcs().end() ? &*found : nullptr;
19}
20
21Errand AtGiver(Errand::Kind kind, const protocol::Npc& npc, uint32_t quest) {
22 Errand errand{.kind = kind, .x = npc.x(), .z = npc.z()};
23 errand.quest.set_npc(npc.id());
24 errand.quest.set_quest(quest);
25 return errand;
26}
27
28} // namespace
29
30std::optional<Errand> NextErrand(const Content& content,
31 const protocol::Quests& quests,
32 const protocol::World& seen, float x,
33 float z) {
34 std::optional<Errand> hunt;
35 float nearest = kJoinTheFight;
36 for (const protocol::Monster& monster : seen.monsters()) {
37 const float distance = std::hypot(monster.x() - x, monster.z() - z);
38 if (monster.phase() != 0 && monster.motion() != protocol::MOTION_DEAD &&
39 distance <= nearest) {
40 nearest = distance;
41 hunt = Errand{.kind = Errand::Kind::kHunt,
42 .x = monster.x(),
43 .z = monster.z(),
44 .monster = monster.id()};
45 }
46 }
47 if (hunt) {
48 return hunt;
49 }
50 nearest = std::numeric_limits<float>::infinity();
51 for (const auto& [id, killed] : quests.on()) {
52 const auto quest = content.quests.find(id);
53 if (quest == content.quests.end()) {
54 continue;
55 }
56 if (killed >= quest->second.kill().count()) {
57 if (const protocol::Npc* giver = Giver(quest->second, seen)) {
58 return AtGiver(Errand::Kind::kTurnIn, *giver, id);
59 }
60 continue;
61 }
62 for (const protocol::Monster& monster : seen.monsters()) {
63 const float distance = std::hypot(monster.x() - x, monster.z() - z);
64 if (monster.kind() == quest->second.kill().monster() &&
65 monster.motion() != protocol::MOTION_DEAD && distance < nearest) {
66 nearest = distance;
67 hunt = Errand{.kind = Errand::Kind::kHunt,
68 .x = monster.x(),
69 .z = monster.z(),
70 .monster = monster.id()};
71 }
72 }
73 }
74 if (hunt) {
75 return hunt;
76 }
77 for (const auto& [id, quest] : content.quests) {
78 if (quests.on().contains(id) ||
79 std::ranges::find(quests.done(), id) != quests.done().end()) {
80 continue;
81 }
82 if (const protocol::Npc* giver = Giver(quest, seen)) {
83 return AtGiver(Errand::Kind::kTake, *giver, id);
84 }
85 }
86 return std::nullopt;
87}
88
89bool Dead(const protocol::World& seen, uint32_t monster) {
90 return std::ranges::any_of(
91 seen.monsters(), [monster](const protocol::Monster& unit) {
92 return unit.id() == monster && unit.motion() == protocol::MOTION_DEAD;
93 });
94}
95
96} // namespace nanommo
Definition chat.cc:9
constexpr float kJoinTheFight
Definition quests.h:32
bool Dead(const protocol::World &seen, uint32_t monster)
Definition quests.cc:89
std::optional< Errand > NextErrand(const Content &content, const protocol::Quests &quests, const protocol::World &seen, float x, float z)
Definition quests.cc:30
std::map< uint32_t, quests::Quest > quests
By id.
Definition content.h:31
protocol::Quest quest
Who to, and which quest, for a quest.
Definition quests.h:24