R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
bot.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/ecs/components.hpp>
5#include <aegis2d/net/context.hpp>
6
9
10#include <chrono>
11#include <cstddef>
12#include <cstdint>
13#include <optional>
14#include <random>
15#include <variant>
16#include <vector>
17
18namespace rtype {
19
25public:
27 using Clock = std::chrono::steady_clock;
29 static constexpr std::chrono::milliseconds shortest_hold{150};
31 static constexpr std::chrono::milliseconds longest_hold{900};
32
35 explicit RandomInputs(std::uint32_t seed);
36
40 [[nodiscard]] aegis2d::ecs::PlayerInput update(Clock::time_point now);
41
44 [[nodiscard]] std::uint64_t picks() const noexcept;
45
46private:
47 std::mt19937 random_;
48 aegis2d::ecs::PlayerInput input_;
49 Clock::time_point until_{};
50 std::uint64_t picks_ = 0;
51};
52
56 ClientSettings server{.host = "127.0.0.1", .tcp_port = 4242, .name = "bot"};
58 std::uint32_t seed = 0;
60 std::optional<InputScript> script;
61};
62
70[[nodiscard]] std::vector<BotSettings> bot_team(const BotSettings& base, std::size_t count);
71
78class Bot {
79public:
81 using Clock = std::chrono::steady_clock;
82
86 Bot(aegis2d::net::NetworkContext& context, const BotSettings& settings);
87
89 void start();
90
94 void update(Clock::time_point now);
95
98 void leave();
99
102 [[nodiscard]] const ClientSession& session() const noexcept;
103
106 [[nodiscard]] aegis2d::ecs::PlayerInput input() const noexcept;
107
110 [[nodiscard]] std::uint64_t picks() const noexcept;
111
112private:
113 ClientSession session_;
114 std::variant<RandomInputs, ScriptedInputs> inputs_;
115 aegis2d::ecs::PlayerInput input_;
116};
117
118} // namespace rtype
A client without window nor sound: a ClientSession whose inputs are RandomInputs, or ScriptedInputs w...
Definition bot.hpp:78
void leave()
Leaves the server: sends Logout; keep running the context a little for it to leave.
std::uint64_t picks() const noexcept
Number of inputs picked: random holds, or steps of the script.
const ClientSession & session() const noexcept
The session with the server: its state, error, traffic and world.
std::chrono::steady_clock Clock
Clock of the session and the inputs.
Definition bot.hpp:81
void start()
Connects to the server and logs in.
void update(Clock::time_point now)
Runs the session's timers and, in game, picks the inputs; a script starts at the first update in game...
Bot(aegis2d::net::NetworkContext &context, const BotSettings &settings)
Creates a bot, not started.
aegis2d::ecs::PlayerInput input() const noexcept
The actions held now.
The client side of a session (RFC section 3).
Definition client_session.hpp:103
Random actions held for random times, like a player mashing the keys.
Definition bot.hpp:24
static constexpr std::chrono::milliseconds shortest_hold
Shortest time an input is held.
Definition bot.hpp:29
static constexpr std::chrono::milliseconds longest_hold
Longest time an input is held.
Definition bot.hpp:31
std::chrono::steady_clock Clock
Clock of the holds.
Definition bot.hpp:27
aegis2d::ecs::PlayerInput update(Clock::time_point now)
The input to hold now: a new one once the last has been held long enough.
std::uint64_t picks() const noexcept
Number of inputs picked.
RandomInputs(std::uint32_t seed)
Creates the generator.
Plays an input script from the first call to update(), looping at its end.
Definition input_script.hpp:52
The client's session with the server: TCP login, then UDP binding (RFC section 3).
Recorded inputs for r-type_bot --inputs: what to hold, and for how long.
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
std::vector< BotSettings > bot_team(const BotSettings &base, std::size_t count)
The settings of count bots that play together, for --count.
What a bot needs to join a server.
Definition bot.hpp:54
std::uint32_t seed
Seed of its random inputs.
Definition bot.hpp:58
ClientSettings server
Server and player name.
Definition bot.hpp:56
std::optional< InputScript > script
Inputs to replay instead of random ones (--inputs).
Definition bot.hpp:60
What the client needs to join a server.
Definition client_session.hpp:70
std::string host
Numeric IPv4 or IPv6 address of the server, or "localhost".
Definition client_session.hpp:72