Started to introduce GameProtocol (which will combine several

individual protocols.
This commit is contained in:
hiker 2016-12-08 09:02:10 +11:00
parent 32daf4ce29
commit 4d065f67e6
11 changed files with 217 additions and 206 deletions

View File

@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically.
# This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@ -38,7 +38,7 @@
#include "karts/rescue_animation.hpp"
#include "modes/world.hpp"
#include "network/network_config.hpp"
#include "network/race_event_manager.hpp"
#include "network/protocols/game_protocol.hpp"
#include "race/history.hpp"
#include "states_screens/race_gui_base.hpp"
#include "tracks/track.hpp"
@ -144,13 +144,12 @@ void LocalPlayerController::action(PlayerAction action, int value)
PlayerController::action(action, value);
// If this is a client, send the action to the server
if (World::getWorld()->isNetworkWorld() &&
NetworkConfig::get()->isClient() &&
RaceEventManager::getInstance()->isRunning() )
if (World::getWorld()->isNetworkWorld() &&
NetworkConfig::get()->isClient() )
{
RaceEventManager::getInstance()->controllerAction(this, action, value);
GameProtocol::getInstance()->controllerAction(m_kart->getWorldKartId(),
action, value);
}
} // action
//-----------------------------------------------------------------------------

View File

@ -353,6 +353,13 @@ public:
m_current_offset = 5; // ignore type and token
} // NetworkString
// ------------------------------------------------------------------------
/** Empties the string, but does not reset the pre-allocated size. */
void clear()
{
m_buffer.erase(m_buffer.begin() + 5, m_buffer.end());
m_current_offset = 5;
} // clear
// ------------------------------------------------------------------------
/** Returns the protocol type of this message. */
ProtocolType getProtocolType() const

View File

@ -1,136 +0,0 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2015 Supertuxkart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/controller_events_protocol.hpp"
#include "modes/world.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/controller/controller.hpp"
#include "network/event.hpp"
#include "network/network_config.hpp"
#include "network/network_player_profile.hpp"
#include "network/game_setup.hpp"
#include "network/network_config.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp"
#include "network/stk_peer.hpp"
#include "utils/log.hpp"
//-----------------------------------------------------------------------------
ControllerEventsProtocol::ControllerEventsProtocol()
: Protocol( PROTOCOL_CONTROLLER_EVENTS)
{
} // ControllerEventsProtocol
//-----------------------------------------------------------------------------
ControllerEventsProtocol::~ControllerEventsProtocol()
{
} // ~ControllerEventsProtocol
//-----------------------------------------------------------------------------
bool ControllerEventsProtocol::notifyEventAsynchronous(Event* event)
{
if(!checkDataSize(event, 13)) return true;
NetworkString &data = event->data();
float time = data.getFloat();
uint8_t client_index = -1;
while (data.size() >= 9)
{
uint8_t kart_id = data.getUInt8();
if (kart_id >=World::getWorld()->getNumKarts())
{
Log::warn("ControllerEventProtocol", "No valid kart id (%s).",
kart_id);
continue;
}
uint8_t serialized_1 = data.getUInt8();
uint8_t serialized_2 = data.getUInt8();
uint8_t serialized_3 = data.getUInt8();
PlayerAction action = (PlayerAction)(data.getUInt8());
int action_value = data.getUInt32();
Log::info("ControllerEventsProtocol", "KartID %d action %d value %d",
kart_id, action, action_value);
Controller *controller = World::getWorld()->getKart(kart_id)
->getController();
KartControl *controls = controller->getControls();
controls->setBrake( (serialized_1 & 0x40)!=0);
controls->setNitro( (serialized_1 & 0x20)!=0);
controls->setRescue( (serialized_1 & 0x10)!=0);
controls->setFire( (serialized_1 & 0x08)!=0);
controls->setLookBack((serialized_1 & 0x04)!=0);
controls->setSkidControl(KartControl::SkidControl(serialized_1 & 0x03));
controller->action(action, action_value);
}
if (data.size() > 0 )
{
Log::warn("ControllerEventProtocol",
"The data seems corrupted. Remains %d", data.size());
}
if (NetworkConfig::get()->isServer())
{
// Send update to all clients except the original sender.
STKHost::get()->sendPacketExcept(event->getPeer(),
&data, false);
} // if server
return true;
} // notifyEventAsynchronous
//-----------------------------------------------------------------------------
/** Called from the local kart controller when an action (like steering,
* acceleration, ...) was triggered. It compresses the current kart control
* state and sends a message with the new info to the server.
* \param controller The controller that triggered the action.
* \param action Which action was triggered.
* \param value New value for the given action.
*/
void ControllerEventsProtocol::controllerAction(Controller* controller,
PlayerAction action, int value)
{
assert(!NetworkConfig::get()->isServer());
KartControl* controls = controller->getControls();
uint8_t serialized_1 = 0;
serialized_1 |= (controls->getBrake()==true);
serialized_1 <<= 1;
serialized_1 |= (controls->getNitro()==true);
serialized_1 <<= 1;
serialized_1 |= (controls->getRescue()==true);
serialized_1 <<= 1;
serialized_1 |= (controls->getFire()==true);
serialized_1 <<= 1;
serialized_1 |= (controls->getLookBack()==true);
serialized_1 <<= 2;
serialized_1 += controls->getSkidControl();
uint8_t serialized_2 = (uint8_t)(controls->getAccel()*255.0);
uint8_t serialized_3 = (uint8_t)(controls->getSteer()*127.0);
NetworkString *ns = getNetworkString(13);
ns->addFloat(World::getWorld()->getTime());
ns->addUInt8(controller->getKart()->getWorldKartId());
ns->addUInt8(serialized_1).addUInt8(serialized_2).addUInt8(serialized_3);
ns->addUInt8((uint8_t)(action)).addUInt32(value);
sendToServer(ns, false); // send message to server
delete ns;
Log::info("ControllerEventsProtocol", "Action %d value %d", action, value);
} // controllerAction

View File

@ -1,47 +0,0 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2015 Supertuxkart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef CONTROLLER_EVENTS_PROTOCOL_HPP
#define CONTROLLER_EVENTS_PROTOCOL_HPP
#include "network/protocol.hpp"
#include "input/input.hpp"
#include "utils/cpp2011.hpp"
class Controller;
class STKPeer;
class ControllerEventsProtocol : public Protocol
{
public:
ControllerEventsProtocol();
virtual ~ControllerEventsProtocol();
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE;
virtual void update(float dt) OVERRIDE {};
virtual void setup() OVERRIDE {};
virtual void asynchronousUpdate() OVERRIDE {}
void controllerAction(Controller* controller, PlayerAction action,
int value);
}; // class ControllerEventsProtocol
#endif // CONTROLLER_EVENTS_PROTOCOL_HPP

View File

@ -0,0 +1,130 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2015 Supertuxkart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "network/protocols/game_protocol.hpp"
#include "modes/world.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/controller/controller.hpp"
#include "network/event.hpp"
#include "network/network_config.hpp"
#include "network/network_player_profile.hpp"
#include "network/game_setup.hpp"
#include "network/network_config.hpp"
#include "network/network_string.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp"
#include "network/stk_peer.hpp"
#include "utils/log.hpp"
//-----------------------------------------------------------------------------
/** Constructor. Allocates the buffer for events to send to the server. */
GameProtocol::GameProtocol()
: Protocol( PROTOCOL_CONTROLLER_EVENTS)
{
m_data_to_send = getNetworkString();
} // GameProtocol
//-----------------------------------------------------------------------------
GameProtocol::~GameProtocol()
{
delete m_data_to_send;
} // ~GameProtocol
//-----------------------------------------------------------------------------
bool GameProtocol::notifyEventAsynchronous(Event* event)
{
if(!checkDataSize(event, 1)) return true;
NetworkString &data = event->data();
uint8_t count = data.getUInt8();
for (unsigned int i = 0; i < count; i++)
{
float time = data.getFloat();
uint8_t kart_id = data.getUInt8();
PlayerAction action = (PlayerAction)(data.getUInt8());
int value = data.getUInt32();
Log::info("GameProtocol", "Action at %f: %d %d %d",
time, kart_id, action, value);
assert(kart_id < World::getWorld()->getNumKarts());
Controller *controller = World::getWorld()->getKart(kart_id)
->getController();
controller->action(action, value);
}
if (data.size() > 0 )
{
Log::warn("ControllerEventProtocol",
"The data seems corrupted. Remains %d", data.size());
}
if (NetworkConfig::get()->isServer())
{
// Send update to all clients except the original sender.
STKHost::get()->sendPacketExcept(event->getPeer(),
&data, false);
} // if server
return true;
} // notifyEventAsynchronous
//-----------------------------------------------------------------------------
/** Synchronous update - will send all commands collected during the last
* frame (and could optional only send messages every N frames). */
void GameProtocol::update(float dt)
{
if (m_all_actions.size() == 0) return; // nothing to do
// Clear left-over data from previous frame. This way the network
// string will increase till it reaches maximum size necessary
m_data_to_send->clear();
m_data_to_send->addUInt8(m_all_actions.size());
// Add all actions
for (auto a : m_all_actions)
{
m_data_to_send->addFloat(a.m_time);
m_data_to_send->addUInt8(a.m_kart_id);
m_data_to_send->addUInt8((uint8_t)(a.m_action)).addUInt32(a.m_value);
} // for a in m_all_actions
// FIXME: for now send reliable
sendToServer(m_data_to_send, /*reliable*/ true);
m_all_actions.clear();
} // update
//-----------------------------------------------------------------------------
/** Called from the local kart controller when an action (like steering,
* acceleration, ...) was triggered. It compresses the current kart control
* state and sends a message with the new info to the server.
* \param Kart id that triggered the action.
* \param action Which action was triggered.
* \param value New value for the given action.
*/
void GameProtocol::controllerAction(int kart_id, PlayerAction action,
int value)
{
assert(NetworkConfig::get()->isClient());
Action a;
a.m_kart_id = kart_id;
a.m_action = action;
a.m_value = value;
a.m_time = World::getWorld()->getTime();
m_all_actions.push_back(a);
Log::info("GameProtocol", "Action %d value %d", action, value);
} // controllerAction

View File

@ -0,0 +1,71 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2015 Supertuxkart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef GAME_PROTOCOL_HPP
#define GAME_PROTOCOL_HPP
#include "network/protocol.hpp"
#include "input/input.hpp" // for PlayerAction
#include "utils/cpp2011.hpp"
#include "utils/singleton.hpp"
#include <vector>
class NetworkString;
class GameProtocol : public Protocol
, public Singleton<GameProtocol>
{
private:
/** The type of game events to be forwarded to the server. */
enum { GP_CONTROLLER_ACTION = 0x01};
/** A network string that collects all information from the server to be sent
* next. */
NetworkString *m_data_to_send;
// Dummy data structure to save all kart actions.
struct Action
{
float m_time;
int m_kart_id;
PlayerAction m_action;
int m_value;
}; // struct Action
// List of all kart actions to send to the server
std::vector<Action> m_all_actions;
public:
GameProtocol();
virtual ~GameProtocol();
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE;
virtual void update(float dt) OVERRIDE;
void controllerAction(int kart_id, PlayerAction action,
int value);
// ------------------------------------------------------------------------
virtual void setup() OVERRIDE {};
// ------------------------------------------------------------------------
virtual void asynchronousUpdate() OVERRIDE {}
}; // class GameProtocol
#endif // GAME_PROTOCOL_HPP

View File

@ -24,7 +24,7 @@
#include "modes/world.hpp"
#include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/controller_events_protocol.hpp"
#include "network/protocols/game_protocol.hpp"
#include "network/protocols/game_events_protocol.hpp"
#include "network/protocols/kart_update_protocol.hpp"
#include "network/protocols/latency_protocol.hpp"
@ -125,7 +125,7 @@ void LobbyProtocol::loadWorld()
m_game_setup->getRaceConfig()->loadWorld();
World::getWorld()->setNetworkWorld(true);
(new KartUpdateProtocol())->requestStart();
(new ControllerEventsProtocol())->requestStart();
GameProtocol::getInstance()->requestStart();
(new GameEventsProtocol())->requestStart();
} // loadWorld

View File

@ -5,7 +5,6 @@
#include "modes/world.hpp"
#include "network/network_config.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/controller_events_protocol.hpp"
#include "network/protocols/game_events_protocol.hpp"
@ -85,13 +84,3 @@ void RaceEventManager::collectedItem(Item *item, AbstractKart *kart)
protocol->collectedItem(item,kart);
} // collectedItem
// ----------------------------------------------------------------------------
void RaceEventManager::controllerAction(Controller* controller,
PlayerAction action, int value)
{
ControllerEventsProtocol* protocol = static_cast<ControllerEventsProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_CONTROLLER_EVENTS));
if (protocol)
protocol->controllerAction(controller, action, value);
} // controllerAction

View File

@ -53,8 +53,6 @@ public:
bool isRaceOver();
void collectedItem(Item *item, AbstractKart *kart);
void controllerAction(Controller* controller, PlayerAction action,
int value);
void kartFinishedRace(AbstractKart *kart, float time);
// ------------------------------------------------------------------------
/** Returns if this instance is in running state or not. */

View File

@ -230,7 +230,7 @@ void STKHost::create()
* at that stage as well.
*
* Once the countdown is 0 (or below), the Synchronization Protocol will
* start the protocols: KartUpdateProtocol, ControllerEventsProtocol,
* start the protocols: KartUpdateProtocol, GameProtocol,
* GameEventsProtocol. Then the LatencyProtocol is terminated
* which indicates to the main loop to start the actual game.
*/