nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
interest.h
Go to the documentation of this file.
1#ifndef NANOMMO_SERVER_INTEREST_H_
2#define NANOMMO_SERVER_INTEREST_H_
3
4#include <compare>
5#include <cstdint>
6#include <map>
7#include <vector>
8
9#include "server/world.h"
10
11namespace nanommo {
12
13/// In metres, like the client's chunks.
14constexpr float kCellSize = 32.0f;
15
16/// Cells each way, so that a player gets everyone within 64 metres.
17constexpr int kInterestCells = 2;
18
19struct Cell {
20 int x = 0;
21 int z = 0;
22
23 auto operator<=>(const Cell&) const = default;
24};
25
26Cell CellOf(float x, float z);
27
28/// Whether the players in one cell see what happens in the other: within
29/// kInterestCells of it each way, as they get the units there.
30bool Near(Cell from, Cell to);
31
32/// The ids of what's in each cell.
33using Cells = std::map<Cell, std::vector<uint32_t>>;
34
35template <typename Unit>
36Cells CellsOf(const std::map<uint32_t, Unit>& units) {
37 Cells cells;
38 for (const auto& [id, unit] : units) {
39 cells[CellOf(unit.x, unit.z)].push_back(id);
40 }
41 return cells;
42}
43
44/// What the players in a cell get: the players, monsters and NPCs near it.
45struct View {
46 std::vector<uint32_t> here;
47 std::vector<uint32_t> players;
48 std::vector<uint32_t> monsters;
49 std::vector<uint32_t> npcs;
50};
51
52/// Players and monsters move, so they go into their cells each time. NPCs
53/// stand still, so the server puts them in theirs once, when they spawn.
54std::map<Cell, View> Views(const std::map<uint32_t, Player>& players,
55 const std::map<uint32_t, Monster>& monsters,
56 const Cells& npcs);
57
58} // namespace nanommo
59
60#endif // NANOMMO_SERVER_INTEREST_H_
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
auto operator<=>(const Cell &) const =default
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