Skip to content

Commit

Permalink
Use setting groups for NoiseParams
Browse files Browse the repository at this point in the history
Add format example to minetest.conf.example
Add Settings::setU16()
Throw exception on attempted access of NULL settings groups
  • Loading branch information
kwolekr committed Dec 2, 2014
1 parent 78103e6 commit 68c799b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 10 deletions.
14 changes: 14 additions & 0 deletions minetest.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,21 @@
#mgv5_np_wetness = 0, 1, (40, 40, 40), 32474, 4, 1.1

#mgv6_spflags = biomeblend, jungles, mudflow

#
# Noise parameters can be specified as a set of positional values:
#mgv6_np_terrain_base = -4, 20, (250, 250, 250), 82341, 5, 0.6
#
# Or the new group format can be used instead:
#mgv6_np_terrain_base = {
# offset = -4
# scale = 20
# spread = (250, 250, 250)
# seed = 82341
# octaves = 5
# persistence = 0.6
#}

#mgv6_np_terrain_higher = 20, 16, (500, 500, 500), 85039, 5, 0.6
#mgv6_np_steepness = 0.85, 0.5, (125, 125, 125), -932, 5, 0.7
#mgv6_np_height_select = 0.5, 1, (250, 250, 250), 4213, 5, 0.69
Expand Down
15 changes: 7 additions & 8 deletions src/noise.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ struct NoiseParams {
float offset;
float scale;
v3f spread;
int seed;
int octaves;
s32 seed;
u16 octaves;
float persist;
bool eased;

Expand All @@ -91,12 +91,11 @@ struct NoiseParams {
};


// Convenience macros for getting/setting NoiseParams in Settings

#define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"

#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
// Convenience macros for getting/setting NoiseParams in Settings as a string
// WARNING: Deprecated, use Settings::getNoiseParamsFromValue() instead
#define NOISEPARAMS_FMT_STR "f,f,v3,s32,u16,f"
//#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
//#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))

class Noise {
public:
Expand Down
76 changes: 75 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "util/serialize.h"
#include "filesys.h"
#include "noise.h"
#include <cctype>
#include <algorithm>

Expand Down Expand Up @@ -358,7 +359,10 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const

Settings *Settings::getGroup(const std::string &name) const
{
return getEntry(name).group;
Settings *group = getEntry(name).group;
if (group == NULL)
throw SettingNotFoundException("Setting [" + name + "] is not a group.");
return group;
}


Expand Down Expand Up @@ -461,6 +465,55 @@ bool Settings::getStruct(const std::string &name, const std::string &format,
}


bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
{
return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
}


bool Settings::getNoiseParamsFromValue(const std::string &name,
NoiseParams &np) const
{
std::string value;

if (!getNoEx(name, value))
return false;

Strfnd f(value);

np.offset = stof(f.next(","));
np.scale = stof(f.next(","));
f.next("(");
np.spread.X = stof(f.next(","));
np.spread.Y = stof(f.next(","));
np.spread.Z = stof(f.next(")"));
np.seed = stoi(f.next(","));
np.octaves = stoi(f.next(","));
np.persist = stof(f.next(""));

return true;
}


bool Settings::getNoiseParamsFromGroup(const std::string &name,
NoiseParams &np) const
{
Settings *group = NULL;

if (!getGroupNoEx(name, group))
return false;

group->getFloatNoEx("offset", np.offset);
group->getFloatNoEx("scale", np.scale);
group->getV3FNoEx("spread", np.spread);
group->getS32NoEx("seed", np.seed);
group->getU16NoEx("octaves", np.octaves);
group->getFloatNoEx("persistence", np.persist);

return true;
}


bool Settings::exists(const std::string &name) const
{
JMutexAutoLock lock(m_mutex);
Expand Down Expand Up @@ -682,6 +735,12 @@ void Settings::setS16(const std::string &name, s16 value)
}


void Settings::setU16(const std::string &name, u16 value)
{
set(name, itos(value));
}


void Settings::setS32(const std::string &name, s32 value)
{
set(name, itos(value));
Expand Down Expand Up @@ -737,6 +796,21 @@ bool Settings::setStruct(const std::string &name, const std::string &format,
}


void Settings::setNoiseParams(const std::string &name, const NoiseParams &np)
{
Settings *group = new Settings;

group->setFloat("offset", np.offset);
group->setFloat("scale", np.scale);
group->setV3F("spread", np.spread);
group->setS32("seed", np.seed);
group->setU16("octaves", np.octaves);
group->setFloat("persistence", np.persist);

setGroup(name, group);
}


bool Settings::remove(const std::string &name)
{
JMutexAutoLock lock(m_mutex);
Expand Down
6 changes: 6 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <set>

class Settings;
struct NoiseParams;

/** function type to register a changed callback */
typedef void (*setting_changed_callback)(const std::string);
Expand Down Expand Up @@ -142,6 +143,9 @@ class Settings {
// the behavior is undefined.
bool getStruct(const std::string &name, const std::string &format,
void *out, size_t olen) const;
bool getNoiseParams(const std::string &name, NoiseParams &np) const;
bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;

// return all keys used
std::vector<std::string> getNames() const;
Expand Down Expand Up @@ -181,13 +185,15 @@ class Settings {
void setGroupDefault(const std::string &name, Settings *group);
void setBool(const std::string &name, bool value);
void setS16(const std::string &name, s16 value);
void setU16(const std::string &name, u16 value);
void setS32(const std::string &name, s32 value);
void setU64(const std::string &name, u64 value);
void setFloat(const std::string &name, float value);
void setV2F(const std::string &name, v2f value);
void setV3F(const std::string &name, v3f value);
void setFlagStr(const std::string &name, u32 flags,
const FlagDesc *flagdesc, u32 flagmask);
void setNoiseParams(const std::string &name, const NoiseParams &np);
// N.B. if setStruct() is used to write a non-POD aggregate type,
// the behavior is undefined.
bool setStruct(const std::string &name, const std::string &format, void *value);
Expand Down
2 changes: 1 addition & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ struct TestSettings: public TestBase
// Test settings groups
Settings *group = s.getGroup("asdf");
UASSERT(group != NULL);
UASSERT(s.getGroup("zoop") == NULL);
UASSERT(s.getGroupNoEx("zoop", group) == false);
UASSERT(group->getS16("a") == 5);
UASSERT(fabs(group->getFloat("b") - 2.5) < 0.001);

Expand Down

1 comment on commit 68c799b

@mdoege
Copy link
Contributor

@mdoege mdoege commented on 68c799b Dec 2, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble with this commit and old maps with single-line noise parameters: https://forum.minetest.net/viewtopic.php?f=6&t=10673

Please sign in to comment.