Optimization pass for number of tokens used - eliminate unnecessary tables, constants.

This commit is contained in:
Anna Rose 2019-12-09 16:35:45 -05:00
parent 753700c144
commit 130c393c51
3 changed files with 117 additions and 89 deletions

View File

@ -1,8 +1,8 @@
function _init() function _init()
init_sound() init_sound()
init_mapgen() init_mapgen()
init_player({block_size/2, block_size/2}) init_player(32, 32)
generate_map({0, 0}) generate_map(0, 0)
debug = false debug = false
end end
@ -10,13 +10,13 @@ end
function _update() function _update()
handle_input() handle_input()
handle_map_update() handle_map_update()
if (btn(5) and btn(4) and btnp(3)) debug = not debug debug_input()
end end
function _draw() function _draw()
rectfill(0, 0, 127, 127, 0) rectfill(0, 0, 127, 127, 0)
-- the screen is 128x128 pixels, so it fits 16x16 sprites -- the screen is 128x128 pixels, so it fits 16x16 sprites
map(camera_pos[1], camera_pos[2], 0, 0, 16, 16) map(camera_pos_x, camera_pos_y, 0, 0, 16, 16)
draw_player() draw_player()
if debug then if debug then
render_debug_info() render_debug_info()
@ -26,25 +26,55 @@ end
-- decide whether we need to regenerate the map. -- decide whether we need to regenerate the map.
-- if so, call generate_map appropriately and reset coordinates. -- if so, call generate_map appropriately and reset coordinates.
function handle_map_update() function handle_map_update()
if out_of_bounds(camera_pos) then -- out_of_bounds() checks all screen bounds if out_of_bounds(camera_pos_x, camera_pos_y) then
-- out_of_bounds() checks all screen bounds
-- we need to regenerate the map, so we generate a map chunk that -- we need to regenerate the map, so we generate a map chunk that
-- places the player in the middle of it. -- places the player in the middle of it.
generate_map({player_pos[1]-block_size/2, player_pos[2]-block_size/2}) generate_map(player_pos_x-32, player_pos_y-32)
camera_pos = {block_size/2 - 8, block_size/2 - 8} camera_pos_x, camera_pos_y = 24
end end
end end
function out_of_bounds(pos) function out_of_bounds(pos_x, pos_y)
return pos[1] < 0 or pos[1] > block_size-16 or pos[2] < 0 or pos[2] > block_size-16 return pos_x < 0 or pos_x > 48 or
pos_y < 0 or pos_y > 48
end
function debug_input()
if btn"5" and btn"4" then
if (btnp"0") debug = "res"
if (btnp"1") debug = "sfx"
if (btnp"2") debug = "map"
if (btnp"3") debug = nil
end
end end
function render_debug_info() function render_debug_info()
clip(0, 0, 32, 24) if debug == "res" then
rectfill(0, 0, 32, 24, 0) clip(0, 0, 32, 24)
print(stat(0), 0, 0, 15) rectfill(0, 0, 32, 24, 0)
print(camera_pos[1], 0, 8, 15)
print(camera_pos[2], 16, 8, 15) print(stat(0), 0, 0, 15)
print(player_pos[1], 0, 16, 15) print(stat(1), 0, 8, 15)
print(player_pos[2], 16, 16, 15) print(stat(9), 0, 16, 15)
print("/", 12, 16, 15)
print(stat(8), 20, 16, 15)
elseif debug == "sfx" then
clip(0, 0, 16, 8)
rectfill(0, 0, 16, 8, 0)
print(stat(24), 0, 0, 15)
elseif debug == "map" then
clip(0, 0, 32, 24)
rectfill(0, 0, 32, 24, 0)
print(camera_pos_x, 0, 0, 15)
print(camera_pos_y, 16, 0, 15)
print(player_pos_x, 0, 8, 15)
print(player_pos_y, 16, 8, 15)
print(get_biome_name(player_pos_x, player_pos_y), 0, 16, 15)
end
clip() clip()
end end

View File

@ -2,8 +2,8 @@
function init_mapgen() function init_mapgen()
uid_seed = 2229 -- arbitrarily chosen number uid_seed = 2229 -- arbitrarily chosen number
block_size = 64 -- block_size = 64
biome_size = 128 -- biome_size = 128
-- Metadata for different biomes -- Metadata for different biomes
-- frequencies don't have to add up to 100, but they should by convention -- 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. -- so the biome_metadata array needs to have its entries appear consistently.
biome_list = {"grassland", "forest", "desert"} biome_list = {"grassland", "forest", "desert"}
init_biomes()
end
function init_biomes()
-- this is the frequency list for the biomes themselves -- this is the frequency list for the biomes themselves
biome_metadata = {} biome_metadata = {}
@ -66,31 +62,33 @@ end
-- generates a unique identifier for a position -- generates a unique identifier for a position
-- uses srand() and rand() to get an unpredictable value (is this too slow?) -- uses srand() and rand() to get an unpredictable value (is this too slow?)
function generate_uid(pos) -- the two seed values are randomly chosen. Change them and change the world.
srand((pos[1] + uid_seed) * (pos[2] + (uid_seed^2))) function generate_uid(pos_x, pos_y)
return flr(rnd(-1)) srand((pos_x + 2229) * (pos_y + 12295))
return flr(rnd(0xffff))
end end
-- given an {x,y} position, calculates the aligned starting position for the -- given an {x,y} position, calculates the aligned starting position for the
-- biome that position is in. -- biome that position is in.
function calculate_biome_pos(pos) -- biomes are currently defined to be 128x128
return {flr(pos[1] / biome_size), flr(pos[2] / biome_size)} function calculate_biome_pos(pos_x, pos_y)
return flr(pos_x / 128), flr(pos_y / 128)
end end
-- determines which biome a given world map position should be, -- determines which biome a given world map position should be,
-- returns the object out of the biome_data table -- returns the object out of the biome_data table
function get_biome_name(pos) function get_biome_name(pos_x, pos_y)
local biome_pos = calculate_biome_pos(pos) local biome_pos_x, biome_pos_y = calculate_biome_pos(pos_x, pos_y)
local uid = generate_uid(biome_pos) local uid = generate_uid(biome_pos_x, biome_pos_y)
return biome_metadata[(uid % #biome_metadata) + 1] return biome_metadata[(uid % #biome_metadata) + 1]
end end
-- determine what sprite to render for a given position. -- determine what sprite to render for a given position.
-- todo: this needs the ability to have a list of 'changed' tiles to check against. -- todo: this needs the ability to have a list of 'changed' tiles to check against.
function get_tile(pos) function get_tile(pos_x, pos_y)
local biome_name = get_biome_name(pos) local biome_name = get_biome_name(pos_x, pos_y)
local biome = biome_data[biome_name] 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] return biome["tile_lookup"][(uid % #biome["tile_lookup"]) + 1]
end end
@ -100,10 +98,10 @@ end
-- writes block_size x block_size tiles -- 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. -- 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 } -- camera at { (block_size / 2) - 8, (block_size / 2) - 8 }
function generate_map(start) function generate_map(start_x, start_y)
for x=0,block_size-1 do for x=0,63 do
for y=0,block_size-1 do for y=0,63 do
mset(x, y, get_tile({start[1]+x, start[2]+y})) mset(x, y, get_tile(start_x+x, start_y+y))
end end
end end
end end

View File

@ -2,43 +2,43 @@
-- Larger sprites will probably work for objects, but not for the player. -- Larger sprites will probably work for objects, but not for the player.
-- --
-- exported variables: -- exported variables:
-- player_pos # integer tuple {x,y}. absolute position of the player on the -- player_pos_[xy] # absolute position of the player on the 'world map'
-- # 'world map' -- camera_pos_[xy] # Represents top-left position of the pico-8 camera on
-- camera_pos # integer tuple {x,y}. Represents top-left position of the -- # the pico-8 map
-- # pico-8 camera on the pico-8 map -- facing_[vh] # Current facing of the player. Values of
-- facing # integer tuple {v,h}. Current facing of the player. Values of -- # each element can be -1 (up or left), 0 (neutral),
-- # each element can be -1 (up or left), 0 (neutral), -- # or 1 (down or right)
-- # or 1 (down or right)
-- --
-- functions: -- functions:
-- init_player({x,y}) # call this in _init(). Sets player starting position, -- init_player(x,y) # call this in _init(). Sets player starting position,
-- # assumes the top-left of the pico-8 map is currently -- # assumes the top-left of the pico-8 map is currently
-- # world position 0,0 -- # world position 0,0
-- handle_input() # call in _update() to handle movement key presses. -- handle_input() # call in _update() to handle movement key presses.
-- # Assumes any sprite with flag 0 set is an obstacle. -- # Assumes any sprite with flag 0 set is an obstacle.
-- draw_player() # call in _draw(). Draws the player sprite. -- draw_player() # call in _draw(). Draws the player sprite.
function init_player(start_pos) function init_player(start_pos_x, start_pos_y)
player_pos = start_pos player_pos_x, player_pos_y = start_pos_x, start_pos_y
camera_pos = {start_pos[1]-8, start_pos[2]-8} camera_pos_x, camera_pos_y = start_pos_x-8, start_pos_y-8
facing = {1, 0} facing_v = 1
facing_h = 0
-- this is a constant for looking up player sprites by facing -- this is a constant for looking up player sprites by facing
player_lookup = { player_lookup = {
[0] = { [0] = {
[0] = -1, -- error state [0] = 0xffff, -- error state
[-1] = 122, -- left [0xffff] = 122, -- left
[1] = 123, -- right [1] = 123, -- right
}, },
[-1] = { [0xffff] = {
[0] = 120, -- up [0] = 120, -- up
[-1] = 124, -- up-left [0xffff] = 124, -- up-left
[1] = 125, -- up-right [1] = 125, -- up-right
}, },
[1] = { [1] = {
[0] = 121, -- down [0] = 121, -- down
[-1] = 126, -- down-left [0xffff] = 126, -- down-left
[1] = 127, -- down-right [1] = 127, -- down-right
}, },
} }
@ -46,51 +46,51 @@ end
function handle_input() function handle_input()
local new_pos = {camera_pos[1], camera_pos[2]} local new_pos_x, new_pos_y = camera_pos_x, camera_pos_y
local new_ppos = {player_pos[1], player_pos[2]} local new_ppos_x, new_ppos_y = player_pos_x, player_pos_y
if btnp(0) or btnp(1) or btnp(2) or btnp(3) then if btnp"0" or btnp"1" or btnp"2" or btnp"3" then
if btnp(0) then if btnp"0" then
new_pos[1] -= 1 -- move left new_pos_x -= 1 -- move left
new_ppos[1] -= 1 new_ppos_x -= 1
facing[2] = -1 facing_h = 0xffff
end end
if btnp(1) then if btnp"1" then
new_pos[1] += 1 -- move right new_pos_x += 1 -- move right
new_ppos[1] += 1 -- move right new_ppos_x += 1 -- move right
facing[2] = 1 facing_h = 1
end end
if not (btnp(0) or btnp(1)) then if not (btnp"0" or btnp"1") then
facing[2] = 0 facing_h = 0
end end
if btnp(2) then if btnp"2" then
new_pos[2] -= 1 -- move up new_pos_y -= 1 -- move up
new_ppos[2] -= 1 -- move up new_ppos_y -= 1 -- move up
facing[1] = -1 facing_v = 0xffff
end end
if btnp(3) then if btnp"3" then
new_pos[2] += 1 -- move down new_pos_y += 1 -- move down
new_ppos[2] += 1 -- move down new_ppos_y += 1 -- move down
facing[1] = 1 facing_v = 1
end end
if not (btnp(2) or btnp(3)) then if not (btnp"2" or btnp"3") then
facing[1] = 0 facing_v = 0
end end
end end
if _legal_move(new_pos) then if legal_move(new_pos_x, new_pos_y) then
camera_pos = new_pos camera_pos_x, camera_pos_y = new_pos_x, new_pos_y
player_pos = new_ppos player_pos_x, player_pos_y = new_ppos_x, new_ppos_y
end end
end end
function draw_player() function draw_player()
spr(player_lookup[facing[1]][facing[2]], 64, 64) spr(player_lookup[facing_v][facing_h], 64, 64)
-- todo: animate the character on move -- todo: animate the character on move
-- draw the player's HUD -- draw the player's HUD
if fget(mget(player_pos[1], player_pos[2]), 1) then if fget(mget(player_pos_x, player_pos_y), 1) then
-- flag 1 represents an interactable (read: destructible) sprite. -- flag 1 represents an interactable (read: destructible) sprite.
-- need a map of destructible map objects, appropriate sfx, replacement -- need a map of destructible map objects, appropriate sfx, replacement
-- sprites. -- sprites.
@ -101,6 +101,6 @@ end
-- pos is camera position, meaning the map-relative player -- pos is camera position, meaning the map-relative player
-- position is pos + {8, 8}. -- position is pos + {8, 8}.
function _legal_move(pos) function legal_move(pos_x, pos_y)
return not fget(mget(pos[1]+8, pos[2]+8), 0) return not fget(mget(pos_x+8, pos_y+8), 0)
end end