Advertisement
Myros27

ME-Defragger

Aug 10th, 2022 (edited)
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. local robot = require("robot")
  2. local term = require("term")
  3. local computer = require("computer")
  4. local component = require("component")
  5. local sides = require("sides")
  6. local keyboard = require("keyboard")
  7.  
  8. local function Sleepy()
  9.   while computer.energy() < 20000 do
  10.     os.sleep(20)
  11.   end
  12. end
  13.  
  14. local function DumpAll()
  15.   for i = 1, robot.inventorySize() do
  16.     robot.select(i)
  17.     robot.drop()
  18.   end
  19.   robot.select(1);
  20.   Sleepy()
  21. end
  22.  
  23. local function safeForward(x)
  24.   if (x ~= 0) then
  25.     for i = 1, x do
  26.       local canmoveF = nil
  27.       while (canmoveF == nil) do
  28.         canmoveF = robot.forward()
  29.       end
  30.     end
  31.   end
  32. end
  33.  
  34. local function safeUp(x)
  35.   if (x ~= 0) then
  36.     for i = 1, x do
  37.       local canmoveU = nil
  38.       while (canmoveU == nil) do
  39.         canmoveU = robot.up()
  40.       end
  41.     end
  42.   end
  43. end
  44.  
  45. local function safeDown(x)
  46.   if (x ~= 0) then
  47.     for i = 1, x do
  48.       local canmoveD = nil
  49.       while (canmoveD == nil) do
  50.         canmoveD = robot.down()
  51.       end
  52.     end
  53.   end
  54. end
  55.  
  56. local function driveHandler(highX, wideX, driveX, backX)
  57.   robot.turnLeft()
  58.   drivePresent = false
  59.   component.sign.setValue("High: " .. highX + 1 .. "\nWide: " .. wideX + 1 .. "\nDrive: " .. driveX + 1 .. "\nTime: " .. os.clock())
  60.   safeUp(2)
  61.   robot.turnLeft()
  62.   safeUp(highX)
  63.   safeForward(wideX)
  64.   robot.turnLeft()
  65.   if (backX == false) then
  66.     drivePresent = component.inventory_controller.suckFromSlot(3, driveX + 1)
  67.     if (drivePresent == 1) then
  68.       component.inventory_controller.equip()
  69.     end
  70.   end
  71.   if (backX == true) then
  72.     component.inventory_controller.equip()
  73.     component.inventory_controller.dropIntoSlot(3, driveX + 1)
  74.   end
  75.   robot.turnLeft()
  76.   safeForward(wideX)
  77.   safeDown(highX)
  78.   safeDown(1)
  79.   if (backX == false) then
  80.     if (drivePresent == 1) then
  81.       component.inventory_controller.equip()
  82.       component.inventory_controller.dropIntoSlot(3, 1)
  83.     end
  84.   end
  85.   safeDown(1)
  86.   if (drivePresent == 1) then
  87.     return true
  88.   end
  89.   if (drivePresent ~= 1) then
  90.     return false
  91.   end
  92.   end
  93.  
  94. -- init
  95. robot.select(1);
  96.  
  97. -- find startposition
  98. local canmove = true
  99. local infront = true
  100. local useless = true
  101. local high = 1
  102. local wide = 1
  103. local drive = 1
  104. local back = false
  105. local drivePresentInRobot = false
  106.  
  107. while (canmove == true) do
  108.   canmove = robot.down()
  109. end
  110.  
  111. while (infront ~= "passable") do
  112.   useless, infront = robot.detect()
  113.   canmove = true
  114.   while (canmove == true) do
  115.     canmove = robot.forward()
  116.   end
  117.   robot.turnLeft()
  118.   useless, infront = robot.detect()
  119. end
  120.  
  121. component.sign.setValue("\nBooting Up")
  122.  
  123. robot.turnLeft()
  124.  
  125. safeForward(3)
  126. safeUp(2)
  127.  
  128. DumpAll()
  129. component.inventory_controller.equip()
  130. DumpAll()
  131.  
  132. robot.turnAround()
  133. safeDown(2)
  134. safeForward(3)
  135.  
  136. --do loop
  137.  
  138. while true do
  139.   for high = 1, 8 do
  140.     for wide = 1, 4 do
  141.       for drive = 1, 10 do
  142.         os.sleep(1)
  143.         drivePresentInRobot = false
  144.         drivePresentInRobot = driveHandler(high - 1, wide - 1, drive - 1, false)
  145.         if (drivePresentInRobot == true) then
  146.           local finished = false
  147.           while (finished == false) do
  148.             finished = component.inventory_controller.suckFromSlot(3,1)
  149.             os.sleep(1)
  150.           end
  151.           component.inventory_controller.equip()
  152.           driveHandler(high - 1, wide - 1, drive - 1, true)
  153.         end
  154.       end
  155.     end
  156.   end
  157.   robot.turnLeft()
  158.   for sleeep = 1, 60 do
  159.     component.sign.setValue("Sleeping ...\n" .. sleeep .. "/60")
  160.     os.sleep(60)
  161.   end
  162.   robot.turnRight()
  163. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement