forked from erle/xmaps
Compare commits
2 Commits
rail-rende
...
master
Author | SHA1 | Date |
---|---|---|
LissoBone | 4cc163e668 | |
LissoBone | b56598a477 |
195
init.lua
195
init.lua
|
@ -15,30 +15,37 @@ 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
|
|
||||||
overlap = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if overlap then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if overlap then
|
|
||||||
return
|
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
|
if color then
|
||||||
self.pixels[z + i_z][x + i_x] = { color }
|
self.pixels[z + i_z][x + i_x] = { color }
|
||||||
end
|
end
|
||||||
|
@ -65,11 +72,11 @@ 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")
|
||||||
|
@ -78,6 +85,8 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
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)
|
||||||
|
|
||||||
|
-- Size is defined above
|
||||||
|
-- I love this, it's so clever
|
||||||
local minp = vector.multiply(vector.floor(vector.divide(pos, size)), size)
|
local minp = vector.multiply(vector.floor(vector.divide(pos, size)), size)
|
||||||
meta:set_string("xmaps:minp", minetest.pos_to_string(minp))
|
meta:set_string("xmaps:minp", minetest.pos_to_string(minp))
|
||||||
|
|
||||||
|
@ -113,8 +122,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
{ 150, 105, 55 }, -- more liquid
|
{ 150, 105, 55 }, -- more liquid
|
||||||
{ 60, 35, 16 }, -- tree outline
|
{ 60, 35, 16 }, -- tree outline
|
||||||
{ 150, 105, 55 }, -- tree fill
|
{ 150, 105, 55 }, -- tree fill
|
||||||
{ 60, 35, 15 }, -- rail line
|
|
||||||
{ 60, 35, 15 }, -- rail line
|
|
||||||
}
|
}
|
||||||
for x = 1,size,1 do
|
for x = 1,size,1 do
|
||||||
for z = 1,size,1 do
|
for z = 1,size,1 do
|
||||||
|
@ -168,126 +175,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
|
|
||||||
local image = tga_encoder.image(pixels)
|
local image = tga_encoder.image(pixels)
|
||||||
|
|
||||||
local positions = minetest.find_nodes_in_area_under_air(
|
|
||||||
minp,
|
|
||||||
maxp,
|
|
||||||
"group:rail"
|
|
||||||
)
|
|
||||||
RAIL = 10
|
|
||||||
for _, p in ipairs(positions) do
|
|
||||||
local z = p.z - minp.z + 1
|
|
||||||
local x = p.x - minp.x + 1
|
|
||||||
pixels[z][x] = { RAIL }
|
|
||||||
end
|
|
||||||
-- thin out diagonal lines
|
|
||||||
for z = 1,#pixels do
|
|
||||||
for x = 1,#pixels[z] do
|
|
||||||
if (
|
|
||||||
RAIL == pixels[z][x][1] and
|
|
||||||
x < #pixels[z] -1 and
|
|
||||||
RAIL == pixels[z][x+1][1] and
|
|
||||||
not ( RAIL == pixels[z][x+2][1] )
|
|
||||||
) then
|
|
||||||
local color = 0 + ( ( x + z + 1 ) % 2 )
|
|
||||||
pixels[z][x+1] = { color }
|
|
||||||
end
|
|
||||||
if (
|
|
||||||
RAIL == pixels[z][x][1] and
|
|
||||||
z < #pixels - 1 and
|
|
||||||
RAIL == pixels[z+1][x][1] and
|
|
||||||
not ( RAIL == pixels[z+2][x][1] )
|
|
||||||
) then
|
|
||||||
local color = 0 + ( ( x + z + 1 ) % 2 )
|
|
||||||
pixels[z+1][x] = { color }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
for _, p in ipairs(positions) do
|
|
||||||
local z = p.z - minp.z + 1
|
|
||||||
local x = p.x - minp.x + 1
|
|
||||||
local draw_rail = (
|
|
||||||
z > 1 and
|
|
||||||
z < size - 1 and
|
|
||||||
x > 1 and
|
|
||||||
x < size - 1
|
|
||||||
)
|
|
||||||
if draw_rail then
|
|
||||||
local _ = { nil } -- transparent
|
|
||||||
local R = { 11 } -- line
|
|
||||||
local rail
|
|
||||||
local node_sw = minetest.get_node({
|
|
||||||
x=p.x - 1,
|
|
||||||
y=p.y,
|
|
||||||
z=p.z - 1,
|
|
||||||
})
|
|
||||||
local node_se = minetest.get_node({
|
|
||||||
x=p.x - 1,
|
|
||||||
y=p.y,
|
|
||||||
z=p.z + 1,
|
|
||||||
})
|
|
||||||
local node_nw = minetest.get_node({
|
|
||||||
x=p.x + 1,
|
|
||||||
y=p.y,
|
|
||||||
z=p.z - 1,
|
|
||||||
})
|
|
||||||
local node_ne = minetest.get_node({
|
|
||||||
x=p.x + 1,
|
|
||||||
y=p.y,
|
|
||||||
z=p.z + 1,
|
|
||||||
})
|
|
||||||
if (
|
|
||||||
minetest.get_item_group(
|
|
||||||
node_sw.name,
|
|
||||||
"rail"
|
|
||||||
) > 0 or
|
|
||||||
minetest.get_item_group(
|
|
||||||
node_se.name,
|
|
||||||
"rail"
|
|
||||||
) > 0 or
|
|
||||||
minetest.get_item_group(
|
|
||||||
node_nw.name,
|
|
||||||
"rail"
|
|
||||||
) > 0 or
|
|
||||||
minetest.get_item_group(
|
|
||||||
node_ne.name,
|
|
||||||
"rail"
|
|
||||||
) > 0
|
|
||||||
) then
|
|
||||||
rail = {
|
|
||||||
{ R, _, R },
|
|
||||||
{ _, R, _ },
|
|
||||||
{ R, _, R },
|
|
||||||
}
|
|
||||||
else
|
|
||||||
rail = {
|
|
||||||
{ _, R, _ },
|
|
||||||
{ R, R, R },
|
|
||||||
{ _, R, _ },
|
|
||||||
}
|
|
||||||
end
|
|
||||||
if 0 == ( x + z ) % 4 then
|
|
||||||
rail = {}
|
|
||||||
end
|
|
||||||
assert( rail )
|
|
||||||
image:blit_icon(
|
|
||||||
rail,
|
|
||||||
{
|
|
||||||
x = x - 2,
|
|
||||||
z = z - 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
[4] = true,
|
|
||||||
[5] = true,
|
|
||||||
[6] = true,
|
|
||||||
[7] = true,
|
|
||||||
[8] = true,
|
|
||||||
[9] = true,
|
|
||||||
[11] = true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local positions = minetest.find_nodes_in_area(
|
local positions = minetest.find_nodes_in_area(
|
||||||
minp,
|
minp,
|
||||||
maxp,
|
maxp,
|
||||||
|
@ -329,8 +216,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
[7] = true,
|
[7] = true,
|
||||||
[8] = true,
|
[8] = true,
|
||||||
[9] = true,
|
[9] = true,
|
||||||
[10] = true,
|
|
||||||
[11] = true,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -403,8 +288,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
[7] = true,
|
[7] = true,
|
||||||
[8] = true,
|
[8] = true,
|
||||||
[9] = true,
|
[9] = true,
|
||||||
[10] = true,
|
|
||||||
[11] = true,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -447,8 +330,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
[7] = true,
|
[7] = true,
|
||||||
[8] = true,
|
[8] = true,
|
||||||
[9] = true,
|
[9] = true,
|
||||||
[10] = true,
|
|
||||||
[11] = true,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -490,8 +371,6 @@ xmaps.create_map_item = function(pos, properties)
|
||||||
[7] = true,
|
[7] = true,
|
||||||
[8] = true,
|
[8] = true,
|
||||||
[9] = true,
|
[9] = true,
|
||||||
[10] = true,
|
|
||||||
[11] = true,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -641,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
|
||||||
|
@ -973,10 +852,16 @@ 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()
|
||||||
|
if protector then
|
||||||
|
if minetest.is_protected (pos, puncher) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
local itemstring = self._itemstring
|
local itemstring = self._itemstring
|
||||||
|
|
||||||
if pos and itemstring then
|
if pos and itemstring then
|
||||||
minetest.add_item(
|
minetest.add_item(
|
||||||
pos,
|
pos,
|
||||||
|
@ -995,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
|
||||||
|
|
||||||
|
@ -1005,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,
|
||||||
|
@ -1058,6 +946,10 @@ if minetest.registered_items["map:mapping_kit"] then
|
||||||
{
|
{
|
||||||
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,
|
||||||
|
@ -1069,6 +961,7 @@ 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)
|
local map = xmaps.create_map_item(pos)
|
||||||
return map
|
return map
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue