Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local numFilters = 0 -- Number of filter slots
- local invNumBlocksByType = {} -- Number of blocks in our inventory, grouped by block type
- local invNumBlocksByTypeInStack = {} -- Number of blocks in our inventory in the currently filling slot, grouped by block type
- local invNumBlocksTotal = 0 -- Total number of blocks in our inventory
- local invNumEmptySlots = 0 -- Number of empty slots in our inventory
- local itemDrops = {} -- Does the block drop itself or something else, or multiple types ["unknown"|"self"|"other"|"multiple"]
- local dropCount = {} -- Number of items dropped per block
- -- Initialize the inventory related tables
- function initInventory()
- local num = 0
- local firstEmpty = 0
- local currentFilter = 0
- local didStack = false
- local stackedWith = 0
- invNumBlocksTotal = 0
- -- FIXME TODO: check for auto-refuel enabled
- local lastSlot = 16
- -- Note: We don't touch the last slot if we are using the auto-refuel feature
- for i = 1, lastSlot do
- num = turtle.getItemCount(i)
- -- Items in the current slot
- if num > 0 then
- -- Sort the filter items starting from slot 1 so that there are no empty slots in between.
- -- First, check if the current slot can be stacked with a previous one
- turtle.select(i)
- for k = 1, i - 1 do
- -- The current slot's item matches a previous slot
- if turtle.compareTo(k) == true then
- -- Stack the items, but only if they all fit into the previous slot
- if turtle.getItemSpace(k) >= num then
- turtle.transferTo(k, num)
- didStack = true
- stackedWith = k
- -- FIXME if the items do not fit into one slot, then a second
- -- duplicate filter is created
- end
- break
- end
- end
- -- The current slot contains a unique item, it did not match any of the previous filters
- if didStack == false then
- -- There have been empty slots before, but the current slot is not empty
- if firstEmpty > 0 then
- -- turtle.select(i)
- turtle.transferTo(firstEmpty, num)
- local j = firstEmpty + 1
- firstEmpty = 0
- for j = j, i do
- if turtle.getItemCount(j) == 0 then
- firstEmpty = j
- break
- end
- end
- end
- -- Initialize and calculate the inventory/filtering related things
- currentFilter = currentFilter + 1
- invNumBlocksByType[currentFilter] = num
- invNumBlocksByTypeInStack[currentFilter] = num
- invNumBlocksTotal = invNumBlocksTotal + num
- itemDrops[currentFilter] = "unknown"
- dropCount[currentFilter] = -1
- else -- didStack
- -- Update the inventory/filtering related things since we moved some items
- invNumBlocksByType[stackedWith] = invNumBlocksByType[stackedWith] + num
- invNumBlocksByTypeInStack[stackedWith] = invNumBlocksByTypeInStack[stackedWith] + num
- invNumBlocksTotal = invNumBlocksTotal + num
- end
- didStack = false
- elseif firstEmpty == 0 then
- firstEmpty = i
- end
- end
- numFilters = currentFilter
- invNumEmptySlots = 16 - numFilters
- return true
- end
- initInventory()
- print("numFilters: " .. numFilters)
- print("invNumBlocksTotal: " .. invNumBlocksTotal)
- print("invNumEmptySlots: " .. invNumEmptySlots)
- --print("invNumBlocksByType: ")
- for key, value in pairs (invNumBlocksByType) do
- print("invNumBlocksByType[" .. tostring(key) .. "]: " .. tostring(value))
- end
- --print("invNumBlocksByTypeInStack: ")
- for key, value in pairs (invNumBlocksByTypeInStack) do
- print("invNumBlocksByTypeInStack[" .. tostring(key) .. "]: " .. tostring(value))
- end
- --print("itemDrops: ")
- for key, value in pairs (itemDrops) do
- print("itemDrops[" .. tostring(key) .. "]: " .. tostring(value))
- end
- --print("dropCount: ")
- for key, value in pairs (dropCount) do
- print("dropCount[" .. tostring(key) .. "]: " .. tostring(value))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement