18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 02:27:06 +01:00
Files
maszyna/scripting/lua.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

51 lines
1.8 KiB
C++

#pragma once
#include <lua.hpp>
#include <stdafx.h>
class basic_event;
class TMemCell;
class TDynamicObject;
struct memcell_values { const char *str; double num1; double num2; };
class lua
{
lua_State *state;
static int atpanic(lua_State *s);
static int scriptapi_event_create(lua_State *L);
static int scriptapi_event_find(lua_State *L);
static int scriptapi_event_exists(lua_State *L);
static int scriptapi_event_getname(lua_State *L);
static int scriptapi_event_dispatch(lua_State *L);
static int scriptapi_event_dispatch_n(lua_State *L);
static int scriptapi_track_find(lua_State *L);
static int scriptapi_track_isoccupied(lua_State *L);
static int scriptapi_track_isoccupied_n(lua_State *L);
static int scriptapi_isolated_find(lua_State *L);
static int scriptapi_isolated_isoccupied(lua_State *L);
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);
static int scriptapi_memcell_update(lua_State *L);
static int scriptapi_memcell_update_n(lua_State *L);
static int scriptapi_random(lua_State *L);
static int scriptapi_writelog(lua_State *L);
static int scriptapi_writeerrorlog(lua_State *L);
public:
lua();
~lua();
std::string get_error() const;
void interpret(const std::string& file) const;
static void unref(lua_State *L, int ref);
static void dispatch_event(lua_State *L, int handler, basic_event *event, const TDynamicObject *activator);
static void push_memcell_values(lua_State *L, const TMemCell *mc);
static memcell_values get_memcell_values(lua_State *L, int idx);
};