Optimization pass for number of tokens used - eliminate unnecessary tables, constants.
This commit is contained in:
parent
753700c144
commit
130c393c51
3 changed files with 117 additions and 89 deletions
40
mapgen.lua
40
mapgen.lua
|
@ -2,8 +2,8 @@
|
|||
|
||||
function init_mapgen()
|
||||
uid_seed = 2229 -- arbitrarily chosen number
|
||||
block_size = 64
|
||||
biome_size = 128
|
||||
-- block_size = 64
|
||||
-- biome_size = 128
|
||||
|
||||
-- Metadata for different biomes
|
||||
-- frequencies don't have to add up to 100, but they should by convention
|
||||
|
@ -29,10 +29,6 @@ function init_mapgen()
|
|||
-- so the biome_metadata array needs to have its entries appear consistently.
|
||||
biome_list = {"grassland", "forest", "desert"}
|
||||
|
||||
init_biomes()
|
||||
end
|
||||
|
||||
function init_biomes()
|
||||
-- this is the frequency list for the biomes themselves
|
||||
biome_metadata = {}
|
||||
|
||||
|
@ -66,31 +62,33 @@ end
|
|||
|
||||
-- generates a unique identifier for a position
|
||||
-- uses srand() and rand() to get an unpredictable value (is this too slow?)
|
||||
function generate_uid(pos)
|
||||
srand((pos[1] + uid_seed) * (pos[2] + (uid_seed^2)))
|
||||
return flr(rnd(-1))
|
||||
-- the two seed values are randomly chosen. Change them and change the world.
|
||||
function generate_uid(pos_x, pos_y)
|
||||
srand((pos_x + 2229) * (pos_y + 12295))
|
||||
return flr(rnd(0xffff))
|
||||
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)}
|
||||
-- biomes are currently defined to be 128x128
|
||||
function calculate_biome_pos(pos_x, pos_y)
|
||||
return flr(pos_x / 128), flr(pos_y / 128)
|
||||
end
|
||||
|
||||
-- determines which biome a given world map position should be,
|
||||
-- returns the object out of the biome_data table
|
||||
function get_biome_name(pos)
|
||||
local biome_pos = calculate_biome_pos(pos)
|
||||
local uid = generate_uid(biome_pos)
|
||||
function get_biome_name(pos_x, pos_y)
|
||||
local biome_pos_x, biome_pos_y = calculate_biome_pos(pos_x, pos_y)
|
||||
local uid = generate_uid(biome_pos_x, biome_pos_y)
|
||||
return biome_metadata[(uid % #biome_metadata) + 1]
|
||||
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)
|
||||
local biome_name = get_biome_name(pos)
|
||||
function get_tile(pos_x, pos_y)
|
||||
local biome_name = get_biome_name(pos_x, pos_y)
|
||||
local biome = biome_data[biome_name]
|
||||
local uid = generate_uid(pos)
|
||||
local uid = generate_uid(pos_x, pos_y)
|
||||
|
||||
return biome["tile_lookup"][(uid % #biome["tile_lookup"]) + 1]
|
||||
end
|
||||
|
@ -100,10 +98,10 @@ end
|
|||
-- writes block_size x block_size tiles
|
||||
-- after a call to generate_map you should always center the camera/player over the map, i.e.
|
||||
-- camera at { (block_size / 2) - 8, (block_size / 2) - 8 }
|
||||
function generate_map(start)
|
||||
for x=0,block_size-1 do
|
||||
for y=0,block_size-1 do
|
||||
mset(x, y, get_tile({start[1]+x, start[2]+y}))
|
||||
function generate_map(start_x, start_y)
|
||||
for x=0,63 do
|
||||
for y=0,63 do
|
||||
mset(x, y, get_tile(start_x+x, start_y+y))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue