aboutsummaryrefslogtreecommitdiff
path: root/advtrains_train_track/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'advtrains_train_track/init.lua')
-rw-r--r--advtrains_train_track/init.lua53
1 files changed, 52 insertions, 1 deletions
diff --git a/advtrains_train_track/init.lua b/advtrains_train_track/init.lua
index 425c840..2bd5e28 100644
--- a/advtrains_train_track/init.lua
+++ b/advtrains_train_track/init.lua
@@ -1,3 +1,5 @@
+-- Default tracks for advtrains
+-- (c) orwell96 and contributors
--flat
advtrains.register_tracks("default", {
@@ -47,7 +49,56 @@ advtrains.register_tracks("default", {
formats={},
get_additional_definiton = advtrains.atc_function
}, advtrains.trackpresets.t_30deg_straightonly)
--- loading and unloading tracks
+
+
+-- Tracks for loading and unloading trains
+-- Copyright (C) 2017 Gabriel PĂ©rez-Cerezo <gabriel@gpcf.eu>
+
+local function get_far_node(pos)
+ local node = minetest.get_node(pos)
+ if node.name == "ignore" then
+ minetest.get_voxel_manip():read_from_map(pos, pos)
+ node = minetest.get_node(pos)
+ end
+ return node
+end
+
+local function train_load(pos, train_id, unload)
+ local train=advtrains.trains[train_id]
+ local below = get_far_node({x=pos.x, y=pos.y-1, z=pos.z})
+ if not string.match(below.name, "chest") then
+ atprint("this is not a chest! at "..minetest.pos_to_string(pos))
+ return
+ end
+ local inv = minetest.get_inventory({type="node", pos={x=pos.x, y=pos.y-1, z=pos.z}})
+ if inv and train.velocity < 2 then
+ for k, v in ipairs(train.trainparts) do
+
+ local i=minetest.get_inventory({type="detached", name="advtrains_wgn_"..v})
+ if i then
+ if not unload then
+ for _, item in ipairs(inv:get_list("main")) do
+ if i:get_list("box") and i:room_for_item("box", item) then
+ i:add_item("box", item)
+ inv:remove_item("main", item)
+ end
+ end
+ else
+ for _, item in ipairs(i:get_list("box")) do
+ if inv:get_list("main") and inv:room_for_item("main", item) then
+ i:remove_item("box", item)
+ inv:add_item("main", item)
+ end
+ end
+ end
+ end
+ end
+ end
+end
+
+
+
+
advtrains.register_tracks("default", {
nodename_prefix="advtrains:dtrack_unload",
texture_prefix="advtrains_dtrack_unload",
' href='#n199'>199
-- train_related.lua
-- Occupation of track sections - mainly implementation of train callbacks

--[[
Track section occupation is saved as follows

In train:
train.il_sections = {
	[n] = {ts_id = <...> (origin = <sigd>)}
}
-- "origin" is the TCB (signal describer) the train initially entered this section

In track section
ts.trains = {
	[n] = <train_id>
}

When any inconsistency is detected, we will assume the most restrictive setup.
It will be possible to indicate a section "free" via the GUI.
]]

local ildb = advtrains.interlocking.db

local sigd_equal = advtrains.interlocking.sigd_equal

local function itexist(tbl, com)
	for _,item in ipairs(tbl) do
		if (item==com) then
			return true
		end
	end
	return false
end
local function itkexist(tbl, ikey, com)
	for _,item in ipairs(tbl) do
		if item[ikey] == com then
			return true
		end
	end
	return false
end

local function itremove(tbl, com)
	local i=1
	while i <= #tbl do
		if tbl[i] == com then
			table.remove(tbl, i)
		else
			i = i + 1
		end