milek7/sim branch network code import

This commit is contained in:
tmj-fstate
2020-02-16 03:03:17 +01:00
parent 2ce3091e8f
commit 7a0c89f508
56 changed files with 2609 additions and 426 deletions

View File

@@ -14,6 +14,8 @@ http://mozilla.org/MPL/2.0/.
#include "Logs.h"
#include "Timer.h"
#include "utilities.h"
#include "simulation.h"
#include "Train.h"
namespace simulation {
@@ -266,7 +268,36 @@ commanddescription_sequence Commands_descriptions = {
{ "speedcontrolbutton6", command_target::vehicle },
{ "speedcontrolbutton7", command_target::vehicle },
{ "speedcontrolbutton8", command_target::vehicle },
{ "speedcontrolbutton9", command_target::vehicle }
{ "speedcontrolbutton9", command_target::vehicle },
{ "globalradiostop", command_target::simulation },
{ "timejump", command_target::simulation },
{ "timejumplarge", command_target::simulation },
{ "timejumpsmall", command_target::simulation },
{ "setdatetime", command_target::simulation },
{ "setweather", command_target::simulation },
{ "settemperature", command_target::simulation },
{ "vehiclemoveforwards", command_target::vehicle },
{ "vehiclemovebackwards", command_target::vehicle },
{ "vehicleboost", command_target::vehicle },
{ "debugtoggle", command_target::simulation },
{ "focuspauseset", command_target::simulation },
{ "pausetoggle", command_target::simulation },
{ "entervehicle", command_target::simulation },
{ "resetconsist", command_target::simulation },
{ "fillcompressor", command_target::simulation },
{ "consistreleaser", command_target::simulation },
{ "queueevent", command_target::simulation },
{ "setlight", command_target::simulation },
{ "insertmodel", command_target::simulation },
{ "deletemodel", command_target::simulation },
{ "trainsetmove", command_target::simulation },
{ "consistteleport", command_target::simulation },
{ "pullalarmchain", command_target::simulation },
{ "sendaicommand", command_target::simulation },
{ "spawntrainset", command_target::simulation },
{ "destroytrainset", command_target::simulation },
{ "quitsimulation", command_target::simulation },
};
} // simulation
@@ -274,10 +305,18 @@ commanddescription_sequence Commands_descriptions = {
// posts specified command for specified recipient
void
command_queue::push( command_data const &Command, std::size_t const Recipient ) {
if (is_network_target(Recipient)) {
auto lookup = m_intercept_queue.emplace(Recipient, commanddata_sequence());
lookup.first->second.emplace_back(Command);
} else {
push_direct(Command, Recipient);
}
}
auto lookup = m_commands.emplace( Recipient, commanddata_sequence() );
// recipient stack was either located or created, so we can add to it quite safely
lookup.first->second.emplace( Command );
void command_queue::push_direct(const command_data &Command, const uint32_t Recipient) {
auto lookup = m_commands.emplace( Recipient, commanddata_sequence() );
// recipient stack was either located or created, so we can add to it quite safely
lookup.first->second.emplace_back( Command );
}
// retrieves oldest posted command for specified recipient, if any. returns: true on retrieval, false if there's nothing to retrieve
@@ -296,15 +335,45 @@ command_queue::pop( command_data &Command, std::size_t const Recipient ) {
}
// we have command stack with command(s) on it, retrieve and pop the first one
Command = commands.front();
commands.pop();
commands.pop_front();
return true;
}
bool command_queue::is_network_target(uint32_t const Recipient) {
const command_target target = (command_target)(Recipient & ~0xffff);
if (target == command_target::entity)
return false;
return true;
}
command_queue::commands_map command_queue::pop_intercept_queue() {
commands_map map(m_intercept_queue);
m_intercept_queue.clear();
return map;
}
void command_queue::push_commands(const commands_map &commands) {
for (auto const &kv : commands)
for (command_data const &data : kv.second)
push_direct(data, kv.first);
}
void
command_relay::post( user_command const Command, double const Param1, double const Param2, int const Action, std::uint16_t const Recipient ) const {
command_relay::post(user_command const Command, double const Param1, double const Param2,
int const Action, uint16_t Recipient, glm::vec3 Position, const std::string *Payload) const {
auto const &command = simulation::Commands_descriptions[ static_cast<std::size_t>( Command ) ];
if (command.target == command_target::vehicle && Recipient == 0) {
// default 0 recipient is currently controlled train
if (simulation::Train == nullptr)
return;
Recipient = simulation::Train->id();
}
if( ( command.target == command_target::vehicle )
&& ( true == FreeFlyModeFlag )
&& ( ( false == DebugModeFlag )
@@ -313,44 +382,15 @@ command_relay::post( user_command const Command, double const Param1, double con
return;
}
simulation::Commands.push(
command_data{
Command,
Action,
Param1,
Param2,
Timer::GetDeltaTime() },
static_cast<std::size_t>( command.target ) | Recipient );
/*
#ifdef _DEBUG
if( Action != GLFW_RELEASE ) {
// key was pressed or is still held
if( false == command.name.empty() ) {
if( false == (
( Command == user_command::moveleft )
|| ( Command == user_command::moveleftfast )
|| ( Command == user_command::moveright )
|| ( Command == user_command::moverightfast )
|| ( Command == user_command::moveforward )
|| ( Command == user_command::moveforwardfast )
|| ( Command == user_command::moveback )
|| ( Command == user_command::movebackfast )
|| ( Command == user_command::moveup )
|| ( Command == user_command::moveupfast )
|| ( Command == user_command::movedown )
|| ( Command == user_command::movedownfast )
|| ( Command == user_command::movevector )
|| ( Command == user_command::viewturn ) ) ) {
WriteLog( "Command issued: " + command.name );
}
}
}
else {
// key was released (but we don't log this)
WriteLog( "Key released: " + command.name );
}
#endif
*/
if (Position == glm::vec3(0.0f))
Position = Global.pCamera.Pos;
uint32_t combined_recipient = static_cast<uint32_t>( command.target ) | Recipient;
command_data commanddata({Command, Action, Param1, Param2, Timer::GetDeltaTime(), FreeFlyModeFlag, Position });
if (Payload)
commanddata.payload = *Payload;
simulation::Commands.push(commanddata, combined_recipient);
}
//---------------------------------------------------------------------------