cartographer/map_api.lua

160 lines
4.1 KiB
Lua
Raw Normal View History

2020-06-13 14:50:36 +02:00
-- Arguments
-- map_data: The map data source
-- chunk: The chunk coordinate conversion API
2020-06-11 02:54:00 +02:00
local map_data, chunk = ...;
2020-06-13 14:50:36 +02:00
-- The map 'class'
2020-06-10 02:17:39 +02:00
local Map = {};
Map.__index = Map;
for _,loaded_map in ipairs(map_data.maps) do
setmetatable(loaded_map, Map);
end
2020-06-13 14:50:36 +02:00
-- Resize this map
--
-- w: The new width
-- h: The new height
2020-06-10 02:17:39 +02:00
function Map.resize(self, w, h)
if w >= self.w and h >= self.h then
self.w = w;
self.h = h;
-- FIXME: Is this really correct? Seems questionable.
2020-02-16 18:55:07 +01:00
end
end
2020-06-13 14:50:36 +02:00
-- Fill in a region of this map
--
-- x: The x position, in map coordinates
-- z: The z position, in map coordinates
-- w: The width, in map coordinates
-- h: The height, in map coordinates
2020-06-10 02:17:39 +02:00
function Map.fill_area(self, x, z, w, h)
for i = math.max(x, self.x),math.min(x + w - 1, self.x + self.w),1 do
for j = math.max(z, self.z),math.min(z + h - 1, self.z + self.h),1 do
self.fill[(i - self.x) + ((j - self.z) * self.w)] = self.detail;
2020-02-16 18:55:07 +01:00
end
end
end
2020-06-13 14:50:36 +02:00
-- Set the marker at the given position
--
-- x: The x position, in map coordinates
-- z: The z position, in map coordinates
-- marker: The marker ID to set, or nil to unset
2020-06-10 02:17:39 +02:00
function Map.set_marker(self, x, z, marker)
if x < self.x or x > self.x + self.w or z < self.z or z > self.z + self.h then
return;
end
2020-06-10 02:17:39 +02:00
if not self.markers[x] then
self.markers[x] = {
[z] = marker,
};
else
2020-06-10 02:17:39 +02:00
self.markers[x][z] = marker;
end
end
2020-06-13 14:50:36 +02:00
-- Get the marker at the given position
--
-- x: The x position, in map coordinates
-- z: The z position, in map coordinates
--
-- Returns a marker id
2020-06-10 02:17:39 +02:00
function Map.get_marker(self, x, z)
if x < self.x or x > self.x + self.w or z < self.z or z > self.z + self.h or not self.markers[x] then
return nil;
end
2020-06-10 02:17:39 +02:00
return self.markers[x][z];
end
2020-04-11 20:02:12 +02:00
-- Fill in the local area of a map around a position
2020-06-13 14:50:36 +02:00
--
2020-04-11 20:02:12 +02:00
-- id: A map ID
-- x: The x position, in world coordinates
-- z: The z position, in world coordinates
function Map.fill_local(self, x, z)
x, z = self:to_coordinates(x, z);
2020-04-11 20:02:12 +02:00
-- TODO: Adjust size to match map scale
if x >= self.x - 2 and x <= self.x + self.w + 1 and z >= self.z - 2 and z <= self.z + self.h + 1 then
self:fill_area(x - 2, z - 2, 5, 5);
2020-02-16 18:55:07 +01:00
end
end
-- Convert a position in world coordinates to the given map's coordinate system
2020-06-13 14:50:36 +02:00
--
-- x: The x position, in world coordinates
-- z: The z position, in world coordinates
--
-- Returns The converted x and z coordinates
2020-06-10 02:17:39 +02:00
function Map.to_coordinates(self, x, z)
if self.scale == 0 then
return chunk.to(x), chunk.to(z);
end
2020-06-10 02:17:39 +02:00
return math.floor(chunk.to(x) / self.scale + 0.5), math.floor(chunk.to(z) / self.scale + 0.5);
end
2020-06-13 14:50:36 +02:00
-- Check if the given position on this map is filled
--
-- x: The x position, in map coordinates
-- z: The z position, in map coordinates
function Map.is_filled(self, x, z)
return self.fill[(x - self.x) + ((z - self.z) * self.w)] ~= nil;
end
2020-06-13 14:50:36 +02:00
-- The Map API
local maps = {
2020-06-13 14:50:36 +02:00
-- Create a new map object with the given parameters
--
-- x: The x position, in map coordinates
-- z: The z position, in map coordinates
-- w: The width, in map coordinates
-- h: The height, in map coordinates
-- filled: Whether or not the map is pre-filled
-- detail: The detail level
-- scale: The scale factor
--
-- Returns the new map's id
create = function(x, z, w, h, filled, detail, scale)
local id = map_data.next_map_id;
local map = {
id = id,
x = x,
z = z,
w = w,
h = h,
detail = detail,
scale = scale,
fill = {},
markers = {},
};
setmetatable(map, Map);
map_data.maps[id] = map;
if filled then
map:fill(map, x, z, w, h);
end
map_data.next_map_id = map_data.next_map_id + 1;
return id;
end,
2020-06-13 14:50:36 +02:00
-- Get the map objwct assigned to the given id
--
-- id: The map id
--
-- Returns a map object, or nil if the id is invalid
get = function(id)
return map_data.maps[id];
end,
}
return maps;