Add API to create filled map

This commit is contained in:
Lizzy Fleckenstein 2021-05-01 17:36:51 +02:00
parent e11185638f
commit 2228b30658
1 changed files with 108 additions and 111 deletions

View File

@ -1,3 +1,5 @@
mcl_maps = {}
local S = minetest.get_translator("mcl_maps") local S = minetest.get_translator("mcl_maps")
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
local modpath = minetest.get_modpath("mcl_maps") local modpath = minetest.get_modpath("mcl_maps")
@ -25,11 +27,9 @@ local loaded_maps = {}
local c_air = minetest.get_content_id("air") local c_air = minetest.get_content_id("air")
-- Turn empty map into filled map by rightclick function mcl_maps.create_map(pos)
local make_filled_map = function(itemstack, placer, pointed_thing) local itemstack = ItemStack("mcl_maps:filled_map")
local new_map = ItemStack("mcl_maps:filled_map") local meta = itemstack:get_meta()
if minetest.settings:get_bool("enable_real_maps", true) then
local meta = new_map:get_meta()
local id = storage:get_int("next_id") local id = storage:get_int("next_id")
storage:set_int("next_id", id + 1) storage:set_int("next_id", id + 1)
local texture_file = "mcl_maps_map_texture_" .. id .. ".tga" local texture_file = "mcl_maps_map_texture_" .. id .. ".tga"
@ -38,13 +38,14 @@ local make_filled_map = function(itemstack, placer, pointed_thing)
meta:set_int("mcl_maps:id", id) meta:set_int("mcl_maps:id", id)
meta:set_string("mcl_maps:texture", texture) meta:set_string("mcl_maps:texture", texture)
meta:set_string("mcl_maps:texture_path", texture_path) meta:set_string("mcl_maps:texture_path", texture_path)
tt.reload_itemstack_description(new_map) tt.reload_itemstack_description(itemstack)
creating_maps[texture] = true creating_maps[texture] = true
local pos = placer:get_pos()
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)
if calls_remaining < 1 then if calls_remaining > 0 then
return
end
local vm = minetest.get_voxel_manip() local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(minp, maxp) local emin, emax = vm:read_from_map(minp, maxp)
local data = vm:get_data() local data = vm:get_data()
@ -61,31 +62,24 @@ local make_filled_map = function(itemstack, placer, pointed_thing)
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]
if c_id ~= c_air then if c_id ~= c_air then
color = color_cache[c_id] color = color_cache[c_id]
if color == nil then if color == nil then
local nodename = minetest.get_name_from_content_id(c_id) local nodename = minetest.get_name_from_content_id(c_id)
local def = minetest.registered_nodes[nodename] local def = minetest.registered_nodes[nodename]
if def then if def then
local texture local texture
if def.palette then if def.palette then
texture = def.palette texture = def.palette
elseif def.tiles then elseif def.tiles then
texture = def.tiles[1] texture = def.tiles[1]
if type(texture) == "table" then if type(texture) == "table" then
texture = texture.name texture = texture.name
end end
end end
if texture then if texture then
texture = texture:match("([^=^%^]-([^.]+))$"):split("^")[1] texture = texture:match("([^=^%^]-([^.]+))$"):split("^")[1]
end end
if def.palette then if def.palette then
local palette = palettes[texture] local palette = palettes[texture]
color = palette and {palette = palette} color = palette and {palette = palette}
@ -117,7 +111,6 @@ local make_filled_map = function(itemstack, placer, pointed_thing)
} }
end end
end end
height = map_y height = map_y
break break
end end
@ -128,13 +121,16 @@ local make_filled_map = function(itemstack, placer, pointed_thing)
end end
last_heightmap = heightmap last_heightmap = heightmap
end end
tga_encoder.image(pixels):save(texture_path) tga_encoder.image(pixels):save(texture_path)
creating_maps[texture] = false creating_maps[texture] = false
end
end) end)
return itemstack
end end
-- Turn empty map into filled map by rightclick
local make_filled_map = function(itemstack, placer, pointed_thing)
if minetest.settings:get_bool("enable_real_maps", true) then
local new_map = mcl_maps.create_map(placer:get_pos())
itemstack:take_item() itemstack:take_item()
if itemstack:is_empty() then if itemstack:is_empty() then
return new_map return new_map
@ -148,6 +144,7 @@ local make_filled_map = function(itemstack, placer, pointed_thing)
return itemstack return itemstack
end end
end end
end
minetest.register_craftitem("mcl_maps:empty_map", { minetest.register_craftitem("mcl_maps:empty_map", {
description = S("Empty Map"), description = S("Empty Map"),