nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
local_connection.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <chrono>
5#include <cstdint>
6#include <iostream>
7#include <memory>
8#include <optional>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "client/translations.h"
15#include "protocol/clock.h"
16#include "protocol/messages.h"
17#include "server/monsters.h"
18#include "server/npcs.h"
19#include "server/shown.h"
20#include "server/weather.h"
21#include "terrain/lands.h"
22#include "terrain/terrain.h"
23
24namespace nanommo {
25namespace {
26
27// The server's tick, 20 a second.
28constexpr std::chrono::milliseconds kTick{50};
29
30// After a long frame, the world catches up a few ticks at once, and no more.
31constexpr int kMostTicksAFrame = 5;
32
33// The NPCs, who don't move, go once a second, as from a server.
34constexpr int kFullEvery = 20;
35
36constexpr std::size_t kChatLines = 50;
37
38} // namespace
39
40LocalConnection::LocalConnection(const Content& content, std::string name)
41 : content_(std::make_shared<const Content>(content)),
42 world_(kSeed, Terrain(kSeed + kLands[kLand].seed, kLands[kLand].wading),
43 content_),
44 next_tick_(std::chrono::steady_clock::now()) {
45 SpawnMonsters(world_, kLand, content_->monsters);
46 SpawnNpcs(world_, kLand, content_->npcs);
47 npc_cells_ = CellsOf(world_.npcs());
48 world_.Join(kYou, Player{.name = std::move(name)});
49 std::cout << "nanommo client: offline, with " << world_.monsters().size()
50 << " monsters and " << world_.npcs().size() << " NPCs\n";
51 Notify(
52 Tr("You're playing offline: the world is yours alone, and nothing is "
53 "saved."));
54}
55
57 const auto now = std::chrono::steady_clock::now();
58 for (int ticks = 0; now >= next_tick_ && ticks < kMostTicksAFrame; ++ticks) {
59 Tick();
60 next_tick_ += kTick;
61 }
62 // Too far behind, as after the page was hidden: on from now.
63 if (now >= next_tick_) {
64 next_tick_ = now + kTick;
65 }
66}
67
68void LocalConnection::Send(const protocol::Input& input) {
69 inputs_.Take(world_, kYou, input);
70 if (input.respawn()) {
71 world_.Respawn(kYou);
72 }
73 if (input.has_walk_to()) {
74 world_.WalkTo(kYou, {input.walk_to().x(), input.walk_to().z()});
75 }
76 if (input.skill() != 0) {
77 world_.UseSkill(kYou, input.skill());
78 }
79 absl::Status done;
80 if (input.has_learn()) {
81 done.Update(world_.Learn(kYou, input.learn().npc(), input.learn().skill()));
82 }
83 if (input.has_take()) {
84 done.Update(
85 world_.TakeQuest(kYou, input.take().npc(), input.take().quest()));
86 }
87 if (input.has_turn_in()) {
88 done.Update(
89 world_.TurnIn(kYou, input.turn_in().npc(), input.turn_in().quest()));
90 }
91 if (input.enlist() != 0) {
92 done.Update(world_.Enlist(kYou, input.enlist()));
93 }
94 if (input.has_item()) {
95 done.Update(DoItem(world_, kYou, input.item()));
96 }
97 if (input.has_craft()) {
98 done.Update(DoCraft(world_, kYou, input.craft()));
99 }
100 if (!done.ok()) {
101 Notify(SentenceOf(done).message);
102 }
103 if (!input.say().empty()) {
104 Say(world_.players().at(kYou).name, input.say());
105 }
106 if (input.has_talk()) {
107 Notify(
108 Tr("The stories come from a server's storyteller, and there's none "
109 "offline."));
110 }
111 if (input.has_guild()) {
112 Notify(Tr("Guilds are kept by the servers, so there are none offline."));
113 }
114 if (input.has_trade()) {
115 Notify(
116 Tr("A trade takes another player, and there's nobody else offline."));
117 }
118 if (input.has_duel()) {
119 Notify(Tr("A duel takes another player, and there's nobody else offline."));
120 }
121}
122
123std::vector<protocol::Event> LocalConnection::TakeEvents() {
124 return std::exchange(events_, {});
125}
126
127std::optional<protocol::GuildInvite> LocalConnection::TakeInvite() {
128 return std::nullopt;
129}
130
131void LocalConnection::Tick() {
132 inputs_.Apply([this](uint32_t) { return &world_; });
133 world_.Step(kTick);
134 for (const Notice& notice : world_.TakeNotices()) {
135 Notify(notice.message, notice.channel);
136 }
137 const Player& you = world_.players().at(kYou);
138 // What happens near us, as a world server shows it.
139 for (const Event& event : world_.TakeEvents()) {
140 const std::optional<protocol::Event> seen = ForPlayers(event);
141 if (seen && Near(CellOf(you.x, you.z), CellOf(event.x, event.z))) {
142 events_.push_back(*seen);
143 }
144 }
145 // A gate takes a player to another land's world, which only a server has.
146 const bool in_gate = std::ranges::any_of(kGates, [&you](const Gate& gate) {
147 return gate.land == kLand && InGate(gate, you.x, you.z);
148 });
149 if (in_gate && !in_gate_) {
150 Notify(
151 Tr("The gates lead to the servers' other lands, and there are none "
152 "offline."));
153 }
154 in_gate_ = in_gate;
155 Show();
156 inventory_ = BagOf(you).inventory();
157 quests_ = QuestsOf(you).quests();
158}
159
160void LocalConnection::Show() {
161 const bool full = ticks_ == 0;
162 ticks_ = (ticks_ + 1) % kFullEvery;
163 protocol::World world;
164 for (const auto& [cell, view] :
165 Views(world_.players(), world_.monsters(), npc_cells_)) {
166 if (std::ranges::find(view.here, kYou) == view.here.end()) {
167 continue;
168 }
169 for (const uint32_t id : view.players) {
170 *world.add_players() = ToProtocol(id, world_.players().at(id));
171 }
172 for (const uint32_t id : view.monsters) {
173 *world.add_monsters() = ToProtocol(id, world_.monsters().at(id));
174 }
175 if (full) {
176 for (const uint32_t id : view.npcs) {
177 *world.add_npcs() = ToProtocol(id, world_.npcs().at(id));
178 }
179 }
180 }
181 world.set_sent(NowMicros());
182 // Our own part goes last, as in a server's datagram.
183 world.MergeFrom(YouIn(kYou, world_.players().at(kYou),
184 kSeed + kLands[kLand].seed, kLand,
185 inputs_.acked(kYou)));
186 // The sky the servers would give the land now.
187 *world.mutable_sky() = SkyAt(kLand, UnixMs());
188 parts_.Add(world);
189 shown_ = parts_.world();
190}
191
192void LocalConnection::Notify(const std::string& text,
193 protocol::Channel channel) {
194 Say("", text, channel);
195}
196
197void LocalConnection::Notify(const protocol::Message& message,
198 protocol::Channel channel) {
199 protocol::Chat line;
200 *line.mutable_message() = message;
201 line.set_channel(channel);
202 Add(std::move(line));
203}
204
205void LocalConnection::Say(const std::string& name, const std::string& text,
206 protocol::Channel channel) {
207 protocol::Chat line;
208 line.set_name(name);
209 line.set_text(text);
210 line.set_channel(channel);
211 Add(std::move(line));
212}
213
214void LocalConnection::Add(protocol::Chat line) {
215 chat_.push_back(std::move(line));
216 while (chat_.size() > kChatLines) {
217 chat_.pop_front();
218 }
219}
220
221} // namespace nanommo
void Apply(const std::function< World *(uint32_t)> &land_of)
Each player's next waiting keys, a tick's worth, in their land.
Definition inputs.cc:44
std::optional< protocol::GuildInvite > TakeInvite() override
The guild that asked us to join since the last time, once.
void Send(const protocol::Input &input) override
std::vector< protocol::Event > TakeEvents() override
const protocol::World & world() const override
void Poll() override
Takes in what's arrived, without waiting.
LocalConnection(const Content &content, std::string name)
void Step(std::chrono::nanoseconds time)
Definition world.cc:940
std::vector< Notice > TakeNotices()
The lines for players since the last time, which it hands over.
Definition world.cc:1691
Definition chat.cc:9
std::optional< protocol::Event > ForPlayers(const Event &event)
Definition shown.cc:151
absl::Status DoCraft(World &land, uint32_t id, const protocol::CraftAction &action)
Definition inputs.cc:101
constexpr std::array< Gate, 4 > kGates
Definition lands.h:45
int64_t UnixMs()
Now, by the wall clock, in milliseconds since 1970.
Definition weather.cc:130
protocol::World YouIn(uint32_t id, const Player &player, uint32_t seed, int land, std::optional< uint32_t > acked)
Definition shown.cc:94
Sentence SentenceOf(const absl::Status &status)
Definition messages.cc:224
void SpawnMonsters(World &land, int index, const MonsterTable &table)
Definition monsters.cc:108
const char * Tr(const char *text)
Cell CellOf(float x, float z)
Definition interest.cc:20
bool InGate(const Gate &gate, float x, float z)
Definition lands.cc:28
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
protocol::Sky SkyAt(int land, int64_t unix_ms)
Definition weather.cc:87
bool Near(Cell from, Cell to)
Definition interest.cc:25
absl::Status DoItem(World &land, uint32_t id, const protocol::ItemAction &action)
Definition inputs.cc:85
protocol::World BagOf(const Player &player)
Their bag, and their quests, each as it goes when it's changed.
Definition shown.cc:136
Cells CellsOf(const std::map< uint32_t, Unit > &units)
Definition interest.h:36
protocol::World QuestsOf(const Player &player)
Definition shown.cc:142
protocol::Motion ToProtocol(Motion motion)
Definition shown.cc:11
int64_t NowMicros()
Definition clock.h:11
std::map< Cell, View > Views(const std::map< uint32_t, Player > &players, const std::map< uint32_t, Monster > &monsters, const Cells &npcs)
Definition interest.cc:30
void SpawnNpcs(World &land, int index, const NpcTable &table)
Definition npcs.cc:9