Skip to content

Commit 3570f3e

Browse files
committedFeb 16, 2014
Add minetest.set_noiseparam_defaults() Lua API
1 parent c873164 commit 3570f3e

13 files changed

+368
-270
lines changed
 

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,10 @@ minetest.set_mapgen_params(MapgenParams)
13861386
^ Leave field unset to leave that parameter unchanged
13871387
^ flags contains a comma-delimited string of flags to set, or if the prefix "no" is attached, clears instead.
13881388
^ flags is in the same format and has the same options as 'mg_flags' in minetest.conf
1389+
minetest.set_noiseparam_defaults({np1=NoiseParams, np2= NoiseParams, ...})
1390+
^ Sets the default value of a noiseparam setting
1391+
^ Takes a table as an argument that maps one or more setting names to NoiseParams structures
1392+
^ Possible setting names consist of any NoiseParams setting exposed through the global settings
13891393
minetest.clear_objects()
13901394
^ clear all objects in the environments
13911395
minetest.line_of_sight(pos1, pos2, stepsize) -> true/false, pos

‎src/defaultsettings.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1818
*/
1919

2020
#include "settings.h"
21+
#include "porting.h"
2122
#include "filesys.h"
2223
#include "config.h"
2324

‎src/emerge.cpp

+12-9
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
125125
emergethread.push_back(new EmergeThread((Server *)gamedef, i));
126126

127127
infostream << "EmergeManager: using " << nthreads << " threads" << std::endl;
128-
129-
loadParamsFromSettings(g_settings);
130-
131-
if (g_settings->get("fixed_map_seed").empty()) {
132-
params.seed = (((u64)(myrand() & 0xffff) << 0)
133-
| ((u64)(myrand() & 0xffff) << 16)
134-
| ((u64)(myrand() & 0xffff) << 32)
135-
| ((u64)(myrand() & 0xffff) << 48));
136-
}
137128
}
138129

139130

@@ -168,6 +159,18 @@ EmergeManager::~EmergeManager() {
168159
}
169160

170161

162+
void EmergeManager::loadMapgenParams() {
163+
loadParamsFromSettings(g_settings);
164+
165+
if (g_settings->get("fixed_map_seed").empty()) {
166+
params.seed = (((u64)(myrand() & 0xffff) << 0)
167+
| ((u64)(myrand() & 0xffff) << 16)
168+
| ((u64)(myrand() & 0xffff) << 32)
169+
| ((u64)(myrand() & 0xffff) << 48));
170+
}
171+
}
172+
173+
171174
void EmergeManager::initMapgens() {
172175
if (mapgen.size())
173176
return;

‎src/emerge.h

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class EmergeManager {
103103
EmergeManager(IGameDef *gamedef);
104104
~EmergeManager();
105105

106+
void loadMapgenParams();
106107
void initMapgens();
107108
Mapgen *getCurrentMapgen();
108109
Mapgen *createMapgen(std::string mgname, int mgid,

‎src/noise.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ struct NoiseParams {
8585

8686

8787
// Convenience macros for getting/setting NoiseParams in Settings
88-
#define getNoiseParams(x, y) getStruct((x), "f,f,v3,s32,s32,f", &(y), sizeof(y))
89-
#define setNoiseParams(x, y) setStruct((x), "f,f,v3,s32,s32,f", &(y))
88+
89+
#define NOISEPARAMS_FMT_STR "f,f,v3,s32,s32,f"
90+
91+
#define getNoiseParams(x, y) getStruct((x), NOISEPARAMS_FMT_STR, &(y), sizeof(y))
92+
#define setNoiseParams(x, y) setStruct((x), NOISEPARAMS_FMT_STR, &(y))
9093

9194
class Noise {
9295
public:

‎src/script/common/c_content.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -958,25 +958,36 @@ void luaentity_get(lua_State *L, u16 id)
958958

959959
/******************************************************************************/
960960
NoiseParams *read_noiseparams(lua_State *L, int index)
961+
{
962+
NoiseParams *np = new NoiseParams;
963+
964+
if (!read_noiseparams_nc(L, index, np)) {
965+
delete np;
966+
np = NULL;
967+
}
968+
969+
return np;
970+
}
971+
972+
bool read_noiseparams_nc(lua_State *L, int index, NoiseParams *np)
961973
{
962974
if (index < 0)
963975
index = lua_gettop(L) + 1 + index;
964976

965977
if (!lua_istable(L, index))
966-
return NULL;
978+
return false;
967979

968-
NoiseParams *np = new NoiseParams;
980+
np->offset = getfloatfield_default(L, index, "offset", 0.0);
981+
np->scale = getfloatfield_default(L, index, "scale", 0.0);
982+
np->persist = getfloatfield_default(L, index, "persist", 0.0);
983+
np->seed = getintfield_default(L, index, "seed", 0);
984+
np->octaves = getintfield_default(L, index, "octaves", 0);
969985

970-
np->offset = getfloatfield_default(L, index, "offset", 0.0);
971-
np->scale = getfloatfield_default(L, index, "scale", 0.0);
972986
lua_getfield(L, index, "spread");
973987
np->spread = read_v3f(L, -1);
974988
lua_pop(L, 1);
975-
np->seed = getintfield_default(L, index, "seed", 0);
976-
np->octaves = getintfield_default(L, index, "octaves", 0);
977-
np->persist = getfloatfield_default(L, index, "persist", 0.0);
978989

979-
return np;
990+
return true;
980991
}
981992

982993
/******************************************************************************/

‎src/script/common/c_content.h

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ bool string_to_enum (const EnumString *spec,
144144

145145
NoiseParams* read_noiseparams (lua_State *L, int index);
146146

147+
bool read_noiseparams_nc (lua_State *L, int index,
148+
NoiseParams *np);
149+
147150
bool read_schematic (lua_State *L, int index,
148151
DecoSchematic *dschem,
149152
Server *server);

‎src/script/lua_api/l_mapgen.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,33 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L)
229229
return 0;
230230
}
231231

232+
// minetest.set_noiseparam_defaults({np1={noise params}, ...})
233+
// set default values for noise parameters if not present in global settings
234+
int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
235+
{
236+
NoiseParams np;
237+
std::string val, name;
238+
239+
if (!lua_istable(L, 1))
240+
return 0;
241+
242+
lua_pushnil(L);
243+
while (lua_next(L, 1)) {
244+
if (read_noiseparams_nc(L, -1, &np)) {
245+
if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
246+
continue;
247+
if (!lua_isstring(L, -2))
248+
continue;
249+
250+
name = lua_tostring(L, -2);
251+
g_settings->setDefault(name, val);
252+
}
253+
lua_pop(L, 1);
254+
}
255+
256+
return 0;
257+
}
258+
232259
// set_gen_notify(string)
233260
int ModApiMapgen::l_set_gen_notify(lua_State *L)
234261
{
@@ -607,6 +634,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top)
607634
API_FCT(get_mapgen_object);
608635

609636
API_FCT(set_mapgen_params);
637+
API_FCT(set_noiseparam_defaults);
610638
API_FCT(set_gen_notify);
611639

612640
API_FCT(register_biome);

‎src/script/lua_api/l_mapgen.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ModApiMapgen : public ModApiBase {
3232
// set mapgen parameters
3333
static int l_set_mapgen_params(lua_State *L);
3434

35+
// minetest.set_noiseparam_defaults({np1={noise params}, ...})
36+
static int l_set_noiseparam_defaults(lua_State *L);
37+
3538
// set_gen_notify(flagstring)
3639
static int l_set_gen_notify(lua_State *L);
3740

‎src/server.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ Server::Server(
341341
// Apply item aliases in the node definition manager
342342
m_nodedef->updateAliases(m_itemdef);
343343

344+
// Load the mapgen params from global settings now after any
345+
// initial overrides have been set by the mods
346+
m_emerge->loadMapgenParams();
347+
344348
// Initialize Environment
345349
ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
346350
m_env = new ServerEnvironment(servermap, m_script, this);

0 commit comments

Comments
 (0)
Please sign in to comment.