apemanzilla

beebreed.lua

Oct 21st, 2016
93
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function copy(tbl)
  2.     local output = {}
  3.     for k, v in pairs(tbl) do
  4.         if type(v) == "table" then
  5.             if v == tbl then
  6.                 output[k] = output
  7.             else
  8.                 output[k] = copy(v)
  9.             end
  10.         else
  11.             output[k] = v
  12.         end
  13.     end
  14.     return output
  15. end
  16.  
  17. local f = fs.open("bee.dat", "r")
  18. local d = f.readAll()
  19. f.close()
  20.  
  21. local raw = textutils.unserialize(d)
  22.  
  23. local allSpecies = {}
  24. for k, v in pairs(raw) do
  25.     allSpecies[v.allele1] = true
  26.     allSpecies[v.allele2] = true
  27.     allSpecies[v.result] = true
  28. end
  29.  
  30. local iallSpecies = {}
  31. for k, v in pairs(allSpecies) do
  32.     table.insert(iallSpecies, k)
  33. end
  34.  
  35. print("Total species: " .. #iallSpecies)
  36.  
  37. local unbreedable = copy(allSpecies)
  38. for k, v in pairs(raw) do
  39.     unbreedable[v.result] = nil
  40. end
  41.  
  42. local iunbreedable = {}
  43. for k, v in pairs(unbreedable) do
  44.     table.insert(iunbreedable, k)
  45. end
  46.  
  47. print("Unbreedable: " .. #iunbreedable)
  48.  
  49. print("Building tree...")
  50. local tree = {}
  51.  
  52. -- initial pass
  53. for k, v in pairs(raw) do
  54.     local data = {v.allele1, v.allele2, v.chance}
  55.     local res = v.result
  56.     if not tree[res] then tree[res] = {} end
  57.     table.insert(tree[res], data)
  58. end
  59.  
  60. local f = fs.open("out.dat", "w")
  61. f.write(textutils.serialize(tree))
  62. f.close()
  63.  
  64. local available = {
  65.     Meadows=true,
  66.     Forest=true,
  67. }
  68.  
  69. local function breedable(available)
  70.     local out = copy(available)
  71.     for species, combos in pairs(tree) do
  72.         for i, combo in ipairs(combos) do
  73.             if available[combo[1]] and available[combo[2]] then
  74.                 if out[species] then
  75.                     if out[species][3] < combo[3] then
  76.                         out[species] = combo
  77.                     end
  78.                 else
  79.                     out[species] = combo
  80.                 end
  81.             end
  82.         end
  83.     end
  84.  
  85.     local recurse = false
  86.  
  87.     for k, v in pairs(out) do
  88.         if not available[k] then recurse = true break end
  89.     end
  90.  
  91.     if recurse then out = breedable(out) end
  92.  
  93.     return out
  94. end
  95.  
  96. --print(textutils.serialize(breedable(available)))
  97. return breedable
RAW Paste Data