Compare commits

...

2 Commits

Author SHA1 Message Date
LissoBone 4cc163e668 Added protector optional dependency. 2023-09-18 16:21:53 +09:00
LissoBone b56598a477 Slightly improved first two functions in init.lua, added protection support. 2023-09-18 15:54:40 +09:00
2 changed files with 77 additions and 54 deletions

129
init.lua
View File

@ -15,34 +15,41 @@ Codezeilen und schreibe das auch nicht dran.
]]-- ]]--
local protector = minetest.get_modpath ("protector")
-- Detect stopping colors.
function tga_encoder.image:detect_stop_colors(icon, pos, stop_colors)
local x = pos.x
local z = pos.z
for i_z = 1,#icon do
for i_x = 1,#icon[i_z] do
if stop_colors[self.pixels[z + i_z][x + i_x][1]] then
return true
end
end
end
end
-- blit an icon into the image unless its rect overlaps pixels that -- blit an icon into the image unless its rect overlaps pixels that
-- have any of the stop_colors, treating a nil pixel as transparent -- have any of the stop_colors, treating a nil pixel as transparent
function tga_encoder.image:blit_icon(icon, pos, stop_colors) function tga_encoder.image:blit_icon(icon, pos, stop_colors)
local x = pos.x local x = pos.x
local z = pos.z local z = pos.z
local overlap = false
for i_z = 1,#icon do -- Just as the comment above says, exit IMMEDIATELY if there
for i_x = 1,#icon[i_z] do -- are stop_colors present in the image.
local color = self.pixels[z + i_z][x + i_x][1] if self:detect_stop_colors(icon, pos, stop_colors) then
if stop_colors[color] then return
overlap = true
break
end
end
if overlap then
break
end
end
if overlap then
return
end end
for i_z = 1,#icon do for i_z = 1,#icon do
for i_x = 1,#icon[i_z] do for i_x = 1,#icon[i_z] do
local color = icon[i_z][i_x][1] local color = icon[i_z][i_x][1]
if color then
self.pixels[z + i_z][x + i_x] = { color } if color then
end self.pixels[z + i_z][x + i_x] = { color }
end end
end
end end
end end
@ -65,24 +72,26 @@ local worldpath = minetest.get_worldpath()
local textures_dir = worldpath .. "/xmaps/" local textures_dir = worldpath .. "/xmaps/"
minetest.mkdir(textures_dir) minetest.mkdir(textures_dir)
xmaps.get_map_filename = function(map_id) function xmaps.get_map_filename(map_id)
return "xmaps_map_texture_" .. map_id .. ".tga" return "xmaps_map_texture_" .. map_id .. ".tga"
end end
xmaps.create_map_item = function(pos, properties) function xmaps.create_map_item(pos, properties)
properties = properties or {} properties = properties or {}
local itemstack = ItemStack("xmaps:map") local itemstack = ItemStack("xmaps:map")
local meta = itemstack:get_meta() local meta = itemstack:get_meta()
local map_id = tostring(os.time() + math.random()) local map_id = tostring(os.time() + math.random())
meta:set_string("xmaps:id", map_id) meta:set_string("xmaps:id", map_id)
local minp = vector.multiply(vector.floor(vector.divide(pos, size)), size) -- Size is defined above
meta:set_string("xmaps:minp", minetest.pos_to_string(minp)) -- I love this, it's so clever
local minp = vector.multiply(vector.floor(vector.divide(pos, size)), size)
local maxp = vector.add(minp, vector.new(size - 1, size - 1, size - 1)) meta:set_string("xmaps:minp", minetest.pos_to_string(minp))
meta:set_string("xmaps:maxp", minetest.pos_to_string(maxp))
local maxp = vector.add(minp, vector.new(size - 1, size - 1, size - 1))
meta:set_string("xmaps:maxp", minetest.pos_to_string(maxp))
if properties.draw_x then if properties.draw_x then
local xpos = vector.round(pos) local xpos = vector.round(pos)
@ -511,7 +520,7 @@ xmaps.load_map_item = function(itemstack)
) )
end end
else else
-- no texture file could be a world download -- no texture file -> could be a world download
-- so we look for missing texture in item meta -- so we look for missing texture in item meta
-- and write that to the map texture file here -- and write that to the map texture file here
if texture_item_meta_exists then if texture_item_meta_exists then
@ -843,17 +852,23 @@ minetest.register_entity(
} }
) )
end, end,
on_punch = function(self) on_punch = function(self, puncher)
-- TODO: implement protection -- Protection implemented.
local pos = self.object:get_pos() local pos = self.object:get_pos()
local itemstring = self._itemstring if protector then
if pos and itemstring then if minetest.is_protected (pos, puncher) then
minetest.add_item( return
pos, end
itemstring end
) local itemstring = self._itemstring
self.object:remove()
end if pos and itemstring then
minetest.add_item(
pos,
itemstring
)
self.object:remove()
end
end end
} }
) )
@ -865,7 +880,7 @@ minetest.register_craftitem(
inventory_image = "xmaps_map.tga", inventory_image = "xmaps_map.tga",
groups = { not_in_creative_inventory = 1 }, groups = { not_in_creative_inventory = 1 },
on_place = function(itemstack, player, pointed_thing) on_place = function(itemstack, player, pointed_thing)
if "node" ~= pointed_thing.type then if pointed_thing.type ~= "node" then
return return
end end
@ -875,7 +890,10 @@ minetest.register_craftitem(
end end
local node_pos = pointed_thing.under local node_pos = pointed_thing.under
if protector and minetest.is_protected (pointed_thing.above, player)
then
return
end
local direction = vector.normalize( local direction = vector.normalize(
vector.subtract( vector.subtract(
node_pos, node_pos,
@ -927,7 +945,11 @@ if minetest.registered_items["map:mapping_kit"] then
"map:mapping_kit", "map:mapping_kit",
{ {
on_place = function(itemstack, player, pointed_thing) on_place = function(itemstack, player, pointed_thing)
local pos = pointed_thing.under local pos = pointed_thing.under
if protector and minetest.is_protected (pointed_thing.above, player)
then
return
end
if pos then if pos then
local map = xmaps.create_map_item( local map = xmaps.create_map_item(
pos, pos,
@ -939,8 +961,9 @@ if minetest.registered_items["map:mapping_kit"] then
on_secondary_use = function(itemstack, player, pointed_thing) on_secondary_use = function(itemstack, player, pointed_thing)
local pos = player:get_pos() local pos = player:get_pos()
if pos then if pos then
local map = xmaps.create_map_item(pos)
return map local map = xmaps.create_map_item(pos)
return map
end end
end, end,
} }

View File

@ -1,4 +1,4 @@
depends = tga_encoder depends = tga_encoder
optional_depends = map optional_depends = map, protector
description = Adds map items that show terrain in HUD description = Adds map items that show terrain in HUD
name = xmaps name = xmaps