Implement multiple biomes.
This commit is contained in:
parent
a8b26c113d
commit
0b10b2df67
3 changed files with 74 additions and 42 deletions
85
mapgen.lua
85
mapgen.lua
|
@ -3,8 +3,9 @@
|
|||
function init_mapgen()
|
||||
uid_seed = 2229 -- arbitrarily chosen number
|
||||
block_size = 64
|
||||
biome_size = 128
|
||||
|
||||
-- Sprite ratios for different biomes, of the form { "biome_name": {frequency, sprite index} ... } }
|
||||
-- Metadata for different biomes
|
||||
-- frequencies don't have to add up to 100, but they should by convention
|
||||
--
|
||||
-- Sprites are:
|
||||
|
@ -14,31 +15,58 @@ function init_mapgen()
|
|||
-- * 5 - red flowers
|
||||
-- * 6 - pink flowers
|
||||
-- * 7 - mushrooms
|
||||
biomes = { grassland = { {40, 2}, {28, 5}, {28, 6}, {3, 3}, {1, 4} },
|
||||
forest = { {55, 2}, {25, 4}, {5, 3}, {5, 5}, {5, 7}, {5, 6} },
|
||||
}
|
||||
|
||||
build_biome("forest")
|
||||
-- * 8 - big mushroom
|
||||
-- * 9 - sand
|
||||
-- * 10 - cactus with flower
|
||||
-- * 11 - pebbles
|
||||
-- * 12 - rock
|
||||
-- * 13 - cactus
|
||||
biome_data = {
|
||||
grassland = {
|
||||
biome_frequency = 75,
|
||||
tile_frequencies = { {40, 2}, {28, 5}, {28, 6}, {3, 3}, {1, 4} }
|
||||
},
|
||||
forest = {
|
||||
biome_frequency = 20,
|
||||
tile_frequencies = { {60, 2}, {20, 4}, {5, 3}, {5, 5}, {4, 7}, {5, 6}, {1, 8} }
|
||||
},
|
||||
desert = {
|
||||
biome_frequency = 5,
|
||||
tile_frequencies = { {80, 9}, {10, 11}, {6, 13}, {3, 12}, {1, 10} },
|
||||
}
|
||||
}
|
||||
|
||||
init_biomes()
|
||||
end
|
||||
|
||||
-- use biome1 if mix == false
|
||||
function build_biome(biome1, biome2, mix)
|
||||
if mix == true then
|
||||
else
|
||||
frequencies = biomes[biome1]
|
||||
end
|
||||
function init_biomes()
|
||||
-- this is the frequency list for the biomes themselves
|
||||
biome_metadata = {}
|
||||
|
||||
-- global variable used for lookup during mapgen
|
||||
-- build_biome() writes the biome_lookup table such
|
||||
-- that biome_lookup[uid] returns a sprite index,
|
||||
-- weighted by the frequency information for current biome.
|
||||
biome_lookup = {}
|
||||
for i=1,#frequencies do
|
||||
local tuple=frequencies[i]
|
||||
for k,v in pairs(biome_data) do
|
||||
-- add the biome's name N times to the biome metadata 'hat'
|
||||
for i=1,v["biome_frequency"] do
|
||||
add(biome_metadata, k)
|
||||
end
|
||||
|
||||
build_biome(k, v)
|
||||
end
|
||||
end
|
||||
|
||||
-- build the lookup table for a given biome, based on the biome_meta data for
|
||||
-- that string.
|
||||
function build_biome(biome_name, data)
|
||||
local meta_frequencies = data["tile_frequencies"]
|
||||
local tile_lookup = {}
|
||||
|
||||
for i=1,#meta_frequencies do
|
||||
local tuple = meta_frequencies[i]
|
||||
for j=1,tuple[1] do
|
||||
add(biome_lookup, tuple[2])
|
||||
add(tile_lookup, tuple[2])
|
||||
end
|
||||
end
|
||||
|
||||
data["tile_lookup"] = tile_lookup
|
||||
end
|
||||
|
||||
-- generates a unique identifier for a position
|
||||
|
@ -48,9 +76,22 @@ function generate_uid(pos)
|
|||
return flr(rnd(-1))
|
||||
end
|
||||
|
||||
-- determine what sprite to render for a given position
|
||||
-- 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 uid = generate_uid(biome_pos)
|
||||
local biome_name = biome_metadata[(uid % #biome_metadata) + 1]
|
||||
return biome_data[biome_name]
|
||||
end
|
||||
|
||||
-- determine what sprite to render for a given position.
|
||||
-- todo: this needs the ability to have a list of 'changed' tiles to check against.
|
||||
function get_tile(pos)
|
||||
return biome_lookup[(generate_uid(pos) % #biome_lookup) + 1]
|
||||
local biome = get_biome(pos)
|
||||
local uid = generate_uid(pos)
|
||||
|
||||
return biome["tile_lookup"][(uid % #biome["tile_lookup"]) + 1]
|
||||
end
|
||||
|
||||
-- generate the map and writes to the map area from 0 - block_size,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue