z_mirror/CursorEnhancements

git clone https://git.raiguard.me/z_mirror/CursorEnhancements

Caleb HeuerPipette offshore pump when over water902f690

smart-quickbar
1.8 KiB69 linesraw
1local bounding_box = require("__flib__/bounding-box")
2
3local util = require("__CursorEnhancements__/scripts/util")
4
5--- @param surface LuaSurface
6--- @param cursor_position MapPosition
7--- @return string?
8local function get_tile_item(surface, cursor_position)
9  -- Get tile
10  local tile_area = bounding_box.from_position(cursor_position, true)
11  local tile = surface.find_tiles_filtered({ area = tile_area })[1]
12  if not tile or not tile.valid then
13    return
14  end
15
16  --- @type LuaEntityPrototype|LuaTilePrototype
17  local prototype = tile.prototype
18  if prototype.collision_mask["water-tile"] then
19    -- XXX: LuaCustomTable doesn't support next()
20    for _, entity in pairs(game.get_filtered_entity_prototypes({ { filter = "type", type = "offshore-pump" } })) do
21      prototype = entity
22      break
23    end
24  end
25
26  local items_to_place_this = prototype.items_to_place_this
27  if not items_to_place_this then
28    return
29  end
30  local item_to_place = items_to_place_this[1]
31  if item_to_place then
32    return item_to_place.name
33  end
34end
35
36--- @param e EventData.CustomInputEvent
37local function on_smart_pipette(e)
38  local player = game.get_player(e.player_index)
39  if not player then
40    return
41  end
42
43  -- Don't override base game logic
44  local cursor_stack = player.cursor_stack
45  if not cursor_stack or cursor_stack.valid_for_read or player.cursor_ghost or player.selected then
46    return
47  end
48
49  local item = util.get_selected_item(e.selected_prototype) or get_tile_item(player.surface, e.cursor_position)
50  if not item then
51    return
52  end
53
54  if not util.set_cursor(player, item) then
55    return
56  end
57
58  player.play_sound({
59    path = "utility/smart_pipette",
60  })
61end
62
63local smartpipette = {}
64
65smartpipette.events = {
66  ["cen-linked-smart-pipette"] = on_smart_pipette,
67}
68
69return smartpipette