nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
parts.cc
Go to the documentation of this file.
1#include "protocol/parts.h"
2
3#include <algorithm>
4#include <chrono>
5#include <string>
6#include <string_view>
7#include <type_traits>
8#include <utility>
9#include <vector>
10
11namespace nanommo {
12
13namespace {
14
15// Room for the fields besides the players and the monsters: sent, part,
16// parts, and the client's own you, experience, next level, seed, land,
17// respawn, acked and steered, whose field numbers take two bytes, its
18// cooldowns, up to ten bytes each for the nine skills a hotbar holds, the
19// skills it knows, a byte each, after two for the field, its own player,
20// without its name, up to 60 bytes, the sky, up to 25, and what it's making,
21// up to 15.
22constexpr std::size_t kRoom = 65 + 9 * 10 + 2 + 9 + 60 + 25 + 15;
23
24// Names, kinds, levels, weapons and looks come once a second, and protobuf
25// leaves out a field that's empty or zero, which no name, level or kind is,
26// so we keep the last ones we had. A player can hold no weapon, and have no
27// looks, from before there were any, so those go by the name: a player with
28// their name has their weapon, guild, looks and faction too, none included.
29template <typename Unit>
30void Update(Unit& known, const Unit& now) {
31 Unit merged = now;
32 if (merged.level() == 0) {
33 merged.set_level(known.level());
34 }
35 if (merged.max_health() == 0) {
36 merged.set_max_health(known.max_health());
37 }
38 if constexpr (std::is_same_v<Unit, protocol::Player>) {
39 if (merged.name().empty()) {
40 merged.set_name(known.name());
41 merged.set_weapon(known.weapon());
42 merged.set_guild(known.guild());
43 merged.set_faction(known.faction());
44 if (known.has_looks()) {
45 *merged.mutable_looks() = known.looks();
46 }
47 }
48 } else {
49 if (merged.kind() == 0) {
50 merged.set_kind(known.kind());
51 }
52 }
53 known = std::move(merged);
54}
55
56} // namespace
57
58std::string Piece(const protocol::Player& player) {
59 protocol::World world;
60 *world.add_players() = player;
61 return world.SerializeAsString();
62}
63
64std::string Piece(const protocol::Monster& monster) {
65 protocol::World world;
66 *world.add_monsters() = monster;
67 return world.SerializeAsString();
68}
69
70std::string Piece(const protocol::Npc& npc) {
71 protocol::World world;
72 *world.add_npcs() = npc;
73 return world.SerializeAsString();
74}
75
76std::vector<std::string> Split(const std::vector<std::string_view>& pieces,
77 int64_t sent, std::size_t max_size) {
78 std::vector<std::vector<std::string_view>> groups(1);
79 std::size_t size = 0;
80 for (const std::string_view piece : pieces) {
81 if (!groups.back().empty() && size + piece.size() + kRoom > max_size) {
82 groups.emplace_back();
83 size = 0;
84 }
85 groups.back().push_back(piece);
86 size += piece.size();
87 }
88 std::vector<std::string> parts;
89 for (std::size_t i = 0; i < groups.size(); ++i) {
90 protocol::World header;
91 header.set_sent(sent);
92 header.set_part(static_cast<uint32_t>(i));
93 header.set_parts(static_cast<uint32_t>(groups.size()));
94 std::string part = header.SerializeAsString();
95 for (const std::string_view piece : groups[i]) {
96 part.append(piece);
97 }
98 parts.push_back(std::move(part));
99 }
100 return parts;
101}
102
103void Parts::Add(const protocol::World& part) {
104 if (part.sent() < sent_) {
105 return;
106 }
107 if (part.sent() > sent_) {
108 sent_ = part.sent();
109 arrived_.clear();
110 seen_.clear();
111 seen_monsters_.clear();
112 }
113 // Each land has NPCs of its own, which may have the last one's ids.
114 if (part.land() != land_) {
115 npcs_.clear();
116 }
117 you_ = part.you();
118 experience_ = part.experience();
119 next_level_ = part.next_level();
120 seed_ = part.seed();
121 land_ = part.land();
122 respawn_ = part.respawn();
123 acked_ = part.acked();
124 steered_ = part.steered();
125 sky_ = part.sky();
126 crafting_ = part.crafting();
127 cooldowns_ = {part.cooldowns().begin(), part.cooldowns().end()};
128 skills_ = {part.skills().begin(), part.skills().end()};
129 arrived_.insert(part.part());
130 for (const protocol::Player& player : part.players()) {
131 Update(players_[player.id()], player);
132 seen_.insert(player.id());
133 }
134 for (const protocol::Monster& monster : part.monsters()) {
135 Update(monsters_[monster.id()], monster);
136 seen_monsters_.insert(monster.id());
137 }
138 // Those that haven't changed since the world we have whole are as we have
139 // them, and still here.
140 seen_.insert(part.still_players().begin(), part.still_players().end());
141 seen_monsters_.insert(part.still_monsters().begin(),
142 part.still_monsters().end());
143 for (const protocol::Npc& npc : part.npcs()) {
144 npcs_[npc.id()] = {.npc = npc, .sent = part.sent()};
145 }
146 std::erase_if(npcs_, [this](const auto& npc) {
147 return std::chrono::microseconds(sent_ - npc.second.sent) > kNpcTimeout;
148 });
149 if (arrived_.size() == std::max(part.parts(), 1u)) {
150 whole_ = sent_;
151 std::erase_if(players_, [this](const auto& player) {
152 return !seen_.contains(player.first);
153 });
154 std::erase_if(monsters_, [this](const auto& monster) {
155 return !seen_monsters_.contains(monster.first);
156 });
157 }
158}
159
160protocol::World Parts::world() const {
161 protocol::World world;
162 world.set_you(you_);
163 world.set_experience(experience_);
164 world.set_next_level(next_level_);
165 world.set_seed(seed_);
166 world.set_land(land_);
167 world.set_respawn(respawn_);
168 world.set_acked(acked_);
169 world.set_steered(steered_);
170 *world.mutable_sky() = sky_;
171 *world.mutable_crafting() = crafting_;
172 world.mutable_cooldowns()->insert(cooldowns_.begin(), cooldowns_.end());
173 world.mutable_skills()->Add(skills_.begin(), skills_.end());
174 world.set_sent(sent_);
175 for (const auto& [id, player] : players_) {
176 *world.add_players() = player;
177 }
178 for (const auto& [id, monster] : monsters_) {
179 *world.add_monsters() = monster;
180 }
181 for (const auto& [id, heard] : npcs_) {
182 *world.add_npcs() = heard.npc;
183 }
184 return world;
185}
186
187} // namespace nanommo
void Add(const protocol::World &part)
Definition parts.cc:103
protocol::World world() const
Definition parts.cc:160
Definition chat.cc:9
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