Skip to content

Instantly share code, notes, and snippets.

@keith-kurak
Created October 30, 2022 17:43
Show Gist options
  • Save keith-kurak/59113e005d2540495e584dd865afc4c4 to your computer and use it in GitHub Desktop.
Save keith-kurak/59113e005d2540495e584dd865afc4c4 to your computer and use it in GitHub Desktop.
PICO-03: map generation
-- ** this part generates a simple map, generating more each time you reach the end of the memory map **
-- works around the overlap when resetting position by making the overlap area the same
-- 1) add map generation fn to tab 2
function gen_map()
--clear
for i=0,127 do
for j=0,15 do
mset(i,j,3)
end
end
-- set flat space at begin/ end
for i=0,15 do
mset(i,15,1)
end
for i=112,127 do
mset(i,15,1)
end
-- set variable space
for i=16,111 do
for j=0,rnd(6)-2 do
-- negative nums make no floors
mset(i,15-j,1)
end
end
end
-- 2) call gen_map() in _init()
-- just add this to the end
gen_map()
-- 3) conditionally call gen_map in _update60()
-- just add the below to the very end of the fn
-- reset to beginning
-- generate more map
if p.x >= (127-12)*8 then
p.x = 24
gen_map()
end
-- ** here we'll fix the overlap by regenerating just the overlapping area once it's offscreen **
-- 1) refactor tab 2's map generation into separate functions for generating height, setting x coord, and setting main and "missing" spaces
function gen_map(init)
-- set flat space at begin
if (init) then
for i=0,15 do
set_map_x(1,i)
end
end
-- set variable space
for i=16,111 do
local flr_hgt = gen_floor_height()
set_map_x(flr_hgt,i)
end
end
function gen_missing_map()
for i=0,15 do
local flr_hgt = gen_floor_height()
set_map_x(flr_hgt,i)
set_map_x(flr_hgt,i+112)
end
end
function gen_floor_height()
-- negative = no floor
return rnd(6)-2
end
-- set a single space on map
function set_map_x(flr_hgt,x)
-- clear
for j=0,15 do
mset(x,j,3)
end
for j=0,flr_hgt do
-- negative nums make no floors
mset(x,15-j,1)
end
end
-- 2) pass true to gen_map in init so it generates the initial space
gen_map(true)
-- 3) add needs_missing_map global variable to ensure we only generate missing map once per run
needs_missing_map = true
-- 4) update _update60() to generate "main" and "missing" map
-- reset to beginning
-- generate more map
if p.x >= (127-12)*8 then
p.x = 24
needs_missing_map = true
gen_map()
end
if needs_missing_map and p.x >= (36-12)*8 then
gen_missing_map()
needs_missing_map = false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment