From 0b10b2df674c804295c602074fe2f8a1071cdb39 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sat, 7 Dec 2019 16:30:41 -0500 Subject: [PATCH] Implement multiple biomes. --- infimap.p8 | 18 ++++++------ mapgen.lua | 85 ++++++++++++++++++++++++++++++++++++++++-------------- player.lua | 13 ++------- 3 files changed, 74 insertions(+), 42 deletions(-) diff --git a/infimap.p8 b/infimap.p8 index 521e283..cb9c953 100644 --- a/infimap.p8 +++ b/infimap.p8 @@ -5,14 +5,14 @@ __lua__ #include player.lua #include main.lua __gfx__ -000000000000000033333333335555333344443338333333333333e3383333830000000000000000000000000000000000000000000000000000000000000000 -00000000000000003333333335554553344444438a833383333e3e9e888338880000000000000000000000000000000000000000000000000000000000000000 -0070070000000000333333335555555534444443383338a833e9e3e3373333730000000000000000000000000000000000000000000000000000000000000000 -000770000000000033333333554554553344443333333383333e3333333333330000000000000000000000000000000000000000000000000000000000000000 -00077000000000003333333355555555333553333333833333333333333338330000000000000000000000000000000000000000000000000000000000000000 -00700700000000003333333355545555333553333338a8333e333e33333388830000000000000000000000000000000000000000000000000000000000000000 -000000000000000033333333355555533335533333338333e9e3e9e3333337330000000000000000000000000000000000000000000000000000000000000000 -0000000000000000333333333355553333555533333333333e333e33333333330000000000000000000000000000000000000000000000000000000000000000 +000000000000000033333333335555333344443338333333333333e3383333833333333399999999999899999994999499999999999999990000000000000000 +00000000000000003333333335554553344444438a833383333e3e9e888338883333333399999999998a89999499949999999999999999990000000000000000 +0070070000000000333333335555555534444443383338a833e9e3e33733337333333333999999999998b9b99949994999944999999bb9b90000000000000000 +000770000000000033333333554554553344443333333383333e33333333333333378333999999999b9bb9b999994999994444999b9bb9b90000000000000000 +000770000000000033333333555555553335533333338333333333333333383333888733999999999b9bbbb999499994944444499b9bbbb90000000000000000 +00700700000000003333333355545555333553333338a8333e333e333333888338788883999999999bbbb99999994999944444499bbbb9990000000000000000 +000000000000000033333333355555533335533333338333e9e3e9e3333337333337733399999999999bb9999499994999444499999bb9990000000000000000 +0000000000000000333333333355553333555533333333333e333e33333333333337733399999999999bb9999999999999944999999bb9990000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 @@ -70,5 +70,5 @@ __gfx__ 00000000000000000000000000000000000000000000000000000000000000000000000000700700007000000000070000000000000000007000000000000007 00000000000000000000000000000000000000000000000000000000000000000000000000077000000700000000700000000000000000000777700000077770 __gff__ -0000000303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +0000000303000000020003000103000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 diff --git a/mapgen.lua b/mapgen.lua index 8b2d807..51cad83 100644 --- a/mapgen.lua +++ b/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, diff --git a/player.lua b/player.lua index 1f93fc5..837f85f 100644 --- a/player.lua +++ b/player.lua @@ -1,5 +1,5 @@ --- this module assumes all objects and the player are --- 1x1 sprite in size. larger sprites are not currently supported +-- This module assumes all objects and the player are 1x1 sprite in size. +-- Larger sprites will probably work for objects, but not for the player. -- -- exported variables: -- player_pos # integer tuple {x,y}. absolute position of the player on the @@ -10,10 +10,6 @@ -- # each element can be -1 (up or left), 0 (neutral), -- # or 1 (down or right) -- --- exported constants: --- player_lookup # used internally to look up the sprite to render for a --- # given facing --- -- functions: -- init_player({x,y}) # call this in _init(). Sets player starting position, -- # assumes the top-left of the pico-8 map is currently @@ -23,14 +19,9 @@ -- draw_player() # call in _draw(). Draws the player sprite. function init_player(start_pos) - -- camera_pos is the relative coordinate of the camera - -- on the currently generated map chunk. player_pos is - -- the *absolute* world coordinates of the player. player_pos = start_pos camera_pos = {start_pos[1]-8, start_pos[2]-8} - -- [1] is vertical facing, [2] is horizontal, -1 is up/left, 0 is neutral, - -- and 1 is down/right facing = {1, 0} -- this is a constant for looking up player sprites by facing