/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lua_api/l_mapgen.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_vmanip.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "cpp_api/s_security.h"
#include "util/serialize.h"
#include "server.h"
#include "environment.h"
#include "emerge.h"
#include "mapgen/mg_biome.h"
#include "mapgen/mg_ore.h"
#include "mapgen/mg_decoration.h"
#include "mapgen/mg_schematic.h"
#include "mapgen/mapgen_v5.h"
#include "mapgen/mapgen_v7.h"
#include "filesys.h"
#include "settings.h"
#include "log.h"
struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
{
{BIOMETYPE_NORMAL, "normal"},
{0, NULL},
};
struct EnumString ModApiMapgen::es_DecorationType[] =
{
{DECO_SIMPLE, "simple"},
{DECO_SCHEMATIC, "schematic"},
{DECO_LSYSTEM, "lsystem"},
{0, NULL},
};
struct EnumString ModApiMapgen::es_MapgenObject[] =
{
{MGOBJ_VMANIP, "voxelmanip"},
{MGOBJ_HEIGHTMAP, "heightmap"},
{MGOBJ_BIOMEMAP, "biomemap"},
{MGOBJ_HEATMAP, "heatmap"},
{MGOBJ_HUMIDMAP, "humiditymap"},
{MGOBJ_GENNOTIFY, "gennotify"},
{0, NULL},
};
struct EnumString ModApiMapgen::es_OreType[] =
{
{ORE_SCATTER, "scatter"},
{ORE_SHEET, "sheet"},
{ORE_PUFF, "puff"},
{ORE_BLOB, "blob"},
{ORE_VEIN, "vein"},
{ORE_STRATUM, "stratum"},
{0, NULL},
};
struct EnumString ModApiMapgen::es_Rotation[] =
{
{ROTATE_0, "0"},
{ROTATE_90, "90"},
{ROTATE_180, "180"},
{ROTATE_270, "270"},
{ROTATE_RAND, "random"},
{0, NULL},
};
struct EnumString ModApiMapgen::es_SchematicFormatType[] =
{
{SCHEM_FMT_HANDLE, "handle"},
{SCHEM_FMT_MTS, "mts"},
{SCHEM_FMT_LUA, "lua"},
{0, NULL},
};
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, const NodeDefManager *ndef);
size_t get_biome_list(lua_State *L, int index,
BiomeManager *biomemgr, std::unordered_set<u8> *biome_id_list);
Schematic *get_or_load_schematic(lua_State *L, int index,
SchematicManager *schemmgr, StringMap *replace_names);
Schematic *load_schematic(lua_State *L, int index, const NodeDefManager *ndef,
StringMap *replace_names);
Schematic *load_schematic_from_def(lua_State *L, int index,
const NodeDefManager *ndef, StringMap *replace_names);
bool read_schematic_def(lua_State *L, int index,
Schematic *schem, std::vector<std::string> *names);
bool read_deco_simple(lua_State *L, DecoSimple *deco);
bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
///////////////////////////////////////////////////////////////////////////////
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 *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->getNodeDef(),
replace_names);
if (!schem)
return NULL;
if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
delete schem;
return NULL;
}
return schem;
}
Schematic *load_schematic(lua_State *L, int index, const NodeDefManager *ndef,
StringMap *replace_names)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
Schematic *schem = NULL;
if (lua_istable(L, index)) {
schem = load_schematic_from_def(L, index, ndef,
replace_names);
if (!schem) {
delete schem;
return NULL;
}
} else if (lua_isnumber(L, index)) {
return NULL;
} else if (lua_isstring(L, index)) {
schem = SchematicManager::create(SCHEMATIC_NORMAL);
std::string filepath = lua_tostring(L, index);
if (!fs::IsPathAbsolute(filepath))
filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
if (!schem->loadSchematicFromFile(filepath, ndef,
replace_names)) {
delete schem;
return NULL;
}
}
return schem;
}
Schematic *load_schematic_from_def(lua_State *L, int index,
const NodeDefManager *ndef, StringMap *replace_names)
{
Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
delete schem;
return NULL;
}
size_t num_nodes = schem->m_nodenames.size();
schem->m_nnlistsizes.push_back(num_nodes);
if (replace_names) {
for (size_t i = 0; i != num_nodes; i++) {
StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
if (it != replace_names->end())
schem->m_nodenames[i] = it->second;
}
}
if (ndef)
ndef->pendNodeResolve(schem);
return schem;
}
bool read_schematic_def(lua_State *L, int index,
Schematic *schem, std::vector<std::string> *names)
{
if (!lua_istable(L, index))
return false;
//// Get schematic size
lua_getfield(L, index, "size");
v3s16 size = check_v3s16(L, -1);
lua_pop(L, 1);
schem->size = size;
//// Get schematic data
lua_getfield(L, index, "data");
luaL_checktype(L, -1, LUA_TTABLE);
u32 numnodes = size.X * size.Y * size.Z;
schem->schemdata = new MapNode[numnodes];
size_t names_base = names->size();
std::unordered_map<std::string, content_t> name_id_map;
u32 i = 0;
for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
if (i >= numnodes)
continue;
//// Read name
std::string name;
if (!getstringfield(L, -1, "name", name))
throw LuaError("Schematic data definition with missing name field");
|