Advertisement
KaosKlaus

Item Collector

Mar 21st, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.28 KB | None | 0 0
  1. -- ************ ItemCollector **************
  2. -- * 1.0 release!
  3. -- ******************************************
  4.  
  5. verNum = "v1.0"
  6.  
  7. local startTime = os.clock()
  8.  
  9. local tArgs = { ... }
  10. local delay = 0.5
  11.  
  12. local level = 1
  13. local nextTurn = 0
  14. local itemsCollected = 0
  15. local screenX, screenY = term.getSize()
  16.  
  17. local tTurns = {
  18.   [0] = turtle.turnRight,
  19.   [1] = turtle.turnLeft,
  20. }
  21.  
  22. local forward = {
  23.   go = turtle.forward,
  24.   detect = turtle.detect,
  25.   inspect = turtle.inspect,
  26.   suck = turtle.suck,
  27.   drop = turtle.drop,
  28. }
  29.  
  30. local up = {
  31.   go = turtle.up,
  32.   detect = turtle.detectUp,
  33.   inspect = turtle.inspectUp,
  34.   suck = turtle.suckUp,
  35.   drop = turtle.dropUp,
  36. }
  37.  
  38. local down = {
  39.   go = turtle.down,
  40.   detect = turtle.detectDown,
  41.   inspect = turtle.inspectDown,
  42.   suck = turtle.suckDown,
  43.   drop = turtle.dropDown,
  44. }
  45.  
  46. local inventoryStorageSlots = {
  47.   [15] = {name = "minecraft:coal", slot = 16, commonName = "Charcoalz"},
  48. }
  49.  
  50. local signalBlock = {
  51.   ["minecraft:stained_hardened_clay"] = true,
  52. }
  53.  
  54. local sapling = {
  55.   name = "Sapling",
  56.   ["minecraft:sapling"] = true,
  57.   ["minecraft:golden_rail"] = true,
  58.   ["Railcraft:track"] = true,
  59.   ["IC2:blockRubSapling"] = true,
  60. }
  61.  
  62. local leaves = {
  63.   ["minecraft:leaves"] = true,
  64.   ["IC2:blockRubLeaves"] = true,
  65. }
  66.  
  67. local wood = {
  68.   ["minecraft:log"] = true,
  69.   ["minecraft:log2"] = true,
  70.   ["IC2:blockRubWood"] = true,
  71. }
  72.  
  73. local fuel = {
  74.   ["minecraft:coal"] = 80,
  75.   ["minecraft:stick"] = 5,
  76. }
  77.  
  78. local container = {
  79.   ["minecraft:chest"] = true,
  80.   ["minecraft:trapped_chest"] = true,
  81.   ["JABBA:barrel"] = true,
  82.   ["IronChest:BlockIronChest"] = true,
  83.   ["EnderStorage:enderChest"] = true,
  84.   ["CarpentersBlocks:blockCarpentersSafe"] = true,
  85. }
  86.  
  87. local function displayLogo()
  88.   term.clear()
  89.   term.setCursorPos(1,2)
  90.   print("**** KaosKrise Itamz collect0r "..verNum.." ****")
  91.   print()
  92.   print("Iz tiem 2 clean up dis mes!!")
  93.   print()
  94. end
  95.  
  96. local function formatSeconds(seconds)
  97.   if seconds >= 3600 then
  98.     return string.format("%d:%.2d:%.2d", math.floor(seconds/3600), math.floor(seconds/60%60), math.floor(seconds%60))
  99.   else
  100.     return string.format("%d:%.2d", math.floor(seconds/60%60), math.floor(seconds%60))
  101.   end
  102. end
  103.  
  104. local function displayTime()
  105.   local x, y = term.getCursorPos()
  106.   local time = os.clock()
  107.   local runTime = time - startTime
  108.   local string = formatSeconds(runTime)
  109.   term.setCursorPos(screenX -(#string) ,screenY)
  110.   write(string)
  111.   term.setCursorPos(x, y)
  112.   return true
  113. end
  114.  
  115. local function prnLines(...)
  116.   local x, y = term.getCursorPos()
  117.   for i,v in ipairs(arg) do
  118.     term.clearLine()
  119.     print(v)
  120.   end
  121.   term.setCursorPos(x,y)
  122. end
  123.  
  124. local function getInventoryCount()
  125.   local inventoryCount = 0
  126.   for i=1,16 do
  127.     inventoryCount = inventoryCount + turtle.getItemCount(i)
  128.   end
  129.   return inventoryCount
  130. end
  131.  
  132. local function suck(direction)
  133.   direction = direction or forward
  134.   local pickedUp = getInventoryCount()
  135.   if direction.suck() then
  136.     pickedUp = getInventoryCount() - pickedUp
  137.   else
  138.     return false
  139.   end
  140.   itemsCollected = itemsCollected + pickedUp
  141.   local x, y = term.getCursorPos()
  142.   term.setCursorPos(1,screenY)
  143.   write("I has found "..itemsCollected.." itamz!")
  144.   term.setCursorPos(x, y)
  145.   return true
  146. end
  147.  
  148. local function isItem(slot)
  149.   slot = slot or turtle.getSelectedSlot()
  150.   local data = turtle.getItemDetail(slot)
  151.  
  152.   if data then
  153.     return data.name
  154.   else
  155.     return false
  156.   end
  157. end
  158.  
  159. local function selectItem(item)
  160.   for i=1,16 do
  161.     if item[isItem(i)] then
  162.       turtle.select(i)
  163.       return true
  164.     end
  165.   end
  166.   return false
  167. end
  168.  
  169.  
  170. local function inspectBlock(direction)
  171.   if direction == nil then direction = forward end
  172.   local success, data = direction.inspect()
  173.   if success then
  174.     return data.name, data.metadata
  175.   else
  176.     return false
  177.   end
  178. end
  179.  
  180. local function refuel()
  181.   local fuelLevel = turtle.getFuelLevel()
  182.   if fuelLevel == "unlimited" or fuelLevel > 0 then
  183.     return true
  184.   end
  185.  
  186.   local function tryRefuel()
  187.     local bestFuel
  188.     local bestFuelVal = 0
  189.     for i=1,16 do
  190.       local itemInSlot = isItem(i)
  191.       if fuel[itemInSlot] and fuel[itemInSlot] > bestFuelVal then
  192.         bestFuel = i
  193.         bestFuelVal = fuel[itemInSlot]
  194.       end
  195.     end
  196.     if bestFuel then
  197.       turtle.select(bestFuel)
  198.       if turtle.refuel(1) then
  199.         turtle.select(1)
  200.         return true
  201.       end
  202.     end
  203.     return false
  204.   end
  205.  
  206.   if not tryRefuel() then
  207.     displayLogo()
  208.     prnLines( "Add more fuel to continue.","" )
  209.     while not tryRefuel() do
  210.       displayTime()
  211.       sleep(1)
  212.     end
  213.     prnLines( "Resuming...","" )
  214.   end
  215.   return true
  216. end
  217.  
  218. local function goDirection(direction)
  219.   suck(direction)
  220.   if not refuel() then
  221.     return false
  222.   end
  223.   while displayTime() and not direction.go() do
  224.     sleep(delay)
  225.   end
  226.   return true
  227. end
  228.  
  229. --[[
  230. Syntax:   go( string detectedBlockname)
  231. Returns:  boolean whether the turtle succeeded.
  232. --]]
  233. local function go()
  234.   return goDirection(forward)
  235. end
  236.  
  237. local function goUp()
  238.   level = level + 1
  239.   return goDirection(up)
  240. end
  241.  
  242. local function goDown()
  243.   level = level -1
  244.   return goDirection(down)
  245. end
  246.  
  247. local function patrol()
  248.   while true do
  249.     repeat
  250.       suck()
  251.       refuel()
  252.       displayTime()
  253.     until not turtle.forward()
  254.     local block, meta = inspectBlock()
  255.     if block then
  256.       return block, meta
  257.     else
  258.       sleep(delay)
  259.     end
  260.   end
  261. end
  262.  
  263. local function nextFreeSlot()
  264.   for i=1,16 do
  265.     if turtle.getItemCount(i) == 0 then
  266.       return true, i
  267.     end
  268.   end
  269.   return false
  270. end
  271.  
  272. --local function refill()
  273. --    local item = isItem(16)
  274. --  if "minecraft:coal" ~= item then
  275. --    turtle.select(16)
  276. --    success, slot =  nextFreeSlot()
  277. --    if success then
  278. --      turtle.transferTo(slot)
  279. --    else
  280. --    turtle.dropDown()
  281. --    end
  282. --  end
  283. --end
  284.  
  285. local function unload(direction)
  286.   turtle.select(1)
  287.    prnLines("I has findz chest.", "Will try 2 unload mah stuff...")
  288.  
  289.   local dropCount = 0
  290.   local item = isItem(16)
  291.   if "minecraft:coal" ~= item then
  292.     turtle.select(16)
  293.     dropCount = dropCount + turtle.getItemCount()
  294.     direction.drop()
  295.   end
  296.  
  297.   for i=1,15 do
  298.     if turtle.getItemCount(i) > 0 then
  299.       turtle.select(i)
  300.       item = isItem(i)
  301.       if item and item == "minecraft:coal" then
  302.         turtle.transferTo(16)
  303.         if turtle.getItemCount(i) > 0 then
  304.           dropCount = dropCount + turtle.getItemCount()
  305.           direction.drop()
  306.         end
  307.       else
  308.         dropCount = dropCount + turtle.getItemCount()
  309.         direction.drop()
  310.       end
  311.     end
  312.   end
  313.  
  314.   turtle.select(1)
  315.   if dropCount > 0 then
  316.     prnLines("I has putz "..dropCount.." itamz in teh chest. Yay!")
  317.   else
  318.     prnLines("I has nothin 2 giv :(","maybe nxt tiem :)")
  319.   end
  320. end
  321.  
  322. local function refill(meta)
  323.   if inventoryStorageSlots[meta] then
  324.     local name = inventoryStorageSlots[meta].name
  325.     local slot = inventoryStorageSlots[meta].slot
  326.  
  327.     while turtle.getItemCount(slot) > 0 and name ~= isItem(slot) do
  328.       local success, freeSlot =  nextFreeSlot()
  329.       if success then
  330.         turtle.select(slot)
  331.         turtle.transferTo(freeSlot)
  332.       else
  333.         prnLines("I has too much stuffz!","")
  334.         sleep(delay)
  335.       end
  336.     end
  337.  
  338.     if turtle.getItemSpace(slot) > 0 then
  339.       turtle.select(slot)
  340.       prnLines("I needz "..turtle.getItemSpace().." "..inventoryStorageSlots[meta].commonName)
  341.       turtle.suckDown(turtle.getItemSpace())
  342.       turtle.select(1)
  343.     end
  344.   else
  345.     return false
  346.   end
  347. end
  348.  
  349. local function turn()
  350.   --  tryUnload()
  351.   tTurns[nextTurn]()
  352.   suck()
  353.   if forward.detect() then
  354.     tTurns[nextTurn]()
  355.   else
  356.     go()
  357.   end
  358.   tTurns[nextTurn]()
  359.   nextTurn = 1 - nextTurn
  360. end
  361.  
  362.  
  363.  
  364.  
  365. -- ************MAIN************
  366. turtle.select(1)
  367.  
  368. displayLogo()
  369.  
  370. while true do
  371.   local block, meta = patrol()
  372. --  prnLines(block,meta)
  373.   if block then
  374.     if leaves[block] then
  375.       if not turtle.dig() then
  376.         return false
  377.       end
  378.     elseif signalBlock[block] then
  379.       if meta == 15 then
  380.         refill(meta)
  381.       elseif meta == 0 then
  382.         unload(down)
  383.       end
  384.       turn()
  385.     else
  386.       turn()
  387.     end
  388.   else
  389.     return false
  390.   end
  391. end
  392.  
  393.  
  394. --  local event, key = os.pullEvent( "key" )  -- DEBUG BREAK POINT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement