nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
interest.cc
Go to the documentation of this file.
1#include "server/interest.h"
2
3#include <cmath>
4#include <cstdlib>
5#include <map>
6#include <vector>
7
8namespace nanommo {
9
10namespace {
11
12void Gather(const Cells& cells, Cell cell, std::vector<uint32_t>& ids) {
13 if (const auto found = cells.find(cell); found != cells.end()) {
14 ids.insert(ids.end(), found->second.begin(), found->second.end());
15 }
16}
17
18} // namespace
19
20Cell CellOf(float x, float z) {
21 return {.x = static_cast<int>(std::floor(x / kCellSize)),
22 .z = static_cast<int>(std::floor(z / kCellSize))};
23}
24
25bool Near(Cell from, Cell to) {
26 return std::abs(from.x - to.x) <= kInterestCells &&
27 std::abs(from.z - to.z) <= kInterestCells;
28}
29
30std::map<Cell, View> Views(const std::map<uint32_t, Player>& players,
31 const std::map<uint32_t, Monster>& monsters,
32 const Cells& npcs) {
33 const Cells players_in = CellsOf(players);
34 const Cells monsters_in = CellsOf(monsters);
35
36 std::map<Cell, View> views;
37 for (const auto& [cell, here] : players_in) {
38 View& view = views[cell];
39 view.here = here;
40 for (int dz = -kInterestCells; dz <= kInterestCells; ++dz) {
41 for (int dx = -kInterestCells; dx <= kInterestCells; ++dx) {
42 const Cell near{.x = cell.x + dx, .z = cell.z + dz};
43 Gather(players_in, near, view.players);
44 Gather(monsters_in, near, view.monsters);
45 Gather(npcs, near, view.npcs);
46 }
47 }
48 }
49 return views;
50}
51
52} // namespace nanommo
Definition chat.cc:9
Cell CellOf(float x, float z)
Definition interest.cc:20
bool Near(Cell from, Cell to)
Definition interest.cc:25
Cells CellsOf(const std::map< uint32_t, Unit > &units)
Definition interest.h:36
constexpr int kInterestCells
Cells each way, so that a player gets everyone within 64 metres.
Definition interest.h:17
constexpr float kCellSize
In metres, like the client's chunks.
Definition interest.h:14
std::map< Cell, std::vector< uint32_t > > Cells
The ids of what's in each cell.
Definition interest.h:33
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
What the players in a cell get: the players, monsters and NPCs near it.
Definition interest.h:45
std::vector< uint32_t > here
Definition interest.h:46
std::vector< uint32_t > monsters
Definition interest.h:48
std::vector< uint32_t > players
Definition interest.h:47
std::vector< uint32_t > npcs
Definition interest.h:49