aboutsummaryrefslogtreecommitdiff
path: root/wagons.lua
diff options
context:
space:
mode:
Diffstat (limited to 'wagons.lua')
-rw-r--r--wagons.lua58
1 files changed, 37 insertions, 21 deletions
diff --git a/wagons.lua b/wagons.lua
index 69504a1..c12d62d 100644
--- a/wagons.lua
+++ b/wagons.lua
@@ -37,12 +37,15 @@ function wagon:on_rightclick(clicker)
return
end
if self.driver and clicker == self.driver then
+ advtrains.player_to_wagon_mapping[self.driver:get_player_name()]=nil
advtrains.set_trainhud(self.driver:get_player_name(), "")
self.driver = nil
+ self.driver_name = nil
clicker:set_detach()
clicker:set_eye_offset({x=0,y=0,z=0}, {x=0,y=0,z=0})
elseif not self.driver then
self.driver = clicker
+ advtrains.player_to_wagon_mapping[clicker:get_player_name()]=self
clicker:set_attach(self.object, "", self.attach_offset, {x=0,y=0,z=0})
clicker:set_eye_offset(self.view_offset, self.view_offset)
end
@@ -127,7 +130,7 @@ function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direct
table.remove(self:train().trainparts, self.pos_in_trainparts)
advtrains.update_trainpart_properties(self.train_id)
advtrains.wagon_save[self.unique_id]=nil
- if self.discouple_id and minetest.object_refs[self.discouple_id] then minetest.object_refs[self.discouple_id]:remove() end
+ if self.discouple then self.discouple.object:remove() end--will have no effect on unloaded objects
return
@@ -156,25 +159,14 @@ function wagon:on_step(dtime)
self.initialized=true
end
- --DisCouple
- if self.pos_in_trainparts and self.pos_in_trainparts>1 then
- if not self.discouple_id or not minetest.luaentities[self.discouple_id] then
- local object=minetest.add_entity(pos, "advtrains:discouple")
- if object then
- print("spawning discouple")
- local le=object:get_luaentity()
- le.wagon=self
- --box is hidden when attached, so unuseful.
- --object:set_attach(self.object, "", {x=0, y=0, z=self.wagon_span*10}, {x=0, y=0, z=0})
- --find in object_refs
- for aoi, compare in pairs(minetest.object_refs) do
- if compare==object then
- self.discouple_id=aoi
- end
- end
- else
- print("Couldn't spawn DisCouple")
- end
+ --re-attach driver if he got lost
+ if not self.driver and self.driver_name then
+ local clicker=minetest.get_player_by_name(self.driver_name)
+ if clicker then
+ self.driver = clicker
+ advtrains.player_to_wagon_mapping[clicker:get_player_name()]=self
+ clicker:set_attach(self.object, "", self.attach_offset, {x=0,y=0,z=0})
+ clicker:set_eye_offset(self.view_offset, self.view_offset)
end
end
@@ -190,10 +182,12 @@ function wagon:on_step(dtime)
self:train().tarvelocity=math.max(self:train().tarvelocity-1, -(advtrains.all_traintypes[self:train().traintype].max_speed or 10))
elseif pc.aux1 then --slower
if true or math.abs(self:train().velocity)<=3 then--TODO debug
+ advtrains.player_to_wagon_mapping[self.driver:get_player_name()]=nil
self.driver:set_detach()
self.driver:set_eye_offset({x=0,y=0,z=0}, {x=0,y=0,z=0})
advtrains.set_trainhud(self.driver:get_player_name(), "")
self.driver = nil
+ self.driver_name = nil
return--(don't let it crash because of statement below)
else
minetest.chat_send_player(self.driver:get_player_name(), "Can't get off driving train!")
@@ -205,6 +199,28 @@ function wagon:on_step(dtime)
end
local gp=self:train()
+
+ --DisCouple
+ if self.pos_in_trainparts and self.pos_in_trainparts>1 then
+ if gp.velocity==0 then
+ if not self.discouple or not self.discouple.object:getyaw() then
+ local object=minetest.add_entity(pos, "advtrains:discouple")
+ if object then
+ local le=object:get_luaentity()
+ le.wagon=self
+ --box is hidden when attached, so unuseful.
+ --object:set_attach(self.object, "", {x=0, y=0, z=self.wagon_span*10}, {x=0, y=0, z=0})
+ self.discouple=le
+ else
+ print("Couldn't spawn DisCouple")
+ end
+ end
+ else
+ if self.discouple and self.discouple.object:getyaw() then
+ self.discouple.object:remove()
+ end
+ end
+ end
--for path to be available. if not, skip step
if not advtrains.get_or_create_path(self.train_id, gp) then
self.object:setvelocity({x=0, y=0, z=0})
@@ -349,7 +365,7 @@ advtrains.register_wagon("newlocomotive", "steam",{
textures = {"advtrains_newlocomotive.png"},
is_locomotive=true,
attach_offset={x=5, y=10, z=-10},
- view_offset={x=0, y=6, z=18},
+ view_offset={x=0, y=6, z=0},
visual_size = {x=1, y=1},
wagon_span=1.85,
collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},