vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
command.h
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <limits>
6#include <vector>
7
8#include "net/wire/bit_stream.h"
9#include "platform/input/input_command.h"
10
11namespace Vkm::Engine {
12
21constexpr uint32_t NET_COMMAND_REDUNDANCY = 12;
22
30constexpr uint32_t NET_MAX_PACKET_COMMANDS = 16;
31
39constexpr uint32_t NET_COMMAND_MEMORY = 64;
40
47constexpr uint32_t NET_DRAWN_FRACTION_BITS = 8;
48
50constexpr uint32_t NET_DRAWN_TICK_BITS = 32 + NET_DRAWN_FRACTION_BITS;
51
58constexpr uint32_t NET_AXIS_BITS = 8;
59
77size_t firstCommandToSend(const std::vector<InputCommand>& unconfirmed, uint32_t heard, uint32_t sent);
78
94size_t writeCommands(
95 BitWriter& out,
96 const std::vector<InputCommand>& commands,
97 size_t first,
98 uint32_t actionCount
99);
100
109bool readCommands(BitReader& in, uint32_t actionCount, std::vector<InputCommand>& out);
110
123class NetCommandBuffer {
124 public:
134 static constexpr size_t MAX_DEPTH = 10;
135
142 static constexpr size_t MAX_QUEUED = 256;
143
150 static constexpr uint32_t REPEAT_TICKS = NET_COMMAND_REDUNDANCY * 2;
151
152 public:
153 NetCommandBuffer() = default;
154 ~NetCommandBuffer() = default;
155
156 NetCommandBuffer(const NetCommandBuffer& other) = delete;
157 NetCommandBuffer& operator=(const NetCommandBuffer& other) = delete;
158
159 NetCommandBuffer(NetCommandBuffer && other) = delete;
160 NetCommandBuffer& operator=(NetCommandBuffer && other) = delete;
161
162 public:
164 void accept(const InputCommand& command);
165
177 InputCommand take(uint32_t tick);
178
187 uint32_t newestRunTick() const { return m_ranCommandTick; }
188
198 uint32_t newestSequence() const { return m_newestSequence; }
199
205 size_t pending() const { return m_commands.size(); }
206
216 size_t lowWater() const {
217 return m_lowWater == std::numeric_limits<size_t>::max() ? m_commands.size() : m_lowWater;
218 }
219
221 void restartLowWater() { m_lowWater = std::numeric_limits<size_t>::max(); }
222
230 uint32_t repeatedTicks() const { return m_repeatedTicks; }
231
238 uint32_t skippedCommands() const { return m_skippedCommands; }
239
240 void clear();
241
242 private:
243 std::vector<InputCommand> m_commands;
244 InputCommand m_last;
245 uint32_t m_newestSequence = 0;
246 uint32_t m_newestTick = 0;
247 uint32_t m_consumedSequence = 0;
248 uint32_t m_ranCommandTick = 0;
249 uint32_t m_carriedPressed = 0;
250 uint32_t m_carriedReleased = 0;
251 uint32_t m_starved = 0;
252 uint32_t m_repeatedTicks = 0;
253 uint32_t m_skippedCommands = 0;
254
256 size_t m_lowWater = std::numeric_limits<size_t>::max();
257
258 bool m_ranReal = false;
259 bool m_started = false;
260};
261
262} // namespace Vkm::Engine
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
Packs values to the bit into a byte buffer.
Definition bit_stream.h:19
void restartLowWater()
Start measuring lowWater() again, once it has been said.
Definition command.h:221
uint32_t skippedCommands() const
Commands passed over unrun, since clear(): skipped down to MAX_DEPTH, or pushed out of a queue at MAX...
Definition command.h:238
static constexpr size_t MAX_QUEUED
Most commands held at once, however fast they arrive.
Definition command.h:142
size_t lowWater() const
The fewest commands waiting at any take() since restartLowWater().
Definition command.h:216
uint32_t newestRunTick() const
The newest command tick actually run, in the client's numbering.
Definition command.h:187
static constexpr size_t MAX_DEPTH
Commands the queue may hold before the oldest are skipped, all at once.
Definition command.h:134
size_t pending() const
How many commands are waiting.
Definition command.h:205
uint32_t repeatedTicks() const
Ticks that ran on a repeat because nothing was waiting, since clear().
Definition command.h:230
uint32_t newestSequence() const
The newest sequence this buffer has accepted.
Definition command.h:198
void accept(const InputCommand &command)
Take one command, ignoring a sequence already seen.
static constexpr uint32_t REPEAT_TICKS
Ticks a repeat may stand in for before it stops moving the body.
Definition command.h:150
InputCommand take(uint32_t tick)
The command to run on the tick about to happen.
Everything one simulation tick was told to do, as a value.
Definition input_command.h:28