Advertisement
soee

cactus

Sep 15th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local tArgs = { ... }
  2.  
  3. local levelHeight = 5
  4. local width = 5
  5.  
  6. local cactusSlot = 1
  7. local floorSlot = 2
  8. local wallSlot = 3
  9. local baseSlot = 4
  10. local sandSlot = 5
  11. local fenceSlot = 6
  12. local torchSlot = 7
  13. local ladderSlot = 8
  14.  
  15. local reservedSlots = 8
  16.  
  17. local moveSleep = 0.2
  18. local paintSleep = 0.5
  19. local blockedMoveSleep = 2
  20.  
  21. local doLadders = turtle.getItemCount(ladderSlot) > 0
  22. local doPaintWalls = turtle.getItemCount(wallSlot) > 0
  23. local doPaintFloor = turtle.getItemCount(floorSlot) > 0
  24.  
  25.  
  26. local function printUsage()
  27.     print("Usage: cactus <levelCount>")
  28.     print("")
  29.     print("Slots:")
  30.     print(" 1 - Cactus")
  31.     print(" 2 - Floor")
  32.     print(" 3 - Wall")
  33.     print(" 4 - Base")
  34.     print(" 5 - Sand")
  35.     print(" 6 - Fence")
  36.     print(" 7 - Torch")
  37.     print(" 8 - Ladder")
  38.     print("")
  39. end
  40.  
  41. if #tArgs ~= 1 then
  42.     printUsage()
  43.     return
  44. end
  45.  
  46. local levelCount = tonumber(tArgs[1])
  47. local roomHeight = levelCount * levelHeight + 2
  48.  
  49.  
  50. local function selectMaterial(typeSlot)
  51.     turtle.select(typeSlot)
  52.     if turtle.getItemCount(typeSlot) > 1 then
  53.         return true
  54.     else
  55.         for otherSlot = 16, 1, -1 do
  56.             if (turtle.compareTo(otherSlot)) then
  57.                 if otherSlot <= reservedSlots then
  58.                     if turtle.getItemCount(otherSlot) > 1 then
  59.                         turtle.select(otherSlot)
  60.                         return true
  61.                     end
  62.                 else
  63.                     turtle.select(otherSlot)
  64.                     return true
  65.                 end
  66.             end
  67.         end
  68.     end
  69.     return false
  70. end
  71.  
  72. local function turtleForward()
  73.     local moveSuccess = false
  74.     while (not moveSuccess) do
  75.         while turtle.detect() do
  76.             turtle.dig()
  77.             sleep(moveSleep)
  78.         end
  79.         moveSuccess = turtle.forward()
  80.         if (not moveSuccess) then
  81.             sleep(blockedMoveSleep)
  82.         end
  83.     end
  84. end
  85.  
  86. local function turtleUp()
  87.     local moveSuccess = false
  88.     while (not moveSuccess) do
  89.         while turtle.detectUp() do
  90.             turtle.digUp()
  91.             sleep(moveSleep)
  92.         end
  93.         moveSuccess = turtle.up()
  94.         if (not moveSuccess) then
  95.             sleep(blockedMoveSleep)
  96.         end
  97.     end
  98. end
  99.  
  100. local function turtleUpX(count)
  101.     for i = 1, count do
  102.         turtleUp()
  103.     end
  104. end
  105.  
  106. local function turtleDown()
  107.     local moveSuccess = false
  108.     while (not moveSuccess) do
  109.         while turtle.detectDown() do
  110.             turtle.digDown()
  111.             sleep(moveSleep)
  112.         end
  113.         moveSuccess = turtle.down()
  114.         if (not moveSuccess) then
  115.             sleep(blockedMoveSleep)
  116.         end
  117.     end
  118. end
  119.  
  120. local function turtleDownX(count)
  121.     for i = 1, count do
  122.         turtleDown()
  123.     end
  124. end
  125.  
  126. local function doPaintSide()
  127.     if doPaintWalls then
  128.         if selectMaterial(wallSlot) then
  129.             if not turtle.compare() then
  130.                 while turtle.detect() do
  131.                     turtle.dig()
  132.                     sleep(paintSleep)
  133.                 end
  134.                 turtle.place()
  135.             end
  136.         end
  137.     end
  138. end
  139.  
  140. function plant()
  141.     selectMaterial(cactusSlot)
  142.     turtle.placeDown()
  143. end
  144.  
  145. local function returnToLevelStart(oddWidth)
  146.     if oddWidth then
  147.         -- odd width of chamber.. in opposite end.
  148.         turtle.turnRight()
  149.         for j = 1, 2 do
  150.             turtle.turnRight()
  151.             for i = 1, width - 1 do
  152.                 turtleForward()
  153.             end
  154.         end
  155.         turtle.turnRight()
  156.     else
  157.         -- even width.. back in same end as we started.
  158.         turtle.turnRight()
  159.         for i = 1, width - 1 do
  160.             turtleForward()
  161.         end
  162.         turtle.turnRight()
  163.     end
  164. end
  165.  
  166. local function turnAround(turnRight, steps)
  167.     if turnRight then
  168.         turtle.turnRight()
  169.         for i = 1, steps do
  170.             turtleForward()
  171.         end
  172.         turtle.turnRight()
  173.     else
  174.         turtle.turnLeft()
  175.         for i = 1, steps do
  176.             turtleForward()
  177.         end
  178.         turtle.turnLeft()
  179.     end
  180.     return (not turnRight)
  181. end
  182.  
  183. local function turn(turnRight)
  184.     if turnRight then
  185.         turtle.turnRight()
  186.     else
  187.         turtle.turnLeft()
  188.     end
  189. end
  190.  
  191.  
  192.  
  193. local function digLevel(digup, digdown)
  194.     local turnRight = true
  195.     for i = 1, width do
  196.         for j = 1, width - 1 do
  197.             if (digup) then
  198.                 turtle.digUp()
  199.             end
  200.             if (digdown) then
  201.                 turtle.digDown()
  202.             end
  203.             turtleForward()
  204.         end
  205.         if (digup) then
  206.             turtle.digUp()
  207.         end
  208.         if (digdown) then
  209.             turtle.digDown()
  210.         end
  211.         if i < width then
  212.             turnRight = turnAround(turnRight, 1)
  213.         end
  214.     end
  215.     returnToLevelStart(turnRight)
  216. end
  217.  
  218. local function paintWall()
  219.     if selectMaterial(wallSlot) then
  220.         if not turtle.compare() then
  221.             while turtle.detect() do
  222.                 turtle.dig()
  223.                 sleep(paintSleep)
  224.             end
  225.             turtle.place()
  226.         end
  227.     end
  228. end
  229.  
  230. local function paintLeftWall()
  231.     turtle.turnLeft()
  232.     paintWall()
  233.     turtle.turnRight()
  234. end
  235.  
  236. local function placeDown(slot)
  237.     if selectMaterial(slot) then
  238.         if not turtle.compareDown() then
  239.             while turtle.detectDown() do
  240.                 turtle.digDown()
  241.                 sleep(paintSleep)
  242.             end
  243.             turtle.placeDown()
  244.         end
  245.     end
  246. end
  247.  
  248. local function paintFloorBlock()
  249.     placeDown(floorSlot)
  250. end
  251.  
  252. local function placeBaseBlock()
  253.     placeDown(baseSlot)
  254. end
  255.  
  256. local function placeSandBlock()
  257.     placeDown(sandSlot)
  258. end
  259.  
  260. local function placeCactusBlock()
  261.     placeDown(cactusSlot)
  262. end
  263.  
  264. local function placeFenceBlock()
  265.     placeDown(fenceSlot)
  266. end
  267.  
  268. local function placeTorchBlock()
  269.     placeDown(torchSlot)
  270. end
  271.  
  272. local function paintLevelWalls()
  273.     for i = 1, 4 do
  274.         for j = 1, width - 1 do
  275.             paintLeftWall()
  276.             turtleForward()
  277.         end
  278.         paintLeftWall()
  279.         turtle.turnRight()
  280.     end
  281. end
  282.  
  283. local function paintEntireFloor()
  284.     local turnRight = true
  285.     for i = 1, width do
  286.         for j = 1, width - 1 do
  287.             paintFloorBlock()
  288.             turtleForward()
  289.         end
  290.         paintFloorBlock()
  291.         if i < width then
  292.             turnRight = turnAround(turnRight, 1)
  293.         end
  294.     end
  295.     returnToLevelStart(turnRight)
  296. end
  297.  
  298.  
  299. local function placeDeltaGridBlocks(paintFunc, offsetX, deltaX, offsetY, deltaY)
  300.  
  301.     local turnRight = true
  302.     turtle.turnRight()
  303.     for i = 1, offsetX do
  304.         turtleForward()
  305.     end
  306.     turtle.turnLeft()
  307.     for i = 1, offsetY do
  308.         turtleForward()
  309.     end
  310.  
  311.     local xWidth = width - (2 * offsetX)
  312.     local yWidth = width - (2 * offsetY)
  313.  
  314.     local x = 0
  315.     while x < xWidth do
  316.         for y = 0, yWidth - 1 do
  317.             if y % deltaY == 0 then
  318.                 paintFunc()
  319.             end
  320.             if (y < yWidth - 1) then
  321.                 turtleForward()
  322.             end
  323.         end
  324.         if x < xWidth - 1 then
  325.             turnRight = turnAround(turnRight, deltaX)
  326.         end
  327.         x = x + deltaX
  328.     end
  329.  
  330.     turn(turnRight)
  331.     for i = 1, offsetX do
  332.         turtleForward()
  333.     end
  334.     turn(not turnRight)
  335.     for i = 1, offsetY do
  336.         turtleForward()
  337.     end
  338.  
  339.     returnToLevelStart(turnRight)
  340. end
  341.  
  342. local function placeGridBlocks(paintFunc)
  343.     placeDeltaGridBlocks(paintFunc, 1, 2, 1, 2)
  344. end
  345.  
  346. local function placeBetweenGridBlocks(paintFunc)
  347.     placeDeltaGridBlocks(paintFunc, 2, 4, 1, 2)
  348. end
  349.  
  350. local function placeBaseBlocks()
  351.     placeGridBlocks(placeBaseBlock)
  352. end
  353.  
  354. local function placeSandBlocks()
  355.     placeGridBlocks(placeSandBlock)
  356. end
  357.  
  358. local function placeCactusBlocks()
  359.     placeGridBlocks(placeCactusBlock)
  360. end
  361.  
  362. local function placeFenceBlocks()
  363.     placeBetweenGridBlocks(placeFenceBlock)
  364. end
  365.  
  366. local function placeTorchBlocks()
  367.     placeBetweenGridBlocks(placeTorchBlock)
  368. end
  369.  
  370.  
  371. local function digout2(height)
  372.     for i = 1, height do
  373.         digLevel(false, false)
  374.         if (doPaintWalls) then
  375.             paintLevelWalls()
  376.         end
  377.         if i < height then
  378.             turtleDown()
  379.         end
  380.     end
  381.     if (doPaintFloor) then
  382.         paintEntireFloor()
  383.     end
  384. end
  385.  
  386. local function paintWalls(height)
  387.     for i = 1, height - 1 do
  388.         turtleUp()
  389.     end
  390.     paintLevelWalls()
  391.     for i = 1, height - 1 do
  392.         turtleDown()
  393.         paintLevelWalls()
  394.     end
  395. end
  396.  
  397.  
  398. local function digout(height)
  399.     local i = 0
  400.     while (i < height) do
  401.         local delta = 0
  402.         if (height - i >= 3) then
  403.             turtleDown()
  404.             digLevel(true, true)
  405.             turtleDown()
  406.             delta = 3
  407.         elseif (height - i == 2) then
  408.             turtleDown()
  409.             digLevel(true, false)
  410.             delta = 2
  411.         elseif (height - i == 1) then
  412.             digLevel(false, false)
  413.             delta = 1
  414.         end
  415.         i = i + delta
  416.  
  417.         --        if (doPaintWalls) then
  418.         --            paintWalls(delta)
  419.         --        end
  420.         if i < height then
  421.             turtleDown()
  422.         end
  423.     end
  424.     if (doPaintFloor) then
  425.         paintEntireFloor()
  426.     end
  427. end
  428.  
  429. local function placeLevel()
  430.     turtleUp()
  431.     placeBaseBlocks()
  432.     turtleUp()
  433.     placeSandBlocks()
  434.     turtleUp()
  435.     placeCactusBlocks()
  436.     turtleUp()
  437.     placeFenceBlocks()
  438.     turtleUp()
  439.     placeTorchBlocks()
  440. end
  441.  
  442. local function moveUpDown(goingUp)
  443.     if (goingUp) then
  444.         turtleUp()
  445.     else
  446.         turtleDown()
  447.     end
  448. end
  449.  
  450. local function paintWalls2()
  451.     turtleUpX(roomHeight - 1)
  452.     turtle.turnLeft()
  453.     local goingUp = false
  454.     for k = 1, 4 do
  455.         for j = 1, width do
  456.             for i = 1, roomHeight do
  457.                 paintWall()
  458.                 if (i < roomHeight) then
  459.                     moveUpDown(goingUp)
  460.                 end
  461.             end
  462.             goingUp = not goingUp
  463.             if j < width then
  464.                 turtle.turnRight()
  465.                 turtleForward()
  466.                 turtle.turnLeft()
  467.             end
  468.         end
  469.         turtle.turnRight()
  470.     end
  471.     turtle.turnRight()
  472.     turtleDownX(roomHeight - 1)
  473. end
  474.  
  475.  
  476. digout(roomHeight)
  477.  
  478. if (doPaintWalls) then
  479.     paintWalls2()
  480. end
  481. turtleUp()
  482. for i = 1, levelCount do
  483.     placeLevel()
  484. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement