#include "stdafx.h" #include "StringHelpers.h" #include "net.minecraft.world.level.levelgen.flat.h" #include "net.minecraft.world.level.tile.h" #include "net.minecraft.world.level.biome.h" #include "FlatGeneratorInfo.h" const wstring FlatGeneratorInfo::STRUCTURE_VILLAGE = L"village"; const wstring FlatGeneratorInfo::STRUCTURE_BIOME_SPECIFIC = L"biome_1"; const wstring FlatGeneratorInfo::STRUCTURE_STRONGHOLD = L"stronghold"; const wstring FlatGeneratorInfo::STRUCTURE_MINESHAFT = L"mineshaft"; const wstring FlatGeneratorInfo::STRUCTURE_BIOME_DECORATION = L"decoration"; const wstring FlatGeneratorInfo::STRUCTURE_LAKE = L"lake"; const wstring FlatGeneratorInfo::STRUCTURE_LAVA_LAKE = L"lava_lake"; const wstring FlatGeneratorInfo::STRUCTURE_DUNGEON = L"dungeon"; const wstring FlatGeneratorInfo::STRUCTURE_CAVES = L"caves"; const wstring FlatGeneratorInfo::STRUCTURE_RAVINES = L"ravine"; const wstring FlatGeneratorInfo::STRUCTURE_OCEAN_MONUMENT = L"oceanmonument"; FlatGeneratorInfo::FlatGeneratorInfo() { biome = 1; } int FlatGeneratorInfo::getBiome() { return biome; } void FlatGeneratorInfo::setBiome(int biome) { this->biome = biome; } unordered_map > *FlatGeneratorInfo::getStructures() { return &structures; } bool FlatGeneratorInfo::hasStructure(const wstring &name) { return structures.find(name) != structures.end(); } void FlatGeneratorInfo::setStructure(const wstring &name, bool enabled) { if (enabled) { if (!hasStructure(name)) structures[name] = unordered_map(); } else { structures.erase(name); } } wstring FlatGeneratorInfo::getOption(const wstring &name, const wstring &option, const wstring &fallback) { auto it = structures.find(name); if (it == structures.end()) return fallback; auto opt = it->second.find(option); if (opt == it->second.end()) return fallback; return opt->second; } int FlatGeneratorInfo::getIntOption(const wstring &name, const wstring &option, int fallback) { wstring value = getOption(name, option, L""); if (value.empty()) return fallback; return Mth::getInt(value, fallback); } void FlatGeneratorInfo::setOption(const wstring &name, const wstring &option, const wstring &value) { structures[name][option] = value; } void FlatGeneratorInfo::setIntOption(const wstring &name, const wstring &option, int value) { setOption(name, option, std::to_wstring(value)); } vector *FlatGeneratorInfo::getLayers() { return &layers; } int FlatGeneratorInfo::getTotalHeight() { int total = 0; for (auto &layer : layers) total += layer.getHeight(); return total; } void FlatGeneratorInfo::updateLayers() { int y = 0; for (auto &layer : layers) { layer.setStart(y); y += layer.getHeight(); } } void FlatGeneratorInfo::addLayerOnTop(int height, int id, int data) { layers.push_back(FlatLayerInfo(height, id, data)); updateLayers(); } void FlatGeneratorInfo::clear() { layers.clear(); structures.clear(); biome = 1; } wstring FlatGeneratorInfo::toString() { wstring result = std::to_wstring(SERIALIZATION_VERSION); result += L";"; for (size_t i = 0; i < layers.size(); i++) { if (i > 0) result += L","; result += layers[i].toString(); } result += L";"; result += std::to_wstring(biome); result += L";"; int structCount = 0; for (auto &structure : structures) { if (structCount++ > 0) result += L","; result += structure.first; if (!structure.second.empty()) { result += L"("; int optionCount = 0; for (auto &option : structure.second) { if (optionCount++ > 0) result += L" "; result += option.first + L"=" + option.second; } result += L")"; } } return result; } bool FlatGeneratorInfo::parseLayer(const wstring &input, FlatLayerInfo &out) { if (input.empty()) return false; int height = 1; wstring identity = input; size_t x = input.find(L'x'); if (x != wstring::npos) { height = Mth::getInt(input.substr(0, x), 1); identity = input.substr(x + 1); } int id = 0; int data = 0; size_t colon = identity.find(L':'); if (colon != wstring::npos) { id = Mth::getInt(identity.substr(0, colon), 0); data = Mth::getInt(identity.substr(colon + 1), 0); } else { id = Mth::getInt(identity, 0); } if (id < 0 || id >= 256 || (id != 0 && Tile::tiles[id] == nullptr)) { id = 0; data = 0; } if (data < 0 || data > 15) data = 0; if (height < 1) height = 1; out = FlatLayerInfo(height, id, data); return true; } FlatGeneratorInfo FlatGeneratorInfo::fromValue(const wstring &input) { if (input.empty()) return getDefault(); std::vector parts = stringSplit(input, L';'); if (parts.size() < 2) return getDefault(); int version = Mth::getInt(parts[0], -1); if (version < 0 || version > SERIALIZATION_VERSION) return getDefault(); FlatGeneratorInfo result; result.structures.clear(); int total = 0; std::vector depths = stringSplit(parts[1], L','); for (auto &depth : depths) { FlatLayerInfo layer(1, 0); if (!parseLayer(depth, layer)) continue; if (total + layer.getHeight() > MAX_HEIGHT) layer.setHeight(MAX_HEIGHT - total); if (layer.getHeight() <= 0) break; total += layer.getHeight(); result.layers.push_back(layer); if ((int)result.layers.size() >= MAX_LAYERS) break; } result.updateLayers(); int biome = 1; if (parts.size() > 2) biome = Mth::getInt(parts[2], 1); if (biome < 0 || biome >= 256 || Biome::biomes[biome] == nullptr) biome = 1; result.setBiome(biome); if (parts.size() > 3 && !parts[3].empty()) { std::vector entries = stringSplit(parts[3], L','); for (auto &entry : entries) { wstring name = entry; wstring options; size_t open = entry.find(L'('); if (open != wstring::npos) { name = entry.substr(0, open); size_t close = entry.find(L')', open); options = entry.substr(open + 1, close == wstring::npos ? wstring::npos : close - open - 1); } if (name.empty()) continue; unordered_map &opts = result.structures[name]; if (!options.empty()) { std::vector pairs = stringSplit(options, L' '); for (auto &pair : pairs) { size_t eq = pair.find(L'='); if (eq != wstring::npos && eq > 0) opts[pair.substr(0, eq)] = pair.substr(eq + 1); } } } } return result; } FlatGeneratorInfo FlatGeneratorInfo::getDefault() { FlatGeneratorInfo result; result.setBiome(Biome::plains->id); result.layers.push_back(FlatLayerInfo(1, Tile::bedrock_Id)); result.layers.push_back(FlatLayerInfo(2, Tile::dirt_Id)); result.layers.push_back(FlatLayerInfo(1, Tile::grass_Id)); result.updateLayers(); result.structures[STRUCTURE_VILLAGE] = unordered_map(); return result; }