Advertisement
Guest User

movement.lua

a guest
Dec 15th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.59 KB | None | 0 0
  1. move = {}
  2.  
  3. require("action")
  4.  
  5. local robot = require("robot")
  6. local component = require("component")
  7. local inventory = component.inventory_controller
  8.  
  9. function move(movementCommands, isBreak)
  10.   if movementCommands == nil then
  11.     return false
  12.   end
  13.  
  14.   local moves = {}
  15.   local steps = {}
  16.  
  17.   --Check the commands
  18.   if #movementCommands % 2 == 1 then
  19.     return false
  20.   end
  21.  
  22.   local numberOfMoves = #movementCommands / 2
  23.  
  24.   --Check the strings
  25.   for i = 1, numberOfMoves, 2 do
  26.     local sub = string.lower(string.sub(movementCommands, i, i))
  27.     if sub ~= "f" and
  28.        sub ~= "b" and
  29.        sub ~= "l" and
  30.        sub ~= "r" and
  31.        sub ~= "u" and
  32.        sub ~= "d" then
  33.       return false
  34.     end
  35.   end
  36.  
  37.   --Check the numbers
  38.   for i = 2, numberOfMoves, 2 do
  39.     local sub = tonumber(string.sub(movementCommands, i, i))
  40.     if sub == nil then
  41.       return false
  42.     end
  43.   end
  44.  
  45.   local moveNumber = 1
  46.  
  47.   --Create the movement arrays
  48.   for i = 1, #movementCommands, 2 do
  49.     moves[moveNumber] = string.lower(string.sub(movementCommands, i, i))
  50.  
  51.     if moveNumber < #movementCommands / 2 then
  52.       steps[moveNumber] = tonumber(string.sub(movementCommands, i + 1, i + 1))
  53.     else
  54.       steps[moveNumber] = tonumber(string.sub(movementCommands, i + 1))
  55.     end
  56.  
  57.     moveNumber = moveNumber + 1
  58.   end
  59.  
  60.   for i = 1, numberOfMoves do
  61.     if moves[i] == "f" then
  62.       moveForward(steps[i], isBreak)
  63.     elseif moves[i] == "b" then
  64.       moveBack(steps[i], isBreak)
  65.     elseif moves[i] == "l" then
  66.       moveLeft(steps[i], isBreak)
  67.     elseif moves[i] == "r" then
  68.       moveRight(steps[i], isBreak)
  69.     elseif moves[i] == "u" then
  70.       moveUp(steps[i], isBreak)
  71.     elseif moves[i] == "d" then
  72.       moveDown(steps[i], isBreak)
  73.     end
  74.   end
  75.   return true
  76. end
  77.  
  78. function moveDown(steps, isBreak)
  79.   if steps == nil then
  80.     steps = 1
  81.   end
  82.  
  83.   if isBreak == nil then
  84.     for i = 1, steps do
  85.       while not robot.down() do
  86.         if not swing("bottom") then
  87.           return i - 1
  88.         end
  89.       end
  90.     end
  91.   elseif isBreak == true then
  92.     for i = 1, steps do
  93.       while not robot.down() do
  94.         if not robot.swingDown() then
  95.           return i - 1
  96.         end
  97.       end
  98.     end
  99.   else
  100.     for i = 1, steps do
  101.       if not robot.down() then
  102.         return i - 1
  103.       end
  104.     end
  105.   end
  106. end
  107.  
  108. function moveForward(steps, isBreak)
  109.   if steps == nil then
  110.     steps = 1
  111.   end
  112.  
  113.   if isBreak == nil then
  114.     for i = 1, steps do
  115.       while not robot.forward() do
  116.         if not swing("forward") then
  117.           return i - 1
  118.         end
  119.       end
  120.     end
  121.   elseif isBreak == true then
  122.     for i = 1, steps do
  123.       while not robot.forward() do
  124.         if not robot.swing() then
  125.           return i - 1
  126.         end
  127.       end
  128.     end
  129.   else
  130.     for i = 1, steps do
  131.       if not robot.forward() then
  132.         return i - 1
  133.       end
  134.     end
  135.   end
  136.   return steps
  137. end
  138.  
  139. function moveBack(steps, isBreak)
  140.   robot.turnAround()
  141.   return moveForward(steps, isBreak)
  142. end
  143.  
  144. function moveLeft(steps, isBreak)
  145.   robot.turnLeft()
  146.   return moveForward(steps, isBreak)
  147. end
  148.  
  149. function moveRight(steps, isBreak)
  150.   robot.turnRight()
  151.   return moveForward(steps, isBreak)
  152. end
  153.  
  154. function moveUp(steps, isBreak)
  155.   if steps == nil then
  156.     steps = 1
  157.   end
  158.  
  159.   if isBreak == nil then
  160.     for i = 1, steps do
  161.       while not robot.up() do
  162.         if not swing("top") then
  163.           return i - 1
  164.         end      
  165.       end
  166.     end
  167.   elseif isBreak == true then
  168.     for i = 1, steps do
  169.       while not robot.up() do
  170.         if not robot.swingUp() then
  171.           return i - 1
  172.         end
  173.       end
  174.     end
  175.   else
  176.     for i = 1, steps do
  177.       if not robot.up() then
  178.         return i - 1
  179.       end
  180.     end
  181.   end
  182. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement