diff --git a/mapgen.lua b/mapgen.lua index 51cad83..6578c61 100644 --- a/mapgen.lua +++ b/mapgen.lua @@ -36,6 +36,12 @@ function init_mapgen() } } + -- Why is this hard-coded separately from the biome_data? glad you asked. + -- Lua's pairs() function appears not to guarantee a consistent return order, + -- and we want our world to be deterministically generated, + -- so the biome_metadata array needs to have its entries appear consistently. + biome_list = {"grassland", "forest", "desert"} + init_biomes() end @@ -43,13 +49,15 @@ function init_biomes() -- this is the frequency list for the biomes themselves biome_metadata = {} - for k,v in pairs(biome_data) do + for i=1,#biome_list do + local biome = biome_list[i] + -- add the biome's name N times to the biome metadata 'hat' - for i=1,v["biome_frequency"] do - add(biome_metadata, k) + for i=1,biome_data[biome]["biome_frequency"] do + add(biome_metadata, biome) end - build_biome(k, v) + build_biome(biome, biome_data[biome]) end end @@ -76,10 +84,16 @@ function generate_uid(pos) return flr(rnd(-1)) end +-- given an {x,y} position, calculates the aligned starting position for the +-- biome that position is in. +function calculate_biome_pos(pos) + return {flr(pos[1] / biome_size), flr(pos[2] / biome_size)} +end + -- determines which biome a given world map position should be, -- returns the object out of the biome_data table function get_biome(pos) - local biome_pos = {flr(pos[1] / biome_size), flr(pos[2] / biome_size)} + local biome_pos = calculate_biome_pos(pos) local uid = generate_uid(biome_pos) local biome_name = biome_metadata[(uid % #biome_metadata) + 1] return biome_data[biome_name]