Advertisement
sunbro3

Count Raw Ingredients In Blueprint [Factorio]

Oct 8th, 2023 (edited)
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | Gaming | 0 0
  1. /c --[[ hold blueprint when using this --]]
  2. local player = game.player
  3. local counts = {}
  4. for k,v in pairs(player.get_blueprint_entities() or error("Need blueprint on cursor", 0)) do
  5.   counts[v.name] = (counts[v.name] or 0) + 1
  6. end
  7.  
  8. repeat
  9.   local ingredient_has_ingredients = false
  10.   local next_counts = {}
  11.   for name, count in pairs(counts) do
  12.     local recipe = game.recipe_prototypes[name]
  13.     if recipe then
  14.       ingredient_has_ingredients = true
  15.       for _,ing in pairs(recipe.ingredients) do
  16.         --[[ type, name, amount --]]
  17.         local partname, partamount = ing.name, ing.amount
  18.         local modded_count = count / recipe.main_product.amount * recipe.main_product.probability
  19.         next_counts[partname] = (next_counts[partname] or 0) + (modded_count * partamount)
  20.       end
  21.     else
  22.       next_counts[name] = (next_counts[name] or 0) + count
  23.     end
  24.   end
  25.   counts = next_counts
  26. until not ingredient_has_ingredients
  27.  
  28. local ordered_counts = {}
  29. for name, count in pairs(counts) do
  30.   table.insert(ordered_counts, { name=name, count=count })
  31. end
  32. table.sort(ordered_counts, function(a, b) return a.name < b.name end)
  33. player.print("rawcount:")
  34. for _,t in pairs(ordered_counts) do
  35.   player.print(t.name..": "..t.count)
  36. end
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement