mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 18:47:06 +01:00
feat(lua): api.spawn_trainset() places trainsets from scenery scripts
A scenery Lua script can place trainsets while the simulation runs, with spawn_trainset(definition) in eu07.events. The definition is written as in a scenery file, from "trainset" to "endtrainset", e.g. as copied from the scenario starter; it can hold more than one trainset. - The trainsets are queued and placed at the start of the next simulation update, ahead of everything that goes through the vehicles. A Lua event with no delay runs at once, also from a track event fired while the vehicles are being updated, and a vehicle added then would change the list being iterated. A call made while the scenery loads waits until it's loaded. - The whole definition is checked before anything is made: only trainset, reversed, assignment, vehicle nodes and endtrainset, each trainset on a track which exists, with vehicle names not in use and not repeated. Otherwise nothing is placed and the log says why. - The statements go through the same code as in a scenery file, so "reversed" and "assignment" work as there. The trainset header is read without the overrides of the scenario starter, which go by the line of the scenario file (open_trainset, split off deserialize_trainset; loading is unchanged). - The argument is checked with luaL_checkstring, a nil is a Lua error. Based on PR #159, without its move of the smoke sources and lights into TDynamicObject::Init (vehicles of road points would get their smoke twice) and without the velocity parameter of insert_trainset. Nothing changes for existing sceneries; no game data changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user