z_mirror/MouseOverConstruction

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

Caleb HeuerEnsure that recheck_on_move is initialized6750c47

trunk
3.3 KiB138 linesraw
1local construct = require("scripts.construct")
2local deconstruct = require("scripts.deconstruct")
3local repair = require("scripts.repair")
4local upgrade = require("scripts.upgrade")
5
6--- @type Set<string>
7local ignored_entities = prototypes.mod_data["moc-ignored-entities"].data
8
9--- @param entity LuaEntity
10local function should_ignore_entity(entity)
11  local useful_name = ""
12  if entity.type == "entity-ghost" then
13    useful_name = entity.ghost_name
14  else
15    useful_name = entity.name
16  end
17  return ignored_entities[useful_name] or false
18end
19
20--- @param player LuaPlayer
21--- @param entity LuaEntity
22local function try_execute(player, entity)
23  if not storage.mouseover_active[player.index] then
24    return
25  end
26
27  if should_ignore_entity(entity) then
28    return
29  end
30
31  if not player.can_reach_entity(entity) then
32    storage.recheck_on_move[player.index] = true
33    return
34  end
35
36  local cursor_stack = player.cursor_stack
37  if not cursor_stack then
38    return
39  end
40
41  local is_empty = not cursor_stack.valid_for_read
42  local is_repair_tool = not is_empty and cursor_stack.type == "repair-tool"
43
44  local upgrade_prototype, upgrade_quality = entity.get_upgrade_target()
45  local entity_name, entity_quality = entity.name, entity.quality
46  if upgrade_prototype then
47    --- @cast upgrade_quality -?
48    entity_name = upgrade_prototype.name
49    entity_quality = upgrade_quality
50  elseif entity.type == "entity-ghost" then
51    entity_name = entity.ghost_name
52  end
53  local matches_selected = not is_empty
54    and cursor_stack.prototype.place_result
55    and cursor_stack.prototype.place_result.name == entity_name
56    and cursor_stack.quality == entity_quality
57
58  local should_execute = matches_selected or is_empty or is_repair_tool
59
60  if not should_execute then
61    return
62  end
63
64  if construct(player, entity) then
65    return
66  end
67
68  if is_repair_tool and repair.start(player, entity) then
69    return
70  end
71
72  if upgrade(player, entity) then
73    return
74  end
75
76  deconstruct.start(player, entity)
77end
78
79--- @param e EventData.on_player_changed_position
80local function on_player_changed_position(e)
81  if not storage.recheck_on_move[e.player_index] then
82    return
83  end
84  storage.recheck_on_move[e.player_index] = nil
85
86  if not storage.mouseover_active[e.player_index] then
87    return
88  end
89
90  local player = game.get_player(e.player_index)
91  if not player then
92    return
93  end
94
95  local selected = player.selected
96  if not selected then
97    return
98  end
99
100  try_execute(player, selected)
101end
102
103--- @param e EventData.on_selected_entity_changed
104local function on_selected_entity_changed(e)
105  deconstruct.cancel(e.player_index)
106  repair.cancel(e.player_index)
107  storage.recheck_on_move[e.player_index] = nil
108
109  local player = game.get_player(e.player_index)
110  --- @cast player -?
111
112  local selected = player.selected
113  if not selected then
114    return
115  end
116
117  try_execute(player, selected)
118end
119
120local function init()
121  if not storage.recheck_on_move then
122    --- @type Set<PlayerIndex>
123    storage.recheck_on_move = {}
124  end
125end
126
127--- @class orchestrator_handler : event_handler
128local orchestrator_handler = {}
129
130orchestrator_handler.on_init = init
131orchestrator_handler.on_configuration_changed = init
132
133orchestrator_handler.events = {
134  [defines.events.on_player_changed_position] = on_player_changed_position,
135  [defines.events.on_selected_entity_changed] = on_selected_entity_changed,
136}
137
138return orchestrator_handler