TheKojent

platform.lua

Mar 29th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. os.loadAPI("check") --http://pastebin.com/Sc2TWpeH
  2. os.loadAPI("try") --http://pastebin.com/ADttMQXz
  3.  
  4. local tArgs = { ... }
  5. if #tArgs ~= 2 then
  6.   print("Usage: platform <length> <width>")
  7.   return
  8. end
  9.  
  10. x = tonumber(tArgs[1])
  11. z = tonumber(tArgs[2])
  12.  
  13. if not check.whole(x) or not check.positive(x) then
  14.   print("Improper arguments!")
  15.   return
  16. end
  17.  
  18. if not check.whole(z) or not check.positive(z) then
  19.   print("Improper arguments!")
  20.   return
  21. end
  22.  
  23. --Checking for block amount
  24. local total = x * z
  25. local inv = 0
  26. local nItems = 0
  27. for i = 1,16 do
  28.   nItems = turtle.getItemCount(i)
  29.   inv = inv + nItems
  30. end
  31. if total > inv then
  32.   print("Not enough blocks!")
  33.   return
  34. end
  35.  
  36. local function tryPlace()
  37.   while not turtle.placeDown() do
  38.     if turtle.detectDown() then
  39.       if not turtle.digDown() then
  40.         print("Cannot place block, exiting script!")
  41.         return false
  42.       end
  43.     elseif not turtle.attackDown() then
  44.       sleep(0.5)
  45.     end
  46.   end
  47.   return true
  48. end
  49.  
  50. local function forward()
  51.   if not try.forward() then
  52.     print("Cannot move forward, exiting script!")
  53.     return false
  54.   end
  55.   return true
  56. end
  57.  
  58. local slot = 1
  59.  
  60. function checkSlot()
  61.   turtle.select(slot)
  62.   while turtle.getItemCount(slot) == 0 do
  63.     slot = slot + 1
  64.     turtle.select(slot)
  65.   end
  66. end
  67.  
  68. --Start building
  69. local dir = true
  70. local done = false
  71. for j = 1,z-1 do
  72.   for k = 1,x-1 do
  73.     checkSlot()
  74.     if not tryPlace() then return end
  75.     if not forward() then return end
  76.   end
  77.   if not tryPlace() then return end
  78.   if dir then
  79.     turtle.turnRight()
  80.     if not forward() then return end
  81.     turtle.turnRight()
  82.   else
  83.     turtle.turnLeft()
  84.     if not forward() then return end
  85.     turtle.turnLeft()
  86.   end
  87.   dir = not dir
  88. end
  89. for k = 1,x-1 do
  90.   checkSlot()
  91.   if not tryPlace() then return end
  92.   if not forward() then return end
  93. end
  94. if not tryPlace() then return end
  95.  
  96. print("Platform built! Used " .. tostring(total) .. " blocks.")
Advertisement
Add Comment
Please, Sign In to add comment