Advertisement
Oeed

Collector

Jul 29th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. local MAX_LEVEL = 2
  2. local price = 16
  3. local WHEREIS_CHANNEL = 420
  4.  
  5. local LEVEL_DETECTION_BLOCK = "minecraft:planks"
  6. local CHEST_BLOCK = "ironchest:BlockIronChest"
  7. local CHEST_POSITION_BLOCK = "minecraft:sandstone"
  8. local RIGHT_CHEST_TYPE = "smooth_sandstone"
  9.  
  10. local activities = {
  11.     TRANSIT_DOWN = 1, -- go down until current level == target level. when meeting a plank block increase current level
  12.     TRANSIT_OUT = 2, -- back out of the vertical track until bottom is sandstone
  13.     CHEST_IN = 3, -- turn toward the chest and travel forward until the chest is met
  14.     CHEST_COLLECT = 4, -- fill up the turtle
  15.     CHECK_OUT = 5, -- go backwards from the chest until bottom is sandstone
  16.     TRANSIT_IN = 6, -- turn toward the vertical track until front is not air
  17.     TRANSIT_UP = 7, -- goes up until above is not air
  18.     CHEST_DROP = 8, -- drop entire inventory up. set current level to zero. increase target level by one, wrapping if needed
  19. }
  20.  
  21. local directions = {
  22.     CROP_TOWARD = 0,
  23.     CHEST_TOWARD = 1,
  24.     CROP_AWAY = 2,
  25.     CHEST_AWAY = 3
  26. }
  27.  
  28. -- fix the modem bug
  29. if not peripheral.find("modem") then
  30.     -- first find an empty slot
  31.     for i = 1, 16 do
  32.         if turtle.getItemCount(i) == 0 then
  33.             turtle.select(i)
  34.             break
  35.         elseif i == 16 then
  36.             turtle.select(i)
  37.             turtle.drop()
  38.         end
  39.     end
  40.     turtle.equipLeft()
  41.     turtle.equipLeft()
  42.     turtle.equipRight()
  43.     turtle.equipRight()
  44. end
  45.  
  46. local state
  47. local function loadState()
  48.     local h = fs.open("mover.state", "r")
  49.     if h then
  50.         state = textutils.unserialise(h.readAll())
  51.         h.close()
  52.     else
  53.         state = {
  54.             activity = activities.TRANSIT_DOWN,
  55.             direction = directions.CROP_TOWARD,
  56.             currentLevel = 0,
  57.             targetLevel = 1,
  58.             startFuel = turtle.getFuelLevel(),
  59.             isChestRight = false
  60.         }
  61.     end
  62. end
  63.  
  64. local function saveState()
  65.     local h = fs.open("mover.state", "w")
  66.     if h then
  67.         h.write(textutils.serialise(state))
  68.         h.close()
  69.     end
  70. end
  71.  
  72. local function turnTo(direction)
  73.     local difference = direction - state.direction
  74.     if math.abs(difference) >= 3 then
  75.         difference = (difference - 2 * (difference / math.abs(difference))) * -1
  76.     end
  77.     if difference > 0 then
  78.         for i = 1, difference do
  79.             turtle.turnLeft()
  80.             state.direction = (state.direction + 1) % 4
  81.             saveState()
  82.         end
  83.     elseif difference < 0 then
  84.         for i = 1, math.abs(difference) do
  85.             turtle.turnRight()
  86.             state.direction = (state.direction - 1) % 4
  87.             saveState()
  88.         end
  89.     end
  90. end
  91.  
  92. local function setActivity(activity)
  93.     if not activity then error('act') end
  94.     state.activity = activity
  95.     saveState()
  96. end
  97.  
  98. local funcs = {}
  99.  
  100. local function activity(activity)
  101.     setActivity(activity)
  102.     return funcs[activity]()
  103. end
  104.  
  105. funcs[activities.TRANSIT_DOWN] = function()
  106.     print("Descending to level " .. state.targetLevel)
  107.     turnTo(directions.CROP_TOWARD)
  108.     while true do
  109.         local isBlock, block = turtle.inspect()
  110.         if isBlock and block.name == LEVEL_DETECTION_BLOCK then
  111.             state.currentLevel = state.currentLevel + 1
  112.             print("Level: " .. state.currentLevel)
  113.             if state.currentLevel == state.targetLevel then
  114.                 return activity(activities.TRANSIT_OUT)
  115.             end
  116.         end
  117.         turtle.down()
  118.     end
  119. end
  120.  
  121. funcs[activities.TRANSIT_OUT] = function()
  122.     print("Leaving vertical track")
  123.     turnTo(directions.CROP_TOWARD)
  124.     while true do
  125.         local isBlock, block = turtle.inspectDown()
  126.         if isBlock and block.name == CHEST_POSITION_BLOCK then
  127.             state.isChestRight = block.state.type == RIGHT_CHEST_TYPE
  128.             saveState()
  129.             return activity(activities.CHEST_IN)
  130.         else
  131.             turtle.back()
  132.         end
  133.     end
  134. end
  135.  
  136. funcs[activities.CHEST_IN] = function()
  137.     print("Heading toward chest")
  138.     turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
  139.     while true do
  140.         local isBlock, block = turtle.inspect()
  141.         if isBlock then
  142.             return activity(activities.CHEST_COLLECT)
  143.         else
  144.             turtle.forward()
  145.         end
  146.     end
  147. end
  148.  
  149. funcs[activities.CHEST_COLLECT] = function()
  150.     print("Collecting items...")
  151.     turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
  152.     while turtle.suck() do end
  153.     return activity(activities.CHECK_OUT)
  154. end
  155.  
  156. funcs[activities.CHECK_OUT] = function()
  157.     print("Leaving chest...")
  158.     turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
  159.     while true do
  160.         local isBlock, block = turtle.inspectDown()
  161.         if isBlock and block.name == CHEST_POSITION_BLOCK then
  162.             return activity(activities.TRANSIT_IN)
  163.         else
  164.             turtle.back()
  165.         end
  166.     end
  167. end
  168.  
  169. funcs[activities.TRANSIT_IN] = function()
  170.     print("Heading toward vertical track...")
  171.     turnTo(directions.CROP_TOWARD)
  172.     while true do
  173.         local isBlock, block = turtle.inspect()
  174.         if isBlock then
  175.             return activity(activities.TRANSIT_UP)
  176.         else
  177.             turtle.forward()
  178.         end
  179.     end
  180. end
  181.  
  182. funcs[activities.TRANSIT_UP] = function()
  183.     print("Travelling up to the drop chest...")
  184.     turnTo(directions.CROP_TOWARD)
  185.     while true do
  186.         local isBlock, block = turtle.inspectUp()
  187.         if isBlock and block.name == CHEST_BLOCK then
  188.             return activity(activities.CHEST_DROP)
  189.         else
  190.             turtle.up()
  191.         end
  192.     end
  193. end
  194.  
  195. funcs[activities.CHEST_DROP] = function()
  196.     print("Dropping the items in the chest...")
  197.     local count = 0
  198.     for i = 1, 16 do
  199.         count = count + turtle.getItemCount(i)
  200.         turtle.select(i)
  201.         turtle.dropUp()
  202.     end
  203.     print("Transported " .. count .. " melon.")
  204.     print(string.format("Value: $%.2f", (count / 64) * price))
  205.     print("Fuel consumption: " .. state.startFuel - turtle.getFuelLevel())
  206.     state.currentLevel = 0
  207.     state.targetLevel = state.targetLevel + 1
  208.     if state.targetLevel > MAX_LEVEL then
  209.         state.targetLevel = 1
  210.     end
  211.     print("Now targeting: " .. state.targetLevel)
  212.     return
  213. end
  214.  
  215. -- depart from the idle chest location
  216. function resume()
  217.     print("Resuming...")
  218.     loadState()
  219.     activity(state.activity)
  220. end
  221.  
  222. parallel.waitForAll(function()
  223.     resume()
  224.     while true do
  225.         sleep(delay)
  226.         print("Starting fresh...")
  227.         state.startFuel = turtle.getFuelLevel()
  228.         state.isChestRight = false
  229.         saveState()
  230.         activity(activities.TRANSIT_DOWN)
  231.     end
  232. end, function()
  233.     local modem = peripheral.find("modem")
  234.     modem.open(WHEREIS_CHANNEL)
  235.     while true do
  236.         local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  237.         if channel == WHEREIS_CHANNEL and message == "WHEREIS" then
  238.             modem.transmit(replyChannel, channel, {os.getComputerID(), os.getComputerLabel(), gps.locate()})
  239.         end
  240.     end
  241. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement