Add Ore infrastructure and l_register_ore()

This commit is contained in:
kwolekr 2013-03-24 01:43:38 -04:00
parent f70378f7f5
commit 57cbb8bfd8
10 changed files with 248 additions and 17 deletions

@ -63,8 +63,9 @@ public:
std::map<v3s16, BlockEmergeData *> blocks_enqueued; std::map<v3s16, BlockEmergeData *> blocks_enqueued;
std::map<u16, u16> peer_queue_count; std::map<u16, u16> peer_queue_count;
//biome manager //Mapgen-related structures
BiomeDefManager *biomedef; BiomeDefManager *biomedef;
std::vector<Ore *> ores;
EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef); EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef);
~EmergeManager(); ~EmergeManager();

@ -49,6 +49,140 @@ FlagDesc flagdesc_mapgen[] = {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
Ore *createOre(OreType type) {
switch (type) {
case ORE_SCATTER:
return new OreScatter;
case ORE_SHEET:
return new OreSheet;
//case ORE_CLAYLIKE: //TODO: implement this!
// return new OreClaylike;
default:
return NULL;
}
}
void Ore::resolveNodeNames(INodeDefManager *ndef) {
if (ore == CONTENT_IGNORE) {
ore = ndef->getId(ore_name);
if (ore == CONTENT_IGNORE) {
errorstream << "Ore::resolveNodeNames: ore node '"
<< ore_name << "' not defined";
ore = CONTENT_AIR;
wherein = CONTENT_AIR;
}
}
if (wherein == CONTENT_IGNORE) {
wherein = ndef->getId(wherein_name);
if (wherein == CONTENT_IGNORE) {
errorstream << "Ore::resolveNodeNames: wherein node '"
<< wherein_name << "' not defined";
ore = CONTENT_AIR;
wherein = CONTENT_AIR;
}
}
}
void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
if (nmin.Y > height_max || nmax.Y < height_min)
return;
resolveNodeNames(mg->ndef);
MapNode n_ore(ore);
ManualMapVoxelManipulator *vm = mg->vm;
PseudoRandom pr(blockseed);
int ymin = MYMAX(nmin.Y, height_min);
int ymax = MYMIN(nmax.Y, height_max);
if (clust_size >= ymax - ymin + 1)
return;
int volume = (nmax.X - nmin.X + 1) *
(nmax.Y - nmin.Y + 1) *
(nmax.Z - nmin.Z + 1);
int csize = clust_size;
int orechance = (csize * csize * csize) / clust_num_ores;
int nclusters = volume / clust_scarcity;
for (int i = 0; i != nclusters; i++) {
int x0 = pr.range(nmin.X, nmax.X - csize + 1);
int y0 = pr.range(ymin, ymax - csize + 1);
int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
continue;
for (int z1 = 0; z1 != csize; z1++)
for (int y1 = 0; y1 != csize; y1++)
for (int x1 = 0; x1 != csize; x1++) {
if (pr.range(1, orechance) != 1)
continue;
u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
if (vm->m_data[i].getContent() == wherein)
vm->m_data[i] = n_ore;
}
}
}
void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
if (nmin.Y > height_max || nmax.Y < height_min)
return;
resolveNodeNames(mg->ndef);
MapNode n_ore(ore);
ManualMapVoxelManipulator *vm = mg->vm;
PseudoRandom pr(blockseed + 4234);
int ymin = MYMAX(nmin.Y, height_min);
int ymax = MYMIN(nmax.Y, height_max);
int x0 = nmin.X;
int z0 = nmin.Z;
int x1 = nmax.X;
int z1 = nmax.Z;
int max_height = clust_size;
int y_start = pr.range(ymin, ymax - max_height);
if (!noise) {
int sx = nmax.X - nmin.X + 1;
int sz = nmax.Z - nmin.Z + 1;
noise = new Noise(np, 0, sx, sz);
}
noise->seed = mg->seed + y_start;
noise->perlinMap2D(x0, z0);
int index = 0;
for (int z = z0; z != z1; z++)
for (int x = x0; x != x1; x++) {
if (noise->result[index++] < nthresh)
continue;
int height = max_height * (1. / pr.range(1, 3));
int y0 = y_start + pr.range(1, 3) - 1;
int y1 = y0 + height;
for (int y = y0; y != y1; y++) {
u32 i = vm->m_area.index(x, y, z);
if (!vm->m_area.contains(i))
continue;
if (vm->m_data[i].getContent() == wherein)
vm->m_data[i] = n_ore;
}
}
}
void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) { void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
bool isliquid, wasliquid; bool isliquid, wasliquid;
v3s16 em = vm->m_area.getExtent(); v3s16 em = vm->m_area.getExtent();

@ -97,5 +97,48 @@ struct MapgenFactory {
virtual MapgenParams *createMapgenParams() = 0; virtual MapgenParams *createMapgenParams() = 0;
}; };
enum OreType {
ORE_SCATTER,
ORE_SHEET,
ORE_CLAYLIKE
};
class Ore {
public:
std::string ore_name;
std::string wherein_name;
content_t ore;
content_t wherein; // the node to be replaced
s16 clust_scarcity; //
s16 clust_num_ores; // how many ore nodes are in a chunk
s16 clust_size; // how large (in nodes) a chunk of ore is
s16 height_min;
s16 height_max;
float nthresh; // threshhold for noise at which an ore is placed
NoiseParams *np; // noise for distribution of clusters (NULL for uniform scattering)
Noise *noise;
Ore() {
ore = CONTENT_IGNORE;
wherein = CONTENT_IGNORE;
np = NULL;
noise = NULL;
}
void resolveNodeNames(INodeDefManager *ndef);
virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
};
class OreScatter : public Ore {
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
};
class OreSheet : public Ore {
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
};
Ore *createOre(OreType type);
#endif #endif

@ -80,7 +80,9 @@ void NoiseIndev::transformNoiseMapFarScale(float xx, float yy, float zz) {
} }
} }
MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params) : MapgenV6(mapgenid, params) { MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge)
: MapgenV6(mapgenid, params, emerge)
{
noiseindev_terrain_base = new NoiseIndev(params->npindev_terrain_base, seed, csize.X, csize.Z); noiseindev_terrain_base = new NoiseIndev(params->npindev_terrain_base, seed, csize.X, csize.Z);
noiseindev_terrain_higher = new NoiseIndev(params->npindev_terrain_higher, seed, csize.X, csize.Z); noiseindev_terrain_higher = new NoiseIndev(params->npindev_terrain_higher, seed, csize.X, csize.Z);
noiseindev_steepness = new NoiseIndev(params->npindev_steepness, seed, csize.X, csize.Z); noiseindev_steepness = new NoiseIndev(params->npindev_steepness, seed, csize.X, csize.Z);

@ -126,7 +126,7 @@ class MapgenIndev : public MapgenV6 {
NoiseIndev *noiseindev_float_islands2; NoiseIndev *noiseindev_float_islands2;
NoiseIndev *noiseindev_float_islands3; NoiseIndev *noiseindev_float_islands3;
MapgenIndev(int mapgenid, MapgenIndevParams *params); MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge);
~MapgenIndev(); ~MapgenIndev();
void calculateNoise(); void calculateNoise();
@ -141,7 +141,7 @@ class MapgenIndev : public MapgenV6 {
struct MapgenFactoryIndev : public MapgenFactoryV6 { struct MapgenFactoryIndev : public MapgenFactoryV6 {
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) {
return new MapgenIndev(mgid, (MapgenIndevParams *)params); return new MapgenIndev(mgid, (MapgenIndevParams *)params, emerge);
}; };
MapgenParams *createMapgenParams() { MapgenParams *createMapgenParams() {

@ -64,9 +64,10 @@ NoiseParams nparams_v6_def_apple_trees =
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) { MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge) {
this->generating = false; this->generating = false;
this->id = mapgenid; this->id = mapgenid;
this->emerge = emerge;
this->seed = (int)params->seed; this->seed = (int)params->seed;
this->water_level = params->water_level; this->water_level = params->water_level;
@ -463,6 +464,12 @@ void MapgenV6::makeChunk(BlockMakeData *data) {
if (flags & MG_TREES) if (flags & MG_TREES)
placeTreesAndJungleGrass(); placeTreesAndJungleGrass();
// Generate the registered ores
for (unsigned int i = 0; i != emerge->ores.size(); i++) {
Ore *ore = emerge->ores[i];
ore->generate(this, blockseed + i, node_min, node_max);
}
// Calculate lighting // Calculate lighting
calcLighting(node_min, node_max); calcLighting(node_min, node_max);
@ -494,14 +501,13 @@ void MapgenV6::calculateNoise() {
noise_height_select->perlinMap2D( noise_height_select->perlinMap2D(
x + 0.5 * noise_height_select->np->spread.X, x + 0.5 * noise_height_select->np->spread.X,
z + 0.5 * noise_height_select->np->spread.Z); z + 0.5 * noise_height_select->np->spread.Z);
}
if (!(flags & MG_FLAT)) {
noise_mud->perlinMap2D( noise_mud->perlinMap2D(
x + 0.5 * noise_mud->np->spread.X, x + 0.5 * noise_mud->np->spread.X,
z + 0.5 * noise_mud->np->spread.Z); z + 0.5 * noise_mud->np->spread.Z);
noise_mud->transformNoiseMap(); noise_mud->transformNoiseMap();
} }
noise_beach->perlinMap2D( noise_beach->perlinMap2D(
x + 0.2 * noise_beach->np->spread.X, x + 0.2 * noise_beach->np->spread.X,
z + 0.7 * noise_beach->np->spread.Z); z + 0.7 * noise_beach->np->spread.Z);

@ -91,6 +91,8 @@ struct MapgenV6Params : public MapgenParams {
class MapgenV6 : public Mapgen { class MapgenV6 : public Mapgen {
public: public:
EmergeManager *emerge;
int ystride; int ystride;
v3s16 csize; v3s16 csize;
u32 flags; u32 flags;
@ -128,7 +130,7 @@ public:
content_t c_desert_sand; content_t c_desert_sand;
content_t c_desert_stone; content_t c_desert_stone;
MapgenV6(int mapgenid, MapgenV6Params *params); MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge);
~MapgenV6(); ~MapgenV6();
void makeChunk(BlockMakeData *data); void makeChunk(BlockMakeData *data);
@ -172,7 +174,7 @@ public:
struct MapgenFactoryV6 : public MapgenFactory { struct MapgenFactoryV6 : public MapgenFactory {
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) {
return new MapgenV6(mgid, (MapgenV6Params *)params); return new MapgenV6(mgid, (MapgenV6Params *)params, emerge);
}; };
MapgenParams *createMapgenParams() { MapgenParams *createMapgenParams() {

@ -30,6 +30,7 @@ extern "C" {
#include "settings.h" // For accessing g_settings #include "settings.h" // For accessing g_settings
#include "main.h" // For g_settings #include "main.h" // For g_settings
#include "biome.h" #include "biome.h"
#include "emerge.h"
#include "script.h" #include "script.h"
#include "rollback.h" #include "rollback.h"
@ -242,6 +243,14 @@ struct EnumString es_BiomeTerrainType[] =
{0, NULL}, {0, NULL},
}; };
struct EnumString es_OreType[] =
{
{ORE_SCATTER, "scatter"},
{ORE_SHEET, "sheet"},
{ORE_CLAYLIKE, "claylike"},
{0, NULL},
};
/*****************************************************************************/ /*****************************************************************************/
/* Parameters */ /* Parameters */
/*****************************************************************************/ /*****************************************************************************/
@ -612,8 +621,6 @@ static int l_register_biome_groups(lua_State *L)
{ {
luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 1, LUA_TTABLE);
int index = 1; int index = 1;
if (!lua_istable(L, index))
throw LuaError(L, "register_biome_groups: parameter is not a table");
BiomeDefManager *bmgr = get_server(L)->getBiomeDef(); BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
if (!bmgr) { if (!bmgr) {
@ -686,6 +693,40 @@ static int l_register_biome(lua_State *L)
} }
static int l_register_ore(lua_State *L)
{
int index = 1;
luaL_checktype(L, index, LUA_TTABLE);
IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
EmergeManager *emerge = get_server(L)->getEmergeManager();
enum OreType oretype = (OreType)getenumfield(L, index,
"ore_type", es_OreType, ORE_SCATTER);
Ore *ore = createOre(oretype);
ore->ore_name = getstringfield_default(L, index, "ore", "");
ore->wherein_name = getstringfield_default(L, index, "wherein", "");
ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 0);
ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 0);
ore->clust_size = getintfield_default(L, index, "clust_size", 0);
ore->height_min = getintfield_default(L, index, "height_min", 0);
ore->height_max = getintfield_default(L, index, "height_max", 0);
ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
lua_getfield(L, index, "noise_params");
ore->np = read_noiseparams(L, -1);
lua_pop(L, 1);
ore->noise = NULL;
emerge->ores.push_back(ore);
verbosestream << "register_ore: ore '" << ore->ore_name
<< "' registered" << std::endl;
return 0;
}
// setting_set(name, value) // setting_set(name, value)
static int l_setting_set(lua_State *L) static int l_setting_set(lua_State *L)
@ -1060,6 +1101,7 @@ static const struct luaL_Reg minetest_f [] = {
{"register_craft", l_register_craft}, {"register_craft", l_register_craft},
{"register_biome", l_register_biome}, {"register_biome", l_register_biome},
{"register_biome_groups", l_register_biome_groups}, {"register_biome_groups", l_register_biome_groups},
{"register_ore", l_register_ore},
{"setting_set", l_setting_set}, {"setting_set", l_setting_set},
{"setting_get", l_setting_get}, {"setting_get", l_setting_get},
{"setting_getbool", l_setting_getbool}, {"setting_getbool", l_setting_getbool},

@ -269,6 +269,7 @@ NoiseParams *read_noiseparams(lua_State *L, int index)
np->scale = getfloatfield_default(L, index, "scale", 0.0); np->scale = getfloatfield_default(L, index, "scale", 0.0);
lua_getfield(L, index, "spread"); lua_getfield(L, index, "spread");
np->spread = read_v3f(L, -1); np->spread = read_v3f(L, -1);
lua_pop(L, 1);
np->seed = getintfield_default(L, index, "seed", 0); np->seed = getintfield_default(L, index, "seed", 0);
np->octaves = getintfield_default(L, index, "octaves", 0); np->octaves = getintfield_default(L, index, "octaves", 0);
np->persist = getfloatfield_default(L, index, "persist", 0.0); np->persist = getfloatfield_default(L, index, "persist", 0.0);
@ -276,6 +277,7 @@ NoiseParams *read_noiseparams(lua_State *L, int index)
return np; return np;
} }
/* /*
LuaPseudoRandom LuaPseudoRandom
*/ */

@ -653,7 +653,6 @@ Server::Server(
m_craftdef(createCraftDefManager()), m_craftdef(createCraftDefManager()),
m_event(new EventManager()), m_event(new EventManager()),
m_thread(this), m_thread(this),
//m_emergethread(this),
m_time_of_day_send_timer(0), m_time_of_day_send_timer(0),
m_uptime(0), m_uptime(0),
m_shutdown_requested(false), m_shutdown_requested(false),
@ -698,7 +697,10 @@ Server::Server(
// Create biome definition manager // Create biome definition manager
m_biomedef = new BiomeDefManager(this); m_biomedef = new BiomeDefManager(this);
// Create emerge manager
m_emerge = new EmergeManager(this, m_biomedef);
// Create rollback manager // Create rollback manager
std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt"; std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
m_rollback = createRollbackManager(rollback_path, this); m_rollback = createRollbackManager(rollback_path, this);
@ -814,9 +816,6 @@ Server::Server(
// Add default biomes after nodedef had its aliases added // Add default biomes after nodedef had its aliases added
m_biomedef->addDefaultBiomes(); m_biomedef->addDefaultBiomes();
// Create emerge manager
m_emerge = new EmergeManager(this, m_biomedef);
// Initialize Environment // Initialize Environment
ServerMap *servermap = new ServerMap(path_world, this, m_emerge); ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
m_env = new ServerEnvironment(servermap, m_lua, this, this); m_env = new ServerEnvironment(servermap, m_lua, this, this);