LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface/Handlers - TerrainHandlers.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 285 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 51 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "MessageHandler.h"
      21             : 
      22             : #include "graphics/MiniPatch.h"
      23             : #include "graphics/RenderableObject.h"
      24             : #include "graphics/Terrain.h"
      25             : #include "graphics/TerrainTextureEntry.h"
      26             : #include "graphics/TerrainTextureManager.h"
      27             : #include "graphics/TextureManager.h"
      28             : #include "lib/debug.h"
      29             : #include "lib/file/vfs/vfs.h"
      30             : #include "lib/path.h"
      31             : #include "lib/posix/posix_types.h"
      32             : #include "lib/status.h"
      33             : #include "lib/tex/tex.h"
      34             : #include "maths/Vector3D.h"
      35             : #include "ps/CStr.h"
      36             : #include "ps/Filesystem.h"
      37             : #include "ps/Game.h"
      38             : #include "ps/World.h"
      39             : #include "renderer/Renderer.h"
      40             : #include "scriptinterface/Interface.h"
      41             : #include "simulation2/components/ICmpPathfinder.h"
      42             : #include "simulation2/helpers/Grid.h"
      43             : #include "simulation2/helpers/Pathfinding.h"
      44             : #include "simulation2/system/Component.h"
      45             : #include "simulation2/system/Entity.h"
      46             : #include "tools/atlas/GameInterface/Brushes.h"
      47             : #include "tools/atlas/GameInterface/CommandProc.h"
      48             : #include "tools/atlas/GameInterface/DeltaArray.h"
      49             : #include "tools/atlas/GameInterface/Messages.h"
      50             : #include "tools/atlas/GameInterface/Shareable.h"
      51             : #include "tools/atlas/GameInterface/SharedTypes.h"
      52             : #include "tools/atlas/GameInterface/View.h"
      53             : 
      54             : #include <algorithm>
      55             : #include <cwchar>
      56             : #include <map>
      57             : #include <memory>
      58             : #include <queue>
      59             : #include <string>
      60             : #include <utility>
      61             : #include <vector>
      62             : 
      63             : namespace AtlasMessage
      64             : {
      65             : 
      66             : namespace
      67             : {
      68             : 
      69           0 : sTerrainTexturePreview MakeEmptyTerrainTexturePreview()
      70             : {
      71           0 :     sTerrainTexturePreview preview{};
      72           0 :     preview.name = std::wstring();
      73           0 :     preview.loaded = false;
      74           0 :     preview.imageHeight = 0;
      75           0 :     preview.imageWidth = 0;
      76           0 :     preview.imageData = {};
      77           0 :     return preview;
      78           0 : }
      79             : 
      80           0 : bool CompareTerrain(const sTerrainTexturePreview& a, const sTerrainTexturePreview& b)
      81             : {
      82           0 :     return (wcscmp(a.name.c_str(), b.name.c_str()) < 0);
      83             : }
      84             : 
      85           0 : sTerrainTexturePreview GetPreview(CTerrainTextureEntry* tex, size_t width, size_t height)
      86             : {
      87           0 :     sTerrainTexturePreview preview;
      88           0 :     preview.name = tex->GetTag().FromUTF8();
      89             : 
      90           0 :     const size_t previewBPP = 3;
      91           0 :     std::vector<std::uint8_t> buffer(width * height * previewBPP);
      92             : 
      93             :     // It's not good to shrink the entire texture to fit the small preview
      94             :     // window, since it's the fine details in the texture that are
      95             :     // interesting; so just go down one mipmap level, then crop a chunk
      96             :     // out of the middle.
      97             : 
      98           0 :     VfsPath texturePath;
      99           0 :     if (!tex->GetDiffuseTexturePath().empty())
     100             :     {
     101           0 :         const VfsPath cachedTexturePath = g_Renderer.GetTextureManager().GetCachedPath(
     102           0 :             tex->GetDiffuseTexturePath());
     103           0 :         if (g_VFS->GetFileInfo(cachedTexturePath, nullptr) == INFO::OK)
     104           0 :             texturePath = cachedTexturePath;
     105             :         else
     106           0 :             texturePath = tex->GetDiffuseTexturePath();
     107           0 :     }
     108             : 
     109           0 :     std::shared_ptr<std::uint8_t> fileData;
     110           0 :     size_t fileSize;
     111           0 :     Tex texture;
     112           0 :     const bool canUsePreview =
     113           0 :         !texturePath.empty() &&
     114           0 :         g_VFS->LoadFile(texturePath, fileData, fileSize) == INFO::OK &&
     115           0 :         texture.decode(fileData, fileSize) == INFO::OK &&
     116             :         // Check that we can fit the texture into the preview size before any transform.
     117           0 :         texture.m_Width >= width && texture.m_Height >= height &&
     118             :         // Transform to a single format that we can process.
     119           0 :         texture.transform_to((texture.m_Flags | TEX_MIPMAPS) & ~(TEX_DXT | TEX_GREY | TEX_BGR)) == INFO::OK &&
     120           0 :         (texture.m_Bpp == 24 || texture.m_Bpp == 32);
     121           0 :     if (canUsePreview)
     122             :     {
     123             :         size_t level = 0;
     124           0 :         while ((texture.m_Width >> (level + 1)) >= width && (texture.m_Height >> (level + 1)) >= height && level < texture.GetMIPLevels().size())
     125           0 :             ++level;
     126             :         // Extract the middle section (as a representative preview),
     127             :         // and copy into buffer.
     128           0 :         std::uint8_t* data = texture.GetMIPLevels()[level].data;
     129           0 :         ENSURE(data);
     130           0 :         const size_t levelWidth = texture.m_Width >> level;
     131           0 :         const size_t levelHeight = texture.m_Height >> level;
     132           0 :         const size_t dataShiftX = (levelWidth - width) / 2;
     133           0 :         const size_t dataShiftY = (levelHeight - height) / 2;
     134           0 :         for (size_t y = 0; y < height; ++y)
     135           0 :             for (size_t x = 0; x < width; ++x)
     136             :             {
     137           0 :                 const size_t bufferOffset = (y * width + x) * previewBPP;
     138           0 :                 const size_t dataOffset = ((y + dataShiftY) * levelWidth + x + dataShiftX) * texture.m_Bpp / 8;
     139           0 :                 buffer[bufferOffset + 0] = data[dataOffset + 0];
     140           0 :                 buffer[bufferOffset + 1] = data[dataOffset + 1];
     141           0 :                 buffer[bufferOffset + 2] = data[dataOffset + 2];
     142             :             }
     143           0 :         preview.loaded = true;
     144             :     }
     145             :     else
     146             :     {
     147             :         // Too small to preview. Just use a flat color instead.
     148           0 :         const std::uint32_t baseColor = tex->GetBaseColor();
     149           0 :         for (size_t i = 0; i < width * height; ++i)
     150             :         {
     151           0 :             buffer[i * previewBPP + 0] = (baseColor >> 16) & 0xff;
     152           0 :             buffer[i * previewBPP + 1] = (baseColor >> 8) & 0xff;
     153           0 :             buffer[i * previewBPP + 2] = (baseColor >> 0) & 0xff;
     154             :         }
     155           0 :         preview.loaded = tex->GetTexture()->IsLoaded();
     156             :     }
     157             : 
     158           0 :     preview.imageWidth = width;
     159           0 :     preview.imageHeight = height;
     160           0 :     preview.imageData = buffer;
     161             : 
     162           0 :     return preview;
     163           0 : }
     164             : 
     165             : } // anonymous namespace
     166             : 
     167           0 : QUERYHANDLER(GetTerrainGroups)
     168             : {
     169           0 :     const CTerrainTextureManager::TerrainGroupMap &groups = g_TexMan.GetGroups();
     170           0 :     std::vector<std::wstring> groupNames;
     171           0 :     for (CTerrainTextureManager::TerrainGroupMap::const_iterator it = groups.begin(); it != groups.end(); ++it)
     172           0 :         groupNames.push_back(it->first.FromUTF8());
     173           0 :     msg->groupNames = groupNames;
     174           0 : }
     175             : 
     176           0 : QUERYHANDLER(GetTerrainGroupTextures)
     177             : {
     178           0 :     std::vector<std::wstring> names;
     179             : 
     180           0 :     CTerrainGroup* group = g_TexMan.FindGroup(CStrW(*msg->groupName).ToUTF8());
     181           0 :     if (group)
     182             :     {
     183           0 :         for (std::vector<CTerrainTextureEntry*>::const_iterator it = group->GetTerrains().begin(); it != group->GetTerrains().end(); ++it)
     184           0 :             names.emplace_back((*it)->GetTag().FromUTF8());
     185             :     }
     186           0 :     std::sort(names.begin(), names.end());
     187           0 :     msg->names = names;
     188           0 : }
     189             : 
     190           0 : QUERYHANDLER(GetTerrainGroupPreviews)
     191             : {
     192           0 :     std::vector<sTerrainTexturePreview> previews;
     193             : 
     194           0 :     CTerrainGroup* group = g_TexMan.FindGroup(CStrW(*msg->groupName).ToUTF8());
     195           0 :     for (std::vector<CTerrainTextureEntry*>::const_iterator it = group->GetTerrains().begin(); it != group->GetTerrains().end(); ++it)
     196             :     {
     197           0 :         previews.push_back(GetPreview(*it, msg->imageWidth, msg->imageHeight));
     198             :     }
     199             : 
     200             :     // Sort the list alphabetically by name
     201           0 :     std::sort(previews.begin(), previews.end(), CompareTerrain);
     202           0 :     msg->previews = previews;
     203           0 : }
     204             : 
     205           0 : QUERYHANDLER(GetTerrainPassabilityClasses)
     206             : {
     207           0 :     CmpPtr<ICmpPathfinder> cmpPathfinder(*AtlasView::GetView_Game()->GetSimulation2(), SYSTEM_ENTITY);
     208           0 :     if (cmpPathfinder)
     209             :     {
     210           0 :         std::map<std::string, pass_class_t> nonPathfindingClasses, pathfindingClasses;
     211             :         cmpPathfinder->GetPassabilityClasses(nonPathfindingClasses, pathfindingClasses);
     212             : 
     213           0 :         std::vector<std::wstring> classNames;
     214           0 :         for (std::map<std::string, pass_class_t>::iterator it = nonPathfindingClasses.begin(); it != nonPathfindingClasses.end(); ++it)
     215           0 :             classNames.push_back(CStr(it->first).FromUTF8());
     216           0 :         msg->classNames = classNames;
     217           0 :     }
     218           0 : }
     219             : 
     220           0 : QUERYHANDLER(GetTerrainTexture)
     221             : {
     222           0 :     ssize_t x, y;
     223           0 :     g_CurrentBrush.m_Centre = msg->pos->GetWorldSpace();
     224           0 :     g_CurrentBrush.GetCentre(x, y);
     225             : 
     226           0 :     const CTerrain& terrain = g_Game->GetWorld()->GetTerrain();
     227           0 :     CMiniPatch* const tile = terrain.GetTile(x, y);
     228           0 :     if (tile)
     229             :     {
     230           0 :         CTerrainTextureEntry* tex = tile->GetTextureEntry();
     231           0 :         msg->texture = tex->GetTag().FromUTF8();
     232             :     }
     233             :     else
     234             :     {
     235           0 :         msg->texture = std::wstring();
     236             :     }
     237           0 : }
     238             : 
     239           0 : QUERYHANDLER(GetTerrainTexturePreview)
     240             : {
     241           0 :     CTerrainTextureEntry* tex = g_TexMan.FindTexture(CStrW(*msg->name).ToUTF8());
     242           0 :     if (tex)
     243           0 :         msg->preview = GetPreview(tex, msg->imageWidth, msg->imageHeight);
     244             :     else
     245           0 :         msg->preview = MakeEmptyTerrainTexturePreview();
     246           0 : }
     247             : 
     248             : //////////////////////////////////////////////////////////////////////////
     249             : 
     250             : namespace
     251             : {
     252             : 
     253             : struct TerrainTile
     254             : {
     255           0 :     TerrainTile(CTerrainTextureEntry* t, ssize_t p) : tex(t), priority(p) {}
     256             :     CTerrainTextureEntry* tex;
     257             :     ssize_t priority;
     258             : };
     259             : 
     260           0 : class TerrainArray : public DeltaArray2D<TerrainTile>
     261             : {
     262             : public:
     263           0 :     void Init()
     264             :     {
     265           0 :         m_Terrain = &g_Game->GetWorld()->GetTerrain();
     266           0 :         m_VertsPerSide = m_Terrain->GetVerticesPerSide();
     267             :     }
     268             : 
     269           0 :     void UpdatePriority(ssize_t x, ssize_t y, CTerrainTextureEntry* tex, ssize_t priorityScale, ssize_t& priority)
     270             :     {
     271           0 :         CMiniPatch* tile = m_Terrain->GetTile(x, y);
     272           0 :         if (!tile)
     273             :             return; // tile was out-of-bounds
     274             : 
     275             :         // If this tile matches the current texture, we just want to match its
     276             :         // priority; otherwise we want to exceed its priority
     277           0 :         if (tile->GetTextureEntry() == tex)
     278           0 :             priority = std::max(priority, tile->GetPriority()*priorityScale);
     279             :         else
     280           0 :             priority = std::max(priority, tile->GetPriority()*priorityScale + 1);
     281             :     }
     282             : 
     283           0 :     CTerrainTextureEntry* GetTexEntry(ssize_t x, ssize_t y)
     284             :     {
     285           0 :         if (size_t(x) >= size_t(m_VertsPerSide-1) || size_t(y) >= size_t(m_VertsPerSide-1))
     286             :             return NULL;
     287             : 
     288           0 :         return get(x, y).tex;
     289             :     }
     290             : 
     291           0 :     ssize_t GetPriority(ssize_t x, ssize_t y)
     292             :     {
     293           0 :         if (size_t(x) >= size_t(m_VertsPerSide-1) || size_t(y) >= size_t(m_VertsPerSide-1))
     294             :             return 0;
     295             : 
     296           0 :         return get(x, y).priority;
     297             :     }
     298             : 
     299           0 :     void PaintTile(ssize_t x, ssize_t y, CTerrainTextureEntry* tex, ssize_t priority)
     300             :     {
     301             :         // Ignore out-of-bounds tiles
     302           0 :         if (size_t(x) >= size_t(m_VertsPerSide-1) || size_t(y) >= size_t(m_VertsPerSide-1))
     303             :             return;
     304             : 
     305           0 :         set(x,y, TerrainTile(tex, priority));
     306             :     }
     307             : 
     308           0 :     ssize_t GetTilesPerSide()
     309             :     {
     310           0 :         return m_VertsPerSide-1;
     311             :     }
     312             : 
     313             : protected:
     314           0 :     TerrainTile getOld(ssize_t x, ssize_t y)
     315             :     {
     316           0 :         CMiniPatch* mp = m_Terrain->GetTile(x, y);
     317           0 :         ENSURE(mp);
     318           0 :         return TerrainTile(mp->Tex, mp->Priority);
     319             :     }
     320           0 :     void setNew(ssize_t x, ssize_t y, const TerrainTile& val)
     321             :     {
     322           0 :         CMiniPatch* mp = m_Terrain->GetTile(x, y);
     323           0 :         ENSURE(mp);
     324           0 :         mp->Tex = val.tex;
     325           0 :         mp->Priority = val.priority;
     326           0 :     }
     327             : 
     328             :     CTerrain* m_Terrain;
     329             :     ssize_t m_VertsPerSide;
     330             : };
     331             : 
     332             : }
     333             : 
     334           0 : BEGIN_COMMAND(PaintTerrain)
     335             : {
     336             :     TerrainArray m_TerrainDelta;
     337             :     ssize_t m_i0, m_j0, m_i1, m_j1; // dirtied tiles (inclusive lower bound, exclusive upper)
     338             : 
     339           0 :     cPaintTerrain()
     340           0 :     {
     341           0 :         m_TerrainDelta.Init();
     342             :     }
     343             : 
     344           0 :     void MakeDirty()
     345             :     {
     346           0 :         g_Game->GetWorld()->GetTerrain().MakeDirty(m_i0, m_j0, m_i1, m_j1,
     347             :             RENDERDATA_UPDATE_INDICES);
     348           0 :     }
     349             : 
     350           0 :     void Do()
     351             :     {
     352           0 :         g_CurrentBrush.m_Centre = msg->pos->GetWorldSpace();
     353             : 
     354           0 :         ssize_t x0, y0;
     355           0 :         g_CurrentBrush.GetBottomLeft(x0, y0);
     356             : 
     357           0 :         CTerrainTextureEntry* texentry = g_TexMan.FindTexture(CStrW(*msg->texture).ToUTF8());
     358           0 :         if (! texentry)
     359             :         {
     360           0 :             debug_warn(L"Can't find texentry"); // TODO: nicer error handling
     361           0 :             return;
     362             :         }
     363             : 
     364             :         // Priority system: If the new tile should have a high priority,
     365             :         // set it to one plus the maximum priority of all surrounding tiles
     366             :         // that aren't included in the brush (so that it's definitely the highest).
     367             :         // Similar for low priority.
     368           0 :         ssize_t priorityScale = (msg->priority == ePaintTerrainPriority::HIGH ? +1 : -1);
     369           0 :         ssize_t priority = 0;
     370             : 
     371           0 :         for (ssize_t dy = -1; dy < g_CurrentBrush.m_H+1; ++dy)
     372             :         {
     373           0 :             for (ssize_t dx = -1; dx < g_CurrentBrush.m_W+1; ++dx)
     374             :             {
     375           0 :                 if (!(g_CurrentBrush.Get(dx, dy) > 0.5f)) // ignore tiles that will be painted over
     376           0 :                     m_TerrainDelta.UpdatePriority(x0+dx, y0+dy, texentry, priorityScale, priority);
     377             :             }
     378             :         }
     379             : 
     380           0 :         for (ssize_t dy = 0; dy < g_CurrentBrush.m_H; ++dy)
     381             :         {
     382           0 :             for (ssize_t dx = 0; dx < g_CurrentBrush.m_W; ++dx)
     383             :             {
     384           0 :                 if (g_CurrentBrush.Get(dx, dy) > 0.5f) // TODO: proper solid brushes
     385           0 :                     m_TerrainDelta.PaintTile(x0+dx, y0+dy, texentry, priority*priorityScale);
     386             :             }
     387             :         }
     388             : 
     389           0 :         m_i0 = x0 - 1;
     390           0 :         m_j0 = y0 - 1;
     391           0 :         m_i1 = x0 + g_CurrentBrush.m_W + 1;
     392           0 :         m_j1 = y0 + g_CurrentBrush.m_H + 1;
     393           0 :         MakeDirty();
     394             :     }
     395             : 
     396           0 :     void Undo()
     397             :     {
     398           0 :         m_TerrainDelta.Undo();
     399           0 :         MakeDirty();
     400           0 :     }
     401             : 
     402           0 :     void Redo()
     403             :     {
     404           0 :         m_TerrainDelta.Redo();
     405           0 :         MakeDirty();
     406           0 :     }
     407             : 
     408           0 :     void MergeIntoPrevious(cPaintTerrain* prev)
     409             :     {
     410           0 :         prev->m_TerrainDelta.OverlayWith(m_TerrainDelta);
     411           0 :         prev->m_i0 = std::min(prev->m_i0, m_i0);
     412           0 :         prev->m_j0 = std::min(prev->m_j0, m_j0);
     413           0 :         prev->m_i1 = std::max(prev->m_i1, m_i1);
     414           0 :         prev->m_j1 = std::max(prev->m_j1, m_j1);
     415           0 :     }
     416             : };
     417           0 : END_COMMAND(PaintTerrain)
     418             : 
     419             : //////////////////////////////////////////////////////////////////////////
     420             : 
     421           0 : BEGIN_COMMAND(ReplaceTerrain)
     422             : {
     423             :     TerrainArray m_TerrainDelta;
     424             :     ssize_t m_i0, m_j0, m_i1, m_j1; // dirtied tiles (inclusive lower bound, exclusive upper)
     425             : 
     426           0 :     cReplaceTerrain()
     427           0 :     {
     428           0 :         m_TerrainDelta.Init();
     429             :     }
     430             : 
     431           0 :     void MakeDirty()
     432             :     {
     433           0 :         g_Game->GetWorld()->GetTerrain().MakeDirty(m_i0, m_j0, m_i1, m_j1,
     434             :             RENDERDATA_UPDATE_INDICES);
     435           0 :     }
     436             : 
     437           0 :     void Do()
     438             :     {
     439           0 :         g_CurrentBrush.m_Centre = msg->pos->GetWorldSpace();
     440             : 
     441           0 :         ssize_t x0, y0;
     442           0 :         g_CurrentBrush.GetBottomLeft(x0, y0);
     443             : 
     444           0 :         m_i0 = m_i1 = x0;
     445           0 :         m_j0 = m_j1 = y0;
     446             : 
     447           0 :         CTerrainTextureEntry* texentry = g_TexMan.FindTexture(CStrW(*msg->texture).ToUTF8());
     448           0 :         if (! texentry)
     449             :         {
     450           0 :             debug_warn(L"Can't find texentry"); // TODO: nicer error handling
     451           0 :             return;
     452             :         }
     453             : 
     454           0 :         CTerrainTextureEntry* replacedTex = m_TerrainDelta.GetTexEntry(x0, y0);
     455             : 
     456             :         // Don't bother if we're not making a change
     457           0 :         if (texentry == replacedTex)
     458             :         {
     459             :             return;
     460             :         }
     461             : 
     462           0 :         ssize_t tiles = m_TerrainDelta.GetTilesPerSide();
     463             : 
     464           0 :         for (ssize_t j = 0; j < tiles; ++j)
     465             :         {
     466           0 :             for (ssize_t i = 0; i < tiles; ++i)
     467             :             {
     468           0 :                 if (m_TerrainDelta.GetTexEntry(i, j) == replacedTex)
     469             :                 {
     470           0 :                     m_i0 = std::min(m_i0, i-1);
     471           0 :                     m_j0 = std::min(m_j0, j-1);
     472           0 :                     m_i1 = std::max(m_i1, i+2);
     473           0 :                     m_j1 = std::max(m_j1, j+2);
     474           0 :                     m_TerrainDelta.PaintTile(i, j, texentry, m_TerrainDelta.GetPriority(i, j));
     475             :                 }
     476             :             }
     477             :         }
     478             : 
     479           0 :         MakeDirty();
     480             :     }
     481             : 
     482           0 :     void Undo()
     483             :     {
     484           0 :         m_TerrainDelta.Undo();
     485           0 :         MakeDirty();
     486           0 :     }
     487             : 
     488           0 :     void Redo()
     489             :     {
     490           0 :         m_TerrainDelta.Redo();
     491           0 :         MakeDirty();
     492           0 :     }
     493             : };
     494           0 : END_COMMAND(ReplaceTerrain)
     495             : 
     496             : //////////////////////////////////////////////////////////////////////////
     497             : 
     498           0 : BEGIN_COMMAND(FillTerrain)
     499             : {
     500             :     TerrainArray m_TerrainDelta;
     501             :     ssize_t m_i0, m_j0, m_i1, m_j1; // dirtied tiles (inclusive lower bound, exclusive upper)
     502             : 
     503           0 :     cFillTerrain()
     504           0 :     {
     505             :         m_TerrainDelta.Init();
     506             :     }
     507             : 
     508           0 :     void MakeDirty()
     509             :     {
     510           0 :         g_Game->GetWorld()->GetTerrain().MakeDirty(m_i0, m_j0, m_i1, m_j1,
     511             :             RENDERDATA_UPDATE_INDICES);
     512           0 :     }
     513             : 
     514           0 :     void Do()
     515             :     {
     516           0 :         g_CurrentBrush.m_Centre = msg->pos->GetWorldSpace();
     517             : 
     518           0 :         ssize_t x0, y0;
     519           0 :         g_CurrentBrush.GetBottomLeft(x0, y0);
     520             : 
     521           0 :         m_i0 = m_i1 = x0;
     522           0 :         m_j0 = m_j1 = y0;
     523             : 
     524           0 :         CTerrainTextureEntry* texentry = g_TexMan.FindTexture(CStrW(*msg->texture).ToUTF8());
     525           0 :         if (! texentry)
     526             :         {
     527           0 :             debug_warn(L"Can't find texentry"); // TODO: nicer error handling
     528           0 :             return;
     529             :         }
     530             : 
     531           0 :         CTerrainTextureEntry* replacedTex = m_TerrainDelta.GetTexEntry(x0, y0);
     532             : 
     533             :         // Don't bother if we're not making a change
     534           0 :         if (texentry == replacedTex)
     535             :         {
     536             :             return;
     537             :         }
     538             : 
     539           0 :         ssize_t tiles = m_TerrainDelta.GetTilesPerSide();
     540             : 
     541             :         // Simple 4-way flood fill algorithm using queue and a grid to keep track of visited tiles,
     542             :         //  almost as fast as loop for filling whole map, much faster for small patches
     543           0 :         SparseGrid<bool> visited(tiles, tiles);
     544           0 :         std::queue<std::pair<std::uint16_t, std::uint16_t>> queue;
     545             : 
     546             :         // Initial tile
     547           0 :         queue.push(std::make_pair(static_cast<std::uint16_t>(x0), static_cast<std::uint16_t>(y0)));
     548           0 :         visited.set(x0, y0, true);
     549             : 
     550           0 :         while(!queue.empty())
     551             :         {
     552             :             // Check front of queue
     553           0 :             std::pair<std::uint16_t, std::uint16_t> t = queue.front();
     554           0 :             queue.pop();
     555           0 :             std::uint16_t i = t.first;
     556           0 :             std::uint16_t j = t.second;
     557             : 
     558           0 :             if (m_TerrainDelta.GetTexEntry(i, j) == replacedTex)
     559             :             {
     560             :                 // Found a tile to replace: adjust bounds and paint it
     561           0 :                 m_i0 = std::min(m_i0, (ssize_t)i-1);
     562           0 :                 m_j0 = std::min(m_j0, (ssize_t)j-1);
     563           0 :                 m_i1 = std::max(m_i1, (ssize_t)i+2);
     564           0 :                 m_j1 = std::max(m_j1, (ssize_t)j+2);
     565           0 :                 m_TerrainDelta.PaintTile(i, j, texentry, m_TerrainDelta.GetPriority(i, j));
     566             : 
     567             :                 // Visit 4 adjacent tiles (could visit 8 if we want to count diagonal adjacency)
     568           0 :                 if (i > 0 && !visited.get(i-1, j))
     569             :                 {
     570           0 :                     visited.set(i-1, j, true);
     571           0 :                     queue.push(std::make_pair(i-1, j));
     572             :                 }
     573           0 :                 if (i < (tiles-1) && !visited.get(i+1, j))
     574             :                 {
     575           0 :                     visited.set(i+1, j, true);
     576           0 :                     queue.push(std::make_pair(i+1, j));
     577             :                 }
     578           0 :                 if (j > 0 && !visited.get(i, j-1))
     579             :                 {
     580           0 :                     visited.set(i, j-1, true);
     581           0 :                     queue.push(std::make_pair(i, j-1));
     582             :                 }
     583           0 :                 if (j < (tiles-1) && !visited.get(i, j+1))
     584             :                 {
     585           0 :                     visited.set(i, j+1, true);
     586           0 :                     queue.push(std::make_pair(i, j+1));
     587             :                 }
     588             :             }
     589             :         }
     590             : 
     591           0 :         MakeDirty();
     592             :     }
     593             : 
     594           0 :     void Undo()
     595             :     {
     596           0 :         m_TerrainDelta.Undo();
     597           0 :         MakeDirty();
     598           0 :     }
     599             : 
     600           0 :     void Redo()
     601             :     {
     602           0 :         m_TerrainDelta.Redo();
     603           0 :         MakeDirty();
     604           0 :     }
     605             : };
     606           0 : END_COMMAND(FillTerrain)
     607             : 
     608             : } // namespace AtlasMessage

Generated by: LCOV version 1.16