nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
parts.h
Go to the documentation of this file.
1#ifndef NANOMMO_PROTOCOL_PARTS_H_
2#define NANOMMO_PROTOCOL_PARTS_H_
3
4#include <chrono>
5#include <cstddef>
6#include <cstdint>
7#include <map>
8#include <set>
9#include <string>
10#include <string_view>
11#include <vector>
12
13#include "protocol/nanommo.pb.h"
14
15namespace nanommo {
16
17/// Most networks carry 1500 bytes a packet, less what tunnels and VPNs
18/// take, so IP never has to split a datagram this size.
19constexpr std::size_t kMaxDatagramSize = 1200;
20
21/// NPCs come once a second, so a client keeps each for this long after it last
22/// heard of it, and one lost datagram doesn't make them blink out.
23constexpr std::chrono::seconds kNpcTimeout{3};
24
25/// A player, a monster or an NPC in a world of its own, serialized. Protobuf
26/// reads worlds that follow each other as one, so pieces put one after another
27/// make a world.
28std::string Piece(const protocol::Player& player);
29std::string Piece(const protocol::Monster& monster);
30std::string Piece(const protocol::Npc& npc);
31
32/// The parts of a world sent at a time, each a datagram of up to max_size
33/// bytes, packed from pieces.
34std::vector<std::string> Split(const std::vector<std::string_view>& pieces,
35 int64_t sent,
36 std::size_t max_size = kMaxDatagramSize);
37
38class Parts {
39 public:
40 void Add(const protocol::World& part);
41
42 protocol::World world() const;
43
44 /// The newest world that's come whole, every part of it, by when it was
45 /// sent, or 0: the server leaves out of the next ones what hasn't changed
46 /// since.
47 int64_t whole() const { return whole_; }
48
49 private:
50 struct HeardNpc {
51 protocol::Npc npc;
52 /// When the world it last came in was sent.
53 int64_t sent = 0;
54 };
55
56 uint32_t you_ = 0;
57 uint32_t experience_ = 0;
58 uint32_t next_level_ = 0;
59 uint32_t seed_ = 0;
60 uint32_t land_ = 0;
61 uint32_t respawn_ = 0;
62 uint32_t acked_ = 0;
63 bool steered_ = false;
64 protocol::Sky sky_;
65 protocol::Crafting crafting_;
66 std::map<uint32_t, uint32_t> cooldowns_;
67 std::vector<uint32_t> skills_;
68 int64_t sent_ = 0;
69 int64_t whole_ = 0;
70 std::map<uint32_t, protocol::Player> players_;
71 std::map<uint32_t, protocol::Monster> monsters_;
72 std::map<uint32_t, HeardNpc> npcs_;
73 std::set<uint32_t> arrived_;
74 std::set<uint32_t> seen_;
75 std::set<uint32_t> seen_monsters_;
76};
77
78} // namespace nanommo
79
80#endif // NANOMMO_PROTOCOL_PARTS_H_
void Add(const protocol::World &part)
Definition parts.cc:103
protocol::World world() const
Definition parts.cc:160
int64_t whole() const
Definition parts.h:47
Definition chat.cc:9
constexpr std::size_t kMaxDatagramSize
Definition parts.h:19
std::vector< std::string > Split(const std::vector< std::string_view > &pieces, int64_t sent, std::size_t max_size)
Definition parts.cc:76
std::string Piece(const protocol::Player &player)
Definition parts.cc:58
constexpr std::chrono::seconds kNpcTimeout
Definition parts.h:23