// 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. // PORT STATUS (see /memories/repo/cevo-evo-game.md for the full writeup): // this is BATCH 1 of a multi-batch port of IsoEngine.pas (1782 lines). Done: // class skeleton (every method declared in IsoEngine.h), lifecycle // (create/destroy/reset/setOutput/setPaintBounds), the pure-geometry // helpers (poleTile/connection4/connection8/oceanConnection/isShoreTile), // the simple output primitives (fillRect/textOut/bitBltBitmapOutput/sprite/ // terrainSprite), line/shading primitives (clippedLine/paintGrid/cityGrid/ // makeDark/shadeOutside), attack state (attackBegin/Effect/End), and // CitiesPictures::prepare(). NOT YET PORTED (left declared-only in the // header, safe since nothing calls IsoMap yet - Term.pas isn't ported): // applyTileSize()/reduceTerrainIconsSize() (the ~280-line dithered-patch // asset preparation), paintLandOcean/paintShore/paintTileExtraTerrain // (terrain-transition rendering), paintTileObjects/1/2/3, paintUnit, // paintCity, paintBorder, showSpacePort, nameCity, and the top-level // paint() dispatcher. setTileSize() is a placeholder (just stores the // value) until applyTileSize() exists - calling the real Pascal chain // (TileSize setter -> ApplyTileSize) would need that method defined first. #include "IsoEngine.h" #include "Dpi/DpiPixelPointer.h" namespace Cevo { namespace IsoEngine { int unFocus = -1; bool blinkOn = false; bool destinationMarkOn = false; int clientMode = 0; ClientTools::MapOptions mapOptions = 0; namespace { InitEnemyModelEvent onInitEnemyModel; } bool isJungle(int y) { return y > (ClientTools::g.ly - 2) / 4 && ClientTools::g.ly - 1 - y > (ClientTools::g.ly - 2) / 4; } void init(InitEnemyModelEvent initEnemyModelHandler) { onInitEnemyModel = std::move(initEnemyModelHandler); } void CitiesPictures::prepare(GraphicSet *hGrCities, int xxt, int yyt) { for (int age = 2; age <= 3; ++age) { for (int size = 0; size <= 3; ++size) { Tribes::CityPicture &pic = picture(age, size); Tribes::findPosition(hGrCities, size * (xxt * 2 + 1), (age - 2) * (yyt * 3 + 1), xxt * 2 - 1, yyt * 3 - 1, colorFromDelphi(0x00FFFF), pic.xShield, pic.yShield); } } } IsoMapCache::IsoMapCache() { landPatch.image().fill(0); oceanPatch.image().fill(0); borders.image().fill(0); citiesPictures = std::make_unique(); } IsoMapCache::~IsoMapCache() = default; void IsoMapCache::assignToIsoMap(IsoMap &isoMap) { isoMap.hGrTerrain = hGrTerrain; isoMap.hGrCities = hGrCities; isoMap.m_borders = &borders; isoMap.m_bordersOk = &bordersOk; isoMap.m_landPatch = &landPatch; isoMap.m_oceanPatch = &oceanPatch; isoMap.terrainSpriteSize = &terrainSpriteSize; isoMap.m_citiesPictures = citiesPictures.get(); } IsoMap::IsoMap() { m_left = 0; m_top = 0; m_right = 0; m_bottom = 0; m_attLoc = -1; m_defLoc = -1; m_adviceLoc = -1; // NOTE: the original's constructor also does `TileSize := DefaultTileSize` // here, which triggers ApplyTileSize() (asset loading) - deferred to // batch 2, see the file banner comment above. } IsoMap::~IsoMap() = default; void IsoMap::reset() { if (m_bordersOk) *m_bordersOk = 0; } void IsoMap::setOutput(Dpi::Bitmap *output) { m_output = output; m_left = 0; m_top = 0; m_right = output->width(); m_bottom = output->height(); } void IsoMap::setPaintBounds(int left, int top, int right, int bottom) { m_left = left; m_top = top; m_right = right; m_bottom = bottom; } void IsoMap::setTileSize(TileSize value) { // Placeholder until applyTileSize() (batch 2) exists; just remembers // the requested size instead of (re)loading its terrain/city assets. m_tileSize = value; } void IsoMap::fillRect(int x, int y, int width, int height, QColor color) { if (x < m_left) { width -= (m_left - x); x = m_left; } if (y < m_top) { height -= (m_top - y); y = m_top; } if (x + width >= m_right) width = m_right - x; if (y + height >= m_bottom) height = m_bottom - y; if (width <= 0 || height <= 0) return; Dpi::Canvas &canvas = m_output->canvas(); canvas.brush().setColor(color); canvas.fillRect(QRect(x, y, width, height)); canvas.brush().setStyle(Qt::NoBrush); } void IsoMap::textOut(int x, int y, QColor color, const QString &s) { Dpi::Canvas &canvas = m_output->canvas(); canvas.font().setColor(color); canvas.textOut(x, y, s); } void IsoMap::bitBltBitmapOutput(Dpi::Bitmap &src, int x, int y, int width, int height, int xSrc, int ySrc, RasterOp rop) { if (x < m_left) { width -= (m_left - x); xSrc += (m_left - x); x = m_left; } if (y < m_top) { height -= (m_top - y); ySrc += (m_top - y); y = m_top; } if (x + width >= m_right) width = m_right - x; if (y + height >= m_bottom) height = m_bottom - y; if (width <= 0 || height <= 0) return; bitBltBitmap(*m_output, x, y, width, height, src, xSrc, ySrc, rop); } void IsoMap::sprite(GraphicSet *hGr, int xDst, int yDst, int width, int height, int xGr, int yGr) { bitBltBitmapOutput(hGr->mask, xDst, yDst, width, height, xGr, yGr, RasterOp::And); bitBltBitmapOutput(hGr->data, xDst, yDst, width, height, xGr, yGr, RasterOp::Paint); } void IsoMap::terrainSprite(int xDst, int yDst, int grix, bool pureBlack) { const SpriteBounds &bounds = (*terrainSpriteSize)[static_cast(grix)]; int width = bounds.right - bounds.left; int height = bounds.bottom - bounds.top; int xSrc = 1 + grix % TerrainIconCols * (xxt * 2 + 1) + bounds.left; int ySrc = 1 + grix / TerrainIconCols * (yyt * 3 + 1) + bounds.top; xDst += bounds.left; yDst = yDst - yyt + bounds.top; if (xDst < m_left) { width -= (m_left - xDst); xSrc += (m_left - xDst); xDst = m_left; } if (yDst < m_top) { height -= (m_top - yDst); ySrc += (m_top - yDst); yDst = m_top; } if (xDst + width >= m_right) width = m_right - xDst; if (yDst + height >= m_bottom) height = m_bottom - yDst; if (width <= 0 || height <= 0) return; bitBltBitmap(*m_output, xDst, yDst, width, height, hGrTerrain->mask, xSrc, ySrc, RasterOp::And); if (!pureBlack) bitBltBitmap(*m_output, xDst, yDst, width, height, hGrTerrain->data, xSrc, ySrc, RasterOp::Paint); } int IsoMap::poleTile(int loc) const { // virtual pole tile int result = fUNKNOWN; const int lx = ClientTools::g.lx; const int ly = ClientTools::g.ly; if (loc < -2 * lx) { // off both poles } else if (loc < -lx) { if ((ClientTools::myMap[ClientTools::dLoc(loc, 0, 2)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, -2, 2)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, 2, 2)] & fTerrain) != fUNKNOWN) result = fArctic; if ((ClientTools::myMap[ClientTools::dLoc(loc, 0, 2)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, -2, 2)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, 2, 2)] & fObserved) != 0) result |= fObserved; } else if (loc < 0) { if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, 1)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, 1, 1)] & fTerrain) != fUNKNOWN) result = fArctic; if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, 1)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, 1, 1)] & fObserved) != 0) result |= fObserved; } else if (loc < lx * (ly + 1)) { if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, -1)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, 1, -1)] & fTerrain) != fUNKNOWN) result = fArctic; if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, -1)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, 1, -1)] & fObserved) != 0) result |= fObserved; } else if (loc < lx * (ly + 2)) { if ((ClientTools::myMap[ClientTools::dLoc(loc, 0, -2)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, -2, -2)] & fTerrain) != fUNKNOWN && (ClientTools::myMap[ClientTools::dLoc(loc, 2, -2)] & fTerrain) != fUNKNOWN) result = fArctic; if ((ClientTools::myMap[ClientTools::dLoc(loc, 0, -2)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, -2, -2)] & fObserved) != 0 && (ClientTools::myMap[ClientTools::dLoc(loc, 2, -2)] & fObserved) != 0) result |= fObserved; } return result; } namespace { constexpr int Dirx[8] = { 1, 2, 1, 0, -1, -2, -1, 0 }; constexpr int Diry[8] = { -1, 0, 1, 2, 1, 0, -1, -2 }; } // namespace int IsoMap::connection4(int loc, std::uint32_t mask, std::uint32_t value) const { int result = 0; const int lx = ClientTools::g.lx; const int ly = ClientTools::g.ly; if (ClientTools::dLoc(loc, 1, -1) >= 0) { if ((ClientTools::myMap[ClientTools::dLoc(loc, 1, -1)] & mask) == value) result += 1; if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, -1)] & mask) == value) result += 8; } if (ClientTools::dLoc(loc, 1, 1) < lx * ly) { if ((ClientTools::myMap[ClientTools::dLoc(loc, 1, 1)] & mask) == value) result += 2; if ((ClientTools::myMap[ClientTools::dLoc(loc, -1, 1)] & mask) == value) result += 4; } return result; } int IsoMap::connection8(int loc, std::uint32_t mask) const { int result = 0; const int lx = ClientTools::g.lx; const int ly = ClientTools::g.ly; for (int dir = 0; dir < 8; ++dir) { const int connLoc = ClientTools::dLoc(loc, Dirx[dir], Diry[dir]); if (connLoc >= 0 && connLoc < lx * ly && (ClientTools::myMap[connLoc] & mask) != 0) result += 1 << dir; } return result; } int IsoMap::oceanConnection(int loc) const { int result = 0; const int lx = ClientTools::g.lx; const int ly = ClientTools::g.ly; for (int dir = 0; dir < 8; ++dir) { const int connLoc = ClientTools::dLoc(loc, Dirx[dir], Diry[dir]); if (connLoc < 0 || connLoc >= lx * ly || ((ClientTools::myMap[connLoc] - 2) & fTerrain) < 13) result += 1 << dir; } return result; } bool IsoMap::isShoreTile(int loc) const { const int lx = ClientTools::g.lx; const int ly = ClientTools::g.ly; for (int dir = 0; dir < 8; ++dir) { const int connLoc = ClientTools::dLoc(loc, Dirx[dir], Diry[dir]); if (connLoc < 0 || connLoc >= lx * ly || ((ClientTools::myMap[connLoc] - 2) & fTerrain) < 13) return true; } return false; } void IsoMap::makeDark(Dpi::PixelPointer &line, int length) { for (int i = 0; i < length; ++i) { const QRgb c = line.pixelARGB(); line.setPixelARGB(qRgb((qRed(c) >> 1) & 0x7F, (qGreen(c) >> 1) & 0x7F, (qBlue(c) >> 1) & 0x7F)); line.nextPixel(); } } void IsoMap::shadeOutside(int x0, int y0, int width, int height, int xm, int ym) { constexpr double rShade = 3.75; Dpi::PixelPointer line(&m_output->image(), x0, y0); for (int y = 0; y < height; ++y) { const double yN = static_cast(y + y0 - ym) / yyt; if (std::abs(yN) < rShade) { // Darken left and right parts of the ellipse. const double wN = std::sqrt(rShade * rShade - yN * yN); const int wBright = static_cast(wN * xxt + 0.5); line.setX(0); makeDark(line, xm - wBright); line.setX(xm + wBright); makeDark(line, width - xm - wBright); } else { // Darken the entire line. line.setX(0); makeDark(line, width); } line.nextLine(); } } void IsoMap::clippedLine(int x, int y, int dx0, int dy0, bool mirror) { Dpi::Canvas &canvas = m_output->canvas(); const int dxmin = (m_left - x) / xxt; const int dymin = (m_realTop - y) / yyt; const int dxmax = (m_right - x - 1) / xxt + 1; const int dymax = (m_realBottom - y - 1) / yyt + 1; int n = dymax - dy0; if (mirror) { if (dx0 - dxmin < n) n = dx0 - dxmin; if (dx0 > dxmax) { n -= (dx0 - dxmax); dy0 += (dx0 - dxmax); dx0 = dxmax; } if (dy0 < dymin) { n -= (dymin - dy0); dx0 -= (dymin - dy0); dy0 = dymin; } } else { if (dxmax - dx0 < n) n = dxmax - dx0; if (dx0 < dxmin) { n -= (dxmin - dx0); dy0 += (dxmin - dx0); dx0 = dxmin; } if (dy0 < dymin) { n -= (dymin - dy0); dx0 += (dymin - dy0); dy0 = dymin; } } if (n <= 0) return; int x0, x1; if (mirror) { x0 = x + dx0 * xxt - 1; x1 = x + (dx0 - n) * xxt - 1; } else { x0 = x + dx0 * xxt; x1 = x + (dx0 + n) * xxt; } canvas.moveTo(x0, y + dy0 * yyt); canvas.lineTo(x1, y + (dy0 + n) * yyt); } void IsoMap::paintGrid(int x, int y, int nx, int ny) { Dpi::Canvas &canvas = m_output->canvas(); canvas.pen().setColor(Qt::black); for (int i = 0; i <= nx / 2; ++i) clippedLine(x, y, i * 2, 0, false); for (int i = 1; i <= (nx + 1) / 2; ++i) clippedLine(x, y, i * 2, 0, true); for (int i = 0; i <= ny / 2; ++i) { clippedLine(x, y, 0, 2 * i + 2, false); clippedLine(x, y, nx + 1, 2 * i + 1 + (nx & 1), true); } } void IsoMap::cityGrid(int xm, int ym, bool cityAllowClick) { Dpi::Canvas &canvas = m_output->canvas(); canvas.pen().setColor(cityAllowClick ? QColor(Qt::white) : QColor(Qt::black)); canvas.pen().setWidth(1); for (int i = 0; i <= 3; ++i) { canvas.moveTo(xm - xxt * (4 - i), ym + yyt * (1 + i)); canvas.lineTo(xm + xxt * (1 + i), ym - yyt * (4 - i)); canvas.moveTo(xm - xxt * (4 - i), ym - yyt * (1 + i)); canvas.lineTo(xm + xxt * (1 + i), ym + yyt * (4 - i)); } canvas.moveTo(xm - xxt * 4, ym + yyt * 1); canvas.lineTo(xm - xxt * 1, ym + yyt * 4); canvas.moveTo(xm + xxt * 1, ym + yyt * 4); canvas.lineTo(xm + xxt * 4, ym + yyt * 1); canvas.moveTo(xm - xxt * 4, ym - yyt * 1); canvas.lineTo(xm - xxt * 1, ym - yyt * 4); canvas.moveTo(xm + xxt * 1, ym - yyt * 4); canvas.lineTo(xm + xxt * 4, ym - yyt * 1); } void IsoMap::attackBegin(const TShowMove &showMove) { m_attLoc = showMove.FromLoc; m_defLoc = ClientTools::dLoc(m_attLoc, showMove.dx, showMove.dy); m_defHealth = -1; } void IsoMap::attackEffect(const TShowMove &showMove) { m_defHealth = showMove.EndHealthDef; } void IsoMap::attackEnd() { m_attLoc = -1; m_defLoc = -1; } } // namespace IsoEngine } // namespace Cevo