Advertisement
masa-

CC Turtle Devel: initInventory v2

Apr 12th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 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.     local didStack = false
  16.     local stackedWith = 0
  17.     invNumBlocksTotal = 0
  18.  
  19.     -- FIXME TODO: check for auto-refuel enabled
  20.     local lastSlot = 16
  21.     -- Note: We don't touch the last slot if we are using the auto-refuel feature
  22.     for i = 1, lastSlot do
  23.         num = turtle.getItemCount(i)
  24.  
  25.         -- Items in the current slot
  26.         if num > 0 then
  27.             -- Sort the filter items starting from slot 1 so that there are no empty slots in between.
  28.  
  29.             -- First, check if the current slot can be stacked with a previous one
  30.             turtle.select(i)
  31.             for k = 1, i - 1 do
  32.                 -- The current slot's item matches a previous slot
  33.                 if turtle.compareTo(k) == true then
  34.                     -- Stack the items, but only if they all fit into the previous slot
  35.                     if turtle.getItemSpace(k) >= num then
  36.                         turtle.transferTo(k, num)
  37.                         didStack = true
  38.                         stackedWith = k
  39.                         -- FIXME if the items do not fit into one slot, then a second
  40.                         -- duplicate filter is created
  41.                     end
  42.                     break
  43.                 end
  44.             end
  45.  
  46.             -- The current slot contains a unique item, it did not match any of the previous filters
  47.             if didStack == false then
  48.                 -- There have been empty slots before, but the current slot is not empty
  49.                 if firstEmpty > 0 then
  50. --                  turtle.select(i)
  51.                     turtle.transferTo(firstEmpty, num)
  52.                     local j = firstEmpty + 1
  53.                     firstEmpty = 0
  54.                     for j = j, i do
  55.                         if turtle.getItemCount(j) == 0 then
  56.                             firstEmpty = j
  57.                             break
  58.                         end
  59.                     end
  60.                 end
  61.                 -- Initialize and calculate the inventory/filtering related things
  62.                 currentFilter = currentFilter + 1
  63.                 invNumBlocksByType[currentFilter] = num
  64.                 invNumBlocksByTypeInStack[currentFilter] = num
  65.                 invNumBlocksTotal = invNumBlocksTotal + num
  66.                 itemDrops[currentFilter] = "unknown"
  67.                 dropCount[currentFilter] = -1
  68.             else -- didStack
  69.                 -- Update the inventory/filtering related things since we moved some items
  70.                 invNumBlocksByType[stackedWith] = invNumBlocksByType[stackedWith] + num
  71.                 invNumBlocksByTypeInStack[stackedWith] = invNumBlocksByTypeInStack[stackedWith] + num
  72.                 invNumBlocksTotal = invNumBlocksTotal + num
  73.             end
  74.  
  75.             didStack = false
  76.         elseif firstEmpty == 0 then
  77.             firstEmpty = i
  78.         end
  79.     end
  80.  
  81.     numFilters = currentFilter
  82.     invNumEmptySlots = 16 - numFilters
  83.  
  84.     return true
  85. end
  86.  
  87. initInventory()
  88.  
  89. print("numFilters: " .. numFilters)
  90. print("invNumBlocksTotal: " .. invNumBlocksTotal)
  91. print("invNumEmptySlots: " .. invNumEmptySlots)
  92.  
  93. --print("invNumBlocksByType: ")
  94. for key, value in pairs (invNumBlocksByType) do
  95.     print("invNumBlocksByType[" .. tostring(key) .. "]: " .. tostring(value))
  96. end
  97.  
  98. --print("invNumBlocksByTypeInStack: ")
  99. for key, value in pairs (invNumBlocksByTypeInStack) do
  100.     print("invNumBlocksByTypeInStack[" .. tostring(key) .. "]: " .. tostring(value))
  101. end
  102.  
  103. --print("itemDrops: ")
  104. for key, value in pairs (itemDrops) do
  105.     print("itemDrops[" .. tostring(key) .. "]: " .. tostring(value))
  106. end
  107.  
  108. --print("dropCount: ")
  109. for key, value in pairs (dropCount) do
  110.     print("dropCount[" .. tostring(key) .. "]: " .. tostring(value))
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement