19#ifndef GAME_PROTOCOL_HPP
20#define GAME_PROTOCOL_HPP
22#include "network/event_rewinder.hpp"
25#include "input/input.hpp"
26#include "utils/cpp2011.hpp"
27#include "utils/stk_process.hpp"
45 mutable std::mutex m_world_deleting_mutex;
48 enum { GP_CONTROLLER_ACTION,
75 std::vector<Action> m_all_actions;
79 void handleAdjustTime(
Event *event);
81 static std::weak_ptr<GameProtocol> m_game_protocol[PT_COUNT];
84 std::tuple<uint8_t, uint16_t, uint16_t, uint16_t>
85 compressAction(
const Action& a)
87 uint8_t w = (uint8_t)(a.m_action & 63) |
88 (a.m_value_l > 0 ? 64 : 0) | (a.m_value_r > 0 ? 128 : 0);
89 uint16_t x = (uint16_t)a.m_value;
90 uint16_t y = (uint16_t)std::abs(a.m_value_l);
91 uint16_t z = (uint16_t)std::abs(a.m_value_r);
92 return std::make_tuple(w, x, y, z);
94 std::tuple<PlayerAction, int, int, int>
95 decompressAction(uint8_t w, uint16_t x, uint16_t y , uint16_t z)
98 int l_sign = ((w >> 6) & 1) != 0 ? 1 : -1;
99 int r_sign = ((w >> 7) & 1) != 0 ? 1 : -1;
103 return std::make_tuple(a, b, c, d);
110 virtual void update(
int ticks) OVERRIDE;
113 int value,
int val_l,
int val_r);
127 static std::shared_ptr<GameProtocol> createInstance();
129 static bool emptyInstance()
131 ProcessType pt = STKProcess::getType();
132 return m_game_protocol[pt].expired();
135 static std::shared_ptr<GameProtocol> lock()
137 ProcessType pt = STKProcess::getType();
138 return m_game_protocol[pt].lock();
144 std::unique_lock<std::mutex> acquireWorldDeletingMutex()
const
145 {
return std::unique_lock<std::mutex>(m_world_deleting_mutex); }
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
A simple class that defines an interface to event rewinding: an undo() function when going back in ti...
Definition: event_rewinder.hpp:29
Class representing an event that need to pass trough the system. This is used to remove ENet dependen...
Definition: event.hpp:73
Definition: game_protocol.hpp:41
NetworkString * m_data_to_send
A network string that collects all information from the server to be sent next.
Definition: game_protocol.hpp:57
GameProtocol()
Constructor.
Definition: game_protocol.cpp:60
void handleState(Event *event)
Called when a new full state is received form the server.
Definition: game_protocol.cpp:342
std::vector< int8_t > m_adjust_time
The server might request that the world clock of a client is adjusted to reduce number of rollbacks.
Definition: game_protocol.hpp:61
void startNewState()
Called by the server before assembling a new message containing the full state of the race to be sent...
Definition: game_protocol.cpp:285
void finalizeState(std::vector< std::string > &cur_rewinder)
Called by a server to finalize the current state, which add updated names of rewinder using to the be...
Definition: game_protocol.cpp:310
void handleControllerAction(Event *event)
Called when a controller event is received - either on the server from a client, or on a client from ...
Definition: game_protocol.cpp:185
void addState(BareNetworkString *buffer)
Called by a server to add data to the current state.
Definition: game_protocol.cpp:298
virtual void setup() OVERRIDE
Called when the protocol is going to start.
Definition: game_protocol.hpp:123
virtual void undo(BareNetworkString *buffer) OVERRIDE
Called from the RewindManager when rolling back.
Definition: game_protocol.cpp:369
void sendActions()
Synchronous update - will send all commands collected during the last frame (and could optional only ...
Definition: game_protocol.cpp:78
virtual bool notifyEventAsynchronous(Event *event) OVERRIDE
Called when a message from a remote GameProtocol is received.
Definition: game_protocol.cpp:119
virtual void asynchronousUpdate() OVERRIDE
Called by the protocol listener as often as possible.
Definition: game_protocol.hpp:125
virtual void update(int ticks) OVERRIDE
Called by the protocol listener, synchronously with the main loop.
Definition: game_protocol.cpp:398
void sendState()
Called when the last state information has been added and the message can be sent to the clients.
Definition: game_protocol.cpp:333
void handleItemEventConfirmation(Event *event)
Handles an item even confirmation from a client.
Definition: game_protocol.cpp:274
void controllerAction(int kart_id, PlayerAction action, int value, int val_l, int val_r)
Called from the local kart controller when an action (like steering, acceleration,...
Definition: game_protocol.cpp:153
virtual void rewind(BareNetworkString *buffer) OVERRIDE
Called from the RewindManager after a rollback to replay the stored events.
Definition: game_protocol.cpp:379
void sendItemEventConfirmation(int ticks)
Sends a confirmation to the server that all item events up to 'ticks' have been received.
Definition: game_protocol.cpp:257
NetworkString * getState() const
Returns the NetworkString in which a state was saved.
Definition: game_protocol.hpp:142
The network item manager is responsible for handling all network related item manager tasks - synchro...
Definition: network_item_manager.hpp:45
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
Abstract class used to define the global protocol functions.
Definition: protocol.hpp:92
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
Generic protocols declarations.
Definition: game_protocol.hpp:65