z_mirror/BlueprintTools
git clone https://git.raiguard.me/z_mirror/BlueprintTools
d6a0c1c
trunk
1local constants =require ( "constants" ) 2 3local util ={} 4 5--- Get the current blueprint from the item stack, if it exists and is set up. 6--- @param item_stack LuaItemStack 7--- @return LuaItemStack? 8function util . get_blueprint ( item_stack ) 9if not item_stack or not item_stack . valid_for_read then 10return 11end 12local blueprint 13if item_stack . is_blueprint then 14blueprint =item_stack 15elseif item_stack . is_blueprint_book and item_stack . active_index then 16local inventory =item_stack . get_inventory ( defines . inventory . item_main ) --[[@as LuaInventory]] 17if inventory . is_empty () or item_stack . active_index > # inventory then 18return 19end 20blueprint =util . get_blueprint ( inventory [ item_stack . active_index ]) 21end 22 23if blueprint and blueprint . is_blueprint_setup () then 24return blueprint 25end 26end 27 28--- @param entity BlueprintEntity 29function util . get_rail_tiles ( entity ) 30local category =constants . rail_tiles [ prototypes . entity [ entity . name ]. type ] 31if not category then 32return 33end 34 35return category [ entity . direction or 0 ] 36end 37 38--- Displays notification for player at cursor position. 39--- @param player LuaPlayer Player to notify. 40--- @param message LocalisedString Message to show to player. 41--- @param sound SoundPath|nil Optional sound to play for the player. 42function util . cursor_notification ( player , message , sound ) 43player . create_local_flying_text ( { 44text =message , 45create_at_cursor =true , 46} ) 47 48if sound then 49player . play_sound ( { path =sound } ) 50end 51end 52 53--- Calculates largest build grid size for all entities in the passed-in list of blueprints. 54--- Useful when dealing with things like rails and train stops that can only be repositioned in increments of two. 55--- @param blueprints LuaItemStack[] List of blueprints for which to calculate the value. Must be valid for read. 56function util . get_blueprint_largest_build_grid_size ( blueprints ) 57local build_grid_size =1 58 59for _ , blueprint in pairs ( blueprints ) do 60for _ , entity in pairs ( blueprint . get_blueprint_entities () or {} ) do 61if prototypes . entity [ entity . name ]. building_grid_bit_shift > build_grid_size then 62build_grid_size =prototypes . entity [ entity . name ]. building_grid_bit_shift 63end 64end 65end 66 67return build_grid_size 68end 69 70--- Returns list of all blueprints in a blueprint book. 71--- @param book LuaItemStack 72--- @return LuaItemStack[] 73function util . get_book_blueprints ( book ) 74--- Helper function that recursively collects blueprints from the passed-in blueprint book and stores them in the passed-in result table. 75--- @param book LuaItemStack 76--- @param result_table LuaItemStack[] 77local function collect_blueprints ( book , result_table ) 78local items =book . get_inventory ( defines . inventory . item_main ) --[[@as LuaInventory]] 79for i =1 , # items do 80local item =items [ i ] 81if not item then 82return 83elseif item . is_blueprint_book then 84collect_blueprints ( item , result_table ) 85elseif item . is_blueprint then 86table . insert ( result_table , item ) 87end 88end 89end 90 91local blueprints ={} 92collect_blueprints ( book , blueprints ) 93return blueprints 94end 95 96--- @param box BoundingBox 97--- @param callback fun(pos: MapPosition) 98function util . for_each_position ( box , callback ) 99for y =box . left_top . y , box . right_bottom . y - 1 do 100for x =box . left_top . x , box . right_bottom . x - 1 do 101callback ( { x =x , y =y } ) 102end 103end 104end 105 106return util