SuperTuxKart
Loading...
Searching...
No Matches
game_protocol.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2015 Supertuxkart-Team
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19#ifndef GAME_PROTOCOL_HPP
20#define GAME_PROTOCOL_HPP
21
22#include "network/event_rewinder.hpp"
23#include "network/protocol.hpp"
24
25#include "input/input.hpp" // for PlayerAction
26#include "utils/cpp2011.hpp"
27#include "utils/stk_process.hpp"
28
29#include <cstdlib>
30#include <mutex>
31#include <vector>
32#include <tuple>
33
36class NetworkString;
37class STKPeer;
38
39class GameProtocol : public Protocol
40 , public EventRewinder
41{
42private:
43 /* Used to check if deleting world is doing at the same the for
44 * asynchronous event update. */
45 mutable std::mutex m_world_deleting_mutex;
46
48 enum { GP_CONTROLLER_ACTION,
49 GP_STATE,
50 GP_ITEM_UPDATE,
51 GP_ITEM_CONFIRMATION,
52 GP_ADJUST_TIME
53 };
54
58
61 std::vector<int8_t> m_adjust_time;
62
63 // Dummy data structure to save all kart actions.
64 struct Action
65 {
66 int m_ticks;
67 int m_kart_id;
68 PlayerAction m_action;
69 int m_value;
70 int m_value_l;
71 int m_value_r;
72 }; // struct Action
73
74 // List of all kart actions to send to the server
75 std::vector<Action> m_all_actions;
76
77 void handleControllerAction(Event *event);
78 void handleState(Event *event);
79 void handleAdjustTime(Event *event);
81 static std::weak_ptr<GameProtocol> m_game_protocol[PT_COUNT];
82 NetworkItemManager* m_network_item_manager;
83 // Maximum value of values are only 32768
84 std::tuple<uint8_t, uint16_t, uint16_t, uint16_t>
85 compressAction(const Action& a)
86 {
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);
93 }
94 std::tuple<PlayerAction, int, int, int>
95 decompressAction(uint8_t w, uint16_t x, uint16_t y , uint16_t z)
96 {
97 PlayerAction a = (PlayerAction)(w & 63);
98 int l_sign = ((w >> 6) & 1) != 0 ? 1 : -1;
99 int r_sign = ((w >> 7) & 1) != 0 ? 1 : -1;
100 int b = x;
101 int c = y * l_sign;
102 int d = z * r_sign;
103 return std::make_tuple(a, b, c, d);
104 }
105public:
106 GameProtocol();
107 virtual ~GameProtocol();
108
109 virtual bool notifyEventAsynchronous(Event* event) OVERRIDE;
110 virtual void update(int ticks) OVERRIDE;
111 void sendActions();
112 void controllerAction(int kart_id, PlayerAction action,
113 int value, int val_l, int val_r);
114 void startNewState();
115 void addState(BareNetworkString *buffer);
116 void sendState();
117 void finalizeState(std::vector<std::string>& cur_rewinder);
118 void sendItemEventConfirmation(int ticks);
119
120 virtual void undo(BareNetworkString *buffer) OVERRIDE;
121 virtual void rewind(BareNetworkString *buffer) OVERRIDE;
122 // ------------------------------------------------------------------------
123 virtual void setup() OVERRIDE {};
124 // ------------------------------------------------------------------------
125 virtual void asynchronousUpdate() OVERRIDE {}
126 // ------------------------------------------------------------------------
127 static std::shared_ptr<GameProtocol> createInstance();
128 // ------------------------------------------------------------------------
129 static bool emptyInstance()
130 {
131 ProcessType pt = STKProcess::getType();
132 return m_game_protocol[pt].expired();
133 } // emptyInstance
134 // ------------------------------------------------------------------------
135 static std::shared_ptr<GameProtocol> lock()
136 {
137 ProcessType pt = STKProcess::getType();
138 return m_game_protocol[pt].lock();
139 } // lock
140 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
144 std::unique_lock<std::mutex> acquireWorldDeletingMutex() const
145 { return std::unique_lock<std::mutex>(m_world_deleting_mutex); }
146}; // class GameProtocol
147
148#endif // GAME_PROTOCOL_HPP
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
PlayerAction
types of input events / what actions the players can do
Definition: input.hpp:117
Generic protocols declarations.
Definition: game_protocol.hpp:65