Add markers to maps (using banners)

This commit is contained in:
Nils Dagsson Moskopp 2022-02-05 05:48:18 +01:00
parent f0a925aa20
commit 6ff0303802
Signed by untrusted user who does not match committer: erle
GPG Key ID: A3BC671C35191080
5 changed files with 119 additions and 8 deletions

View File

@ -343,6 +343,18 @@ minetest.register_node("mcl_banners:hanging_banner", {
end, end,
}) })
function mcl_banners.get_banner_colorid(pos)
local node_meta = minetest.get_meta(pos)
local banner_item = node_meta:get_inventory():get_stack("banner", 1)
local banner_item_name = banner_item:get_name()
local colorid = colors_reverse[banner_item_name]
return colorid
end
function mcl_banners.colorid_to_hexcolor(colorid)
return mcl_banners.colors[colorid][4]
end
-- for pattern_name, pattern in pairs(patterns) do -- for pattern_name, pattern in pairs(patterns) do
for colorid, colortab in pairs(mcl_banners.colors) do for colorid, colortab in pairs(mcl_banners.colors) do
for i, pattern_name in ipairs(pattern_names) do for i, pattern_name in ipairs(pattern_names) do

View File

@ -70,11 +70,24 @@ minetest.register_entity("mcl_itemframes:map", {
self._pos = { x = 0/0, y = 0/0, z = 0/0 } self._pos = { x = 0/0, y = 0/0, z = 0/0 }
self._minp = { x = 0/0, y = 0/0, z = 0/0 } self._minp = { x = 0/0, y = 0/0, z = 0/0 }
self._maxp = { x = 0/0, y = 0/0, z = 0/0 } self._maxp = { x = 0/0, y = 0/0, z = 0/0 }
self._banners = {}
else else
self.id = data.id self.id = data.id
self._pos = data._pos self._pos = data._pos
self._minp = data._minp self._minp = data._minp
self._maxp = data._maxp self._maxp = data._maxp
self._banners = data._banners
end
local texture = mcl_maps.load_map(self.id)
if nil ~= self._banners and #self._banners > 0 then
local border = { top = 0, left = 0, bottom = 0, right = 0 }
local banner_overlay = mcl_maps.make_banner_overlay(
self._banners,
self._minp,
self._maxp,
border
)
texture = texture .. banner_overlay
end end
local marker = "" local marker = ""
if ( if (
@ -95,7 +108,7 @@ minetest.register_entity("mcl_itemframes:map", {
end end
self.object:set_properties( self.object:set_properties(
{ {
textures = { mcl_maps.load_map(self.id) .. marker } textures = { texture .. marker }
} }
) )
end, end,
@ -105,6 +118,7 @@ minetest.register_entity("mcl_itemframes:map", {
_pos = self._pos, _pos = self._pos,
_minp = self._minp, _minp = self._minp,
_maxp = self._maxp, _maxp = self._maxp,
_banners = self._banners,
} }
return minetest.serialize(data) return minetest.serialize(data)
end, end,
@ -164,11 +178,19 @@ local update_item_entity = function(pos, node, param2)
e:set_yaw(yaw) e:set_yaw(yaw)
end end
else else
local banners
local banners_string = meta:get_string("mcl_maps:banners")
if "" == banners_string then
banners = {}
else
banners = minetest.deserialize(banners_string)
end
local staticdata = { local staticdata = {
id = map_id, id = map_id,
_pos = pos, _pos = pos,
_minp = minetest.string_to_pos(meta:get_string("mcl_maps:minp")), _minp = minetest.string_to_pos(meta:get_string("mcl_maps:minp")),
_maxp = minetest.string_to_pos(meta:get_string("mcl_maps:maxp")), _maxp = minetest.string_to_pos(meta:get_string("mcl_maps:maxp")),
_banners = banners,
} }
local e = minetest.add_entity(pos, "mcl_itemframes:map", minetest.serialize(staticdata)) local e = minetest.add_entity(pos, "mcl_itemframes:map", minetest.serialize(staticdata))
e:set_yaw(yaw) e:set_yaw(yaw)

View File

@ -316,6 +316,44 @@ local filled_def = {
minetest.register_craftitem("mcl_maps:filled_map", filled_def) minetest.register_craftitem("mcl_maps:filled_map", filled_def)
local function add_marker(itemstack, placer, pointed_thing)
local new_stack = mcl_util.call_on_rightclick(itemstack, placer, pointed_thing)
if new_stack then
return new_stack
end
if "node" == pointed_thing.type then
local item_meta = itemstack:get_meta()
local node_pos = pointed_thing.under
local node = minetest.get_node(node_pos)
local name = node.name
if name == "mcl_banners:hanging_banner" or name == "mcl_banners:standing_banner" then
local banners_string = item_meta:get_string("mcl_maps:banners")
local banners_table
if "" == banners_string then
banners_table = {}
else
banners_table = minetest.deserialize(banners_string)
end
local colorid = mcl_banners.get_banner_colorid(node_pos)
local banner = {
node_pos,
colorid
}
table.insert(
banners_table,
banner
)
item_meta:set_string(
"mcl_maps:banners",
minetest.serialize(banners_table)
)
end
end
return itemstack
end
local filled_wield_def = table.copy(filled_def) local filled_wield_def = table.copy(filled_def)
filled_wield_def.use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false filled_wield_def.use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false
filled_wield_def.visual_scale = 1 filled_wield_def.visual_scale = 1
@ -324,7 +362,7 @@ filled_wield_def.paramtype = "light"
filled_wield_def.drawtype = "mesh" filled_wield_def.drawtype = "mesh"
filled_wield_def.node_placement_prediction = "" filled_wield_def.node_placement_prediction = ""
filled_wield_def.range = minetest.registered_items[""].range filled_wield_def.range = minetest.registered_items[""].range
filled_wield_def.on_place = mcl_util.call_on_rightclick filled_wield_def.on_place = add_marker
for _, texture in pairs(mcl_skins.list) do for _, texture in pairs(mcl_skins.list) do
local def = table.copy(filled_wield_def) local def = table.copy(filled_wield_def)
@ -411,6 +449,30 @@ minetest.register_on_leaveplayer(function(player)
huds[player] = nil huds[player] = nil
end) end)
function mcl_maps.make_banner_overlay(banners, minp, maxp, border)
local texture = ""
for i=1, #banners do
local banner_pos = banners[i][1]
local banner_colorid = banners[i][2]
local banner_hexcolor = mcl_banners.colorid_to_hexcolor(banner_colorid)
if (
banner_pos.x - 3 > minp.x and
banner_pos.x - 3 < maxp.x and
banner_pos.z - 7 > minp.z and
banner_pos.z - 7 < maxp.z
) then
local height = math.abs(maxp.x - minp.x) + border.top + border.bottom
local width = math.abs(maxp.z - minp.z) + border.left + border.right
-- marker is 8×8 & points down, map is 140×140
-- offsets have been experimentally determined
local banner_x = banner_pos.x - minp.x - 3 + border.left
local banner_z = maxp.z - banner_pos.z - 7 + border.top
texture = texture .. "^([combine:" .. width .. "x" .. height .. ":" .. banner_x .. "," .. banner_z .. "=mcl_maps_banner_location_marker.png^[colorize:" .. banner_hexcolor .. ":127)"
end
end
return texture
end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
for _, player in pairs(get_connected_players()) do for _, player in pairs(get_connected_players()) do
local wield = player:get_wielded_item() local wield = player:get_wielded_item()
@ -425,16 +487,31 @@ minetest.register_globalstep(function(dtime)
player:set_wielded_item(wield) player:set_wielded_item(wield)
end end
if texture ~= maps[player] then
player:hud_change(hud.map, "text", "[combine:140x140:0,0=mcl_maps_map_background.png:6,6=" .. texture)
maps[player] = texture
end
local pos = vector.round(player:get_pos()) local pos = vector.round(player:get_pos())
local meta = wield:get_meta() local meta = wield:get_meta()
local minp = string_to_pos(meta:get_string("mcl_maps:minp")) local minp = string_to_pos(meta:get_string("mcl_maps:minp"))
local maxp = string_to_pos(meta:get_string("mcl_maps:maxp")) local maxp = string_to_pos(meta:get_string("mcl_maps:maxp"))
local banners_string = meta:get_string("mcl_maps:banners")
if "" ~= banners_string then
local banners = minetest.deserialize(banners_string)
if #banners > 0 then
local border = { top = 6, right = 6, bottom = 6, left = 6 }
local banner_overlay = mcl_maps.make_banner_overlay(
banners,
minp,
maxp,
border
)
texture = texture .. banner_overlay
end
end
if texture ~= maps[player] then
player:hud_change(hud.map, "text", "[combine:140x140:0,0=mcl_maps_map_background.png:6,6=" .. texture)
maps[player] = texture
end
local marker local marker
if pos.x < minp.x then if pos.x < minp.x then

View File

@ -1,2 +1,2 @@
name = mcl_maps name = mcl_maps
depends = mcl_core, mcl_flowers, tga_encoder, tt, mcl_colors, mcl_skins, mcl_util depends = mcl_banners, mcl_core, mcl_flowers, tga_encoder, tt, mcl_colors, mcl_skins, mcl_util

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 B