Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pos = { x=0, y=0, z=0,
- facing = 0, --clockwise
- iter = 0
- }
- iterCounter = 0
- filename = "3dBuilder.txt"
- Xdir = true
- Ydir = true
- wasPlacedUp = 0
- wasPlacedDown = 0
- firstFloor = 0
- getRoundX = false
- continue = false
- currDebugMess=1
- debugMess = {"","","","","","","","","","","","",""}
- function sendToDebug(str)
- debugMess[currDebugMess] = str
- if currDebugMess < #debugMess then currDebugMess=currDebugMess+1
- else currDebugMess = 1 end
- end
- function printDebug()
- term.clear()
- messNum=currDebugMess
- for i=1,#debugMess do
- term.setCursorPos(1,i)
- term.write(debugMess[messNum])
- if messNum < #debugMess then messNum=messNum+1
- else messNum = 1 end
- end
- end
- function save()
- file=fs.open(filename, "w")
- file.write(textutils.serialize(pos))
- file.close()
- end
- function wantToContinue()
- if fs.exists(filename) then
- term.clear()
- term.setCursorPos(1,1)
- file=fs.open(filename, "r")
- data = textutils.unserialize(file.readAll())
- file.close()
- print("There are unfinished project. Information:")
- print("Relative position (x,y,z) = "..data.x..","..data.y..","..data.z)
- facing = "forward"
- if data.facing == 1 then facing = "right"
- elseif data.facing == 2 then facing = "back"
- elseif data.facing == 3 then facing = "left" end
- print("Relative facing = " .. facing)
- print("Iteration = "..data.iter)
- print("Are you want to continue? Then place me on that position and press y.")
- print("Else press n. It will destroy all saved data!")
- answer = read()
- if answer == "n" then
- fs.delete(filename)
- else
- pos.x=data.x pos.y=data.y pos.z=data.z pos.facing=data.facing pos.iter=data.iter
- continue = true
- end
- end
- end
- function sendAlarm(text)
- term.clear()
- term.setCursorPos(1,1)
- print(text)
- print("Press Enter to continue")
- read()
- term.clear()
- end
- function sortInv()
- for i=1,16 do
- itemCount = turtle.getItemCount(i)
- if turtle.getItemSpace(i) ~= 0 then
- for j=16,i+1,-1 do
- turtle.select(i)
- if turtle.getItemCount(j)~=0 and (turtle.getItemCount(i) == 0 or turtle.compareTo(j)) then
- turtle.select(j)
- turtle.transferTo(i)
- if turtle.getItemSpace(i) == 0 then break end
- end
- end
- end
- if turtle.getItemCount(i) == 0 then break end
- end
- turtle.select(1)
- end
- function cantPlace(n)--side, 0=up, 1=down, 2=front
- sortInv()
- if turtle.getItemCount(1) == 0 then toChest()
- else
- side = ""
- if n==0 and turtle.detectUp() then
- side = "up"
- elseif n==1 and turtle.detectDown() then
- side = "down"
- elseif n==2 and turtle.detect() then
- side = "front"
- end
- if side ~= "" then
- sendAlarm("I cant place the block in " .. side .. " direction!")
- end
- end
- end
- function cantGo(n) --direction, 0-up, 1-down, 2-forward
- error = "I cant go "
- direction = "forward. "
- if n == 0 then direction = "up. " elseif n == 1 then direction = "down. " end
- if turtle.getFuelLevel() == 0 then
- sortInv()
- turtle.select(16)
- repeat
- sendAlarm(error..direction.."Put fuel to my 16 slot plz.")
- turtle.refuel()
- until turtle.getFuelLevel() ~= 0
- turtle.select(1)
- else
- sendAlarm(error..direction.."Fuel level="..turtle.getFuelLevel())
- end
- end
- function forward(n)
- for i=1,n do
- success = false
- repeat
- success = turtle.forward()
- if success == false then cantGo(2) end
- until success == true
- if pos.facing == 0 then pos.x = pos.x + 1
- elseif pos.facing == 1 then pos.y = pos.y + 1
- elseif pos.facing == 2 then pos.x = pos.x - 1
- else pos.y = pos.y - 1 end
- end
- wasPlacedUp = 0
- wasPlacedDown = 0
- end
- function up(n)
- for i=1,n do
- success = false
- repeat
- success = turtle.up()
- if success == false then cantGo(0) end
- until success == true
- pos.z = pos.z + 1
- end
- end
- function down(n)
- for i=1,n do
- success = false
- repeat
- success = turtle.down()
- if success == false then cantGo(1) end
- until success == true
- pos.z = pos.z - 1
- end
- end
- function turnLeft()
- turtle.turnLeft()
- pos.facing = pos.facing - 1
- if pos.facing < 0 then pos.facing = 3 end
- end
- function turnRight()
- turtle.turnRight()
- pos.facing = pos.facing + 1
- if pos.facing > 3 then pos.facing = 0 end
- end
- function placeUp()
- if turtle.placeUp() == false then cantPlace(0) turtle.placeUp() end
- end
- function placeDown()
- if turtle.placeDown() == false then cantPlace(1) turtle.placeDown() end
- end
- function place()
- if turtle.place() == false then cantPlace(2) turtle.place() end
- end
- function placeMid()
- forward(1)
- turnLeft()
- turnLeft()
- place()
- end
- function setFacing(n)
- if pos.facing ~= n then
- k = n - pos.facing
- if k < 0 then k = k + 4 end
- if k < 3 then
- while n ~= pos.facing do turnRight() end
- else
- while n ~= pos.facing do turnLeft() end
- end
- end
- end
- function setFacingX(bool)
- if bool then
- if Xdir then setFacing(0) else setFacing(2) end
- else
- if Xdir then setFacing(2) else setFacing(0) end
- end
- end
- function setFacingY(bool)
- if bool then
- if Ydir then setFacing(1) else setFacing(3) end
- else
- if Ydir then setFacing(3) else setFacing(1) end
- end
- end
- function useChest()
- while true do
- repeat
- i = 0
- sortInv()
- success = true
- while success do
- success = turtle.suckDown()
- i = i + 1
- end
- until i == 1
- if turtle.getItemCount(1) == 0 then
- sendAlarm("I need the blocks. Put them somewhere plz.")
- else break end
- end
- end
- function goZ(z)
- if pos.z ~= z then
- if z>pos.z then
- if wasPlacedUp==1 then setFacingY(true) forward(1) end
- up(z-pos.z)
- else
- if wasPlacedDown==1 then setFacingY(true) forward(1) end
- down(pos.z-z) end
- end
- end
- function goX(x)
- if pos.x < x then
- setFacing(0)
- else
- setFacing(2)
- end
- forward(math.abs(x - pos.x))
- end
- function goY(y)
- if pos.y ~= y then
- if pos.y < y then
- setFacing(1)
- else
- setFacing(3)
- end
- forward(math.abs(y - pos.y))
- end
- end
- function goto(x,y,z)
- sendToDebug("["..pos.x..","..pos.y..","..pos.z.."] --> ["..x..","..y..","..z.."]")
- sendToDebug("Xdir="..tostring(Xdir)..", Ydir="..tostring(Ydir))
- if x == pos.x then
- goZ(z) goY(y)
- else
- if (pos.x < x and Xdir == false) or (pos.x > x and Xdir) then
- if Xdir then goZ(pos.z+2) getRoundX = true goX(x) goY(y) goZ(z)
- else goZ(z) goY(y) goX(x) end
- else
- if Xdir then
- if getRoundX then goZ(z+2) goY(y) setFacingY(true) forward(1) goX(x) goZ(z) setFacingY(false) forward(1) getRoundX=false
- else goZ(z) goX(x) goY(y) end
- else goX(x) goY(y) goZ(z) end
- end
- end
- end
- function toChest()
- oldPos = { x=pos.x,y=pos.y,z=pos.z,facing=pos.facing }
- goto(-1,0,1)
- useChest()
- goto(oldPos.x,oldPos.y,oldPos.z)
- setFacing(oldPos.facing)
- end
- function updateXdir(z)
- z = z + 1
- level = 1
- if firstFloor == 0 then
- surplus = z%3
- level = (z-surplus)/3
- if surplus ~= 0 then level = level + 1 end
- elseif z-1>firstFloor then
- mainFloors = z - firstFloor
- surplus = mainFloors%3
- level = (mainFloors-surplus)/3 + 1
- if surplus ~= 0 then level = level + 1 end
- end
- if level%2 ~=0 then Xdir = true else Xdir = false end
- --sendToDebug("Xdir="..Xdir..", level="..level..",fF="..firstFloor)
- end
- function updateYdir(x)
- surplus = x%2
- if (surplus==0 and Xdir) or (surplus ~= 0 and Xdir == false) then Ydir = true else Ydir = false end
- end
- function g(x,y,z,_placeUp,_placeDown,_placeMid)
- if continue then
- iterCounter = iterCounter+1
- if iterCounter >= pos.iter then continue = false
- updateXdir(z)
- updateYdir(x)
- setFacingY(true)
- wasPlacedUp = _placeUp
- wasPlacedDown = _placeDown
- end
- else
- needToUpdXdir = false
- if z~=pos.z then needToUpdXdir = true end
- goto(x,y,z)
- sendToDebug("need to place (up,down,mid) = ".. _placeUp .. ",".._placeDown..",".._placeMid)
- if needToUpdXdir then updateXdir(z) end
- updateYdir(x)
- setFacingY(true)
- if _placeUp == 1 then placeUp() end
- wasPlacedUp = _placeUp
- if _placeDown == 1 then placeDown() end
- wasPlacedDown = _placeDown
- if _placeMid == 1 then placeMid() end
- pos.iter = pos.iter + 1
- save()
- end
- end
- function viewDebug()
- while true do
- event, scancode = os.pullEvent("key")
- if scancode == 32 then printDebug() end --"D"
- end
- end
- function main()
- wantToContinue()
- --MAIN CODE HERE
- end
- parallel.waitForAny(main,viewDebug)
Advertisement
Add Comment
Please, Sign In to add comment