diff --git a/commands.lua b/commands.lua index f446304..0cb90b9 100644 --- a/commands.lua +++ b/commands.lua @@ -1,3 +1,5 @@ +local map_formspec = ...; + local MAXINT = 2147483647; -- /map -- Displays a regional map around the player @@ -27,7 +29,7 @@ minetest.register_chatcommand("map", { end cartographer.map_sound("cartographer_open_map", player); - minetest.show_formspec(name, "map", cartographer.get_map_formspec(math.floor((player_x / scale) + 0.5), math.floor((player_z / scale) + 0.5), 40, 40, detail, scale, true)); + minetest.show_formspec(name, "map", map_formspec.from_coords(math.floor((player_x / scale) + 0.5), math.floor((player_z / scale) + 0.5), 40, 40, detail, scale, true)); end, }) diff --git a/init.lua b/init.lua index a85e737..0005907 100644 --- a/init.lua +++ b/init.lua @@ -59,7 +59,7 @@ cartographer.skin = skin; loadfile(modpath .. "/scanner.lua") (map_data, chunk); loadfile(modpath .. "/map_api.lua") (chunk, biome_lookup, marker_lookup); -loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin); -loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin); -loadfile(modpath .. "/commands.lua") (); +local map_formspec = loadfile(modpath .. "/map_formspec.lua") (map_data, gui, skin); +loadfile(modpath .. "/items.lua") (chunk, marker_lookup, gui, skin, map_formspec); +loadfile(modpath .. "/commands.lua") (map_formspec); loadfile(modpath .. "/table.lua") (_cartographer.materials_by_name, _cartographer.materials_by_group, gui, skin); diff --git a/items.lua b/items.lua index f155273..bc49dea 100644 --- a/items.lua +++ b/items.lua @@ -1,4 +1,4 @@ -local chunk, marker_lookup, gui, skin = ... +local chunk, marker_lookup, gui, skin, map_formspec = ...; -- The list of players looking at maps, and the map IDs that they're looking at local player_maps = {}; @@ -126,7 +126,7 @@ local function show_map_id_formspec(id, player_x, player_z, player_name, height_ local map = cartographer.get_map(id); player_x, player_z = cartographer.to_map_coordinates(map, player_x, player_z) - local formspec, formspec_width, _ = cartographer.get_map_formspec_map(map, player_x, player_z, height_mode); + local formspec, formspec_width, _ = map_formspec.from_map(map, player_x, player_z, height_mode); if #marker_lookup > 0 then local height_button_texture; if height_mode then diff --git a/map_formspec.lua b/map_formspec.lua index 0a7d66f..2a6048d 100644 --- a/map_formspec.lua +++ b/map_formspec.lua @@ -19,28 +19,14 @@ local MAP_NOISE = { }; -- Get the variant of the tile at a given position +-- prefix: The part of the tile texture name before the variant -- x: The X position of the tile (in map coordinates) -- z: The Z position of the tile (in map coordinates) -- noise: A 2d lookup table of perlin noise. Must contain the position [x + 1][y + 1] -- --- Returns a number from 1 to 4 -local function get_variant(x, z, noise) - return math.floor(math.min(noise[x + 1][z + 1] * 3, 3)) + 1; -end - --- Generate formspec markup for an unknown biome tile --- x: The x position of the tile --- y: The y position of the tile --- detail: The detail level --- variant: The tile variant --- --- Returns a formspec string -local function unknown_biome_tile(x, y, detail, variant) - return string.format("image[%f,%f;%f,%f;%s.%s.png]", - x, y, - TILE_SIZE, TILE_SIZE, - cartographer.detail_texture(cartographer.skin.unknown_biome_textures, detail), - variant); +-- Returns a string in the format 'prefix.variant.png', where variant is a number from 1 to 4 +local function get_variant(prefix, x, z, noise) + return string.format("%s.%d.png", prefix, math.floor(math.min(noise[x + 1][z + 1] * 3, 3)) + 1); end -- Generate formspec markup for a given map @@ -55,10 +41,11 @@ end -- height_mode: If true, displaces tiles by their height -- (Optional) is_visible: Callback to determine if a tile should be drawn -- (Optional) get_marker: Callback to get the marker for any given tile --- (Optional) user: userdata passed to the is_visible/get_marker callbacks +-- +-- Additional arguments are provided to is_visible / get_marker -- -- Returns a formspec string -local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, height_mode, is_visible, get_marker, user) +local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, height_mode, is_visible, get_marker, ...) local str = ""; local noise = PerlinNoiseMap(MAP_NOISE, { x=w + 1, y=h + 1, z=1}):get_2d_map({ x=x, y=y}); @@ -67,8 +54,16 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h local column = map_data.generated[i * map_scale]; for j = y + h,y,-1 do local fy = (y + h - j) * TILE_OFFSET; - if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(user, i, j)) then - str = str .. unknown_biome_tile(fx, fy, detail, get_variant(i - x, j - y, noise)); + if column == nil or column[j * map_scale] == nil or (is_visible and not is_visible(..., i, j)) then + local unknown_tex = cartographer.detail_texture(skin.unknown_biome_textures, detail); + str = str .. gui.image { + x = fx, + y = fy, + w = TILE_SIZE, + h = TILE_SIZE, + + image = get_variant(unknown_tex, i - x, j - y, noise), + }; else local name = minetest.get_biome_name(column[j * map_scale].biome); local height = column[j * map_scale].height; @@ -105,11 +100,11 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h w = TILE_SIZE, h = TILE_SIZE, - image = biome .. "." .. tostring(get_variant(i - x, j - y, noise)) .. ".png" .. mod, + image = get_variant(biome, i - x, j - y, noise) .. mod, }; if get_marker then - local marker = cartographer.get_marker_texture(get_marker(user, i, j), detail); + local marker = cartographer.get_marker_texture(get_marker(..., i, j), detail); if marker then str = str .. gui.image { x = fx, @@ -134,7 +129,15 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h }; end else - str = str .. unknown_biome_tile(fx, fy, detail, get_variant(i - x, j - y, noise)); + local unknown_tex = cartographer.detail_texture(skin.unknown_biome_textures, detail); + str = str .. gui.image { + x = fx, + y = fy, + w = TILE_SIZE, + h = TILE_SIZE, + + image = get_variant(unknown_tex, i - x, j - y, noise), + }; end end end @@ -143,6 +146,8 @@ local function generate_map(x, y, w, h, player_x, player_y, detail, map_scale, h return str; end +local map_formspec = {}; + -- Get the formspec for a given map segment -- x: The X position of the map, in map coordinates -- y: The Y position of the map, in map coordinates @@ -154,7 +159,7 @@ end -- -- Returns a formspec string, the width of the formspec, and the height of the -- formspec -function cartographer.get_map_formspec(x, y, w, h, detail, scale, height_mode) +function map_formspec.from_coords(x, y, w, h, detail, scale, height_mode) local formspec_width = (w + 1) * TILE_OFFSET + 0.01; local formspec_height = (h + 1) * TILE_OFFSET + 0.01; return gui.formspec { @@ -173,7 +178,7 @@ end -- -- Returns a formspec string, the width of the formspec, and the height of the -- formspec -function cartographer.get_map_formspec_map(map, x, y, height_mode) +function map_formspec.from_map(map, x, y, height_mode) local formspec_width = (map.w + 1) * TILE_OFFSET + 0.01; local formspec_height = (map.h + 1) * TILE_OFFSET + 0.01; return gui.formspec { @@ -183,3 +188,5 @@ function cartographer.get_map_formspec_map(map, x, y, height_mode) generate_map(map.x, map.z, map.w, map.h, x, y, map.detail, map.scale, height_mode, cartographer.is_filled, cartographer.get_marker, map), }, formspec_width, formspec_height; end + +return map_formspec;