z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
a9f2be7
trunk
1--- @class flib_technology 2local flib_technology ={} 3 4--- Gets the active or saved research progress for the given technology. 5--- @param technology LuaTechnology 6--- @param level uint 7--- @return double 8function flib_technology . get_research_progress ( technology , level ) 9local force =technology . force 10local current_research =force . current_research 11if current_research and current_research . name == technology . name then 12if technology . level == level or not flib_technology . is_multilevel ( technology ) then 13return force . research_progress 14else 15return 0 16end 17elseif technology . level == level then 18return technology . saved_progress 19else 20return 0 21end 22end 23 24--- Gets the research unit count for the given technology. 25--- @param technology LuaTechnology 26--- @param level uint? 27--- @return uint 28function flib_technology . get_research_unit_count ( technology , level ) 29local formula =technology . research_unit_count_formula 30if formula then 31local level =level or technology . level 32return math . floor ( helpers . evaluate_expression ( formula , { l =level , L =level } )) 33end 34return math . floor ( technology . research_unit_count ) 35end 36 37--- Returns whether the technology has multiple levels. 38--- @param technology LuaTechnology|LuaTechnologyPrototype 39--- @return boolean 40function flib_technology . is_multilevel ( technology ) 41if technology . object_name == "LuaTechnology" then 42technology =technology . prototype 43end 44return technology . level ~= technology . max_level 45end 46 47--- Returns `true` if the first technology should be ordered before the second technology. For use in `table.sort`. 48--- @param tech_a LuaTechnologyPrototype 49--- @param tech_b LuaTechnologyPrototype 50--- @return boolean 51function flib_technology . sort_predicate ( tech_a , tech_b ) 52local ingredients_a =tech_a . research_unit_ingredients 53local ingredients_b =tech_b . research_unit_ingredients 54local len_a =# ingredients_a 55local len_b =# ingredients_b 56-- Always put technologies with zero ingredients at the front 57if ( len_a == 0 ) ~= ( len_b == 0 ) then 58return len_a == 0 59end 60if # ingredients_a > 0 then 61-- Compare ingredient order strings 62-- Check the most expensive packs first, and sort based on the first difference 63for i =0 , math . min ( len_a , len_b ) - 1 do 64local ingredient_a =ingredients_a [ len_a - i ] 65local ingredient_b =ingredients_b [ len_b - i ] 66local order_a =prototypes . item [ ingredient_a . name ]. order 67local order_b =prototypes . item [ ingredient_b . name ]. order 68-- Cheaper pack goes in front 69if order_a ~= order_b then 70return order_a < order_b 71end 72end 73-- Sort the technology with fewer ingredients in front 74if len_a ~= len_b then 75return len_a < len_b 76end 77end 78-- Compare technology order strings 79local order_a =tech_a . order 80local order_b =tech_b . order 81if order_a ~= order_b then 82return order_a < order_b 83end 84-- Compare prototype names 85return tech_a . name < tech_b . name 86end 87 88--- Returns the technology's prototype name with the level suffix stripped. 89--- @param technology LuaTechnology|LuaTechnologyPrototype 90--- @return string 91function flib_technology . get_base_name ( technology ) 92local result =string . gsub ( technology . name , "%-%d*$" , "" ) 93return result 94end 95 96--- If the technology is multi-level, returns the technology's base name with that level appended, otherwise returns the technology name. 97--- @param technology LuaTechnology 98--- @param level uint 99--- @return string 100function flib_technology . get_leveled_name ( technology , level ) 101if flib_technology . is_multilevel ( technology ) then 102return flib_technology . get_base_name ( technology ) .. "-" .. level 103else 104return technology . name 105end 106end 107 108--- @enum TechnologyResearchState 109flib_technology . research_state ={ 110available =1 , 111conditionally_available =2 , 112not_available =3 , 113researched =4 , 114disabled =5 , 115} 116 117return flib_technology