aboutsummaryrefslogtreecommitdiff
path: root/advtrains/occupation.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2018-05-17 21:37:01 +0200
committerorwell96 <orwell@bleipb.de>2018-06-14 17:39:42 +0200
commit24b0639c5f057ccb0fccc3c65be923bee1b571db (patch)
treebe7ece8c6c70701dc02ee68cf86998f310efd2ec /advtrains/occupation.lua
parent5dca1553333b8267de72ebf8788afbb928251ebf (diff)
downloadadvtrains-24b0639c5f057ccb0fccc3c65be923bee1b571db.tar.gz
advtrains-24b0639c5f057ccb0fccc3c65be923bee1b571db.tar.bz2
advtrains-24b0639c5f057ccb0fccc3c65be923bee1b571db.zip
Mainly make collisions and coupling work
Missing: ATC stuff, yaw problems
Diffstat (limited to 'advtrains/occupation.lua')
-rw-r--r--advtrains/occupation.lua40
1 files changed, 34 insertions, 6 deletions
diff --git a/advtrains/occupation.lua b/advtrains/occupation.lua
index 6316bd3..99de521 100644
--- a/advtrains/occupation.lua
+++ b/advtrains/occupation.lua
@@ -56,7 +56,7 @@ Whenever a path gets invalidated or path items are deleted, their index counterp
When a train needs to know whether a position is blocked by another train, it will (and is permitted to)
query the train.index and train.end_index and compare them to the blocked position's index.
-Callback system for 3rd-party path checkers: TODO
+Callback system for 3rd-party path checkers:
advtrains.te_register_on_new_path(func(id, train))
-- Called when a train's path is re-initalized, either when it was invalidated
-- or the saves were just loaded
@@ -72,6 +72,12 @@ advtrains.te_register_on_update(func(id, train))
-- It is ensured that on_new_path callbacks are executed prior to these callbacks whenever
-- an invalidation or a reload occured.
+advtrains.te_register_on_create(func(id, train))
+-- Called right after a train is created, right after the initial new_path callback
+advtrains.te_register_on_remove(func(id, train))
+-- Called right before a train is deleted
+
+
All callbacks are allowed to save certain values inside the train table, but they must ensure that
those are reinitialized in the on_new_path callback. The on_new_path callback must explicitly
set ALL OF those values to nil or to a new updated value, and must not rely on their existence.
@@ -162,11 +168,13 @@ function o.check_collision(pos, train_id)
if not t then return end
local i = 1
while t[i] do
- if t[i]~=train_id then
+ local ti = t[i]
+ if ti~=train_id then
local idx = t[i+1]
- local train = advtrains.trains[train_id]
- advtrains.train_ensure_init(train_id, train)
+ local train = advtrains.trains[ti]
+ atdebug("checking train",t[i],"index",idx,"<>",train.index,train.end_index)
if idx >= train.end_index and idx <= train.index then
+ atdebug("collides.")
return true
end
end
@@ -183,19 +191,39 @@ function o.get_occupations(train, index)
local ppos, ontrack = advtrains.path_get(train, index)
if not ontrack then
atdebug("Train",train.id,"get_occupations requested off-track",index)
- return {}, pos
+ return {}, ppos
end
local pos = advtrains.round_vector_floor_y(ppos)
local t = occget(pos)
+ if not t then return {} end
local r = {}
local i = 1
local train_id = train.id
while t[i] do
if t[i]~=train_id then
- r[train_id] = t[i+1]
+ r[t[i]] = t[i+1]
end
i = i + 2
end
return r, pos
end
+-- Gets a mapping of train id's to indexes of trains that stand or drive over
+-- returns (table with train_id->index), position
+function o.get_trains_at(ppos)
+ local pos = advtrains.round_vector_floor_y(ppos)
+ local t = occget(pos)
+ if not t then return {} end
+ local r = {}
+ local i = 1
+ while t[i] do
+ local train = advtrains.trains[t[i]]
+ local idx = t[i+1]
+ if train.end_index - 0.5 <= idx and idx <= train.index + 0.5 then
+ r[t[i]] = idx
+ end
+ i = i + 2
+ end
+ return r
+end
+
advtrains.occ = o