Skip to content

Commit

Permalink
add isogen.draw_map(pos1, pos2) (#3)
Browse files Browse the repository at this point in the history
* add `isogen.draw_map(pos1, pos2)`

* draw_map test

---------

Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
  • Loading branch information
BuckarooBanzay and BuckarooBanzay committed May 23, 2024
1 parent b532b1f commit ee35b4e
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
23 changes: 23 additions & 0 deletions chatcommand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ worldedit.register_command("isogen", {
local path = minetest.get_worldpath() .. "/" .. filename .. ".png"
minetest.safe_file_write(path, png)

return true, "png saved: " .. #png .. " bytes"
end
})

worldedit.register_command("isogen_map", {
params = "[filename]",
description = "Generate a map image of the world region",
privs = {worldedit=true},
require_pos = 2,
parse = function(param)
return true, param
end,
func = function(name, filename)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
if not filename or filename == "" then
return false, "please specify a filename"
end

minetest.load_area(pos1, pos2)
local png = isogen.draw_map(pos1, pos2)
local path = minetest.get_worldpath() .. "/" .. filename .. ".png"
minetest.safe_file_write(path, png)

return true, "png saved: " .. #png .. " bytes"
end
})
5 changes: 5 additions & 0 deletions common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function isogen.probe_position(min, max, pos, ipos, list)
order = order,
node = node
})

if not color.a or color.a == 255 then
-- solid color
break
end
end
pos = vector.add(pos, ipos)
end
Expand Down
45 changes: 45 additions & 0 deletions draw_map.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

function isogen.draw_map(pos1, pos2)
pos1, pos2 = vector.sort(pos1, pos2)

minetest.load_area(pos1, pos2)

local height = pos2.z - pos1.z + 1
local width = pos2.x - pos1.x + 1

local canvas = isogen.create_canvas(width, height)

-- left-right
for x=pos1.x,pos2.x do
-- top-down
for z=pos1.z,pos2.z do
local list = {}

-- up-down
for y=pos2.y,pos1.y,-1 do
local pos = vector.new(x, y, z)
local node = minetest.get_node(pos)
local color = isogen.get_color(node)
if color then
table.insert(list, color)
if not color.a or color.a == 255 then
break
end
end
end

-- draw from bottom-up (transparency)
if #list > 0 then
local pos = vector.new(x, 0, z)
for i=#list,1,-1 do
local rel_pos = vector.subtract(pos, pos1)
local cx = width - rel_pos.x - 1 -- inverted x axis
local cy = rel_pos.z
canvas:add_pixel(cx, cy, list[i])
end
end
end
end

return canvas:png()
end
27 changes: 27 additions & 0 deletions draw_map.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local pos1 = vector.new(200,100,100)
local pos2 = vector.add(pos1, 15)

mtt.emerge_area(pos1, pos2)

mtt.register("draw_map", function(callback)
minetest.load_area(pos1, pos2)

minetest.set_node(pos1, { name = "mapgen_stone" })
minetest.set_node(vector.add(pos1, vector.new(1, 0, 0)), { name = "mapgen_stone" })
minetest.set_node(vector.add(pos1, vector.new(2, 0, 0)), { name = "mapgen_stone" })
minetest.set_node(vector.add(pos1, vector.new(0, 0, 1)), { name = "mapgen_water_source" })
minetest.set_node(vector.add(pos1, vector.new(1, 0, 1)), { name = "mapgen_water_source" })

-- sanity tests
local node = minetest.get_node(pos1)
assert(node.name ~= "air")
assert(node.name ~= "ignore")

local png = isogen.draw_map(pos1, pos2)
local path = minetest.get_worldpath() .. "/test_map.png"
minetest.safe_file_write(path, png)

print("png saved: " .. #png .. " bytes")

callback()
end)
2 changes: 2 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dofile(MP.."/common.lua")
dofile(MP.."/colors.lua")
dofile(MP.."/canvas.lua")
dofile(MP.."/draw_cube.lua")
dofile(MP.."/draw_map.lua")
dofile(MP.."/draw.lua")

if minetest.get_modpath("worldedit_commands") then
Expand All @@ -15,5 +16,6 @@ if minetest.get_modpath("mtt") and mtt.enabled then
dofile(MP.."/common.spec.lua")
dofile(MP.."/colors.spec.lua")
dofile(MP.."/draw_cube.spec.lua")
dofile(MP.."/draw_map.spec.lua")
dofile(MP.."/draw.spec.lua")
end
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ local path = minetest.get_worldpath() .. "/iso.png"
minetest.safe_file_write(path, png)
```

## `isogen.draw_map(pos1, pos2)`

Renders a map image and returns the png data

Example:
```lua
local pos1 = vector.new(0,0,0)
local pos2 = vector.new(16,16,16) -- NOTE: larger regions require more memory

-- render and save to world-directory
local png = isogen.draw_map(pos1, pos2)
local path = minetest.get_worldpath() .. "/map.png"
minetest.safe_file_write(path, png)
```

## `isogen.create_canvas(width, height)`

Creates a canvas to set and get pixels
Expand Down

0 comments on commit ee35b4e

Please sign in to comment.