1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Decoration: Fix schematic probability mess with new MTS file version

This commit is contained in:
kwolekr 2013-08-04 22:59:22 -04:00
parent 56b9377c1c
commit c06caa14c3
8 changed files with 75 additions and 37 deletions

View file

@ -507,16 +507,19 @@ void DecoSchematic::resolveNodeNames(INodeDefManager *ndef) {
for (size_t i = 0; i != node_names->size(); i++) {
std::string name = node_names->at(i);
std::map<std::string, std::string>::iterator it;
it = replacements.find(name);
if (it != replacements.end())
name = it->second;
content_t c = ndef->getId(name);
if (c == CONTENT_IGNORE) {
errorstream << "DecoSchematic::resolveNodeNames: node '"
<< node_names->at(i) << "' not defined" << std::endl;
<< name << "' not defined" << std::endl;
c = CONTENT_AIR;
}
c_nodes.push_back(c);
}
@ -605,6 +608,9 @@ void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
if (schematic[i].getContent() == CONTENT_IGNORE)
continue;
if (schematic[i].param1 == MTSCHEM_PROB_NEVER)
continue;
if (!force_placement) {
content_t c = vm->m_data[vi].getContent();
@ -612,7 +618,8 @@ void DecoSchematic::blitToVManip(v3s16 p, ManualMapVoxelManipulator *vm,
continue;
}
if (schematic[i].param1 && myrand_range(1, 256) > schematic[i].param1)
if (schematic[i].param1 != MTSCHEM_PROB_ALWAYS &&
myrand_range(1, 255) > schematic[i].param1)
continue;
vm->m_data[vi] = schematic[i];
@ -668,6 +675,9 @@ void DecoSchematic::placeStructure(Map *map, v3s16 p) {
bool DecoSchematic::loadSchematicFile() {
content_t cignore = CONTENT_IGNORE;
bool have_cignore = false;
std::ifstream is(filename.c_str(), std::ios_base::binary);
u32 signature = readU32(is);
@ -678,7 +688,7 @@ bool DecoSchematic::loadSchematicFile() {
}
u16 version = readU16(is);
if (version != 1) {
if (version > 2) {
errorstream << "loadSchematicFile: unsupported schematic "
"file version" << std::endl;
return false;
@ -692,6 +702,11 @@ bool DecoSchematic::loadSchematicFile() {
node_names = new std::vector<std::string>;
for (int i = 0; i != nidmapcount; i++) {
std::string name = deSerializeString(is);
if (name == "ignore") {
name = "air";
cignore = i;
have_cignore = true;
}
node_names->push_back(name);
}
@ -699,7 +714,16 @@ bool DecoSchematic::loadSchematicFile() {
schematic = new MapNode[nodecount];
MapNode::deSerializeBulk(is, SER_FMT_VER_HIGHEST_READ, schematic,
nodecount, 2, 2, true);
if (version == 1) { // fix up the probability values
for (int i = 0; i != nodecount; i++) {
if (schematic[i].param1 == 0)
schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
if (have_cignore && schematic[i].getContent() == cignore)
schematic[i].param1 = MTSCHEM_PROB_NEVER;
}
}
return true;
}
@ -709,7 +733,7 @@ bool DecoSchematic::loadSchematicFile() {
All values are stored in big-endian byte order.
[u32] signature: 'MTSM'
[u16] version: 1
[u16] version: 2
[u16] size X
[u16] size Y
[u16] size Z
@ -726,12 +750,16 @@ bool DecoSchematic::loadSchematicFile() {
For each node in schematic:
[u8] param2
}
Version changes:
1 - Initial version
2 - Fixed messy never/always place; 0 probability is now never, 0xFF is always
*/
void DecoSchematic::saveSchematicFile(INodeDefManager *ndef) {
std::ofstream os(filename.c_str(), std::ios_base::binary);
writeU32(os, MTSCHEM_FILE_SIGNATURE); // signature
writeU16(os, 1); // version
writeU16(os, 2); // version
writeV3S16(os, size); // schematic size
std::vector<content_t> usednodes;
@ -789,7 +817,7 @@ bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
u32 vi = vm->m_area.index(p1.X, y, z);
for (s16 x = p1.X; x <= p2.X; x++, i++, vi++) {
schematic[i] = vm->m_data[vi];
schematic[i].param1 = 0;
schematic[i].param1 = MTSCHEM_PROB_ALWAYS;
}
}
@ -798,16 +826,18 @@ bool DecoSchematic::getSchematicFromMap(Map *map, v3s16 p1, v3s16 p2) {
}
void DecoSchematic::applyProbabilities(std::vector<std::pair<v3s16, s16> > *plist, v3s16 p0) {
void DecoSchematic::applyProbabilities(std::vector<std::pair<v3s16, u8> > *plist,
v3s16 p0) {
for (size_t i = 0; i != plist->size(); i++) {
v3s16 p = (*plist)[i].first - p0;
int index = p.Z * (size.Y * size.X) + p.Y * size.X + p.X;
if (index < size.Z * size.Y * size.X) {
s16 prob = (*plist)[i].second;
if (prob != -1)
schematic[index].param1 = prob;
else
schematic[index].setContent(CONTENT_IGNORE);
u8 prob = (*plist)[i].second;
schematic[index].param1 = prob;
// trim unnecessary node names from schematic
if (prob == MTSCHEM_PROB_NEVER)
schematic[index].setContent(CONTENT_AIR);
}
}
}