Code: Select all
local function scale_position(position, scale, hr)
-- Make sure position is in correct format
position = position or { x = 0, y = 0}
if type(position) ~= "table" or table_size(position) ~= 2 then
error(serpent.line(position) .. " is not a valid position!")
end
-- Is this for the HR version?
hr = hr and 1/scale_factor or 1
local x = (position.x or position[1]) * scale * hr
local y = (position.y or position[2]) * scale * hr
return (position.x and position.y) and { x = x, y = y} or {x, y}
end
local function scale_image(img, scale)
if not img.scaled then
-- Set scale (defaults to 1 if not explicitly set)
img.scale = (img.scale or 1) * scale
if img.hr_version then
img.hr_version.scale = (img.hr_version.scale or .5) * scale
end
-- Set shift
img.shift = scale_position(img.shift, scale)
if img.hr_version then
img.hr_version.shift = scale_position(img.hr_version.shift, scale, "HR")
end
-- Set position
if img.position then
img.position = scale_position(img.position, scale)
if img.hr_version then
img.hr_version.position = scale_position(img.hr_version.position, scale, "HR")
end
end
-- Mark picture as scaled -- otherwise it will be scaled each time it's used!
img.scaled = true
if img.hr_version then
img.hr_version.scaled = true
end
end
end
local function recurse(tab, scale)
-- Top level may already contain an image
if tab.filename and string.match(tab.filename:lower(), "^.+%.png") then
scale_image(tab, scale)
end
for p, picture in pairs(tab) do
if type(picture) == "table" then
if picture.filename and string.match(picture.filename:lower(), "^.+%.png") then
scale_image(picture, scale)
else
recurse(picture, scale)
end
end
end
end
Code: Select all
local function scale_bounding_boxes(prototype, scale)
local bounding_box
for b, box in pairs({
"collision_box",
"drawing_box",
"hit_visualization_box",
"map_generator_bounding_box",
"selection_box",
"sticker_box",
}) do
bounding_box = prototype[box]
if bounding_box then
prototype[box] = {
scale_position(bounding_box[1], scale),
scale_position(bounding_box[2], scale),
}
end
end
end
- For a scale factor <1, the selection box has the correct dimensions, but it has an offset which didn't get scaled (see right part of the picture). Did I forget to scale some property?
- For a scale factor >1, the selection box will be too big and the game errors on loading. I've added some logging to the function scale_bounding_boxes(), here is the relevant part of the log file:
Code: Select all
1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Scaling bounding boxes of logistic-robot (110%) 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for collision_box 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Before scaling: {{0, 0}, {0, 0}} 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: After scaling: {{0, 0}, {0, 0}} 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for drawing_box 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for hit_visualization_box 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Before scaling: {{-0.1, -1.1000000000000001}, {0.1, -1}} 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: After scaling: {{-0.11000000000000001, -1.2100000000000002}, {0.11000000000000001, -1.1000000000000001}} 1.925 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for map_generator_bounding_box 1.926 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for selection_box 1.926 Script @__SmallRobots__/data-final-fixes.lua:5: Before scaling: {{-0.5, -1.5}, {0.5, -0.5}} 1.926 Script @__SmallRobots__/data-final-fixes.lua:5: After scaling: {{-0.55, -1.6500000000000002}, {0.55, -0.55}} 1.926 Script @__SmallRobots__/data-final-fixes.lua:5: Looking for sticker_box! 2.028 Checksum for core: 870127790 2.028 Checksum of base: 3065294274 2.028 Checksum of SmallRobots: 1392438958 2.239 Error ModManager.cpp:1560: Error while loading entity prototype "logistic-robot" (logistic-robot): Selection box has to have distance at most 0.5 from the [0,0] point.