13#include "absl/status/status.h"
28constexpr std::chrono::milliseconds kTick{50};
31constexpr int kMostTicksAFrame = 5;
34constexpr int kFullEvery = 20;
36constexpr std::size_t kChatLines = 50;
41 : content_(std::make_shared<const
Content>(content)),
44 next_tick_(std::chrono::steady_clock::now()) {
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";
52 Tr(
"You're playing offline: the world is yours alone, and nothing is "
57 const auto now = std::chrono::steady_clock::now();
58 for (
int ticks = 0; now >= next_tick_ && ticks < kMostTicksAFrame; ++ticks) {
63 if (now >= next_tick_) {
64 next_tick_ = now + kTick;
69 inputs_.Take(world_, kYou, input);
70 if (input.respawn()) {
73 if (input.has_walk_to()) {
74 world_.WalkTo(kYou, {input.walk_to().x(), input.walk_to().z()});
76 if (input.skill() != 0) {
77 world_.UseSkill(kYou, input.skill());
80 if (input.has_learn()) {
81 done.Update(world_.Learn(kYou, input.learn().npc(), input.learn().skill()));
83 if (input.has_take()) {
85 world_.TakeQuest(kYou, input.take().npc(), input.take().quest()));
87 if (input.has_turn_in()) {
89 world_.TurnIn(kYou, input.turn_in().npc(), input.turn_in().quest()));
91 if (input.enlist() != 0) {
92 done.Update(world_.Enlist(kYou, input.enlist()));
94 if (input.has_item()) {
95 done.Update(
DoItem(world_, kYou, input.item()));
97 if (input.has_craft()) {
98 done.Update(
DoCraft(world_, kYou, input.craft()));
103 if (!input.say().empty()) {
104 Say(world_.players().at(kYou).name, input.say());
106 if (input.has_talk()) {
108 Tr(
"The stories come from a server's storyteller, and there's none "
111 if (input.has_guild()) {
112 Notify(
Tr(
"Guilds are kept by the servers, so there are none offline."));
114 if (input.has_trade()) {
116 Tr(
"A trade takes another player, and there's nobody else offline."));
118 if (input.has_duel()) {
119 Notify(
Tr(
"A duel takes another player, and there's nobody else offline."));
124 return std::exchange(events_, {});
131void LocalConnection::Tick() {
132 inputs_.
Apply([
this](uint32_t) {
return &world_; });
134 for (
const Notice& notice : world_.
TakeNotices()) {
135 Notify(notice.message, notice.channel);
137 const Player& you = world_.players().at(kYou);
139 for (
const Event& event : world_.TakeEvents()) {
140 const std::optional<protocol::Event> seen =
ForPlayers(event);
142 events_.push_back(*seen);
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);
149 if (in_gate && !in_gate_) {
151 Tr(
"The gates lead to the servers' other lands, and there are none "
156 inventory_ =
BagOf(you).inventory();
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()) {
169 for (
const uint32_t
id : view.players) {
172 for (
const uint32_t
id : view.monsters) {
176 for (
const uint32_t
id : view.npcs) {
183 world.MergeFrom(
YouIn(kYou, world_.players().at(kYou),
184 kSeed +
kLands[kLand].seed, kLand,
185 inputs_.acked(kYou)));
189 shown_ = parts_.world();
192void LocalConnection::Notify(
const std::string& text,
193 protocol::Channel channel) {
194 Say(
"", text, channel);
197void LocalConnection::Notify(
const protocol::Message& message,
198 protocol::Channel channel) {
200 *line.mutable_message() = message;
201 line.set_channel(channel);
202 Add(std::move(line));
205void LocalConnection::Say(
const std::string& name,
const std::string& text,
206 protocol::Channel channel) {
210 line.set_channel(channel);
211 Add(std::move(line));
214void LocalConnection::Add(protocol::Chat line) {
215 chat_.push_back(std::move(line));
216 while (chat_.size() > kChatLines) {
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)
std::vector< Notice > TakeNotices()
The lines for players since the last time, which it hands over.
std::optional< protocol::Event > ForPlayers(const Event &event)
absl::Status DoCraft(World &land, uint32_t id, const protocol::CraftAction &action)
constexpr std::array< Gate, 4 > kGates
int64_t UnixMs()
Now, by the wall clock, in milliseconds since 1970.
protocol::World YouIn(uint32_t id, const Player &player, uint32_t seed, int land, std::optional< uint32_t > acked)
Sentence SentenceOf(const absl::Status &status)
void SpawnMonsters(World &land, int index, const MonsterTable &table)
const char * Tr(const char *text)
Cell CellOf(float x, float z)
bool InGate(const Gate &gate, float x, float z)
constexpr std::array< Land, 3 > kLands
protocol::Sky SkyAt(int land, int64_t unix_ms)
bool Near(Cell from, Cell to)
absl::Status DoItem(World &land, uint32_t id, const protocol::ItemAction &action)
protocol::World BagOf(const Player &player)
Their bag, and their quests, each as it goes when it's changed.
Cells CellsOf(const std::map< uint32_t, Unit > &units)
protocol::World QuestsOf(const Player &player)
protocol::Motion ToProtocol(Motion motion)
std::map< Cell, View > Views(const std::map< uint32_t, Player > &players, const std::map< uint32_t, Monster > &monsters, const Cells &npcs)
void SpawnNpcs(World &land, int index, const NpcTable &table)