Skip to content

Instantly share code, notes, and snippets.

@morsk
Created October 3, 2024 23:39
Show Gist options
  • Save morsk/d34859517f7a556e703dc96a5600a947 to your computer and use it in GitHub Desktop.
Save morsk/d34859517f7a556e703dc96a5600a947 to your computer and use it in GitHub Desktop.
/sc --[[ blueprint change grid position ]]
local function printf(...) game.player.print(string.format(...)) end
local function handle_blueprint(bp, new_position) --[[ not recursive; does one blueprint ]]
if not (bp.blueprint_snap_to_grid and bp.blueprint_absolute_snapping) then
return 0 --[[ changed nothing ]]
end
local old_x = bp.blueprint_position_relative_to_grid.x
local old_y = bp.blueprint_position_relative_to_grid.y
if new_position.x ~= old_x or new_position.y ~= old_y then
printf("Converting %d,%d -> %d,%d \"%s\"", old_x, old_y, new_position.x, new_position.y, bp.label)
bp.blueprint_position_relative_to_grid = new_position --[[ the only line that actually changes something ]]
return 1 --[[ changed 1 blueprint ]]
else
return 0 --[[ changed nothing ]]
end
end
local function handle(item, new_position) --[[ this is recursive ]]
if item.type == "blueprint" then
return handle_blueprint(item, new_position)
elseif item.type == "blueprint-book" then
local converted = 0
for i = 1,#item.get_inventory(1) do
if item.get_inventory(1)[i].valid_for_read then
converted = converted + handle(item.get_inventory(1)[i], new_position)
end
end
return converted
elseif item.type == "upgrade-item" or item.type == "deconstruction-item" then
return 0 --[[ changed nothing ]]
else
error("weird item type: "..item.type, 0)
end
end
local function go(x, y)
local new_position = {x = x, y = y}
printf("Converted %d blueprint(s).", handle(game.player.cursor_stack, new_position))
end
--[[ I parameterize scripts at the bottom like this, not at the top, because ]]
--[[ it's easier to change the bottom when pasting into the command line. ]]
--[[ Just edit the end of the line before hitting Enter. ]]
go(16,16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment