Engine/Mapgen

From Minetest Developer Wiki
< Engine
Revision as of 15:05, 16 March 2013 by Kwolekr (talk | contribs)
Jump to navigation Jump to search

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