18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 00:22:42 +01:00

Merge pull request #559 from jakubg1/feat/lua-spawn-trainset

feat(lua): api.spawn_trainset() places trainsets from scenery scripts
This commit is contained in:
2026-11-30 18:57:35 +00:00
committed by GitHub
6 changed files with 193 additions and 0 deletions
+9
View File
@@ -30,6 +30,7 @@ lua::lua()
{"isolated_isoccupied_n", scriptapi_isolated_isoccupied_n},
{"train_getname", scriptapi_train_getname},
{"dynobj_putvalues", scriptapi_dynobj_putvalues},
{"spawn_trainset", scriptapi_spawn_trainset},
{"memcell_find", scriptapi_memcell_find},
{"memcell_read", scriptapi_memcell_read},
{"memcell_read_n", scriptapi_memcell_read_n},
@@ -296,6 +297,14 @@ int lua::scriptapi_dynobj_putvalues(lua_State *L)
return 0;
}
/// Places the trainsets of a definition written as in a scenery file (`trainset ... endtrainset`), for example one copied
/// from the scenario starter. They're placed at the start of the next simulation update, and the log says why if they can't be.
int lua::scriptapi_spawn_trainset(lua_State *L)
{
simulation::State.queue_trainsets(luaL_checkstring(L, 1));
return 0;
}
int lua::scriptapi_memcell_find(lua_State *L)
{
std::string str = lua_tostring(L, 1);
+1
View File
@@ -27,6 +27,7 @@ class lua
static int scriptapi_isolated_isoccupied_n(lua_State *L);
static int scriptapi_train_getname(lua_State *L);
static int scriptapi_dynobj_putvalues(lua_State *L);
static int scriptapi_spawn_trainset(lua_State *L);
static int scriptapi_memcell_find(lua_State *L);
static int scriptapi_memcell_read(lua_State *L);
static int scriptapi_memcell_read_n(lua_State *L);
+25
View File
@@ -36,6 +36,9 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Logs.h"
#include "vehicle/Driver.h"
#include <format>
#include <utility>
namespace simulation {
state_manager State;
@@ -121,6 +124,9 @@ 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; }
// ahead of everything which goes through the vehicles, as the added ones change their list
place_queued_trainsets();
auto const totaltime { Deltatime * Iterationcount };
// NOTE: we perform animations first, as they can determine factors like contact with powergrid
TAnimModel::AnimUpdate( totaltime ); // wykonanie zakolejkowanych animacji
@@ -132,6 +138,25 @@ state_manager::update( double const Deltatime, int Iterationcount ) {
simulation::Vehicles.update( Deltatime, Iterationcount );
}
void
state_manager::queue_trainsets( std::string Definition ) {
m_queuedtrainsets.emplace_back( std::move( Definition ) );
}
void
state_manager::place_queued_trainsets() {
if( true == m_queuedtrainsets.empty() ) { return; }
// taken out of the queue first, so that what placing the vehicles leads to can queue more for the next update
auto const queued { std::exchange( m_queuedtrainsets, {} ) };
for( auto const &definition : queued ) {
if( auto const vehicles { m_serializer.insert_trainsets( definition ) }; false == vehicles.empty() ) {
WriteLog( std::format( "Placed {} vehicle(s) of the trainsets given while the simulation runs, the first of them \"{}\"", vehicles.size(), vehicles.front()->name() ) );
}
}
}
void
state_manager::update_clocks() {
+9
View File
@@ -43,6 +43,10 @@ public:
// places the vehicles, given as their node statements, as a trainset on the path. returns: the vehicles made
std::vector<TDynamicObject *>
insert_trainset(std::string const &Name, TTrack *Path, double const Offset, std::string const &Vehicles, bool const Reversed = false);
// places the trainsets of the definition, written as in a scenery file (trainset ... endtrainset), at the start of the next
// update of the simulation. a script can ask for it while the vehicles are being updated, or while the scenery loads
void
queue_trainsets(std::string Definition);
// create eventlauncher from node string
TEventLauncher *
create_eventlauncher(const std::string &src, const std::string &name, const glm::dvec3 &position);
@@ -74,8 +78,13 @@ public:
export_as_text( std::string const &Scenariofile ) const;
private:
// methods
// places the trainsets queued since the last update
void
place_queued_trainsets();
// members
state_serializer m_serializer;
std::vector<std::string> m_queuedtrainsets; // definitions of the trainsets to be placed at the next update
struct {
std::shared_ptr<TMemCell> weather, time, date;
} m_scriptinginterface;
+143
View File
@@ -37,7 +37,11 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/utilities.h"
#include "editor/editorTerrainStreamer.hpp"
#include <algorithm>
#include <format>
#include <iterator>
#include <string_view>
#include <unordered_set>
namespace simulation {
@@ -49,6 +53,113 @@ double vehicle_length( std::string const &Folder, std::string const &Type ) {
return probe.LoadFIZ( paths::dynamic + Folder + "/" ) ? probe.Dim.L : 0.0;
}
// checks the definition of trainsets to be placed while the simulation runs. it can hold only what a scenery file has
// from "trainset" to "endtrainset", each trainset on a track which exists, with vehicles of names not in use yet
class trainset_check {
public:
explicit trainset_check( std::string const &Definition ) :
m_input( Definition, cParser::buffer_TEXT, Global.asCurrentSceneryPath, Global.bLoadTraction )
{}
// returns: what keeps the definition from being placed, or nothing if it can be
std::string fault() {
auto token { m_input.getToken<std::string>() };
while( false == token.empty() && true == m_fault.empty() ) {
statement( token );
token = m_input.getToken<std::string>();
}
if( false == m_fault.empty() ) { return m_fault; }
if( true == m_open ) { return "the last trainset doesn't end with \"endtrainset\""; }
if( m_trainsets == 0 ) { return "no trainset is defined"; }
return {};
}
private:
void statement( std::string const &Token ) {
if( Token == "trainset" ) { header(); }
else if( Token == "reversed" ) { reversed(); }
else if( Token == "assignment" ) { assignment(); }
else if( Token == "node" ) { vehicle(); }
else if( Token == "endtrainset" ) { close(); }
else { m_fault = std::format( "unexpected token \"{}\"", Token ); }
}
void header() {
if( true == m_open ) {
m_fault = "a trainset begins before the previous one ends";
return;
}
std::string name;
std::string track;
m_input.getTokens( 4 );
m_input >> name >> track;
if( simulation::Paths.find( track ) == nullptr ) {
m_fault = std::format( "track \"{}\" of trainset \"{}\" doesn't exist", track, name );
return;
}
m_open = true;
m_vehicles = 0;
}
void reversed() {
if( false == m_open || m_vehicles > 0 ) {
m_fault = "\"reversed\" has to follow the trainset header, ahead of its vehicles";
}
}
void assignment() {
if( false == m_open ) {
m_fault = "\"assignment\" outside of a trainset";
return;
}
skip_until( "endassignment" );
}
void vehicle() {
if( false == m_open ) {
m_fault = "a node outside of a trainset";
return;
}
std::string rangemax;
std::string rangemin;
std::string name;
std::string type;
m_input.getTokens( 4 );
m_input >> rangemax >> rangemin >> name >> type;
if( type != "dynamic" ) {
m_fault = std::format( "node \"{}\" of type \"{}\", only vehicles can be placed", name, type );
return;
}
skip_until( "enddynamic" );
++m_vehicles;
if( name.empty() || name == "none" ) { return; }
if( simulation::Vehicles.find( name ) != nullptr || false == m_names.insert( name ).second ) {
m_fault = std::format( "vehicle name \"{}\" is in use", name );
}
}
void close() {
if( false == m_open ) {
m_fault = "\"endtrainset\" outside of a trainset";
return;
}
if( m_vehicles == 0 ) {
m_fault = "a trainset without vehicles";
return;
}
m_open = false;
++m_trainsets;
}
void skip_until( std::string_view Token ) {
auto token { m_input.getToken<std::string>() };
while( false == token.empty() && token != Token ) {
token = m_input.getToken<std::string>();
}
}
cParser m_input;
std::string m_fault;
bool m_open { false };
int m_vehicles { 0 };
int m_trainsets { 0 };
std::unordered_set<std::string> m_names;
};
} // namespace
std::shared_ptr<deserializer_state>
@@ -1095,6 +1206,12 @@ state_serializer::deserialize_trainset( cParser &Input, scene::scratch_data &Scr
}
}
open_trainset( Input, Scratchpad );
}
void
state_serializer::open_trainset( cParser &Input, scene::scratch_data &Scratchpad ) {
if( true == Scratchpad.trainset.is_open ) {
// shouldn't happen but if it does wrap up currently open trainset and report an error
deserialize_endtrainset( Input, Scratchpad );
@@ -1930,6 +2047,32 @@ std::vector<TDynamicObject *> state_serializer::insert_trainset(std::string cons
return vehicles;
}
std::vector<TDynamicObject *> state_serializer::insert_trainsets(std::string const &Definition) {
// checked in full first, so that nothing is placed when a part of it can't be
if (auto const fault { trainset_check(Definition).fault() }; false == fault.empty()) {
ErrorLog(std::format("Bad trainset: {}, the trainsets given while the simulation runs weren't placed", fault));
return {};
}
scene::scratch_data scratch;
cParser parser(Definition, cParser::buffer_TEXT, Global.asCurrentSceneryPath, Global.bLoadTraction);
std::vector<TDynamicObject *> vehicles;
auto token { parser.getToken<std::string>() };
while (false == token.empty()) {
// the check leaves nothing else to come across. the header is read without the overrides of the scenario
// starter, which go by the line of the scenario file
if (token == "trainset") { open_trainset(parser, scratch); }
else if (token == "reversed") { deserialize_reversed(parser, scratch); }
else if (token == "assignment") { deserialize_assignment(parser, scratch); }
else if (token == "node") { deserialize_node(parser, scratch); }
else if (token == "endtrainset") {
std::ranges::copy(scratch.trainset.vehicles, std::back_inserter(vehicles));
deserialize_endtrainset(parser, scratch);
}
token = parser.getToken<std::string>();
}
return vehicles;
}
std::pair<int, int> state_serializer::preview_include(std::string const &Directive, scene::layer_context const &Context, scene::layer_handle Layer, scene::instance_handle Instance) {
// statements which take more than a single token, with the tokens ending them
static std::unordered_map<std::string, std::string> const nodeends {
+6
View File
@@ -46,6 +46,10 @@ public:
TAnimModel * create_model(std::string const &src, std::string const &name, const glm::dvec3 &position);
// places the vehicles, given as their node statements, as a trainset on the path, while the simulation runs. returns: the vehicles made
std::vector<TDynamicObject *> insert_trainset(std::string const &Name, TTrack *Path, double const Offset, std::string const &Vehicles, bool const Reversed = false);
// places the trainsets of the definition, written as in a scenery file (trainset ... endtrainset), while the simulation runs.
// nothing is placed if the definition holds anything else, or names a track which doesn't exist or a vehicle name in use.
// returns: the vehicles made
std::vector<TDynamicObject *> insert_trainsets(std::string const &Definition);
// create new eventlauncher from node stirng
TEventLauncher * create_eventlauncher(std::string const &src, std::string const &name, const glm::dvec3 &position);
// creates model instances defined by an include directive placed or changed in the scenery editor, to show the outcome.
@@ -79,6 +83,8 @@ private:
void deserialize_test( cParser &Input, scene::scratch_data &Scratchpad );
void deserialize_time( cParser &Input, scene::scratch_data &Scratchpad );
void deserialize_trainset( cParser &Input, scene::scratch_data &Scratchpad );
// opens the trainset whose header follows in the input
void open_trainset( cParser &Input, scene::scratch_data &Scratchpad );
void deserialize_terrain( cParser &Input, scene::scratch_data &Scratchpad );
void deserialize_heightmapterrain( cParser &Input, scene::scratch_data &Scratchpad );
void deserialize_editorterrain( cParser &Input, scene::scratch_data &Scratchpad );