Add basic Perlin noise creator tool
This commit is contained in:
parent
2e85769e3f
commit
a9e6ebb2d7
120
init.lua
120
init.lua
@ -3,11 +3,13 @@
|
|||||||
local COLORIZE_NODES = true
|
local COLORIZE_NODES = true
|
||||||
|
|
||||||
local S = minetest.get_translator("perlin_explorer")
|
local S = minetest.get_translator("perlin_explorer")
|
||||||
|
local F = minetest.formspec_escape
|
||||||
|
|
||||||
-- Holds the currently used Perlin noise
|
-- Holds the currently used Perlin noise
|
||||||
local current_perlin = {}
|
local current_perlin = {}
|
||||||
-- holds the current PerlinNoise object
|
-- holds the current PerlinNoise object
|
||||||
current_perlin.noise = nil
|
current_perlin.noise = nil
|
||||||
|
current_perlin.noiseparams = {}
|
||||||
-- Side length of calculated perlin area
|
-- Side length of calculated perlin area
|
||||||
current_perlin.size = nil
|
current_perlin.size = nil
|
||||||
-- Theoretical min and max values for Perlin noise (for colorization)
|
-- Theoretical min and max values for Perlin noise (for colorization)
|
||||||
@ -67,6 +69,13 @@ minetest.register_tool("perlin_explorer:getter", {
|
|||||||
wield_image = "perlin_explorer_getter.png",
|
wield_image = "perlin_explorer_getter.png",
|
||||||
groups = { disable_repair = 1 },
|
groups = { disable_repair = 1 },
|
||||||
on_use = function(itemstack, user, pointed_thing)
|
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
|
||||||
|
minetset.chat_send_player(user:get_player_name(), S("Insufficient privileges! You need the @1 privilege to use this tool.", "server"))
|
||||||
|
end
|
||||||
if current_perlin.noise then
|
if current_perlin.noise then
|
||||||
if pointed_thing.type ~= "node" then
|
if pointed_thing.type ~= "node" then
|
||||||
-- No-op for non-nodes
|
-- No-op for non-nodes
|
||||||
@ -84,7 +93,7 @@ minetest.register_tool("perlin_explorer:getter", {
|
|||||||
end
|
end
|
||||||
minetest.chat_send_player(user:get_player_name(), S("pos=@1, value=@2", minetest.pos_to_string(pos), val))
|
minetest.chat_send_player(user:get_player_name(), S("pos=@1, value=@2", minetest.pos_to_string(pos), val))
|
||||||
else
|
else
|
||||||
local msg = S("No Perlin noise set. Set one with /set_perlin!")
|
local msg = S("No Perlin noise set. Set one with /set_perlin_noise!")
|
||||||
minetest.chat_send_player(user:get_player_name(), msg)
|
minetest.chat_send_player(user:get_player_name(), msg)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
@ -236,6 +245,7 @@ end
|
|||||||
-- * noiseparams: NoiseParams table (see Minetest's Lua API documentation)
|
-- * noiseparams: NoiseParams table (see Minetest's Lua API documentation)
|
||||||
local set_perlin_noise = function(noiseparams)
|
local set_perlin_noise = function(noiseparams)
|
||||||
current_perlin.noise = PerlinNoise(noiseparams)
|
current_perlin.noise = PerlinNoise(noiseparams)
|
||||||
|
current_perlin.noiseparams = noiseparams
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_chatcommand("perlin_set_options", {
|
minetest.register_chatcommand("perlin_set_options", {
|
||||||
@ -325,4 +335,112 @@ minetest.register_chatcommand("perlin_generate", {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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 3D
|
||||||
|
-- TODO: Add pos
|
||||||
|
-- TODO: Add noise options
|
||||||
|
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"))..[[]
|
||||||
|
dropdown[3.5,4.5;3;dimensions;]]..F(S("2D"))..[[;1;true]
|
||||||
|
|
||||||
|
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
|
||||||
|
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)
|
||||||
|
local spread = vector.new(sx, sy, sz)
|
||||||
|
local octaves = tonumber(fields.octaves)
|
||||||
|
local persistence = tonumber(fields.persistence)
|
||||||
|
local lacunarity = tonumber(fields.lacunarity)
|
||||||
|
local dimensions = 2 -- TODO
|
||||||
|
if not (offset and scale and spread_x and spread_y and spread_z and octaves and persistence and dimensions) then
|
||||||
|
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
|
||||||
|
minetset.chat_send_player(user:get_player_name(), S("Insufficient privileges! You need the @1 privilege to use this tool.", "server"))
|
||||||
|
end
|
||||||
|
show_formspec(user)
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
BIN
textures/perlin_explorer_creator.png
Normal file
BIN
textures/perlin_explorer_creator.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.3 KiB |
Loading…
x
Reference in New Issue
Block a user