aboutsummaryrefslogtreecommitdiff
path: root/advtrains/trainlogic.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2017-01-02 18:40:49 +0100
committerorwell96 <orwell@bleipb.de>2017-01-02 18:40:49 +0100
commit79d0549c8af7eaa6ba97b8beaab52fb1012350f3 (patch)
tree1c7ee525e3694d146c719e3159d70f40e6a8508c /advtrains/trainlogic.lua
parent2a924d086a2bce6ce4030c81175d6f214d3be5d1 (diff)
downloadadvtrains-79d0549c8af7eaa6ba97b8beaab52fb1012350f3.tar.gz
advtrains-79d0549c8af7eaa6ba97b8beaab52fb1012350f3.tar.bz2
advtrains-79d0549c8af7eaa6ba97b8beaab52fb1012350f3.zip
unify update_trainpart_properties and check_trainpartload
Diffstat (limited to 'advtrains/trainlogic.lua')
-rw-r--r--advtrains/trainlogic.lua96
1 files changed, 42 insertions, 54 deletions
diff --git a/advtrains/trainlogic.lua b/advtrains/trainlogic.lua
index 9c93d96..4ac4b29 100644
--- a/advtrains/trainlogic.lua
+++ b/advtrains/trainlogic.lua
@@ -286,35 +286,7 @@ function advtrains.train_step(id, train, dtime)
should_check=should_check or ((vector.distance(ori_pos, p:getpos())<node_range))
end
if should_check then
- --it is better to iterate luaentites only once
- --print("check_trainpartload")
- local found_uids={}
- for _,wagon in pairs(minetest.luaentities) do
- if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
- if found_uids[wagon.unique_id] then
- --duplicate found, delete it
- if wagon.object then wagon.object:remove() end
- else
- found_uids[wagon.unique_id]=true
- end
- end
- end
- --print("found_uids: "..dump(found_uids))
- --now iterate trainparts and check. then cross them out to see if there are wagons over for any reason
- for pit, w_id in ipairs(train.trainparts) do
- if found_uids[w_id] then
- --print(w_id.." still loaded")
- elseif advtrains.wagon_save[w_id] then
- --print(w_id.." not loaded, but save available")
- --spawn a new and initialize it with the properties from wagon_save
- local le=minetest.env:add_entity(ori_pos, advtrains.wagon_save[w_id].entity_name):get_luaentity()
- le:init_from_wagon_save(w_id)
- else
- print(w_id.." not loaded and no save available")
- --what the hell...
- table.remove(train.trainparts, pit)
- end
- end
+ advtrains.update_trainpart_properties(id)
end
train.check_trainpartload=2
end
@@ -560,35 +532,51 @@ function advtrains.update_trainpart_properties(train_id, invert_flipstate)
local rel_pos=0
local count_l=0
for i, w_id in ipairs(train.trainparts) do
- local any_loaded=false
- for _,wagon in pairs(minetest.luaentities) do
- if wagon.is_wagon and wagon.initialized and wagon.unique_id==w_id then
- rel_pos=rel_pos+wagon.wagon_span
- wagon.train_id=train_id
- wagon.pos_in_train=rel_pos
- wagon.pos_in_trainparts=i
- wagon.old_velocity_vector=nil
- if wagon.is_locomotive then
- count_l=count_l+1
- end
- if invert_flipstate then
- wagon.wagon_flipped = not wagon.wagon_flipped
+ local wagon=nil
+ for _,iwagon in pairs(minetest.luaentities) do
+ if iwagon.is_wagon and iwagon.initialized and iwagon.unique_id==w_id then
+ if wagon then
+ --duplicate
+ iwagon.object:remove()
+ else
+ wagon=iwagon
end
- rel_pos=rel_pos+wagon.wagon_span
- any_loaded=true
-
- if wagon.drives_on then
- for k,_ in pairs(train.drives_on) do
- if not wagon.drives_on[k] then
- train.drives_on[k]=nil
- end
+ end
+ end
+ if not wagon then
+ if advtrains.wagon_save[w_id] then
+ --spawn a new and initialize it with the properties from wagon_save
+ wagon=minetest.env:add_entity(train.last_pos, advtrains.wagon_save[w_id].entity_name):get_luaentity()
+ wagon:init_from_wagon_save(w_id)
+ end
+ end
+ if wagon then
+ rel_pos=rel_pos+wagon.wagon_span
+ wagon.train_id=train_id
+ wagon.pos_in_train=rel_pos
+ wagon.pos_in_trainparts=i
+ wagon.old_velocity_vector=nil
+ if wagon.is_locomotive then
+ count_l=count_l+1
+ end
+ if invert_flipstate then
+ wagon.wagon_flipped = not wagon.wagon_flipped
+ end
+ rel_pos=rel_pos+wagon.wagon_span
+ any_loaded=true
+
+ if wagon.drives_on then
+ for k,_ in pairs(train.drives_on) do
+ if not wagon.drives_on[k] then
+ train.drives_on[k]=nil
end
end
- train.max_speed=math.min(train.max_speed, wagon.max_speed)
end
- end
- if not any_loaded then
- print("update_trainpart_properties wagon "..w_id.." not loaded, ignoring it.")
+ train.max_speed=math.min(train.max_speed, wagon.max_speed)
+ else
+ print(w_id.." not loaded and no save available")
+ --what the hell...
+ table.remove(train.trainparts, pit)
end
end
train.trainlen=rel_pos