2022-01-05 15:49:54 +01:00
|
|
|
/* Copyright (C) 2022 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2011-07-20 21:48:06 +02:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2011-07-20 21:48:06 +02:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2011-07-20 21:48:06 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2011-07-20 21:48:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "TerritoryTexture.h"
|
|
|
|
|
2019-01-13 16:38:41 +01:00
|
|
|
#include "graphics/Color.h"
|
2011-07-20 21:48:06 +02:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "lib/bits.h"
|
|
|
|
#include "ps/Profile.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/IDevice.h"
|
|
|
|
#include "renderer/backend/IDeviceCommandContext.h"
|
2011-07-20 21:48:06 +02:00
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "simulation2/Simulation2.h"
|
2020-06-14 22:39:03 +02:00
|
|
|
#include "simulation2/helpers/Grid.h"
|
New long-range pathfinder.
Based on Philip's work located at
http://git.wildfiregames.com/gitweb/?p=0ad.git;a=shortlog;h=refs/heads/projects/philip/pathfinder
Includes code by wraitii, sanderd17 and kanetaka.
An updated version of docs/pathfinder.pdf describing the changes in
detail will be committed ASAP.
Running update-workspaces is needed after this change.
Fixes #1756.
Fixes #930, #1259, #2908, #2960, #3097
Refs #1200, #1914, #1942, #2568, #2132, #2563
This was SVN commit r16751.
2015-06-12 20:58:24 +02:00
|
|
|
#include "simulation2/helpers/Pathfinding.h"
|
2011-07-30 14:07:27 +02:00
|
|
|
#include "simulation2/components/ICmpPlayer.h"
|
|
|
|
#include "simulation2/components/ICmpPlayerManager.h"
|
2011-07-20 21:48:06 +02:00
|
|
|
#include "simulation2/components/ICmpTerrain.h"
|
|
|
|
#include "simulation2/components/ICmpTerritoryManager.h"
|
|
|
|
|
|
|
|
// TODO: There's a lot of duplication with CLOSTexture - might be nice to refactor a bit
|
|
|
|
|
|
|
|
CTerritoryTexture::CTerritoryTexture(CSimulation2& simulation) :
|
2021-12-30 17:37:51 +01:00
|
|
|
m_Simulation(simulation), m_DirtyID(0), m_MapSize(0)
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CTerritoryTexture::~CTerritoryTexture()
|
|
|
|
{
|
2021-12-25 01:26:10 +01:00
|
|
|
DeleteTexture();
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTerritoryTexture::DeleteTexture()
|
|
|
|
{
|
2021-12-25 01:26:10 +01:00
|
|
|
m_Texture.reset();
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CTerritoryTexture::UpdateDirty()
|
|
|
|
{
|
|
|
|
CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
|
2018-02-03 15:17:31 +01:00
|
|
|
return cmpTerritoryManager && cmpTerritoryManager->NeedUpdateTexture(&m_DirtyID);
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::ITexture* CTerritoryTexture::GetTexture()
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
2022-01-05 15:49:54 +01:00
|
|
|
ENSURE(!UpdateDirty());
|
2021-12-30 17:37:51 +01:00
|
|
|
return m_Texture.get();
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
2022-04-27 21:53:42 +02:00
|
|
|
const CMatrix3D& CTerritoryTexture::GetTextureMatrix()
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
|
|
|
ENSURE(!UpdateDirty());
|
2022-04-27 21:53:42 +02:00
|
|
|
return m_TextureMatrix;
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
const CMatrix3D& CTerritoryTexture::GetMinimapTextureMatrix()
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
|
|
|
ENSURE(!UpdateDirty());
|
2022-01-05 15:49:54 +01:00
|
|
|
return m_MinimapTextureMatrix;
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
void CTerritoryTexture::ConstructTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
|
|
|
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (!cmpTerrain)
|
2011-07-20 21:48:06 +02:00
|
|
|
return;
|
|
|
|
|
New long-range pathfinder.
Based on Philip's work located at
http://git.wildfiregames.com/gitweb/?p=0ad.git;a=shortlog;h=refs/heads/projects/philip/pathfinder
Includes code by wraitii, sanderd17 and kanetaka.
An updated version of docs/pathfinder.pdf describing the changes in
detail will be committed ASAP.
Running update-workspaces is needed after this change.
Fixes #1756.
Fixes #930, #1259, #2908, #2960, #3097
Refs #1200, #1914, #1942, #2568, #2132, #2563
This was SVN commit r16751.
2015-06-12 20:58:24 +02:00
|
|
|
// Convert size from terrain tiles to territory tiles
|
2021-05-02 19:40:00 +02:00
|
|
|
m_MapSize = cmpTerrain->GetMapSize() * Pathfinding::NAVCELL_SIZE_INT / ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE;
|
2011-07-20 21:48:06 +02:00
|
|
|
|
2021-12-30 17:37:51 +01:00
|
|
|
const uint32_t textureSize = round_up_to_pow2(static_cast<uint32_t>(m_MapSize));
|
2011-07-20 21:48:06 +02:00
|
|
|
|
2022-02-13 20:30:28 +01:00
|
|
|
m_Texture = deviceCommandContext->GetDevice()->CreateTexture2D("TerritoryTexture",
|
2022-10-13 01:19:27 +02:00
|
|
|
Renderer::Backend::ITexture::Usage::TRANSFER_DST |
|
|
|
|
Renderer::Backend::ITexture::Usage::SAMPLED,
|
2022-03-06 23:14:57 +01:00
|
|
|
Renderer::Backend::Format::R8G8B8A8_UNORM, textureSize, textureSize,
|
2021-12-25 01:26:10 +01:00
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::LINEAR,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
|
2011-07-20 21:48:06 +02:00
|
|
|
|
|
|
|
// Initialise texture with transparency, for the areas we don't
|
2022-01-05 15:49:54 +01:00
|
|
|
// overwrite with uploading later.
|
|
|
|
std::unique_ptr<u8[]> texData = std::make_unique<u8[]>(textureSize * textureSize * 4);
|
|
|
|
memset(texData.get(), 0x00, textureSize * textureSize * 4);
|
|
|
|
deviceCommandContext->UploadTexture(
|
2022-03-06 23:14:57 +01:00
|
|
|
m_Texture.get(), Renderer::Backend::Format::R8G8B8A8_UNORM,
|
2022-02-13 20:30:28 +01:00
|
|
|
texData.get(), textureSize * textureSize * 4);
|
2022-01-05 15:49:54 +01:00
|
|
|
texData.reset();
|
2011-07-20 21:48:06 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
// Texture matrix: We want to map
|
|
|
|
// world pos (0, y, 0) (i.e. bottom-left of first tile)
|
|
|
|
// onto texcoord (0, 0) (i.e. bottom-left of first texel);
|
|
|
|
// world pos (mapsize*cellsize, y, mapsize*cellsize) (i.e. top-right of last tile)
|
|
|
|
// onto texcoord (mapsize / texsize, mapsize / texsize) (i.e. top-right of last texel)
|
|
|
|
|
2021-12-30 17:37:51 +01:00
|
|
|
float s = 1.f / static_cast<float>(textureSize * TERRAIN_TILE_SIZE);
|
2011-07-20 21:48:06 +02:00
|
|
|
float t = 0.f;
|
|
|
|
m_TextureMatrix.SetZero();
|
|
|
|
m_TextureMatrix._11 = s;
|
|
|
|
m_TextureMatrix._23 = s;
|
|
|
|
m_TextureMatrix._14 = t;
|
|
|
|
m_TextureMatrix._24 = t;
|
|
|
|
m_TextureMatrix._44 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Minimap matrix: We want to map UV (0,0)-(1,1) onto (0,0)-(mapsize/texsize, mapsize/texsize)
|
|
|
|
|
2021-12-30 17:37:51 +01:00
|
|
|
float s = m_MapSize / static_cast<float>(textureSize);
|
2011-07-20 21:48:06 +02:00
|
|
|
m_MinimapTextureMatrix.SetZero();
|
|
|
|
m_MinimapTextureMatrix._11 = s;
|
|
|
|
m_MinimapTextureMatrix._22 = s;
|
|
|
|
m_MinimapTextureMatrix._44 = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
void CTerritoryTexture::RecomputeTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
|
2011-07-20 21:48:06 +02:00
|
|
|
{
|
|
|
|
// If the map was resized, delete and regenerate the texture
|
|
|
|
if (m_Texture)
|
|
|
|
{
|
|
|
|
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
|
2011-07-20 21:48:06 +02:00
|
|
|
DeleteTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_Texture)
|
2022-01-05 15:49:54 +01:00
|
|
|
ConstructTexture(deviceCommandContext);
|
2011-07-20 21:48:06 +02:00
|
|
|
|
|
|
|
PROFILE("recompute territory texture");
|
|
|
|
|
|
|
|
CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (!cmpTerritoryManager)
|
2011-07-20 21:48:06 +02:00
|
|
|
return;
|
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
std::unique_ptr<u8[]> bitmap = std::make_unique<u8[]>(m_MapSize * m_MapSize * 4);
|
|
|
|
GenerateBitmap(cmpTerritoryManager->GetTerritoryGrid(), bitmap.get(), m_MapSize, m_MapSize);
|
2011-07-20 21:48:06 +02:00
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
deviceCommandContext->UploadTextureRegion(
|
2022-03-06 23:14:57 +01:00
|
|
|
m_Texture.get(), Renderer::Backend::Format::R8G8B8A8_UNORM, bitmap.get(), m_MapSize * m_MapSize * 4,
|
2022-01-05 15:49:54 +01:00
|
|
|
0, 0, m_MapSize, m_MapSize);
|
2011-07-20 21:48:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CTerritoryTexture::GenerateBitmap(const Grid<u8>& territories, u8* bitmap, ssize_t w, ssize_t h)
|
|
|
|
{
|
|
|
|
int alphaMax = 0xC0;
|
|
|
|
int alphaFalloff = 0x20;
|
|
|
|
|
2011-07-30 14:07:27 +02:00
|
|
|
CmpPtr<ICmpPlayerManager> cmpPlayerManager(m_Simulation, SYSTEM_ENTITY);
|
|
|
|
|
2011-08-17 01:39:16 +02:00
|
|
|
std::vector<CColor> colors;
|
|
|
|
i32 numPlayers = cmpPlayerManager->GetNumPlayers();
|
|
|
|
for (i32 p = 0; p < numPlayers; ++p)
|
|
|
|
{
|
|
|
|
CColor color(1, 0, 1, 1);
|
|
|
|
CmpPtr<ICmpPlayer> cmpPlayer(m_Simulation, cmpPlayerManager->GetPlayerByID(p));
|
2012-02-08 03:46:15 +01:00
|
|
|
if (cmpPlayer)
|
2018-02-03 15:17:31 +01:00
|
|
|
color = cmpPlayer->GetDisplayedColor();
|
2011-08-17 01:39:16 +02:00
|
|
|
colors.push_back(color);
|
|
|
|
}
|
|
|
|
|
2011-07-20 21:48:06 +02:00
|
|
|
u8* p = bitmap;
|
|
|
|
for (ssize_t j = 0; j < h; ++j)
|
|
|
|
for (ssize_t i = 0; i < w; ++i)
|
|
|
|
{
|
2011-08-18 22:28:53 +02:00
|
|
|
u8 val = territories.get(i, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK;
|
2011-07-30 14:07:27 +02:00
|
|
|
|
|
|
|
CColor color(1, 0, 1, 1);
|
2018-02-17 02:37:52 +01:00
|
|
|
if (val < colors.size())
|
2011-08-17 01:39:16 +02:00
|
|
|
color = colors[val];
|
2011-07-20 21:48:06 +02:00
|
|
|
|
2018-02-17 02:37:52 +01:00
|
|
|
*p++ = (int)(color.r * 255.f);
|
|
|
|
*p++ = (int)(color.g * 255.f);
|
|
|
|
*p++ = (int)(color.b * 255.f);
|
2011-07-30 14:07:27 +02:00
|
|
|
|
2018-02-17 02:37:52 +01:00
|
|
|
// Use alphaMax for borders and gaia territory; these tiles will be deleted later
|
|
|
|
if (val == 0 ||
|
|
|
|
(i > 0 && (territories.get(i-1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
|
|
|
|
(i < w-1 && (territories.get(i+1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
|
|
|
|
(j > 0 && (territories.get(i, j-1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
|
|
|
|
(j < h-1 && (territories.get(i, j+1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val))
|
2011-07-20 21:48:06 +02:00
|
|
|
*p++ = alphaMax;
|
|
|
|
else
|
|
|
|
*p++ = 0x00;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a low-quality cheap blur effect
|
|
|
|
|
|
|
|
for (ssize_t j = 0; j < h; ++j)
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
|
|
|
|
a = 0;
|
|
|
|
for (ssize_t i = 0; i < w; ++i)
|
|
|
|
{
|
|
|
|
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
|
|
|
|
bitmap[(j*w+i)*4 + 3] = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 0;
|
|
|
|
for (ssize_t i = w-1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
|
|
|
|
bitmap[(j*w+i)*4 + 3] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ssize_t i = 0; i < w; ++i)
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
|
|
|
|
a = 0;
|
|
|
|
for (ssize_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
|
|
|
|
bitmap[(j*w+i)*4 + 3] = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = 0;
|
|
|
|
for (ssize_t j = w-1; j >= 0; --j)
|
|
|
|
{
|
|
|
|
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
|
|
|
|
bitmap[(j*w+i)*4 + 3] = a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a gap between the boundaries, by deleting the max-alpha tiles
|
|
|
|
for (ssize_t j = 0; j < h; ++j)
|
|
|
|
for (ssize_t i = 0; i < w; ++i)
|
|
|
|
if (bitmap[(j*w+i)*4 + 3] == alphaMax)
|
|
|
|
bitmap[(j*w+i)*4 + 3] = 0;
|
|
|
|
}
|
2022-01-05 15:49:54 +01:00
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
void CTerritoryTexture::UpdateIfNeeded(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
|
2022-01-05 15:49:54 +01:00
|
|
|
{
|
|
|
|
if (UpdateDirty())
|
|
|
|
RecomputeTexture(deviceCommandContext);
|
|
|
|
}
|