Advertisement
moxenb

chamber

Jun 27th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.91 KB | None | 0 0
  1. gapLength=25
  2. coloffset=8
  3.  
  4. package.path = package.path .. ";../?.lua"
  5. require("mockturtle")
  6.  
  7. function run()
  8.   jogLeft(1)
  9.   digCube(7, 3, 3, -1)
  10.  
  11.   forward(1)
  12.   up(1)
  13.   turnLeft()
  14.   forward()
  15.   digCube(gapLength, 2, 5, -1)
  16.  
  17.   forward(1)
  18.   jogRight(1)
  19.   down(2)
  20.   digCube(gapLength - 2, 2, 3, -1)
  21.  
  22.   up(2)
  23.   turnAround()
  24.   forward(5)
  25.   jogRight(1)
  26.   digCube(gapLength, 2, -5, -1)
  27.  
  28.   forward(1)
  29.   jogLeft(1)
  30.   down(2)
  31.   digCube(gapLength - 2, 2, -3, -1)
  32.  
  33.   up(2)
  34.   turnAround()
  35.  
  36.   forward(1)
  37.   turnLeft()
  38.   forward(2)
  39.   turnLeft()
  40.   down(1)
  41.  
  42.   digCube(coloffset, 3, 5, -1)
  43.   forward(coloffset + 3)
  44.   digCube(gapLength - coloffset - 3, 3, 5, -1)
  45.   back(coloffset + 5)
  46.   turnLeft()
  47.  
  48. --  forward(2)
  49. --  down(1)
  50. --  jogLeft(2)
  51. --  forward(1)
  52. --  turnRight()
  53. --  showPos("  done!")
  54. end
  55.  
  56.   -- *********************** STOCK ***********************
  57.   _debug = true
  58.   fuelSlot = 1
  59.   _xPos, _yPos, _zPos = 0, 0, 0
  60.   NORTH, EAST, SOUTH, WEST = 0, 1, 2, 3
  61.   _heading = NORTH
  62.  
  63.   function getOption(val, t, default)
  64.     if type(val) == t then
  65.       return val
  66.     else
  67.       return default
  68.     end
  69.   end
  70.  
  71.   function headingToString(heading)
  72.     if _heading == NORTH then
  73.       return 'north'
  74.     elseif _heading == SOUTH then
  75.       return 'south'
  76.     elseif _heading == EAST then
  77.       return 'east'
  78.     elseif _heading == WEST then
  79.       return 'west'
  80.     else
  81.       return 'UNKNOWN'
  82.     end
  83.   end
  84.  
  85.   function showPos(message)
  86.     print(string.format("(%d,%d,%d) - %s - %s", _xPos, _yPos, _zPos, headingToString(_heading), message))
  87.   end
  88.  
  89.   function clearForward()
  90.     while turtle.detect() do turtle.dig() end
  91.   end
  92.  
  93.   function clearDown()
  94.     if turtle.detect() then turtle.dig() end
  95.   end
  96.  
  97.   function clearUp()
  98.     while (turtle.detectUp()) do
  99.       turtle.digUp()
  100.     end
  101.   end
  102.  
  103.   function refuel()
  104.     if turtle.getFuelLevel() < 15 then
  105.       print("refueling")
  106.       turtle.select(fuelSlot)
  107.       turtle.refuel(5)
  108.     end
  109.   end
  110.  
  111.   function selectSlot(slot, overflow)
  112.     while turtle.getItemCount(slot) == 0 do
  113.       slot = slot + 1
  114.       if not overflow or slot > overflow then
  115.         return
  116.       end
  117.     end
  118.     turtle.select(slot)
  119.   end
  120.  
  121.   function place(slot, overflow)
  122.     selectSlot(slot, overflow)
  123.     turtle.place()
  124.   end
  125.  
  126.   function placeDown(slot, overflow)
  127.     selectSlot(slot, overflow)
  128.     turtle.placeDown()
  129.   end
  130.  
  131.   function placeUp(slot, overflow)
  132.     selectSlot(slot, overflow)
  133.     turtle.placeUp()
  134.   end
  135.  
  136.   function loopUntil(name, f)
  137.     local remaining = 10
  138.     refuel()
  139.     while remaining > 0 and not f() do
  140.       remaining = remaining - 1
  141.       os.sleep(1)
  142.       refuel()
  143.     end
  144.  
  145.     if remaining == 0 then
  146.       print("Failed to invoke " .. name .. " after 10 tries. Halted")
  147.       read()
  148.       return false
  149.     else
  150.       return true
  151.     end
  152.   end
  153.  
  154.   function forward(distance)
  155.     if not distance then
  156.       distance = 1
  157.     end
  158.     if _debug then print("  -- forward: " ..distance.."/"..headingToString(_heading)) end
  159.  
  160.     for i=1,distance do
  161.       local success = false
  162.       while not success do
  163.         -- make sure the way is clear
  164.         while turtle.detect() do
  165.           turtle.dig()
  166.         end
  167.         success = loopUntil("forward", function() return turtle.forward() end)
  168.       end
  169.  
  170.       -- update position
  171.       if _heading == NORTH then
  172.         _xPos = _xPos + 1
  173.       elseif _heading == SOUTH then
  174.         _xPos = _xPos - 1
  175.       elseif _heading == EAST then
  176.         _yPos = _yPos + 1
  177.       elseif _heading == WEST then
  178.         _yPos = _yPos - 1
  179.       else
  180.         print("UNKNOWN HEADING: " .. headingToString(_heading))
  181.       end
  182.     end
  183.   end
  184.  
  185.   function back(distance)
  186.     if not distance then distance = 1 end
  187.     if _debug then print("  -- back: " ..distance) end
  188.  
  189.     for i=1,distance do
  190.       loopUntil("back", function() return turtle.back() end)
  191.  
  192.       -- update position
  193.       if _heading == NORTH then
  194.         _xPos = _xPos - 1
  195.       elseif _heading == SOUTH then
  196.         _xPos = _xPos + 1
  197.       elseif _heading == EAST then
  198.         _yPos = _yPos - 1
  199.       elseif _heading == WEST then
  200.         _yPos = _yPos + 1
  201.       else
  202.       print("UNKNOWN HEADING: " .. headingToString(_heading))
  203.       end
  204.     end
  205.   end
  206.  
  207.   function up(distance)
  208.   if _debug then print("  -- up: " ..distance) end
  209.     if not distance then distance = 1 end
  210.  
  211.     for i = 1, distance do
  212.       local success = false
  213.       while not success do
  214.         while turtle.detectUp() do
  215.           turtle.digUp()
  216.         end
  217.         refuel()
  218.         success = loopUntil("up", function() return turtle.up() end)
  219.       end
  220.       _zPos = _zPos + 1
  221.     end
  222.   end
  223.  
  224.   function down(distance)
  225.   if _debug then print("  -- down: " ..distance) end
  226.     if not distance then distance = 1 end
  227.  
  228.     for i = 1, distance do
  229.       local success = false
  230.       while not success do
  231.         while turtle.detectDown() do
  232.           turtle.digDown()
  233.         end
  234.         refuel()
  235.         success = loopUntil("down", function() return turtle.down() end)
  236.       end
  237.       _zPos = _zPos - 1
  238.     end
  239.   end
  240.  
  241.   function turnLeft()
  242.     turtle.turnLeft();
  243.     _heading = (_heading + 3) % 4
  244.     if _debug then print("  -- turnLeft: " .. headingToString(_heading)) end
  245.   end
  246.  
  247.   function turnRight()
  248.     turtle.turnRight();
  249.     _heading = (_heading + 1) % 4
  250.     if _debug then print("  -- turnRight: " .. headingToString(_heading)) end
  251.   end
  252.  
  253.   function set_heading(new_heading)
  254.     -- this is stupid, but I'm lazy
  255.     while _heading ~= new_heading do
  256.       turnRight()
  257.     end
  258.   end
  259.  
  260.   function turnAround()
  261.     turnRight()
  262.     turnRight()
  263.   end
  264.  
  265.   function jogRight(distance)
  266.     turnRight()
  267.     forward(distance)
  268.     turnLeft()
  269.   end
  270.  
  271.   function jogLeft(distance)
  272.     turnLeft()
  273.     forward(distance)
  274.     turnRight()
  275.   end
  276.  
  277.   function boolToString(b)
  278.     if b then return "true" else return "false" end
  279.   end
  280.  
  281.   function go(options)
  282.     local distance = getOption(options.distance, "number", 1)
  283.     local digUp = getOption(options.digUp, "boolean", false)
  284.     local digDown = getOption(options.digDown, "boolean", false)
  285.     local fillFloorSlot = getOption(options.fillFloorSlot, "number", -1)
  286.  
  287.     showPos(string.format("go(dist=%d,up=%s,down=%s,fill=%d)",
  288.       distance,
  289.       boolToString(digUp),
  290.       boolToString(digDown),
  291.       fillFloorSlot))
  292.  
  293.     if options.dir then
  294.       set_heading(options.dir)
  295.     end
  296.  
  297.     while distance > 0 do
  298.       -- dig up and down in the starting place
  299.       if digUp then
  300.         clearUp()
  301.       end
  302.       if digDown and turtle.detectDown() then
  303.         turtle.digDown()
  304.       end
  305.       if fillFloorSlot > 0 then
  306.         turtle.select(fillFloorSlot)
  307.         turtle.placeDown()
  308.       end
  309.  
  310.       -- move forward
  311.       forward()
  312.  
  313.       distance = distance - 1
  314.     end
  315.  
  316.     -- dig up and down after moving
  317.     if digUp then
  318.       clearUp()
  319.     end
  320.     if digDown and turtle.detectDown() then
  321.       turtle.digDown()
  322.     end
  323.     if fillFloorSlot > 0 then
  324.       turtle.select(fillFloorSlot)
  325.       turtle.placeDown()
  326.     end
  327.   end
  328.  
  329.   function digVerticalSegment(depth, height, fillFloorSlot)
  330.     if not floorFillSlot then floorFillSlot = -1 end
  331.  
  332.     local startZ = _zPos
  333.  
  334.     showPos(string.format("digVerticalSegment(depth=%d,height=%s,fill=%d)",
  335.       depth, height, floorFillSlot))
  336.     go{distance = depth - 1, digUp = height > 1, digDown = false, fillFloorSlot = fillFloorSlot}
  337.     back(depth - 1)
  338.  
  339.     local rowsLeft = height - 2;
  340.  
  341.     -- smart people would only take one pass, but that requires actual thought
  342.     while rowsLeft > 0 do
  343.       if rowsLeft > 2 then
  344.         up(3)
  345.         go{distance = depth - 1, digUp = rowsLeft > 2, digDown = true}
  346.         rowsLeft = rowsLeft - 3 -- technically wrong, but will exit the loop
  347.       else
  348.         up(2)
  349.         go{distance = depth - 1, digUp = rowsLeft > 2, digDown = false}
  350.         rowsLeft = rowsLeft - 2 -- technically wrong, but will exit the loop
  351.       end
  352.       back(depth - 1)
  353.     end
  354.  
  355.     -- return to start pos
  356.     down(_zPos - startZ)
  357.   end
  358.  
  359.   function digCube(depth, height, width, fillFloorSlot)
  360.     if not fillFloorSlot then fillFloorSlot = -1 end
  361.  
  362.     showPos(string.format("digCube(depth=%d, height=%d, width=%d, ff=%d)",
  363.       depth, height, width, fillFloorSlot))
  364.  
  365.     local indexFn
  366.     if width > 0 then indexFn = jogRight else indexFn = jogLeft end
  367.  
  368.     digVerticalSegment(depth, height, fillFloorSlot)
  369.     for i = 1, math.abs(width) - 1 do
  370.       -- move to next segment
  371.       if width > 0 then
  372.         indexFn(1)
  373.       else
  374.         indexFn(1)
  375.       end
  376.       digVerticalSegment(depth, height, fillFloorSlot)
  377.     end
  378.  
  379.     -- return to _yPos 0
  380.     if width > 0 then
  381.       jogLeft(math.abs(width) - 1)
  382.     else
  383.       jogRight(math.abs(width) - 1)
  384.     end
  385.  
  386.     showPos("  digCube done")
  387.   end
  388.  
  389.   -- *********************** STOCK ***********************
  390.  
  391.   run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement