Make maps show their image when in itemframes

This commit is contained in:
Elias Fleckenstein 2021-05-02 16:04:48 +02:00
parent bd74dbe321
commit 9a3ae17564
3 changed files with 81 additions and 71 deletions

View file

@ -53,6 +53,24 @@ minetest.register_entity("mcl_itemframes:item",{
end, end,
}) })
minetest.register_entity("mcl_itemframes:map", {
initial_properties = {
visual = "upright_sprite",
visual_size = {x = 1, y = 1},
pointable = false,
physical = false,
collide_with_objects = false,
textures = {"blank.png"},
},
on_activate = function(self, staticdata)
self.id = staticdata
self.object:set_properties({textures = {mcl_maps.load_map(self.id)}})
end,
get_staticdata = function(self)
return self.id
end,
})
local facedir = {} local facedir = {}
facedir[0] = {x=0,y=0,z=1} facedir[0] = {x=0,y=0,z=1}
@ -61,13 +79,10 @@ facedir[2] = {x=0,y=0,z=-1}
facedir[3] = {x=-1,y=0,z=0} facedir[3] = {x=-1,y=0,z=0}
local remove_item_entity = function(pos, node) local remove_item_entity = function(pos, node)
local objs = nil
if node.name == "mcl_itemframes:item_frame" then if node.name == "mcl_itemframes:item_frame" then
objs = minetest.get_objects_inside_radius(pos, .5) for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
end local entity = obj:get_luaentity()
if objs then if entity and (entity.name == "mcl_itemframes:item" or entity.name == "mcl_itemframes:map") then
for _, obj in ipairs(objs) do
if obj and obj:get_luaentity() and obj:get_luaentity().name == "mcl_itemframes:item" then
obj:remove() obj:remove()
end end
end end
@ -89,6 +104,9 @@ local update_item_entity = function(pos, node, param2)
pos.y = pos.y + posad.y*6.5/16 pos.y = pos.y + posad.y*6.5/16
pos.z = pos.z + posad.z*6.5/16 pos.z = pos.z + posad.z*6.5/16
end end
local yaw = math.pi*2 - param2 * math.pi/2
local map_id = item:get_meta():get_string("mcl_maps:id")
if map_id == "" then
local e = minetest.add_entity(pos, "mcl_itemframes:item") local e = minetest.add_entity(pos, "mcl_itemframes:item")
local lua = e:get_luaentity() local lua = e:get_luaentity()
lua._nodename = node.name lua._nodename = node.name
@ -99,15 +117,14 @@ local update_item_entity = function(pos, node, param2)
else else
lua._texture = itemname lua._texture = itemname
local def = minetest.registered_items[itemname] local def = minetest.registered_items[itemname]
if def and def.wield_scale then lua._scale = def and def.wield_scale and def.wield_scale.x or 1
lua._scale = def.wield_scale.x
else
lua._scale = 1
end
end end
lua:_update_texture() lua:_update_texture()
if node.name == "mcl_itemframes:item_frame" then if node.name == "mcl_itemframes:item_frame" then
local yaw = math.pi*2 - param2 * math.pi/2 e:set_yaw(yaw)
end
else
local e = minetest.add_entity(pos, "mcl_itemframes:map", map_id)
e:set_yaw(yaw) e:set_yaw(yaw)
end end
end end

View file

@ -1,3 +1,3 @@
name = mcl_itemframes name = mcl_itemframes
depends = mcl_core, mcl_sounds depends = mcl_core, mcl_sounds, mcl_maps
optional_depends = screwdriver optional_depends = screwdriver

View file

@ -22,7 +22,6 @@ local palettes = load_json_file("palettes")
local color_cache = {} local color_cache = {}
local creating_maps = {} local creating_maps = {}
local loading_maps = {}
local loaded_maps = {} local loaded_maps = {}
local c_air = minetest.get_content_id("air") local c_air = minetest.get_content_id("air")
@ -30,16 +29,12 @@ local c_air = minetest.get_content_id("air")
function mcl_maps.create_map(pos) function mcl_maps.create_map(pos)
local itemstack = ItemStack("mcl_maps:filled_map") local itemstack = ItemStack("mcl_maps:filled_map")
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local id = storage:get_int("next_id") local next_id = storage:get_int("next_id")
storage:set_int("next_id", id + 1) storage:set_int("next_id", next_id + 1)
local texture_file = "mcl_maps_map_texture_" .. id .. ".tga" local id = tostring(next_id)
local texture_path = map_textures_path .. texture_file meta:set_string("mcl_maps:id", id)
local texture = "[combine:140x140:0,0=mcl_maps_map_background.png:6,6=" .. texture_file
meta:set_int("mcl_maps:id", id)
meta:set_string("mcl_maps:texture", texture)
meta:set_string("mcl_maps:texture_path", texture_path)
tt.reload_itemstack_description(itemstack) tt.reload_itemstack_description(itemstack)
creating_maps[texture] = true creating_maps[id] = true
local minp = vector.multiply(vector.floor(vector.divide(pos, 128)), 128) local minp = vector.multiply(vector.floor(vector.divide(pos, 128)), 128)
local maxp = vector.add(minp, vector.new(127, 127, 127)) local maxp = vector.add(minp, vector.new(127, 127, 127))
minetest.emerge_area(minp, maxp, function(blockpos, action, calls_remaining) minetest.emerge_area(minp, maxp, function(blockpos, action, calls_remaining)
@ -58,7 +53,7 @@ function mcl_maps.create_map(pos)
local heightmap = {} local heightmap = {}
for z = 1, 128 do for z = 1, 128 do
local map_z = minp.z - 1 + z local map_z = minp.z - 1 + z
local color local color, height
for map_y = maxp.y, minp.y, -1 do for map_y = maxp.y, minp.y, -1 do
local index = area:index(map_x, map_y, map_z) local index = area:index(map_x, map_y, map_z)
local c_id = data[index] local c_id = data[index]
@ -115,20 +110,43 @@ function mcl_maps.create_map(pos)
break break
end end
end end
heightmap[z] = height heightmap[z] = height or minp.y
pixels[z] = pixels[z] or {} pixels[z] = pixels[z] or {}
pixels[z][x] = color or {0, 0, 0} pixels[z][x] = color or {0, 0, 0}
end end
last_heightmap = heightmap last_heightmap = heightmap
end end
tga_encoder.image(pixels):save(texture_path) tga_encoder.image(pixels):save(map_textures_path .. "mcl_maps_map_texture_" .. id .. ".tga")
creating_maps[texture] = false creating_maps[id] = nil
end) end)
return itemstack return itemstack
end end
-- Turn empty map into filled map by rightclick function mcl_maps.load_map(id)
local make_filled_map = function(itemstack, placer, pointed_thing) if id == "" or creating_maps[id] then
return
end
local texture = "mcl_maps_map_texture_" .. id .. ".tga"
if not loaded_maps[id] then
loaded_maps[id] = true
minetest.dynamic_add_media(map_textures_path .. texture, function() end)
end
return texture
end
function mcl_maps.load_map_item(itemstack)
return mcl_maps.load_map(itemstack:get_meta():get_string("mcl_maps:id"))
end
local function fill_map(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 minetest.settings:get_bool("enable_real_maps", true) then if minetest.settings:get_bool("enable_real_maps", true) then
local new_map = mcl_maps.create_map(placer:get_pos()) local new_map = mcl_maps.create_map(placer:get_pos())
itemstack:take_item() itemstack:take_item()
@ -151,8 +169,8 @@ minetest.register_craftitem("mcl_maps:empty_map", {
_doc_items_longdesc = S("Empty maps are not useful as maps, but they can be stacked and turned to maps which can be used."), _doc_items_longdesc = S("Empty maps are not useful as maps, but they can be stacked and turned to maps which can be used."),
_doc_items_usagehelp = S("Rightclick to create a filled map (which can't be stacked anymore)."), _doc_items_usagehelp = S("Rightclick to create a filled map (which can't be stacked anymore)."),
inventory_image = "mcl_maps_map_empty.png", inventory_image = "mcl_maps_map_empty.png",
on_place = make_filled_map, on_place = fill_map,
on_secondary_use = make_filled_map, on_secondary_use = fill_map,
stack_max = 64, stack_max = 64,
}) })
@ -220,38 +238,13 @@ minetest.register_on_leaveplayer(function(player)
huds[player] = nil huds[player] = nil
end) end)
local function is_holding_map(player)
local wield = player:get_wielded_item()
if wield:get_name() ~= "mcl_maps:filled_map" then
return
end
local meta = wield:get_meta()
local texture = meta:get_string("mcl_maps:texture")
if texture == "" then
return
end
if loaded_maps[texture] then
return texture
end
local path = meta:get_string("mcl_maps:texture_path")
if not creating_maps[texture] and not loading_maps[texture] then
loading_maps[texture] = true
local player_name = player:get_player_name()
minetest.dynamic_add_media(path, function(finished_name)
if player_name == finished_name then
loading_maps[texture] = false
loaded_maps[texture] = true
end
end)
end
end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do for _, player in pairs(minetest.get_connected_players()) do
local texture = is_holding_map(player) local wield = player:get_wielded_item()
local texture = mcl_maps.load_map_item(wield)
if texture then if texture then
if texture ~= maps[player] then if texture ~= maps[player] then
player:hud_change(huds[player], "text", texture) player:hud_change(huds[player], "text", "[combine:140x140:0,0=mcl_maps_map_background.png:6,6=" .. texture)
maps[player] = texture maps[player] = texture
end end
elseif maps[player] then elseif maps[player] then