Advertisement
skypop

platform

Oct 7th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. local size,side,blockType = ...
  2.  
  3. -- [S] = ^
  4. -- [E] = stop
  5. --| > > > > > > > v
  6. --| ^ > > > > > v v
  7. --| ^ ^ > > > v v v
  8. --| ^ ^ ^ > v v v v
  9. --| ^ ^ ^[S]v v v v
  10. --| ^ ^ ^ < < v v v
  11. --| ^ ^ < < < < v v
  12. --| ^ < < < < < <[E]
  13.  
  14. local function usage()
  15.     textutils.pagedPrint([[Usage:
  16. platform <size> [down|up] <blockName>
  17.  
  18. Build a square platform.]])
  19. end
  20.  
  21. if size=="help" then
  22.     usage()
  23.     return
  24. end
  25.  
  26. if not size or not tonumber(size) or tonumber(size)<1 then
  27.     error("<size> Should be a positive number",0)
  28.     usage()
  29.     return
  30. end
  31. size = tonumber(size)
  32. local blockNeeds = size*size
  33.  
  34. if not side then side = "down"
  35. elseif side~="down" and side~="up" then
  36.     error("Wrong parameter\nside should be down or up",0)
  37.     usage()
  38.     return
  39. end
  40.  
  41. local function supply(s)
  42.     s = s or 1
  43.     local _s
  44.     for _s=s,16 do
  45.         if blockType then
  46.             local data = turtle.getItemDetail(_s)
  47.             if data and data.name==blockType then
  48.                 return _s
  49.             end
  50.         elseif turtle.getItemCount(_s) then
  51.             return _s
  52.         end
  53.     end
  54.     return false
  55. end
  56.  
  57. local function supplyCount()
  58.     local total,s=0
  59.     for s=1,16 do
  60.         if blockType then
  61.             local data = turtle.getItemDetail(s)
  62.             if data and data.name==blockType then
  63.                 total = total + turtle.getItemCount(s)
  64.             end
  65.         else
  66.             total = total + turtle.getItemCount(s)
  67.         end
  68.     end
  69.     return total
  70. end
  71.  
  72. local slot = supply()
  73. if not slot then
  74.     error(blockType and "You should supply "..blockType or "You should supply blocks",-1)
  75.     usage()
  76.     return
  77. end
  78.  
  79. local blockCount = supplyCount()
  80. if blockNeeds > 16*64 then
  81.     print(string.format([[A %dx%d platform require %d blocks.
  82. A Turtle can't supply more than %d blocks.
  83. Continue ? Y/N]],size,size,blockNeeds,16*64))
  84.     local confirm=false
  85.     while not confirm do
  86.         local e,k=os.pullEvent("key")
  87.         if k==keys.n then sleep(.3) return end
  88.         confirm = k==keys.y
  89.         sleep(.3)
  90.     end
  91.     print("Yes")
  92. elseif blockNeeds > blockCount then
  93.     print(string.format([[A %dx%d platform require %d blocks.
  94. You should put %d blocks more.
  95. Continue ? Y/N]],size,size,blockNeeds,blockNeeds-blockCount))
  96.     local confirm=false
  97.     while not confirm do
  98.         local e,k=os.pullEvent("key")
  99.         if k==keys.n then sleep(.3) return end
  100.         confirm = k==keys.y
  101.         sleep(.3)
  102.     end
  103.     print("Yes")
  104. end
  105.  
  106. function build()
  107.     local place,detect
  108.     if side=="up" then
  109.         place = function() return turtle.placeUp() end
  110.         detect = function() return turtle.detectUp() end
  111.     else
  112.         place = function() return turtle.placeDown() end
  113.         detect = function() return turtle.detectDown() end
  114.     end
  115.     local sections = (size*2)-1
  116.     local step,step_z,length
  117.     local last_x, last_z
  118.     for step=1,sections do
  119.         local length=math.ceil(step/2)
  120.         turtle.turnRight()
  121.         for step_z=1,length do
  122.             if turtle.getItemCount(slot)<=0 then
  123.                 slot = supply(slot)
  124.                 if not slot then
  125.                     print("Not enough blocks...")
  126.                     return length,step_z
  127.                 end
  128.             end
  129.             if turtle.getSelectedSlot() ~= slot then
  130.                 turtle.select(slot)
  131.             end
  132.             while not place() and not detect() do
  133.                 slot = supply(slot)
  134.                 if not slot then
  135.                     print("Not enough placable blocks...")
  136.                     return length,step_z
  137.                 end
  138.                 turtle.select(slot)
  139.             end
  140.             if not (step==sections and step_z==length) then
  141.                 turtle.forward()
  142.                 last_x, last_z = length,step_z
  143.             end
  144.         end
  145.     end
  146.     return last_x, last_z
  147. end
  148.  
  149. local last_x, last_z = build()
  150.  
  151. -- Back to center
  152. local i
  153. local stepBack_x = last_x - math.ceil(size/2)
  154. local stepBack_z = last_z - math.floor(size/2)
  155. turtle.turnRight()
  156. for i=1,math.abs(stepBack_x) do
  157.     if stepBack_x < 1 then turtle.back()
  158.     else turtle.forward() end
  159. end
  160. turtle.turnRight()
  161. for i=1,math.abs(stepBack_z) do
  162.     if stepBack_z < 1 then turtle.back()
  163.     else turtle.forward() end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement