18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 03:37:09 +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:
jakubg1
2026-11-30 02:24:35 +01:00
parent 571487612b
commit d19411ce40
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);