Refactor map icon blitting

This commit is contained in:
Nils Dagsson Moskopp 2022-05-19 03:19:25 +02:00
parent af983dbe38
commit 9dea792ca8
Signed by untrusted user who does not match committer: erlehmann
GPG Key ID: A3BC671C35191080
1 changed files with 65 additions and 41 deletions

106
init.lua
View File

@ -15,6 +15,37 @@ Codezeilen und schreibe das auch nicht dran.
]]--
-- blit an icon into the image unless its rect overlaps pixels that
-- have any of the stop_colors, treating a nil pixel as transparent
function tga_encoder.image:blit_icon(icon, pos, stop_colors)
local x = pos.x
local z = pos.z
local overlap = false
for i_z = 1,#icon do
for i_x = 1,#icon[i_z] do
local color = self.pixels[z + i_z][x + i_x][1]
if stop_colors[color] then
overlap = true
break
end
end
if overlap then
break
end
end
if overlap then
return
end
for i_z = 1,#icon do
for i_x = 1,#icon[i_z] do
local color = icon[i_z][i_x][1]
if color then
self.pixels[z + i_z][x + i_x] = { color }
end
end
end
end
maps = {}
maps.dots = {}
maps.maps = {}
@ -134,6 +165,8 @@ maps.create_map = function(pos, player_name)
end
end
local image = tga_encoder.image(pixels)
local positions = minetest.find_nodes_in_area_under_air(
minp,
maxp,
@ -161,58 +194,49 @@ maps.create_map = function(pos, player_name)
)
if draw_tree then
local tree = {}
local _ = { nil } -- transparent
local O = { 7 } -- outline
local F = { 8 } -- filling
if nil ~= node.name:find("pine") then
tree = {
" # ",
"#####",
"#...#",
" #.# ",
" #.# ",
" # ",
{ _, _, O, _, _ },
{ O, O, O, O, O },
{ O, F, F, F, O },
{ _, O, F, O, _ },
{ _, O, F, O, _ },
{ _, _, O, _, _ },
}
else
tree = {
" # ",
" # ",
" ### ",
"#...#",
"#...#",
"#...#",
" ### ",
{ _, _, O, _, _ },
{ _, _, O, _, _ },
{ _, O, O, O, _ },
{ O, F, F, F, O },
{ O, F, F, F, O },
{ O, F, F, F, O },
{ _, O, O, O, _ },
}
end
local overlap = false
for t_z = 1,#tree do
for t_x = 1,#tree[t_z] do
local color = pixels[z + t_z][x + t_x - 3][1]
-- do not draw trees close to trees or water
if color > 2 then
overlap = true
break
end
end
if overlap then
break
end
end
if overlap then
tree = {}
end
for t_z = 1,#tree do
for t_x = 1,#tree[t_z] do
local byte = tree[t_z]:byte(t_x)
if 35 == byte then -- #
pixels[z + t_z][x + t_x - 3] = { 7 }
elseif 46 == byte then -- .
pixels[z + t_z][x + t_x - 3] = { 8 }
end
end
end
image:blit_icon(
tree,
{
x = x - 3,
z = z
},
{
[3] = true,
[4] = true,
[5] = true,
[6] = true,
[7] = true,
[8] = true
}
)
end
end
local filepath = textures_dir .. filename
tga_encoder.image(pixels):save(
image:save(
filepath,
{ colormap=colormap }
)