39 changes: 33 additions & 6 deletions builtin/mainmenu/generate_from_settingtypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ local minetest_example_header = [[
]]

local group_format_template = [[
# %s = {
# offset = %s,
# scale = %s,
# spread = (%s, %s, %s),
# seed = %s,
# octaves = %s,
# persistence = %s,
# lacunarity = %s,
# flags = "%s"
# }
]]

local function create_minetest_conf_example()
local result = { minetest_example_header }
for _, entry in ipairs(settings) do
Expand All @@ -33,6 +47,12 @@ local function create_minetest_conf_example()
insert(result, "# " .. entry.name .. "\n\n")
end
else
local group_format = false
if entry.noise_params and entry.values then
if entry.type == "noise_params_2d" or entry.type == "noise_params_3d" then
group_format = true
end
end
if entry.comment ~= "" then
for _, comment_line in ipairs(entry.comment:split("\n", true)) do
insert(result, "# " .. comment_line .. "\n")
Expand All @@ -45,18 +65,25 @@ local function create_minetest_conf_example()
if entry.max then
insert(result, " max: " .. entry.max)
end
if entry.values then
if entry.values and entry.noise_params == nil then
insert(result, " values: " .. concat(entry.values, ", "))
end
if entry.possible then
insert(result, " possible values: " .. entry.possible:gsub(",", ", "))
insert(result, " possible values: " .. concat(entry.possible, ", "))
end
insert(result, "\n")
local append
if entry.default ~= "" then
append = " " .. entry.default
if group_format == true then
insert(result, sprintf(group_format_template, entry.name, entry.values[1],
entry.values[2], entry.values[3], entry.values[4], entry.values[5],
entry.values[6], entry.values[7], entry.values[8], entry.values[9],
entry.values[10]))
else
local append
if entry.default ~= "" then
append = " " .. entry.default
end
insert(result, sprintf("# %s =%s\n\n", entry.name, append or ""))
end
insert(result, sprintf("# %s =%s\n\n", entry.name, append or ""))
end
end
return concat(result)
Expand Down
190 changes: 93 additions & 97 deletions builtin/settingtypes.txt

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions doc/lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1043,13 +1043,13 @@ Accumulates the absolute value of each noise gradient result.
Noise parameters format example for 2D or 3D perlin noise or perlin noise maps:

np_terrain = {
offset = 0,
scale = 1,
spread = {x=500, y=500, z=500},
offset = "0",

This comment has been minimized.

Copy link
@paramat

paramat Oct 23, 2017

Contributor

Oops, this format example should not have been changed, as it is for noise parameters used in mods, this has nothing to do with the format needed for minetest.conf, which is already shown in minetest.conf.example.
Sorry i missed this in the PR.
@srifqi @SmallJoker

This comment has been minimized.

Copy link
@srifqi

srifqi Oct 23, 2017

Author Member

Create new PR?

This comment has been minimized.

Copy link
@paramat

paramat Oct 23, 2017

Contributor

Yes please if you can.

This comment has been minimized.

Copy link
@srifqi

srifqi Oct 23, 2017

Author Member
    np_terrain = {
        offset = 0,
        scale = 1,
        spread = {x=500, y=500, z=500},
        seed = 571347,
        octaves = 5,
        persist = 0.63,
        lacunarity = 2.0,
        flags = "defaults, absvalue"
    }

Like that?

This comment has been minimized.

Copy link
@paramat

paramat Oct 23, 2017

Contributor

Yes an exact revert.

scale = "1",
spread = {x="500", y="500", z="500"},
seed = 571347,
octaves = 5,
persist = 0.63,
lacunarity = 2.0,
persist = "0.63",
lacunarity = "2.0",
flags = "defaults, absvalue"
}
^ A single noise parameter table can be used to get 2D or 3D noise,
Expand Down Expand Up @@ -4024,12 +4024,16 @@ It can be created via `Settings(filename)`.
#### Methods
* `get(key)`: returns a value
* `get_bool(key)`: returns a boolean
* `get_np_group(key)`: returns a NoiseParams table
* `set(key, value)`
* Setting names can't contain whitespace or any of `="{}#`.
* Setting values can't contain the sequence `\n"""`.
* Setting names starting with "secure." can't be set on the main settings object (`minetest.settings`).
* `set_bool(key, value)`
* See documentation for set() above.
* `set_np_group(key, value)`
* `value` is a NoiseParams table.
* Also, see documentation for set() above.
* `remove(key)`: returns a boolean (`true` for success)
* `get_names()`: returns `{key1,...}`
* `write()`: returns a boolean (`true` for success)
Expand Down
10 changes: 5 additions & 5 deletions src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1586,13 +1586,13 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
void push_noiseparams(lua_State *L, NoiseParams *np)
{
lua_newtable(L);
lua_pushnumber(L, np->offset);
push_float_string(L, np->offset);
lua_setfield(L, -2, "offset");
lua_pushnumber(L, np->scale);
push_float_string(L, np->scale);
lua_setfield(L, -2, "scale");
lua_pushnumber(L, np->persist);
push_float_string(L, np->persist);
lua_setfield(L, -2, "persistence");
lua_pushnumber(L, np->lacunarity);
push_float_string(L, np->lacunarity);
lua_setfield(L, -2, "lacunarity");
lua_pushnumber(L, np->seed);
lua_setfield(L, -2, "seed");
Expand All @@ -1603,7 +1603,7 @@ void push_noiseparams(lua_State *L, NoiseParams *np)
np->flags);
lua_setfield(L, -2, "flags");

push_v3f(L, np->spread);
push_v3_float_string(L, np->spread);
lua_setfield(L, -2, "spread");
}

Expand Down
29 changes: 29 additions & 0 deletions src/script/common/c_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ if (value < F1000_MIN || value > F1000_MAX) { \
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)


void push_float_string(lua_State *L, float value)
{
std::stringstream ss;
std::string str;
ss << value;
str = ss.str();
lua_pushstring(L, str.c_str());
}

void push_v3f(lua_State *L, v3f p)
{
lua_newtable(L);
Expand All @@ -71,6 +80,26 @@ void push_v2f(lua_State *L, v2f p)
lua_setfield(L, -2, "y");
}

void push_v3_float_string(lua_State *L, v3f p)
{
lua_newtable(L);
push_float_string(L, p.X);
lua_setfield(L, -2, "x");
push_float_string(L, p.Y);
lua_setfield(L, -2, "y");
push_float_string(L, p.Z);
lua_setfield(L, -2, "z");
}

void push_v2_float_string(lua_State *L, v2f p)
{
lua_newtable(L);
push_float_string(L, p.X);
lua_setfield(L, -2, "x");
push_float_string(L, p.Y);
lua_setfield(L, -2, "y");
}

v2s16 read_v2s16(lua_State *L, int index)
{
v2s16 p;
Expand Down
3 changes: 3 additions & 0 deletions src/script/common/c_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ std::vector<aabb3f> read_aabb3f_vector (lua_State *L, int index, f32 scale);
size_t read_stringlist (lua_State *L, int index,
std::vector<std::string> *result);

void push_float_string (lua_State *L, float value);
void push_v3_float_string(lua_State *L, v3f p);
void push_v2_float_string(lua_State *L, v2f p);
void push_v2s16 (lua_State *L, v2s16 p);
void push_v2s32 (lua_State *L, v2s32 p);
void push_v3s16 (lua_State *L, v3s16 p);
Expand Down
38 changes: 38 additions & 0 deletions src/script/lua_api/l_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "cpp_api/s_security.h"
#include "settings.h"
#include "noise.h"
#include "log.h"


Expand Down Expand Up @@ -105,6 +106,24 @@ int LuaSettings::l_get_bool(lua_State* L)
return 1;
}

// get_np_group(self, key) -> value
int LuaSettings::l_get_np_group(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaSettings *o = checkobject(L, 1);

std::string key = std::string(luaL_checkstring(L, 2));
if (o->m_settings->exists(key)) {
NoiseParams np;
o->m_settings->getNoiseParams(key, np);
push_noiseparams(L, &np);
} else {
lua_pushnil(L);
}

return 1;
}

// set(self, key, value)
int LuaSettings::l_set(lua_State* L)
{
Expand Down Expand Up @@ -138,6 +157,23 @@ int LuaSettings::l_set_bool(lua_State* L)
return 1;
}

// set(self, key, value)
int LuaSettings::l_set_np_group(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaSettings *o = checkobject(L, 1);

std::string key = std::string(luaL_checkstring(L, 2));
NoiseParams value;
read_noiseparams(L, 3, &value);

SET_SECURITY_CHECK(L, key);

o->m_settings->setNoiseParams(key, value, false);

return 0;
}

// remove(self, key) -> success
int LuaSettings::l_remove(lua_State* L)
{
Expand Down Expand Up @@ -264,8 +300,10 @@ const char LuaSettings::className[] = "Settings";
const luaL_Reg LuaSettings::methods[] = {
luamethod(LuaSettings, get),
luamethod(LuaSettings, get_bool),
luamethod(LuaSettings, get_np_group),
luamethod(LuaSettings, set),
luamethod(LuaSettings, set_bool),
luamethod(LuaSettings, set_np_group),
luamethod(LuaSettings, remove),
luamethod(LuaSettings, get_names),
luamethod(LuaSettings, write),
Expand Down
7 changes: 7 additions & 0 deletions src/script/lua_api/l_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "common/c_content.h"
#include "lua_api/l_base.h"

class Settings;
Expand All @@ -38,12 +39,18 @@ class LuaSettings : public ModApiBase
// get_bool(self, key) -> boolean
static int l_get_bool(lua_State *L);

// get_np_group(self, key) -> noiseparam
static int l_get_np_group(lua_State *L);

// set(self, key, value)
static int l_set(lua_State *L);

// set_bool(self, key, value)
static int l_set_bool(lua_State *L);

// set_np_group(self, key, value)
static int l_set_np_group(lua_State *L);

// remove(self, key) -> success
static int l_remove(lua_State *L);

Expand Down