mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-03-22 15:05:03 +01:00
work
This commit is contained in:
10
DynObj.cpp
10
DynObj.cpp
@@ -6643,13 +6643,13 @@ TDynamicObject::update_shake( double const Timedelta ) {
|
||||
|
||||
auto shake { 1.25 * ShakeSpring.ComputateForces( shakevector, ShakeState.offset ) };
|
||||
|
||||
if( Random( iVel ) > 25.0 ) {
|
||||
if( LocalRandom( iVel ) > 25.0 ) {
|
||||
// extra shake at increased velocity
|
||||
shake += ShakeSpring.ComputateForces(
|
||||
Math3D::vector3(
|
||||
( Random( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.x,
|
||||
( Random( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.y,
|
||||
( Random( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.z )
|
||||
( LocalRandom( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.x,
|
||||
( LocalRandom( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.y,
|
||||
( LocalRandom( iVel * 2 ) - iVel ) / ( ( iVel * 2 ) * 4 ) * BaseShake.jolt_scale.z )
|
||||
// * (( 200 - DynamicObject->MyTrack->iQualityFlag ) * 0.0075 ) // scale to 75-150% based on track quality
|
||||
* 1.25,
|
||||
ShakeState.offset );
|
||||
@@ -6960,7 +6960,7 @@ TDynamicObject::powertrain_sounds::render( TMoverParameters const &Vehicle, doub
|
||||
if( ( volume < 1.0 )
|
||||
&& ( Vehicle.EnginePower < 100 ) ) {
|
||||
|
||||
auto const volumevariation { Random( 100 ) * Vehicle.enrot / ( 1 + Vehicle.nmax ) };
|
||||
auto const volumevariation { LocalRandom( 100 ) * Vehicle.enrot / ( 1 + Vehicle.nmax ) };
|
||||
if( volumevariation < 2 ) {
|
||||
volume += volumevariation / 200;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@ struct global_settings {
|
||||
bool shiftState{ false }; //m7todo: brzydko
|
||||
bool ctrlState{ false };
|
||||
bool altState{ false };
|
||||
std::mt19937 random_engine{ std::mt19937( static_cast<unsigned int>( std::time( NULL ) ) ) };
|
||||
std::mt19937 random_engine;
|
||||
std::mt19937 local_random_engine;
|
||||
uint32_t random_seed;
|
||||
TDynamicObject *changeDynObj{ nullptr };// info o zmianie pojazdu
|
||||
TCamera pCamera; // parametry kamery
|
||||
TCamera pDebugCamera;
|
||||
|
||||
@@ -109,6 +109,11 @@ eu07_application::init( int Argc, char *Argv[] ) {
|
||||
|
||||
int result { 0 };
|
||||
|
||||
WriteLog( "Starting MaSzyna rail vehicle simulator (release: " + Global.asVersion + ")" );
|
||||
WriteLog( "For online documentation and additional files refer to: http://eu07.pl" );
|
||||
WriteLog( "Authors: Marcin_EU, McZapkie, ABu, Winger, Tolaris, nbmx, OLO_EU, Bart, Quark-t, "
|
||||
"ShaXbee, Oli_EU, youBy, KURS90, Ra, hunter, szociu, Stele, Q, firleju and others\n" );
|
||||
|
||||
init_debug();
|
||||
init_files();
|
||||
if( ( result = init_settings( Argc, Argv ) ) != 0 ) {
|
||||
@@ -118,11 +123,6 @@ eu07_application::init( int Argc, char *Argv[] ) {
|
||||
return result;
|
||||
}
|
||||
|
||||
WriteLog( "Starting MaSzyna rail vehicle simulator (release: " + Global.asVersion + ")" );
|
||||
WriteLog( "For online documentation and additional files refer to: http://eu07.pl" );
|
||||
WriteLog( "Authors: Marcin_EU, McZapkie, ABu, Winger, Tolaris, nbmx, OLO_EU, Bart, Quark-t, "
|
||||
"ShaXbee, Oli_EU, youBy, KURS90, Ra, hunter, szociu, Stele, Q, firleju and others\n" );
|
||||
|
||||
if( ( result = init_glfw() ) != 0 ) {
|
||||
return result;
|
||||
}
|
||||
@@ -138,6 +138,9 @@ eu07_application::init( int Argc, char *Argv[] ) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//Global.random_seed = std::time(nullptr);
|
||||
//Global.random_engine.seed(Global.random_seed);
|
||||
|
||||
if (!init_network())
|
||||
return -1;
|
||||
|
||||
@@ -167,6 +170,18 @@ void eu07_application::spawn_train(std::string name) {
|
||||
}
|
||||
}
|
||||
|
||||
double eu07_application::generate_sync() {
|
||||
if (Timer::GetDeltaTime() == 0.0)
|
||||
return 0.0;
|
||||
double sync = 0.0;
|
||||
for (const TDynamicObject* vehicle : simulation::Vehicles.sequence()) {
|
||||
glm::vec3 pos = vehicle->GetPosition();
|
||||
sync += pos.x + pos.y + pos.z;
|
||||
}
|
||||
sync += Random(1.0, 100.0);
|
||||
return sync;
|
||||
}
|
||||
|
||||
int
|
||||
eu07_application::run() {
|
||||
|
||||
@@ -180,6 +195,7 @@ eu07_application::run() {
|
||||
{
|
||||
if (!m_modes[ m_modestack.top() ]->update())
|
||||
break;
|
||||
simulation::Commands->update();
|
||||
}
|
||||
else if (!Global.network_conf.is_server) {
|
||||
command_queue_client *queue = dynamic_cast<command_queue_client*>(simulation::Commands.get());
|
||||
@@ -187,26 +203,35 @@ eu07_application::run() {
|
||||
do {
|
||||
auto tup = m_network->get_next_delta();
|
||||
delta = std::get<0>(tup);
|
||||
queue->push_server_commands(std::get<1>(tup));
|
||||
m_network->send_commands(queue->pop_queued_commands());
|
||||
Timer::set_delta_override(delta);
|
||||
queue->push_server_commands(std::get<2>(tup));
|
||||
|
||||
m_network->send_commands(queue->pop_queued_commands());
|
||||
|
||||
if (!m_modes[ m_modestack.top() ]->update())
|
||||
break;
|
||||
|
||||
queue->update();
|
||||
|
||||
double sync = generate_sync();
|
||||
if (sync != std::get<1>(tup)) {
|
||||
WriteLog("net: DESYNC!", logtype::net);
|
||||
}
|
||||
}
|
||||
while (delta != 0.0);
|
||||
}
|
||||
else {
|
||||
command_queue_server *queue = dynamic_cast<command_queue_server*>(simulation::Commands.get());
|
||||
queue->push_client_commands(m_network->pop_commands());
|
||||
auto commands = queue->pop_queued_commands();
|
||||
|
||||
queue->push_client_commands(m_network->pop_commands());
|
||||
queue->update();
|
||||
if (!m_modes[ m_modestack.top() ]->update())
|
||||
break;
|
||||
|
||||
queue->update();
|
||||
|
||||
double delta = Timer::GetDeltaTime();
|
||||
m_network->push_delta(delta, commands);
|
||||
m_network->push_delta(delta, generate_sync(), commands);
|
||||
}
|
||||
|
||||
m_taskqueue.update();
|
||||
|
||||
@@ -78,6 +78,7 @@ public:
|
||||
|
||||
void request_train(std::string name);
|
||||
void spawn_train(std::string name);
|
||||
double generate_sync();
|
||||
|
||||
private:
|
||||
// types
|
||||
|
||||
@@ -339,7 +339,6 @@ void command_queue_client::push_server_commands(const commands_map &commands) {
|
||||
for (auto const &kv : commands)
|
||||
for (command_data const &data : kv.second)
|
||||
command_queue::push(data, kv.first);
|
||||
update();
|
||||
}
|
||||
|
||||
// --------------------
|
||||
|
||||
@@ -107,6 +107,13 @@ driver_mode::update() {
|
||||
|
||||
if (deltatime != 0.0)
|
||||
{
|
||||
static uint32_t fcount = 0;
|
||||
fcount ++;
|
||||
if (fcount > 500) {
|
||||
fcount = 0;
|
||||
WriteLog(std::to_string(Random(1.0, 100.0)));
|
||||
}
|
||||
|
||||
// jak pauza, to nie ma po co tego przeliczać
|
||||
simulation::Time.update( deltatime );
|
||||
|
||||
@@ -722,12 +729,14 @@ driver_mode::OnKeyDown(int cKey) {
|
||||
|| ( 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;
|
||||
InOutKey();
|
||||
}
|
||||
|
||||
@@ -378,8 +378,9 @@ material_manager::create( std::string const &Filename, bool const Loadnow ) {
|
||||
// if we have material name and shader it means resource was processed succesfully
|
||||
material.finalize(Loadnow);
|
||||
materialhandle = m_materials.size();
|
||||
m_materialmappings.emplace( material.name, materialhandle );
|
||||
m_materials.emplace_back( std::move(material) );
|
||||
m_materialmappings.emplace( material.name, materialhandle );
|
||||
std::cout << m_materials.size() << std::endl;
|
||||
}
|
||||
else {
|
||||
// otherwise record our failure to process the resource, to speed up subsequent attempts
|
||||
|
||||
@@ -21,14 +21,14 @@ void network::manager::connect()
|
||||
client = std::make_shared<tcp_client>(io_context);
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::manager::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::manager::get_next_delta()
|
||||
{
|
||||
return client->get_next_delta();
|
||||
}
|
||||
|
||||
void network::manager::push_delta(double delta, command_queue::commands_map commands)
|
||||
void network::manager::push_delta(double delta, double sync, command_queue::commands_map commands)
|
||||
{
|
||||
server->push_delta(delta, commands);
|
||||
server->push_delta(delta, sync, commands);
|
||||
}
|
||||
|
||||
command_queue::commands_map network::manager::pop_commands()
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace network
|
||||
void connect();
|
||||
void poll();
|
||||
|
||||
std::tuple<double, command_queue::commands_map> get_next_delta();
|
||||
void push_delta(double delta, command_queue::commands_map commands);
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
void push_delta(double delta, double sync, command_queue::commands_map commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void send_commands(command_queue::commands_map commands);
|
||||
void request_train(std::string name);
|
||||
|
||||
@@ -11,6 +11,21 @@ void network::message::deserialize(std::istream &stream)
|
||||
|
||||
}
|
||||
|
||||
void network::accept_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_uint32(stream, seed);
|
||||
}
|
||||
|
||||
void network::accept_message::deserialize(std::istream &stream)
|
||||
{
|
||||
seed = sn_utils::ld_uint32(stream);
|
||||
}
|
||||
|
||||
size_t network::accept_message::get_size()
|
||||
{
|
||||
return message::get_size() + 4;
|
||||
}
|
||||
|
||||
void::network::command_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_uint32(stream, commands.size());
|
||||
@@ -66,12 +81,13 @@ size_t network::command_message::get_size()
|
||||
|
||||
size_t network::delta_message::get_size()
|
||||
{
|
||||
return command_message::get_size() + 8;
|
||||
return command_message::get_size() + 16;
|
||||
}
|
||||
|
||||
void network::delta_message::serialize(std::ostream &stream)
|
||||
{
|
||||
sn_utils::ls_float64(stream, dt);
|
||||
sn_utils::ls_float64(stream, sync);
|
||||
|
||||
command_message::serialize(stream);
|
||||
}
|
||||
@@ -79,6 +95,7 @@ void network::delta_message::serialize(std::ostream &stream)
|
||||
void network::delta_message::deserialize(std::istream &stream)
|
||||
{
|
||||
dt = sn_utils::ld_float64(stream);
|
||||
sync = sn_utils::ld_float64(stream);
|
||||
|
||||
command_message::deserialize(stream);
|
||||
}
|
||||
@@ -103,7 +120,13 @@ std::shared_ptr<network::message> network::deserialize_message(std::istream &str
|
||||
message::type_e type = (message::type_e)sn_utils::ld_uint16(stream);
|
||||
|
||||
std::shared_ptr<message> msg;
|
||||
if (type == message::STEP_INFO) {
|
||||
if (type == message::CONNECT_ACCEPT) {
|
||||
auto m = std::make_shared<accept_message>();
|
||||
m->type = type;
|
||||
m->deserialize(stream);
|
||||
msg = m;
|
||||
}
|
||||
else if (type == message::STEP_INFO) {
|
||||
auto m = std::make_shared<delta_message>();
|
||||
m->type = type;
|
||||
m->deserialize(stream);
|
||||
|
||||
@@ -26,6 +26,17 @@ namespace network
|
||||
virtual size_t get_size() { return 2; }
|
||||
};
|
||||
|
||||
struct accept_message : public message
|
||||
{
|
||||
accept_message() : message(CONNECT_ACCEPT) {}
|
||||
|
||||
uint32_t seed;
|
||||
|
||||
virtual void serialize(std::ostream &stream) override;
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
virtual size_t get_size() override;
|
||||
};
|
||||
|
||||
struct command_message : public message
|
||||
{
|
||||
command_message() : message(CLIENT_COMMAND) {}
|
||||
@@ -43,6 +54,7 @@ namespace network
|
||||
delta_message() : command_message(STEP_INFO) {}
|
||||
|
||||
double dt;
|
||||
double sync;
|
||||
|
||||
virtual void serialize(std::ostream &stream) override;
|
||||
virtual void deserialize(std::istream &stream) override;
|
||||
|
||||
@@ -9,8 +9,10 @@ void network::connection::connected()
|
||||
{
|
||||
WriteLog("net: socket connected", logtype::net);
|
||||
|
||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
||||
send_message(hello);
|
||||
if (!Global.network_conf.is_server) {
|
||||
std::shared_ptr<message> hello = std::make_shared<message>(message::CONNECT_REQUEST);
|
||||
send_message(hello);
|
||||
}
|
||||
}
|
||||
|
||||
void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
@@ -23,7 +25,8 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
|
||||
if (msg->type == message::CONNECT_REQUEST)
|
||||
{
|
||||
std::shared_ptr<message> reply = std::make_shared<message>(message::CONNECT_ACCEPT);
|
||||
std::shared_ptr<accept_message> reply = std::make_shared<accept_message>();
|
||||
reply->seed = Global.random_seed;
|
||||
|
||||
WriteLog("client accepted", logtype::net);
|
||||
|
||||
@@ -31,7 +34,9 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
}
|
||||
else if (msg->type == message::CONNECT_ACCEPT)
|
||||
{
|
||||
auto cmd = std::dynamic_pointer_cast<accept_message>(msg);
|
||||
WriteLog("accept received", logtype::net);
|
||||
//Global.random_engine.seed(cmd->seed);
|
||||
}
|
||||
else if (msg->type == message::CLIENT_COMMAND)
|
||||
{
|
||||
@@ -57,11 +62,11 @@ void network::connection::message_received(std::shared_ptr<message> &msg)
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::connection::get_next_delta()
|
||||
{
|
||||
if (delta_queue.empty()) {
|
||||
return std::tuple<double,
|
||||
command_queue::commands_map>(0.0, command_queue::commands_map());
|
||||
return std::tuple<double, double,
|
||||
command_queue::commands_map>(0.0, 0.0, command_queue::commands_map());
|
||||
}
|
||||
|
||||
///auto now = std::chrono::high_resolution_clock::now();
|
||||
@@ -76,7 +81,7 @@ std::tuple<double, command_queue::commands_map> network::connection::get_next_de
|
||||
//accum -= remote_dt;
|
||||
|
||||
delta_queue.pop();
|
||||
return std::make_tuple(entry.second->dt, entry.second->commands);
|
||||
return std::make_tuple(entry.second->dt, entry.second->sync, entry.second->commands);
|
||||
//}
|
||||
//else {
|
||||
//return 0.0;
|
||||
@@ -111,10 +116,11 @@ void network::connection::send_message(std::shared_ptr<message> msg)
|
||||
send_data(buf);
|
||||
}
|
||||
|
||||
void network::server::push_delta(double dt, command_queue::commands_map commands)
|
||||
void network::server::push_delta(double dt, double sync, command_queue::commands_map commands)
|
||||
{
|
||||
std::shared_ptr<delta_message> msg = std::make_shared<delta_message>();
|
||||
msg->dt = dt;
|
||||
msg->sync = sync;
|
||||
msg->commands = commands;
|
||||
|
||||
for (auto c : clients)
|
||||
@@ -144,7 +150,7 @@ command_queue::commands_map network::server::pop_commands()
|
||||
return map;
|
||||
}
|
||||
|
||||
std::tuple<double, command_queue::commands_map> network::client::get_next_delta()
|
||||
std::tuple<double, double, command_queue::commands_map> network::client::get_next_delta()
|
||||
{
|
||||
return conn->get_next_delta();
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace network
|
||||
void send_message(std::shared_ptr<message> msg);
|
||||
virtual void connected();
|
||||
|
||||
std::tuple<double, command_queue::commands_map> get_next_delta();
|
||||
std::tuple<double, double, command_queue::commands_map> get_next_delta();
|
||||
command_queue::commands_map pop_commands();
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace network
|
||||
std::vector<std::shared_ptr<connection>> clients;
|
||||
|
||||
public:
|
||||
void push_delta(double dt, command_queue::commands_map commands);
|
||||
void push_delta(double dt, double sync, command_queue::commands_map commands);
|
||||
command_queue::commands_map pop_commands();
|
||||
void notify_train(std::string name);
|
||||
};
|
||||
@@ -54,7 +54,7 @@ namespace network
|
||||
std::shared_ptr<connection> conn;
|
||||
|
||||
public:
|
||||
std::tuple<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 request_train(std::string name);
|
||||
};
|
||||
|
||||
@@ -2853,7 +2853,7 @@ void opengl_renderer::Render_precipitation()
|
||||
{
|
||||
if (Global.Weather == "rain:")
|
||||
// oddly enough random streaks produce more natural looking rain than ones the eye can follow
|
||||
m_precipitationrotation = Random() * 360;
|
||||
m_precipitationrotation = LocalRandom() * 360;
|
||||
else
|
||||
m_precipitationrotation = 0.0;
|
||||
}
|
||||
|
||||
@@ -333,7 +333,7 @@ sound_source::play( int const Flags ) {
|
||||
|
||||
// initialize emitter-specific pitch variation if it wasn't yet set
|
||||
if( m_pitchvariation == 0.f ) {
|
||||
m_pitchvariation = 0.01f * static_cast<float>( Random( 97.5, 102.5 ) );
|
||||
m_pitchvariation = 0.01f * static_cast<float>( LocalRandom( 97.5, 102.5 ) );
|
||||
}
|
||||
/*
|
||||
if( ( ( m_flags & sound_flags::exclusive ) != 0 )
|
||||
|
||||
@@ -109,6 +109,12 @@ double Random(double a, double b)
|
||||
return dis(Global.random_engine);
|
||||
}
|
||||
|
||||
double LocalRandom(double a, double b)
|
||||
{
|
||||
std::uniform_real_distribution<> dis(a, b);
|
||||
return dis(Global.local_random_engine);
|
||||
}
|
||||
|
||||
bool FuzzyLogic(double Test, double Threshold, double Probability)
|
||||
{
|
||||
if ((Test > Threshold) && (!DebugModeFlag))
|
||||
|
||||
11
utilities.h
11
utilities.h
@@ -56,6 +56,7 @@ inline long Round(double const f)
|
||||
}
|
||||
|
||||
double Random(double a, double b);
|
||||
double LocalRandom(double a, double b);
|
||||
|
||||
inline double Random()
|
||||
{
|
||||
@@ -67,6 +68,16 @@ inline double Random(double b)
|
||||
return Random(0.0, b);
|
||||
}
|
||||
|
||||
inline double LocalRandom()
|
||||
{
|
||||
return LocalRandom(0.0,1.0);
|
||||
}
|
||||
|
||||
inline double LocalRandom(double b)
|
||||
{
|
||||
return LocalRandom(0.0, b);
|
||||
}
|
||||
|
||||
inline double BorlandTime()
|
||||
{
|
||||
auto timesinceepoch = std::time( nullptr );
|
||||
|
||||
@@ -27,5 +27,5 @@ void ui::vehiclelist_panel::render_contents() {
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
//ImGui::ShowDemoWindow();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user