z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
04c2d70
trunk
1local grouped =require ( "scripts.database.grouped" ) 2local util =require ( "scripts.util" ) 3 4--- Each top-level prototype sorted into groups and subgroups for the search panel. 5--- @class SearchTree 6--- @field groups table<string, table<string, GenericPrototype[]>> Group name -> subgroup name -> members 7--- @field order table<SpritePath, integer> 8 9--- @alias GenericPrototype LuaEntityPrototype|LuaEquipmentPrototype|LuaFluidPrototype|LuaItemPrototype|LuaRecipePrototype|LuaTechnologyPrototype|LuaTilePrototype 10 11--- @param grouping GroupingMode 12--- @param materials_only boolean? 13--- @return SearchTree 14local function build_tree ( grouping , materials_only ) 15--- @type SearchTree 16local self ={ 17groups ={} , 18order ={} , 19} 20 21--- @param prototype GenericPrototype 22local function add ( prototype ) 23if not util . should_include ( prototype ) then 24return 25end 26if prototype . factoriopedia_alternative then 27return 28end 29if prototype . parameter and prototype . object_name ~= "LuaEntityPrototype" then 30return 31end 32 33local subgroup =self . groups [ util . get_group ( prototype ). name ][ util . get_subgroup ( prototype ). name ] 34assert ( subgroup , "Subgroup was nil." ) 35subgroup [ # subgroup + 1 ] =prototype 36end 37 38for group_name , group_prototype in pairs ( prototypes . item_group ) do 39local subgroups ={} 40for _ , subgroup_prototype in pairs ( group_prototype . subgroups ) do 41subgroups [ subgroup_prototype . name ] ={} 42end 43self . groups [ group_name ] =subgroups 44end 45 46for _ , item in pairs ( prototypes . item ) do 47add ( item ) 48end 49for _ , fluid in pairs ( prototypes . fluid ) do 50add ( fluid ) 51end 52if not materials_only then 53for _ , entity in pairs ( prototypes . entity ) do 54if grouping == "none" or not grouped . material [ util . get_path ( entity )] then 55add ( entity ) 56end 57end 58for _ , recipe in pairs ( prototypes . recipe ) do 59if grouping ~= "all" or not grouped . material [ util . get_path ( recipe )] then 60add ( recipe ) 61end 62end 63for _ , tile in pairs ( prototypes . tile ) do 64if grouping == "none" or not grouped . material [ util . get_path ( tile )] then 65add ( tile ) 66end 67end 68-- Space location 69-- Asteroid chunk 70-- Ammo 71-- Space Connection 72-- Virtual signal 73-- Surface 74end 75 76local order =0 77for group_name , group in pairs ( self . groups ) do 78for subgroup_name , subgroup in pairs ( group ) do 79if not next ( subgroup ) then 80group [ subgroup_name ] =nil 81goto continue 82end 83 84-- TODO: This doesn't match up with Factoriopedia 85table . sort ( subgroup , function ( a , b ) 86local a_order , b_order =a . order , b . order 87if a_order == b_order then 88return a . name < b . name 89end 90return a_order < b_order 91end ) 92 93for i =1 , # subgroup do 94order =order + 1 95self . order [ util . get_path ( subgroup [ i ])] =order 96end 97 98:: continue :: 99end 100if not next ( group ) then 101self . groups [ group_name ] =nil 102end 103end 104 105return self 106end 107 108--- @class SearchTreeMod 109local search_tree ={} 110 111search_tree . all =build_tree ( "all" ) 112search_tree . separate_recipes =build_tree ( "exclude-recipes" ) 113search_tree . materials =build_tree ( "none" , true ) 114search_tree . plain =build_tree ( "none" ) 115 116return search_tree