Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ahicks92 committed Sep 24, 2024
1 parent 78beaa6 commit 8e033d5
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 174 deletions.
20 changes: 5 additions & 15 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2841,24 +2841,14 @@ function cursor_mode_move(direction, pindex, single_only)
else
-- Larger cursor sizes: scan area
local scan_left_top = {
math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size,
math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size,
x=math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size,
y=math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size,
}
local scan_right_bottom = {
math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1,
math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1,
x=math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1,
y=math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1,
}
players[pindex].nearby.index = 1
players[pindex].nearby.ents = fa_scanner.scan_area(
math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size,
math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size,
players[pindex].cursor_size * 2 + 1,
players[pindex].cursor_size * 2 + 1,
pindex
)
fa_scanner.populate_list_categories(pindex)
players[pindex].cursor_scan_center = players[pindex].cursor_pos
local scan_summary = fa_scanner.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex)
local scan_summary = fa_info.area_scan_summary_info(pindex, scan_left_top, scan_right_bottom)
fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex)
printout(scan_summary, pindex)
end
Expand Down
132 changes: 132 additions & 0 deletions scripts/fa-info.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1497,4 +1497,136 @@ function mod.cursor_is_at_mining_drill_output_part(pindex, ent)
return util.distance(correct_pos, players[pindex].cursor_pos) < 0.6
end

--Returns an info string about the entities and tiles found within an area scan done by an enlarged cursor.
---@param pindex number
---@param left_top fa.Point
---@param right_bottom fa.Point
---@return LocalisedString
function mod.area_scan_summary_info(pindex, left_top, right_bottom)
local result = {}

local chunk_lt_x = math.floor(left_top.x / 32)
local chunk_lt_y = math.floor(left_top.y / 32)
local chunk_rb_x = math.ceil(right_bottom.x / 32)
local chunk_rb_y = math.ceil(right_bottom.y / 32)

local player = assert(game.get_player(pindex))
---@cast player LuaPlayer
local surf = player.surface

local generated_chunk_count = 0
local total_chunks_covered = 0
for cx = chunk_lt_x, chunk_rb_x do
for cy = chunk_lt_y, chunk_rb_y do
if surf.is_chunk_generated({ cx, cy }) then generated_chunk_count = generated_chunk_count + 1 end
total_chunks_covered = total_chunks_covered + 1
end
end
if total_chunks_covered > 0 and generated_chunk_count < 1 then
return "Charted 0%, you need to chart this area by approaching it or using a radar."
elseif total_chunks_covered > 0 and generated_chunk_count < total_chunks_covered then
table.insert(result, "Charted")
table.insert(result, math.floor(generated_chunk_count / total_chunks_covered * 100) .. "%,")
end

---@type { name: string, count: string, category: string }[]
local counts = {}

local covered_area = (right_bottom.x - left_top.x) * (right_bottom.y - left_top.y)
assert(covered_area > 0)

local water_count = surf.count_tiles_filtered({
name = { "water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube" },
area = { left_top, right_bottom },
})

if water_count > 0 then table.insert(counts, { name = "water", count = water_count, category = "resource" }) end

local path_count = surf.count_tiles_filtered({ name = "stone-path", area = { left_top, right_bottom } })
if path_count > 0 then
table.insert(counts, { name = "stone-brick-path", count = path_count, category = "flooring" })
end

local concrete_count = surf.count_tiles_filtered({
name = { "concrete", "hazard-concrete-left", "hazard-concrete-right" },
area = { left_top, right_bottom },
})
if concrete_count > 0 then
table.insert(counts, { name = "concrete", count = concrete_count, category = "flooring" })
end

local refined_concrete_count = surf.count_tiles_filtered({
name = { "refined-concrete", "refined-hazard-concrete-left", "refined-hazard-concrete-right" },
area = { left_top, right_bottom },
})
if refined_concrete_count > 0 then
table.insert(counts, { name = "refined-concrete", count = refined_concrete_count, category = "flooring" })
end

for _, res_proto in pairs(game.entity_prototypes) do
if res_proto.type == "resource" then
local res_count = surf.count_entities_filtered({ name = res_proto.name, area = { left_top, right_bottom } })
table.insert(counts, { name = res_proto.name, count = res_count, category = "resource" })
end
end

local tree_count = surf.count_entities_filtered({ type = "tree", area = { left_top, right_bottom } })
if tree_count > 0 then table.insert(counts, { name = "trees", count = tree_count, category = "resource" }) end

local others = surf.find_entities_filtered({
type = { "resource", "tree" },
area = { left_top, right_bottom },
invert = true,
})

local others_by_proto = {}
for _, ent in pairs(others) do
if ent.valid then others_by_proto[ent.name] = (others_by_proto[ent.name] or 0) + 1 end
end

for n, c in pairs(others_by_proto) do
if c > 0 then table.insert(counts, { name = n, count = c, category = "other" }) end
end

table.sort(counts, function(k1, k2)
return k1.count > k2.count
end)

local count_total = 0
for _, i in pairs(counts) do
count_total = count_total + i.count
end

-- Spacecat can't help us here. Why? We can't have spaces between words and
-- commas, e.g. "iron ," is wrong. The real problem isn't spacecat, it's
-- that this should be localised and the commas should be baked into the
-- localised strings, then those get fed to spacecat.
local contains_list = {}
for _, entry in pairs(counts) do
if entry.count == 0 then break end

local fragment = ""
fragment = fragment .. tostring(entry.count) .. " " .. entry.name
if entry.category == "resource" or entry.category == "flooring" then
fragment = fragment .. " " .. math.floor(entry.count / covered_area * 100) .. " " .. "percent"
end

table.insert(contains_list, fragment .. ",")
end

if next(contains_list) then
table.insert(result, "Area contains")
for _, f in pairs(contains_list) do
table.insert(result, f)
end
table.insert(result, "total space occupied")
table.insert(result, math.floor(count_total / covered_area * 100))
table.insert(result, "percent")
else
table.insert(result, "Area empty")
end

return fa_utils.spacecat_table(result)
end

return mod
45 changes: 37 additions & 8 deletions scripts/fa-utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -925,20 +925,49 @@ function mod.player_was_still_for_1_second(pindex)
end
end

-- Given a list of items which may be stringified, concatenate them all together
-- with a space between, efficiently.
-- Concatenate a bunch of stuff together, efficiently, and return this as a
-- localised string.
--
-- Assumes that tables are localised strings, otherwise calls tostring(x)
--
-- Works with more than 20 items by folding the localised strings into each
-- other, forming a tree structure.
---@return LocalisedString
mod.spacecat = function(...)
local tab = table.pack(...)
local will_cat = {}
mod.spacecat_table(tab)
end

-- Like spacecat but for a table.
---@param tab any[]
---@return LocalisedString
function mod.spacecat_table(tab)
local will_cat = { "" }

for i = 1, tab.n do
for i = 1, #tab do
local ent = tab[i]
local stringified = tostring(ent)
if stringified == nil then stringified = "NIL!" end
table.insert(will_cat, stringified)
local adding = type(ent) == "table" and ent or tostring(ent)
if adding == nil then adding = "NIL!" end
table.insert(will_cat, adding)
table.insert(will_cat, " ")
end

-- 21 because 20 params, then the first is the "" part.
while #will_cat > 21 do
local new_cat = { "" }

for i = 1, #will_cat, 20 do
local seg = { "" }
for j = 1, 20 do
table.insert(seg, will_cat[i + j])
end
table.insert(new_cat, seg)
end

will_cat = new_cat
end

return table.concat(will_cat, " ")
return will_cat
end

--Returns the name for the item related to the entity name being checked
Expand Down
151 changes: 0 additions & 151 deletions scripts/scanner-old.lua
Original file line number Diff line number Diff line change
Expand Up @@ -895,157 +895,6 @@ function mod.selection_down(pindex)
end
end

--Returns an info string about the entities and tiles found within an area scan done by an enlarged cursor.
function mod.area_scan_summary_info(scan_left_top, scan_right_bottom, pindex)
local result = ""
local explored_left_top = {
x = math.floor((players[pindex].cursor_pos.x - 1 - players[pindex].cursor_size) / 32),
y = math.floor((players[pindex].cursor_pos.y - 1 - players[pindex].cursor_size) / 32),
}
local explored_right_bottom = {
x = math.floor((players[pindex].cursor_pos.x + 1 + players[pindex].cursor_size) / 32),
y = math.floor((players[pindex].cursor_pos.y + 1 + players[pindex].cursor_size) / 32),
}
local count = 0
local total = 0
for i = explored_left_top.x, explored_right_bottom.x do
for i1 = explored_left_top.y, explored_right_bottom.y do
if game.get_player(pindex).surface.is_chunk_generated({ i, i1 }) then count = count + 1 end
total = total + 1
end
end
if total > 0 and count < 1 then
result = result .. "Charted 0%, you need to chart this area by approaching it or using a radar."
return result
elseif total > 0 and count < total then
result = result .. "Charted " .. math.floor((count / total) * 100) .. "%, "
end

local percentages = {}
local percent_total = 0
local surf = game.get_player(pindex).surface
--Scan for Tiles and Resources, because they behave weirdly in scan_area due to aggregation, or are skipped
local percent = 0
local res_count = surf.count_tiles_filtered({
name = { "water", "deepwater", "water-green", "deepwater-green", "water-shallow", "water-mud", "water-wube" },
area = { scan_left_top, scan_right_bottom },
})
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "water", percent = percent, count = "resource" }) end
percent_total = percent_total + percent --water counts as filling a space

res_count = surf.count_tiles_filtered({ name = "stone-path", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then
table.insert(percentages, { name = "stone-brick-path", percent = percent, count = "flooring" })
end

res_count = surf.count_tiles_filtered({
name = { "concrete", "hazard-concrete-left", "hazard-concrete-right" },
area = { scan_left_top, scan_right_bottom },
})
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "concrete", percent = percent, count = "flooring" }) end

res_count = surf.count_tiles_filtered({
name = { "refined-concrete", "refined-hazard-concrete-left", "refined-hazard-concrete-right" },
area = { scan_left_top, scan_right_bottom },
})
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then
table.insert(percentages, { name = "refined-concrete", percent = percent, count = "flooring" })
end

res_count = surf.count_entities_filtered({ name = "coal", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "coal", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ name = "stone", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "stone", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ name = "iron-ore", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "iron-ore", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ name = "copper-ore", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "copper-ore", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ name = "uranium-ore", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "uranium-ore", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ name = "crude-oil", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((9 * res_count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5)
if percent > 0 then table.insert(percentages, { name = "crude-oil", percent = percent, count = "resource" }) end

res_count = surf.count_entities_filtered({ type = "tree", area = { scan_left_top, scan_right_bottom } })
percent = math.floor((res_count * 4 / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.5) --trees are bigger than 1 tile
if percent > 0 then table.insert(percentages, { name = "trees", percent = percent, count = res_count }) end
percent_total = percent_total + percent

if #players[pindex].nearby.ents > 0 then --Note: Resources are included here as aggregates.
for i, ent in ipairs(players[pindex].nearby.ents) do
local area = 0
--this confirmation is necessary because all we have is the ent name, and some distant resources show up on the list.
if
fa_utils.is_ent_inside_area(
fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)),
scan_left_top,
scan_right_bottom,
pindex
)
then
area = fa_utils.get_ent_area_from_name(
fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name)),
pindex
)
if area == -1 then
area = 1
game.get_player(pindex).print(
fa_utils.get_substring_before_space(fa_utils.get_substring_before_comma(ent.name))
.. " could not be found for the area check ",
{ volume_modifier = 0 }
) --bug: unable to get area from name
end
end
local percentage = math.floor(
(area * players[pindex].nearby.ents[i].count / ((1 + players[pindex].cursor_size * 2) ^ 2) * 100) + 0.95
) --Tolerate up to 0.05%
if not ent.aggregate and percentage > 0 then
table.insert(
percentages,
{ name = ent.name, percent = percentage, count = players[pindex].nearby.ents[i].count }
)
end
percent_total = percent_total + percentage
end
table.sort(percentages, function(k1, k2)
return k1.percent > k2.percent
end)
result = result .. " Area contains "
local i = 1
while i <= #percentages and (i <= 4 or percentages[i].percent > 1) do
result = result .. ", " .. percentages[i].count .. " " .. percentages[i].name .. " "
if percentages[i].count == "resource" or percentages[i].count == "flooring" then
result = result .. percentages[i].percent .. "% "
end
i = i + 1
end
if percent_total == 0 then --Note there are still some entities in here, but with zero area...
result = result .. " nothing "
elseif i >= 4 then
result = result .. ", and other things "
end
result = result .. ", total space occupied " .. math.floor(percent_total) .. " percent "
else
result = result .. " Empty Area "
end
players[pindex].cursor_scanned = true
return result
end

--Brief extra entity info is given here, for mentioning in the scanner list. If the parameter "info_comes_after_indexing" is not true, then this info distinguishes the entity plus its description as a new line of the scanner list, such as how assembling machines with different recipes are listed separately.
function mod.ent_extra_list_info(ent, pindex, info_comes_after_indexing)
local result = ""
Expand Down

0 comments on commit 8e033d5

Please sign in to comment.