Advertisement
masa-

CC Turtle Devel: initInventory

Apr 12th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. local numFilters = 0            -- Number of filter slots
  2. local invNumBlocksByType = {}           -- Number of blocks in our inventory, grouped by block type
  3. local invNumBlocksByTypeInStack = {}    -- Number of blocks in our inventory in the currently filling slot, grouped by block type
  4. local invNumBlocksTotal = 0     -- Total number of blocks in our inventory
  5. local invNumEmptySlots = 0      -- Number of empty slots in our inventory
  6. local itemDrops = {}            -- Does the block drop itself or something else, or multiple types ["unknown"|"self"|"other"|"multiple"]
  7. local dropCount = {}            -- Number of items dropped per block
  8.  
  9.  
  10. -- Initialize the inventory related tables
  11. function initInventory()
  12.     local num = 0
  13.     local firstEmpty = 0
  14.     local currentFilter = 0
  15.     invNumBlocksTotal = 0
  16.  
  17.     -- FIXME TODO: check for auto-refuel enabled
  18.     local lastSlot = 16
  19.     -- Note: We don't touch the last slot if we are using the auto-refuel feature
  20.     for i = 1, lastSlot do
  21.         num = turtle.getItemCount(i)
  22.  
  23.         -- Items in the current slot
  24.         if num > 0 then
  25.             -- Sort the filter items starting from slot 1 so that there are no empty slots in between.
  26.             -- There have been empty slots before, but the current slot is not empty
  27.             if firstEmpty > 0 then
  28.                 turtle.select(i)
  29.                 turtle.transferTo(firstEmpty, num)
  30.                 local j = firstEmpty + 1
  31.                 firstEmpty = 0
  32.                 for j = j, i do
  33.                     if turtle.getItemCount(j) == 0 then
  34.                         firstEmpty = j
  35.                         break
  36.                     end
  37.                 end
  38.             end
  39.             -- Initialize and calculate the inventory/filtering related things
  40.             currentFilter = currentFilter + 1
  41.             invNumBlocksByType[currentFilter] = num
  42.             invNumBlocksByTypeInStack[currentFilter] = num
  43.             invNumBlocksTotal = invNumBlocksTotal + num
  44.             itemDrops[currentFilter] = "unknown"
  45.             dropCount[currentFilter] = -1
  46.         elseif firstEmpty == 0 then
  47.             firstEmpty = i
  48.         end
  49.     end
  50.  
  51.     numFilters = currentFilter
  52.     invNumEmptySlots = 16 - numFilters
  53.  
  54.     return true
  55. end
  56.  
  57. initInventory()
  58.  
  59. print("numFilters: " .. numFilters)
  60. print("invNumBlocksTotal: " .. invNumBlocksTotal)
  61. print("invNumEmptySlots: " .. invNumEmptySlots)
  62.  
  63. print("invNumBlocksByType: ")
  64. for key, value in pairs (invNumBlocksByType) do
  65.     print("invNumBlocksByType[" .. tostring(key) .. "]: " .. tostring(value))
  66. end
  67.  
  68. print("invNumBlocksByTypeInStack: ")
  69. for key, value in pairs (invNumBlocksByTypeInStack) do
  70.     print("invNumBlocksByTypeInStack[" .. tostring(key) .. "]: " .. tostring(value))
  71. end
  72.  
  73. print("itemDrops: ")
  74. for key, value in pairs (itemDrops) do
  75.     print("itemDrops[" .. tostring(key) .. "]: " .. tostring(value))
  76. end
  77.  
  78. print("dropCount: ")
  79. for key, value in pairs (dropCount) do
  80.     print("dropCount[" .. tostring(key) .. "]: " .. tostring(value))
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement