Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarGameTimers.cpp
blob: 0bdd1e590efc3c0124999bf7176eb7ae346298e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "StarGameTimers.hpp"
#include "StarJsonExtra.hpp"
#include "StarDataStreamExtra.hpp"

namespace Star {

GameTimer::GameTimer() : time(), timer() {}

GameTimer::GameTimer(float time) : time(time) {
  reset();
}

bool GameTimer::tick(float dt) {
  timer = approach(0.0f, timer, dt);
  return timer == 0.0f;
}

bool GameTimer::ready() const {
  return timer == 0.0f;
}

bool GameTimer::wrapTick(float dt) {
  auto res = tick(dt);
  if (res)
    reset();
  return res;
}

void GameTimer::reset() {
  timer = time;
}

void GameTimer::setDone() {
  timer = 0.0f;
}

void GameTimer::invert() {
  timer = time - timer;
}

float GameTimer::percent() const {
  if (time)
    return timer / time;
  else
    return 0.0f;
}

DataStream& operator>>(DataStream& ds, GameTimer& gt) {
  ds >> gt.time;
  ds >> gt.timer;
  return ds;
}

DataStream& operator<<(DataStream& ds, GameTimer const& gt) {
  ds << gt.time;
  ds << gt.timer;
  return ds;
}

SlidingWindow::SlidingWindow() : windowSize(1.0f), resolution(1) {}

SlidingWindow::SlidingWindow(float windowSize, size_t resolution, float initialValue)
  : windowSize(windowSize), resolution(resolution) {
  sampleTimer = GameTimer(windowSize / resolution);
  window = std::vector<float>(resolution);
  reset(initialValue);
}

void SlidingWindow::reset(float initialValue) {
  sampleTimer.reset();
  currentIndex = 0;
  currentMin = initialValue;
  currentMax = initialValue;
  currentAverage = initialValue;
  std::fill(window.begin(), window.end(), initialValue);
}

void SlidingWindow::update(function<float()> sampleFunction) {
  if (sampleTimer.wrapTick()) {
    processUpdate(sampleFunction());
  }
}

void SlidingWindow::update(float newValue) {
  if (sampleTimer.wrapTick()) {
    processUpdate(newValue);
  }
}

void SlidingWindow::processUpdate(float newValue) {
  ++currentIndex;
  currentIndex = currentIndex % resolution;
  window[currentIndex] = newValue;

  currentMin = std::numeric_limits<float>::max();
  currentMax = 0;
  float total = 0;
  for (float v : window) {
    total += v;
    currentMin = std::min(currentMin, v);
    currentMax = std::max(currentMax, v);
  }
  currentAverage = total / resolution;
}

float SlidingWindow::min() {
  return currentMin;
}

float SlidingWindow::max() {
  return currentMax;
}

float SlidingWindow::average() {
  return currentAverage;
}

EpochTimer::EpochTimer() : m_elapsedTime(0.0) {}

EpochTimer::EpochTimer(Json json) {
  m_lastSeenEpochTime = json.get("lastEpochTime").optDouble();
  m_elapsedTime = json.getDouble("elapsedTime");
}

Json EpochTimer::toJson() const {
  return JsonObject{{"lastEpochTime", jsonFromMaybe(m_lastSeenEpochTime)}, {"elapsedTime", m_elapsedTime}};
}

void EpochTimer::update(double newEpochTime) {
  if (!m_lastSeenEpochTime) {
    m_lastSeenEpochTime = newEpochTime;
  } else {
    // Don't allow elapsed time to go backwards in the case of the epoch time
    // being lost or wrong.
    double difference = newEpochTime - *m_lastSeenEpochTime;
    if (difference > 0)
      m_elapsedTime += difference;
    m_lastSeenEpochTime = newEpochTime;
  }
}

double EpochTimer::elapsedTime() const {
  return m_elapsedTime;
}

void EpochTimer::setElapsedTime(double elapsedTime) {
  m_elapsedTime = elapsedTime;
}

DataStream& operator>>(DataStream& ds, EpochTimer& et) {
  ds >> et.m_lastSeenEpochTime;
  ds >> et.m_elapsedTime;
  return ds;
}

DataStream& operator<<(DataStream& ds, EpochTimer const& et) {
  ds << et.m_lastSeenEpochTime;
  ds << et.m_elapsedTime;
  return ds;
}

}