Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- builds a room with inner dimensions width, depth, height as specified by args out of the given materials
- -- supply with:
- -- slot 1: nothing (used to store wall materials
- -- slot 2: resupply chest
- -- slot 3: refuel chest
- -- slot 4: dump chest
- -- and put into bottom front left corner of the room
- local swall = 1
- local ssupply = 2
- local sfuel = 3
- local sdump = 4
- local sfree = math.max(sdump,sfuel,ssupply,swall)+1
- local args = {...}
- local w, d, h = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
- function dump()
- local isFull = true
- for s = 16,sfree,-1 do
- if turtle.getItemCount(s) == 0 then
- isFull = false
- break
- end
- end
- if isFull then
- print("dumping inventory")
- -- put down dump chest
- turtle.select(sdump)
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- -- drop stuff
- for s = 16,sfree,-1 do
- if turtle.getItemCount(s) > 0 then
- turtle.select(s)
- while not turtle.dropDown() do
- sleep(0.2)
- end
- end
- end
- -- pick up dump chest
- turtle.select(sdump)
- turtle.digDown()
- turtle.select(swall)
- end
- end
- function refuel(level)
- if turtle.getFuelLevel() < level then
- -- free up inventory
- dump()
- -- put down fuel chest
- print("refueling")
- turtle.select(sfuel)
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- -- get fuel from chest
- while turtle.getFuelLevel() < level do
- if turtle.getItemCount(sfuel) < 1 then
- turtle.suckDown()
- end
- turtle.refuel(1)
- print("fueled up to "..turtle.getFuelLevel().." / "..level)
- end
- -- put remaining fuel back in chest and move it out of the way if the chest is full
- turtle.dropDown()
- if turtle.getItemCount(sfuel) > 0 then
- for s = 16,sfree,-1 do
- if turtle.transferTo(s) then
- break
- end
- end
- if turtle.getItemCount(sfuel) > 0 then
- turtle.select(sfuel)
- turtle.drop()
- end
- end
- -- pick up fuel chest
- turtle.select(sfuel)
- turtle.digDown()
- turtle.select(swall)
- end
- end
- function resupply()
- -- free up inventory
- dump()
- print("resupplying")
- -- remove any items that could be in the way
- if turtle.getItemCount(swall) > 0 then
- turtle.select(swall)
- for s = 16,sfree,-1 do
- if turtle.transferTo(s) then
- break
- end
- end
- if turtle.getItemCount(swall) > 0 then
- turtle.select(swall)
- turtle.drop()
- end
- end
- -- put down supply chest
- turtle.select(ssupply)
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- -- get supplies
- turtle.select(swall)
- turtle.suckDown()
- -- pick up supply chest
- turtle.select(ssupply)
- if turtle.getItemCount(ssupply) > 0 then
- for s = 16,sfree,-1 do
- if turtle.transferTo(s) then
- break
- end
- end
- if turtle.getItemCount(ssupply) > 0 then
- turtle.select(ssupply)
- turtle.drop()
- end
- end
- turtle.select(ssupply)
- turtle.digDown()
- turtle.select(swall)
- end
- -- movement functions
- function left(turns)
- if turns == nil then
- turns = 1
- end
- for i = 1,turns do
- turtle.turnLeft()
- end
- end
- function right(turns)
- if turns == nil then
- turns = 1
- end
- for i = 1,turns do
- turtle.turnRight()
- end
- end
- function move(dir, dist)
- if dist == nil then
- dist = 1
- end
- for d = 1, dist do
- if dir == "f" then
- while not turtle.forward() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- elseif dir == "u" then
- while not turtle.up() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- sleep(0.2)
- end
- elseif dir == "d" then
- while not turtle.down() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- elseif turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- elseif dir == "b" then
- while not turtle.back() do
- if turtle.getFuelLevel() == 0 then
- refuel(1000)
- else
- left(2)
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- left(2)
- break
- end
- end
- end
- end
- end
- function buildCeiling(w,d)
- for cw = 1,w do
- for cd = 1,d do
- --replace ceiling if necessary
- if not turtle.compareUp() then
- while not turtle.placeUp() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- sleep(0.2)
- end
- end
- -- check if supplies are still okay
- if turtle.getItemCount(swall) == 0 then
- resupply()
- end
- -- dig floor
- if turtle.detectDown() then
- turtle.digDown()
- end
- -- move forward if not at end, else turn to next line
- if cd < d then
- move("f")
- elseif cw < w then
- if cw % 2 == 1 then
- right()
- move("f")
- right()
- else
- left()
- move("f")
- left()
- end
- end
- end
- end
- end
- function buildWallSegment(w,d,h,sh)
- m1, m2 = w, d
- if w%2 == 0 then
- right()
- else
- left()
- end
- dir = "f"
- for i=1,4 do
- if i == 3 then
- dir = "b"
- m1, m2 = m2, m1
- end
- for c1 = 1,m1 do
- if i < 3 then
- if turtle.detectUp() then
- if not turtle.compareUp() then
- turtle.digUp()
- dump()
- end
- end
- if turtle.detectDown() then
- if not turtle.compareDown() then
- turtle.digDown()
- dump()
- end
- end
- else
- if not turtle.compareUp() then
- while not turtle.placeUp() do
- if turtle.detectUp() then
- turtle.digUp()
- dump()
- else
- turtle.attackUp()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- end
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- end
- end
- end
- if c1 < m1 then
- move(dir)
- if i>2 then
- if not turtle.compare() then
- while not turtle.place() do
- if turtle.detect() then
- turtle.dig()
- dump()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- end
- end
- end
- end
- if w%2 == 0 then
- if i < 3 then
- right()
- else
- left()
- end
- else
- if i < 3 then
- left()
- else
- right()
- end
- end
- for c2 = 1,m2 do
- if i<3 then
- if turtle.detectUp() then
- if not turtle.compareUp() then
- turtle.digUp()
- dump()
- end
- end
- if turtle.detectDown() then
- if not turtle.compareDown() then
- turtle.digDown()
- dump()
- end
- end
- else
- if not turtle.compareUp() then
- while not turtle.placeUp() do
- if turtle.detectUp() then
- turtle.digUp()
- dump()
- else
- turtle.attackUp()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- end
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- end
- end
- end
- if ((c2 < m2) and (i < 4)) or ((c2 < m2-1) and (i==4)) then
- move(dir)
- if i>2 then
- if not turtle.compare() then
- while not turtle.place() do
- if turtle.detect() then
- turtle.dig()
- dump()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(1) < 1 then
- resupply()
- end
- end
- end
- end
- end
- if (i==1) or (i==3) then
- if w%2 == 0 then
- if i < 3 then
- right()
- else
- left()
- end
- else
- if i < 3 then
- left()
- else
- right()
- end
- end
- end
- end
- end
- function buildWalls(w,d,h)
- move("d")
- local segments = math.floor((h-1)/3)
- local sh = 3
- for s = 1,segments+1 do
- if s==segments then
- sh = (h-1)%3
- end
- buildWallSegment(w,d,h,sh)
- if s <= segments then
- if w%2 == 0 then
- right()
- else
- left()
- end
- move("f")
- left(2)
- while not turtle.place() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(swall) < 1 then
- resupply()
- end
- move("d",sh)
- move("f")
- if w%2 == 0 then
- left()
- else
- right()
- end
- move("f")
- if w%2 == 0 then
- right()
- else
- left()
- end
- end
- end
- if w%2 == 0 then
- right()
- else
- left()
- end
- move("f")
- left(2)
- while not turtle.place() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(swall) < 1 then
- resupply()
- end
- left(2)
- end
- function clearInside(w,d,h)
- move("u",h-2)
- layers = math.floor(h/3)
- ch = 3
- m1, m2 = w, d
- for l=1,layers+1 do
- if l == layers then
- ch = (h+1)%3
- end
- for c1 = 1,m1 do
- for c2 = 1,m2 do
- if turtle.detectUp() then
- turtle.digUp()
- dump()
- end
- if l==layers+1 then
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- if turtle.getItemCount(swall) < 1 then
- resupply()
- end
- if not turtle.compareDown() then
- while not turtle.placeDown() do
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- else
- turtle.attackDown()
- end
- sleep(0.2)
- end
- end
- end
- else
- if turtle.detectDown() then
- turtle.digDown()
- dump()
- end
- end
- if c2 < m2 then
- move("f")
- end
- end
- if c1 < m1 then
- if m1%2 == 1 then
- right()
- move("f")
- right()
- else
- left()
- move("f")
- left()
- end
- end
- end
- if l <= layers then
- move("d",ch)
- if m1%2 == 1 then
- left(2)
- else
- right()
- m1, m2 = m2, m1
- end
- end
- end
- end
- if h > 3 then
- print("building a "..w.." wide, "..d.." deep, "..h.." high room.")
- -- refuel
- refuel(w*d*h)
- resupply()
- -- goto one block below top left front corner
- for i = 1,2 do
- left()
- move("f")
- end
- left(2)
- move("u",h-1)
- turtle.select(swall)
- buildCeiling(w+2,d+2)
- buildWalls(w+2,d+2,h+2)
- clearInside(w,d,h)
- end
Advertisement
Add Comment
Please, Sign In to add comment