mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
feat(event): putvalues Crew:<vehicle> puts an AI driver in a vehicle or takes it off (GE#510) (#422)
* test: scenario runs convert no models in the game dir * feat(vehicle): rules of the crew command of putvalues events Crew:<vehicle> <cab> <flags> (eu07/maszyna#510): reading the command, what gets done in each state of the vehicle, and the state a train is left in when the driver in charge of it is taken off. Core library, with unit tests; nothing calls it yet. * feat(event): putvalues Crew:<vehicle> puts an AI driver in a vehicle or takes it off event <name> putvalues <delay> none 0 0 0 Crew:<vehicle> <cab> <flags> endevent (eu07/maszyna#510): cab 1 or -1 gives the vehicle named an AI driver in cab A or B, the way the scenery creates one, 0 takes the AI driver off and leaves the train with the power off and the brake valve in the emergency position. A vehicle with a user in it is left alone, with a line in the log. The vehicle is named in the text, so the event needs nothing to set it off. Without the command nothing changes. * test: scenario checks on the driver and the speed of a vehicle in the black box * test: scenario cases of the crew event on td.scn A driver appears in cab A and in cab B of a standing locomotive and leaves with it, a train on the run loses its driver and stops (EU07, EN57, Vectron, SM42), a driver is replaced, and the commands which have to be refused change nothing about a timetable run. * feat(event): road vehicles take a driver from the crew command too They have no driver's brake valve, which is what tells a cab in a rail vehicle, and any of them is driven from its own seat. * test: scenario case of the crew event on a car stops_driverless holds until the vehicle gets a driver again, not only to the end of the recording. * fix(event): the crew command clears the driver from every vehicle in its charge A coupler broken under force or in a collision parts the train without clearing the owner of the vehicles parted from it. When the driver was taken off, these kept a pointer to the deleted driver. All vehicles are now looked at, not only the ones still coupled. The log tells how many vehicles the driver let go. * test: scenario case of the crew event on a train parted by a broken coupler A new black box check, parts, tells two vehicles came apart in the run. The case parts an EN57-2000 unit by a hit at 100 km/h and takes its driver off: the driver had the parted car in charge too, and lets it go. * fix(event): one vehicle in the log line of the crew command
This commit is contained in:
134
vehicle/crew.cpp
Normal file
134
vehicle/crew.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "vehicle/crew.h"
|
||||
|
||||
#include "McZapkie/MOVER.h"
|
||||
#include "vehicle/handover.h"
|
||||
|
||||
namespace crew {
|
||||
|
||||
namespace {
|
||||
|
||||
// the scenery parser folds the ASCII letters of the names only, and so does this
|
||||
char lower_ascii(char const Character) {
|
||||
return Character >= 'A' && Character <= 'Z' ? static_cast<char>(Character - 'A' + 'a') : Character;
|
||||
}
|
||||
|
||||
// the controllers of the vehicle to the positions giving no power, and the vehicles it controls told so
|
||||
void cut_power(TMoverParameters &Vehicle) {
|
||||
Vehicle.MainCtrlPos = Vehicle.MainCtrlNoPowerPos();
|
||||
Vehicle.ScndCtrlPos = 0;
|
||||
// what the integrated handle and the cruise control asked for last stays in force until somebody drives
|
||||
Vehicle.eimic = 0.0;
|
||||
Vehicle.eimic_real = 0.0;
|
||||
Vehicle.SpeedCtrlValue = 0.0;
|
||||
Vehicle.SpeedCtrlUnit.IsActive = false;
|
||||
Vehicle.SendCtrlToNext("MainCtrl", Vehicle.MainCtrlPosForCoupled(), Vehicle.CabActive);
|
||||
Vehicle.SendCtrlToNext("ScndCtrl", Vehicle.ScndCtrlPos, Vehicle.CabActive);
|
||||
Vehicle.SendCtrlToNext("EIMIC", 0.0, Vehicle.CabActive);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool is_command(std::string_view const Text) {
|
||||
return Text.starts_with(command_prefix);
|
||||
}
|
||||
|
||||
request parse(std::string_view const Text, double const Cab, double const Flags) {
|
||||
request result;
|
||||
if (false == is_command(Text)) {
|
||||
return result;
|
||||
}
|
||||
result.vehicle = Text.substr(command_prefix.size());
|
||||
std::ranges::transform(result.vehicle, result.vehicle.begin(), lower_ascii);
|
||||
result.cab_valid = Cab == 1.0 || Cab == -1.0 || Cab == 0.0;
|
||||
if (result.cab_valid) {
|
||||
result.cab = static_cast<int>(Cab);
|
||||
}
|
||||
result.flags = static_cast<int>(std::clamp(Flags, -65535.0, 65535.0));
|
||||
return result;
|
||||
}
|
||||
|
||||
action decide(request const &Request, vehicle_state const &State) {
|
||||
using enum action;
|
||||
if (false == Request.cab_valid) {
|
||||
return refuse_cab;
|
||||
}
|
||||
if (State.occupied || (State.has_driver && false == State.driver_is_ai)) {
|
||||
return refuse_user;
|
||||
}
|
||||
if (Request.cab == 0) {
|
||||
return State.has_driver ? remove : nothing;
|
||||
}
|
||||
if (State.has_driver) {
|
||||
return refuse_crewed;
|
||||
}
|
||||
if (false == State.has_cab) {
|
||||
return refuse_no_cab;
|
||||
}
|
||||
if (State.consist_driven) {
|
||||
return refuse_consist_driven;
|
||||
}
|
||||
return Request.cab > 0 ? add_front : add_rear;
|
||||
}
|
||||
|
||||
bool changes_crew(action const Action) {
|
||||
return Action == action::add_front || Action == action::add_rear || Action == action::remove;
|
||||
}
|
||||
|
||||
std::string_view describe(action const Action) {
|
||||
using enum action;
|
||||
switch (Action) {
|
||||
case add_front:
|
||||
return "an AI driver takes cab A";
|
||||
case add_rear:
|
||||
return "an AI driver takes cab B";
|
||||
case remove:
|
||||
return "the AI driver leaves";
|
||||
case nothing:
|
||||
return "no driver to take off";
|
||||
case refuse_cab:
|
||||
return "refused, the cab has to be 1, -1 or 0";
|
||||
case refuse_user:
|
||||
return "refused, a user is in the vehicle";
|
||||
case refuse_crewed:
|
||||
return "refused, the vehicle has a driver already";
|
||||
case refuse_no_cab:
|
||||
return "refused, the vehicle has no cab";
|
||||
case refuse_consist_driven:
|
||||
return "refused, the consist has a driver in charge in another vehicle";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void secure(TMoverParameters &Occupied, TMoverParameters &Controlling) {
|
||||
handover::release_ai_controls(Occupied, Controlling);
|
||||
cut_power(Controlling);
|
||||
if (&Occupied != &Controlling) {
|
||||
cut_power(Occupied);
|
||||
}
|
||||
// the emergency position vents the brake pipe whatever the brake mode, and with nobody in the cab too
|
||||
Occupied.BrakeLevelSet(Occupied.Handle->GetPos(bh_EB));
|
||||
// a vehicle with no train brake (a car, a draisine) gets the brake its driver stops it with, in full
|
||||
if (Occupied.BrakeSystem == TBrakeSystem::Individual) {
|
||||
if (Occupied.LocalBrake == TLocalBrake::ManualBrake) {
|
||||
Occupied.IncManualBrakeLevel(ManualBrakePosNo);
|
||||
}
|
||||
else {
|
||||
Occupied.IncLocalBrakeLevel(LocalBrakePosNo);
|
||||
}
|
||||
}
|
||||
Occupied.CabDeactivisation();
|
||||
Occupied.CabOccupied = 0;
|
||||
}
|
||||
|
||||
} // namespace crew
|
||||
82
vehicle/crew.h
Normal file
82
vehicle/crew.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
class TMoverParameters;
|
||||
|
||||
// a driver put in a vehicle, or taken off it, while the scenario runs (eu07/maszyna#510): the command of a
|
||||
// putvalues event
|
||||
// event <name> putvalues <delay> none 0 0 0 Crew:<vehicle> <cab> <flags> endevent
|
||||
// with the name of the vehicle in the text, so the event works whatever sets it off (a key, an event
|
||||
// launcher), the cab in the first value: 1 an AI driver in cab A, -1 in cab B, 0 nobody, and the second value
|
||||
// reserved (0). "Crew:" with no name is for the vehicle which set the event off.
|
||||
// here are the parts which need no vehicle of the game: reading the command, deciding what to do about it,
|
||||
// and the state a train is left in when its driver goes. the game's side of it is in crewchange.h
|
||||
namespace crew {
|
||||
|
||||
// what the text of the command starts with, in this very case (the commands of the events are case sensitive)
|
||||
inline constexpr std::string_view command_prefix{"Crew:"};
|
||||
|
||||
// whether the text of a putvalues event is the crew command
|
||||
bool is_command(std::string_view Text);
|
||||
|
||||
// the command as the event gives it
|
||||
struct request {
|
||||
std::string vehicle; // as the scenery keeps the names, in lower case; empty: the vehicle which set the event off
|
||||
int cab{0}; // 1 a driver in cab A (the front one), -1 in cab B, 0 nobody
|
||||
bool cab_valid{false}; // the value was one of the three
|
||||
int flags{0}; // reserved, nothing reads it yet
|
||||
};
|
||||
|
||||
// reads the command from the text of the event and its two values
|
||||
request parse(std::string_view Text, double Cab, double Flags);
|
||||
|
||||
// what the command finds in the vehicle
|
||||
struct vehicle_state {
|
||||
bool has_cab{false}; // the type of the vehicle has a driver's cab; a road vehicle or a boat counts as having one
|
||||
bool has_driver{false};
|
||||
bool driver_is_ai{false}; // the driver it has is the AI, not a user
|
||||
bool occupied{false}; // a user sits in the vehicle, whoever drives it
|
||||
bool consist_driven{false}; // another vehicle of the consist has the driver in charge of it
|
||||
};
|
||||
|
||||
enum class action {
|
||||
add_front, // an AI driver goes into cab A
|
||||
add_rear, // an AI driver goes into cab B
|
||||
remove, // the AI driver is taken off
|
||||
nothing, // asked to take the driver off a vehicle which has none
|
||||
refuse_cab, // the cab asked for is none of 1, -1, 0
|
||||
refuse_user, // a user is in the vehicle, or drives it
|
||||
refuse_crewed, // asked for a driver where there is one already
|
||||
refuse_no_cab, // asked for a driver in a vehicle with no cab
|
||||
refuse_consist_driven // asked for a driver in a consist which another vehicle's driver is in charge of
|
||||
};
|
||||
|
||||
// what to do about the command. a user in the vehicle is never touched: neither replaced nor left without
|
||||
// the driver the cab of the game works with
|
||||
action decide(request const &Request, vehicle_state const &State);
|
||||
|
||||
// whether the action changes anything
|
||||
bool changes_crew(action Action);
|
||||
|
||||
// the action in words, for the log
|
||||
std::string_view describe(action Action);
|
||||
|
||||
// the state a train is left in when the driver in charge of it goes: what the AI held is let go (as when it
|
||||
// hands over to a user, see handover.h), the power is off, the driver's brake valve is in the emergency position,
|
||||
// so a train on the move stops and stays braked, and the cab is neither active nor occupied. the engine, the
|
||||
// pantographs and the lights stay as they were. Occupied is the vehicle of the driver, Controlling the one whose
|
||||
// motors it drove (the AI's mvOccupied and mvControlling; the same one in a locomotive)
|
||||
void secure(TMoverParameters &Occupied, TMoverParameters &Controlling);
|
||||
|
||||
} // namespace crew
|
||||
138
vehicle/crewchange.cpp
Normal file
138
vehicle/crewchange.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "vehicle/crewchange.h"
|
||||
|
||||
#include "simulation/simulation.h"
|
||||
#include "utilities/Logs.h"
|
||||
#include "vehicle/Driver.h"
|
||||
#include "vehicle/DynObj.h"
|
||||
#include "vehicle/Train.h"
|
||||
#include "vehicle/handover.h"
|
||||
|
||||
namespace crew {
|
||||
|
||||
namespace {
|
||||
|
||||
// TMoverParameters::CategoryFlag of the rail vehicles
|
||||
constexpr int rail_category{1};
|
||||
// more vehicles than a consist has, for a walk along one whose couplers form a loop
|
||||
constexpr int consist_walk_limit{300};
|
||||
|
||||
// calls the function for each vehicle of the consist, from one end of it to the other. goes by the couplers
|
||||
// alone, as the driver's own check of the consist does, so it works for a consist nobody has put in order
|
||||
template <typename Function_>
|
||||
void for_each_in_consist(TDynamicObject &Vehicle, Function_ const Function) {
|
||||
auto side{static_cast<int>(end::front)};
|
||||
auto *vehicle{Vehicle.FirstFind(side)};
|
||||
side = 1 - side;
|
||||
auto count{0};
|
||||
while (vehicle != nullptr && count < consist_walk_limit) {
|
||||
Function(*vehicle);
|
||||
vehicle = vehicle->Neighbour(side);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
// whether another vehicle of the consist has the driver in charge of it
|
||||
bool is_consist_driven(TDynamicObject &Vehicle) {
|
||||
auto driven{false};
|
||||
for_each_in_consist(Vehicle, [&Vehicle, &driven](TDynamicObject const &Other) {
|
||||
if (&Other != &Vehicle && Other.Mechanik != nullptr && Other.Mechanik->primary()) {
|
||||
driven = true;
|
||||
}
|
||||
});
|
||||
return driven;
|
||||
}
|
||||
|
||||
vehicle_state state_of(TDynamicObject &Vehicle) {
|
||||
vehicle_state state;
|
||||
// a rail vehicle has a cab when it has the driver's brake valve. the others (cars, boats) have no such valve
|
||||
// to tell by, and each of them is driven from its own seat
|
||||
state.has_cab = Vehicle.MoverParameters->HasDriverCab() || Vehicle.MoverParameters->CategoryFlag != rail_category;
|
||||
state.has_driver = Vehicle.Mechanik != nullptr;
|
||||
state.driver_is_ai = state.has_driver && Vehicle.Mechanik->AIControllFlag;
|
||||
// a cab of the game is bound to the vehicle: the user's own, or another user's in a network game. the cab
|
||||
// reads the driver of its vehicle all the time, so that driver stays
|
||||
state.occupied = simulation::Trains.find(Vehicle.name()) != nullptr;
|
||||
state.consist_driven = is_consist_driven(Vehicle);
|
||||
return state;
|
||||
}
|
||||
|
||||
// an AI driver for the vehicle, as the scenery gives one to a vehicle of its own outside a trainset: in charge of
|
||||
// the consist, with no timetable, waiting for orders when the vehicle stands and driving on when it rolls
|
||||
void add_driver(TDynamicObject &Vehicle, int const Cab) {
|
||||
Vehicle.MoverParameters->CabOccupied = Cab;
|
||||
Vehicle.Controller = AIdriver;
|
||||
Vehicle.create_controller(Cab > 0 ? "1" : "2", false);
|
||||
}
|
||||
|
||||
// takes the AI driver off. nothing but the vehicle and the vehicles the driver is in charge of points at a driver
|
||||
// (the panels, the events, the network and the black box read it from a vehicle each time), so these are cleared
|
||||
// and the driver deleted. the vehicles in its charge are looked for among all vehicles, not along the couplers:
|
||||
// a coupler broken under force or in a collision (TMoverParameters::damage_coupler()) parts the train and leaves
|
||||
// the vehicles parted from it in the driver's charge, as uncoupling (TDynamicObject::Dettach()) doesn't
|
||||
void remove_driver(TDynamicObject &Vehicle) {
|
||||
auto *driver{Vehicle.Mechanik};
|
||||
auto &occupied{*Vehicle.MoverParameters};
|
||||
auto &controlling{*Vehicle.FindPowered()->MoverParameters};
|
||||
if (driver->primary()) {
|
||||
// the train this driver was in charge of stops and stays braked
|
||||
secure(occupied, controlling);
|
||||
}
|
||||
else {
|
||||
// somebody else drives the train, so only what this one held is let go
|
||||
handover::release_ai_controls(occupied, controlling);
|
||||
occupied.CabOccupied = 0;
|
||||
}
|
||||
Vehicle.Mechanik = nullptr;
|
||||
auto released{0};
|
||||
for (auto *other : simulation::Vehicles.sequence()) {
|
||||
if (other->ctOwner == driver) {
|
||||
other->ctOwner = nullptr;
|
||||
++released;
|
||||
}
|
||||
}
|
||||
WriteLog("Crew: [" + Vehicle.name() + "] the driver was in charge of " + std::to_string(released) + (released == 1 ? " vehicle" : " vehicles"));
|
||||
delete driver;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void change(request const &Request, TDynamicObject const *Activator) {
|
||||
std::string const name{Request.vehicle.empty() && Activator != nullptr ? Activator->name() : Request.vehicle};
|
||||
if (name.empty()) {
|
||||
ErrorLog("Bad event: \"" + std::string{command_prefix} + "\" names no vehicle and no vehicle set the event off");
|
||||
return;
|
||||
}
|
||||
auto *vehicle{simulation::Vehicles.find(name)};
|
||||
if (vehicle == nullptr || false == vehicle->bEnabled) {
|
||||
ErrorLog("Bad event: \"" + std::string{command_prefix} + name + "\" can't find the vehicle");
|
||||
return;
|
||||
}
|
||||
if (Request.flags != 0) {
|
||||
WriteLog("Crew: [" + name + "] flags " + std::to_string(Request.flags) + " ignored, none is defined yet");
|
||||
}
|
||||
auto const decision{decide(Request, state_of(*vehicle))};
|
||||
WriteLog("Crew: [" + name + "] " + std::string{describe(decision)});
|
||||
switch (decision) {
|
||||
case action::add_front:
|
||||
case action::add_rear:
|
||||
add_driver(*vehicle, Request.cab);
|
||||
break;
|
||||
case action::remove:
|
||||
remove_driver(*vehicle);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace crew
|
||||
24
vehicle/crewchange.h
Normal file
24
vehicle/crewchange.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "vehicle/crew.h"
|
||||
|
||||
class TDynamicObject;
|
||||
|
||||
namespace crew {
|
||||
|
||||
// carries out the crew command of an event (see crew.h) on the vehicle it names, or on Activator, the vehicle
|
||||
// which set the event off, when it names none: puts an AI driver in, the way the scenery does for a vehicle of
|
||||
// its own with a driver, or takes the AI driver off and leaves the train secured. whatever it does or refuses
|
||||
// goes to the log. called from the event queue, between the updates of the vehicles
|
||||
void change(request const &Request, TDynamicObject const *Activator);
|
||||
|
||||
} // namespace crew
|
||||
Reference in New Issue
Block a user