mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 15:17:08 +01:00
d19411ce40
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>
116 lines
7.1 KiB
C++
116 lines
7.1 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 "utilities/parser.h"
|
|
#include "scene/scene.h"
|
|
#include "scene/scenelayers.h"
|
|
|
|
namespace simulation {
|
|
|
|
struct deserializer_state {
|
|
std::string scenariofile;
|
|
cParser input;
|
|
scene::scratch_data scratchpad;
|
|
using deserializefunctionbind = std::function<void()>;
|
|
std::unordered_map<
|
|
std::string,
|
|
deserializefunctionbind> functionmap;
|
|
|
|
deserializer_state(std::string const &File, cParser::buffertype const Type, const std::string &Path, bool const Loadtraction)
|
|
: scenariofile(File), input(File, Type, Path, Loadtraction) { }
|
|
};
|
|
|
|
class state_serializer {
|
|
|
|
public:
|
|
|
|
// methods
|
|
// 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;
|
|
// create new model from node stirng
|
|
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.
|
|
// everything else the included template defines is left out, and takes effect when the saved scenery is loaded.
|
|
// Context: placement in effect at the location of the directive. returns: number of created models and of skipped statements
|
|
std::pair<int, int> preview_include(std::string const &Directive, scene::layer_context const &Context, scene::layer_handle Layer, scene::instance_handle Instance);
|
|
|
|
private:
|
|
// methods
|
|
// restores class data from provided stream
|
|
void deserialize_area( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_isolated( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_assignment( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_atmo( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_camera( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_config( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_description( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_event( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_lua( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_firstinit( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_group( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_endgroup( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_light( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_node( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_origin( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_endorigin( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_scale( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_endscale( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_rotate( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_sky( cParser &Input, scene::scratch_data &Scratchpad );
|
|
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 );
|
|
void deserialize_endtrainset( cParser &Input, scene::scratch_data &Scratchpad );
|
|
void deserialize_reversed( cParser &Input, scene::scratch_data &Scratchpad );
|
|
TTrack * deserialize_path( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
TTraction * deserialize_traction( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
TTractionPowerSource * deserialize_tractionpowersource( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
TMemCell * deserialize_memorycell( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
TEventLauncher * deserialize_eventlauncher( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
TAnimModel * deserialize_model( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
// loads a run of model instances straight from the text. returns: true if any were loaded, false if the definition at hand is left to the parser
|
|
bool deserialize_models( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data &Nodedata, std::size_t Line, std::streamoff Sourcebegin );
|
|
// makes model instance defined by a scenery file a part of the simulation
|
|
void insert_model( TAnimModel *Instance, cParser const &Input, std::size_t Line, scene::source_span const &Span );
|
|
TDynamicObject * deserialize_dynamic( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
sound_source * deserialize_sound( cParser &Input, scene::scratch_data &Scratchpad, scene::node_data const &Nodedata );
|
|
void init_time();
|
|
// skips content of stream until specified token
|
|
void skip_until( cParser &Input, std::string const &Token );
|
|
// transforms provided location by specifed rotation and offset
|
|
glm::dvec3 transform( glm::dvec3 Location, scene::scratch_data const &Scratchpad );
|
|
void export_nodes_to_stream( std::ostream &, bool Dirty ) const;
|
|
};
|
|
|
|
} // simulation
|
|
|
|
//---------------------------------------------------------------------------
|