Skip to content

Commit

Permalink
Search Engine: Add support for item and fluid producers
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Feb 22, 2021
1 parent 0da7311 commit c9c38b3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
local PRODUCERS = "Producers"

local function expected_amount(product)
local expected = product.amount
if not expected then
expected = (product.amount_min + product.amount_max) / 2.0
end
local probability = product.probability or 1

return expected * probability
end

local function recipe_produces(recipe, product_type, product_name)
if recipe == nil then return false end
for _, product in pairs(recipe.products) do
if product.type == product_type and product.name == product_name then
return expected_amount(product)
end
end
return false
end

return {
search_options = {
items = { PRODUCERS },
fluids = { PRODUCERS }
},
search_loops = {
items = {
[PRODUCERS] = function(_, _, task_data)
local player = game.get_player(task_data.player_index)
return player.surface.find_entities_filtered {
type = { "assembling-machine", "furnace" },
force = player.force
}
end
},
fluids = {
[PRODUCERS] = function(_, _, task_data)
local player = game.get_player(task_data.player_index)
return player.surface.find_entities_filtered {
type = "assembling-machine",
force = player.force
}
end
}
},
search_filters = {
items = {
[PRODUCERS] = {
function(player, search_params, item, results)
if not item.valid then return end

local recipe = nil
if item.type == "assembling-machine" then
recipe = item.get_recipe()
end
if item.type == "furnace" then
recipe = item.previous_recipe
end

local amount = recipe_produces(recipe, "item", search_params.name)
if amount ~= false then
table.insert(results, {
entity = item,
location = item.position,
owner = item.last_user,
count = amount
})
end
end
}
},
fluids = {
[PRODUCERS] = {
function(player, search_params, item, results)
if not item.valid then return end
if item.type == "assembling-machine" then
local amount = recipe_produces(item.get_recipe(), "fluid", search_params.name)
if amount ~= false then
table.insert(results, {
entity = item,
location = item.position,
owner = item.last_user,
count = amount
})
end
end
end
}
}
}
-- game.print(tostring(#game.player.surface.find_entities_filtered { type="transport-belt", force=game.player.force }))
}
5 changes: 5 additions & 0 deletions factorio-search-engine_0.0.1/searcher/main_searcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ local searcher = require "searcher/searcher"
local main_searcher = searcher:new()
main_searcher:add_type_plugin(require "plugins/items/items")
main_searcher:add_search_plugin(require "plugins/items/item_container")
--main_searcher:add_search_plugin(require "plugins/items/item_belt")

main_searcher:add_type_plugin(require "plugins/fluids/fluids")
main_searcher:add_search_plugin(require "plugins/fluids/fluids_container")
--main_searcher:add_search_plugin(require "plugins/fluids/fluids_pipe")

main_searcher:add_search_plugin(require "plugins/items/items_fluids_producers")

main_searcher:add_type_plugin(require "plugins/signals/signals")
main_searcher:add_search_plugin(require "plugins/signals/signals_search")
Expand Down

0 comments on commit c9c38b3

Please sign in to comment.