474 lines
16 KiB
Lua
Raw Normal View History

-- If true, the Perlin test nodes will support color
-- (set to false in case of performance problems)
local COLORIZE_NODES = true
2022-04-09 18:25:27 +02:00
local S = minetest.get_translator("perlin_explorer")
2022-04-10 01:45:19 +02:00
local F = minetest.formspec_escape
2022-04-09 18:23:01 +02:00
-- Holds the currently used Perlin noise
local current_perlin = {}
-- holds the current PerlinNoise object
current_perlin.noise = nil
2022-04-10 01:54:20 +02:00
current_perlin.noiseparams = {
offset = 0.0,
scale = 1.0,
spread = vector.new(10, 10, 10),
seed = 0,
octaves = 2,
persistence = 0.7,
lacunarity = 2.0,
}
2022-04-10 00:33:16 +02:00
-- Side length of calculated perlin area
current_perlin.size = nil
2022-04-09 18:23:01 +02:00
-- Theoretical min and max values for Perlin noise (for colorization)
current_perlin.max = 1
current_perlin.min = -1
2022-04-09 21:43:33 +02:00
-- dimensions of current Perlin noise (2 or 3)
current_perlin.dimensions = 2
-- If greater than 1, the Perlin noise values are "pixelized". Noise values at
-- coordinates not divisible by sidelen will be set equal to the noise value
-- of the nearest number (counting downwards) that is divisible by sidelen.
-- This is (kind of) analogous to the "sidelen" parameter of mapgen decorations.
current_perlin.sidelen = 1
------------
2022-04-09 21:43:33 +02:00
-- Helper functions
-- Reduce the pos coordinates down to the closest numbers divisible by sidelen
local sidelen_pos = function(pos, sidelen)
local newpos = table.copy(pos)
if sidelen <= 1 then
return newpos
end
2022-04-09 23:11:58 +02:00
newpos.x = newpos.x - newpos.x % sidelen
newpos.y = newpos.y - newpos.y % sidelen
newpos.z = newpos.z - newpos.z % sidelen
2022-04-09 21:43:33 +02:00
return newpos
end
2022-04-09 18:23:01 +02:00
-- Test nodes to generate a map based on a perlin noise
local paramtype2
if COLORIZE_NODES then
paramtype2 = "color"
end
2022-04-09 18:25:27 +02:00
minetest.register_node("perlin_explorer:node", {
2022-04-09 18:23:01 +02:00
description = S("Perlin Test Node"),
2022-04-10 01:54:20 +02:00
paramtype = "light",
sunlight_propagates = true,
paramtype2 = paramtype2,
2022-04-09 18:25:27 +02:00
tiles = {"perlin_explorer_node.png"},
palette = "perlin_explorer_node_palette.png",
2022-04-09 18:23:01 +02:00
groups = { dig_immediate = 3 },
-- Force-drop without metadata to avoid spamming the inventory
2022-04-09 18:25:27 +02:00
drop = "perlin_explorer:node",
2022-04-09 18:23:01 +02:00
})
2022-04-09 18:25:27 +02:00
minetest.register_node("perlin_explorer:node_negative", {
2022-04-09 18:23:01 +02:00
description = S("Negative Perlin Test Node"),
2022-04-10 01:54:20 +02:00
paramtype = "light",
sunlight_propagates = true,
paramtype2 = paramtype2,
2022-04-09 18:25:27 +02:00
tiles = {"perlin_explorer_node_neg.png"},
palette = "perlin_explorer_node_palette_neg.png",
2022-04-09 18:23:01 +02:00
groups = { dig_immediate = 3 },
-- Force-drop without metadata to avoid spamming the inventory
2022-04-09 18:25:27 +02:00
drop = "perlin_explorer:node_negative",
2022-04-09 18:23:01 +02:00
})
2022-04-09 20:39:15 +02:00
minetest.register_tool("perlin_explorer:getter", {
description = S("Perlin Value Getter"),
_tt_help = S("Punch a node to display the Perlin noise value at this position"),
inventory_image = "perlin_explorer_getter.png",
wield_image = "perlin_explorer_getter.png",
groups = { disable_repair = 1 },
on_use = function(itemstack, user, pointed_thing)
2022-04-10 01:45:19 +02:00
if not user then
return
end
local privs = minetest.get_player_privs(user:get_player_name())
if not privs.server then
2022-04-10 01:54:20 +02:00
minetest.chat_send_player(user:get_player_name(), S("Insufficient privileges! You need the @1 privilege to use this tool.", "server"))
return
2022-04-10 01:45:19 +02:00
end
2022-04-09 20:39:15 +02:00
if current_perlin.noise then
if pointed_thing.type ~= "node" then
-- No-op for non-nodes
return
end
local pos = pointed_thing.under
2022-04-09 21:43:33 +02:00
local getpos = sidelen_pos(pos, current_perlin.sidelen)
2022-04-09 20:39:15 +02:00
local val
if current_perlin.dimensions == 2 then
2022-04-09 21:43:33 +02:00
val = current_perlin.noise:get_2d({x=getpos.x, y=getpos.z})
2022-04-09 21:00:12 +02:00
elseif current_perlin.dimensions == 3 then
2022-04-09 21:43:33 +02:00
val = current_perlin.noise:get_3d(getpos)
2022-04-09 21:00:12 +02:00
else
2022-04-09 21:43:33 +02:00
minetest.chat_send_player(user:get_player_name(), S("Unknown/invalid number of Perlin noise dimensions. Use /generate_perlin first!"))
2022-04-09 20:39:15 +02:00
end
minetest.chat_send_player(user:get_player_name(), S("pos=@1, value=@2", minetest.pos_to_string(pos), val))
else
2022-04-10 01:45:19 +02:00
local msg = S("No Perlin noise set. Set one with /set_perlin_noise!")
2022-04-09 20:39:15 +02:00
minetest.chat_send_player(user:get_player_name(), msg)
end
end,
})
2022-04-09 18:25:27 +02:00
local CONTENT_TEST_NODE = minetest.get_content_id("perlin_explorer:node")
local CONTENT_TEST_NODE_NEGATIVE = minetest.get_content_id("perlin_explorer:node_negative")
2022-04-09 18:23:01 +02:00
2022-04-09 23:17:35 +02:00
local update_map = function(pos, set_nodes)
2022-04-09 18:23:01 +02:00
local stats
if not current_perlin.noise then
return
end
2022-04-10 00:33:16 +02:00
local size_v = vector.new(current_perlin.size, current_perlin.size, current_perlin.size)
2022-04-09 18:23:01 +02:00
local startpos = pos
2022-04-10 00:33:16 +02:00
local endpos = vector.add(startpos, current_perlin.size-1)
2022-04-09 18:23:01 +02:00
local y_max = endpos.y - startpos.y
if current_perlin.dimensions == 2 then
y_max = 0
startpos.y = pos.y
endpos.y = pos.y
end
2022-04-09 23:17:35 +02:00
local vmanip, emin, emax, vdata, vdata2, varea
if set_nodes then
vmanip = VoxelManip(startpos, endpos)
emin, emax = vmanip:get_emerged_area()
vdata = vmanip:get_data()
vdata2 = vmanip:get_param2_data()
varea = VoxelArea:new({MinEdge = emin, MaxEdge = emax})
end
2022-04-09 21:43:33 +02:00
2022-04-09 18:23:01 +02:00
stats = {}
stats.avg = 0
local sum_of_values = 0
local value_count = 0
for x=0, endpos.x - startpos.x do
for y=0, y_max do
for z=0, endpos.z - startpos.z do
-- Get Perlin value at current pos
local relpos = vector.new(x,y,z)
local abspos = vector.add(startpos, relpos)
2022-04-09 21:43:33 +02:00
local abspos_get = sidelen_pos(abspos, current_perlin.sidelen)
2022-04-09 18:23:01 +02:00
local perlin_value
if current_perlin.dimensions == 2 then
2022-04-09 21:43:33 +02:00
perlin_value = current_perlin.noise:get_2d({x=abspos_get.x, y=abspos_get.z})
2022-04-09 18:23:01 +02:00
elseif current_perlin.dimensions == 3 then
2022-04-09 21:43:33 +02:00
perlin_value = current_perlin.noise:get_3d(abspos_get)
2022-04-09 18:23:01 +02:00
else
2022-04-09 18:25:27 +02:00
error("[perlin_explorer] Unknown/invalid number of Perlin noise dimensions!")
2022-04-09 18:23:01 +02:00
return
end
-- Statistics
if not stats.min then
stats.min = perlin_value
elseif perlin_value < stats.min then
stats.min = perlin_value
end
if not stats.max then
stats.max = perlin_value
elseif perlin_value > stats.max then
stats.max = perlin_value
end
sum_of_values = sum_of_values + perlin_value
value_count = value_count + 1
-- Calculate color (param2) for node
2022-04-09 22:48:27 +02:00
local zeropoint = 0
local min_size = zeropoint - current_perlin.min
local max_size = current_perlin.max - zeropoint
local node_param2
if perlin_value >= zeropoint then
node_param2 = (math.abs(perlin_value) / max_size) * 255
else
node_param2 = (math.abs(perlin_value) / min_size) * 255
end
node_param2 = math.floor(math.abs(node_param2))
2022-04-09 18:23:01 +02:00
node_param2 = math.max(0, math.min(255, node_param2))
2022-04-09 23:17:35 +02:00
if set_nodes then
-- Get vmanip index
local index = varea:indexp(abspos)
if not index then
return
end
2022-04-09 18:23:01 +02:00
2022-04-09 23:17:35 +02:00
-- Set node and param2
if perlin_value >= zeropoint then
vdata[index] = CONTENT_TEST_NODE
2022-04-09 18:23:01 +02:00
vdata2[index] = node_param2
else
2022-04-09 23:17:35 +02:00
if current_perlin.show_negative == true then
vdata[index] = CONTENT_TEST_NODE_NEGATIVE
vdata2[index] = node_param2
else
vdata[index] = minetest.CONTENT_AIR
vdata2[index] = 0
end
2022-04-09 18:23:01 +02:00
end
end
end
end
end
stats.avg = sum_of_values / value_count
2022-04-09 23:17:35 +02:00
if set_nodes then
-- Set vmanip, return stats
vmanip:set_data(vdata)
vmanip:set_param2_data(vdata2)
vmanip:write_to_map()
end
2022-04-09 18:23:01 +02:00
return stats
end
-- Creates and demonstrates a Perlin noise.
-- * pos: Where the Perlin noise starts
-- * options: table with:
-- * dimensions: number of Perlin noise dimensions (2 or 3)
2022-04-10 00:33:16 +02:00
-- * size: side length of area/volume to calculate)
2022-04-09 19:44:15 +02:00
-- * show_negative: if true, places nodes for negative Perlin values (default: true for 2 dimensions and false for 3 dimensions)
2022-04-09 23:17:35 +02:00
-- * set_nodes: if true, will set nodes, otherwise it's a "dry run"
local create_perlin = function(pos, options)
2022-04-09 19:44:15 +02:00
if not current_perlin.noise then
return false
end
2022-04-09 18:23:01 +02:00
current_perlin.dimensions = options.dimensions
2022-04-10 00:33:16 +02:00
current_perlin.size = options.size
2022-04-09 18:23:01 +02:00
current_perlin.show_negative = options.show_negative
if current_perlin.show_negative == nil then
2022-04-09 19:44:15 +02:00
if current_perlin.dimensions == 2 then
2022-04-09 20:39:15 +02:00
current_perlin.show_negative = true
2022-04-09 19:44:15 +02:00
elseif current_perlin.dimensions == 3 then
2022-04-09 20:39:15 +02:00
current_perlin.show_negative = false
2022-04-09 19:44:15 +02:00
end
2022-04-09 18:23:01 +02:00
end
local mpos = vector.round(pos)
2022-04-09 23:17:35 +02:00
local set_nodes = options.set_nodes ~= false
local stats = update_map(mpos, set_nodes)
2022-04-09 18:23:01 +02:00
if stats then
2022-04-09 19:44:15 +02:00
return string.format("Perlin noise created! Stats: min. value=%.3f, max. value=%.3f, avg. value=%.3f", stats.min, stats.max, stats.avg)
2022-04-09 18:23:01 +02:00
end
end
2022-04-09 19:44:15 +02:00
-- Sets the currently active Perlin noise.
-- * noiseparams: NoiseParams table (see Minetest's Lua API documentation)
2022-04-09 23:17:35 +02:00
local set_perlin_noise = function(noiseparams)
2022-04-09 19:44:15 +02:00
current_perlin.noise = PerlinNoise(noiseparams)
2022-04-10 01:45:19 +02:00
current_perlin.noiseparams = noiseparams
2022-04-09 19:44:15 +02:00
end
2022-04-09 21:43:33 +02:00
minetest.register_chatcommand("perlin_set_options", {
privs = { server = true },
description = S("Set Perlin options"),
2022-04-09 22:48:27 +02:00
params = S("<dimensions> <sidelen> <minval> <maxval>"),
2022-04-09 21:43:33 +02:00
func = function(name, param)
2022-04-09 22:48:27 +02:00
local dimensions, sidelen, min, max = string.match(param, "([23]) ([0-9]+) ([0-9.-]+) ([0-9.-]+)")
if not dimensions then
2022-04-09 21:43:33 +02:00
return false
end
dimensions = tonumber(dimensions)
sidelen = tonumber(sidelen)
2022-04-09 22:48:27 +02:00
min = tonumber(min)
max = tonumber(max)
if not dimensions or not sidelen or not min or not max then
2022-04-09 21:43:33 +02:00
return false, S("Invalid parameter type.")
end
current_perlin.dimensions = dimensions
current_perlin.sidelen = sidelen
2022-04-09 22:48:27 +02:00
current_perlin.min = min
current_perlin.max = max
2022-04-09 21:43:33 +02:00
return true, S("Perlin options set!")
end,
})
2022-04-09 23:17:35 +02:00
2022-04-09 21:43:33 +02:00
minetest.register_chatcommand("perlin_set_noise", {
2022-04-09 19:44:15 +02:00
privs = { server = true },
description = S("Set Perlin noise parameters"),
params = S("<octaves> <offset> <scale> <spread_x> <spread_y> <spread_z> <persistence> <lacunarity> <seed>"),
func = function(name, param)
local octaves, offset, scale, sx, sy, sz, persistence, lacunarity, seed = string.match(param, string.rep("([0-9.-]+) ", 8) .. "([0-9.-]+)")
if not octaves then
return false
end
octaves = tonumber(octaves)
offset = tonumber(offset)
sx = tonumber(sx)
sy = tonumber(sy)
sz = tonumber(sz)
persistence = tonumber(persistence)
lacunarity = tonumber(lacunarity)
seed = tonumber(seed)
if not octaves or not offset or not sx or not sy or not sz or not persistence or not lacunarity or not seed then
2022-04-09 21:43:33 +02:00
return false, S("Invalid parameter type.")
2022-04-09 19:44:15 +02:00
end
set_perlin_noise({
octaves = octaves,
offset = offset,
scale = scale,
spread = { x = sx, y = sy, z = sz },
persistence = persistence,
lacunarity = lacunarity,
seed = seed,
})
return true, S("Perlin noise set!")
end,
})
minetest.register_chatcommand("perlin_generate", {
privs = { server = true },
description = S("Generate Perlin noise"),
2022-04-10 00:33:16 +02:00
params = S("<pos> <size>"),
2022-04-09 19:44:15 +02:00
func = function(name, param)
2022-04-10 00:33:16 +02:00
local x, y, z, size = string.match(param, "([0-9.-]+) ([0-9.-]+) ([0-9.-]+) ([0-9]+)")
2022-04-09 20:39:15 +02:00
if not x then
2022-04-09 19:44:15 +02:00
return false
end
x = tonumber(x)
y = tonumber(y)
z = tonumber(z)
2022-04-10 00:33:16 +02:00
size = tonumber(size)
if not x or not y or not z or not size then
2022-04-09 19:44:15 +02:00
return false
end
2022-04-10 00:33:16 +02:00
if not x or not y or not z or not size then
2022-04-09 19:44:15 +02:00
return false, S("Invalid parameter type.")
end
local pos = vector.new(x, y, z)
minetest.chat_send_player(name, S("Creating Perlin noise, please wait …"))
2022-04-10 00:33:16 +02:00
local msg = create_perlin(pos, {dimensions=current_perlin.dimensions, size=size})
2022-04-09 19:44:15 +02:00
if msg == false then
2022-04-09 21:43:33 +02:00
return false, S("No Perlin noise set. Set one with '/perlin_set_noise' first!")
2022-04-09 19:44:15 +02:00
end
return true, msg
end,
})
2022-04-10 01:45:19 +02:00
local show_formspec = function(player)
local offset = tostring(current_perlin.noiseparams.offset or "")
local scale = tostring(current_perlin.noiseparams.scale or "")
local seed = tostring(current_perlin.noiseparams.seed or "")
local sx, sy, sz = "", "", ""
if current_perlin.noiseparams.spread then
sx = tostring(current_perlin.noiseparams.spread.x or "")
sy = tostring(current_perlin.noiseparams.spread.y or "")
sz = tostring(current_perlin.noiseparams.spread.z or "")
end
local octaves = tostring(current_perlin.noiseparams.octaves or "")
local persistence = tostring(current_perlin.noiseparams.persistence or "")
local lacunarity = tostring(current_perlin.noiseparams.lacunarity or "")
-- TODO: Add pos
-- TODO: Add noise options
2022-04-10 23:41:41 +02:00
local dimensions_index = (current_perlin.dimensions or 2) - 1
2022-04-10 01:45:19 +02:00
local form = [[
formspec_version[4]size[10,10]
field[0.5,0.5;3,0.75;offset;]]..F(S("Offset"))..[[;]]..offset..[[]
field[3.5,0.5;3,0.75;scale;]]..F(S("Scale"))..[[;]]..scale..[[]
field[6.5,0.5;3,0.75;seed;]]..F(S("Seed"))..[[;]]..seed..[[]
field[0.5,1.75;3,0.75;spread_x;]]..F(S("X Spread"))..[[;]]..sx..[[]
field[3.5,1.75;3,0.75;spread_y;]]..F(S("Y Spread"))..[[;]]..sy..[[]
field[6.5,1.75;3,0.75;spread_z;]]..F(S("Z Spread"))..[[;]]..sz..[[]
field[0.5,3;3,0.75;octaves;]]..F(S("Octaves"))..[[;]]..octaves..[[]
field[3.5,3;3,0.75;persistence;]]..F(S("Persistence"))..[[;]]..persistence..[[]
field[6.5,3;3,0.75;lacunarity;]]..F(S("Lacunarity"))..[[;]]..lacunarity..[[]
field_close_on_enter[offset;false]
field_close_on_enter[scale;false]
field_close_on_enter[seed;false]
field_close_on_enter[spread_x;false]
field_close_on_enter[spread_y;false]
field_close_on_enter[spread_z;false]
field_close_on_enter[octaves;false]
field_close_on_enter[persistence;false]
field_close_on_enter[lacunarity;false]
label[0.5,4.5;]]..F(S("Dimensions"))..[[]
2022-04-10 23:41:41 +02:00
dropdown[3.5,4.5;3;dimensions;]]..F(S("2D"))..[[,]]..F(S("3D"))..[[;]]..dimensions_index..[[;true]
2022-04-10 01:45:19 +02:00
button[0.5,8.5;3,1;apply;]]..F(S("Apply"))..[[]
button[3.5,8.5;3,1;create;]]..F(S("Apply and create"))..[[]
button_exit[6.5,8.5;3,1;close;]]..F(S("Close"))..[[]
]]
minetest.show_formspec(player:get_player_name(), "perlin_explorer:creator", form)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "perlin_explorer:creator" then
return
end
2022-04-10 01:46:30 +02:00
-- Require 'server' priv
local privs = minetest.get_player_privs(player:get_player_name())
if not privs.server then
return
end
2022-04-10 01:45:19 +02:00
local do_apply = fields.apply ~= nil
local do_create = fields.create ~= nil or fields.key_enter_field ~= nil
if (do_create or do_apply) then
if fields.offset and fields.scale and fields.seed and fields.spread_x and fields.spread_y and fields.spread_z and fields.octaves and fields.persistence and fields.lacunarity then
local offset = tonumber(fields.offset)
local scale = tonumber(fields.scale)
local seed = tonumber(fields.seed)
local sx = tonumber(fields.spread_x)
local sy = tonumber(fields.spread_y)
local sz = tonumber(fields.spread_z)
2022-04-10 03:33:22 +02:00
if not sx or not sy or not sz then
return
end
2022-04-10 01:45:19 +02:00
local spread = vector.new(sx, sy, sz)
local octaves = tonumber(fields.octaves)
local persistence = tonumber(fields.persistence)
local lacunarity = tonumber(fields.lacunarity)
2022-04-10 23:41:41 +02:00
local dimensions = tonumber(fields.dimensions)
2022-04-10 03:33:22 +02:00
if (offset and scale and spread and octaves and persistence and dimensions) then
2022-04-10 23:41:41 +02:00
-- Convert dropdown index to actual dimensions number
dimensions = dimensions + 1
-- Spread is used differently in 2D
2022-04-10 03:33:22 +02:00
if dimensions == 2 then
spread.y = spread.z
end
2022-04-10 01:45:19 +02:00
set_perlin_noise({
offset = offset,
scale = scale,
seed = seed,
spread = spread,
octaves = octaves,
persistence = persistence,
lacunarity = lacunarity,
})
if do_create then
local pos = player:get_pos()
pos.y = pos.y - 10
local msg = create_perlin(pos, {dimensions=dimensions, size=64})
if msg == false then
minetest.log("error", "[perlin_explorer] Error generating Perlin noise nodes!")
end
end
end
end
end
end)
minetest.register_tool("perlin_explorer:creator", {
description = S("Perlin Noise Creator"),
_tt_help = S("Punch to open the Perlin noise creation menu"),
inventory_image = "perlin_explorer_creator.png",
wield_image = "perlin_explorer_creator.png",
groups = { disable_repair = 1 },
on_use = function(itemstack, user, pointed_thing)
if not user then
return
end
local privs = minetest.get_player_privs(user:get_player_name())
if not privs.server then
2022-04-10 01:54:20 +02:00
minetest.chat_send_player(user:get_player_name(), S("Insufficient privileges! You need the @1 privilege to use this tool.", "server"))
return
2022-04-10 01:45:19 +02:00
end
show_formspec(user)
end,
})
2022-04-09 19:44:15 +02:00