2020-03-08 15:34:20 +01:00
|
|
|
function cartographer.create_map(x, z, w, h, filled, detail, scale)
|
2020-02-16 18:55:07 +01:00
|
|
|
local id = _cartographer.next_map_id;
|
|
|
|
|
|
|
|
local map = {
|
|
|
|
id = id,
|
|
|
|
x = x,
|
|
|
|
z = z,
|
|
|
|
w = w,
|
|
|
|
h = h,
|
|
|
|
detail = detail,
|
2020-03-08 15:34:20 +01:00
|
|
|
scale = scale,
|
2020-02-16 18:55:07 +01:00
|
|
|
fill = {},
|
2020-03-17 11:52:15 +01:00
|
|
|
markers = {},
|
2020-02-16 18:55:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
_cartographer.maps[id] = map;
|
|
|
|
if filled then
|
|
|
|
cartographer.fill(map, x, z, w, h);
|
|
|
|
end
|
|
|
|
|
|
|
|
_cartographer.next_map_id = _cartographer.next_map_id + 1;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
end
|
|
|
|
|
|
|
|
function cartographer.get_map(id)
|
|
|
|
return _cartographer.maps[id];
|
|
|
|
end
|
|
|
|
|
|
|
|
function cartographer.resize_map(id, w, h)
|
|
|
|
local map = cartographer.get_map(id);
|
|
|
|
if map ~= nil and w >= map.w and h >= map.h then
|
|
|
|
map.w = w;
|
|
|
|
map.h = h;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function cartographer.fill(map, x, z, w, h)
|
|
|
|
if map == nil then
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = math.max(x, map.x),math.min(x + w - 1, map.x + map.w),1 do
|
|
|
|
for j = math.max(z, map.z),math.min(z + h - 1, map.z + map.h),1 do
|
|
|
|
map.fill[(i - map.x) + ((j - map.z) * map.w)] = map.detail;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-17 11:52:15 +01:00
|
|
|
function cartographer.set_marker(map, x, z, marker)
|
|
|
|
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h then
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
|
|
|
|
if not map.markers[x] then
|
|
|
|
map.markers[x] = {
|
|
|
|
[z] = marker,
|
|
|
|
};
|
|
|
|
else
|
|
|
|
map.markers[x][z] = marker;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function cartographer.get_marker(map, x, z)
|
|
|
|
if map == nil or x < map.x or x > map.x + map.w or z < map.z or z > map.z + map.h or not map.markers[x] then
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
return map.markers[x][z];
|
|
|
|
end
|
|
|
|
|
2020-04-11 20:02:12 +02:00
|
|
|
-- Fill in the local area of a map around a position
|
|
|
|
-- id: A map ID
|
|
|
|
-- x: The x position, in world coordinates
|
|
|
|
-- z: The z position, in world coordinates
|
2020-02-16 18:55:07 +01:00
|
|
|
function cartographer.fill_local(id, x, z)
|
|
|
|
local map = cartographer.get_map(id);
|
|
|
|
|
2020-04-11 20:02:12 +02:00
|
|
|
x, z = cartographer.to_map_coordinates(map, x, z);
|
|
|
|
|
|
|
|
-- TODO: Adjust size to match map scale
|
|
|
|
if map and x >= map.x - 2 and x <= map.x + map.w + 1 and z >= map.z - 2 and z <= map.z + map.h + 1 then
|
2020-02-16 18:55:07 +01:00
|
|
|
cartographer.fill(map, x - 2, z - 2, 5, 5);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 23:52:09 +02:00
|
|
|
-- Convert a position in world coordinates to the given map's coordinate system
|
|
|
|
-- map: The map to use as reference, or nil to use the default size (map scale of 1)
|
|
|
|
-- x: The x position, in world coordinates
|
|
|
|
-- z: The z position, in world coordinates
|
|
|
|
--
|
|
|
|
-- Returns The converted x and z coordinates
|
|
|
|
function cartographer.to_map_coordinates(map, x, z)
|
|
|
|
if not map or map.scale == 0 then
|
|
|
|
return tochunk(x), tochunk(z);
|
|
|
|
end
|
|
|
|
|
|
|
|
return math.floor(tochunk(x) / map.scale + 0.5), math.floor(tochunk(z) / map.scale + 0.5);
|
|
|
|
end
|
|
|
|
|
2020-06-02 00:10:26 +02:00
|
|
|
-- Periodically-called function to fill in maps and queue chunks for manual
|
|
|
|
-- scanning
|
|
|
|
local function fill_loop()
|
|
|
|
-- Fill in all player-held maps
|
|
|
|
for _,p in ipairs(minetest.get_connected_players()) do
|
|
|
|
local inventory = p:get_inventory();
|
|
|
|
local pos = p:get_pos();
|
|
|
|
if pos.y > -10 then
|
|
|
|
for i = 1,inventory:get_size("main") do
|
|
|
|
local stack = inventory:get_stack("main", i);
|
|
|
|
|
|
|
|
if stack:get_name() == "cartographer:map" then
|
|
|
|
cartographer.fill_local(stack:get_meta():get_int("cartographer:map_id"), pos.x, pos.z);
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
2020-06-02 00:10:26 +02:00
|
|
|
end
|
|
|
|
for i = -2,2 do
|
|
|
|
for j = -2,2 do
|
|
|
|
local adjusted_pos = {
|
|
|
|
x = pos.x + fromchunk(i),
|
|
|
|
y = pos.y,
|
|
|
|
z = pos.z + fromchunk(j),
|
|
|
|
}
|
|
|
|
cartographer.queue_region(adjusted_pos);
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-06-02 00:10:26 +02:00
|
|
|
end
|
2020-02-16 18:55:07 +01:00
|
|
|
|
2020-06-02 00:10:26 +02:00
|
|
|
for _ = 1,10 do
|
|
|
|
cartographer.scan_regions();
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
2020-06-02 00:10:26 +02:00
|
|
|
minetest.after(5, fill_loop);
|
|
|
|
end
|
|
|
|
minetest.after(5, fill_loop);
|
2020-02-16 18:55:07 +01:00
|
|
|
|
2020-02-17 02:18:47 +01:00
|
|
|
-- 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)
|
|
|
|
_cartographer.biome_lookup[#_cartographer.biome_lookup + 1] = {
|
|
|
|
name = name,
|
|
|
|
textures = textures,
|
|
|
|
min_height = min_height,
|
|
|
|
max_height = max_height,
|
|
|
|
};
|
2020-02-16 18:55:07 +01:00
|
|
|
end
|
|
|
|
|
2020-04-10 23:19:21 +02:00
|
|
|
-- 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.
|
|
|
|
-- id: The id to format
|
|
|
|
--
|
|
|
|
-- Returns the formatted id
|
|
|
|
local function format_marker_id(id)
|
|
|
|
return id:gsub(":", "_");
|
|
|
|
end
|
|
|
|
|
2020-04-17 01:50:03 +02:00
|
|
|
-- Find the marker data for a given id
|
|
|
|
-- id: The id to search for
|
|
|
|
--
|
|
|
|
-- Returns the marker data, or nil if not found
|
|
|
|
function cartographer.get_marker_data(id)
|
|
|
|
if not id then
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
id = format_marker_id(id);
|
|
|
|
for _,marker in pairs(_cartographer.marker_lookup) do
|
|
|
|
if marker.id == id then
|
|
|
|
return marker;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
2020-03-17 12:15:38 +01:00
|
|
|
-- Register a marker with textures to display
|
|
|
|
-- id: A string containing the id of the marker
|
|
|
|
-- name: A string containing the displayedname of the marker
|
|
|
|
-- 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
|
|
|
|
function cartographer.register_marker(id, name, textures)
|
2020-04-10 23:19:21 +02:00
|
|
|
if not id then
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
id = format_marker_id(id);
|
2020-04-17 01:50:03 +02:00
|
|
|
local existing_marker = cartographer.get_marker_data(id);
|
|
|
|
if existing_marker then
|
|
|
|
existing_marker.name = name;
|
|
|
|
existing_marker.textures = textures;
|
|
|
|
else
|
|
|
|
_cartographer.marker_lookup[#_cartographer.marker_lookup+1] = {
|
|
|
|
id = id,
|
|
|
|
name = name,
|
|
|
|
textures = textures,
|
|
|
|
};
|
2020-03-28 14:51:38 +01:00
|
|
|
end
|
2020-03-17 12:15:38 +01:00
|
|
|
end
|
|
|
|
|
2020-02-16 18:55:07 +01:00
|
|
|
function cartographer.is_filled(map, x, z)
|
|
|
|
if map == nil then
|
|
|
|
return false;
|
|
|
|
end
|
|
|
|
|
2020-03-07 16:26:24 +01:00
|
|
|
-- minetest.chat_send_all(tostring(x).. ", " .. tostring(z) .. "(" .. tostring(x + (z * map.w)) .."): " .. tostring(map.fill[x + (z * map.w)]));
|
2020-02-16 18:55:07 +01:00
|
|
|
return map.fill[(x - map.x) + ((z - map.z) * map.w)] ~= nil;
|
|
|
|
end
|
2020-02-17 02:18:47 +01:00
|
|
|
|
2020-04-18 15:15:13 +02:00
|
|
|
-- Get an entry from a list for a given detail level
|
|
|
|
-- textures: An array of textures
|
|
|
|
-- detail: The detail level
|
|
|
|
--
|
|
|
|
-- Returns the entry at detail, or the last entry if detail is out-of-bounds
|
|
|
|
function cartographer.detail_texture(textures, detail)
|
|
|
|
return textures[math.min(detail, #textures)];
|
|
|
|
end
|
|
|
|
|
2020-02-17 02:18:47 +01:00
|
|
|
-- 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(_cartographer.biome_lookup) do
|
2020-04-18 15:15:13 +02:00
|
|
|
if biome.name == name and (not biome.min_height or height >= biome.min_height) and (not biome.max_height or height <= biome.max_height) then
|
|
|
|
return cartographer.detail_texture(biome.textures, detail);
|
2020-02-17 02:18:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
end
|
2020-03-17 12:15:38 +01:00
|
|
|
|
|
|
|
-- Get the texture name (minus extension) for the given marker and detail level.
|
|
|
|
-- id: A string containing the marker id
|
|
|
|
-- detail: The detail level
|
|
|
|
-- Returns a string with a texture name, or nil if no matching marker was found.
|
|
|
|
function cartographer.get_marker_texture(id, detail)
|
2020-04-10 23:19:21 +02:00
|
|
|
if not id then
|
|
|
|
return nil;
|
|
|
|
end
|
|
|
|
|
|
|
|
id = format_marker_id(id);
|
2020-04-17 01:50:03 +02:00
|
|
|
local marker = cartographer.get_marker_data(id);
|
2020-03-17 12:15:38 +01:00
|
|
|
|
|
|
|
if marker then
|
|
|
|
return marker.textures[math.min(detail, #marker.textures)];
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
end
|
2020-04-18 17:28:21 +02:00
|
|
|
|
|
|
|
-- Play a feedback sound localized on the given player
|
|
|
|
-- sound: The sound to play
|
|
|
|
-- player: The player who triggered the sound
|
|
|
|
function cartographer.map_sound(sound, player)
|
|
|
|
minetest.sound_play(sound, { pos=player:get_pos(), max_hear_distance=5 }, true);
|
|
|
|
end
|