From 9a3ae17564bc04cb1313760ac49613ab700afadd 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/ITEMS/mcl_itemframes/init.lua | 63 ++++++++++++++-------- mods/ITEMS/mcl_itemframes/mod.conf | 2 +- mods/ITEMS/mcl_maps/init.lua | 87 ++++++++++++++---------------- 3 files changed, 81 insertions(+), 71 deletions(-) diff --git a/mods/ITEMS/mcl_itemframes/init.lua b/mods/ITEMS/mcl_itemframes/init.lua index 073933fcf7..4d81e76c3f 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 39f4370aa4..bbf4ec9691 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 357c0f48ee..76ec134dc5 100644 --- a/mods/ITEMS/mcl_maps/init.lua +++ b/mods/ITEMS/mcl_maps/init.lua @@ -22,7 +22,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") @@ -30,16 +29,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) @@ -58,7 +53,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] @@ -111,24 +106,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() @@ -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_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, }) @@ -220,38 +238,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