From f3f2560b3219a70c223af8f194de60e9dfc1e690 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Sun, 2 May 2021 16:04:48 +0200 Subject: [PATCH] Make maps show their image when in itemframes --- mods/CORE/mcl_util/init.lua | 15 ++++++ mods/ITEMS/mcl_itemframes/init.lua | 63 ++++++++++++++-------- mods/ITEMS/mcl_itemframes/mod.conf | 2 +- mods/ITEMS/mcl_maps/init.lua | 87 ++++++++++++++---------------- 4 files changed, 96 insertions(+), 71 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 6c63c21a..83a203d4 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -405,3 +405,18 @@ function mcl_util.get_object_center(obj) pos.y = pos.y + (ymax - ymin) / 2.0 return pos end + +function mcl_util.call_on_rightclick(itemstack, player, pointed_thing) + -- Call on_rightclick if the pointed node defines it + if pointed_thing and pointed_thing.type == "node" then + local pos = pointed_thing.under + local node = minetest.get_node(pos) + if player and not player:get_player_control().sneak then + local nodedef = minetest.registered_nodes[node.name] + local on_rightclick = nodedef and nodedef.on_rightclick + if on_rightclick then + return on_rightclick(pos, node, player, itemstack, pointed_thing) or itemstack + end + end + end +end diff --git a/mods/ITEMS/mcl_itemframes/init.lua b/mods/ITEMS/mcl_itemframes/init.lua index 073933fc..4d81e76c 100644 --- a/mods/ITEMS/mcl_itemframes/init.lua +++ b/mods/ITEMS/mcl_itemframes/init.lua @@ -53,6 +53,24 @@ minetest.register_entity("mcl_itemframes:item",{ 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 = {} 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} local remove_item_entity = function(pos, node) - local objs = nil if node.name == "mcl_itemframes:item_frame" then - objs = minetest.get_objects_inside_radius(pos, .5) - end - if objs then - for _, obj in ipairs(objs) do - if obj and obj:get_luaentity() and obj:get_luaentity().name == "mcl_itemframes:item" then + for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do + local entity = obj:get_luaentity() + if entity and (entity.name == "mcl_itemframes:item" or entity.name == "mcl_itemframes:map") then obj:remove() end end @@ -89,25 +104,27 @@ local update_item_entity = function(pos, node, param2) pos.y = pos.y + posad.y*6.5/16 pos.z = pos.z + posad.z*6.5/16 end - local e = minetest.add_entity(pos, "mcl_itemframes:item") - local lua = e:get_luaentity() - lua._nodename = node.name - local itemname = item:get_name() - if itemname == "" or itemname == nil then - lua._texture = "blank.png" - lua._scale = 1 - else - lua._texture = itemname - local def = minetest.registered_items[itemname] - if def and def.wield_scale then - lua._scale = def.wield_scale.x - else + 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 lua = e:get_luaentity() + lua._nodename = node.name + local itemname = item:get_name() + if itemname == "" or itemname == nil then + lua._texture = "blank.png" lua._scale = 1 + else + lua._texture = itemname + local def = minetest.registered_items[itemname] + lua._scale = def and def.wield_scale and def.wield_scale.x or 1 end - end - lua:_update_texture() - if node.name == "mcl_itemframes:item_frame" then - local yaw = math.pi*2 - param2 * math.pi/2 + lua:_update_texture() + if node.name == "mcl_itemframes:item_frame" then + e:set_yaw(yaw) + end + else + local e = minetest.add_entity(pos, "mcl_itemframes:map", map_id) e:set_yaw(yaw) end end diff --git a/mods/ITEMS/mcl_itemframes/mod.conf b/mods/ITEMS/mcl_itemframes/mod.conf index 39f4370a..bbf4ec96 100644 --- a/mods/ITEMS/mcl_itemframes/mod.conf +++ b/mods/ITEMS/mcl_itemframes/mod.conf @@ -1,3 +1,3 @@ name = mcl_itemframes -depends = mcl_core, mcl_sounds +depends = mcl_core, mcl_sounds, mcl_maps optional_depends = screwdriver diff --git a/mods/ITEMS/mcl_maps/init.lua b/mods/ITEMS/mcl_maps/init.lua index ee68ac75..a9928b12 100644 --- a/mods/ITEMS/mcl_maps/init.lua +++ b/mods/ITEMS/mcl_maps/init.lua @@ -21,7 +21,6 @@ local palettes = load_json_file("palettes") local color_cache = {} local creating_maps = {} -local loading_maps = {} local loaded_maps = {} local c_air = minetest.get_content_id("air") @@ -29,16 +28,12 @@ local c_air = minetest.get_content_id("air") function mcl_maps.create_map(pos) local itemstack = ItemStack("mcl_maps:filled_map") local meta = itemstack:get_meta() - local id = storage:get_int("next_id") - storage:set_int("next_id", id + 1) - local texture_file = "mcl_maps_map_texture_" .. id .. ".tga" - local texture_path = map_textures_path .. texture_file - 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) + local next_id = storage:get_int("next_id") + storage:set_int("next_id", next_id + 1) + local id = tostring(next_id) + meta:set_string("mcl_maps:id", id) 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 maxp = vector.add(minp, vector.new(127, 127, 127)) minetest.emerge_area(minp, maxp, function(blockpos, action, calls_remaining) @@ -57,7 +52,7 @@ function mcl_maps.create_map(pos) local heightmap = {} for z = 1, 128 do local map_z = minp.z - 1 + z - local color + local color, height for map_y = maxp.y, minp.y, -1 do local index = area:index(map_x, map_y, map_z) local c_id = data[index] @@ -110,24 +105,47 @@ function mcl_maps.create_map(pos) } end end - height = map_y + height = map_y break end end - heightmap[z] = height + heightmap[z] = height or minp.y pixels[z] = pixels[z] or {} pixels[z][x] = color or {0, 0, 0} end last_heightmap = heightmap end - tga_encoder.image(pixels):save(texture_path) - creating_maps[texture] = false + tga_encoder.image(pixels):save(map_textures_path .. "mcl_maps_map_texture_" .. id .. ".tga") + creating_maps[id] = nil end) return itemstack end --- Turn empty map into filled map by rightclick -local make_filled_map = function(itemstack, placer, pointed_thing) +function mcl_maps.load_map(id) + 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 local new_map = mcl_maps.create_map(placer:get_pos()) itemstack:take_item() @@ -150,8 +168,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_usagehelp = S("Rightclick to create a filled map (which can't be stacked anymore)."), inventory_image = "mcl_maps_map_empty.png", - on_place = make_filled_map, - on_secondary_use = make_filled_map, + on_place = fill_map, + on_secondary_use = fill_map, stack_max = 64, }) @@ -219,38 +237,13 @@ minetest.register_on_leaveplayer(function(player) huds[player] = nil 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) 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 ~= 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 end elseif maps[player] then