nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
shown.cc
Go to the documentation of this file.
1#include "server/shown.h"
2
3#include <chrono>
4#include <cstdint>
5#include <optional>
6
7#include "server/levels.h"
8
9namespace nanommo {
10
11protocol::Motion ToProtocol(Motion motion) {
12 switch (motion) {
13 case Motion::kWalk:
14 return protocol::MOTION_WALK;
15 case Motion::kRun:
16 return protocol::MOTION_RUN;
17 case Motion::kAttack:
18 return protocol::MOTION_ATTACK;
19 case Motion::kDead:
20 return protocol::MOTION_DEAD;
21 case Motion::kSkill:
22 return protocol::MOTION_SKILL;
23 case Motion::kIdle:
24 break;
25 }
26 return protocol::MOTION_IDLE;
27}
28
29protocol::Player ToProtocol(uint32_t id, const Player& player) {
30 protocol::Player sent;
31 sent.set_id(id);
32 sent.set_x(player.x);
33 sent.set_z(player.z);
34 sent.set_name(player.name);
35 if (player.looks) {
36 *sent.mutable_looks() = *player.looks;
37 }
38 sent.set_faction(player.faction);
39 sent.set_yaw(player.yaw);
40 sent.set_motion(ToProtocol(player.motion));
41 const int level = LevelFor(player.experience).number;
42 sent.set_level(level);
43 sent.set_health(player.health);
44 sent.set_max_health(MaxHealthOf(player));
45 sent.set_weapon(MainHandOf(player));
46 for (const uint32_t item : WornOf(player)) {
47 sent.add_wearing(item);
48 }
49 if (player.motion == Motion::kSkill) {
50 sent.set_skill(player.skill);
51 sent.set_target(player.target);
52 sent.set_target_player(player.target_player);
53 sent.set_casts(player.casts);
54 }
55 return sent;
56}
57
58protocol::Monster ToProtocol(uint32_t id, const Monster& monster) {
59 protocol::Monster sent;
60 sent.set_id(id);
61 sent.set_kind(monster.kind);
62 sent.set_x(monster.x);
63 sent.set_z(monster.z);
64 sent.set_yaw(monster.yaw);
65 sent.set_motion(ToProtocol(monster.motion));
66 sent.set_level(monster.level);
67 sent.set_health(monster.health);
68 sent.set_max_health(monster.max_health);
69 sent.set_height(monster.height);
70 if (monster.boss()) {
71 sent.set_phase(static_cast<uint32_t>(monster.phase) + 1);
72 }
73 if (monster.motion == Motion::kSkill) {
74 sent.set_skill(monster.casting.id());
75 sent.set_lands_in(static_cast<uint32_t>(
76 std::chrono::duration_cast<std::chrono::milliseconds>(
77 std::chrono::milliseconds(monster.casting.telegraph_ms()) -
78 monster.cast)
79 .count()));
80 }
81 return sent;
82}
83
84protocol::Npc ToProtocol(uint32_t id, const Npc& npc) {
85 protocol::Npc sent;
86 sent.set_id(id);
87 sent.set_kind(npc.kind);
88 sent.set_x(npc.x);
89 sent.set_z(npc.z);
90 sent.set_yaw(npc.yaw);
91 return sent;
92}
93
94protocol::World YouIn(uint32_t id, const Player& player, uint32_t seed,
95 int land, std::optional<uint32_t> acked) {
96 const Level level = LevelFor(player.experience);
97 protocol::World you;
98 you.set_you(id);
99 you.set_experience(level.experience);
100 you.set_next_level(level.next);
101 for (const auto& [skill, left] : player.cooldowns) {
102 (*you.mutable_cooldowns())[skill] = static_cast<uint32_t>(
103 std::chrono::duration_cast<std::chrono::milliseconds>(left).count());
104 }
105 you.mutable_skills()->Add(player.skills.begin(), player.skills.end());
106 if (player.motion == Motion::kDead) {
107 you.set_respawn(static_cast<uint32_t>(
108 std::chrono::duration_cast<std::chrono::milliseconds>(player.respawn)
109 .count()));
110 }
111 if (player.crafting != 0) {
112 you.mutable_crafting()->set_recipe(player.crafting);
113 you.mutable_crafting()->set_done(static_cast<uint32_t>(
114 std::chrono::duration_cast<std::chrono::milliseconds>(player.crafted)
115 .count()));
116 }
117 you.set_seed(seed);
118 you.set_land(static_cast<uint32_t>(land));
119 if (acked) {
120 you.set_acked(*acked);
121 }
122 you.set_steered(player.steered);
123 // Where you are, with the last input applied, in every datagram, so the
124 // two always come together, whichever part has you otherwise.
125 protocol::Player* here = you.add_players();
126 *here = ToProtocol(id, player);
127 here->clear_name();
128 here->clear_looks();
129 here->clear_level();
130 here->clear_max_health();
131 here->clear_weapon();
132 here->clear_faction();
133 return you;
134}
135
136protocol::World BagOf(const Player& player) {
137 protocol::World bag;
138 *bag.mutable_inventory() = player.inventory.items();
139 return bag;
140}
141
142protocol::World QuestsOf(const Player& player) {
143 protocol::World quests;
144 protocol::Quests* on = quests.mutable_quests();
145 on->mutable_on()->insert(player.quests.begin(), player.quests.end());
146 on->mutable_done()->Add(player.quests_done.begin(), player.quests_done.end());
147 on->set_revision(player.quests_revision);
148 return quests;
149}
150
151std::optional<protocol::Event> ForPlayers(const Event& event) {
152 protocol::Event shown;
153 shown.set_player(event.player_id);
154 shown.set_amount(event.amount);
155 switch (event.kind) {
157 shown.set_kind(protocol::EVENT_HIT);
158 shown.set_monster(event.monster_id);
159 return shown;
161 shown.set_kind(protocol::EVENT_HURT);
162 return shown;
164 shown.set_kind(protocol::EVENT_HEAL);
165 return shown;
167 shown.set_kind(protocol::EVENT_LEVEL);
168 shown.set_amount(event.level);
169 return shown;
178 return std::nullopt;
179 }
180 return std::nullopt;
181}
182
183} // namespace nanommo
const protocol::Inventory & items() const
Definition inventory.h:35
Definition chat.cc:9
std::optional< protocol::Event > ForPlayers(const Event &event)
Definition shown.cc:151
Motion
Definition world.h:36
protocol::World YouIn(uint32_t id, const Player &player, uint32_t seed, int land, std::optional< uint32_t > acked)
Definition shown.cc:94
Level LevelFor(uint32_t experience)
Definition levels.cc:5
protocol::World BagOf(const Player &player)
Their bag, and their quests, each as it goes when it's changed.
Definition shown.cc:136
protocol::World QuestsOf(const Player &player)
Definition shown.cc:142
protocol::Motion ToProtocol(Motion motion)
Definition shown.cc:11
std::vector< uint32_t > WornOf(const Player &player)
Definition world.cc:194
int MaxHealthOf(const Player &player)
Definition world.cc:148
uint32_t MainHandOf(const Player &player)
Definition world.cc:189
uint32_t monster_id
The monster the player hit, by its id in the world.
Definition world.h:293
uint32_t level
The level a player's reached.
Definition world.h:299
uint32_t amount
The health a hit took, or that a potion or a skill gave back.
Definition world.h:301
uint32_t player_id
The player's id in the world, which the clients know them by.
Definition world.h:283
uint32_t experience
Definition levels.h:10
uint32_t next
Definition levels.h:11
std::chrono::nanoseconds cast
Definition world.h:201
std::size_t phase
Definition world.h:178
Motion motion
Definition world.h:158
bool boss() const
A monster with phases is a boss.
Definition world.h:229
uint32_t kind
By its id in the monsters file.
Definition world.h:154
monsters::Skill casting
Definition world.h:200
float yaw
Definition world.h:240
uint32_t kind
By its id in the NPCs file.
Definition world.h:237
float z
Definition world.h:239
float x
Definition world.h:238
uint32_t experience
Definition world.h:64
std::chrono::nanoseconds respawn
While they're dead, how long until they can come back.
Definition world.h:116
std::string name
Definition world.h:49
std::set< uint32_t > skills
The skills the player knows, by their ids in the skills file.
Definition world.h:72
std::chrono::nanoseconds crafted
Definition world.h:132
uint32_t target_player
Definition world.h:87
uint32_t target
Definition world.h:83
uint32_t casts
Definition world.h:105
std::set< uint32_t > quests_done
The quests they've turned in, which they can't take again.
Definition world.h:77
uint32_t crafting
Definition world.h:131
std::optional< protocol::Looks > looks
Definition world.h:52
float yaw
In radians, around the vertical. Zero faces along z, towards the camera.
Definition world.h:62
uint32_t quests_revision
Definition world.h:80
uint32_t skill
Definition world.h:93
std::map< uint32_t, uint32_t > quests
Definition world.h:75
Motion motion
Definition world.h:63
std::map< uint32_t, std::chrono::nanoseconds > cooldowns
How long until each skill they've used is ready again, by its id.
Definition world.h:111
uint32_t faction
Definition world.h:58
Inventory inventory
What the player carries and wears.
Definition world.h:69