Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local size,side,blockType = ...
- -- [S] = ^
- -- [E] = stop
- --| > > > > > > > v
- --| ^ > > > > > v v
- --| ^ ^ > > > v v v
- --| ^ ^ ^ > v v v v
- --| ^ ^ ^[S]v v v v
- --| ^ ^ ^ < < v v v
- --| ^ ^ < < < < v v
- --| ^ < < < < < <[E]
- local function usage()
- textutils.pagedPrint([[Usage:
- platform <size> [down|up] <blockName>
- Build a square platform.]])
- end
- if size=="help" then
- usage()
- return
- end
- if not size or not tonumber(size) or tonumber(size)<1 then
- error("<size> Should be a positive number",0)
- usage()
- return
- end
- size = tonumber(size)
- local blockNeeds = size*size
- if not side then side = "down"
- elseif side~="down" and side~="up" then
- error("Wrong parameter\nside should be down or up",0)
- usage()
- return
- end
- local function supply(s)
- s = s or 1
- local _s
- for _s=s,16 do
- if blockType then
- local data = turtle.getItemDetail(_s)
- if data and data.name==blockType then
- return _s
- end
- elseif turtle.getItemCount(_s) then
- return _s
- end
- end
- return false
- end
- local function supplyCount()
- local total,s=0
- for s=1,16 do
- if blockType then
- local data = turtle.getItemDetail(s)
- if data and data.name==blockType then
- total = total + turtle.getItemCount(s)
- end
- else
- total = total + turtle.getItemCount(s)
- end
- end
- return total
- end
- local slot = supply()
- if not slot then
- error(blockType and "You should supply "..blockType or "You should supply blocks",-1)
- usage()
- return
- end
- local blockCount = supplyCount()
- if blockNeeds > 16*64 then
- print(string.format([[A %dx%d platform require %d blocks.
- A Turtle can't supply more than %d blocks.
- Continue ? Y/N]],size,size,blockNeeds,16*64))
- local confirm=false
- while not confirm do
- local e,k=os.pullEvent("key")
- if k==keys.n then sleep(.3) return end
- confirm = k==keys.y
- sleep(.3)
- end
- print("Yes")
- elseif blockNeeds > blockCount then
- print(string.format([[A %dx%d platform require %d blocks.
- You should put %d blocks more.
- Continue ? Y/N]],size,size,blockNeeds,blockNeeds-blockCount))
- local confirm=false
- while not confirm do
- local e,k=os.pullEvent("key")
- if k==keys.n then sleep(.3) return end
- confirm = k==keys.y
- sleep(.3)
- end
- print("Yes")
- end
- function build()
- local place,detect
- if side=="up" then
- place = function() return turtle.placeUp() end
- detect = function() return turtle.detectUp() end
- else
- place = function() return turtle.placeDown() end
- detect = function() return turtle.detectDown() end
- end
- local sections = (size*2)-1
- local step,step_z,length
- local last_x, last_z
- for step=1,sections do
- local length=math.ceil(step/2)
- turtle.turnRight()
- for step_z=1,length do
- if turtle.getItemCount(slot)<=0 then
- slot = supply(slot)
- if not slot then
- print("Not enough blocks...")
- return length,step_z
- end
- end
- if turtle.getSelectedSlot() ~= slot then
- turtle.select(slot)
- end
- while not place() and not detect() do
- slot = supply(slot)
- if not slot then
- print("Not enough placable blocks...")
- return length,step_z
- end
- turtle.select(slot)
- end
- if not (step==sections and step_z==length) then
- turtle.forward()
- last_x, last_z = length,step_z
- end
- end
- end
- return last_x, last_z
- end
- local last_x, last_z = build()
- -- Back to center
- local i
- local stepBack_x = last_x - math.ceil(size/2)
- local stepBack_z = last_z - math.floor(size/2)
- turtle.turnRight()
- for i=1,math.abs(stepBack_x) do
- if stepBack_x < 1 then turtle.back()
- else turtle.forward() end
- end
- turtle.turnRight()
- for i=1,math.abs(stepBack_z) do
- if stepBack_z < 1 then turtle.back()
- else turtle.forward() end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement