Advertisement
Wyvern67

World eater

Jan 14th, 2021
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. -- Demo : https://streamable.com/f261pq
  2.  
  3. term.clear()
  4. term.setCursorPos(1,1)
  5.  
  6. -- 1:  Creative motor set at speed 64
  7. -- 12: Campfire
  8. local slowness = 12
  9.  
  10. local state = {
  11.     data = {
  12.     }
  13. }
  14.  
  15. local function delay(to_wait)
  16.     local wait_time = 0.1
  17.     local progress = 0
  18.  
  19.     local filled_bar = "========================="
  20.     local empty_bar  = "                 "
  21.     local c1 = string.sub(filled_bar, -1)
  22.     local c2 = string.sub(empty_bar, -1)
  23.     local bar_length = string.len(filled_bar)
  24.     local increments = bar_length / (to_wait/wait_time)
  25.     local bar_progress = 0
  26.     local bar = ""
  27.    
  28.     while progress <= to_wait do
  29.         sleep(wait_time)
  30.         progress = progress+wait_time
  31.  
  32.         bar_progress = math.min(bar_length, bar_progress + increments)
  33.         bar = string.rep(c1, math.floor(bar_progress)) .. string.rep(c2, math.ceil(bar_length - bar_progress))
  34.  
  35.  
  36.         term.setCursorPos(1,13)
  37.         term.write(string.format("%.1f", progress) .. "   [" .. bar .. "]   " .. string.format("%.1f", to_wait))
  38.         term.setCursorPos(1,1)
  39.     end
  40. end
  41.  
  42. function state:save()
  43.     local txt = textutils.serialize(self.data)
  44.     local f = fs.open("state", "w")
  45.     f.write(txt)
  46.     f.close()
  47. end
  48.  
  49. function state:load()
  50.     local f = fs.open("state", "r")
  51.     if not f then
  52.         return nil
  53.     end
  54.     local txt = f.readAll()
  55.     self.data = textutils.unserialize(txt)
  56.     f.close()
  57.  
  58.     return self.data
  59. end
  60.  
  61. function state:print()
  62.     term.clear()
  63.     term.setCursorPos(1,1)
  64.     print(textutils.serialize(self.data))
  65. end
  66.  
  67.  
  68. function state:set(k, v)
  69.     self.data[k] = v
  70.     self:save()
  71.  
  72.     self:print()
  73.  
  74.     return true
  75. end
  76.  
  77. function state:extrapolate_current_state()
  78.     local front = turtle.detect()
  79.     turtle.turnRight()
  80.     turtle.turnRight()
  81.     local back = turtle.detect()
  82.     turtle.turnRight()
  83.     turtle.turnRight()
  84.  
  85.     -- 00
  86.     if not front and not back then
  87.         self:set("error", true)
  88.     end
  89.  
  90.     -- 01
  91.     if not front and back then
  92.         self:set("backed", true)
  93.         self:set("coupled", false)
  94.     end
  95.  
  96.     -- 1X
  97.     if front then
  98.         self:set("front_extended", false)
  99.         self:set("backed", false)
  100.  
  101.         self:set("coupled", back)
  102.     end
  103. end
  104.  
  105. function state:forward()
  106.     self:set("action", "retracting " .. side)
  107.  
  108.     return turtle.forward
  109. end
  110.  
  111. function state:extend(side)
  112.     self:set("action", "extending " .. side)
  113.     if side == "front" then
  114.         if not turtle.detect() then
  115.             self:set("front_extended", true)
  116.             self:set("coupled", false)
  117.  
  118.             return true
  119.         end
  120.  
  121.         if turtle.dig() then
  122.             self:set("front_extended", true)
  123.             self:set("coupled", false)
  124.             delay(slowness*0.5)
  125.  
  126.             return true
  127.         end
  128.     elseif side == "back" then
  129.         rs.setOutput("back", true)
  130.         self:set("backed", true)
  131.         self:set("coupled", false)
  132.         delay(slowness*0.5) --extending piston
  133.         self:set("action", "digging")
  134.         delay(1.2)
  135.         return true
  136.     end
  137. end
  138.  
  139. function state:retract(side)
  140.     self:set("action", "retracting " .. side)
  141.  
  142.     if side == "front" then
  143.         if turtle.place() then
  144.             self:set("front_extended", false)
  145.             self:set("backed", false)
  146.             self:set("coupled", true)
  147.             delay(slowness*0.5)
  148.  
  149.             return true
  150.         end
  151.     elseif side == "back" then
  152.         rs.setOutput("back", false)
  153.         return true
  154.     end
  155. end
  156.  
  157. function state:act()
  158.     local error          = state.data.error
  159.     local coupled        = state.data.coupled
  160.     local uncoupled      = not state.data.coupled
  161.     local backed         = state.data.backed
  162.     local front_extended = state.data.front_extended
  163.  
  164.     self:print()
  165.  
  166.     if error then
  167.         delay(slowness*1)
  168.     end
  169.  
  170.     if coupled == false and backed then
  171.         state:set("action", "moving forward")
  172.         if turtle.forward() then
  173.             self:set("backed", false)
  174.         end
  175.     elseif coupled == false and backed == false then
  176.         if front_extended then
  177.             self:retract("front")
  178.         else
  179.             self:extend("front")
  180.         end
  181.     elseif coupled then
  182.         self:extend("back")
  183.     end
  184. end
  185.  
  186. --if true then return end
  187.  
  188. if true or not state:load() then
  189.     state:extrapolate_current_state()
  190. end
  191.  
  192. state:print()
  193. --if true then return end
  194.  
  195. while true do
  196.     state:act()
  197.     delay(slowness*0.1)
  198. end
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement