Advertisement
Altaric

Factorio/Inserter_tool

Dec 23rd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. -- Inserter tool
  2. local inserter_tool_selected = {}
  3. local inserter_tool_status = 0
  4. script.on_event(defines.events.on_player_selected_area, function(event)
  5.     if event.item == "picker-ore-eraser" then
  6.         local player = game.players[event.player_index]
  7.         local loc = event.area.left_top
  8.         local entities = event.entities
  9.         local d
  10.         local reach = 2.5
  11.         reach = reach * reach
  12.         if (inserter_tool_status == 0) then
  13.             inserter_tool_selected = {}
  14.             for _,entity in ipairs(entities) do
  15.                 if entity.type == "inserter" then
  16.                     table.insert(inserter_tool_selected, entity)
  17.                 end
  18.             end
  19.             if (#inserter_tool_selected  > 0) then
  20.                 player.print("Selected " .. #inserter_tool_selected .. " inserters. Please select the drop location.")
  21.                 inserter_tool_status = 1
  22.             end
  23.         else if (inserter_tool_status == 1) then
  24.             inserter_tool_status = 2
  25.             for _,entity in ipairs(inserter_tool_selected) do
  26.                 d = (entity.position.x - loc.x)*(entity.position.x - loc.x)+(entity.position.y - loc.y)*(entity.position.y - loc.y)
  27.                 if (d > reach) then
  28.                     player.print("Too far! "..math.sqrt(d))
  29.                     inserter_tool_status = 0
  30.                     break
  31.                 else
  32.                     entity.drop_position = loc                
  33.                     entity.direction = entity.direction -- set direction to force update
  34.                 end
  35.             end            
  36.         else if (inserter_tool_status == 2) then
  37.             inserter_tool_status = 0
  38.             for _,entity in ipairs(inserter_tool_selected) do
  39.                 d = (entity.position.x - loc.x)*(entity.position.x - loc.x)+(entity.position.y - loc.y)*(entity.position.y - loc.y)
  40.                 if (d > reach) then
  41.                     player.print("Too far! "..math.sqrt(d))
  42.                     break
  43.                 else
  44.                     entity.pickup_position = loc
  45.                     entity.direction = entity.direction -- set direction to force update
  46.                 end
  47.             end      
  48.         end end end --if (inserter_tool_status == 0) then
  49.     end
  50. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement