Refactor biome calls into separate file

This commit is contained in:
Hugues Ross 2020-06-10 19:18:36 -04:00
parent 3f641f6d63
commit d7f7210a2d
4 changed files with 44 additions and 39 deletions

36
biome_api.lua Normal file
View File

@ -0,0 +1,36 @@
local biome_lookup = {};
return {
-- Register a biome with textures to display
-- name: A string containing the biome name
-- textures: A table of texture names.
-- These should correspond with detail levels,
-- any detail level past the length of the table will return the last texture
-- (Optional) min_height: The minimum Y position where this biome data should be used
-- (Optional) max_height: The maximum Y position where this biome data should be used
add = function (name, textures, min_height, max_height)
biome_lookup[#biome_lookup + 1] = {
name = name,
textures = textures,
min_height = min_height,
max_height = max_height,
};
end,
-- Get the texture name (minus index/extension) for the given biome, height, and detail level.
-- name: A string containing the biome name
-- height: A number representing the Y position of the biome
-- detail: The detail level
-- Returns a string with a texture name, or nil if no matching biome entry was found.
get_texture = function (name, height, detail)
for _,biome in ipairs(biome_lookup) do
local matches_height = (not biome.min_height or height >= biome.min_height)
and (not biome.max_height or height <= biome.max_height);
if biome.name == name and matches_height then
return cartographer.detail_texture(biome.textures, detail);
end
end
return nil;
end,
};

View File

@ -43,7 +43,6 @@ local chunk = {
end
};
local biome_lookup = {};
local marker_lookup = {};
-- Includes
@ -51,11 +50,14 @@ local skin = loadfile(modpath .. "/skin_api.lua") ();
local gui = loadfile(modpath .. "/formspec.lua") ();
local audio = loadfile(modpath .. "/audio.lua") ();
local biomes = loadfile(modpath .. "/biome_api.lua") ();
local scanner = loadfile(modpath .. "/scanner.lua") (map_data, chunk);
loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner, biome_lookup, marker_lookup);
local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin);
loadfile(modpath .. "/map_api.lua") (map_data, chunk, scanner, marker_lookup);
local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin, biomes);
loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, audio, map_formspec);
loadfile(modpath .. "/commands.lua") (chunk, audio, map_formspec);
loadfile(modpath .. "/table.lua") (gui, skin, audio);
cartographer.skin = skin;
cartographer.biomes = biomes;

View File

@ -1,4 +1,4 @@
local map_data, chunk, scanner, biome_lookup, marker_lookup = ...;
local map_data, chunk, scanner, marker_lookup = ...;
local Map = {};
Map.__index = Map;
@ -143,22 +143,6 @@ local function fill_loop()
end
minetest.after(5, fill_loop);
-- Register a biome with textures to display
-- name: A string containing the biome name
-- textures: A table of texture names.
-- These should correspond with detail levels,
-- any detail level past the length of the table will return the last texture
-- (Optional) min_height: The minimum Y position where this biome data should be used
-- (Optional) max_height: The maximum Y position where this biome data should be used
function cartographer.register_biome(name, textures, min_height, max_height)
biome_lookup[#biome_lookup + 1] = {
name = name,
textures = textures,
min_height = min_height,
max_height = max_height,
};
end
-- Format marker ids to allow their use as formspec element ids.
-- We're mostly concerned with guarding against the : character because it is
-- common for ids and has an alternate meaning in formspecs.
@ -226,23 +210,6 @@ function cartographer.detail_texture(textures, detail)
return textures[math.min(detail, #textures)];
end
-- Get the texture name (minus index/extension) for the given biome, height, and detail level.
-- name: A string containing the biome name
-- height: A number representing the Y position of the biome
-- detail: The detail level
-- Returns a string with a texture name, or nil if no matching biome entry was found.
function cartographer.get_biome_texture(name, height, detail)
for _,biome in ipairs(biome_lookup) do
local matches_height = (not biome.min_height or height >= biome.min_height)
and (not biome.max_height or height <= biome.max_height);
if biome.name == name and matches_height then
return cartographer.detail_texture(biome.textures, detail);
end
end
return nil;
end
-- Get the texture name (minus extension) for the given marker and detail level.
-- id: A string containing the marker id
-- detail: The detail level

View File

@ -1,6 +1,6 @@
-- Arguments
-- map_data: The cartographer map data table
local map_data, gui, skin = ...;
local map_data, gui, skin, biomes = ...;
-- Constants
local TILE_SIZE = 0.25;
@ -75,7 +75,7 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h
else
local name = minetest.get_biome_name(column[j * map_scale].biome);
local height = column[j * map_scale].height;
local biome = cartographer.get_biome_texture(name, math.floor(height + 0.5), detail);
local biome = biomes.get_texture(name, math.floor(height + 0.5), detail);
if biome then
local depth = math.min(math.max(math.floor(height / 8), -8), 0) * -1