Advertisement
Punio

RIM_lift_startup

Nov 20th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1.  
  2. -- #################################
  3. -- vars
  4. -- #################################
  5.  
  6. cycleFile   = "_cycles"
  7. dirFile     = "_dir"
  8.  
  9. -- #################################
  10. -- main
  11. -- #################################
  12.  
  13. while true do
  14.    rednet.close("back")
  15.    
  16.    cycleTimes  = 0
  17.    dir         = "none"
  18.  
  19.    if fs.exists(cycleFile) then
  20.       file = fs.open(cycleFile, "r")
  21.       cycleTimes = tonumber(file.readLine())
  22.       file.close()
  23.    end
  24.    
  25.    if cycleTimes > 0 then
  26.       if fs.exists(dirFile) then
  27.          file = fs.open(dirFile, "r")
  28.          dir = file.readLine()
  29.          file.close()
  30.  
  31.          cyclesLeft = cycleTimes - 1
  32.          
  33.          file = fs.open(cycleFile, "w")
  34.          file.write(cyclesLeft)
  35.          file.close(file)
  36.      
  37.          print("Cycles left: " .. cyclesLeft)
  38.          print("Dir        : " .. dir)
  39.          
  40.          sleep(.1)
  41.          
  42.          shell.run("move " .. dir .. " " .. cycleTimes)
  43.       end
  44.    else
  45.       fs.delete(dirFile)
  46.       fs.delete(cycleFile)
  47.          
  48.       rednet.open("back")
  49.      
  50.       print("wait for message !")
  51.       id, msg, dist = rednet.receive()
  52.      
  53.       print("message received !")
  54.      
  55.       if msg == "move up!" then
  56.          dir = "up"
  57.          print("valide action " .. msg .. " !")
  58.       elseif msg == "move down!" then
  59.          dir = "down"
  60.          print("valide action " .. msg .. " !")
  61.       else
  62.          print("invalide action " .. msg .. " !")
  63.       end
  64.          
  65.       if dir ~= "none" then
  66.          print("dir : " .. dir)
  67.      
  68.          file = fs.open(dirFile, "w")
  69.          file.write(dir)
  70.          file.close(file)
  71.          
  72.          print("ask for steps !")        
  73.          rednet.send(id, "steps?")
  74.          
  75.          print("wait for answer ...")        
  76.          id, msg, dist = rednet.receive(10)
  77.          if msg ~= nil and tonumber(msg) > 0 then
  78.             print("answer " .. msg .." !")
  79.            
  80.             cycleTimes = tonumber(msg)
  81.            
  82.             file = fs.open(cycleFile, "w")
  83.             file.write(cycleTimes)
  84.             file.close(file)
  85.          else
  86.             print("no or invalide answer !")
  87.          end
  88.       end
  89.    end
  90.    
  91.    sleep(.1)
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement