// This file is part of c-evo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. #include "Cevo/Map.h" #include "Cevo/Protocol.h" #include #include #include #include #include #include #include namespace Cevo { namespace { constexpr char fileId[8] = "cEvoMap"; // 7 chars + implicit trailing NUL = 8 bytes // Local equivalent of common/XML.pas's write helpers (not reused from // cevocommon, which pulls in Qt::Widgets - cevogame must stay Widgets-free). void writeChild(QDomElement &node, const QString &name, const QString &text) { QDomElement child = node.ownerDocument().createElement(name); child.appendChild(node.ownerDocument().createTextNode(text)); node.appendChild(child); } void writeInteger(QDomElement &node, const QString &name, int value) { writeChild(node, name, QString::number(value)); } void writeBoolean(QDomElement &node, const QString &name, bool value) { writeChild(node, name, value ? QStringLiteral("True") : QStringLiteral("False")); } void writeString(QDomElement &node, const QString &name, const QString &value) { writeChild(node, name, value); } } // namespace void Map::setSize(const QPoint &value) { if (m_size == value) return; m_size = value; tiles.resize(static_cast(mapSize())); } void Map::saveToFile(const QString &fileName) const { QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) throw std::runtime_error(("Could not open " + fileName).toStdString()); file.write(fileId, 8); file.write(reinterpret_cast(&formatId), 4); file.write(reinterpret_cast(&maxTurn), 4); const int x = m_size.x(); const int y = m_size.y(); file.write(reinterpret_cast(&x), 4); file.write(reinterpret_cast(&y), 4); if (mapSize() > 0) file.write(reinterpret_cast(tiles.data()), mapSize() * 4); } void Map::loadFromFile(const QString &fileName) { if (!QFileInfo::exists(fileName)) return; QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) throw std::runtime_error(("Could not open " + fileName).toStdString()); char readFileId[8] = {}; file.read(readFileId, 8); if (std::memcmp(readFileId, fileId, 8) != 0) throw std::runtime_error("Incorrect map file id. Expected 'cEvoMap' but '" + QByteArray(readFileId, 8).trimmed().toStdString() + "' read."); int readFormatId = 0; file.read(reinterpret_cast(&readFormatId), 4); if (readFormatId != 0) throw std::runtime_error("Unsupported map format id " + std::to_string(readFormatId) + "."); file.read(reinterpret_cast(&maxTurn), 4); int width = 0; int height = 0; file.read(reinterpret_cast(&width), 4); file.read(reinterpret_cast(&height), 4); height &= ~1; if (width > lxmax) width = lxmax; if (height > lymax) height = lymax; setSize(QPoint(width, height)); if (mapSize() > 0) file.read(reinterpret_cast(tiles.data()), mapSize() * 4); for (int loc1 = 0; loc1 < mapSize(); ++loc1) { tiles[loc1] = (tiles[loc1] & (0x7F01FFFFu | fPrefStartPos | fStartPos)) | (0xFu << 27); if ((tiles[loc1] & (fTerrain | fSpecial)) == static_cast(fSwamp | fSpecial2)) tiles[loc1] = (tiles[loc1] & ~static_cast(fTerrain | fSpecial)) | (fSwamp | fSpecial1); if ((tiles[loc1] & fDeadLands) != 0 && (tiles[loc1] & fTerrain) != static_cast(fArctic)) tiles[loc1] = (tiles[loc1] & ~static_cast(fTerrain | fSpecial)) | fDesert; } } void Map::saveToFileXml(const QString &fileName) { constexpr std::uint32_t fTerritory = 0x78000000u; QDomDocument doc; QDomElement rootNode = doc.createElement(QStringLiteral("cEvoMap")); doc.appendChild(rootNode); writeInteger(rootNode, QStringLiteral("FormatId"), formatId); writeInteger(rootNode, QStringLiteral("MaxTurn"), maxTurn); writeInteger(rootNode, QStringLiteral("SizeX"), m_size.x()); writeInteger(rootNode, QStringLiteral("SizeY"), m_size.y()); if (mapSize() > 0) { QDomElement tilesNode = doc.createElement(QStringLiteral("Tiles")); // Scratch locals persist across iterations without being reset, // matching the original's own (buggy) variable reuse - see the // Improvement switch's default case below. QString terrain; QString improvement; QString special; QString modern; for (int i = 0; i < mapSize(); ++i) { QDomElement tileNode = doc.createElement(QStringLiteral("Tile")); std::uint32_t &tile = tiles[i]; // destructively consumed below tile &= ~fTerritory; writeInteger(tileNode, QStringLiteral("I"), i); switch (tile & fTerrain) { case fOcean: terrain = QStringLiteral("Ocean"); break; case fShore: terrain = QStringLiteral("Shore"); break; case fGrass: terrain = QStringLiteral("Grass"); break; case fDesert: terrain = QStringLiteral("Desert"); break; case fPrairie: terrain = QStringLiteral("Prairie"); break; case fTundra: terrain = QStringLiteral("Tundra"); break; case fArctic: terrain = QStringLiteral("Arctic"); break; case fSwamp: terrain = QStringLiteral("Swamp"); break; case fForest: terrain = QStringLiteral("Forest"); break; case fHills: terrain = QStringLiteral("Hills"); break; case fMountains: terrain = QStringLiteral("Mountains"); break; default: terrain = QStringLiteral("Unknown"); break; } if (!terrain.isEmpty()) writeString(tileNode, QStringLiteral("Terrain"), terrain); tile &= ~static_cast(fTerrain); switch (tile & fTerImp) { case tiNone: improvement.clear(); break; case tiIrrigation: improvement = QStringLiteral("Irrigation"); break; case tiFarm: improvement = QStringLiteral("Farmland"); break; case tiMine: improvement = QStringLiteral("Mine"); break; case tiFort: improvement = QStringLiteral("Fort"); break; case tiBase: improvement = QStringLiteral("MilitaryBase"); break; default: terrain = QStringLiteral("Unknown"); break; // faithful: upstream typo sets Terrain here, not Improvement } if (!improvement.isEmpty()) writeString(tileNode, QStringLiteral("Improvement"), improvement); tile &= ~static_cast(fTerImp); switch (tile & fSpecial) { case 0: special.clear(); break; case fSpecial1: special = QStringLiteral("Special1"); break; case fSpecial2: special = QStringLiteral("Special2"); break; default: special = QStringLiteral("Unknown"); break; } if (!special.isEmpty()) writeString(tileNode, QStringLiteral("Special"), special); tile &= ~static_cast(fSpecial); if (tile & fRiver) writeBoolean(tileNode, QStringLiteral("River"), true); tile &= ~static_cast(fRiver); if (tile & fRoad) writeBoolean(tileNode, QStringLiteral("Road"), true); tile &= ~static_cast(fRoad); if (tile & fRR) writeBoolean(tileNode, QStringLiteral("Railroad"), true); tile &= ~static_cast(fRR); if (tile & fCanal) writeBoolean(tileNode, QStringLiteral("Canal"), true); tile &= ~static_cast(fCanal); if (tile & fPoll) writeBoolean(tileNode, QStringLiteral("Pollution"), true); tile &= ~static_cast(fPoll); if (tile & fPrefStartPos) writeBoolean(tileNode, QStringLiteral("PrefStartPos"), true); tile &= ~static_cast(fPrefStartPos); if (tile & fStartPos) writeBoolean(tileNode, QStringLiteral("StartPos"), true); tile &= ~static_cast(fStartPos); if (tile & fDeadLands) writeBoolean(tileNode, QStringLiteral("DeadLands"), true); tile &= ~static_cast(fDeadLands); if (tile & fGrWall) writeBoolean(tileNode, QStringLiteral("GrWall"), true); tile &= ~static_cast(fGrWall); switch (tile & fModern) { case 0: modern.clear(); break; case fCobalt: modern = QStringLiteral("Cobalt"); break; case fUranium: modern = QStringLiteral("Uranium"); break; case fMercury: modern = QStringLiteral("Mercury"); break; default: modern = QStringLiteral("Unknown"); break; } if (!modern.isEmpty()) writeString(tileNode, QStringLiteral("Modern"), modern); tile &= ~static_cast(fModern); if (tile > 0) writeString(tileNode, QStringLiteral("Remaining"), QStringLiteral("%1").arg(tile, 8, 16, QLatin1Char('0')).toUpper()); tilesNode.appendChild(tileNode); } rootNode.appendChild(tilesNode); } const QString dir = QFileInfo(fileName).path(); if (!dir.isEmpty()) QDir().mkpath(dir); QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) throw std::runtime_error(("Could not open " + fileName).toStdString()); QTextStream stream(&file); doc.save(stream, 2); } } // namespace Cevo