nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
history.cc
Go to the documentation of this file.
1#include "server/history.h"
2
3#include <algorithm>
4#include <chrono>
5#include <cstdint>
6#include <deque>
7#include <iterator>
8#include <optional>
9
10namespace nanommo {
11
12void History::Record(uint32_t id, std::chrono::nanoseconds time, Place place) {
13 std::deque<Moment>& moments = moments_[id];
14 moments.push_back({.time = time, .place = place});
15 while (moments.front().time < time - kKept) {
16 moments.pop_front();
17 }
18}
19
20std::optional<Place> History::At(uint32_t id,
21 std::chrono::nanoseconds time) const {
22 const auto found = moments_.find(id);
23 if (found == moments_.end()) {
24 return std::nullopt;
25 }
26 const std::deque<Moment>& moments = found->second;
27 // The first place after the time, and the one before it.
28 const auto after = std::ranges::upper_bound(moments, time, {}, &Moment::time);
29 if (after == moments.begin()) {
30 return moments.front().place;
31 }
32 if (after == moments.end()) {
33 return moments.back().place;
34 }
35 const Moment& from = *std::prev(after);
36 const float share = std::chrono::duration<float>(time - from.time) /
37 std::chrono::duration<float>(after->time - from.time);
38 return Place{.x = from.place.x + (after->place.x - from.place.x) * share,
39 .z = from.place.z + (after->place.z - from.place.z) * share};
40}
41
42void History::Leave(uint32_t id) { moments_.erase(id); }
43
44} // namespace nanommo
std::optional< Place > At(uint32_t id, std::chrono::nanoseconds time) const
Definition history.cc:20
static constexpr std::chrono::seconds kKept
A second: more than any blow goes back.
Definition history.h:21
void Record(uint32_t id, std::chrono::nanoseconds time, Place place)
Definition history.cc:12
void Leave(uint32_t id)
Forgets a player who's left.
Definition history.cc:42
Definition chat.cc:9
In metres from the middle of a land.
Definition lands.h:13
float z
Definition lands.h:15
float x
Definition lands.h:14