From 7261fe9fbd5e4e58c5a738aa2171c8a337459aa0 Mon Sep 17 00:00:00 2001 From: milek7 Date: Wed, 10 Apr 2019 10:45:22 +0200 Subject: [PATCH] ai alert mode --- Driver.cpp | 76 ++++++++++++++++++++++++++++++++++------- Driver.h | 11 +++++- command.cpp | 5 ++- command.h | 3 ++ simulation.cpp | 19 +++++++++++ widgets/map.cpp | 30 ++++++++++++++++ widgets/map.h | 11 ++++++ widgets/map_objects.cpp | 25 ++++++++++++++ widgets/map_objects.h | 6 ++++ 9 files changed, 172 insertions(+), 14 deletions(-) diff --git a/Driver.cpp b/Driver.cpp index 8b31289d..2c9038bf 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -4045,6 +4045,20 @@ bool TController::PutCommand( std::string NewCommand, double NewValue1, double N return true; } + if (NewCommand == "SetSignal") { + TSignals signal = (TSignals)std::round(NewValue1); + + mvOccupied->iLights[0] = 0; + mvOccupied->iLights[1] = 0; + mvOccupied->WarningSignal = 0; + + for (int i = Signal_START; i <= Signal_MAX; i++) + iDrivigFlags &= ~(1 << i); + + if (NewValue2 > 0.5) + iDrivigFlags |= 1 << (uint32_t)signal; + } + return false; // nierozpoznana - wysłać bezpośrednio do pojazdu }; @@ -4413,18 +4427,56 @@ TController::UpdateSituation(double dt) { } } - // horn control - if( fWarningDuration > 0.0 ) { - // jeśli pozostało coś do wytrąbienia trąbienie trwa nadal - fWarningDuration -= dt; - if( fWarningDuration < 0.05 ) - mvOccupied->WarningSignal = 0; // a tu się kończy - } - if( mvOccupied->Vel >= 5.0 ) { - // jesli jedzie, można odblokować trąbienie, bo się wtedy nie włączy - iDrivigFlags &= ~moveStartHornDone; // zatrąbi dopiero jak następnym razem stanie - iDrivigFlags |= moveStartHorn; // i trąbić przed następnym ruszeniem - } + if (iDrivigFlags & (1 << Signal_A1)) { + if (fWarningDuration < 2.0) + mvOccupied->WarningSignal = 1; + else if (fWarningDuration < 2.5) + mvOccupied->WarningSignal = 0; + else if (fWarningDuration < 3.0) + mvOccupied->WarningSignal = 1; + else if (fWarningDuration < 3.5) + mvOccupied->WarningSignal = 0; + else if (fWarningDuration < 4.0) + mvOccupied->WarningSignal = 1; + else if (fWarningDuration < 4.5) + mvOccupied->WarningSignal = 0; + else if (fWarningDuration < 5.0) + mvOccupied->WarningSignal = 1; + else if (fWarningDuration < 7.0) + mvOccupied->WarningSignal = 0; + else + fWarningDuration = 0.0; + + if (std::fmod(fWarningDuration, 1.0) < 0.5) { + mvOccupied->iLights[0] = headlight_right | headlight_left; + mvOccupied->iLights[1] = headlight_right | headlight_left; + } + else { + mvOccupied->iLights[0] = 0; + mvOccupied->iLights[1] = 0; + } + + fWarningDuration += dt; + } + else if (iDrivigFlags & (1 << Signal_Pc6)) { + mvOccupied->WarningSignal = 0; + mvOccupied->iLights[0] = redmarker_right | redmarker_left | headlight_upper; + mvOccupied->iLights[1] = redmarker_right | redmarker_left | headlight_upper; + } + else { + // horn control + if( fWarningDuration > 0.0 ) { + // jeśli pozostało coś do wytrąbienia trąbienie trwa nadal + fWarningDuration -= dt; + if( fWarningDuration < 0.05 ) + mvOccupied->WarningSignal = 0; // a tu się kończy + } + if( mvOccupied->Vel >= 5.0 ) { + // jesli jedzie, można odblokować trąbienie, bo się wtedy nie włączy + iDrivigFlags &= ~moveStartHornDone; // zatrąbi dopiero jak następnym razem stanie + iDrivigFlags |= moveStartHorn; // i trąbić przed następnym ruszeniem + } + } if( ( true == TestFlag( iDrivigFlags, moveStartHornNow ) ) && ( true == Ready ) diff --git a/Driver.h b/Driver.h index 68f9b6d9..1e4e5aed 100644 --- a/Driver.h +++ b/Driver.h @@ -30,7 +30,16 @@ enum TOrders Obey_train = 1 << 7, // tryb pociągowy Bank = 1 << 8, // assist mode // others - Jump_to_first_order = 1 << 9 // zapęlenie do pierwszej pozycji (po co?) + Jump_to_first_order = 1 << 9, // zapęlenie do pierwszej pozycji (po co?) +}; + +// TSignals is shifted into TMovementStatus +enum TSignals +{ + Signal_START = 30, + Signal_Pc6 = 30, + Signal_A1 = 31, + Signal_MAX = 31 }; enum TMovementStatus diff --git a/command.cpp b/command.cpp index 103ef323..bc5bc728 100644 --- a/command.cpp +++ b/command.cpp @@ -253,7 +253,10 @@ commanddescription_sequence Commands_descriptions = { { "trainsetmove", command_target::simulation, command_mode::oneoff }, { "consistteleport", command_target::simulation, command_mode::oneoff }, { "pullalarmchain", command_target::simulation, command_mode::oneoff }, - { "notifyquit", command_target::simulation, command_mode::oneoff }, + { "sendaicommand", command_target::simulation, command_mode::oneoff }, + { "spawntrainset", command_target::simulation, command_mode::oneoff }, + { "destroytrainset", command_target::simulation, command_mode::oneoff }, + { "quitsimulation", command_target::simulation, command_mode::oneoff }, }; } // simulation diff --git a/command.h b/command.h index 05e715bf..0e1e9d67 100644 --- a/command.h +++ b/command.h @@ -247,6 +247,9 @@ enum class user_command { dynamicmove, consistteleport, pullalarmchain, + sendaicommand, + spawntrainset, + destroytrainset, quitsimulation, diff --git a/simulation.cpp b/simulation.cpp index bf2f5764..ad8aade2 100644 --- a/simulation.cpp +++ b/simulation.cpp @@ -24,6 +24,7 @@ http://mozilla.org/MPL/2.0/. #include "scene.h" #include "Train.h" #include "application.h" +#include "Driver.h" namespace simulation { @@ -263,12 +264,30 @@ void state_manager::process_commands() { } } + if (commanddata.command == user_command::spawntrainset) { + + } + if (commanddata.command == user_command::pullalarmchain) { TDynamicObject *vehicle = simulation::Vehicles.find(commanddata.payload); if (vehicle) vehicle->MoverParameters->AlarmChainSwitch(true); } + if (commanddata.command == user_command::sendaicommand) { + std::istringstream ss(commanddata.payload); + + std::string vehicle_name; + std::string command; + std::getline(ss, vehicle_name, '%'); + std::getline(ss, command, '%'); + + TDynamicObject *vehicle = simulation::Vehicles.find(vehicle_name); + glm::dvec3 location = commanddata.location; + if (vehicle && vehicle->Mechanik) + vehicle->Mechanik->PutCommand(command, commanddata.param1, commanddata.param2, &location); + } + if (commanddata.command == user_command::quitsimulation) { Application.queue_quit(); } diff --git a/widgets/map.cpp b/widgets/map.cpp index 8d1ba27e..fd69cb0a 100644 --- a/widgets/map.cpp +++ b/widgets/map.cpp @@ -317,6 +317,10 @@ void ui::handle_map_object_click(ui_panel &parent, std::shared_ptr(parent, std::move(obstacle))); } + else if (auto obstacle = std::dynamic_pointer_cast(obj)) + { + parent.register_popup(std::make_unique(parent, std::move(obstacle))); + } } void ui::handle_map_object_hover(std::shared_ptr &obj) @@ -538,3 +542,29 @@ void ui::obstacle_remove_window::render_content() ImGui::CloseCurrentPopup(); } } + +ui::vehicle_click_window::vehicle_click_window(ui_panel &panel, std::shared_ptr &&obstacle) + : popup(panel), m_obstacle(obstacle) { } + +void ui::vehicle_click_window::render_content() +{ + std::string name = m_obstacle->name + "%" + "SetSignal"; + + if (ImGui::Button(u8"wyłącz")) { + m_relay.post(user_command::sendaicommand, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(), &name); + + ImGui::CloseCurrentPopup(); + } + if (ImGui::Button(u8"Pc6")) { + std::string name = m_obstacle->name + "%" + "SetSignal"; + m_relay.post(user_command::sendaicommand, Signal_Pc6, 1.0, GLFW_PRESS, 0, glm::vec3(), &name); + + ImGui::CloseCurrentPopup(); + } + if (ImGui::Button(u8"A1")) { + std::string name = m_obstacle->name + "%" + "SetSignal"; + m_relay.post(user_command::sendaicommand, Signal_A1, 1.0, GLFW_PRESS, 0, glm::vec3(), &name); + + ImGui::CloseCurrentPopup(); + } +} diff --git a/widgets/map.h b/widgets/map.h index be3c8b20..47f1c63f 100644 --- a/widgets/map.h +++ b/widgets/map.h @@ -65,6 +65,17 @@ class obstacle_remove_window : public popup virtual void render_content() override; }; +class vehicle_click_window : public popup +{ + std::shared_ptr m_obstacle; + command_relay m_relay; + + public: + vehicle_click_window(ui_panel &panel, std::shared_ptr &&obstacle); + + virtual void render_content() override; +}; + class map_panel : public ui_panel { std::unique_ptr m_track_shader; diff --git a/widgets/map_objects.cpp b/widgets/map_objects.cpp index 10709c65..b6d47bcb 100644 --- a/widgets/map_objects.cpp +++ b/widgets/map_objects.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" #include "widgets/map_objects.h" +#include "simulation.h" +#include "DynObj.h" map::objects map::Objects; @@ -27,5 +29,28 @@ map::sorted_object_list map::objects::find_in_range(glm::vec3 from, float distan } } + for (TDynamicObject *dynobj : simulation::Vehicles.sequence()) { + if (!dynobj->Controller || dynobj->Prev()) + continue; + + glm::vec3 entry_location = dynobj->GetPosition(); + glm::vec3 search_point = from; + + if (glm::isnan(from.y)) + { + entry_location.y = 0.0f; + search_point.y = 0.0f; + } + + float dist = glm::distance2(entry_location, search_point); + if (dist < max_distance2) + { + auto entry = std::make_shared(); + entry->dynobj = dynobj; + entry->name = dynobj->name(); + items.emplace(dist, std::move(entry)); + } + } + return items; } diff --git a/widgets/map_objects.h b/widgets/map_objects.h index b37cb986..f693f588 100644 --- a/widgets/map_objects.h +++ b/widgets/map_objects.h @@ -59,6 +59,12 @@ struct obstacle : public map_object } }; +// vehicle wrapper for map display +struct vehicle : public map_object +{ + TDynamicObject *dynobj = nullptr; +}; + struct objects { std::vector> entries;