ObjDefManager, Mapgen SAPI: Huge refactoring

- General code cleanup
 - Unified object creation and loading
 - Specifying objects in APIs is now orthogonal (i.e. anything can take an ID,
   name string, or the raw table definition (and automatically registers if present
master
kwolekr 2015-03-31 23:27:19 -04:00
parent d1d5618bb8
commit 337e79c656
8 changed files with 319 additions and 171 deletions

View File

@ -3195,6 +3195,7 @@ Definition tables
biomes = {"Oceanside", "Hills", "Plains"}, biomes = {"Oceanside", "Hills", "Plains"},
-- ^ List of biomes in which this decoration occurs. Occurs in all biomes if this is omitted, -- ^ List of biomes in which this decoration occurs. Occurs in all biomes if this is omitted,
-- ^ and ignored if the Mapgen being used does not support biomes. -- ^ and ignored if the Mapgen being used does not support biomes.
-- ^ Can be a list of (or a single) biome names, IDs, or definitions.
y_min = -31000 y_min = -31000
y_max = 31000 y_max = 31000
-- ^ Minimum and maximum `y` positions these decorations can be generated at. -- ^ Minimum and maximum `y` positions these decorations can be generated at.
@ -3221,6 +3222,7 @@ Definition tables
schematic = "foobar.mts", schematic = "foobar.mts",
-- ^ If schematic is a string, it is the filepath relative to the current working directory of the -- ^ If schematic is a string, it is the filepath relative to the current working directory of the
-- ^ specified Minetest schematic file. -- ^ specified Minetest schematic file.
-- ^ - OR -, could be the ID of a previously registered schematic
-- ^ - OR -, could instead be a table containing two mandatory fields, size and data, -- ^ - OR -, could instead be a table containing two mandatory fields, size and data,
-- ^ and an optional table yslice_prob: -- ^ and an optional table yslice_prob:
schematic = { schematic = {

View File

@ -213,7 +213,6 @@ public:
virtual const char *getObjectTitle() const = 0; virtual const char *getObjectTitle() const = 0;
virtual ObjDef *create(int type) = 0;
virtual void clear(); virtual void clear();
virtual ObjDef *getByName(const std::string &name) const; virtual ObjDef *getByName(const std::string &name) const;

View File

@ -26,11 +26,11 @@ struct NoiseParams;
enum BiomeType enum BiomeType
{ {
BIOME_TYPE_NORMAL, BIOME_NORMAL,
BIOME_TYPE_LIQUID, BIOME_LIQUID,
BIOME_TYPE_NETHER, BIOME_NETHER,
BIOME_TYPE_AETHER, BIOME_AETHER,
BIOME_TYPE_FLAT BIOME_FLAT
}; };
class Biome : public ObjDef, public NodeResolver { class Biome : public ObjDef, public NodeResolver {
@ -68,7 +68,7 @@ public:
return "biome"; return "biome";
} }
Biome *create(int btt) static Biome *create(BiomeType type)
{ {
return new Biome; return new Biome;
} }

View File

@ -131,7 +131,7 @@ public:
return "decoration"; return "decoration";
} }
Decoration *create(int type) static Decoration *create(DecorationType type)
{ {
switch (type) { switch (type) {
case DECO_SIMPLE: case DECO_SIMPLE:

View File

@ -39,10 +39,10 @@ class MMVManip;
enum OreType { enum OreType {
ORE_TYPE_SCATTER, ORE_SCATTER,
ORE_TYPE_SHEET, ORE_SHEET,
ORE_TYPE_BLOB, ORE_BLOB,
ORE_TYPE_VEIN, ORE_VEIN,
}; };
extern FlagDesc flagdesc_ore[]; extern FlagDesc flagdesc_ore[];
@ -122,16 +122,16 @@ public:
return "ore"; return "ore";
} }
Ore *create(int type) static Ore *create(OreType type)
{ {
switch (type) { switch (type) {
case ORE_TYPE_SCATTER: case ORE_SCATTER:
return new OreScatter; return new OreScatter;
case ORE_TYPE_SHEET: case ORE_SHEET:
return new OreSheet; return new OreSheet;
case ORE_TYPE_BLOB: case ORE_BLOB:
return new OreBlob; return new OreBlob;
case ORE_TYPE_VEIN: case ORE_VEIN:
return new OreVein; return new OreVein;
default: default:
return NULL; return NULL;

View File

@ -41,6 +41,11 @@ class NodeResolver;
#define MTSCHEM_PROB_NEVER 0x00 #define MTSCHEM_PROB_NEVER 0x00
#define MTSCHEM_PROB_ALWAYS 0xFF #define MTSCHEM_PROB_ALWAYS 0xFF
enum SchematicType
{
SCHEMATIC_NORMAL,
};
class Schematic : public ObjDef, public NodeResolver { class Schematic : public ObjDef, public NodeResolver {
public: public:
@ -83,7 +88,7 @@ public:
return "schematic"; return "schematic";
} }
Schematic *create(int type) static Schematic *create(SchematicType type)
{ {
return new Schematic; return new Schematic;
} }

View File

@ -39,11 +39,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
struct EnumString ModApiMapgen::es_BiomeTerrainType[] = struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
{ {
{BIOME_TYPE_NORMAL, "normal"}, {BIOME_NORMAL, "normal"},
{BIOME_TYPE_LIQUID, "liquid"}, {BIOME_LIQUID, "liquid"},
{BIOME_TYPE_NETHER, "nether"}, {BIOME_NETHER, "nether"},
{BIOME_TYPE_AETHER, "aether"}, {BIOME_AETHER, "aether"},
{BIOME_TYPE_FLAT, "flat"}, {BIOME_FLAT, "flat"},
{0, NULL}, {0, NULL},
}; };
@ -68,10 +68,10 @@ struct EnumString ModApiMapgen::es_MapgenObject[] =
struct EnumString ModApiMapgen::es_OreType[] = struct EnumString ModApiMapgen::es_OreType[] =
{ {
{ORE_TYPE_SCATTER, "scatter"}, {ORE_SCATTER, "scatter"},
{ORE_TYPE_SHEET, "sheet"}, {ORE_SHEET, "sheet"},
{ORE_TYPE_BLOB, "blob"}, {ORE_BLOB, "blob"},
{ORE_TYPE_VEIN, "vein"}, {ORE_VEIN, "vein"},
{0, NULL}, {0, NULL},
}; };
@ -86,13 +86,46 @@ struct EnumString ModApiMapgen::es_Rotation[] =
}; };
bool read_schematic_def(lua_State *L, int index, Schematic *schem, ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
Biome *get_or_load_biome(lua_State *L, int index,
BiomeManager *biomemgr);
Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
size_t get_biome_list(lua_State *L, int index,
BiomeManager *biomemgr, std::set<u8> *biome_id_list);
Schematic *get_or_load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names);
Schematic *read_schematic_def(lua_State *L, int index,
INodeDefManager *ndef, StringMap *replace_names); INodeDefManager *ndef, StringMap *replace_names);
Schematic *load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names);
bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco);
bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
Schematic *get_schematic(lua_State *L, int index, ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
// If a number, assume this is a handle to an object def
if (lua_isnumber(L, index))
return objmgr->get(lua_tointeger(L, index));
// If a string, assume a name is given instead
if (lua_isstring(L, index))
return objmgr->getByName(lua_tostring(L, index));
return NULL;
}
Schematic *load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names) SchematicManager *schemmgr, StringMap *replace_names)
{ {
if (index < 0) if (index < 0)
@ -100,23 +133,17 @@ Schematic *get_schematic(lua_State *L, int index,
Schematic *schem; Schematic *schem;
if (lua_isnumber(L, index)) { if (lua_istable(L, index)) {
return (Schematic *)schemmgr->get(lua_tointeger(L, index)); schem = read_schematic_def(L, index, schemmgr->getNodeDef(), replace_names);
} else if (lua_istable(L, index)) { if (!schem) {
schem = new Schematic;
if (!read_schematic_def(L, index, schem,
schemmgr->getNodeDef(), replace_names)) {
delete schem; delete schem;
return NULL; return NULL;
} }
} else if (lua_isstring(L, index)) { } else if (lua_isnumber(L, index)) {
const char *filename = lua_tostring(L, index); return NULL;
schem = (Schematic *)schemmgr->getByName(filename); } else if (lua_isstring(L, index)) {
if (schem) schem = SchematicManager::create(SCHEMATIC_NORMAL);
return schem; if (!schem->loadSchematicFromFile(lua_tostring(L, index),
schem = new Schematic;
if (!schem->loadSchematicFromFile(filename,
schemmgr->getNodeDef(), replace_names)) { schemmgr->getNodeDef(), replace_names)) {
delete schem; delete schem;
return NULL; return NULL;
@ -125,6 +152,127 @@ Schematic *get_schematic(lua_State *L, int index,
return NULL; return NULL;
} }
return schem;
}
Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
if (biome)
return biome;
biome = read_biome_def(L, index, biomemgr->getNodeDef());
if (!biome)
return NULL;
if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
delete biome;
return NULL;
}
return biome;
}
Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
{
if (!lua_istable(L, index))
return NULL;
BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
Biome *b = BiomeManager::create(biometype);
b->name = getstringfield_default(L, index, "name", "");
b->depth_top = getintfield_default(L, index, "depth_top", 1);
b->depth_filler = getintfield_default(L, index, "depth_filler", 2);
b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
b->y_min = getintfield_default(L, index, "y_min", -31000);
b->y_max = getintfield_default(L, index, "y_max", 31000);
b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
b->flags = 0; //reserved
NodeResolveInfo *nri = new NodeResolveInfo(b);
std::list<std::string> &nnames = nri->nodenames;
nnames.push_back(getstringfield_default(L, index, "node_top", ""));
nnames.push_back(getstringfield_default(L, index, "node_filler", ""));
nnames.push_back(getstringfield_default(L, index, "node_stone", ""));
nnames.push_back(getstringfield_default(L, index, "node_water_top", ""));
nnames.push_back(getstringfield_default(L, index, "node_water", ""));
nnames.push_back(getstringfield_default(L, index, "node_dust", ""));
ndef->pendNodeResolve(nri);
return b;
}
size_t get_biome_list(lua_State *L, int index,
BiomeManager *biomemgr, std::set<u8> *biome_id_list)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
if (lua_isnil(L, index))
return 0;
bool is_single = true;
if (lua_istable(L, index)) {
lua_getfield(L, index, "name");
is_single = !lua_isnil(L, -1);
lua_pop(L, 1);
}
if (is_single) {
Biome *biome = get_or_load_biome(L, index, biomemgr);
if (!biome) {
errorstream << "get_biome_list: failed to get biome" << std::endl;
return 1;
}
biome_id_list->insert(biome->index);
return 0;
}
// returns number of failed resolutions
size_t fail_count = 0;
size_t count = 0;
for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
count++;
Biome *biome = get_or_load_biome(L, -1, biomemgr);
if (!biome) {
fail_count++;
errorstream << "get_biome_list: failed to load biome (index "
<< count << ")" << std::endl;
continue;
}
biome_id_list->insert(biome->index);
}
return fail_count;
}
Schematic *get_or_load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
if (schem)
return schem;
schem = load_schematic(L, index, schemmgr, replace_names);
if (!schem)
return NULL;
if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) { if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
delete schem; delete schem;
return NULL; return NULL;
@ -134,9 +282,12 @@ Schematic *get_schematic(lua_State *L, int index,
} }
bool read_schematic_def(lua_State *L, int index, Schematic *schem, Schematic *read_schematic_def(lua_State *L, int index,
INodeDefManager *ndef, StringMap *replace_names) INodeDefManager *ndef, StringMap *replace_names)
{ {
if (!lua_istable(L, index))
return NULL;
//// Get schematic size //// Get schematic size
lua_getfield(L, index, "size"); lua_getfield(L, index, "size");
v3s16 size = read_v3s16(L, -1); v3s16 size = read_v3s16(L, -1);
@ -188,7 +339,7 @@ bool read_schematic_def(lua_State *L, int index, Schematic *schem,
"nodes provided in raw schematic data (got " << i << "nodes provided in raw schematic data (got " << i <<
", expected " << numnodes << ")." << std::endl; ", expected " << numnodes << ")." << std::endl;
delete schemdata; delete schemdata;
return false; return NULL;
} }
//// Get Y-slice probability values (if present) //// Get Y-slice probability values (if present)
@ -208,6 +359,8 @@ bool read_schematic_def(lua_State *L, int index, Schematic *schem,
} }
} }
Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
// Here, we read the nodes directly from the INodeDefManager - there is no // Here, we read the nodes directly from the INodeDefManager - there is no
// need for pending node resolutions so we'll mark this schematic as updated // need for pending node resolutions so we'll mark this schematic as updated
schem->flags = SCHEM_CIDS_UPDATED; schem->flags = SCHEM_CIDS_UPDATED;
@ -215,11 +368,10 @@ bool read_schematic_def(lua_State *L, int index, Schematic *schem,
schem->size = size; schem->size = size;
schem->schemdata = schemdata; schem->schemdata = schemdata;
schem->slice_probs = slice_probs; schem->slice_probs = slice_probs;
return true; return schem;
} }
void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names) void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
{ {
if (index < 0) if (index < 0)
@ -249,6 +401,9 @@ void read_schematic_replacements(lua_State *L, int index, StringMap *replace_nam
} }
///////////////////////////////////////////////////////////////////////////////
// get_mapgen_object(objectname) // get_mapgen_object(objectname)
// returns the requested object used during map generation // returns the requested object used during map generation
int ModApiMapgen::l_get_mapgen_object(lua_State *L) int ModApiMapgen::l_get_mapgen_object(lua_State *L)
@ -269,92 +424,93 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
size_t maplen = mg->csize.X * mg->csize.Z; size_t maplen = mg->csize.X * mg->csize.Z;
switch (mgobj) { switch (mgobj) {
case MGOBJ_VMANIP: { case MGOBJ_VMANIP: {
MMVManip *vm = mg->vm; MMVManip *vm = mg->vm;
// VoxelManip object // VoxelManip object
LuaVoxelManip *o = new LuaVoxelManip(vm, true); LuaVoxelManip *o = new LuaVoxelManip(vm, true);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o; *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, "VoxelManip"); luaL_getmetatable(L, "VoxelManip");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
// emerged min pos // emerged min pos
push_v3s16(L, vm->m_area.MinEdge); push_v3s16(L, vm->m_area.MinEdge);
// emerged max pos // emerged max pos
push_v3s16(L, vm->m_area.MaxEdge); push_v3s16(L, vm->m_area.MaxEdge);
return 3; return 3;
}
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);
} }
case MGOBJ_HEIGHTMAP: {
if (!mg->heightmap)
return 0;
return 1;
}
case MGOBJ_BIOMEMAP: {
if (!mg->biomemap)
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);
}
return 1;
}
case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
case MGOBJ_HUMIDMAP:
if (strcmp(emerge->params.mg_name.c_str(), "v7"))
return 0;
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);
}
return 1;
}
case MGOBJ_GENNOTIFY: {
std::map<std::string, std::vector<v3s16> >event_map;
std::map<std::string, std::vector<v3s16> >::iterator it;
mg->gennotify.getEvents(event_map);
lua_newtable(L);
for (it = event_map.begin(); it != event_map.end(); ++it) {
lua_newtable(L); lua_newtable(L);
for (size_t i = 0; i != maplen; i++) {
lua_pushinteger(L, mg->heightmap[i]); for (size_t j = 0; j != it->second.size(); j++) {
lua_rawseti(L, -2, i + 1); push_v3s16(L, it->second[j]);
lua_rawseti(L, -2, j + 1);
} }
return 1; lua_setfield(L, -2, it->first.c_str());
} }
case MGOBJ_BIOMEMAP: {
if (!mg->biomemap)
return 0;
lua_newtable(L); return 1;
for (size_t i = 0; i != maplen; i++) { }
lua_pushinteger(L, mg->biomemap[i]);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
case MGOBJ_HUMIDMAP:
if (strcmp(emerge->params.mg_name.c_str(), "v7"))
return 0;
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);
}
return 1;
}
case MGOBJ_GENNOTIFY: {
std::map<std::string, std::vector<v3s16> >event_map;
std::map<std::string, std::vector<v3s16> >::iterator it;
mg->gennotify.getEvents(event_map);
lua_newtable(L);
for (it = event_map.begin(); it != event_map.end(); ++it) {
lua_newtable(L);
for (size_t j = 0; j != it->second.size(); j++) {
push_v3s16(L, it->second[j]);
lua_rawseti(L, -2, j + 1);
}
lua_setfield(L, -2, it->first.c_str());
}
return 1;
}
} }
return 0; return 0;
} }
int ModApiMapgen::l_get_mapgen_params(lua_State *L) int ModApiMapgen::l_get_mapgen_params(lua_State *L)
{ {
MapgenParams *params = &getServer(L)->getEmergeManager()->params; MapgenParams *params = &getServer(L)->getEmergeManager()->params;
@ -380,6 +536,7 @@ int ModApiMapgen::l_get_mapgen_params(lua_State *L)
return 1; return 1;
} }
// set_mapgen_params(params) // set_mapgen_params(params)
// set mapgen parameters // set mapgen parameters
int ModApiMapgen::l_set_mapgen_params(lua_State *L) int ModApiMapgen::l_set_mapgen_params(lua_State *L)
@ -419,6 +576,7 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L)
return 0; return 0;
} }
// set_noiseparams(name, noiseparams, set_default) // set_noiseparams(name, noiseparams, set_default)
// set global config values for noise parameters // set global config values for noise parameters
int ModApiMapgen::l_set_noiseparams(lua_State *L) int ModApiMapgen::l_set_noiseparams(lua_State *L)
@ -436,6 +594,7 @@ int ModApiMapgen::l_set_noiseparams(lua_State *L)
return 0; return 0;
} }
// set_gen_notify(flags, {deco_id_table}) // set_gen_notify(flags, {deco_id_table})
int ModApiMapgen::l_set_gen_notify(lua_State *L) int ModApiMapgen::l_set_gen_notify(lua_State *L)
{ {
@ -459,6 +618,7 @@ int ModApiMapgen::l_set_gen_notify(lua_State *L)
return 0; return 0;
} }
// register_biome({lots of stuff}) // register_biome({lots of stuff})
int ModApiMapgen::l_register_biome(lua_State *L) int ModApiMapgen::l_register_biome(lua_State *L)
{ {
@ -468,40 +628,21 @@ int ModApiMapgen::l_register_biome(lua_State *L)
INodeDefManager *ndef = getServer(L)->getNodeDefManager(); INodeDefManager *ndef = getServer(L)->getNodeDefManager();
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr; BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
enum BiomeType biometype = (BiomeType)getenumfield(L, index, "type", Biome *biome = read_biome_def(L, index, ndef);
es_BiomeTerrainType, BIOME_TYPE_NORMAL); if (!biome)
Biome *b = bmgr->create(biometype); return 0;
b->name = getstringfield_default(L, index, "name", ""); ObjDefHandle handle = bmgr->add(biome);
b->depth_top = getintfield_default(L, index, "depth_top", 1);
b->depth_filler = getintfield_default(L, index, "depth_filler", 2);
b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
b->y_min = getintfield_default(L, index, "y_min", -31000);
b->y_max = getintfield_default(L, index, "y_max", 31000);
b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
b->flags = 0; //reserved
ObjDefHandle handle = bmgr->add(b);
if (handle == OBJDEF_INVALID_HANDLE) { if (handle == OBJDEF_INVALID_HANDLE) {
delete b; delete biome;
return 0; return 0;
} }
NodeResolveInfo *nri = new NodeResolveInfo(b);
std::list<std::string> &nnames = nri->nodenames;
nnames.push_back(getstringfield_default(L, index, "node_top", ""));
nnames.push_back(getstringfield_default(L, index, "node_filler", ""));
nnames.push_back(getstringfield_default(L, index, "node_stone", ""));
nnames.push_back(getstringfield_default(L, index, "node_water_top", ""));
nnames.push_back(getstringfield_default(L, index, "node_water", ""));
nnames.push_back(getstringfield_default(L, index, "node_dust", ""));
ndef->pendNodeResolve(nri);
lua_pushinteger(L, handle); lua_pushinteger(L, handle);
return 1; return 1;
} }
// register_decoration({lots of stuff}) // register_decoration({lots of stuff})
int ModApiMapgen::l_register_decoration(lua_State *L) int ModApiMapgen::l_register_decoration(lua_State *L)
{ {
@ -519,7 +660,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
Decoration *deco = decomgr->create(decotype); Decoration *deco = decomgr->create(decotype);
if (!deco) { if (!deco) {
errorstream << "register_decoration: decoration placement type " errorstream << "register_decoration: decoration placement type "
<< decotype << " not implemented"; << decotype << " not implemented" << std::endl;
return 0; return 0;
} }
@ -553,24 +694,19 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
lua_pop(L, 1); lua_pop(L, 1);
//// Get biomes associated with this decoration (if any) //// Get biomes associated with this decoration (if any)
std::vector<const char *> biome_list; lua_getfield(L, index, "biomes");
getstringlistfield(L, index, "biomes", biome_list); if (get_biome_list(L, -1, biomemgr, &deco->biomes))
for (size_t i = 0; i != biome_list.size(); i++) { errorstream << "register_decoration: couldn't get all biomes " << std::endl;
Biome *b = (Biome *)biomemgr->getByName(biome_list[i]); lua_pop(L, 1);
if (!b)
continue;
deco->biomes.insert(b->index);
}
//// Handle decoration type-specific parameters //// Handle decoration type-specific parameters
bool success = false; bool success = false;
switch (decotype) { switch (decotype) {
case DECO_SIMPLE: case DECO_SIMPLE:
success = regDecoSimple(L, nri, (DecoSimple *)deco); success = read_deco_simple(L, nri, (DecoSimple *)deco);
break; break;
case DECO_SCHEMATIC: case DECO_SCHEMATIC:
success = regDecoSchematic(L, schemmgr, (DecoSchematic *)deco); success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
break; break;
case DECO_LSYSTEM: case DECO_LSYSTEM:
break; break;
@ -593,8 +729,8 @@ int ModApiMapgen::l_register_decoration(lua_State *L)
return 1; return 1;
} }
bool ModApiMapgen::regDecoSimple(lua_State *L,
NodeResolveInfo *nri, DecoSimple *deco) bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco)
{ {
int index = 1; int index = 1;
@ -633,13 +769,13 @@ bool ModApiMapgen::regDecoSimple(lua_State *L,
return true; return true;
} }
bool ModApiMapgen::regDecoSchematic(lua_State *L,
SchematicManager *schemmgr, DecoSchematic *deco) bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
{ {
int index = 1; int index = 1;
deco->rotation = (Rotation)getenumfield(L, index, "rotation", deco->rotation = (Rotation)getenumfield(L, index, "rotation",
es_Rotation, ROTATE_0); ModApiMapgen::es_Rotation, ROTATE_0);
StringMap replace_names; StringMap replace_names;
lua_getfield(L, index, "replacements"); lua_getfield(L, index, "replacements");
@ -648,13 +784,14 @@ bool ModApiMapgen::regDecoSchematic(lua_State *L,
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "schematic"); lua_getfield(L, index, "schematic");
Schematic *schem = get_schematic(L, -1, schemmgr, &replace_names); Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
lua_pop(L, 1); lua_pop(L, 1);
deco->schematic = schem; deco->schematic = schem;
return schem != NULL; return schem != NULL;
} }
// register_ore({lots of stuff}) // register_ore({lots of stuff})
int ModApiMapgen::l_register_ore(lua_State *L) int ModApiMapgen::l_register_ore(lua_State *L)
{ {
@ -665,7 +802,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr; OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
enum OreType oretype = (OreType)getenumfield(L, index, enum OreType oretype = (OreType)getenumfield(L, index,
"ore_type", es_OreType, ORE_TYPE_SCATTER); "ore_type", es_OreType, ORE_SCATTER);
Ore *ore = oremgr->create(oretype); Ore *ore = oremgr->create(oretype);
if (!ore) { if (!ore) {
errorstream << "register_ore: ore_type " << oretype << " not implemented"; errorstream << "register_ore: ore_type " << oretype << " not implemented";
@ -716,7 +853,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
} }
lua_pop(L, 1); lua_pop(L, 1);
if (oretype == ORE_TYPE_VEIN) { if (oretype == ORE_VEIN) {
OreVein *orevein = (OreVein *)ore; OreVein *orevein = (OreVein *)ore;
orevein->random_factor = getfloatfield_default(L, index, orevein->random_factor = getfloatfield_default(L, index,
"random_factor", 1.f); "random_factor", 1.f);
@ -743,6 +880,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
return 1; return 1;
} }
// register_schematic({schematic}, replacements={}) // register_schematic({schematic}, replacements={})
int ModApiMapgen::l_register_schematic(lua_State *L) int ModApiMapgen::l_register_schematic(lua_State *L)
{ {
@ -752,7 +890,7 @@ int ModApiMapgen::l_register_schematic(lua_State *L)
if (lua_istable(L, 2)) if (lua_istable(L, 2))
read_schematic_replacements(L, 2, &replace_names); read_schematic_replacements(L, 2, &replace_names);
Schematic *schem = get_schematic(L, 1, schemmgr, &replace_names); Schematic *schem = load_schematic(L, 1, schemmgr, &replace_names);
if (!schem) if (!schem)
return 0; return 0;
@ -766,6 +904,7 @@ int ModApiMapgen::l_register_schematic(lua_State *L)
return 1; return 1;
} }
// clear_registered_biomes() // clear_registered_biomes()
int ModApiMapgen::l_clear_registered_biomes(lua_State *L) int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
{ {
@ -774,6 +913,7 @@ int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
return 0; return 0;
} }
// clear_registered_decorations() // clear_registered_decorations()
int ModApiMapgen::l_clear_registered_decorations(lua_State *L) int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
{ {
@ -782,6 +922,7 @@ int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
return 0; return 0;
} }
// clear_registered_ores() // clear_registered_ores()
int ModApiMapgen::l_clear_registered_ores(lua_State *L) int ModApiMapgen::l_clear_registered_ores(lua_State *L)
{ {
@ -790,6 +931,7 @@ int ModApiMapgen::l_clear_registered_ores(lua_State *L)
return 0; return 0;
} }
// clear_registered_schematics() // clear_registered_schematics()
int ModApiMapgen::l_clear_registered_schematics(lua_State *L) int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
{ {
@ -798,6 +940,7 @@ int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
return 0; return 0;
} }
// generate_ores(vm, p1, p2, [ore_id]) // generate_ores(vm, p1, p2, [ore_id])
int ModApiMapgen::l_generate_ores(lua_State *L) int ModApiMapgen::l_generate_ores(lua_State *L)
{ {
@ -821,6 +964,7 @@ int ModApiMapgen::l_generate_ores(lua_State *L)
return 0; return 0;
} }
// generate_decorations(vm, p1, p2, [deco_id]) // generate_decorations(vm, p1, p2, [deco_id])
int ModApiMapgen::l_generate_decorations(lua_State *L) int ModApiMapgen::l_generate_decorations(lua_State *L)
{ {
@ -844,6 +988,7 @@ int ModApiMapgen::l_generate_decorations(lua_State *L)
return 0; return 0;
} }
// create_schematic(p1, p2, probability_list, filename, y_slice_prob_list) // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
int ModApiMapgen::l_create_schematic(lua_State *L) int ModApiMapgen::l_create_schematic(lua_State *L)
{ {
@ -905,6 +1050,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L)
return 1; return 1;
} }
// place_schematic(p, schematic, rotation, replacement) // place_schematic(p, schematic, rotation, replacement)
int ModApiMapgen::l_place_schematic(lua_State *L) int ModApiMapgen::l_place_schematic(lua_State *L)
{ {
@ -930,7 +1076,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
read_schematic_replacements(L, 4, &replace_names); read_schematic_replacements(L, 4, &replace_names);
//// Read schematic //// Read schematic
Schematic *schem = get_schematic(L, 2, schemmgr, &replace_names); Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
if (!schem) { if (!schem) {
errorstream << "place_schematic: failed to get schematic" << std::endl; errorstream << "place_schematic: failed to get schematic" << std::endl;
return 0; return 0;
@ -943,6 +1089,7 @@ int ModApiMapgen::l_place_schematic(lua_State *L)
return 1; return 1;
} }
void ModApiMapgen::Initialize(lua_State *L, int top) void ModApiMapgen::Initialize(lua_State *L, int top)
{ {
API_FCT(get_mapgen_object); API_FCT(get_mapgen_object);

View File

@ -84,19 +84,14 @@ private:
// place_schematic(p, schematic, rotation, replacement) // place_schematic(p, schematic, rotation, replacement)
static int l_place_schematic(lua_State *L); static int l_place_schematic(lua_State *L);
static bool regDecoSimple(lua_State *L, public:
NodeResolveInfo *nri, DecoSimple *deco); static void Initialize(lua_State *L, int top);
static bool regDecoSchematic(lua_State *L,
SchematicManager *schemmgr, DecoSchematic *deco);
static struct EnumString es_BiomeTerrainType[]; static struct EnumString es_BiomeTerrainType[];
static struct EnumString es_DecorationType[]; static struct EnumString es_DecorationType[];
static struct EnumString es_MapgenObject[]; static struct EnumString es_MapgenObject[];
static struct EnumString es_OreType[]; static struct EnumString es_OreType[];
static struct EnumString es_Rotation[]; static struct EnumString es_Rotation[];
public:
static void Initialize(lua_State *L, int top);
}; };
#endif /* L_MAPGEN_H_ */ #endif /* L_MAPGEN_H_ */