nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
bot.h
Go to the documentation of this file.
1#ifndef NANOMMO_BOT_BOT_H_
2#define NANOMMO_BOT_BOT_H_
3
4#include <array>
5#include <chrono>
6#include <cstddef>
7#include <cstdint>
8#include <deque>
9#include <optional>
10#include <random>
11#include <string>
12#include <string_view>
13#include <vector>
14
15#include "asio/io_context.hpp"
16#include "asio/ip/tcp.hpp"
17#include "asio/ip/udp.hpp"
18#include "asio/ssl/context.hpp"
19#include "asio/steady_timer.hpp"
20#include "bot/quests.h"
21#include "bot/wander.h"
22#include "bot/war.h"
23#include "content/content.h"
24#include "protocol/framing.h"
25#include "protocol/nanommo.pb.h"
26#include "protocol/parts.h"
27#include "protocol/tls.h"
28
29namespace nanommo {
30
31class Bot {
32 public:
33 /// Finds the NPCs that talk through their kinds in the content, and what
34 /// its items do through the items. A bot with a side is at war. Its seed
35 /// picks the faction it swears to.
36 Bot(asio::io_context& io, asio::ssl::context& tls, uint32_t seed, bool udp,
37 const Content& content, std::optional<Side> side = std::nullopt);
38
39 Bot(const Bot&) = delete;
40 Bot& operator=(const Bot&) = delete;
41
42 void Start(const asio::ip::tcp::resolver::results_type& server,
43 const std::string& token);
44
45 bool connected() const { return connected_; }
46
47 /// Where the bot's world told it to go, once it hands its players over.
48 const std::optional<protocol::Move>& move() const { return move_; }
49
50 /// Closes the connection, as a player who goes elsewhere does.
51 void Leave();
52
53 const std::vector<int64_t>& newest() const { return newest_; }
54
55 private:
56 void Greet(const std::string& token, const std::string& host,
57 std::error_code error);
58 void Read();
59 void HelloOverUdp();
60 void ReceiveOverUdp();
61 void Receive(std::string_view bytes);
62 void Wander();
63 /// Writes the input to the server, after any still being written.
64 void Send(const protocol::Input& input);
65 /// Each of the server's ticks, as the client does: the keys as they are,
66 /// and the newest world the bot has whole.
67 void Tick();
68 void Write(std::string frame);
69 /// Wanders again once the wait is over.
70 void WanderIn(std::chrono::milliseconds wait);
71 void Stop();
72
73 TlsStream stream_;
74 asio::ip::udp::socket udp_socket_;
75 asio::steady_timer timer_;
76 asio::steady_timer hello_timer_;
77 asio::steady_timer tick_timer_;
78 std::mt19937 random_;
79 bool udp_;
80 const Content& content_;
81 std::string token_;
82 std::array<char, 65536> buffer_;
83 std::array<char, 2048> datagram_;
84 FrameReader reader_;
85 std::vector<int64_t> newest_;
86 bool udp_worlds_ = false;
87 /// What's to be written, the one being written first.
88 std::deque<std::string> outbox_;
89 /// The keys the bot holds down, as it last sent them.
90 protocol::Input keys_;
91 bool connected_ = false;
92 float x_ = 0.0f;
93 float z_ = 0.0f;
94 uint32_t land_ = 0;
95 std::optional<Trip> trip_;
96 int stay_ = 0;
97 /// The monster the bot swings at, by its id, while it hunts.
98 uint32_t hunting_ = 0;
99 /// The storyteller in the bot's land, once the bot has seen it.
100 std::optional<protocol::Npc> storyteller_;
101 /// Whether the trip goes to the storyteller, for a story.
102 bool asking_ = false;
103 /// The skills the bot knows, from its world.
104 std::vector<uint32_t> skills_;
105 int health_ = 0;
106 int max_health_ = 0;
107 /// At war, the enemy it's after, by their id.
108 uint32_t enemy_ = 0;
109 /// Whether the bot's dead, and if so, whether its wait is over.
110 bool dead_ = false;
111 bool can_respawn_ = false;
112 /// Whether it's running out of a monster's skill on its way.
113 bool stepping_out_ = false;
114 /// At war, whether its side's guild has asked it in.
115 bool asked_in_ = false;
116 protocol::Inventory bag_;
117 /// When it last drank something, which it does once its cooldown's over.
118 std::chrono::steady_clock::time_point drank_;
119 /// The world around the bot, put together from its parts as a client does,
120 /// for the monsters and NPCs in it.
121 Parts parts_;
122 protocol::Quests quests_;
123 /// What the trip's for, if it's for a quest, or an oath.
124 std::optional<Errand> errand_;
125 std::optional<protocol::Move> move_;
126 /// At war, its side, its guild as the server last sent it, and the recruit
127 /// its leader asks next.
128 std::optional<Side> side_;
129 protocol::Guild guild_;
130 std::size_t recruit_ = 0;
131 /// When it joined its side's guild, and whether it's left the camp for the
132 /// charge, which it does once.
133 std::optional<std::chrono::steady_clock::time_point> joined_;
134 bool charged_ = false;
135 /// Whether it's sworn to its faction, or given up, as its herald isn't
136 /// where they stand, and which that is, by its id.
137 bool swore_ = false;
138 uint32_t faction_ = 0;
139};
140
141} // namespace nanommo
142
143#endif // NANOMMO_BOT_BOT_H_
const std::optional< protocol::Move > & move() const
Where the bot's world told it to go, once it hands its players over.
Definition bot.h:48
const std::vector< int64_t > & newest() const
Definition bot.h:53
Bot & operator=(const Bot &)=delete
void Start(const asio::ip::tcp::resolver::results_type &server, const std::string &token)
Definition bot.cc:91
void Leave()
Closes the connection, as a player who goes elsewhere does.
Definition bot.cc:534
bool connected() const
Definition bot.h:45
Bot(asio::io_context &io, asio::ssl::context &tls, uint32_t seed, bool udp, const Content &content, std::optional< Side > side=std::nullopt)
Definition bot.cc:78
Bot(const Bot &)=delete
Definition chat.cc:9
asio::ssl::stream< asio::ip::tcp::socket > TlsStream
Definition tls.h:14
void Wander(Monster &monster, const Terrain &terrain, std::chrono::nanoseconds time, std::mt19937 &random)
Definition monsters.cc:162