Skip to content

Commit

Permalink
Add minetest.set_noiseparam_defaults() Lua API
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Feb 16, 2014
1 parent c873164 commit 3570f3e
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 270 deletions.
4 changes: 4 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1386,6 +1386,10 @@ minetest.set_mapgen_params(MapgenParams)
^ Leave field unset to leave that parameter unchanged
^ flags contains a comma-delimited string of flags to set, or if the prefix "no" is attached, clears instead.
^ flags is in the same format and has the same options as 'mg_flags' in minetest.conf
minetest.set_noiseparam_defaults({np1=NoiseParams, np2= NoiseParams, ...})
^ Sets the default value of a noiseparam setting
^ Takes a table as an argument that maps one or more setting names to NoiseParams structures
^ Possible setting names consist of any NoiseParams setting exposed through the global settings
minetest.clear_objects()
^ clear all objects in the environments
minetest.line_of_sight(pos1, pos2, stepsize) -> true/false, pos
Expand Down
1 change: 1 addition & 0 deletions src/defaultsettings.cpp
Expand Up @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

#include "settings.h"
#include "porting.h"
#include "filesys.h"
#include "config.h"

Expand Down
21 changes: 12 additions & 9 deletions src/emerge.cpp
Expand Up @@ -125,15 +125,6 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
emergethread.push_back(new EmergeThread((Server *)gamedef, i));

infostream << "EmergeManager: using " << nthreads << " threads" << std::endl;

loadParamsFromSettings(g_settings);

if (g_settings->get("fixed_map_seed").empty()) {
params.seed = (((u64)(myrand() & 0xffff) << 0)
| ((u64)(myrand() & 0xffff) << 16)
| ((u64)(myrand() & 0xffff) << 32)
| ((u64)(myrand() & 0xffff) << 48));
}
}


Expand Down Expand Up @@ -168,6 +159,18 @@ EmergeManager::~EmergeManager() {
}


void EmergeManager::loadMapgenParams() {
loadParamsFromSettings(g_settings);

if (g_settings->get("fixed_map_seed").empty()) {
params.seed = (((u64)(myrand() & 0xffff) << 0)
| ((u64)(myrand() & 0xffff) << 16)
| ((u64)(myrand() & 0xffff) << 32)
| ((u64)(myrand() & 0xffff) << 48));
}
}


void EmergeManager::initMapgens() {
if (mapgen.size())
return;
Expand Down
1 change: 1 addition & 0 deletions src/emerge.h
Expand Up @@ -103,6 +103,7 @@ class EmergeManager {
EmergeManager(IGameDef *gamedef);
~EmergeManager();

void loadMapgenParams();
void initMapgens();
Mapgen *getCurrentMapgen();
Mapgen *createMapgen(std::string mgname, int mgid,
Expand Down
7 changes: 5 additions & 2 deletions src/noise.h
Expand Up @@ -85,8 +85,11 @@ struct NoiseParams {


// Convenience macros for getting/setting NoiseParams in Settings
#define getNoiseParams(x, y) getStruct((x), "f,f,v3,s32,s32,f", &(y), sizeof(y))
#define setNoiseParams(x, y) setStruct((x), "f,f,v3,s32,s32,f", &(y))

#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))

class Noise {
public:
Expand Down
27 changes: 19 additions & 8 deletions src/script/common/c_content.cpp
Expand Up @@ -958,25 +958,36 @@ void luaentity_get(lua_State *L, u16 id)

/******************************************************************************/
NoiseParams *read_noiseparams(lua_State *L, int index)
{
NoiseParams *np = new NoiseParams;

if (!read_noiseparams_nc(L, index, np)) {
delete np;
np = NULL;
}

return np;
}

bool read_noiseparams_nc(lua_State *L, int index, NoiseParams *np)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;

if (!lua_istable(L, index))
return NULL;
return false;

NoiseParams *np = new NoiseParams;
np->offset = getfloatfield_default(L, index, "offset", 0.0);
np->scale = getfloatfield_default(L, index, "scale", 0.0);
np->persist = getfloatfield_default(L, index, "persist", 0.0);
np->seed = getintfield_default(L, index, "seed", 0);
np->octaves = getintfield_default(L, index, "octaves", 0);

np->offset = getfloatfield_default(L, index, "offset", 0.0);
np->scale = getfloatfield_default(L, index, "scale", 0.0);
lua_getfield(L, index, "spread");
np->spread = read_v3f(L, -1);
lua_pop(L, 1);
np->seed = getintfield_default(L, index, "seed", 0);
np->octaves = getintfield_default(L, index, "octaves", 0);
np->persist = getfloatfield_default(L, index, "persist", 0.0);

return np;
return true;
}

/******************************************************************************/
Expand Down
3 changes: 3 additions & 0 deletions src/script/common/c_content.h
Expand Up @@ -144,6 +144,9 @@ bool string_to_enum (const EnumString *spec,

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

bool read_noiseparams_nc (lua_State *L, int index,
NoiseParams *np);

bool read_schematic (lua_State *L, int index,
DecoSchematic *dschem,
Server *server);
Expand Down
28 changes: 28 additions & 0 deletions src/script/lua_api/l_mapgen.cpp
Expand Up @@ -229,6 +229,33 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L)
return 0;
}

// minetest.set_noiseparam_defaults({np1={noise params}, ...})
// set default values for noise parameters if not present in global settings
int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
{
NoiseParams np;
std::string val, name;

if (!lua_istable(L, 1))
return 0;

lua_pushnil(L);
while (lua_next(L, 1)) {
if (read_noiseparams_nc(L, -1, &np)) {
if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
continue;
if (!lua_isstring(L, -2))
continue;

name = lua_tostring(L, -2);
g_settings->setDefault(name, val);
}
lua_pop(L, 1);
}

return 0;
}

// set_gen_notify(string)
int ModApiMapgen::l_set_gen_notify(lua_State *L)
{
Expand Down Expand Up @@ -607,6 +634,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top)
API_FCT(get_mapgen_object);

API_FCT(set_mapgen_params);
API_FCT(set_noiseparam_defaults);
API_FCT(set_gen_notify);

API_FCT(register_biome);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_mapgen.h
Expand Up @@ -32,6 +32,9 @@ class ModApiMapgen : public ModApiBase {
// set mapgen parameters
static int l_set_mapgen_params(lua_State *L);

// minetest.set_noiseparam_defaults({np1={noise params}, ...})
static int l_set_noiseparam_defaults(lua_State *L);

// set_gen_notify(flagstring)
static int l_set_gen_notify(lua_State *L);

Expand Down
4 changes: 4 additions & 0 deletions src/server.cpp
Expand Up @@ -341,6 +341,10 @@ Server::Server(
// Apply item aliases in the node definition manager
m_nodedef->updateAliases(m_itemdef);

// Load the mapgen params from global settings now after any
// initial overrides have been set by the mods
m_emerge->loadMapgenParams();

// Initialize Environment
ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
m_env = new ServerEnvironment(servermap, m_script, this);
Expand Down

0 comments on commit 3570f3e

Please sign in to comment.