Guest User

Untitled

a guest
Jan 19th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1.  
  2. local is_plant = { Tree = true, Grass = true, Bush = true }
  3.  
  4. local function vivify(map,key)
  5.     if not map[key] then map[key] = {} end
  6.     return map[key]
  7. end
  8.  
  9. local function count_pop(ninfo, pop, div)
  10.     ninfo.count = (ninfo.count or 0) + 1
  11.     if pop.count_min ~= 10000001 then
  12.         ninfo.min_sum = (ninfo.min_sum or 0) + pop.count_min
  13.         ninfo.min_sum_d = (ninfo.min_sum_d or 0) + pop.count_min/div
  14.     else
  15.         ninfo.min_inf = (ninfo.min_inf or 0) + 1
  16.     end
  17.     if pop.count_max ~= 10000001 then
  18.         ninfo.max_sum = (ninfo.max_sum or 0) + pop.count_max
  19.         ninfo.max_sum_d = (ninfo.max_sum_d or 0) + pop.count_max/div
  20.     else
  21.         ninfo.max_inf = (ninfo.max_inf or 0) + 1
  22.     end
  23. end
  24.  
  25. local function report_pop(ninfo)
  26.     return string.format(
  27.         "count %d, sum %d, avg %f (%f), max sum %d, avg %f (%f)",
  28.         (ninfo.count or 0),
  29.         (ninfo.min_sum or 0),
  30.         ((ninfo.min_sum or 0) / ((ninfo.count or 1) - (ninfo.min_inf or 0))),
  31.         ((ninfo.min_sum_d or 0) / ((ninfo.count or 1) - (ninfo.min_inf or 0))),
  32.         (ninfo.max_sum or 0),
  33.         ((ninfo.max_sum or 0) / ((ninfo.count or 1) - (ninfo.max_inf or 0))),
  34.         ((ninfo.max_sum_d or 0) / ((ninfo.count or 1) - (ninfo.max_inf or 0)))
  35.     )
  36. end
  37.  
  38. local function sort_keys(tab)
  39.     local kt = {}
  40.     for k,v in pairs(tab) do table.insert(kt,k) end
  41.     table.sort(kt)
  42.     return ipairs(kt)
  43. end
  44.  
  45. function classify_population(info, pop, key, div)
  46.     local ptype = df.world_population_type[pop.type]
  47.     local tinfo = vivify(info, ptype)
  48.     local name
  49.     if is_plant[ptype] then
  50.         name = df.plant_raw.find(pop.plant).id
  51.     else
  52.         name = df.creature_raw.find(pop.race).creature_id
  53.     end
  54.     local ninfo = vivify(tinfo, name)
  55.     count_pop(ninfo, pop, div)
  56.     local kinfo = vivify(vivify(ninfo, 'subcat'), key)
  57.     count_pop(kinfo, pop, div)
  58. end
  59.  
  60. local info = {}
  61.  
  62. for _,r in ipairs(df.global.world.world_data.regions) do
  63.     local rtype = df.world_region.T_type[r.type]
  64.     for _,p in ipairs(r.population) do
  65.         classify_population(info, p, rtype, #r.region_coords.x)
  66.     end
  67. end
  68.  
  69. for _,poptype in sort_keys(info) do
  70.     local tinfo = info[poptype]
  71.     print(poptype..':')
  72.     for _,name in sort_keys(tinfo) do
  73.         local ninfo = tinfo[name]
  74.         print('',name..':')
  75.         for _,key in sort_keys(ninfo.subcat) do
  76.             print('','',key,report_pop(ninfo.subcat[key]))
  77.         end
  78.         print('','','TOTAL',report_pop(ninfo))
  79.     end
  80. end
Add Comment
Please, Sign In to add comment