18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 00:22:42 +01:00
Files
maszyna/simulation/simulation.h
T
jakubg1 d19411ce40 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>
2026-11-30 02:24:35 +01:00

127 lines
4.5 KiB
C++

/*
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 "simulation/simulationstateserializer.h"
#include "utilities/Classes.h"
#include "world/Event.h"
#include "vehicle/Train.h"
#include "rendering/particles.h"
#ifdef WITH_LUA
#include "scripting/lua.h"
#endif
namespace simulation {
class state_manager {
public:
// methods
void
init_scripting_interface();
// legacy method, calculates changes in simulation state over specified time
void
update( double Deltatime, int Iterationcount );
void
update_clocks();
void
update_scripting_interface();
// process input commands
void
process_commands();
// create model from node string
TAnimModel *
create_model(const std::string &src, const std::string &name, const glm::dvec3 &position);
// 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);
// creates model instances defined by an include directive placed in the scenery editor
std::pair<int, int>
preview_include(std::string const &Directive, scene::layer_context const &Context, scene::layer_handle Layer, scene::instance_handle Instance);
// replaces models shown for specified include with the ones defined by its current directive. Retired: receives
// the replaced models made by the editor. they're out of the scene already, but the renderer can still hold on
// to them for a few frames; pass them to Instances.purge() afterwards
std::pair<int, int>
rebuild_include(scene::instance_handle Instance, std::vector<TAnimModel *> &Retired);
// delete TAnimModel instance
void
delete_model(TAnimModel *model);
// gives TAnimModel instance another name; empty text leaves it without one. returns: false if another instance uses the name
bool
rename_model(TAnimModel *model, std::string const &name);
// delete TEventLauncher instance
void
delete_eventlauncher(TEventLauncher *launcher);
// starts deserialization from specified file, returns context pointer on success, throws otherwise
std::shared_ptr<deserializer_state>
deserialize_begin(std::string const &Scenariofile);
// continues deserialization for given context, amount limited by time, returns true if needs to be called again
bool
deserialize_continue(std::shared_ptr<deserializer_state> state);
// stores class data in specified file, in legacy (text) format
void
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;
};
// passes specified sound to all vehicles within range as a radio message broadcasted on specified channel
void radio_message( sound_source *Message, int const Channel );
extern state_manager State;
extern event_manager Events;
extern memory_table Memory;
extern path_table Paths;
extern road_table Roads;
extern sweep_table Sweeps;
extern junction_table Junctions;
extern roadpoint_table Roadpoints;
extern traction_table Traction;
extern powergridsource_table Powergrid;
extern instance_table Instances;
extern vehicle_table Vehicles;
extern train_table Trains;
extern light_array Lights;
extern sound_table Sounds;
extern particle_manager Particles;
#ifdef WITH_LUA
extern lua Lua;
#endif
extern scene::basic_region *Region;
extern TTrain *Train;
extern uint16_t prev_train_id;
extern bool is_ready;
struct deserializer_state;
} // simulation
//---------------------------------------------------------------------------