z_mirror/Tapeline
git clone https://git.raiguard.me/z_mirror/Tapeline
cae5ebf
revert-47-freeze-fix
1local flib_bounding_box =require ( "__flib__.bounding-box" ) 2 3--- @param settings TapeSettings 4--- @param tape Tape? 5--- @return string 6local function build_label ( settings , tape ) 7local output ="" 8if tape then 9output =output 10.. tostring ( flib_bounding_box . width ( tape . box )) 11.. "×" 12.. tostring ( flib_bounding_box . height ( tape . box )) 13.. " | " 14end 15if settings . mode == "subgrid" then 16return output .. "Subgrid mode | Size: " .. tostring ( settings . subgrid_size ) 17else 18return output .. "Split mode | # of splits: " .. tostring ( settings . splits ) 19end 20end 21 22--- @param player LuaPlayer 23--- @return LuaItemStack? 24local function get_tool ( player ) 25local cursor_stack =player . cursor_stack 26if cursor_stack and cursor_stack . valid_for_read and cursor_stack . name == "tl-tool" then 27return cursor_stack 28end 29end 30 31--- @param player LuaPlayer 32--- @param tape Tape? 33local function set_tool ( player , tape ) 34local cursor_stack =player . cursor_stack 35if cursor_stack then 36if not cursor_stack . valid_for_read or cursor_stack . name == "tl-tool" or player . clear_cursor () then 37cursor_stack . set_stack ( { name ="tl-tool" , count =10 } ) 38local settings =tape and tape . settings or storage . player_settings [ player . index ] 39cursor_stack . label =build_label ( settings , tape ) 40end 41if player . controller_type == defines . controllers . character and player . character_build_distance_bonus < 1000000 then 42player . character_build_distance_bonus =player . character_build_distance_bonus + 1000000 43end 44end 45end 46 47--- @param e EventData.on_player_cursor_stack_changed 48local function on_player_cursor_stack_changed ( e ) 49local player =game . get_player ( e . player_index ) --[[@as LuaPlayer]] 50local player_tool =get_tool ( player ) 51if player_tool and not storage . holding_tool [ e . player_index ] then 52storage . holding_tool [ player . index ] =true 53set_tool ( player , storage . editing [ e . player_index ] or storage . drawing [ e . player_index ]) 54elseif not player_tool and storage . holding_tool [ player . index ] then 55storage . holding_tool [ player . index ] =nil 56if player . controller_type == defines . controllers . character and player . character_build_distance_bonus >= 1000000 then 57player . character_build_distance_bonus =player . character_build_distance_bonus - 1000000 58end 59end 60end 61 62--- @param e EventData.on_built_entity 63local function on_built_entity ( e ) 64local entity =e . entity 65if not entity . valid then 66return 67end 68local name =entity . name 69if name == "entity-ghost" then 70name =entity . ghost_name 71end 72if name ~= "tl-dummy-entity" then 73return 74end 75local player =game . get_player ( e . player_index ) 76if not player then 77return 78end 79local tape =storage . editing [ e . player_index ] or storage . drawing [ e . player_index ] 80if not tape then 81return 82end 83set_tool ( player , tape ) 84end 85 86--- @param e EventData.on_player_selected_area|EventData.on_player_alt_selected_area 87local function on_player_selected_area ( e ) 88if e . item ~= "tl-tool" then 89return 90end 91local player =game . get_player ( e . player_index ) 92if not player then 93return 94end 95set_tool ( player , storage . editing [ e . player_index ]) 96end 97 98--- @param e EventData.on_player_removed 99local function on_player_removed ( e ) 100storage . holding_tool [ e . player_index ] =nil 101end 102 103local tool ={} 104 105function tool . on_init () 106--- @type table<uint, boolean> 107storage . holding_tool ={} 108end 109 110tool . events ={ 111[ defines . events . on_built_entity ] =on_built_entity , 112[ defines . events . on_player_alt_selected_area ] =on_player_selected_area , 113[ defines . events . on_player_cursor_stack_changed ] =on_player_cursor_stack_changed , 114[ defines . events . on_player_selected_area ] =on_player_selected_area , 115[ defines . events . on_player_removed ] =on_player_removed , 116} 117 118tool . set =set_tool 119tool . get =get_tool 120 121return tool