mikebald

IE Excavator Placer

Dec 23rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --------------------------------------
  2. -- A program to place the excavator
  3. -- from Immersive Engineering.
  4. --
  5. -- Needed items:
  6. --  Slot 1: 23 Steel Scaffoldings
  7. --  Slot 2: 21 Blocks of steel
  8. --  Slot 3: 13 Heavy Engineering Blocks
  9. --  Slot 4: 9 Light Engineering Blocks
  10. --
  11. -- Make sure you clear out a nice
  12. -- 8x3x7 area.
  13. --------------------------------------
  14.  
  15. local missingMessage = "Missing items: "
  16. local matrix = {
  17.     {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}},
  18.     {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 1, 1, 1, 2, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}},
  19.     {{0, 0, 3, 3, 3, 2, 4, 2}, {0, 1, 1, 1, 1, 1, 0, 2}, {0, 0, 3, 4, 3, 2, 3, 3}},
  20.     {{0, 0, 3, 3, 3, 2, 4, 2}, {2, 1, 1, 2, 1, 1, 2, 3}, {0, 0, 4, 4, 4, 2, 2, 4}},
  21.     {{0, 0, 1, 1, 1, 2, 4, 2}, {0, 1, 1, 1, 1, 1, 0, 2}, {0, 0, 0, 4, 0, 2, 3, 3}},
  22.     {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 2, 1, 1, 1, 2, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}},
  23.     {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 2, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}
  24. }
  25.  
  26. local blocks = {
  27.     {"Steel Scaffoldings", 23},
  28.     {"Steel Blocks", 21},
  29.     {"Heavy Engineering Blocks", 13},
  30.     {"Light Engineering Blocks", 9}}
  31.  
  32. --------------------------------------
  33. -- Don't touch things under here...
  34. -- ... yeah you!
  35. --------------------------------------
  36. function tableLen(T)
  37.   local cnt = 0
  38.   for _ in pairs(T) do cnt = cnt + 1 end
  39.   return cnt
  40. end
  41.  
  42. local maxHeight = tableLen(matrix)
  43. local maxWidth = tableLen(matrix[1])
  44. local maxDepth = tableLen(matrix[1][1])
  45.  
  46. local position = {forward=1, right=1, up=0, face=1}
  47. local facing = { "forward", "right", "back", "left" }
  48.  
  49. function _facingToNumber( direction )
  50.     if direction == "forward" then return 1 end
  51.     if direction == "right" then return 2 end
  52.     if direction == "back" then return 3 end
  53.     if direction == "left" then return 4 end
  54.     return 0
  55. end
  56.  
  57. function TurnTo( direction )
  58.     local directionNum = _facingToNumber( direction )
  59.    
  60.     while facing[position.face] ~= direction do
  61.         local difference = directionNum - position.face
  62.         if difference == -1 or difference == 3 then
  63.             turtle.turnLeft()
  64.             position.face = position.face - 1
  65.         else
  66.             turtle.turnRight()
  67.             position.face = position.face + 1
  68.         end
  69.        
  70.         if position.face == 5 then position.face = 1 end
  71.         if position.face == 0 then position.face = 4 end
  72.     end
  73. end
  74.  
  75. function Forward( amount )
  76.     amount = amount or 1
  77.     for i=1,amount do
  78.         assert(turtle.forward(), "I got stuck! Give me more fuel next time and/or clear the area. You have to rerun me from the begining.")
  79.         -- Update Position
  80.         if position.face == 1 then position.forward = position.forward + 1 end
  81.         if position.face == 3 then position.forward = position.forward - 1 end
  82.         if position.face == 2 then position.right = position.right + 1 end
  83.         if position.face == 4 then position.right = position.right - 1 end
  84.     end
  85. end
  86.  
  87. function Up( amount )
  88.     amount = amount or 1
  89.     for i=1,amount do
  90.         assert(turtle.up(), "I got stuck! Give me more fuel next time and/or clear the area. You have to rerun me from the begining.")
  91.         position.up = position.up + 1
  92.     end
  93. end
  94.  
  95. function Down( amount )
  96.     amount = amount or 1
  97.     for i=1,amount do
  98.         turtle.down()
  99.         position.up = position.up - 1
  100.     end
  101. end
  102.  
  103. function MoveTo(forward, right, up)
  104.     local forwardMovement = forward - position.forward
  105.     local rightMovement = right - position.right
  106.     local upMovement = up - position.up
  107.    
  108.     if forwardMovement > 0 then
  109.         TurnTo("forward")
  110.         Forward(forwardMovement)
  111.     end
  112.     if forwardMovement < 0 then
  113.         TurnTo("back")
  114.         Forward(math.abs(forwardMovement))
  115.     end
  116.     if rightMovement > 0 then
  117.         TurnTo("right")
  118.         Forward(rightMovement)
  119.     end
  120.     if rightMovement < 0 then
  121.         TurnTo("left")
  122.         Forward(math.abs(rightMovement))
  123.     end
  124.     if upMovement < 0 then
  125.         Down(upMovement)
  126.     end
  127.     if upMovement > 0 then
  128.         Up(math.abs(upMovement))
  129.     end
  130. end
  131.  
  132. -- Intial Fun
  133. term.clear()
  134. term.setCursorPos(1,1)
  135.  
  136. function checkForNeeded()
  137.     local toReturn = true
  138.     local cnt = 1
  139.     for i, v in pairs(blocks) do
  140.         toReturn = checkSlot(i, v[2], v[1]) and toReturn   
  141.     end
  142.     return toReturn
  143. end
  144.  
  145. function checkSlot(slotNum, itemAmount, itemName)
  146.     if turtle.getItemCount(slotNum) < itemAmount then
  147.         missingMessage = missingMessage .. "\n - " .. itemAmount .. " " .. itemName .. " [Slot " .. slotNum .. "]"
  148.         return false
  149.     else
  150.         return true
  151.     end
  152. end
  153.  
  154. function placeMatrix()
  155.     for height = 1,maxHeight do
  156.         for width = 1,maxWidth do      
  157.             for depth = 1,maxDepth do
  158.                 if matrix[height][width][depth] > 0 then
  159.                     MoveTo(depth, width, height)
  160.                     placeBlock(matrix[height][width][depth])
  161.                 end
  162.             end
  163.         end
  164.     end
  165. end
  166.  
  167. function placeBlock(slotNum)
  168.     if slotNum > 0 then
  169.         turtle.select(slotNum)
  170.         assert(turtle.placeDown(), "Either clear the area or give me permission to place a block. You'll have to start over.")
  171.     end
  172. end
  173.  
  174. -- Do all the stuff!
  175. if not checkForNeeded() then
  176.     print(missingMessage)
  177.     return
  178. end
  179.  
  180. print("Running...")
  181. placeMatrix()
  182. print("Finished running...")
Add Comment
Please, Sign In to add comment