Add door controls and ATC commands
authororwell96 <mono96.mml@gmail.com>
Wed, 18 Jan 2017 18:03:27 +0000 (19:03 +0100)
committerorwell96 <mono96.mml@gmail.com>
Wed, 18 Jan 2017 18:03:27 +0000 (19:03 +0100)
advtrains/advtrains/atc.lua
advtrains/advtrains/trainhud.lua
advtrains/advtrains/wagons.lua
advtrains/advtrains_train_subway/init.lua

index 2a4d226..7117a9a 100644 (file)
@@ -8,7 +8,7 @@ function atc.load_data(data)
        atc.controllers = data and data.controllers or {}
 end
 function atc.save_data()
-       return atc.controllers
+       return {controllers = atc.controllers}
 end
 --contents: {command="...", arrowconn=0-15 where arrow points}
 
@@ -172,6 +172,12 @@ local matchptn={
                end
                return 1
        end,
+       ["O([LRC])"]=function(id, train, match)
+               local tt={L=-1, R=1, C=0}
+               local arr=train.atc_arrow and 1 or -1
+               train.door_open = tt[match]*arr*train.movedir
+               return 2
+       end,
 }
 
 function atc.execute_atc_command(id, train)
index e69f04a..3830b65 100644 (file)
@@ -7,6 +7,7 @@ advtrains.hud[player:get_player_name()] = nil
 end)
 
 local mletter={[1]="F", [-1]="R", [0]="N"}
+local doorstr={[-1]="|<>| >|<", [0]=">|< >|<", [1]=">|< |<>|"}
 
 function advtrains.on_control_change(pc, train, flip)
        if pc.sneak then
@@ -48,6 +49,20 @@ function advtrains.on_control_change(pc, train, flip)
                                train.movedir = -train.movedir
                        end
                end
+               if pc.left then
+                       if train.door_open ~= 0 then
+                               train.door_open = 0
+                       else
+                               train.door_open = -train.movedir
+                       end
+               end
+               if pc.right then
+                       if train.door_open ~= 0 then
+                               train.door_open = 0
+                       else
+                               train.door_open = train.movedir
+                       end
+               end
                if train.brake_hold_state~=2 then
                        train.brake = false
                end
@@ -101,7 +116,7 @@ function advtrains.hud_train_format(train, flip)
        local tvel=advtrains.abs_ceil(train.tarvelocity)
        local topLine, firstLine, secondLine
        
-       topLine="Train".."  ["..mletter[fct*train.movedir].."]  "..(train.brake and "="..( train.brake_hold_state==2 and "^" or "" ).."B=" or "")
+       topLine="  ["..mletter[fct*train.movedir].."]  "..doorstr[(train.door_open or 0) * train.movedir].."  "..(train.brake and "="..( train.brake_hold_state==2 and "^" or "" ).."B=" or "")
        firstLine="Speed: |"..string.rep("+", vel)..string.rep("_", max-vel)..">"
        secondLine="Target: |"..string.rep("+", tvel)..string.rep("_", max-tvel)..">"
        
index 13811ad..f164335 100644 (file)
@@ -23,7 +23,7 @@ function wagon:on_rightclick(clicker)
                --advtrains.dumppath(self:train().path)\r
                --minetest.chat_send_all("at index "..(self:train().index or "nil"))\r
                --advtrains.invert_train(self.train_id)\r
-               minetest.chat_send_all(dump(self:train()))\r
+               atprint(dump(self))\r
                return\r
        end     \r
        local no=self:get_seatno(clicker:get_player_name())\r
@@ -69,10 +69,6 @@ function wagon:on_activate(sd_uid, dtime_s)
                        return\r
                end\r
        end\r
-       \r
-       if self.custom_on_activate then\r
-               self:custom_on_activate(dtime_s)\r
-       end\r
 end\r
 \r
 function wagon:get_staticdata()\r
@@ -149,6 +145,13 @@ function wagon:init_shared()
                        end\r
                end\r
        end\r
+       if self.doors then\r
+               self.door_anim_timer=0\r
+               self.door_state=0\r
+       end\r
+       if self.custom_on_activate then\r
+               self:custom_on_activate(dtime_s)\r
+       end\r
 end\r
 function wagon:ensure_init()\r
        if self.initialized then\r
@@ -286,7 +289,36 @@ function wagon:on_step(dtime)
        end\r
 \r
        local gp=self:train()\r
-\r
+       \r
+       --door animation\r
+       if self.doors then\r
+               if (self.door_anim_timer or 0)<=0 then\r
+                       local fct=self.wagon_flipped and -1 or 1\r
+                       local dstate = (gp.door_open or 0) * fct\r
+                       if dstate ~= self.door_state then\r
+                               local at\r
+                               --meaning of the train.door_open field:\r
+                               -- -1: left doors (rel. to train orientation)\r
+                               --  0: closed\r
+                               --  1: right doors\r
+                               --this code produces the following behavior:\r
+                               -- if changed from 0 to +-1, play open anim. if changed from +-1 to 0, play close.\r
+                               -- if changed from +-1 to -+1, first close and set 0, then it will detect state change again and run open.\r
+                               if self.door_state == 0 then\r
+                                       at=self.doors.open[dstate]\r
+                                       self.object:set_animation(at.frames, at.speed or 15, at.blend or 0, false)\r
+                                       self.door_state = dstate\r
+                               else\r
+                                       at=self.doors.close[self.door_state or 1]--in case it has not been set yet\r
+                                       self.object:set_animation(at.frames, at.speed or 15, at.blend or 0, false)\r
+                                       self.door_state = 0\r
+                               end\r
+                               self.door_anim_timer = at.time\r
+                       end\r
+               else\r
+                       self.door_anim_timer = (self.door_anim_timer or 0) - dtime\r
+               end\r
+       end\r
        --DisCouple\r
        if self.pos_in_trainparts and self.pos_in_trainparts>1 then\r
                if gp.velocity==0 then\r
index b105f1a..85bd28f 100644 (file)
@@ -12,6 +12,16 @@ advtrains.register_wagon("subway_wagon", {
                        driving_ctrl_access=true,
                },
        },
+       doors={
+               open={
+                       [-1]={frames={x=0, y=19}, time=1},
+                       [1]={frames={x=40, y=59}, time=1}
+               },
+               close={
+                       [-1]={frames={x=20, y=39}, time=1},
+                       [1]={frames={x=60, y=81}, time=1}
+               }
+       },
        visual_size = {x=1, y=1},
        wagon_span=2,
        collisionbox = {-1.0,-0.5,-1.0, 1.0,2.5,1.0},