Change from entityID to tag for deduplication

The entityID is not unique per entity. Entites may get a new ID on reload. Or the same ID will be reused by another entity.

Attach "MapCaptured" tag to visited entities.
This commit is contained in:
David Vogel 2022-07-17 17:26:08 +02:00
parent 303f1a9c90
commit 861272187a

View File

@ -31,9 +31,6 @@ CAPTURE_AREA_EXTENDED = {
Bottom = 41984 -- in virtual (world) pixels. (Coordinate is not included in the rectangle) Bottom = 41984 -- in virtual (world) pixels. (Coordinate is not included in the rectangle)
} }
-- Set of already captured entities.
local capturedEntities = {}
local function preparePlayer() local function preparePlayer()
local playerEntity = getPlayer() local playerEntity = getPlayer()
addEffectToEntity(playerEntity, "PROTECTION_ALL") addEffectToEntity(playerEntity, "PROTECTION_ALL")
@ -91,13 +88,13 @@ local function captureScreenshot(x, y, rx, ry, entityFile)
local entities = EntityGetInRadius(x, y, radius) local entities = EntityGetInRadius(x, y, radius)
for _, entityID in ipairs(entities) do for _, entityID in ipairs(entities) do
-- Make sure to only export entities when they are encountered the first time. -- Make sure to only export entities when they are encountered the first time.
if not capturedEntities[entityID] then if not EntityHasTag(entityID, "MapCaptured") then
capturedEntities[entityID] = true
local x, y, rotation, scaleX, scaleY = EntityGetTransform(entityID) local x, y, rotation, scaleX, scaleY = EntityGetTransform(entityID)
local entityName = EntityGetName(entityID) local entityName = EntityGetName(entityID)
local entityTags = EntityGetTags(entityID) local entityTags = EntityGetTags(entityID)
entityFile:write(string.format("%d, %s, %f, %f, %f, %f, %f, %q\n", entityID, entityName, x, y, rotation, scaleX, scaleY, entityTags)) entityFile:write(string.format("%d, %s, %f, %f, %f, %f, %f, %q\n", entityID, entityName, x, y, rotation, scaleX, scaleY, entityTags))
-- TODO: Correctly escape CSV data -- TODO: Correctly escape CSV data
EntityAddTag(entityID, "MapCaptured") -- Prevent recapturing.
end end
end end
entityFile:flush() -- Ensure everything is written to disk before noita decides to crash. entityFile:flush() -- Ensure everything is written to disk before noita decides to crash.
@ -108,21 +105,6 @@ local function captureScreenshot(x, y, rx, ry, entityFile)
end end
local function createOrOpenEntityCaptureFile() local function createOrOpenEntityCaptureFile()
local file = io.open("mods/noita-mapcap/output/entities.csv", "r")
if file then
local _ = file:read() -- Skip first line.
for line in file:lines() do
for field in string.gmatch(line, "([^,]+)") do
local entityID = tonumber(field)
if entityID then
capturedEntities[entityID] = true
end
break
end
end
file:close()
end
-- Create or reopen entities CSV file. -- Create or reopen entities CSV file.
local file = io.open("mods/noita-mapcap/output/entities.csv", "a+") local file = io.open("mods/noita-mapcap/output/entities.csv", "a+")
if file == nil then return nil end if file == nil then return nil end