nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
duels.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_DUELS_H_
2#define NANOMMO_SERVER_DUELS_H_
3
4#include <chrono>
5#include <cstdint>
6#include <map>
7#include <set>
8#include <vector>
9
10#include "terrain/lands.h"
11
12namespace nanommo {
13
14/// The duels between the players in one copy of a land, which its World
15/// keeps, as it does their trades, so that both players are always in it. A
16/// player asks another to duel, and once the other says yes, a countdown
17/// starts, and then the duel. A player is in one duel at a time, from the ask
18/// to the end.
19class Duels {
20 public:
21 /// A player asks another within this many metres of them.
22 static constexpr float kReach = 10.0f;
23
24 /// An ask waits this long for a yes.
25 static constexpr std::chrono::seconds kAskTime{30};
26
27 /// From the yes to the duel, so that both sides are ready for it.
28 static constexpr std::chrono::seconds kCountdown{3};
29
30 /// The flag goes down where the one who asked stands as the other says
31 /// yes, and a duellist who goes further from it than this loses.
32 static constexpr float kFlagReach = 25.0f;
33
34 enum class State { kAsked, kCountdown, kFighting };
35
36 struct Duel {
37 /// Who asked, and who they asked, by their ids.
38 uint32_t challenger = 0;
39 uint32_t challenged = 0;
41 /// How long the ask has left, and then the countdown.
42 std::chrono::nanoseconds left = kAskTime;
44
45 /// The other side from the player's, by their id.
46 uint32_t Against(uint32_t id) const {
47 return id == challenger ? challenged : challenger;
48 }
49 };
50
51 /// The player asks the other to duel. Neither may be in a duel already.
52 void Ask(uint32_t id, uint32_t other);
53
54 /// The player says yes to whoever asked them, which puts the flag down and
55 /// starts the countdown. Returns who asked, or 0 if nobody did.
56 uint32_t Accept(uint32_t id, Place flag);
57
58 /// The player's duel, from the ask to the end, or nullptr.
59 const Duel* Of(uint32_t id) const;
60
61 /// Who the player's duelling, by their id, once the countdown's over, or 0.
62 uint32_t Opponent(uint32_t id) const;
63
64 /// Ends the player's duel, however far it's got. Returns who it was with,
65 /// or 0.
66 uint32_t End(uint32_t id);
67
68 /// Runs the asks and the countdowns down, and starts the duels whose
69 /// countdowns are over. Returns the asks that ran out, which are over.
70 std::vector<Duel> Step(std::chrono::nanoseconds time);
71
72 /// Each duel, by its challenger's id.
73 const std::map<uint32_t, Duel>& all() const { return duels_; }
74
75 /// The players whose duel has been asked for, counted down, started or
76 /// ended since the last time, which it hands over.
77 std::set<uint32_t> TakeNews();
78
79 private:
80 std::map<uint32_t, Duel> duels_;
81 /// The challenger of each player's duel, by the ids of both sides.
82 std::map<uint32_t, uint32_t> challengers_;
83 std::set<uint32_t> news_;
84};
85
86} // namespace nanommo
87
88#endif // NANOMMO_SERVER_DUELS_H_
static constexpr float kFlagReach
Definition duels.h:32
uint32_t Accept(uint32_t id, Place flag)
Definition duels.cc:18
const Duel * Of(uint32_t id) const
The player's duel, from the ask to the end, or nullptr.
Definition duels.cc:34
uint32_t End(uint32_t id)
Definition duels.cc:45
void Ask(uint32_t id, uint32_t other)
The player asks the other to duel. Neither may be in a duel already.
Definition duels.cc:11
const std::map< uint32_t, Duel > & all() const
Each duel, by its challenger's id.
Definition duels.h:73
static constexpr float kReach
A player asks another within this many metres of them.
Definition duels.h:22
uint32_t Opponent(uint32_t id) const
Who the player's duelling, by their id, once the countdown's over, or 0.
Definition duels.cc:39
std::vector< Duel > Step(std::chrono::nanoseconds time)
Definition duels.cc:59
static constexpr std::chrono::seconds kAskTime
An ask waits this long for a yes.
Definition duels.h:25
std::set< uint32_t > TakeNews()
Definition duels.cc:90
static constexpr std::chrono::seconds kCountdown
From the yes to the duel, so that both sides are ready for it.
Definition duels.h:28
Definition chat.cc:9
std::chrono::nanoseconds left
How long the ask has left, and then the countdown.
Definition duels.h:42
uint32_t challenger
Who asked, and who they asked, by their ids.
Definition duels.h:38
uint32_t challenged
Definition duels.h:39
uint32_t Against(uint32_t id) const
The other side from the player's, by their id.
Definition duels.h:46
In metres from the middle of a land.
Definition lands.h:13