z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
317aee5
trunk
1local flib_bounding_box =require ( "__flib__.bounding-box" ) 2local flib_format =require ( "__flib__.format" ) 3local flib_math =require ( "__flib__.math" ) 4 5--- @class Util 6local util ={} 7 8util . crafting_machine ={ 9[ "assembling-machine" ] =true , 10[ "character" ] =true , 11[ "furnace" ] =true , 12[ "rocket-silo" ] =true , 13} 14 15--- @param player LuaPlayer 16--- @param text LocalisedString 17function util . flying_text ( player , text ) 18player . create_local_flying_text ( { 19text =text , 20create_at_cursor =true , 21} ) 22player . play_sound ( { path ="utility/cannot_build" } ) 23end 24 25--- @param num number 26--- @return string 27function util . format_number ( num ) 28return flib_format . number ( flib_math . round ( num , 0.01 )) 29end 30 31function util . get_natural_entities () 32return prototypes . get_entity_filtered ( { 33--- @diagnostic disable-next-line unused-fields 34{ filter ="type" , type ="resource" } , 35--- @diagnostic disable-next-line unused-fields 36{ filter ="type" , type ="fish" } , 37--- @diagnostic disable-next-line unused-fields 38{ filter ="type" , type ="tree" } , 39--- @diagnostic disable-next-line unused-fields 40{ filter ="type" , type ="simple-entity" } , 41} ) 42end 43 44--- @param prototype GenericPrototype 45--- @return string path 46--- @return string type 47function util . get_path ( prototype ) 48local type =util . object_name_to_type [ prototype . object_name ] 49return type .. "/" .. prototype . name , type 50end 51 52--- @param obj DatabaseID 53--- @return GenericPrototype 54function util . get_prototype ( obj ) 55return prototypes [ obj . type ][ obj . name ] 56end 57 58--- @param prototype GenericPrototype 59--- @return DatabaseID 60function util . get_id ( prototype ) 61local type =util . object_name_to_type [ prototype . object_name ] 62return { type =type , name =prototype . name } 63end 64 65util . object_name_to_type ={ 66LuaEntity ="entity" , 67LuaEntityPrototype ="entity" , 68LuaEquipmentPrototype ="equipment" , 69LuaFluidPrototype ="fluid" , 70LuaItemPrototype ="item" , 71LuaRecipePrototype ="recipe" , 72LuaRecipe ="recipe" , 73LuaTechnologyPrototype ="technology" , 74LuaTechnology ="technology" , 75LuaTilePrototype ="tile" , 76} 77 78util . show_hidden_enabled =settings . startup [ "rb-enable-show-hidden" ]. value --[[@as boolean]] 79 80--- @param prototype GenericPrototype 81function util . should_include ( prototype ) 82return util . show_hidden_enabled or ( not prototype . hidden and not prototype . hidden_in_factoriopedia ) 83end 84 85--- @return DatabaseID[] 86function util . unique_id_array () 87local hash ={} 88 89return setmetatable ( {} , { 90--- @param self DatabaseID[] 91--- @param index integer 92--- @param value DatabaseID 93__newindex =function ( self , index , value ) 94if not value then 95return 96end 97local key =value . type .. "/" .. value . name 98if hash [ key ] then 99return 100end 101hash [ key ] =true 102rawset ( self , index , value ) 103end , 104} ) 105end 106 107--- @param prototype GenericPrototype 108--- @return LuaGroup 109function util . get_group ( prototype ) 110if prototype . object_name == "LuaEquipmentPrototype" then 111return prototypes . item_group [ "combat" ] 112end 113return prototype . group 114end 115 116--- @param prototype GenericPrototype 117--- @return LuaGroup 118function util . get_subgroup ( prototype ) 119if prototype . object_name == "LuaEquipmentPrototype" then 120return prototypes . item_subgroup [ "rb-uncategorized-equipment" ] 121end 122return prototype . subgroup 123end 124 125--- @param player LuaPlayer 126--- @param entity LuaEntityPrototype 127--- @param recipe LuaRecipePrototype 128function util . create_blueprint ( player , entity , recipe ) 129local cursor_stack =player . cursor_stack 130if not cursor_stack then 131return 132end 133 134if not player . clear_cursor () then 135return 136end 137 138-- Other mods could have done wacky things to the cursor stack 139if not cursor_stack or not cursor_stack . valid then 140return 141end 142 143local collision_box =entity . collision_box 144local height =flib_bounding_box . height ( collision_box ) 145local width =flib_bounding_box . width ( collision_box ) 146 147cursor_stack . set_stack ( { name ="blueprint" , count =1 } ) 148cursor_stack . set_blueprint_entities ( { 149{ 150entity_number =1 , 151name =entity . name , 152position ={ 153-- Entities with an even number of tiles to a side need to be set at -0.5 instead of 0 154math . ceil ( width ) % 2 == 0 and - 0.5 or 0 , 155math . ceil ( height ) % 2 == 0 and - 0.5 or 0 , 156} , 157recipe =recipe . name , 158} , 159} ) 160 161player . add_to_clipboard ( cursor_stack ) 162player . activate_paste () 163end 164 165return util