ported map

This commit is contained in:
Geoffray Levasseur committed 2026-10-27 14:20:02 +01:00
1 parent ba8b69d771
commit a803d083ca
3 files changed
+350 -5

No files matched your search

+8 -5
View File
@@ -29,8 +29,9 @@
# CevoGame: C++ port of the C-evo game engine itself (perso/cevo/*.pas, the # CevoGame: C++ port of the C-evo game engine itself (perso/cevo/*.pas, the
# unit's top-level files - as opposed to common/ which is Packages/Common's # unit's top-level files - as opposed to common/ which is Packages/Common's
# generic, game-agnostic utility layer). Ported incrementally, file by file; # generic, game-agnostic utility layer). Ported incrementally, file by file;
# depends on Qt::Core (QByteArray/QIODevice/QString), not Widgets - the # depends on Qt::Core (QByteArray/QIODevice/QString) and Qt::Xml (Map.pas's
# simulation/model layer has no business depending on a UI toolkit. # XML map export), not Widgets - the simulation/model layer has no business
# depending on a UI toolkit.
# #
# Also depends on cevoai (ai/): perso/cevo/Protocol.pas and the AI-facing # Also depends on cevoai (ai/): perso/cevo/Protocol.pas and the AI-facing
# "AI Template/Protocol.pas" share the same core gameplay structs verbatim # "AI Template/Protocol.pas" share the same core gameplay structs verbatim
@@ -41,14 +42,15 @@
# AI plugin ABI), so game/ reuses it directly via cevoai's include dir # AI plugin ABI), so game/ reuses it directly via cevoai's include dir
# instead of maintaining a second, duplicate copy. # instead of maintaining a second, duplicate copy.
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Xml)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Xml)
set(CEVO_GAME_HEADERS set(CEVO_GAME_HEADERS
include/Cevo/CityProcessing.h include/Cevo/CityProcessing.h
include/Cevo/CmdList.h include/Cevo/CmdList.h
include/Cevo/Database.h include/Cevo/Database.h
include/Cevo/IPQ.h include/Cevo/IPQ.h
include/Cevo/Map.h
include/Cevo/UnitProcessing.h include/Cevo/UnitProcessing.h
) )
@@ -57,6 +59,7 @@ set(CEVO_GAME_SOURCES
src/CmdList.cpp src/CmdList.cpp
src/Database.cpp src/Database.cpp
src/IPQ.cpp src/IPQ.cpp
src/Map.cpp
src/UnitProcessing.cpp src/UnitProcessing.cpp
) )
@@ -66,7 +69,7 @@ target_include_directories(cevogame PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include
) )
target_link_libraries(cevogame PUBLIC Qt${QT_VERSION_MAJOR}::Core cevoai) target_link_libraries(cevogame PUBLIC Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Xml cevoai)
set_target_properties(cevogame PROPERTIES set_target_properties(cevogame PROPERTIES
CXX_STANDARD 17 CXX_STANDARD 17
+70
View File
@@ -0,0 +1,70 @@
// This file is part of cevo-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.
#pragma once
#include <QPoint>
#include <QString>
#include <cstdint>
#include <vector>
namespace Cevo {
constexpr const char *CevoMapExt = ".cevomap";
constexpr const char *CevoMapPictureExt = ".png";
constexpr const char *CevoMapXmlExt = ".cevomapx";
// Equivalent of Map.pas's TMap: loads/saves the .cevomap binary map-editor
// format (the game's own raw-tile-dump format, little-endian, same
// verbatim-wire-format fidelity as CmdList's save-game log), and exports to
// the human-readable .cevomapx XML format.
class Map {
public:
Map() = default;
void loadFromFile(const QString &fileName);
void saveToFile(const QString &fileName) const;
// NOTE: destructively consumes `tiles` bit-by-bit as it writes each
// field out (matches the original's own behavior - not a port mistake).
// Don't call saveToFile() or otherwise rely on `tiles` afterward.
void saveToFileXml(const QString &fileName);
int mapSize() const { return m_size.x() * m_size.y(); }
QPoint size() const { return m_size; }
void setSize(const QPoint &value);
int maxTurn = 0;
int formatId = 0;
std::vector<std::uint32_t> tiles;
private:
QPoint m_size;
};
} // namespace Cevo
+272
View File
@@ -0,0 +1,272 @@
// This file is part of cevo-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 <QDir>
#include <QDomDocument>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <cstring>
#include <stdexcept>
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<std::size_t>(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<const char *>(&formatId), 4);
file.write(reinterpret_cast<const char *>(&maxTurn), 4);
const int x = m_size.x();
const int y = m_size.y();
file.write(reinterpret_cast<const char *>(&x), 4);
file.write(reinterpret_cast<const char *>(&y), 4);
if (mapSize() > 0)
file.write(reinterpret_cast<const char *>(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<char *>(&readFormatId), 4);
if (readFormatId != 0)
throw std::runtime_error("Unsupported map format id " + std::to_string(readFormatId) + ".");
file.read(reinterpret_cast<char *>(&maxTurn), 4);
int width = 0;
int height = 0;
file.read(reinterpret_cast<char *>(&width), 4);
file.read(reinterpret_cast<char *>(&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<char *>(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<std::uint32_t>(fSwamp | fSpecial2))
tiles[loc1] = (tiles[loc1] & ~static_cast<std::uint32_t>(fTerrain | fSpecial)) | (fSwamp | fSpecial1);
if ((tiles[loc1] & fDeadLands) != 0 && (tiles[loc1] & fTerrain) != static_cast<std::uint32_t>(fArctic))
tiles[loc1] = (tiles[loc1] & ~static_cast<std::uint32_t>(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<std::uint32_t>(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<std::uint32_t>(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<std::uint32_t>(fSpecial);
if (tile & fRiver)
writeBoolean(tileNode, QStringLiteral("River"), true);
tile &= ~static_cast<std::uint32_t>(fRiver);
if (tile & fRoad)
writeBoolean(tileNode, QStringLiteral("Road"), true);
tile &= ~static_cast<std::uint32_t>(fRoad);
if (tile & fRR)
writeBoolean(tileNode, QStringLiteral("Railroad"), true);
tile &= ~static_cast<std::uint32_t>(fRR);
if (tile & fCanal)
writeBoolean(tileNode, QStringLiteral("Canal"), true);
tile &= ~static_cast<std::uint32_t>(fCanal);
if (tile & fPoll)
writeBoolean(tileNode, QStringLiteral("Pollution"), true);
tile &= ~static_cast<std::uint32_t>(fPoll);
if (tile & fPrefStartPos)
writeBoolean(tileNode, QStringLiteral("PrefStartPos"), true);
tile &= ~static_cast<std::uint32_t>(fPrefStartPos);
if (tile & fStartPos)
writeBoolean(tileNode, QStringLiteral("StartPos"), true);
tile &= ~static_cast<std::uint32_t>(fStartPos);
if (tile & fDeadLands)
writeBoolean(tileNode, QStringLiteral("DeadLands"), true);
tile &= ~static_cast<std::uint32_t>(fDeadLands);
if (tile & fGrWall)
writeBoolean(tileNode, QStringLiteral("GrWall"), true);
tile &= ~static_cast<std::uint32_t>(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<std::uint32_t>(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