vkmEngine 1.0.0
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
29constexpr uint32_t NET_MAX_PACKET_COMMANDS = 16;
30
38constexpr uint32_t NET_COMMAND_MEMORY = 64;
39
46constexpr uint32_t NET_AXIS_BITS = 8;
47
65size_t firstCommandToSend(const std::vector<InputCommand>& unconfirmed, uint32_t heard, uint32_t sent);
66
82size_t writeCommands(
83 BitWriter& out,
84 const std::vector<InputCommand>& commands,
85 size_t first,
86 uint32_t actionCount
87);
88
97bool readCommands(BitReader& in, uint32_t actionCount, std::vector<InputCommand>& out);
98
111class NetCommandBuffer {
112 public:
122 static constexpr size_t MAX_DEPTH = 10;
123
130 static constexpr size_t MAX_QUEUED = 256;
131
138 static constexpr uint32_t REPEAT_TICKS = NET_COMMAND_REDUNDANCY * 2;
139
140 public:
141 NetCommandBuffer() = default;
142 ~NetCommandBuffer() = default;
143
144 NetCommandBuffer(const NetCommandBuffer& other) = delete;
145 NetCommandBuffer& operator=(const NetCommandBuffer& other) = delete;
146
147 NetCommandBuffer(NetCommandBuffer && other) = delete;
148 NetCommandBuffer& operator=(NetCommandBuffer && other) = delete;
149
150 public:
152 void accept(const InputCommand& command);
153
165 InputCommand take(uint32_t tick);
166
175 uint32_t newestRunTick() const { return m_ranCommandTick; }
176
186 uint32_t newestSequence() const { return m_newestSequence; }
187
193 size_t pending() const { return m_commands.size(); }
194
204 size_t lowWater() const {
205 return m_lowWater == std::numeric_limits<size_t>::max() ? m_commands.size() : m_lowWater;
206 }
207
209 void restartLowWater() { m_lowWater = std::numeric_limits<size_t>::max(); }
210
218 uint32_t repeatedTicks() const { return m_repeatedTicks; }
219
226 uint32_t skippedCommands() const { return m_skippedCommands; }
227
228 void clear();
229
230 private:
231 std::vector<InputCommand> m_commands;
232 InputCommand m_last;
233 uint32_t m_newestSequence = 0;
234 uint32_t m_newestTick = 0;
235 uint32_t m_consumedSequence = 0;
236 uint32_t m_ranCommandTick = 0;
237 uint32_t m_carriedPressed = 0;
238 uint32_t m_carriedReleased = 0;
239 uint32_t m_starved = 0;
240 uint32_t m_repeatedTicks = 0;
241 uint32_t m_skippedCommands = 0;
242
244 size_t m_lowWater = std::numeric_limits<size_t>::max();
245
246 bool m_ranReal = false;
247 bool m_started = false;
248};
249
250} // 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:209
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:226
static constexpr size_t MAX_QUEUED
Most commands held at once, however fast they arrive.
Definition command.h:130
size_t lowWater() const
The fewest commands waiting at any take() since restartLowWater().
Definition command.h:204
uint32_t newestRunTick() const
The newest command tick actually run, in the client's numbering.
Definition command.h:175
static constexpr size_t MAX_DEPTH
Commands the queue may hold before the oldest are skipped, all at once.
Definition command.h:122
size_t pending() const
How many commands are waiting.
Definition command.h:193
uint32_t repeatedTicks() const
Ticks that ran on a repeat because nothing was waiting, since clear().
Definition command.h:218
uint32_t newestSequence() const
The newest sequence this buffer has accepted.
Definition command.h:186
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:138
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:25