Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local is_plant = { Tree = true, Grass = true, Bush = true }
- local function vivify(map,key)
- if not map[key] then map[key] = {} end
- return map[key]
- end
- local function count_pop(ninfo, pop, div)
- ninfo.count = (ninfo.count or 0) + 1
- if pop.count_min ~= 10000001 then
- ninfo.min_sum = (ninfo.min_sum or 0) + pop.count_min
- ninfo.min_sum_d = (ninfo.min_sum_d or 0) + pop.count_min/div
- else
- ninfo.min_inf = (ninfo.min_inf or 0) + 1
- end
- if pop.count_max ~= 10000001 then
- ninfo.max_sum = (ninfo.max_sum or 0) + pop.count_max
- ninfo.max_sum_d = (ninfo.max_sum_d or 0) + pop.count_max/div
- else
- ninfo.max_inf = (ninfo.max_inf or 0) + 1
- end
- end
- local function report_pop(ninfo)
- return string.format(
- "count %d, sum %d, avg %f (%f), max sum %d, avg %f (%f)",
- (ninfo.count or 0),
- (ninfo.min_sum or 0),
- ((ninfo.min_sum or 0) / ((ninfo.count or 1) - (ninfo.min_inf or 0))),
- ((ninfo.min_sum_d or 0) / ((ninfo.count or 1) - (ninfo.min_inf or 0))),
- (ninfo.max_sum or 0),
- ((ninfo.max_sum or 0) / ((ninfo.count or 1) - (ninfo.max_inf or 0))),
- ((ninfo.max_sum_d or 0) / ((ninfo.count or 1) - (ninfo.max_inf or 0)))
- )
- end
- local function sort_keys(tab)
- local kt = {}
- for k,v in pairs(tab) do table.insert(kt,k) end
- table.sort(kt)
- return ipairs(kt)
- end
- function classify_population(info, pop, key, div)
- local ptype = df.world_population_type[pop.type]
- local tinfo = vivify(info, ptype)
- local name
- if is_plant[ptype] then
- name = df.plant_raw.find(pop.plant).id
- else
- name = df.creature_raw.find(pop.race).creature_id
- end
- local ninfo = vivify(tinfo, name)
- count_pop(ninfo, pop, div)
- local kinfo = vivify(vivify(ninfo, 'subcat'), key)
- count_pop(kinfo, pop, div)
- end
- local info = {}
- for _,r in ipairs(df.global.world.world_data.regions) do
- local rtype = df.world_region.T_type[r.type]
- for _,p in ipairs(r.population) do
- classify_population(info, p, rtype, #r.region_coords.x)
- end
- end
- for _,poptype in sort_keys(info) do
- local tinfo = info[poptype]
- print(poptype..':')
- for _,name in sort_keys(tinfo) do
- local ninfo = tinfo[name]
- print('',name..':')
- for _,key in sort_keys(ninfo.subcat) do
- print('','',key,report_pop(ninfo.subcat[key]))
- end
- print('','','TOTAL',report_pop(ninfo))
- end
- end
Add Comment
Please, Sign In to add comment