Skip to content

Commit

Permalink
Add minetest.get_mapgen_object to API
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Jun 28, 2013
1 parent 2c0b517 commit 8aa930f
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 9 deletions.
31 changes: 31 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -1116,6 +1116,8 @@ minetest.get_perlin(seeddiff, octaves, persistence, scale)
^ Return world-specific perlin noise (int(worldseed)+seeddiff) ^ Return world-specific perlin noise (int(worldseed)+seeddiff)
minetest.get_voxel_manip() minetest.get_voxel_manip()
^ Return voxel manipulator object ^ Return voxel manipulator object
minetest.get_mapgen_object(objectname)
^ Return requested mapgen object if available (see Mapgen objects)
minetest.clear_objects() minetest.clear_objects()
^ clear all objects in the environments ^ clear all objects in the environments
minetest.line_of_sight(pos1,pos2,stepsize) ->true/false minetest.line_of_sight(pos1,pos2,stepsize) ->true/false
Expand Down Expand Up @@ -1544,6 +1546,35 @@ methods:
^ To be used only by VoxelManip objects passed to a callback; otherwise, calculated lighting will be ignored ^ To be used only by VoxelManip objects passed to a callback; otherwise, calculated lighting will be ignored
- update_liquids(): Update liquid flow - update_liquids(): Update liquid flow


Mapgen objects
---------------
A mapgen object is a construct used in map generation. Mapgen objects can be used by an on_generate
callback to speed up operations by avoiding unnecessary recalculations; these can be retrieved using the
minetest.get_mapgen_object() function. If the requested Mapgen object is unavailable, or
get_mapgen_object() was called outside of an on_generate() callback, nil is returned.

The following Mapgen objects are currently available:

- voxelmanip
This returns four values; the VoxelManip object to be used, the voxel data, minimum emerge position,
and maximum emerged position. All mapgens support this object.

- heightmap
Returns an array containing the y coordinates of the ground levels of nodes in the most recently
generated chunk by the current mapgen.

- biomemap
Returns an array containing the biome IDs of nodes in the most recently generated chunk by the
current mapgen.

- heatmap
Returns an array containing the temperature values of nodes in the most recently generated chunk by
the current mapgen.

- humiditymap
Returns an array containing the humidity values of nodes in the most recently generated chunk by the
current mapgen.

Registered entities Registered entities
-------------------- --------------------
- Functions receive a "luaentity" as self: - Functions receive a "luaentity" as self:
Expand Down
10 changes: 10 additions & 0 deletions src/emerge.cpp
Expand Up @@ -144,6 +144,16 @@ void EmergeManager::initMapgens(MapgenParams *mgparams) {
} }




Mapgen *EmergeManager::getCurrentMapgen() {
for (unsigned int i = 0; i != emergethread.size(); i++) {
if (emergethread[i]->IsSameThread())
return emergethread[i]->mapgen;
}

return NULL;
}


bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate) { bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate) {
std::map<v3s16, BlockEmergeData *>::const_iterator iter; std::map<v3s16, BlockEmergeData *>::const_iterator iter;
BlockEmergeData *bedata; BlockEmergeData *bedata;
Expand Down
3 changes: 2 additions & 1 deletion src/emerge.h
Expand Up @@ -93,6 +93,7 @@ class EmergeManager {
~EmergeManager(); ~EmergeManager();


void initMapgens(MapgenParams *mgparams); void initMapgens(MapgenParams *mgparams);
Mapgen *getCurrentMapgen();
Mapgen *createMapgen(std::string mgname, int mgid, Mapgen *createMapgen(std::string mgname, int mgid,
MapgenParams *mgparams); MapgenParams *mgparams);
MapgenParams *createMapgenParams(std::string mgname); MapgenParams *createMapgenParams(std::string mgname);
Expand All @@ -111,14 +112,14 @@ class EmergeManager {


class EmergeThread : public SimpleThread class EmergeThread : public SimpleThread
{ {
public:
Server *m_server; Server *m_server;
ServerMap *map; ServerMap *map;
EmergeManager *emerge; EmergeManager *emerge;
Mapgen *mapgen; Mapgen *mapgen;
bool enable_mapgen_debug_info; bool enable_mapgen_debug_info;
int id; int id;


public:
Event qevent; Event qevent;
std::queue<v3s16> blockqueue; std::queue<v3s16> blockqueue;


Expand Down
1 change: 1 addition & 0 deletions src/jthread/jthread.h
Expand Up @@ -47,6 +47,7 @@ class JThread
virtual void *Thread() = 0; virtual void *Thread() = 0;
bool IsRunning(); bool IsRunning();
void *GetReturnValue(); void *GetReturnValue();
bool IsSameThread();
protected: protected:
void ThreadStarted(); void ThreadStarted();
private: private:
Expand Down
5 changes: 5 additions & 0 deletions src/jthread/pthread/jthread.cpp
Expand Up @@ -148,6 +148,11 @@ void *JThread::GetReturnValue()
return val; return val;
} }


bool JThread::IsSameThread()
{
return pthread_equal(pthread_self(), threadid);
}

void *JThread::TheThread(void *param) void *JThread::TheThread(void *param)
{ {
JThread *jthread; JThread *jthread;
Expand Down
5 changes: 5 additions & 0 deletions src/jthread/win32/jthread.cpp
Expand Up @@ -141,6 +141,11 @@ void *JThread::GetReturnValue()
return val; return val;
} }


bool JThread::IsSameThread()
{
return GetCurrentThreadId() == threadid;
}

#ifndef _WIN32_WCE #ifndef _WIN32_WCE
UINT __stdcall JThread::TheThread(void *param) UINT __stdcall JThread::TheThread(void *param)
#else #else
Expand Down
10 changes: 10 additions & 0 deletions src/mapgen.h
Expand Up @@ -93,8 +93,10 @@ class Mapgen {
int id; int id;
ManualMapVoxelManipulator *vm; ManualMapVoxelManipulator *vm;
INodeDefManager *ndef; INodeDefManager *ndef;

s16 *heightmap; s16 *heightmap;
u8 *biomemap; u8 *biomemap;
v3s16 csize;


Mapgen(); Mapgen();
virtual ~Mapgen() {} virtual ~Mapgen() {}
Expand Down Expand Up @@ -124,6 +126,14 @@ struct MapgenFactory {
virtual ~MapgenFactory() {} virtual ~MapgenFactory() {}
}; };


enum MapgenObject {
MGOBJ_VMANIP,
MGOBJ_HEIGHTMAP,
MGOBJ_BIOMEMAP,
MGOBJ_HEATMAP,
MGOBJ_HUMIDMAP
};

enum OreType { enum OreType {
ORE_SCATTER, ORE_SCATTER,
ORE_SHEET, ORE_SHEET,
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_v6.h
Expand Up @@ -84,7 +84,6 @@ class MapgenV6 : public Mapgen {
EmergeManager *emerge; EmergeManager *emerge;


int ystride; int ystride;
v3s16 csize;
u32 flags; u32 flags;


u32 blockseed; u32 blockseed;
Expand Down
1 change: 0 additions & 1 deletion src/mapgen_v7.h
Expand Up @@ -58,7 +58,6 @@ class MapgenV7 : public Mapgen {
BiomeDefManager *bmgr; BiomeDefManager *bmgr;


int ystride; int ystride;
v3s16 csize;
u32 flags; u32 flags;
bool lighting; bool lighting;
bool ridges; bool ridges;
Expand Down
108 changes: 106 additions & 2 deletions src/script/lua_api/l_env.cpp
Expand Up @@ -35,11 +35,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_noise.h" #include "lua_api/l_noise.h"
#include "treegen.h" #include "treegen.h"
#include "pathfinder.h" #include "pathfinder.h"
#include "emerge.h"
#include "mapgen_v7.h"




#define GET_ENV_PTR ServerEnvironment* env = \ #define GET_ENV_PTR ServerEnvironment* env = \
dynamic_cast<ServerEnvironment*>(getEnv(L)); \ dynamic_cast<ServerEnvironment*>(getEnv(L)); \
if( env == NULL) return 0 if( env == NULL) return 0

struct EnumString ModApiEnvMod::es_MapgenObject[] =
{
{MGOBJ_VMANIP, "voxelmanip"},
{MGOBJ_HEIGHTMAP, "heightmap"},
{MGOBJ_BIOMEMAP, "biomemap"},
{MGOBJ_HEATMAP, "heatmap"},
{MGOBJ_HUMIDMAP, "humiditymap"},
{0, NULL},
};


///////////////////////////////////////////////////////////////////////////////



void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n, void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
u32 active_object_count, u32 active_object_count_wider) u32 active_object_count, u32 active_object_count_wider)
Expand Down Expand Up @@ -541,14 +557,101 @@ int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
GET_ENV_PTR; GET_ENV_PTR;


Map *map = &(env->getMap()); Map *map = &(env->getMap());
LuaVoxelManip *vm = new LuaVoxelManip(map); LuaVoxelManip *o = new LuaVoxelManip(map);


*(void **)(lua_newuserdata(L, sizeof(void *))) = vm; *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, "VoxelManip"); luaL_getmetatable(L, "VoxelManip");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
return 1; return 1;
} }


// minetest.get_mapgen_object(objectname)
// returns the requested object used during map generation
int ModApiEnvMod::l_get_mapgen_object(lua_State *L)
{
const char *mgobjstr = lua_tostring(L, 1);

int mgobjint;
if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
return 0;

enum MapgenObject mgobj = (MapgenObject)mgobjint;

EmergeManager *emerge = getServer(L)->getEmergeManager();
Mapgen *mg = emerge->getCurrentMapgen();

size_t maplen = mg->csize.X * mg->csize.Z;

int nargs = 1;

switch (mgobj) {
case MGOBJ_VMANIP: {
ManualMapVoxelManipulator *vm = mg->vm;

// VoxelManip object
LuaVoxelManip *o = new LuaVoxelManip(vm, false);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, "VoxelManip");
lua_setmetatable(L, -2);

// VoxelManip data
int volume = vm->m_area.getVolume();
lua_newtable(L);
for (int i = 0; i != volume; i++) {
lua_Number cid = vm->m_data[i].getContent();
lua_pushnumber(L, cid);
lua_rawseti(L, -2, i + 1);
}

// emerged min pos
push_v3s16(L, vm->m_area.MinEdge);

// emerged max pos
push_v3s16(L, vm->m_area.MaxEdge);

nargs = 4;

break; }
case MGOBJ_HEIGHTMAP: {
if (!mg->heightmap)
return 0;

lua_newtable(L);
for (size_t i = 0; i != maplen; i++) {
lua_pushinteger(L, mg->heightmap[i]);
lua_rawseti(L, -2, i + 1);
}
break; }
case MGOBJ_BIOMEMAP: {
if (!mg->heightmap)
return 0;

lua_newtable(L);
for (size_t i = 0; i != maplen; i++) {
lua_pushinteger(L, mg->biomemap[i]);
lua_rawseti(L, -2, i + 1);
}
break; }
case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
case MGOBJ_HUMIDMAP:
MapgenV7 *mgv7 = (MapgenV7 *)mg;

float *arr = (mgobj == MGOBJ_HEATMAP) ?
mgv7->noise_heat->result : mgv7->noise_humidity->result;
if (!arr)
return 0;

lua_newtable(L);
for (size_t i = 0; i != maplen; i++) {
lua_pushnumber(L, arr[i]);
lua_rawseti(L, -2, i + 1);
}
break; }
}

return nargs;
}

// minetest.clear_objects() // minetest.clear_objects()
// clear all objects in the environment // clear all objects in the environment
int ModApiEnvMod::l_clear_objects(lua_State *L) int ModApiEnvMod::l_clear_objects(lua_State *L)
Expand Down Expand Up @@ -695,6 +798,7 @@ bool ModApiEnvMod::Initialize(lua_State *L,int top)
retval &= API_FCT(get_perlin); retval &= API_FCT(get_perlin);
retval &= API_FCT(get_perlin_map); retval &= API_FCT(get_perlin_map);
retval &= API_FCT(get_voxel_manip); retval &= API_FCT(get_voxel_manip);
retval &= API_FCT(get_mapgen_object);
retval &= API_FCT(clear_objects); retval &= API_FCT(clear_objects);
retval &= API_FCT(spawn_tree); retval &= API_FCT(spawn_tree);
retval &= API_FCT(find_path); retval &= API_FCT(find_path);
Expand Down
8 changes: 7 additions & 1 deletion src/script/lua_api/l_env.h
Expand Up @@ -113,6 +113,10 @@ class ModApiEnvMod
// minetest.get_voxel_manip() // minetest.get_voxel_manip()
// returns world-specific voxel manipulator // returns world-specific voxel manipulator
static int l_get_voxel_manip(lua_State *L); static int l_get_voxel_manip(lua_State *L);

// minetest.get_mapgen_object(objectname)
// returns the requested object used during map generation
static int l_get_mapgen_object(lua_State *L);


// minetest.clear_objects() // minetest.clear_objects()
// clear all objects in the environment // clear all objects in the environment
Expand All @@ -127,7 +131,9 @@ class ModApiEnvMod
// minetest.find_path(pos1, pos2, searchdistance, // minetest.find_path(pos1, pos2, searchdistance,
// max_jump, max_drop, algorithm) -> table containing path // max_jump, max_drop, algorithm) -> table containing path
static int l_find_path(lua_State *L); static int l_find_path(lua_State *L);


static struct EnumString es_MapgenObject[];

public: public:
bool Initialize(lua_State *L, int top); bool Initialize(lua_State *L, int top);
}; };
Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_item.cpp
Expand Up @@ -466,7 +466,7 @@ int ModApiItemMod::l_get_content_id(lua_State *L)
INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager(); INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
content_t c = ndef->getId(name); content_t c = ndef->getId(name);


lua_pushnumber(L, c); lua_pushinteger(L, c);
return 1; /* number of results */ return 1; /* number of results */
} }


Expand Down
10 changes: 8 additions & 2 deletions src/script/lua_api/l_vmanip.cpp
Expand Up @@ -33,7 +33,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
int LuaVoxelManip::gc_object(lua_State *L) int LuaVoxelManip::gc_object(lua_State *L)
{ {
LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1)); LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
delete o; if (o->do_gc)
delete o;


return 0; return 0;
} }
Expand Down Expand Up @@ -82,7 +83,6 @@ int LuaVoxelManip::l_write_chunk(lua_State *L)
vm->m_data[i].setContent(c); vm->m_data[i].setContent(c);


lua_pop(L, 1); lua_pop(L, 1);

} }


vm->blitBackAll(&o->modified_blocks); vm->blitBackAll(&o->modified_blocks);
Expand Down Expand Up @@ -184,6 +184,12 @@ int LuaVoxelManip::l_update_map(lua_State *L)
return 0; return 0;
} }


LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc)
{
this->vm = mmvm;
this->do_gc = dogc;
}

LuaVoxelManip::LuaVoxelManip(Map *map) LuaVoxelManip::LuaVoxelManip(Map *map)
{ {
vm = new ManualMapVoxelManipulator(map); vm = new ManualMapVoxelManipulator(map);
Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_vmanip.h
Expand Up @@ -36,6 +36,7 @@ class LuaVoxelManip
private: private:
ManualMapVoxelManipulator *vm; ManualMapVoxelManipulator *vm;
std::map<v3s16, MapBlock *> modified_blocks; std::map<v3s16, MapBlock *> modified_blocks;
bool do_gc;


static const char className[]; static const char className[];
static const luaL_reg methods[]; static const luaL_reg methods[];
Expand All @@ -50,6 +51,7 @@ class LuaVoxelManip
static int l_set_lighting(lua_State *L); static int l_set_lighting(lua_State *L);


public: public:
LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc);
LuaVoxelManip(Map *map); LuaVoxelManip(Map *map);
~LuaVoxelManip(); ~LuaVoxelManip();


Expand Down

0 comments on commit 8aa930f

Please sign in to comment.