Skip to content

Commit 68c799b

Browse files
committedDec 2, 2014
Use setting groups for NoiseParams
Add format example to minetest.conf.example Add Settings::setU16() Throw exception on attempted access of NULL settings groups
1 parent 78103e6 commit 68c799b

File tree

5 files changed

+103
-10
lines changed

5 files changed

+103
-10
lines changed
 

‎minetest.conf.example

+14
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,21 @@
443443
#mgv5_np_wetness = 0, 1, (40, 40, 40), 32474, 4, 1.1
444444

445445
#mgv6_spflags = biomeblend, jungles, mudflow
446+
447+
#
448+
# Noise parameters can be specified as a set of positional values:
446449
#mgv6_np_terrain_base = -4, 20, (250, 250, 250), 82341, 5, 0.6
450+
#
451+
# Or the new group format can be used instead:
452+
#mgv6_np_terrain_base = {
453+
# offset = -4
454+
# scale = 20
455+
# spread = (250, 250, 250)
456+
# seed = 82341
457+
# octaves = 5
458+
# persistence = 0.6
459+
#}
460+
447461
#mgv6_np_terrain_higher = 20, 16, (500, 500, 500), 85039, 5, 0.6
448462
#mgv6_np_steepness = 0.85, 0.5, (125, 125, 125), -932, 5, 0.7
449463
#mgv6_np_height_select = 0.5, 1, (250, 250, 250), 4213, 5, 0.69

‎src/noise.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ struct NoiseParams {
7070
float offset;
7171
float scale;
7272
v3f spread;
73-
int seed;
74-
int octaves;
73+
s32 seed;
74+
u16 octaves;
7575
float persist;
7676
bool eased;
7777

@@ -91,12 +91,11 @@ struct NoiseParams {
9191
};
9292

9393

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

101100
class Noise {
102101
public:

‎src/settings.cpp

+75-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2929
#include "log.h"
3030
#include "util/serialize.h"
3131
#include "filesys.h"
32+
#include "noise.h"
3233
#include <cctype>
3334
#include <algorithm>
3435

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

359360
Settings *Settings::getGroup(const std::string &name) const
360361
{
361-
return getEntry(name).group;
362+
Settings *group = getEntry(name).group;
363+
if (group == NULL)
364+
throw SettingNotFoundException("Setting [" + name + "] is not a group.");
365+
return group;
362366
}
363367

364368

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

463467

468+
bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const
469+
{
470+
return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np);
471+
}
472+
473+
474+
bool Settings::getNoiseParamsFromValue(const std::string &name,
475+
NoiseParams &np) const
476+
{
477+
std::string value;
478+
479+
if (!getNoEx(name, value))
480+
return false;
481+
482+
Strfnd f(value);
483+
484+
np.offset = stof(f.next(","));
485+
np.scale = stof(f.next(","));
486+
f.next("(");
487+
np.spread.X = stof(f.next(","));
488+
np.spread.Y = stof(f.next(","));
489+
np.spread.Z = stof(f.next(")"));
490+
np.seed = stoi(f.next(","));
491+
np.octaves = stoi(f.next(","));
492+
np.persist = stof(f.next(""));
493+
494+
return true;
495+
}
496+
497+
498+
bool Settings::getNoiseParamsFromGroup(const std::string &name,
499+
NoiseParams &np) const
500+
{
501+
Settings *group = NULL;
502+
503+
if (!getGroupNoEx(name, group))
504+
return false;
505+
506+
group->getFloatNoEx("offset", np.offset);
507+
group->getFloatNoEx("scale", np.scale);
508+
group->getV3FNoEx("spread", np.spread);
509+
group->getS32NoEx("seed", np.seed);
510+
group->getU16NoEx("octaves", np.octaves);
511+
group->getFloatNoEx("persistence", np.persist);
512+
513+
return true;
514+
}
515+
516+
464517
bool Settings::exists(const std::string &name) const
465518
{
466519
JMutexAutoLock lock(m_mutex);
@@ -682,6 +735,12 @@ void Settings::setS16(const std::string &name, s16 value)
682735
}
683736

684737

738+
void Settings::setU16(const std::string &name, u16 value)
739+
{
740+
set(name, itos(value));
741+
}
742+
743+
685744
void Settings::setS32(const std::string &name, s32 value)
686745
{
687746
set(name, itos(value));
@@ -737,6 +796,21 @@ bool Settings::setStruct(const std::string &name, const std::string &format,
737796
}
738797

739798

799+
void Settings::setNoiseParams(const std::string &name, const NoiseParams &np)
800+
{
801+
Settings *group = new Settings;
802+
803+
group->setFloat("offset", np.offset);
804+
group->setFloat("scale", np.scale);
805+
group->setV3F("spread", np.spread);
806+
group->setS32("seed", np.seed);
807+
group->setU16("octaves", np.octaves);
808+
group->setFloat("persistence", np.persist);
809+
810+
setGroup(name, group);
811+
}
812+
813+
740814
bool Settings::remove(const std::string &name)
741815
{
742816
JMutexAutoLock lock(m_mutex);

‎src/settings.h

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2929
#include <set>
3030

3131
class Settings;
32+
struct NoiseParams;
3233

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

146150
// return all keys used
147151
std::vector<std::string> getNames() const;
@@ -181,13 +185,15 @@ class Settings {
181185
void setGroupDefault(const std::string &name, Settings *group);
182186
void setBool(const std::string &name, bool value);
183187
void setS16(const std::string &name, s16 value);
188+
void setU16(const std::string &name, u16 value);
184189
void setS32(const std::string &name, s32 value);
185190
void setU64(const std::string &name, u64 value);
186191
void setFloat(const std::string &name, float value);
187192
void setV2F(const std::string &name, v2f value);
188193
void setV3F(const std::string &name, v3f value);
189194
void setFlagStr(const std::string &name, u32 flags,
190195
const FlagDesc *flagdesc, u32 flagmask);
196+
void setNoiseParams(const std::string &name, const NoiseParams &np);
191197
// N.B. if setStruct() is used to write a non-POD aggregate type,
192198
// the behavior is undefined.
193199
bool setStruct(const std::string &name, const std::string &format, void *value);

‎src/test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ struct TestSettings: public TestBase
512512
// Test settings groups
513513
Settings *group = s.getGroup("asdf");
514514
UASSERT(group != NULL);
515-
UASSERT(s.getGroup("zoop") == NULL);
515+
UASSERT(s.getGroupNoEx("zoop", group) == false);
516516
UASSERT(group->getS16("a") == 5);
517517
UASSERT(fabs(group->getFloat("b") - 2.5) < 0.001);
518518

1 commit comments

Comments
 (1)

mdoege commented on Dec 2, 2014

@mdoege
Contributor

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.