Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Demo : https://streamable.com/f261pq
- term.clear()
- term.setCursorPos(1,1)
- -- 1: Creative motor set at speed 64
- -- 12: Campfire
- local slowness = 12
- local state = {
- data = {
- }
- }
- local function delay(to_wait)
- local wait_time = 0.1
- local progress = 0
- local filled_bar = "========================="
- local empty_bar = " "
- local c1 = string.sub(filled_bar, -1)
- local c2 = string.sub(empty_bar, -1)
- local bar_length = string.len(filled_bar)
- local increments = bar_length / (to_wait/wait_time)
- local bar_progress = 0
- local bar = ""
- while progress <= to_wait do
- sleep(wait_time)
- progress = progress+wait_time
- bar_progress = math.min(bar_length, bar_progress + increments)
- bar = string.rep(c1, math.floor(bar_progress)) .. string.rep(c2, math.ceil(bar_length - bar_progress))
- term.setCursorPos(1,13)
- term.write(string.format("%.1f", progress) .. " [" .. bar .. "] " .. string.format("%.1f", to_wait))
- term.setCursorPos(1,1)
- end
- end
- function state:save()
- local txt = textutils.serialize(self.data)
- local f = fs.open("state", "w")
- f.write(txt)
- f.close()
- end
- function state:load()
- local f = fs.open("state", "r")
- if not f then
- return nil
- end
- local txt = f.readAll()
- self.data = textutils.unserialize(txt)
- f.close()
- return self.data
- end
- function state:print()
- term.clear()
- term.setCursorPos(1,1)
- print(textutils.serialize(self.data))
- end
- function state:set(k, v)
- self.data[k] = v
- self:save()
- self:print()
- return true
- end
- function state:extrapolate_current_state()
- local front = turtle.detect()
- turtle.turnRight()
- turtle.turnRight()
- local back = turtle.detect()
- turtle.turnRight()
- turtle.turnRight()
- -- 00
- if not front and not back then
- self:set("error", true)
- end
- -- 01
- if not front and back then
- self:set("backed", true)
- self:set("coupled", false)
- end
- -- 1X
- if front then
- self:set("front_extended", false)
- self:set("backed", false)
- self:set("coupled", back)
- end
- end
- function state:forward()
- self:set("action", "retracting " .. side)
- return turtle.forward
- end
- function state:extend(side)
- self:set("action", "extending " .. side)
- if side == "front" then
- if not turtle.detect() then
- self:set("front_extended", true)
- self:set("coupled", false)
- return true
- end
- if turtle.dig() then
- self:set("front_extended", true)
- self:set("coupled", false)
- delay(slowness*0.5)
- return true
- end
- elseif side == "back" then
- rs.setOutput("back", true)
- self:set("backed", true)
- self:set("coupled", false)
- delay(slowness*0.5) --extending piston
- self:set("action", "digging")
- delay(1.2)
- return true
- end
- end
- function state:retract(side)
- self:set("action", "retracting " .. side)
- if side == "front" then
- if turtle.place() then
- self:set("front_extended", false)
- self:set("backed", false)
- self:set("coupled", true)
- delay(slowness*0.5)
- return true
- end
- elseif side == "back" then
- rs.setOutput("back", false)
- return true
- end
- end
- function state:act()
- local error = state.data.error
- local coupled = state.data.coupled
- local uncoupled = not state.data.coupled
- local backed = state.data.backed
- local front_extended = state.data.front_extended
- self:print()
- if error then
- delay(slowness*1)
- end
- if coupled == false and backed then
- state:set("action", "moving forward")
- if turtle.forward() then
- self:set("backed", false)
- end
- elseif coupled == false and backed == false then
- if front_extended then
- self:retract("front")
- else
- self:extend("front")
- end
- elseif coupled then
- self:extend("back")
- end
- end
- --if true then return end
- if true or not state:load() then
- state:extrapolate_current_state()
- end
- state:print()
- --if true then return end
- while true do
- state:act()
- delay(slowness*0.1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement