Advertisement
GNOOR1S

Pocketcomputer Controlled - Strip Mining

Feb 2nd, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. rednet.open("left")
  2.  
  3. local fuelSlot = 16
  4. local mining = false
  5. local distance = 0
  6. local progress = 0
  7.  
  8. local _fileName = "mining.dat"
  9.  
  10. function fuel()
  11.     if turtle.getFuelLevel() < 10 then
  12.         turtle.select(fuelSlot)
  13.         turtle.refuel(1)
  14.         turtle.select(1)
  15.     end
  16.     if turtle.getFuelLevel() < 10 then
  17.         for i = 1, 16 do
  18.             turtle.select(i)
  19.             turtle.refuel(1)
  20.         end
  21.         turtle.select(1)
  22.     end
  23. end
  24.  
  25. function checkProgress()
  26.     if progress >= distance then
  27.         mining = false
  28.     end
  29. end
  30.  
  31. function createPacket(msg)
  32.     return {id = os.getComputerID(), msg = msg}
  33. end
  34.  
  35. function receiveCommand()
  36.     local e, id, msg = os.pullEvent("rednet_message")
  37.         if msg.command == "status" then
  38.             rednet.send(id, createPacket({work=mining,dist=distance,prog=progress}))
  39.         end
  40.         if msg.command == "start" then
  41.             mining = true
  42.             distance = msg.msg
  43.             progress = 0
  44.         end
  45.         if msg.command == "stop" then
  46.             mining = false
  47.             progress = 0
  48.             distance = 0
  49.             saveData()
  50.             os.reboot()
  51.         end
  52.         if msg.command == "turn" then
  53.             turtle.turnLeft()
  54.         end
  55.         if msg.command == "dump" then
  56.             dumpContent()
  57.         end
  58.         if msg.command == "down" then
  59.             turtle.down()
  60.         end
  61.     print(id..": "..textutils.serialize(msg))
  62. end
  63.  
  64. function dumpContent()
  65.     for i = 1, 15 do
  66.         turtle.select(i)
  67.         turtle.dropUp(64)
  68.     end
  69.     turtle.select(1)
  70. end
  71.  
  72. function loadData()
  73.     if fs.exists(_fileName) then
  74.         local file = fs.open(_fileName, "r")
  75.         local data = textutils.unserialize(file.readAll())
  76.         if data ~= nil then
  77.             progress = data.progress
  78.             distance = data.distance
  79.             mining = data.mining
  80.         end
  81.         file.close()
  82.     end
  83. end
  84.  
  85. function saveData()
  86.     local file = fs.open(_fileName, "w")
  87.     local data = {mining=mining, progress=progress, distance=distance}
  88.     file.write(textutils.serialize(data))
  89.     file.close()
  90. end
  91.  
  92. function dig()
  93.     if mining then
  94.         fuel()
  95.         turtle.dig()
  96.         while not turtle.forward() do turtle.dig() end
  97.         turtle.placeDown()
  98.         turtle.digUp()
  99.         turtle.up()
  100.         turtle.digUp()
  101.         turtle.down()
  102.         progress = progress + 1
  103.         checkProgress()
  104.         saveData()
  105.     end
  106.     sleep(.5)
  107. end
  108.  
  109. loadData()
  110. if distance > 0 then
  111.     dig()
  112. end
  113.  
  114. receiveCommand()
  115.  
  116. while true do
  117.     parallel.waitForAny(receiveCommand, dig)
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement