Difference between revisions of "Engine/Mapgen"

From Minetest Developer Wiki
Jump to navigation Jump to search
(Created page with "== Boilerplate Mapgen Code == Here is the bare minimum for a (playable) map generator, given the new infrastructure: <source lang="cpp"> class MapgenTest : public Mapgen { Ma...")
 
Line 1: Line 1:
 +
{{Template:Core}}
 
== Boilerplate Mapgen Code ==
 
== Boilerplate Mapgen Code ==
 
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:

Revision as of 11:11, 18 February 2013

Template:Core

Boilerplate Mapgen Code

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

class MapgenTest : public Mapgen {
	ManualMapVoxelManipulator *vmanip;
	INodeDefManager *ndef;
	
	void makeChunk(BlockMakeData *data) {
		vmanip = 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 = vmanip->m_area.index(x, y, z);
					if (y == -4)
						vmanip->m_data[i] = n_grass;
					else
						vmanip->m_data[i] = n_air;
				}
			}
		}
		
		updateLighting(node_min, node_max);
	}
	
	int getGroundLevelAtPoint(v2s16 p) {
		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);
		}
	}
};

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().