nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
measure.cc
Go to the documentation of this file.
2
3#include <cmath>
4#include <cstddef>
5
6namespace nanommo {
7
8namespace {
9
10// A second of worlds, so that one late tick doesn't swing the speed.
11constexpr double kOver = 1.0;
12// Five seconds of hits, as a swing takes about one.
13constexpr double kHitsOver = 5.0;
14
15} // namespace
16
17void Speedometer::Add(double sent, float x, float z) {
18 if (!places_.empty() && sent <= places_.back().sent) {
19 return;
20 }
21 places_.push_back({.sent = sent, .x = x, .z = z});
22 while (places_.size() > 2 && places_.back().sent - places_[1].sent >= kOver) {
23 places_.pop_front();
24 }
25}
26
28 if (places_.size() < 2) {
29 return 0.0f;
30 }
31 double metres = 0.0;
32 for (std::size_t i = 1; i < places_.size(); ++i) {
33 metres += std::hypot(places_[i].x - places_[i - 1].x,
34 places_[i].z - places_[i - 1].z);
35 }
36 return static_cast<float>(metres /
37 (places_.back().sent - places_.front().sent));
38}
39
40void Hits::Add(double at, uint32_t amount) {
41 hits_.push_back({.at = at, .amount = amount});
42 while (!hits_.empty() && at - hits_.front().at > kHitsOver) {
43 hits_.pop_front();
44 }
45}
46
47float Hits::PerSecond(double now) const {
48 std::size_t count = 0;
49 for (const Hit& hit : hits_) {
50 count += now - hit.at < kHitsOver ? 1 : 0;
51 }
52 return static_cast<float>(static_cast<double>(count) / kHitsOver);
53}
54
55float Hits::DamagePerSecond(double now) const {
56 uint32_t damage = 0;
57 for (const Hit& hit : hits_) {
58 damage += now - hit.at < kHitsOver ? hit.amount : 0;
59 }
60 return static_cast<float>(damage / kHitsOver);
61}
62
63} // namespace nanommo
float DamagePerSecond(double now) const
Definition measure.cc:55
void Add(double at, uint32_t amount)
A hit that did the amount, at the time, in seconds on our clock.
Definition measure.cc:40
float PerSecond(double now) const
Over the last five seconds to now, as a swing takes about one.
Definition measure.cc:47
float MetresASecond() const
Definition measure.cc:27
void Add(double sent, float x, float z)
Definition measure.cc:17
Definition chat.cc:9