Difference between revisions of "Engine/Mapgen"
< Engine
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 { |
− | |||
− | |||
− | |||
void makeChunk(BlockMakeData *data) { | void makeChunk(BlockMakeData *data) { | ||
− | + | vm = data->vmanip; | |
− | ndef | + | 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 = | + | int i = vm->m_area.index(x, y, z); |
if (y == -4) | if (y == -4) | ||
− | + | vm->m_data[i] = n_grass; | |
else | else | ||
− | + | vm->m_data[i] = n_air; | |
} | } | ||
} | } | ||
Line 34: | Line 31: | ||
int getGroundLevelAtPoint(v2s16 p) { | int getGroundLevelAtPoint(v2s16 p) { | ||
return 0; | return 0; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
}; | }; |
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().