z_mirror/MouseOverConstruction
git clone https://git.raiguard.me/z_mirror/MouseOverConstruction
6750c47
trunk
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 ) 11local useful_name ="" 12if entity . type == "entity-ghost" then 13useful_name =entity . ghost_name 14else 15useful_name =entity . name 16end 17return ignored_entities [ useful_name ] or false 18end 19 20--- @param player LuaPlayer 21--- @param entity LuaEntity 22local function try_execute ( player , entity ) 23if not storage . mouseover_active [ player . index ] then 24return 25end 26 27if should_ignore_entity ( entity ) then 28return 29end 30 31if not player . can_reach_entity ( entity ) then 32storage . recheck_on_move [ player . index ] =true 33return 34end 35 36local cursor_stack =player . cursor_stack 37if not cursor_stack then 38return 39end 40 41local is_empty =not cursor_stack . valid_for_read 42local is_repair_tool =not is_empty and cursor_stack . type == "repair-tool" 43 44local upgrade_prototype , upgrade_quality =entity . get_upgrade_target () 45local entity_name , entity_quality =entity . name , entity . quality 46if upgrade_prototype then 47--- @cast upgrade_quality -? 48entity_name =upgrade_prototype . name 49entity_quality =upgrade_quality 50elseif entity . type == "entity-ghost" then 51entity_name =entity . ghost_name 52end 53local matches_selected =not is_empty 54and cursor_stack . prototype . place_result 55and cursor_stack . prototype . place_result . name == entity_name 56and cursor_stack . quality == entity_quality 57 58local should_execute =matches_selected or is_empty or is_repair_tool 59 60if not should_execute then 61return 62end 63 64if construct ( player , entity ) then 65return 66end 67 68if is_repair_tool and repair . start ( player , entity ) then 69return 70end 71 72if upgrade ( player , entity ) then 73return 74end 75 76deconstruct . start ( player , entity ) 77end 78 79--- @param e EventData.on_player_changed_position 80local function on_player_changed_position ( e ) 81if not storage . recheck_on_move [ e . player_index ] then 82return 83end 84storage . recheck_on_move [ e . player_index ] =nil 85 86if not storage . mouseover_active [ e . player_index ] then 87return 88end 89 90local player =game . get_player ( e . player_index ) 91if not player then 92return 93end 94 95local selected =player . selected 96if not selected then 97return 98end 99 100try_execute ( player , selected ) 101end 102 103--- @param e EventData.on_selected_entity_changed 104local function on_selected_entity_changed ( e ) 105deconstruct . cancel ( e . player_index ) 106repair . cancel ( e . player_index ) 107storage . recheck_on_move [ e . player_index ] =nil 108 109local player =game . get_player ( e . player_index ) 110--- @cast player -? 111 112local selected =player . selected 113if not selected then 114return 115end 116 117try_execute ( player , selected ) 118end 119 120local function init () 121if not storage . recheck_on_move then 122--- @type Set<PlayerIndex> 123storage . recheck_on_move ={} 124end 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