diff options
author | Y. Wang <y5nw@protonmail.com> | 2024-09-20 12:45:20 +0000 |
---|---|---|
committer | orwell <orwell@bleipb.de> | 2024-11-09 22:10:06 +0100 |
commit | b075d8e3336dfb2755dd1b9dc29c5320a155013b (patch) | |
tree | 065aa5602afac4b54b1555e44bce4446b59215ea | |
parent | 66868e2eefe97b7a6d35fa3fecc895b9c7bbc4cc (diff) | |
download | advtrains-b075d8e3336dfb2755dd1b9dc29c5320a155013b.tar.gz advtrains-b075d8e3336dfb2755dd1b9dc29c5320a155013b.tar.bz2 advtrains-b075d8e3336dfb2755dd1b9dc29c5320a155013b.zip |
Implement staticdata for trains
This patch exposes train.staticdata that can be used by other modders to
save data in trains across restarts. It additionally exposes two new
APIs for modders where this is relevant:
* advtrains.te_register_on_couple(function(init_train, stat_train)):
registers a callback for train coupling, where stat_train is couple
into init_train and is subsequently removed. This callback is run
before the actual coupling takes place; in particular, it is run
before stat_train is removed.
* advtrains.te_register_on_decouple(function(train, newtrain, index)):
registers a callback for train decoupling, where newtrain is created
by splitting the train at the given index (the wagon at the index is
part of the new train). This callback is run after decoupling takes
place.
advtrains.te_register_on_decouple(function
-rw-r--r-- | advtrains/couple.lua | 16 | ||||
-rw-r--r-- | advtrains/init.lua | 1 | ||||
-rw-r--r-- | advtrains/trainlogic.lua | 6 |
3 files changed, 23 insertions, 0 deletions
diff --git a/advtrains/couple.lua b/advtrains/couple.lua index 9474dcf..cbeb661 100644 --- a/advtrains/couple.lua +++ b/advtrains/couple.lua @@ -28,6 +28,20 @@ end advtrains.register_coupler_type("chain", attrans("Buffer and Chain Coupler")) advtrains.register_coupler_type("scharfenberg", attrans("Scharfenberg Coupler")) +for _, name in pairs {"couple", "decouple"} do + local t = {} + local function reg(f) + table.insert(t, f) + end + local function cb(...) + for _, f in ipairs(t) do + f(...) + end + end + advtrains["te_registered_on_" .. name] = t + advtrains["te_register_on_" .. name] = reg + advtrains["te_run_callbacks_on_" .. name] = cb +end local function create_couple_entity(pos, train1, t1_is_front, train2, t2_is_front) local id1 = train1.id @@ -235,6 +249,8 @@ function advtrains.couple_trains(init_train, invert_init_train, stat_train, stat return end + advtrains.te_run_callbacks_on_couple(init_train, stat_train) + if stat_train_opposite then -- insert wagons in inverse order and set their wagon_flipped state for i=1,stat_wagoncnt do diff --git a/advtrains/init.lua b/advtrains/init.lua index cb2214d..4a0b928 100644 --- a/advtrains/init.lua +++ b/advtrains/init.lua @@ -476,6 +476,7 @@ advtrains.avt_save = function(remove_players_from_wagons) "text_outside", "text_inside", "line", "routingcode", "il_sections", "speed_restriction", "speed_restrictions_t", "is_shunt", "points_split", "autocouple", "atc_wait_autocouple", "ars_disable", + "staticdata", }) --then save it tmp_trains[id]=v diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua index 3b006d2..ed49a4c 100644 --- a/advtrains/trainlogic.lua +++ b/advtrains/trainlogic.lua @@ -263,6 +263,10 @@ function advtrains.train_ensure_init(id, train) atwarn(debug.traceback()) return nil end + + if not train.staticdata then + train.staticdata = {} + end train.dirty = true if train.no_step then @@ -1190,6 +1194,8 @@ function advtrains.split_train_at_index(train, index) newtrain.points_split = advtrains.merge_tables(train.points_split) newtrain.autocouple = train.autocouple + advtrains.te_run_callbacks_on_decouple(train, newtrain, index) + return newtrain_id -- return new train ID, so new train can be manipulated end |