Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...} --command line arguments table
- local resetPos = {} --table of arguments to set the persist values
- local prevPos = {} --table to hold the loaded saved previous values
- local height = tArgs[1] or prevHeight --sets height to equal, in order of precedence, tArgs[1] or prevHeight
- local width = tArgs[2] or prevWidth --sets width to equal, in order of precedence, tArgs[2] or prevWidth
- local depth = tArgs[3] or prevDepth --sets depth to equal, in order of precedence, tArgs[3] or prevDepth
- local prevHeight = prevPos[2] or 0 --sets the prevHeight equal to prevPos[2], but if it cant then it sets it equal to 0
- local prevWidth = prevPos[1] or 0 --same
- local prevDepth = prevPos[3] or 0 --same
- local persist = prevPos[4] or false --same except false.
- local currHeight = 0 --sets a changeable variable to 0 for saving
- local currWidth = 0 --sets a changeable variable to 0 for saving
- local currDepth = 0 --sets a changeable variable to 0 for saving
- local resetHeight = resetPos[2] or 0 --adjusts
- local resetWidth = resetPos[1] or 0
- local resetDepth = resetPos[3] or 0
- local m=0 --makes a multipurpose variable
- fs.makeDir("prevPos")
- fs.makeDir("resetPos")
- function loadPrev()
- local w = fs.open("prevPos/prevWidth","r")
- local h = fs.open("prevPos/prevHeight","r")
- local d = fs.open("prevPos/prevDepth","r")
- table.insert(prevPos,w)
- table.insert(prevPos,h)
- table.insert(prevPos,d)
- w.close()
- h.close()
- d.close()
- end
- function checkPersist()
- local p = fs.open("resetPos/persist","r")
- if p == nil then
- local pw = fs.open("resetPos/persist","w")
- if pw ~= nil then
- pw.write(persist)
- pw.close()
- end
- elseif p ~= nil then
- table.insert(prevPos,p)
- p.close()
- end
- end
- function forward() --makes a persistant forward function in case of gravel/sand/mob/player
- while turtle.forward() == false do --keeps looping if it can't move forward, stops when it does succesfully.
- if turtle.detect() then --detects for a block as obstruction
- turtle.dig() -- breaks block if detected
- else
- turtle.attack() --else attacks to move mob/player
- end
- end
- refuel() --checks if turtle needs fuel, fuels if needed
- end
- function moveRight() --moves turtle right one
- turtle.turnRight()
- forward()
- turtle.turnLeft()
- end
- function moveLeft() -- moves turtle left one
- turtle.turnLeft()
- forward()
- turtle.turnRight()
- end
- function up() --makes a persistant up function in case of gravel/sand/mob/player
- while turtle.up() == false do --loops to move up until succesful
- if turtle.detectUp() == true then --if block in the way
- turtle.digUp() --breaks it
- else --if mob/player is in the way
- turtle.attackUp() --attacks to move it
- end
- end
- end
- function down() -- persistant down function
- while turtle.down() == false do
- if turtle.detectDown() == true then
- turtle.digDown()
- else
- turtle.attack()
- end
- end
- end
- function digRow() --makes function to dig a row. saves turtle position in row. allows session persistance
- local w = fs.open("prevPos/prevWidth","w") --opens width save file for writing
- for i=1,math.abs(resetWidth-width)-1 do --loop to dig row. reacts for session persistance
- turtle.dig()
- if m==0 then --if it last moved left will move right
- moveRight()
- currWidth = currWidth+1 --currWidth increases towards right
- elseif m==1 then --if last moved right will move left
- moveLeft()
- currWidth = currWidth-1 --currWidth decreases towards left
- end
- checkInv()
- w.write(currWidth) --writes currWidth to width save file
- w.close() --closes width save file
- end
- turtle.dig() --breaks the last block in the row without possibly moving too far
- resetWidth=0
- end
- function nextRow() --function to move turtle up to the next row. saves turtle height. allows session persistence
- local h = fs.open("prevPos/prevHeight","w") --opens height save file for writing
- for i=1,math.abs(resetHeight-height)-1 do --loop to dig 1 depth of tunnel
- digRow() --initiates the digRow function
- if m==0 then
- m=1
- elseif m==1 then
- m=0
- end
- up() --when digRow is done moves turtle up to next row
- currHeight = currHeight+1 --currHeight increases going up
- h.write(currHeight) --writes currHeight to height save file
- h.close() --closes height save file
- end
- digRow()
- if m==0 then
- m=1
- elseif m==1 then
- m=0
- end
- resetHeight=0
- end
- function tunnel() --function to move the turtle deeper into tunnel. saves depth. allows session persistence
- local d = fs.open("prevPos/prevDepth","w") --opens depth save file for writing
- persist = true
- for i=1,math.abs(resetDepth-depth)-1 do --loop to dig tunnel
- nextRow() --initiates the function nextRow
- resetTurtlePos()
- forward()
- currDepth = currDepth+1 --currDepth increases further into tunnel
- d.write(currDepth) --writes currDepth to depth save file
- d.close() --closes depth save file.
- end
- nextRow() --digs final depth of tunnel
- resetTurtlePos()
- turtle.turnRight()
- turtle.turnRight()
- for i=1,math.abs(resetDepth-depth) do
- forward()
- end
- resetDepth=0
- end
- function resetTurtlePos() --function to reset the turtle so that the tunnel is straight. saves width and height. allows session persistence
- local h = fs.open("prevPos/prevHeight","w") --opens height save file for writing
- local w = fs.open("prevPos/prevWidth","w") --opens width save file for writing
- if m==1 then --if it just moved right
- for i=1,math.abs(resetHeight-height)-1 do --moves down, session persistence
- down()
- currHeight = currHeight-1 --currHeight decreases as move down
- h.write(currHeight) --writes currHeight to height save file
- h.close() --closes height save file
- end
- turtle.turnLeft()
- for i=1,math.abs(resetWidth-width)-1 do --moves left, session persistence
- forward()
- currWidth = currWidth-1 --currWidth decreases as move left
- w.write(currWidth) --writes currWidth to width save file
- w.close() --closes width save file
- end
- turtle.turnRight()
- elseif m==0 then --if it just moved left
- for i=1,math.abs(resetHeight-height) do --moves down, session persistence
- turtle.down()
- currHeight = currHeight-1 --currHeight decreases as move down
- h.write(currHeight) --write currHeight to height save file
- h.close() --closes height save file
- end
- end
- m=0
- end
- function cleanUp() --cleans inventory
- turtle.select(16) --selects enderchest
- turtle.turnRight()
- turtle.turnRight() --turns around
- turtle.place() --places enderchest
- for i=1,14 do --selects each slot 1 through 14 and drops items into enderchest
- turtle.select(i)
- turtle.drop()
- end
- end
- function checkInv() -- checks if cleanUp needs to happen
- if turtle.getItemCount(14) ~= 0 then --checks last possibly available inventory slot
- cleanUp() --runs cleanUp if inventory is full
- end
- end
- function refuel() --checks if turtle needs fuel
- if turtle.getFuelLevel() <400 then --if turtle has less than 400 fuel
- turtle.select(15) --turtle selects coal
- turtle.refuel(4) --refuels with 4
- turtle.select(1) --selects slot 1 again
- end
- end
- function sessionPersistence() --allows for session persistence in this program
- if resetTurtle == true then
- local w = fs.open("prevPos/prevWidth","w")
- local h = fs.open("prevPos/prevHeight","w")
- local d = fs.open("prevPos/prevDepth","w")
- local p = fs.open("prevPos/persist","w")
- w.write(nil)
- h.write(nil)
- d.write(nil)
- p.write(nil)
- w.close()
- h.close()
- d.close()
- p.close()
- elseif resetTurtle == false then
- loadPrev()
- end
- local w = fs.open("prevPos/prevWidth","r") --opens width save file for reading
- local h = fs.open("prevPos/prevHeight","r") --opens height save file for reading
- local d = fs.open("prevPos/prevDepth","r") --opens depth save file for reading
- table.insert(resetPos,w) --sets data from prevPos/prevWidth to the table resetPos in slot [1]
- table.insert(resetPos,h) --sets data from prevPos/prevHeight to the table resetPos in slot [2]
- table.insert(resetPos,d) --sets data from prevPos/prevDepth to the table resetPos in slot [3]
- if w~=nil and h~=nil and d~=nil then
- w.close()
- h.close()
- d.close()
- end
- end
- function run()
- checkPersist()
- if persist == true then
- print("Would you like to reset this program's persistence? true/false: ")
- local resetTurtle = read()
- end
- sessionPersistence()
- tunnel()
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment