Difference between revisions of "Engine/Mapgen"

From Minetest Developer Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
Here is the bare minimum for a (playable) map generator, given the new infrastructure:
 
Here is the bare minimum for a (playable) map generator, given the new infrastructure:
 
<source lang="cpp">
 
<source lang="cpp">
class MapgenTest : public Mapgen {
+
class MapgenTest : public Mapgen {
ManualMapVoxelManipulator *vmanip;
 
INodeDefManager *ndef;
 
 
 
void makeChunk(BlockMakeData *data) {
 
void makeChunk(BlockMakeData *data) {
vmanip = data->vmanip;
+
vm  = data->vmanip;
ndef   = data->nodedef;
+
ndef = data->nodedef;
 
 
 
v3s16 node_min = data->blockpos_min * MAP_BLOCKSIZE;
 
v3s16 node_min = data->blockpos_min * MAP_BLOCKSIZE;
Line 20: Line 17:
 
for (int y = node_min.Y; y <= node_max.Y; y++) {
 
for (int y = node_min.Y; y <= node_max.Y; y++) {
 
for (int x = node_min.X; x <= node_max.X; x++) {
 
for (int x = node_min.X; x <= node_max.X; x++) {
int i = vmanip->m_area.index(x, y, z);
+
int i = vm->m_area.index(x, y, z);
 
if (y == -4)
 
if (y == -4)
vmanip->m_data[i] = n_grass;
+
vm->m_data[i] = n_grass;
 
else
 
else
vmanip->m_data[i] = n_air;
+
vm->m_data[i] = n_air;
 
}
 
}
 
}
 
}
Line 34: Line 31:
 
int getGroundLevelAtPoint(v2s16 p) {
 
int getGroundLevelAtPoint(v2s16 p) {
 
return 0;
 
return 0;
}
 
 
void updateLighting(v3s16 nmin, v3s16 nmax) {
 
enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
 
 
VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
 
    nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
 
bool block_is_underground = false;
 
bool sunlight = !block_is_underground;
 
 
for (int i = 0; i < 2; i++) {
 
enum LightBank bank = banks[i];
 
core::map<v3s16, bool> light_sources;
 
core::map<v3s16, u8> unlight_from;
 
 
voxalgo::clearLightAndCollectSources(*vmanip, a, bank, ndef, light_sources, unlight_from);
 
voxalgo::propagateSunlight(*vmanip, a, sunlight, light_sources, ndef);
 
vmanip->unspreadLight(bank, unlight_from, light_sources, ndef);
 
vmanip->spreadLight(bank, light_sources, ndef);
 
}
 
 
}
 
}
 
};
 
};

Revision as of 15:05, 16 March 2013

Boilerplate Mapgen Code

Here is the bare minimum for a (playable) map generator, given the new infrastructure:

class MapgenTest : public Mapgen {	
	void makeChunk(BlockMakeData *data) {
		vm   = data->vmanip;
		ndef = data->nodedef;
		
		v3s16 node_min = data->blockpos_min * MAP_BLOCKSIZE;
		v3s16 node_max = (data->blockpos_max + v3s16(1,1,1)) * MAP_BLOCKSIZE - v3s16(1,1,1);
		
		MapNode n_air(ndef->getId("mapgen_air"));
		MapNode n_grass(ndef->getId("mapgen_dirt_with_grass"));
		
		for (int z = node_min.Z; z <= node_max.Z; z++) {
			for (int y = node_min.Y; y <= node_max.Y; y++) {
				for (int x = node_min.X; x <= node_max.X; x++) {
					int i = vm->m_area.index(x, y, z);
					if (y == -4)
						vm->m_data[i] = n_grass;
					else
						vm->m_data[i] = n_air;
				}
			}
		}
		
		updateLighting(node_min, node_max);
	}
	
	int getGroundLevelAtPoint(v2s16 p) {
		return 0;
	}
};

class MapgenTestParams : public MapgenParams {
	bool readParams(Settings *settings) {
		return true;
	}
	
	void writeParams(Settings *settings) {	
	}
};

class MapgenTestFactory : public MapgenFactory {
	Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) {
		return new MapgenTest();
	}
	
	MapgenParams *createMapgenParams() {
		return new MapgenTestParams();
	}
};

Then, simply add

registerMapgen("test", new MapgenTestFactory());

to EmergeManager::EmergeManager().