Advertisement
Oeed

Organiser

Aug 3rd, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. local WHEREIS_CHANNEL = 420
  2.  
  3. local CHEST_BLOCK = "ironchest:BlockIronChest"
  4.  
  5. local activities = {
  6.     COLLECT = 1, -- suck up the contents of the chest, then DROP
  7.     DROP = 2, -- drop left, then drop right, then forward until the invetory is empty or movement is blocked
  8.     RETURN = 3, -- go back to the chest until movement is blocked
  9. }
  10.  
  11. local directions = {
  12.     HOME_AWAY = 0,
  13.     CHEST_RIGHT = 1,
  14.     HOME_TOWARD = 2,
  15.     CHEST_LEFT = 3
  16. }
  17.  
  18. -- fix the modem bug
  19. if not peripheral.find("modem") then
  20.     -- first find an empty slot
  21.     for i = 1, 16 do
  22.         if turtle.getItemCount(i) == 0 then
  23.             turtle.select(i)
  24.             break
  25.         elseif i == 16 then
  26.             turtle.select(i)
  27.             turtle.drop()
  28.         end
  29.     end
  30.     turtle.equipLeft()
  31.     turtle.equipLeft()
  32.     turtle.equipRight()
  33.     turtle.equipRight()
  34. end
  35.  
  36. local state
  37. local function loadState()
  38.     local h = fs.open("organiser.state", "r")
  39.     if h then
  40.         state = textutils.unserialise(h.readAll())
  41.         h.close()
  42.     else
  43.         state = {
  44.             activity = activities.COLLECT,
  45.             direction = directions.HOME_AWAY,
  46.             startFuel = turtle.getFuelLevel()
  47.         }
  48.     end
  49. end
  50.  
  51. local function saveState()
  52.     local h = fs.open("organiser.state", "w")
  53.     if h then
  54.         h.write(textutils.serialise(state))
  55.         h.close()
  56.     end
  57. end
  58.  
  59. local function turnTo(direction)
  60.     local difference = direction - state.direction
  61.     if math.abs(difference) >= 3 then
  62.         difference = (difference - 2 * (difference / math.abs(difference))) * -1
  63.     end
  64.     if difference > 0 then
  65.         for i = 1, difference do
  66.             turtle.turnLeft()
  67.             state.direction = (state.direction + 1) % 4
  68.             saveState()
  69.         end
  70.     elseif difference < 0 then
  71.         for i = 1, math.abs(difference) do
  72.             turtle.turnRight()
  73.             state.direction = (state.direction - 1) % 4
  74.             saveState()
  75.         end
  76.     end
  77. end
  78.  
  79. local function setActivity(activity)
  80.     if not activity then error('act') end
  81.     state.activity = activity
  82.     saveState()
  83. end
  84.  
  85. local funcs = {}
  86.  
  87. local function activity(activity)
  88.     setActivity(activity)
  89.     return funcs[activity]()
  90. end
  91.  
  92. funcs[activities.COLLECT] = function()
  93.     print("Collecting the melon...")
  94.     turnTo(directions.HOME_TOWARD)
  95.     local okay = turtle.suck()
  96.     if not okay then
  97.         print("Nothing to collect...")
  98.         -- there is nothing to collect, wait 10 seconds and try again
  99.         sleep(10)
  100.         return activity(activities.COLLECT)
  101.     end
  102.     while turtle.suck() do end
  103.     return activity(activities.DROP)
  104. end
  105.  
  106. local function drop() -- return false if the chest is full, return true if the turtle is empty
  107.     for i = 1, 16 do
  108.         local okay, err = turtle.drop()
  109.         if not okay then
  110.             if err == "No space for items" then
  111.                 return false
  112.             else
  113.                 return true
  114.             end
  115.         end
  116.     end
  117.     return true
  118. end
  119.  
  120. funcs[activities.DROP] = function()
  121.     print("Dropping the melon in chests")
  122.     while true do
  123.         turnTo(directions.CHEST_RIGHT)
  124.         if drop() then
  125.             break
  126.         end
  127.         turnTo(directions.CHEST_LEFT)
  128.         if drop() then
  129.             break
  130.         end
  131.         turnTo(directions.HOME_AWAY)
  132.         if not turtle.forward() then
  133.             break
  134.         end
  135.     end
  136.     return activity(activities.RETURN)
  137. end
  138.  
  139. funcs[activities.RETURN] = function()
  140.     print("Heading toward chest")
  141.     turnTo(directions.HOME_TOWARD)
  142.     while turtle.forward() do end
  143.     return activity(activities.COLLECT)
  144. end
  145.  
  146. -- depart from the idle chest location
  147. function resume()
  148.     print("Resuming...")
  149.     loadState()
  150.     activity(state.activity)
  151. end
  152.  
  153. parallel.waitForAll(function()
  154.     resume()
  155.     while true do
  156.         sleep(delay)
  157.         print("Starting fresh...")
  158.         state.startFuel = turtle.getFuelLevel()
  159.         state.isChestRight = false
  160.         saveState()
  161.         activity(activities.COLLECT)
  162.     end
  163. end, function()
  164.     local modem = peripheral.find("modem")
  165.     modem.open(WHEREIS_CHANNEL)
  166.     while true do
  167.         local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  168.         if channel == WHEREIS_CHANNEL and message == "WHEREIS" then
  169.             modem.transmit(replyChannel, channel, {os.getComputerID(), os.getComputerLabel(), gps.locate()})
  170.         end
  171.     end
  172. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement