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