16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-23 10:59:18 +02:00
This commit is contained in:
milek7
2019-01-14 22:45:22 +01:00
parent b2a9388fab
commit f55a5465ef
30 changed files with 172 additions and 214 deletions

View File

@@ -7333,8 +7333,11 @@ vehicle_table::erase_disabled() {
&& ( simulation::Train->Dynamic() == vehicle ) ) { && ( simulation::Train->Dynamic() == vehicle ) ) {
// clear potential train binding // clear potential train binding
// TBD, TODO: kill vehicle sounds // TBD, TODO: kill vehicle sounds
SafeDelete( simulation::Train ); simulation::Train = nullptr;
} }
simulation::Trains.purge(vehicle->name());
// remove potential entries in the light array // remove potential entries in the light array
simulation::Lights.remove( vehicle ); simulation::Lights.remove( vehicle );
/* /*

View File

@@ -2032,17 +2032,32 @@ event_manager::insert( basic_event *Event ) {
return true; return true;
} }
// legacy method, returns pointer to specified event, or null basic_event * event_manager::FindEventById(uint32_t id)
basic_event * {
event_manager::FindEvent( std::string const &Name ) { if (id < m_eventmap.size())
return m_events[id];
else
return nullptr;
}
if( Name.empty() ) { return nullptr; } uint32_t event_manager::GetEventId(const basic_event *ev) {
return GetEventId(ev->m_name);
}
uint32_t event_manager::GetEventId(const std::string &Name)
{
if (Name.empty())
return -1;
auto const lookup = m_eventmap.find(Name); auto const lookup = m_eventmap.find(Name);
return ( return lookup != m_eventmap.end() ? lookup->second : -1;
lookup != m_eventmap.end() ? }
m_events[ lookup->second ] :
nullptr ); // legacy method, returns pointer to specified event, or null
basic_event *
event_manager::FindEvent( std::string const &Name )
{
return FindEventById(GetEventId(Name));
} }
// legacy method, inserts specified event in the event query // legacy method, inserts specified event in the event query

View File

@@ -590,6 +590,12 @@ public:
basic_event * basic_event *
begin() { begin() {
return QueryRootEvent; } return QueryRootEvent; }
basic_event*
FindEventById(uint32_t id);
uint32_t GetEventId(const basic_event *ev);
uint32_t GetEventId(std::string const &Name);
// legacy method, returns pointer to specified event, or null // legacy method, returns pointer to specified event, or null
basic_event * basic_event *
FindEvent( std::string const &Name ); FindEvent( std::string const &Name );

View File

@@ -778,20 +778,25 @@ global_settings::ConfigParse(cParser &Parser) {
Parser.getTokens(1); Parser.getTokens(1);
Parser >> python_mipmaps; Parser >> python_mipmaps;
} }
else if (token == "network.enabled")
{
Parser.getTokens(1);
Parser >> network_conf.enabled;
}
else if (token == "network.server") else if (token == "network.server")
{ {
Parser.getTokens(1); Parser.getTokens(1);
Parser >> network_conf.is_server; Parser >> network_conf.is_server;
if (network_conf.is_server) {
Parser.getTokens(2);
Parser >> network_conf.server_host;
Parser >> network_conf.server_port;
}
} }
else if (token == "network.client") else if (token == "network.client")
{ {
Parser.getTokens(1); Parser.getTokens(1);
Parser >> network_conf.is_client; Parser >> network_conf.is_client;
if (network_conf.is_client) {
Parser.getTokens(2);
Parser >> network_conf.client_host;
Parser >> network_conf.client_port;
}
} }
} while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile) } while ((token != "") && (token != "endconfig")); //(!Parser->EndOfFile)
// na koniec trochę zależności // na koniec trochę zależności

View File

@@ -197,9 +197,13 @@ struct global_settings {
bool gfx_usegles = false; bool gfx_usegles = false;
struct network_conf_t { struct network_conf_t {
bool enabled = false;
bool is_server = false; bool is_server = false;
std::string server_host;
uint32_t server_port;
bool is_client = false; bool is_client = false;
std::string client_host;
uint32_t client_port;
} network_conf; } network_conf;
// methods // methods

12
Names.h
View File

@@ -41,6 +41,18 @@ public:
{ {
return insert(Item, Item->name()); return insert(Item, Item->name());
} }
void purge (std::string const &Name)
{
auto lookup = m_itemmap.find( Name );
if (lookup == m_itemmap.end())
return;
delete m_items[lookup->second];
m_items[lookup->second] = nullptr;
// TBD, TODO: remove from m_items?
m_itemmap.erase(lookup);
}
// locates item with specified name. returns pointer to the item, or nullptr // locates item with specified name. returns pointer to the item, or nullptr
Type_ * Type_ *
find( std::string const &Name ) const { find( std::string const &Name ) const {

View File

@@ -8050,10 +8050,9 @@ int TTrain::get_drive_direction()
} }
uint16_t TTrain::id() { uint16_t TTrain::id() {
static uint16_t i = 0; // todo: do something better
if (vid == 0) { if (vid == 0) {
vid = ++i; vid = ++simulation::prev_train_id;
WriteLog("assigning id " + std::to_string(vid) + " to vehicle " + Dynamic()->name()); WriteLog("net: assigning id " + std::to_string(vid) + " to vehicle " + Dynamic()->name(), logtype::net);
} }
return vid; return vid;
} }
@@ -8061,6 +8060,8 @@ uint16_t TTrain::id() {
void train_table::update(double dt) void train_table::update(double dt)
{ {
for (TTrain *train : m_items) { for (TTrain *train : m_items) {
if (!train)
continue;
train->Update(dt); train->Update(dt);
} }
} }

View File

@@ -653,7 +653,7 @@ private:
float m_mastercontrollerreturndelay { 0.f }; float m_mastercontrollerreturndelay { 0.f };
int iRadioChannel { 1 }; // numer aktualnego kana?u radiowego int iRadioChannel { 1 }; // numer aktualnego kana?u radiowego
std::vector<std::tuple<std::string, texture_handle, std::optional<texture_window>>> m_screens; std::vector<std::tuple<std::string, texture_handle, std::optional<texture_window>>> m_screens;
uint16_t vid = 0; uint16_t vid { 0 };
public: public:
float fPress[20][3]; // cisnienia dla wszystkich czlonow float fPress[20][3]; // cisnienia dla wszystkich czlonow

View File

@@ -144,29 +144,6 @@ eu07_application::init( int Argc, char *Argv[] ) {
return result; return result;
} }
void eu07_application::request_train(std::string name) {
m_network->request_train(name);
}
void eu07_application::spawn_train(std::string name) {
TTrain *train = simulation::Trains.find(name);
if (train)
return;
TDynamicObject *dynobj = simulation::Vehicles.find(name);
if (!dynobj)
return;
train = new TTrain();
if (train->Init(dynobj)) {
simulation::Trains.insert(train, name);
}
else {
delete train;
train = nullptr;
}
}
double eu07_application::generate_sync() { double eu07_application::generate_sync() {
if (Timer::GetDeltaTime() == 0.0) if (Timer::GetDeltaTime() == 0.0)
return 0.0; return 0.0;
@@ -709,7 +686,7 @@ eu07_application::init_modes() {
} }
bool eu07_application::init_network() { bool eu07_application::init_network() {
if (Global.network_conf.enabled) { if (Global.network_conf.is_server || Global.network_conf.is_client) {
m_network.emplace(); m_network.emplace();
if (Global.network_conf.is_server) { if (Global.network_conf.is_server) {
m_network->create_server(); m_network->create_server();

View File

@@ -76,8 +76,6 @@ public:
GLFWwindow * GLFWwindow *
window( int const Windowindex = 0 ); window( int const Windowindex = 0 );
void request_train(std::string name);
void spawn_train(std::string name);
double generate_sync(); double generate_sync();
private: private:

View File

@@ -227,6 +227,9 @@ commanddescription_sequence Commands_descriptions = {
{ "vehiclemovebackwards", command_target::vehicle, command_mode::oneoff }, { "vehiclemovebackwards", command_target::vehicle, command_mode::oneoff },
{ "vehicleboost", command_target::vehicle, command_mode::oneoff }, { "vehicleboost", command_target::vehicle, command_mode::oneoff },
{ "debugtoggle", command_target::simulation, command_mode::oneoff }, { "debugtoggle", command_target::simulation, command_mode::oneoff },
{ "pausetoggle", command_target::simulation, command_mode::oneoff },
{ "entervehicle", command_target::simulation, command_mode::oneoff },
{ "queueevent", command_target::simulation, command_mode::oneoff },
}; };
} // simulation } // simulation

View File

@@ -221,6 +221,9 @@ enum class user_command {
vehiclemovebackwards, vehiclemovebackwards,
vehicleboost, vehicleboost,
debugtoggle, debugtoggle,
pausetoggle,
entervehicle,
queueevent,
none = -1 none = -1
}; };

View File

@@ -228,6 +228,7 @@ driverkeyboard_input::default_bindings() {
{ user_command::vehiclemovebackwards, GLFW_KEY_RIGHT_BRACKET | keymodifier::control }, { user_command::vehiclemovebackwards, GLFW_KEY_RIGHT_BRACKET | keymodifier::control },
{ user_command::vehicleboost, GLFW_KEY_TAB | keymodifier::control }, { user_command::vehicleboost, GLFW_KEY_TAB | keymodifier::control },
{ user_command::debugtoggle, GLFW_KEY_F12 | keymodifier::control | keymodifier::shift }, { user_command::debugtoggle, GLFW_KEY_F12 | keymodifier::control | keymodifier::shift },
{ user_command::pausetoggle, GLFW_KEY_ESCAPE }
}; };
} }

View File

@@ -206,6 +206,7 @@ driver_mode::update() {
// render time routines follow: // render time routines follow:
auto const deltarealtime = Timer::GetDeltaRenderTime(); // nie uwzględnia pauzowania ani mnożenia czasu auto const deltarealtime = Timer::GetDeltaRenderTime(); // nie uwzględnia pauzowania ani mnożenia czasu
simulation::State.process_commands();
// fixed step render time routines // fixed step render time routines
@@ -248,27 +249,6 @@ driver_mode::update() {
return true; return true;
} }
TTrain* driver_mode::request_train(TDynamicObject *dynamic) {
TTrain *train = simulation::Trains.find(dynamic->name());
if (train)
return train;
if (!Global.network_conf.enabled) {
train = new TTrain();
if (train->Init(dynamic)) {
simulation::Trains.insert(train, dynamic->name());
}
else {
delete train;
train = nullptr;
}
return train;
} else {
Application.request_train(dynamic->name());
}
return train;
}
// maintenance method, called when the mode is activated // maintenance method, called when the mode is activated
void void
driver_mode::enter() { driver_mode::enter() {
@@ -284,23 +264,17 @@ driver_mode::enter() {
if (nPlayerTrain) if (nPlayerTrain)
{ {
// M7TODO: restore
/*
WriteLog( "Initializing player train, \"" + Global.asHumanCtrlVehicle + "\"" ); WriteLog( "Initializing player train, \"" + Global.asHumanCtrlVehicle + "\"" );
TTrain *train = request_train(nPlayerTrain); Global.pCamera.Pos = nPlayerTrain->GetPosition();
if (train->Init(nPlayerTrain)) { m_relay.post(user_command::entervehicle, 0.0, 0.0, GLFW_PRESS, 0);
simulation::Train = train; */
WriteLog("Player train initialization OK"); FreeFlyModeFlag = true;
Application.set_title( Global.AppName + " (" + simulation::Train->Controlled()->Name + " @ " + Global.SceneryFile + ")" );
CabView();
}
else {
delete train;
Error("Bad init: player train initialization failed");
FreeFlyModeFlag = true; // Ra: automatycznie włączone latanie
Camera.m_owner = nullptr; Camera.m_owner = nullptr;
} DebugCamera = Camera;
} }
else else
{ {
@@ -327,8 +301,6 @@ driver_mode::enter() {
KeyEvents[ 9 ] = simulation::Events.FindEvent( "keyctrl09" ); KeyEvents[ 9 ] = simulation::Events.FindEvent( "keyctrl09" );
} }
//ui_log->enabled = false;
Timer::ResetTimers(); Timer::ResetTimers();
set_picking( Global.ControlPicking ); set_picking( Global.ControlPicking );
@@ -666,7 +638,7 @@ driver_mode::OnKeyDown(int cKey) {
// z [Shift] uruchomienie eventu // z [Shift] uruchomienie eventu
if( ( false == Global.iPause ) // podczas pauzy klawisze nie działają if( ( false == Global.iPause ) // podczas pauzy klawisze nie działają
&& ( KeyEvents[ i ] != nullptr ) ) { && ( KeyEvents[ i ] != nullptr ) ) {
simulation::Events.AddToQuery( KeyEvents[ i ], NULL ); m_relay.post(user_command::queueevent, (double)simulation::Events.GetEventId(KeyEvents[i]), 0.0, GLFW_PRESS, 0);
} }
} }
else if( Global.ctrlState ) { else if( Global.ctrlState ) {
@@ -709,32 +681,19 @@ driver_mode::OnKeyDown(int cKey) {
} }
case GLFW_KEY_F5: { case GLFW_KEY_F5: {
// przesiadka do innego pojazdu // przesiadka do innego pojazdu
if( false == FreeFlyModeFlag ) { if (!FreeFlyModeFlag)
// only available in free fly mode // only available in free fly mode
break; break;
}
TDynamicObject *tmp = std::get<TDynamicObject *>( simulation::Region->find_vehicle( Global.pCamera.Pos, 50, true, false ) ); TDynamicObject *dynamic = std::get<TDynamicObject *>( simulation::Region->find_vehicle( Global.pCamera.Pos, 50, true, false ) );
TTrain *train = simulation::Trains.find(dynamic->name());
if( tmp != nullptr ) { if (train) {
if( ( true == DebugModeFlag )
|| ( tmp->MoverParameters->Vel <= 5.0 ) ) {
TTrain *train = request_train(tmp);
if (train != nullptr) {
/*
if( simulation::Train ) {// jeśli mielismy pojazd
if( simulation::Train->Dynamic()->Mechanik ) { // na skutek jakiegoś błędu może czasem zniknąć
simulation::Train->Dynamic()->Mechanik->TakeControl( true ); // oddajemy dotychczasowy AI
}
}
train->Dynamic()->Mechanik->TakeControl( false );
*/
simulation::Train = train; simulation::Train = train;
InOutKey(); InOutKey();
} else {
m_relay.post(user_command::entervehicle, 0.0, 0.0, GLFW_PRESS, 0);
} }
}
}
break; break;
} }
case GLFW_KEY_F6: { case GLFW_KEY_F6: {

View File

@@ -92,7 +92,6 @@ private:
void ExternalView(); void ExternalView();
void DistantView( bool const Near = false ); void DistantView( bool const Near = false );
void set_picking( bool const Picking ); void set_picking( bool const Picking );
TTrain* request_train(TDynamicObject *dynamic);
// members // members
drivermode_input m_input; drivermode_input m_input;
@@ -109,4 +108,5 @@ private:
double m_primaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for core fixed step routines double m_primaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for core fixed step routines
double m_secondaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for less important fixed step routines double m_secondaryupdateaccumulator { m_secondaryupdaterate }; // keeps track of elapsed simulation time, for less important fixed step routines
int iPause { 0 }; // wykrywanie zmian w zapauzowaniu int iPause { 0 }; // wykrywanie zmian w zapauzowaniu
command_relay m_relay;
}; };

View File

@@ -60,22 +60,6 @@ driver_ui::on_key( int const Key, int const Action ) {
if (ui_layer::on_key(Key, Action)) if (ui_layer::on_key(Key, Action))
return true; return true;
if( Key == GLFW_KEY_ESCAPE ) {
// toggle pause
if( Action != GLFW_PRESS ) { return true; } // recognized, but ignored
if( Global.iPause & 1 ) {
// jeśli pauza startowa
// odpauzowanie, gdy po wczytaniu miało nie startować
Global.iPause ^= 1;
}
else if( ( Global.iMultiplayer & 2 ) == 0 ) {
// w multiplayerze pauza nie ma sensu
Global.iPause ^= 2; // zmiana stanu zapauzowania
}
return true;
}
switch( Key ) { switch( Key ) {
case GLFW_KEY_F1: case GLFW_KEY_F1:
@@ -219,8 +203,7 @@ driver_ui::render_() {
if( ImGui::BeginPopupModal( popupheader, &m_pause_modal_opened, ImGuiWindowFlags_AlwaysAutoResize ) ) { if( ImGui::BeginPopupModal( popupheader, &m_pause_modal_opened, ImGuiWindowFlags_AlwaysAutoResize ) ) {
auto const popupwidth{ locale::strings[ locale::string::driver_pause_header ].size() * 7 }; auto const popupwidth{ locale::strings[ locale::string::driver_pause_header ].size() * 7 };
if( ImGui::Button( locale::strings[ locale::string::driver_pause_resume ].c_str(), ImVec2( popupwidth, 0 ) ) ) { if( ImGui::Button( locale::strings[ locale::string::driver_pause_resume ].c_str(), ImVec2( popupwidth, 0 ) ) ) {
auto const pausemask { 1 | 2 }; m_relay.post(user_command::pausetoggle, 0.0, 0.0, GLFW_PRESS, 0);
Global.iPause &= ~pausemask;
} }
if( ImGui::Button( locale::strings[ locale::string::driver_pause_quit ].c_str(), ImVec2( popupwidth, 0 ) ) ) { if( ImGui::Button( locale::strings[ locale::string::driver_pause_quit ].c_str(), ImVec2( popupwidth, 0 ) ) ) {
glfwSetWindowShouldClose( m_window, 1 ); glfwSetWindowShouldClose( m_window, 1 );

View File

@@ -53,5 +53,6 @@ private:
bool m_paused { false }; bool m_paused { false };
bool m_pause_modal_opened { false }; bool m_pause_modal_opened { false };
command_relay m_relay;
ui::vehiclelist_panel m_vehiclelist; ui::vehiclelist_panel m_vehiclelist;
}; };

View File

@@ -63,7 +63,7 @@ keyboard_input::recall_bindings() {
// misc // misc
{ "f1", GLFW_KEY_F1 }, { "f2", GLFW_KEY_F2 }, { "f3", GLFW_KEY_F3 }, { "f4", GLFW_KEY_F4 }, { "f5", GLFW_KEY_F5 }, { "f1", GLFW_KEY_F1 }, { "f2", GLFW_KEY_F2 }, { "f3", GLFW_KEY_F3 }, { "f4", GLFW_KEY_F4 }, { "f5", GLFW_KEY_F5 },
{ "f6", GLFW_KEY_F6 }, { "f7", GLFW_KEY_F7 }, { "f8", GLFW_KEY_F8 }, { "f9", GLFW_KEY_F9 }, { "f10", GLFW_KEY_F10 }, { "f6", GLFW_KEY_F6 }, { "f7", GLFW_KEY_F7 }, { "f8", GLFW_KEY_F8 }, { "f9", GLFW_KEY_F9 }, { "f10", GLFW_KEY_F10 },
{ "f11", GLFW_KEY_F11 }, { "f12", GLFW_KEY_F12 }, { "tab", GLFW_KEY_TAB } { "f11", GLFW_KEY_F11 }, { "f12", GLFW_KEY_F12 }, { "tab", GLFW_KEY_TAB }, { "esc", GLFW_KEY_ESCAPE }
}; };
// NOTE: to simplify things we expect one entry per line, and whole entry in one line // NOTE: to simplify things we expect one entry per line, and whole entry in one line

View File

@@ -68,7 +68,8 @@ OnCommandGet(multiplayer::DaneRozkaz *pRozkaz)
|| ( typeid( *event ) == typeid( lights_event ) ) || ( typeid( *event ) == typeid( lights_event ) )
|| ( event->m_sibling != 0 ) ) { || ( event->m_sibling != 0 ) ) {
// tylko jawne albo niejawne Multiple // tylko jawne albo niejawne Multiple
simulation::Events.AddToQuery( event, nullptr ); // drugi parametr to dynamic wywołujący - tu brak command_relay relay;
relay.post(user_command::queueevent, (double)simulation::Events.GetEventId(event), 0.0, GLFW_PRESS, 0);
} }
} }
} }

View File

@@ -40,29 +40,3 @@ void network::manager::send_commands(command_queue::commands_map commands)
{ {
client->send_commands(commands); client->send_commands(commands);
} }
void network::manager::request_train(std::string name)
{
if (server) {
TTrain *train = simulation::Trains.find(name);
if (train)
return;
TDynamicObject *dynobj = simulation::Vehicles.find(name);
if (!dynobj)
return;
train = new TTrain();
if (train->Init(dynobj)) {
simulation::Trains.insert(train, name);
server->notify_train(name);
}
else {
delete train;
train = nullptr;
}
}
if (client) {
client->request_train(name);
}
}

View File

@@ -24,6 +24,5 @@ namespace network
void push_delta(double delta, double sync, command_queue::commands_map commands); void push_delta(double delta, double sync, command_queue::commands_map commands);
command_queue::commands_map pop_commands(); command_queue::commands_map pop_commands();
void send_commands(command_queue::commands_map commands); void send_commands(command_queue::commands_map commands);
void request_train(std::string name);
}; };
} }

View File

@@ -144,12 +144,6 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
m->deserialize(stream); m->deserialize(stream);
msg = m; msg = m;
} }
else if (type == message::REQUEST_SPAWN_TRAIN || type == message::SPAWN_TRAIN) {
auto m = std::make_shared<string_message>(type);
m->type = type;
m->deserialize(stream);
msg = m;
}
else { else {
msg = std::make_shared<message>(type); msg = std::make_shared<message>(type);
} }

View File

@@ -13,8 +13,6 @@ namespace network
CONNECT_ACCEPT, CONNECT_ACCEPT,
STEP_INFO, STEP_INFO,
CLIENT_COMMAND, CLIENT_COMMAND,
REQUEST_SPAWN_TRAIN,
SPAWN_TRAIN,
TYPE_MAX TYPE_MAX
}; };

View File

@@ -5,11 +5,15 @@
#include "Timer.h" #include "Timer.h"
#include "application.h" #include "application.h"
network::connection::connection(bool client) {
is_client = client;
}
void network::connection::connected() void network::connection::connected()
{ {
WriteLog("net: socket connected", logtype::net); WriteLog("net: socket connected", logtype::net);
if (!Global.network_conf.is_server) { if (is_client) {
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST); std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
send_message(hello); send_message(hello);
} }
@@ -52,16 +56,6 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
auto now = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now();
delta_queue.push(std::make_pair(now, delta)); delta_queue.push(std::make_pair(now, delta));
} }
else if (msg->type == message::REQUEST_SPAWN_TRAIN)
{
auto req = std::dynamic_pointer_cast<string_message>(msg);
Application.request_train(req->name);
}
else if (msg->type == message::SPAWN_TRAIN)
{
auto req = std::dynamic_pointer_cast<string_message>(msg);
Application.spawn_train(req->name);
}
} }
std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta() std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta()
@@ -125,6 +119,9 @@ network::server::server()
void network::server::push_delta(double dt, double sync, command_queue::commands_map commands) void network::server::push_delta(double dt, double sync, command_queue::commands_map commands)
{ {
if (dt == 0.0 && commands.empty())
return;
std::shared_ptr<delta_message> msg = std::make_shared<delta_message>(); std::shared_ptr<delta_message> msg = std::make_shared<delta_message>();
msg->dt = dt; msg->dt = dt;
msg->sync = sync; msg->sync = sync;
@@ -140,15 +137,6 @@ void network::server::push_delta(double dt, double sync, command_queue::commands
c->send_message(msg); c->send_message(msg);
} }
void network::server::notify_train(std::string name)
{
std::shared_ptr<string_message> msg = std::make_shared<string_message>(message::SPAWN_TRAIN);
msg->name = name;
for (auto c : clients)
c->send_message(msg);
}
command_queue::commands_map network::server::pop_commands() command_queue::commands_map network::server::pop_commands()
{ {
command_queue::commands_map map; command_queue::commands_map map;
@@ -178,11 +166,3 @@ void network::client::send_commands(command_queue::commands_map commands)
conn->send_message(msg); conn->send_message(msg);
} }
void network::client::request_train(std::string name)
{
std::shared_ptr<string_message> msg = std::make_shared<string_message>(message::REQUEST_SPAWN_TRAIN);
msg->name = name;
conn->send_message(msg);
}

View File

@@ -19,6 +19,7 @@ namespace network
std::shared_ptr<delta_message>>> delta_queue; std::shared_ptr<delta_message>>> delta_queue;
command_queue::commands_map client_commands_queue; command_queue::commands_map client_commands_queue;
bool is_client;
//std::chrono::high_resolution_clock::time_point last_time; //std::chrono::high_resolution_clock::time_point last_time;
//double accum = -1.0; //double accum = -1.0;
@@ -30,6 +31,7 @@ namespace network
void data_received(std::string &buffer); void data_received(std::string &buffer);
public: public:
connection(bool client = false);
void send_message(std::shared_ptr<message> msg); void send_message(std::shared_ptr<message> msg);
virtual void connected(); virtual void connected();
@@ -47,7 +49,6 @@ namespace network
server(); server();
void push_delta(double dt, double sync, command_queue::commands_map commands); void push_delta(double dt, double sync, command_queue::commands_map commands);
command_queue::commands_map pop_commands(); command_queue::commands_map pop_commands();
void notify_train(std::string name);
}; };
class client class client
@@ -58,6 +59,5 @@ namespace network
public: public:
std::tuple<double, double, command_queue::commands_map> get_next_delta(); std::tuple<double, double, command_queue::commands_map> get_next_delta();
void send_commands(command_queue::commands_map commands); void send_commands(command_queue::commands_map commands);
void request_train(std::string name);
}; };
} }

View File

@@ -1,9 +1,10 @@
#include "network/tcp.h" #include "network/tcp.h"
#include "Logs.h" #include "Logs.h"
#include "sn_utils.h" #include "sn_utils.h"
#include "Globals.h"
network::tcp_conn::tcp_conn(asio::io_context &io_ctx) network::tcp_conn::tcp_conn(asio::io_context &io_ctx, bool client)
: m_socket(io_ctx) : connection(client), m_socket(io_ctx)
{ {
m_header_buffer.resize(8); m_header_buffer.resize(8);
} }
@@ -82,10 +83,9 @@ asio::ip::tcp::socket& network::tcp_conn::socket()
network::tcp_server::tcp_server(asio::io_context &io_ctx) network::tcp_server::tcp_server(asio::io_context &io_ctx)
: m_acceptor(io_ctx) : m_acceptor(io_ctx)
{ {
auto endpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v6(), 7424); auto endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(Global.network_conf.server_host), Global.network_conf.server_port);
m_acceptor.open(endpoint.protocol()); m_acceptor.open(endpoint.protocol());
m_acceptor.set_option(asio::socket_base::reuse_address(true)); m_acceptor.set_option(asio::socket_base::reuse_address(true));
m_acceptor.set_option(asio::ip::v6_only(false));
m_acceptor.set_option(asio::ip::tcp::no_delay(true)); m_acceptor.set_option(asio::ip::tcp::no_delay(true));
m_acceptor.bind(endpoint); m_acceptor.bind(endpoint);
m_acceptor.listen(10); m_acceptor.listen(10);
@@ -117,11 +117,11 @@ void network::tcp_server::handle_accept(std::shared_ptr<tcp_conn> conn, const as
network::tcp_client::tcp_client(asio::io_context &io_ctx) network::tcp_client::tcp_client(asio::io_context &io_ctx)
{ {
conn = std::make_shared<tcp_conn>(io_ctx); conn = std::make_shared<tcp_conn>(io_ctx, true);
auto tcpconn = std::static_pointer_cast<tcp_conn>(conn); auto tcpconn = std::static_pointer_cast<tcp_conn>(conn);
asio::ip::tcp::endpoint endpoint( asio::ip::tcp::endpoint endpoint(
asio::ip::address::from_string("192.168.0.20"), 7424); asio::ip::address::from_string(Global.network_conf.client_host), Global.network_conf.client_port);
tcpconn->socket().open(endpoint.protocol()); tcpconn->socket().open(endpoint.protocol());
tcpconn->socket().set_option(asio::ip::tcp::no_delay(true)); tcpconn->socket().set_option(asio::ip::tcp::no_delay(true));
tcpconn->socket().async_connect(endpoint, tcpconn->socket().async_connect(endpoint,

View File

@@ -26,7 +26,7 @@ namespace network
void send_data(std::shared_ptr<std::string> buffer) override; void send_data(std::shared_ptr<std::string> buffer) override;
public: public:
tcp_conn(asio::io_context &io_ctx); tcp_conn(asio::io_context &io_ctx, bool client = false);
asio::ip::tcp::socket& socket(); asio::ip::tcp::socket& socket();
void connected() override; void connected() override;

View File

@@ -42,6 +42,7 @@ lua Lua;
scene::basic_region *Region { nullptr }; scene::basic_region *Region { nullptr };
TTrain *Train { nullptr }; TTrain *Train { nullptr };
uint16_t prev_train_id { 0 };
bool is_ready { false }; bool is_ready { false };
bool bool
@@ -60,13 +61,6 @@ state_manager::export_as_text( std::string const &Scenariofile ) const {
// legacy method, calculates changes in simulation state over specified time // legacy method, calculates changes in simulation state over specified time
void void
state_manager::update( double const Deltatime, int Iterationcount ) { state_manager::update( double const Deltatime, int Iterationcount ) {
// aktualizacja animacji krokiem FPS: dt=krok czasu [s], dt*iter=czas od ostatnich przeliczeń
if (Deltatime == 0.0) {
return;
}
process_commands();
auto const totaltime { Deltatime * Iterationcount }; auto const totaltime { Deltatime * Iterationcount };
// NOTE: we perform animations first, as they can determine factors like contact with powergrid // NOTE: we perform animations first, as they can determine factors like contact with powergrid
TAnimModel::AnimUpdate( totaltime ); // wykonanie zakolejkowanych animacji TAnimModel::AnimUpdate( totaltime ); // wykonanie zakolejkowanych animacji
@@ -84,6 +78,51 @@ void state_manager::process_commands() {
if (commanddata.command == user_command::debugtoggle) if (commanddata.command == user_command::debugtoggle)
DebugModeFlag = !DebugModeFlag; DebugModeFlag = !DebugModeFlag;
if (commanddata.command == user_command::pausetoggle) {
if( Global.iPause & 1 ) {
// jeśli pauza startowa
// odpauzowanie, gdy po wczytaniu miało nie startować
Global.iPause ^= 1;
}
else {
Global.iPause ^= 2; // zmiana stanu zapauzowania
}
}
if (commanddata.command == user_command::entervehicle) {
// przesiadka do innego pojazdu
if (!commanddata.freefly)
// only available in free fly mode
continue;
TDynamicObject *dynamic = std::get<TDynamicObject *>( simulation::Region->find_vehicle( commanddata.location, 50, true, false ) );
if (!dynamic)
continue;
TTrain *train = simulation::Trains.find(dynamic->name());
if (train)
continue;
if( ( true == DebugModeFlag )
|| ( dynamic->MoverParameters->Vel <= 5.0 ) ) {
train = new TTrain();
if (train->Init(dynamic)) {
simulation::Trains.insert(train, dynamic->name());
}
else {
delete train;
train = nullptr;
}
}
}
if (commanddata.command == user_command::queueevent) {
uint32_t id = std::round(commanddata.param1);
basic_event *ev = Events.FindEventById(id);
Events.AddToQuery(ev, nullptr);
}
if (DebugModeFlag) { if (DebugModeFlag) {
if (commanddata.command == user_command::timejump) { if (commanddata.command == user_command::timejump) {
Time.update(commanddata.param1); Time.update(commanddata.param1);

View File

@@ -32,12 +32,13 @@ public:
// stores class data in specified file, in legacy (text) format // stores class data in specified file, in legacy (text) format
void void
export_as_text( std::string const &Scenariofile ) const; export_as_text( std::string const &Scenariofile ) const;
// process input commands
void
process_commands();
private: private:
// members // members
state_serializer m_serializer; state_serializer m_serializer;
void process_commands();
}; };
// passes specified sound to all vehicles within range as a radio message broadcasted on specified channel // passes specified sound to all vehicles within range as a radio message broadcasted on specified channel
@@ -59,6 +60,7 @@ extern lua Lua;
extern scene::basic_region *Region; extern scene::basic_region *Region;
extern TTrain *Train; extern TTrain *Train;
extern uint16_t prev_train_id;
extern bool is_ready; extern bool is_ready;
} // simulation } // simulation

View File

@@ -4,7 +4,7 @@
#include "Driver.h" #include "Driver.h"
void ui::vehiclelist_panel::render_contents() { void ui::vehiclelist_panel::render_contents() {
for (const TDynamicObject* vehicle : simulation::Vehicles.sequence()) { for (TDynamicObject* vehicle : simulation::Vehicles.sequence()) {
if (vehicle->Prev()) if (vehicle->Prev())
continue; continue;