nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
copies.cc
Go to the documentation of this file.
1#include "server/copies.h"
2
3#include <algorithm>
4#include <chrono>
5#include <cstdint>
6#include <iostream>
7#include <iterator>
8#include <map>
9#include <random>
10#include <set>
11#include <utility>
12#include <vector>
13
14#include "server/monsters.h"
15#include "server/npcs.h"
16#include "terrain/lands.h"
17
18namespace nanommo {
19
21 : content_(std::make_shared<const Content>(std::move(content))) {
22 for (const Land& land : kLands) {
23 terrains_.emplace_back(seed + land.seed, land.wading);
24 paths_.push_back(std::make_shared<Paths>(terrains_.back()));
25 }
26 for (int land = 0; land < std::ssize(kLands); ++land) {
27 if (!kLands[land].dungeon) {
28 For(land, 0);
29 }
30 }
31}
32
33void Copies::Join(uint32_t id, Player player) {
34 const uint32_t copy = For(player.land, id);
35 copies_.at(copy).world.Join(id, std::move(player));
36 where_[id] = copy;
37}
38
39void Copies::Leave(uint32_t id) {
40 const auto where = where_.find(id);
41 if (where == where_.end()) {
42 return;
43 }
44 copies_.at(where->second).world.Leave(id);
45 where_.erase(where);
46}
47
48Copy* Copies::Of(uint32_t id) {
49 const auto where = where_.find(id);
50 return where != where_.end() ? &copies_.at(where->second) : nullptr;
51}
52
53std::vector<Notice> Copies::TakeNotices() {
54 std::vector<Notice> notices;
55 for (auto& [id, copy] : copies_) {
56 std::ranges::move(copy.world.TakeNotices(), std::back_inserter(notices));
57 }
58 return notices;
59}
60
61std::vector<Event> Copies::TakeEvents() {
62 std::vector<Event> events;
63 for (auto& [id, copy] : copies_) {
64 for (Event& event : copy.world.TakeEvents()) {
65 event.copy = id;
66 events.push_back(std::move(event));
67 }
68 }
69 return events;
70}
71
72std::set<uint32_t> Copies::TakeTradeNews() {
73 std::set<uint32_t> news;
74 for (auto& [id, copy] : copies_) {
75 news.merge(copy.world.TakeTradeNews());
76 }
77 return news;
78}
79
80std::set<uint32_t> Copies::TakeDuelNews() {
81 std::set<uint32_t> news;
82 for (auto& [id, copy] : copies_) {
83 news.merge(copy.world.TakeDuelNews());
84 }
85 return news;
86}
87
88void Copies::SetWars(const std::vector<War>& wars) {
89 wars_ = wars;
90 for (auto& [id, copy] : copies_) {
91 copy.world.SetWars(wars_);
92 }
93}
94
95std::vector<WarKill> Copies::TakeWarKills() {
96 std::vector<WarKill> kills;
97 for (auto& [id, copy] : copies_) {
98 std::ranges::move(copy.world.TakeWarKills(), std::back_inserter(kills));
99 }
100 return kills;
101}
102
103int Copies::players() const {
104 int count = 0;
105 for (const auto& [id, copy] : copies_) {
106 count += static_cast<int>(copy.world.players().size());
107 }
108 return count;
109}
110
111void Copies::Step(std::chrono::nanoseconds time) {
112 for (auto it = copies_.begin(); it != copies_.end();) {
113 Copy& copy = it->second;
114 if (!kLands[copy.land].dungeon || !copy.world.players().empty()) {
115 copy.empty = std::chrono::nanoseconds::zero();
116 copy.world.Step(time);
117 ++it;
118 continue;
119 }
120 copy.empty += time;
121 if (copy.empty < kEmptyLife) {
122 ++it;
123 continue;
124 }
125 std::cout << "dropped player " << copy.owner << "'s copy of the "
126 << kLands[copy.land].name << std::endl;
127 it = copies_.erase(it);
128 }
129 Travel();
130}
131
132uint32_t Copies::For(int land, uint32_t id) {
133 const uint32_t owner = kLands[land].dungeon ? id : 0;
134 for (const auto& [copy_id, copy] : copies_) {
135 if (copy.land == land && copy.owner == owner) {
136 return copy_id;
137 }
138 }
139 const uint32_t made = next_++;
140 Copy& copy = copies_
141 .emplace(made, Copy{.land = land,
142 .world = World(std::random_device()(),
143 terrains_[land], content_,
144 paths_[land]),
145 .owner = owner})
146 .first->second;
147 copy.world.SetWars(wars_);
148 SpawnMonsters(copy.world, land, content_->monsters);
149 SpawnNpcs(copy.world, land, content_->npcs);
150 copy.npc_cells = CellsOf(copy.world.npcs());
151 if (owner != 0) {
152 std::cout << "made player " << owner << " a copy of the "
153 << kLands[land].name << std::endl;
154 }
155 return made;
156}
157
158void Copies::Travel() {
159 // A new copy goes into the map as we go through it, which std::map allows,
160 // and the players who arrive in it are out of its gate.
161 for (auto& [from, copy] : copies_) {
162 // Leaving changes the map we go through, so first we note who's going,
163 // and through which gate.
164 std::vector<std::pair<uint32_t, const Gate*>> travellers;
165 for (const auto& [id, player] : copy.world.players()) {
166 for (const Gate& gate : kGates) {
167 if (gate.land == copy.land && InGate(gate, player.x, player.z)) {
168 travellers.emplace_back(id, &gate);
169 break;
170 }
171 }
172 }
173 for (const auto& [id, gate] : travellers) {
174 Player player = copy.world.players().at(id);
175 copy.world.Leave(id);
176 const Place arrival = Arrival(*gate, player.yaw);
177 player.x = arrival.x;
178 player.z = arrival.z;
179 player.land = gate->to;
180 std::cout << player.name << " went to the " << kLands[gate->to].name
181 << std::endl;
182 const uint32_t to = For(gate->to, id);
183 copies_.at(to).world.Join(id, std::move(player));
184 where_[id] = to;
185 }
186 }
187}
188
189} // namespace nanommo
void SetWars(const std::vector< War > &wars)
Definition copies.cc:88
int players() const
In all the copies.
Definition copies.cc:103
Copies(uint32_t seed, Content content)
The world's seed makes each land's terrain, and the content fills them.
Definition copies.cc:20
Copy * Of(uint32_t id)
The copy the player's in, or nullptr.
Definition copies.cc:48
std::set< uint32_t > TakeTradeNews()
Definition copies.cc:72
std::vector< Event > TakeEvents()
Definition copies.cc:61
static constexpr std::chrono::minutes kEmptyLife
Definition copies.h:39
void Join(uint32_t id, Player player)
Into their land's copy: the open land's, or for a dungeon, their own.
Definition copies.cc:33
std::set< uint32_t > TakeDuelNews()
Definition copies.cc:80
const Content & content() const
What all of them are made of, which doesn't change as the game runs.
Definition copies.h:61
std::vector< Notice > TakeNotices()
The lines each copy has for its players since the last time.
Definition copies.cc:53
std::vector< WarKill > TakeWarKills()
Definition copies.cc:95
void Leave(uint32_t id)
Definition copies.cc:39
void Step(std::chrono::nanoseconds time)
Definition copies.cc:111
const std::map< uint32_t, Player > & players() const
Definition world.h:513
void Step(std::chrono::nanoseconds time)
Definition world.cc:940
Vector3 Place(const Point &point, const Terrain &terrain)
Definition film.cc:212
Definition chat.cc:9
constexpr std::array< Gate, 4 > kGates
Definition lands.h:45
void SpawnMonsters(World &land, int index, const MonsterTable &table)
Definition monsters.cc:108
Place Arrival(const Gate &gate, float yaw)
Definition lands.cc:43
bool InGate(const Gate &gate, float x, float z)
Definition lands.cc:28
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
Cells CellsOf(const std::map< uint32_t, Unit > &units)
Definition interest.h:36
void SpawnNpcs(World &land, int index, const NpcTable &table)
Definition npcs.cc:9
A land, running: a World of it, with its NPCs in their cells.
Definition copies.h:20
World world
Definition copies.h:23
std::chrono::nanoseconds empty
How long it's had nobody in it.
Definition copies.h:29
int land
By its place in kLands.
Definition copies.h:22
uint32_t owner
Definition copies.h:27
int land
Which of the world's lands, by its place in kLands.
Definition world.h:67