Cosmetic changes only (coding style, comments).

This commit is contained in:
hiker
2015-12-04 17:52:21 +11:00
parent 97756dd337
commit 35d1059a39
8 changed files with 134 additions and 93 deletions

View File

@@ -7,18 +7,20 @@
#include "network/protocols/game_events_protocol.hpp"
#include "modes/world.hpp"
#include "karts/controller/controller.hpp"
NetworkWorld::NetworkWorld()
{
m_running = false;
m_has_run = false;
}
} // NetworkWorld
// ----------------------------------------------------------------------------
#include "karts/controller/controller.hpp"
NetworkWorld::~NetworkWorld()
{
}
} // ~NetworkWorld
// ----------------------------------------------------------------------------
void NetworkWorld::update(float dt)
{
if (!m_has_run)
@@ -27,7 +29,8 @@ void NetworkWorld::update(float dt)
ProtocolManager::getInstance()->getProtocol(PROTOCOL_SYNCHRONIZATION));
if (protocol) // if this protocol exists, that's that we play online
{
Log::debug("NetworkWorld", "Coutdown value is %f", protocol->getCountdown());
Log::debug("NetworkWorld", "Coutdown value is %f",
protocol->getCountdown());
if (protocol->getCountdown() > 0.0)
{
return;
@@ -35,44 +38,54 @@ void NetworkWorld::update(float dt)
World::getWorld()->setNetworkWorld(true);
}
World::getWorld()->updateWorld(dt);
if (World::getWorld()->getPhase() >= WorldStatus::RESULT_DISPLAY_PHASE) // means it's the end
// if the race is over
if (World::getWorld()->getPhase() >= WorldStatus::RESULT_DISPLAY_PHASE)
{
// consider the world finished.
stop();
Log::info("NetworkWorld", "The game is considered finish.");
}
}
} // update
// ----------------------------------------------------------------------------
void NetworkWorld::start()
{
m_running = true;
}
} // start
// ----------------------------------------------------------------------------
void NetworkWorld::stop()
{
m_running = false;
}
} // stop
// ----------------------------------------------------------------------------
bool NetworkWorld::isRaceOver()
{
if (!World::getWorld())
if(!World::getWorld())
return false;
return (World::getWorld()->getPhase() > WorldStatus::RACE_PHASE);
}
} // isRaceOver
// ----------------------------------------------------------------------------
void NetworkWorld::collectedItem(Item *item, AbstractKart *kart)
{
assert(NetworkConfig::get()->isServer()); // this is only called in the server
// this is only called in the server
assert(NetworkConfig::get()->isServer());
GameEventsProtocol* protocol = static_cast<GameEventsProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_GAME_EVENTS));
protocol->collectedItem(item,kart);
}
} // collectedItem
void NetworkWorld::controllerAction(Controller* controller, PlayerAction action, int value)
// ----------------------------------------------------------------------------
void NetworkWorld::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

@@ -36,27 +36,36 @@ class Item;
*/
class NetworkWorld : public AbstractSingleton<NetworkWorld>
{
private:
bool m_running;
float m_race_time;
bool m_has_run;
std::string m_self_kart;
friend class AbstractSingleton<NetworkWorld>;
public:
void update(float dt);
void start();
void stop();
bool isRunning() { return m_running; }
bool isRaceOver();
NetworkWorld();
virtual ~NetworkWorld();
void collectedItem(Item *item, AbstractKart *kart);
void controllerAction(Controller* controller, PlayerAction action, int value);
public:
void update(float dt);
std::string m_self_kart;
protected:
bool m_running;
float m_race_time;
bool m_has_run;
void start();
void stop();
bool isRaceOver();
void collectedItem(Item *item, AbstractKart *kart);
void controllerAction(Controller* controller, PlayerAction action,
int value);
// ------------------------------------------------------------------------
/** Sets the name of the kart of this player. */
void setSelfKart(const std::string &name) { m_self_kart = name; }
// ------------------------------------------------------------------------
const std::string& getSelfKart() const { return m_self_kart; }
// ------------------------------------------------------------------------
/** Returns if this instance is in running state or not. */
bool isRunning() { return m_running; }
private:
NetworkWorld();
virtual ~NetworkWorld();
};
#endif // NETWORK_WORLD_HPP

View File

@@ -36,7 +36,7 @@ void ControllerEventsProtocol::setup()
const std::vector<STKPeer*> &peers = STKHost::get()->getPeers();
for (unsigned int i = 0; i < karts.size(); i++)
{
if (karts[i]->getIdent() == NetworkWorld::getInstance()->m_self_kart)
if (karts[i]->getIdent() == NetworkWorld::getInstance()->getSelfKart())
{
Log::info("ControllerEventsProtocol", "My id is %d", i);
m_self_controller_index = i;

View File

@@ -15,9 +15,11 @@ KartUpdateProtocol::KartUpdateProtocol() : Protocol(PROTOCOL_KART_UPDATE)
{
//if (m_karts[i]->getWorldKartId())
{
Log::info("KartUpdateProtocol", "Kart %d has id %d and name %s", i, m_karts[i]->getWorldKartId(), m_karts[i]->getIdent().c_str());
Log::info("KartUpdateProtocol", "Kart %d has id %d and name %s",
i, m_karts[i]->getWorldKartId(),
m_karts[i]->getIdent().c_str());
}
if (m_karts[i]->getIdent() == NetworkWorld::getInstance()->m_self_kart)
if (m_karts[i]->getIdent() == NetworkWorld::getInstance()->getSelfKart())
{
Log::info("KartUpdateProtocol", "My id is %d", i);
m_self_kart_index = i;

View File

@@ -157,10 +157,11 @@ void StartGameProtocol::update()
input_manager->getDeviceManager()->setSinglePlayer(ap);
race_manager->setPlayerKart(i, rki);
race_manager->setLocalKartInfo(new_player_id, profile->getKartName());
race_manager->setLocalKartInfo(new_player_id,
profile->getKartName());
// self config
Log::info("StartGameProtocol", "Self player device added.");
NetworkWorld::getInstance()->m_self_kart = profile->getKartName();
NetworkWorld::getInstance()->setSelfKart(profile->getKartName());
break;
}
}
@@ -230,10 +231,12 @@ void StartGameProtocol::update()
} // update
// ----------------------------------------------------------------------------
/** Callback from the race manager when the world was setup.
*/
void StartGameProtocol::ready()
{
// On clients this means the loading is finished
if (!NetworkConfig::get()->isServer())
if (NetworkConfig::get()->isclient())
{
assert(STKHost::get()->getPeerCount() == 1);
NetworkString ns(5);

View File

@@ -25,14 +25,13 @@ SynchronizationProtocol::SynchronizationProtocol()
m_total_diff.resize(size);
m_average_ping.resize(size);
m_countdown_activated = false;
}
} // SynchronizationProtocol
//-----------------------------------------------------------------------------
SynchronizationProtocol::~SynchronizationProtocol()
{
}
} // ~SynchronizationProtocol
//-----------------------------------------------------------------------------
bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
@@ -72,7 +71,8 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
}
if (peers[peer_id]->getClientServerToken() != token)
{
Log::warn("SynchronizationProtocol", "Bad token from peer %d", talk_id);
Log::warn("SynchronizationProtocol", "Bad token from peer %d",
talk_id);
return true;
}
@@ -81,13 +81,15 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
NetworkString response(10);
response.ai8(data.gui8(talk_id)).ai32(token).ai8(0).ai32(sequence);
sendMessage(peers[peer_id], response, false);
Log::verbose("SynchronizationProtocol", "Answering sequence %u", sequence);
Log::verbose("SynchronizationProtocol", "Answering sequence %u",
sequence);
// countdown time in the message
if (data.size() == 14 && !NetworkConfig::get()->isServer())
{
uint32_t time_to_start = data.gui32(10);
Log::debug("SynchronizationProtocol", "Request to start game in %d.", time_to_start);
Log::debug("SynchronizationProtocol",
"Request to start game in %d.", time_to_start);
if (!m_countdown_activated)
startCountdown(time_to_start);
else
@@ -100,7 +102,8 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
{
if (sequence >= m_pings[peer_id].size())
{
Log::warn("SynchronizationProtocol", "The sequence# %u isn't known.", sequence);
Log::warn("SynchronizationProtocol",
"The sequence# %u isn't known.", sequence);
return true;
}
double current_time = StkTime::getRealTime();
@@ -108,12 +111,14 @@ bool SynchronizationProtocol::notifyEventAsynchronous(Event* event)
Log::verbose("SynchronizationProtocol", "InstantPing is %u",
(unsigned int)((current_time - m_pings[peer_id][sequence])*1000));
m_successed_pings[peer_id]++;
m_average_ping[peer_id] = (int)((m_total_diff[peer_id]/m_successed_pings[peer_id])*1000.0);
m_average_ping[peer_id] =
(int)((m_total_diff[peer_id]/m_successed_pings[peer_id])*1000.0);
Log::debug("SynchronizationProtocol", "Ping is %u", m_average_ping[peer_id]);
Log::debug("SynchronizationProtocol", "Ping is %u",
m_average_ping[peer_id]);
}
return true;
}
} // notifyEventAsynchronous
//-----------------------------------------------------------------------------
@@ -122,7 +127,7 @@ void SynchronizationProtocol::setup()
Log::info("SynchronizationProtocol", "Ready !");
m_countdown = 5.0; // init the countdown to 5s
m_has_quit = false;
}
} // setup
//-----------------------------------------------------------------------------
@@ -134,11 +139,13 @@ void SynchronizationProtocol::asynchronousUpdate()
{
m_countdown -= (current_time - m_last_countdown_update);
m_last_countdown_update = current_time;
Log::debug("SynchronizationProtocol", "Update! Countdown remaining : %f", m_countdown);
Log::debug("SynchronizationProtocol",
"Update! Countdown remaining : %f", m_countdown);
if (m_countdown < 0.0 && !m_has_quit)
{
m_has_quit = true;
Log::info("SynchronizationProtocol", "Countdown finished. Starting now.");
Log::info("SynchronizationProtocol",
"Countdown finished. Starting now.");
(new KartUpdateProtocol())->requestStart();
(new ControllerEventsProtocol())->requestStart();
(new GameEventsProtocol())->requestStart();
@@ -153,7 +160,8 @@ void SynchronizationProtocol::asynchronousUpdate()
else if (seconds != (int)(ceil(m_countdown)))
{
seconds = (int)(ceil(m_countdown));
Log::info("SynchronizationProtocol", "Starting in %d seconds.", seconds);
Log::info("SynchronizationProtocol", "Starting in %d seconds.",
seconds);
}
}
if (current_time > timer+0.1)
@@ -162,22 +170,25 @@ void SynchronizationProtocol::asynchronousUpdate()
for (unsigned int i = 0; i < peers.size(); i++)
{
NetworkString ns(10);
ns.ai8(i).addUInt32(peers[i]->getClientServerToken()).addUInt8(1).addUInt32(m_pings[i].size());
ns.ai8(i).addUInt32(peers[i]->getClientServerToken()).addUInt8(1)
.addUInt32(m_pings[i].size());
// now add the countdown if necessary
if (m_countdown_activated && NetworkConfig::get()->isServer())
{
ns.addUInt32((int)(m_countdown*1000.0));
Log::debug("SynchronizationProtocol", "CNTActivated: Countdown value : %f", m_countdown);
Log::debug("SynchronizationProtocol",
"CNTActivated: Countdown value : %f", m_countdown);
}
Log::verbose("SynchronizationProtocol", "Added sequence number %u for peer %d", m_pings[i].size(), i);
Log::verbose("SynchronizationProtocol",
"Added sequence number %u for peer %d",
m_pings[i].size(), i);
timer = current_time;
m_pings[i].insert(std::pair<int,double>(m_pings_count[i], timer));
sendMessage(peers[i], ns, false);
m_pings_count[i]++;
}
}
}
} // for i M peers
} // if current_time > timer + 0.1
} // asynchronousUpdate
//-----------------------------------------------------------------------------
@@ -186,5 +197,6 @@ void SynchronizationProtocol::startCountdown(int ms_countdown)
m_countdown_activated = true;
m_countdown = (double)(ms_countdown)/1000.0;
m_last_countdown_update = StkTime::getRealTime();
Log::info("SynchronizationProtocol", "Countdown started with value %f", m_countdown);
}
Log::info("SynchronizationProtocol", "Countdown started with value %f",
m_countdown);
} // startCountdown

View File

@@ -7,29 +7,30 @@
class SynchronizationProtocol : public Protocol
{
public:
SynchronizationProtocol();
virtual ~SynchronizationProtocol();
private:
std::vector<std::map<uint32_t, double> > m_pings;
std::vector<uint32_t> m_average_ping;
std::vector<uint32_t> m_pings_count;
std::vector<uint32_t> m_successed_pings;
std::vector<double> m_total_diff;
bool m_countdown_activated;
double m_countdown;
double m_last_countdown_update;
bool m_has_quit;
virtual bool notifyEventAsynchronous(Event* event);
virtual void setup();
virtual void update() {}
virtual void asynchronousUpdate();
public:
SynchronizationProtocol();
virtual ~SynchronizationProtocol();
void startCountdown(int ms_countdown);
virtual bool notifyEventAsynchronous(Event* event);
virtual void setup();
virtual void update() {}
virtual void asynchronousUpdate();
int getCountdown() { return (int)(m_countdown*1000.0); }
void startCountdown(int ms_countdown);
protected:
std::vector<std::map<uint32_t, double> > m_pings;
std::vector<uint32_t> m_average_ping;
std::vector<uint32_t> m_pings_count;
std::vector<uint32_t> m_successed_pings;
std::vector<double> m_total_diff;
bool m_countdown_activated;
double m_countdown;
double m_last_countdown_update;
bool m_has_quit;
};
int getCountdown() { return (int)(m_countdown*1000.0); }
}; // class SynchronizationProtocol
#endif // SYNCHRONIZATION_PROTOCOL_HPP

View File

@@ -336,7 +336,7 @@ void RaceManager::startNew(bool from_overworld)
if (m_saved_gp == NULL)
{
Log::error("Race Manager", "Can not continue Grand Prix '%s'"
"because it could not exist",
"because it could not be loaded",
m_grand_prix.getId().c_str());
m_continue_saved_gp = false; // simple and working
}
@@ -347,10 +347,10 @@ void RaceManager::startNew(bool from_overworld)
m_grand_prix.changeReverse((GrandPrixData::GPReverseType)
m_saved_gp->getReverseType());
m_reverse_track = m_grand_prix.getReverse();
}
}
}
}
} // if m_saved_gp==NULL
} // if m_continue_saved_gp
} // if !network_world
} // if grand prix
// command line parameters: negative numbers=all karts
if(m_num_karts < 0 ) m_num_karts = stk_config->m_max_karts;
@@ -364,14 +364,12 @@ void RaceManager::startNew(bool from_overworld)
(unsigned int) m_num_karts, m_ai_kart_list.size(), m_player_karts.size());
assert((unsigned int)m_num_karts == m_ai_kart_list.size()+m_player_karts.size());
// First add the AI karts (randomly chosen)
// ----------------------------------------
// GP ranks start with -1 for the leader.
int init_gp_rank =
race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER
? -1
: 0;
int init_gp_rank = getMinorMode()==MINOR_MODE_FOLLOW_LEADER ? -1 : 0;
const unsigned int ai_kart_count = m_ai_kart_list.size();
for(unsigned int i = 0; i < ai_kart_count; i++)
{
@@ -389,7 +387,8 @@ void RaceManager::startNew(bool from_overworld)
// -------------------------------------------------
for(unsigned int i = 0; i < m_player_karts.size(); i++)
{
KartType kt= m_player_karts[i].isNetworkPlayer() ? KT_NETWORK_PLAYER : KT_PLAYER;
KartType kt= m_player_karts[i].isNetworkPlayer() ? KT_NETWORK_PLAYER
: KT_PLAYER;
m_kart_status.push_back(KartStatus(m_player_karts[i].getKartName(), i,
m_player_karts[i].getLocalPlayerId(),
m_player_karts[i].getGlobalPlayerId(),
@@ -423,9 +422,9 @@ void RaceManager::startNew(bool from_overworld)
m_grand_prix.getId(),
m_minor_mode,
m_player_karts.size());
}
}
}
} // while m_saved_gp
} // if m_continue_saved_gp
} // if grand prix
startNextRace();
} // startNew
@@ -511,7 +510,7 @@ void RaceManager::startNextRace()
// A second constructor phase is necessary in order to be able to
// call functions which are overwritten (otherwise polymorphism
// will fail and the results will be incorrect . Also in init() functions
// will fail and the results will be incorrect). Also in init() functions
// can be called that use World::getWorld().
World::getWorld()->init();
@@ -533,6 +532,8 @@ void RaceManager::startNextRace()
m_kart_status[i].m_last_time = 0;
}
// In networked races, inform the start game protocol that
// the world has been setup
if(NetworkConfig::get()->isNetworking())
{
StartGameProtocol* protocol = static_cast<StartGameProtocol*>(