Skip to content

Commit 57cbb8b

Browse files
committedMar 24, 2013
Add Ore infrastructure and l_register_ore()
1 parent f70378f commit 57cbb8b

10 files changed

+248
-17
lines changed
 

‎src/emerge.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ class EmergeManager {
6363
std::map<v3s16, BlockEmergeData *> blocks_enqueued;
6464
std::map<u16, u16> peer_queue_count;
6565

66-
//biome manager
66+
//Mapgen-related structures
6767
BiomeDefManager *biomedef;
68+
std::vector<Ore *> ores;
6869

6970
EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef);
7071
~EmergeManager();

‎src/mapgen.cpp

+134
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,140 @@ FlagDesc flagdesc_mapgen[] = {
4949
///////////////////////////////////////////////////////////////////////////////
5050

5151

52+
Ore *createOre(OreType type) {
53+
switch (type) {
54+
case ORE_SCATTER:
55+
return new OreScatter;
56+
case ORE_SHEET:
57+
return new OreSheet;
58+
//case ORE_CLAYLIKE: //TODO: implement this!
59+
// return new OreClaylike;
60+
default:
61+
return NULL;
62+
}
63+
}
64+
65+
66+
void Ore::resolveNodeNames(INodeDefManager *ndef) {
67+
if (ore == CONTENT_IGNORE) {
68+
ore = ndef->getId(ore_name);
69+
if (ore == CONTENT_IGNORE) {
70+
errorstream << "Ore::resolveNodeNames: ore node '"
71+
<< ore_name << "' not defined";
72+
ore = CONTENT_AIR;
73+
wherein = CONTENT_AIR;
74+
}
75+
}
76+
77+
if (wherein == CONTENT_IGNORE) {
78+
wherein = ndef->getId(wherein_name);
79+
if (wherein == CONTENT_IGNORE) {
80+
errorstream << "Ore::resolveNodeNames: wherein node '"
81+
<< wherein_name << "' not defined";
82+
ore = CONTENT_AIR;
83+
wherein = CONTENT_AIR;
84+
}
85+
}
86+
}
87+
88+
89+
void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
90+
if (nmin.Y > height_max || nmax.Y < height_min)
91+
return;
92+
93+
resolveNodeNames(mg->ndef);
94+
95+
MapNode n_ore(ore);
96+
ManualMapVoxelManipulator *vm = mg->vm;
97+
PseudoRandom pr(blockseed);
98+
99+
int ymin = MYMAX(nmin.Y, height_min);
100+
int ymax = MYMIN(nmax.Y, height_max);
101+
if (clust_size >= ymax - ymin + 1)
102+
return;
103+
104+
int volume = (nmax.X - nmin.X + 1) *
105+
(nmax.Y - nmin.Y + 1) *
106+
(nmax.Z - nmin.Z + 1);
107+
int csize = clust_size;
108+
int orechance = (csize * csize * csize) / clust_num_ores;
109+
int nclusters = volume / clust_scarcity;
110+
111+
for (int i = 0; i != nclusters; i++) {
112+
int x0 = pr.range(nmin.X, nmax.X - csize + 1);
113+
int y0 = pr.range(ymin, ymax - csize + 1);
114+
int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
115+
116+
if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
117+
continue;
118+
119+
for (int z1 = 0; z1 != csize; z1++)
120+
for (int y1 = 0; y1 != csize; y1++)
121+
for (int x1 = 0; x1 != csize; x1++) {
122+
if (pr.range(1, orechance) != 1)
123+
continue;
124+
125+
u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
126+
if (vm->m_data[i].getContent() == wherein)
127+
vm->m_data[i] = n_ore;
128+
}
129+
}
130+
}
131+
132+
133+
void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
134+
if (nmin.Y > height_max || nmax.Y < height_min)
135+
return;
136+
137+
resolveNodeNames(mg->ndef);
138+
139+
MapNode n_ore(ore);
140+
ManualMapVoxelManipulator *vm = mg->vm;
141+
PseudoRandom pr(blockseed + 4234);
142+
143+
int ymin = MYMAX(nmin.Y, height_min);
144+
int ymax = MYMIN(nmax.Y, height_max);
145+
146+
int x0 = nmin.X;
147+
int z0 = nmin.Z;
148+
149+
int x1 = nmax.X;
150+
int z1 = nmax.Z;
151+
152+
int max_height = clust_size;
153+
154+
int y_start = pr.range(ymin, ymax - max_height);
155+
156+
if (!noise) {
157+
int sx = nmax.X - nmin.X + 1;
158+
int sz = nmax.Z - nmin.Z + 1;
159+
noise = new Noise(np, 0, sx, sz);
160+
}
161+
noise->seed = mg->seed + y_start;
162+
noise->perlinMap2D(x0, z0);
163+
164+
int index = 0;
165+
for (int z = z0; z != z1; z++)
166+
for (int x = x0; x != x1; x++) {
167+
168+
if (noise->result[index++] < nthresh)
169+
continue;
170+
171+
int height = max_height * (1. / pr.range(1, 3));
172+
int y0 = y_start + pr.range(1, 3) - 1;
173+
int y1 = y0 + height;
174+
for (int y = y0; y != y1; y++) {
175+
u32 i = vm->m_area.index(x, y, z);
176+
if (!vm->m_area.contains(i))
177+
continue;
178+
179+
if (vm->m_data[i].getContent() == wherein)
180+
vm->m_data[i] = n_ore;
181+
}
182+
}
183+
}
184+
185+
52186
void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
53187
bool isliquid, wasliquid;
54188
v3s16 em = vm->m_area.getExtent();

‎src/mapgen.h

+43
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,48 @@ struct MapgenFactory {
9797
virtual MapgenParams *createMapgenParams() = 0;
9898
};
9999

100+
enum OreType {
101+
ORE_SCATTER,
102+
ORE_SHEET,
103+
ORE_CLAYLIKE
104+
};
105+
106+
class Ore {
107+
public:
108+
std::string ore_name;
109+
std::string wherein_name;
110+
111+
content_t ore;
112+
content_t wherein; // the node to be replaced
113+
s16 clust_scarcity; //
114+
s16 clust_num_ores; // how many ore nodes are in a chunk
115+
s16 clust_size; // how large (in nodes) a chunk of ore is
116+
s16 height_min;
117+
s16 height_max;
118+
float nthresh; // threshhold for noise at which an ore is placed
119+
NoiseParams *np; // noise for distribution of clusters (NULL for uniform scattering)
120+
Noise *noise;
121+
122+
Ore() {
123+
ore = CONTENT_IGNORE;
124+
wherein = CONTENT_IGNORE;
125+
np = NULL;
126+
noise = NULL;
127+
}
128+
129+
void resolveNodeNames(INodeDefManager *ndef);
130+
virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
131+
};
132+
133+
class OreScatter : public Ore {
134+
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
135+
};
136+
137+
class OreSheet : public Ore {
138+
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
139+
};
140+
141+
Ore *createOre(OreType type);
142+
100143
#endif
101144

‎src/mapgen_indev.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ void NoiseIndev::transformNoiseMapFarScale(float xx, float yy, float zz) {
8080
}
8181
}
8282

83-
MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params) : MapgenV6(mapgenid, params) {
83+
MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge)
84+
: MapgenV6(mapgenid, params, emerge)
85+
{
8486
noiseindev_terrain_base = new NoiseIndev(params->npindev_terrain_base, seed, csize.X, csize.Z);
8587
noiseindev_terrain_higher = new NoiseIndev(params->npindev_terrain_higher, seed, csize.X, csize.Z);
8688
noiseindev_steepness = new NoiseIndev(params->npindev_steepness, seed, csize.X, csize.Z);

‎src/mapgen_indev.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class MapgenIndev : public MapgenV6 {
126126
NoiseIndev *noiseindev_float_islands2;
127127
NoiseIndev *noiseindev_float_islands3;
128128

129-
MapgenIndev(int mapgenid, MapgenIndevParams *params);
129+
MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge);
130130
~MapgenIndev();
131131
void calculateNoise();
132132

@@ -141,7 +141,7 @@ class MapgenIndev : public MapgenV6 {
141141

142142
struct MapgenFactoryIndev : public MapgenFactoryV6 {
143143
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) {
144-
return new MapgenIndev(mgid, (MapgenIndevParams *)params);
144+
return new MapgenIndev(mgid, (MapgenIndevParams *)params, emerge);
145145
};
146146

147147
MapgenParams *createMapgenParams() {

‎src/mapgen_v6.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ NoiseParams nparams_v6_def_apple_trees =
6464
///////////////////////////////////////////////////////////////////////////////
6565

6666

67-
MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) {
67+
MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge) {
6868
this->generating = false;
6969
this->id = mapgenid;
70+
this->emerge = emerge;
7071

7172
this->seed = (int)params->seed;
7273
this->water_level = params->water_level;
@@ -463,6 +464,12 @@ void MapgenV6::makeChunk(BlockMakeData *data) {
463464
if (flags & MG_TREES)
464465
placeTreesAndJungleGrass();
465466

467+
// Generate the registered ores
468+
for (unsigned int i = 0; i != emerge->ores.size(); i++) {
469+
Ore *ore = emerge->ores[i];
470+
ore->generate(this, blockseed + i, node_min, node_max);
471+
}
472+
466473
// Calculate lighting
467474
calcLighting(node_min, node_max);
468475

@@ -494,14 +501,13 @@ void MapgenV6::calculateNoise() {
494501
noise_height_select->perlinMap2D(
495502
x + 0.5 * noise_height_select->np->spread.X,
496503
z + 0.5 * noise_height_select->np->spread.Z);
497-
}
498-
499-
if (!(flags & MG_FLAT)) {
504+
500505
noise_mud->perlinMap2D(
501506
x + 0.5 * noise_mud->np->spread.X,
502507
z + 0.5 * noise_mud->np->spread.Z);
503508
noise_mud->transformNoiseMap();
504509
}
510+
505511
noise_beach->perlinMap2D(
506512
x + 0.2 * noise_beach->np->spread.X,
507513
z + 0.7 * noise_beach->np->spread.Z);

‎src/mapgen_v6.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct MapgenV6Params : public MapgenParams {
9191

9292
class MapgenV6 : public Mapgen {
9393
public:
94+
EmergeManager *emerge;
95+
9496
int ystride;
9597
v3s16 csize;
9698
u32 flags;
@@ -128,7 +130,7 @@ class MapgenV6 : public Mapgen {
128130
content_t c_desert_sand;
129131
content_t c_desert_stone;
130132

131-
MapgenV6(int mapgenid, MapgenV6Params *params);
133+
MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge);
132134
~MapgenV6();
133135

134136
void makeChunk(BlockMakeData *data);
@@ -172,7 +174,7 @@ class MapgenV6 : public Mapgen {
172174

173175
struct MapgenFactoryV6 : public MapgenFactory {
174176
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) {
175-
return new MapgenV6(mgid, (MapgenV6Params *)params);
177+
return new MapgenV6(mgid, (MapgenV6Params *)params, emerge);
176178
};
177179

178180
MapgenParams *createMapgenParams() {

‎src/scriptapi.cpp

+44-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
#include "settings.h" // For accessing g_settings
3131
#include "main.h" // For g_settings
3232
#include "biome.h"
33+
#include "emerge.h"
3334
#include "script.h"
3435
#include "rollback.h"
3536

@@ -242,6 +243,14 @@ struct EnumString es_BiomeTerrainType[] =
242243
{0, NULL},
243244
};
244245

246+
struct EnumString es_OreType[] =
247+
{
248+
{ORE_SCATTER, "scatter"},
249+
{ORE_SHEET, "sheet"},
250+
{ORE_CLAYLIKE, "claylike"},
251+
{0, NULL},
252+
};
253+
245254
/*****************************************************************************/
246255
/* Parameters */
247256
/*****************************************************************************/
@@ -612,8 +621,6 @@ static int l_register_biome_groups(lua_State *L)
612621
{
613622
luaL_checktype(L, 1, LUA_TTABLE);
614623
int index = 1;
615-
if (!lua_istable(L, index))
616-
throw LuaError(L, "register_biome_groups: parameter is not a table");
617624

618625
BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
619626
if (!bmgr) {
@@ -686,6 +693,40 @@ static int l_register_biome(lua_State *L)
686693
}
687694

688695

696+
static int l_register_ore(lua_State *L)
697+
{
698+
int index = 1;
699+
luaL_checktype(L, index, LUA_TTABLE);
700+
701+
IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
702+
EmergeManager *emerge = get_server(L)->getEmergeManager();
703+
704+
enum OreType oretype = (OreType)getenumfield(L, index,
705+
"ore_type", es_OreType, ORE_SCATTER);
706+
Ore *ore = createOre(oretype);
707+
708+
ore->ore_name = getstringfield_default(L, index, "ore", "");
709+
ore->wherein_name = getstringfield_default(L, index, "wherein", "");
710+
ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 0);
711+
ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 0);
712+
ore->clust_size = getintfield_default(L, index, "clust_size", 0);
713+
ore->height_min = getintfield_default(L, index, "height_min", 0);
714+
ore->height_max = getintfield_default(L, index, "height_max", 0);
715+
ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
716+
717+
lua_getfield(L, index, "noise_params");
718+
ore->np = read_noiseparams(L, -1);
719+
lua_pop(L, 1);
720+
721+
ore->noise = NULL;
722+
723+
emerge->ores.push_back(ore);
724+
725+
verbosestream << "register_ore: ore '" << ore->ore_name
726+
<< "' registered" << std::endl;
727+
return 0;
728+
}
729+
689730

690731
// setting_set(name, value)
691732
static int l_setting_set(lua_State *L)
@@ -1060,6 +1101,7 @@ static const struct luaL_Reg minetest_f [] = {
10601101
{"register_craft", l_register_craft},
10611102
{"register_biome", l_register_biome},
10621103
{"register_biome_groups", l_register_biome_groups},
1104+
{"register_ore", l_register_ore},
10631105
{"setting_set", l_setting_set},
10641106
{"setting_get", l_setting_get},
10651107
{"setting_getbool", l_setting_getbool},

‎src/scriptapi_noise.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,15 @@ NoiseParams *read_noiseparams(lua_State *L, int index)
269269
np->scale = getfloatfield_default(L, index, "scale", 0.0);
270270
lua_getfield(L, index, "spread");
271271
np->spread = read_v3f(L, -1);
272+
lua_pop(L, 1);
272273
np->seed = getintfield_default(L, index, "seed", 0);
273274
np->octaves = getintfield_default(L, index, "octaves", 0);
274275
np->persist = getfloatfield_default(L, index, "persist", 0.0);
275276

276277
return np;
277278
}
278279

280+
279281
/*
280282
LuaPseudoRandom
281283
*/

‎src/server.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,6 @@ Server::Server(
653653
m_craftdef(createCraftDefManager()),
654654
m_event(new EventManager()),
655655
m_thread(this),
656-
//m_emergethread(this),
657656
m_time_of_day_send_timer(0),
658657
m_uptime(0),
659658
m_shutdown_requested(false),
@@ -698,7 +697,10 @@ Server::Server(
698697

699698
// Create biome definition manager
700699
m_biomedef = new BiomeDefManager(this);
701-
700+
701+
// Create emerge manager
702+
m_emerge = new EmergeManager(this, m_biomedef);
703+
702704
// Create rollback manager
703705
std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt";
704706
m_rollback = createRollbackManager(rollback_path, this);
@@ -814,9 +816,6 @@ Server::Server(
814816
// Add default biomes after nodedef had its aliases added
815817
m_biomedef->addDefaultBiomes();
816818

817-
// Create emerge manager
818-
m_emerge = new EmergeManager(this, m_biomedef);
819-
820819
// Initialize Environment
821820
ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
822821
m_env = new ServerEnvironment(servermap, m_lua, this, this);

0 commit comments

Comments
 (0)
Please sign in to comment.