Side Inserters

by GotLag

Adds hotkeys to adjust inserter pickup and drop locations.

Content
3 years ago
0.13 - 1.1
28.1K
Logistics

i Little Code: Range Toggle for any long Inserter

6 years ago

data.lua

{
type = "custom-input",
name = "inserter-Pickup-long-distance-toggle",
key_sequence = "CONTROL + C",
consuming = "script-only"
},
{
type = "custom-input",
name = "inserter-Drop-long-distance-toggle",
key_sequence = "CONTROL + SHIFT + C",
consuming = "script-only"
},

control.lua

script.on_event("inserter-Pickup-long-distance-toggle", function(event)
local selection = game.players[event.player_index].selected
if selection and string.find(selection.name, "long") and (selection.type == "inserter" or (selection.type == "entity-ghost" and selection.ghost_type == "inserter")) then
TogglePickupDistanceLong(selection)
end
end)

script.on_event("inserter-Drop-long-distance-toggle", function(event)
local selection = game.players[event.player_index].selected
if selection and string.find(selection.name, "long") and (selection.type == "inserter" or (selection.type == "entity-ghost" and selection.ghost_type == "inserter")) then
ToggleDropDistanceLong(selection)
end
end)

....

function TogglePickupDistanceLong(entity)
if string.find(entity.name, "long") then

local OFFSET = 1
local px = entity.pickup_position.x - entity.position.x
local py = entity.pickup_position.y - entity.position.y
local dx = entity.drop_position.x - entity.position.x
local dy = entity.drop_position.y - entity.position.y
local dir = GetPickupDirection(entity)


if dir == defines.direction.north then
  if py < 1.5 then
    py = py + OFFSET
  else
    py = py - OFFSET
  end
elseif dir == defines.direction.south then
  if py < -1.5 then
    py = py + OFFSET
  else
    py = py - OFFSET
  end
elseif dir == defines.direction.west then
  if px < 1.5 then
    px = px + OFFSET
  else
    px = px - OFFSET
  end
elseif dir == defines.direction.east then
  if px < -1.5 then
    px = px + OFFSET
  else
    px = px - OFFSET
  end
end



entity.pickup_position =

{
entity.position.x + px,
entity.position.y + py
}

if GetPickupDirection(entity) == entity.direction and math.abs(px - dx) < 0.5 and math.abs(py - dy) < 0.5 then
  ToggleDropDistanceLong(entity)
end

end
end

function ToggleDropDistanceLong(entity)
if string.find(entity.name, "long") then

local OFFSET = 1
local px = entity.pickup_position.x - entity.position.x
local py = entity.pickup_position.y - entity.position.y
local dx = entity.drop_position.x - entity.position.x
local dy = entity.drop_position.y - entity.position.y
local dir = entity.direction

if dir == defines.direction.north then
  if dy < 1.5 then
    dy = dy + OFFSET
  else
    dy = dy - OFFSET
  end
elseif dir == defines.direction.south then
  if dy < -1.5 then
    dy = dy + OFFSET
  else
    dy = dy - OFFSET
  end
elseif dir == defines.direction.west then
  if dx < 1.5 then
    dx = dx + OFFSET
  else
    dx = dx - OFFSET
  end
elseif dir == defines.direction.east then
  if dx < -1.5 then
    dx = dx + OFFSET
  else
    dx = dx - OFFSET
  end
end


entity.drop_position = {entity.position.x + dx, entity.position.y + dy}
-- set direction to force update of insert location when hand is already full
entity.direction = entity.direction

if GetPickupDirection(entity) == entity.direction and math.abs(px - dx) < 0.5 and math.abs(py - dy) < 0.5 then
  TogglePickupDistanceLong(entity)
end

end
end

....

*Change in function RotatePickup*

if GetPickupDirection(entity) == entity.direction and math.abs(px - dx) < 0.5 and math.abs(py - dy) < 0.5 then
ApplyPickupRotation(entity, clockwise)
end

6 years ago
(updated 6 years ago)

Edit: nevermind, I added everyting and it works. But found one bug when changing pickup side for any inserter.
We get this error message:
Error while running event Side Inserters::rotate-inserter-pickup (ID 106)
Side Inserters/control.lua:97: attempt to perform arithmetic on global 'px' (a nil value)

6 years ago

declaring the variables as local inside the function solves the problem

New response