// 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 "MiniMap.h" #include "Cevo/CevoGraphicSet.h" #include "Cevo/CevoScreenTools.h" #include "Dpi/DpiCommon.h" #include "GameServer.h" #include "localplayer/Tribes.h" #include #include #include #include namespace Cevo { namespace { // save map tile flags constexpr int smOwned = 0x20; constexpr int smUnit = 0x40; constexpr int smCity = 0x80; // Colour of a pixel of the system sprite sheet (black if it isn't loaded). QColor systemPixel(int x, int y) { if (!HGrSystem || x >= HGrSystem->data.width() || y >= HGrSystem->data.height()) return Qt::black; return HGrSystem->data.image().pixelColor(x, y); } // Each channel brightened to the upper half of its range. QColor brighter(const QColor &color) { return QColor(0x80 | (color.red() >> 1), 0x80 | (color.green() >> 1), 0x80 | (color.blue() >> 1)); } void putPixel(QImage &image, int x, int y, const QColor &color) { if (x >= 0 && y >= 0 && x < image.width() && y < image.height()) image.setPixelColor(x, y, color); } QColor tribeColor(int owner) { const auto &tribe = Tribes::tribe[static_cast(owner)]; return tribe ? tribe->color : QColor(Qt::black); } } // namespace MiniMap::MiniMap() { for (auto &row : colors) row.fill(QColor(Qt::black)); for (int x = 0; x < 12; ++x) { for (int y = 0; y < 2; ++y) colors[static_cast(x)][static_cast(y)] = systemPixel(66 + x, 67 + y); } } void MiniMap::resizeBitmap() { bitmap.setSize(Dpi::scaleToNative(size.x()) * 2, Dpi::scaleToNative(size.y())); bitmap.image().fill(Qt::black); } void MiniMap::loadFromLogFile(const QString &fileName, int &lastTurn, QPoint defaultSize) { constexpr char FileId[] = "cEvoBook"; MapArray saveMap{}; bool ok = false; QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { const auto readInt = [&file]() { std::int32_t value = 0; file.read(reinterpret_cast(&value), sizeof value); return value; }; const QByteArray id = file.read(8); ok = id == FileId; if (ok) { const std::uint32_t version = static_cast(readInt()); // format id if (version >= 0x000E01) readInt(); // item stored since 0.14.1 size.setX(readInt()); size.setY(readInt()); const int fileLandMass = readInt(); if (fileLandMass == 0) file.seek(file.pos() + static_cast(size.y()) * size.x() * 4); readInt(); // MaxTurn readInt(); // RNG lastTurn = readInt(); file.read(reinterpret_cast(saveMap.data()), 4); mode = saveMap[0] == 0x80 ? Mode::MultiPlayer : Mode::Picture; if (mode == Mode::Picture) { if (size.x() > 0 && size.y() > 0 && static_cast(size.x()) * size.y() <= saveMap.size()) file.read(reinterpret_cast(saveMap.data()) + 4, size.x() * size.y() - 4); else ok = false; // saved map data too big } } } if (!ok) { lastTurn = 0; size = defaultSize; mode = Mode::None; } paintFile(saveMap); } void MiniMap::loadFromMapFile(const QString &fileName, int &mapLandTiles, int &mapStartPositions) { const QString imageFileName = fileName.left(fileName.length() - static_cast(std::strlen(CevoMapExt))) + QLatin1String(CevoMapPictureExt); mode = Mode::Picture; bool paintMapEnabled; if (QFileInfo::exists(imageFileName) && bitmap.image().load(imageFileName)) { bitmap.image() = bitmap.image().convertToFormat(QImage::Format_ARGB32); if (bitmap.width() / 2 > MaxWidthMapLogo) bitmap.image() = bitmap.image().copy(0, 0, MaxWidthMapLogo * 2, bitmap.height()); if (bitmap.height() > MaxHeightMapLogo) bitmap.image() = bitmap.image().copy(0, 0, bitmap.width(), MaxHeightMapLogo); size = QPoint(bitmap.width() / 2, bitmap.height()); paintMapEnabled = false; } else { size = QPoint(MaxWidthMapLogo, MaxHeightMapLogo); paintMapEnabled = true; } Map map; map.loadFromFile(fileName); mapLandTiles = 0; mapStartPositions = 0; for (int y = 0; y < map.size().y(); ++y) { for (int x = 0; x < map.size().x(); ++x) { const std::uint32_t tile = map.tiles[static_cast(y * map.size().x() + x)]; switch (tile & fTerrain) { case fGrass: case fPrairie: case fTundra: case fSwamp: case fForest: case fHills: ++mapLandTiles; break; default: break; } if (tile & (fPrefStartPos | fStartPos)) ++mapStartPositions; } } if (mapStartPositions > nPl) mapStartPositions = nPl; if (paintMapEnabled) { size = map.size(); paintMap(map); } } void MiniMap::paintMap(const Map &map) { resizeBitmap(); if (mode != Mode::Picture) return; const int xx = Dpi::scaleToNative(size.x()); const int yy = Dpi::scaleToNative(size.y()); for (int y = 0; y < yy; ++y) { for (int x = 0; x < xx; ++x) { for (int i = 0; i < 2; ++i) { const int xm = (x * 2 + i + (y & 1)) % (xx * 2); const std::uint32_t tile = map.tiles[static_cast(Dpi::scaleFromNative(x) + size.x() * Dpi::scaleFromNative(y))]; const QColor color = (tile & fTerrain) == fUNKNOWN ? QColor(Qt::black) : colors[tile & fTerrain][static_cast(i)]; putPixel(bitmap.image(), xm, y, color); } } } } void MiniMap::paintRandom(int brightness, int startLandMass, QPoint worldSize) { const std::uint32_t *map = GameServer::previewMap(startLandMass); size = worldSize; resizeBitmap(); const int xx = Dpi::scaleToNative(size.x()); const int yy = Dpi::scaleToNative(size.y()); for (int y = 0; y < yy; ++y) { for (int x = 0; x < xx; ++x) { for (int i = 0; i < 2; ++i) { const int xm = (x * 2 + i + (y & 1)) % (xx * 2); const int index = Dpi::scaleFromNative(x) * lxmax / size.x() + lxmax * ((Dpi::scaleFromNative(y) * (lymax - 1) + size.y() / 2) / (size.y() - 1)); const QColor base = colors[map[index] & fTerrain][static_cast(i)]; putPixel(bitmap.image(), xm, y, QColor(base.red() * brightness / 3, base.green() * brightness / 3, base.blue() * brightness / 3)); } } } } void MiniMap::paintFile(const MapArray &saveMap) { const QColor ownColor = systemPixel(95, 67); const QColor enemyColor = systemPixel(96, 67); resizeBitmap(); if (mode != Mode::Picture) return; const int xx = Dpi::scaleToNative(size.x()); const int yy = Dpi::scaleToNative(size.y()); for (int y = 0; y < yy; ++y) { for (int x = 0; x < xx; ++x) { for (int i = 0; i < 2; ++i) { const int xm = (x * 2 + i + (y & 1)) % (xx * 2); const int tile = saveMap[static_cast(Dpi::scaleFromNative(x) + size.x() * Dpi::scaleFromNative(y))]; QColor color; if ((tile & fTerrain) == fUNKNOWN) { color = Qt::black; } else if (tile & smCity) { color = (tile & smOwned) ? ownColor : enemyColor; if (y > 0) // 2x2 city dot covers two lines putPixel(bitmap.image(), xm, y - 1, color); } else if (i == 0 && (tile & smUnit)) { color = (tile & smOwned) ? ownColor : enemyColor; } else { color = colors[static_cast(tile & fTerrain)][static_cast(i)]; } putPixel(bitmap.image(), xm, y, color); } } } } void MiniMap::paint(const std::uint32_t *myMap, int mapWidth, int clientMode, int xxt, int xwMini) { if (!myMap) return; const QColor polOcean = systemPixel(101, 67); const QColor polNone = systemPixel(102, 67); const int hw = mapWidth / (xxt * 2); resizeBitmap(); const int xx = Dpi::scaleToNative(size.x()); const int yy = Dpi::scaleToNative(size.y()); for (int y = 0; y < yy; ++y) { for (int x = 0; x < xx; ++x) { const int loc = Dpi::scaleFromNative(x) + size.x() * Dpi::scaleFromNative(y); if ((myMap[loc] & fTerrain) == fUNKNOWN) continue; for (int i = 0; i < 2; ++i) { const int xm = ((x - Dpi::scaleToNative(xwMini)) * 2 + i + (y & 1) - Dpi::scaleToNative(hw) + xx * 5) % (xx * 2); std::uint32_t terrainTile = myMap[loc] & fTerrain; if (terrainTile > 11) terrainTile = 0; QColor color = colors[terrainTile][static_cast(i)]; if (clientMode == cEditMap) { if (myMap[loc] & (fPrefStartPos | fStartPos)) color = Qt::white; } else if (myMap[loc] & fCity) { if (ClientTools::getMyCityByLoc(loc)) { color = tribeColor(ClientTools::me); } else if (const TCityInfo *enemyCity = ClientTools::getEnemyCityByLoc(loc)) { color = tribeColor(enemyCity->Owner); } color = brighter(color); if (y > 0) // 2x2 city dot covers two lines putPixel(bitmap.image(), xm, y - 1, color); } else if (i == 0 && (myMap[loc] & fUnit)) { if (ClientTools::getMyUnitByLoc(loc)) { color = tribeColor(ClientTools::me); } else if (const TUnitInfo *enemyUnit = ClientTools::getEnemyUnitByLoc(loc)) { color = tribeColor(enemyUnit->Owner); } color = brighter(color); } else if (ClientTools::hasMapOption(mapOptions, ClientTools::moPolitical)) { if ((myMap[loc] & fTerrain) < fGrass) color = polOcean; else if ((*ClientTools::myRO->Territory)[loc] < 0) color = polNone; else color = tribeColor((*ClientTools::myRO->Territory)[loc]); } putPixel(bitmap.image(), xm, y, color); } } } } } // namespace Cevo