1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "Path.h"
#include "Game/GameHelper.h"
#include "Game/GlobalUnsynced.h"
#include "Game/SelectedUnitsHandler.h"
#include "Game/UI/GuiHandler.h"
#include "Sim/Misc/GlobalConstants.h"
#include "Sim/Misc/LosHandler.h"
#include "Sim/MoveTypes/MoveMath/MoveMath.h"
#include "Sim/MoveTypes/MoveDefHandler.h"
#include "Sim/Units/BuildInfo.h"
#include "Sim/Units/CommandAI/CommandDescription.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitDefHandler.h"
#include "System/Color.h"
#include "System/Exceptions.h"
#include "System/Threading/ThreadPool.h"
#include "System/Log/ILog.h"
CPathTexture::CPathTexture()
: CPboInfoTexture("path")
, isCleared(false)
//, updateFrame(0)
, updateProcess(0)
, lastSelectedPathType(0)
, forcedPathType(-1)
, forcedUnitDef(-1)
, lastUsage(spring_gettime())
{
texSize = int2(mapDims.hmapx, mapDims.hmapy);
texChannels = 4;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSpringTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, texSize.x, texSize.y);
infoTexPBO.Bind();
infoTexPBO.New(texSize.x * texSize.y * texChannels, GL_STREAM_DRAW);
infoTexPBO.Unbind();
if (FBO::IsSupported()) {
fbo.Bind();
fbo.AttachTexture(texture);
/*bool status =*/ fbo.CheckStatus("CPathTexture");
FBO::Unbind();
}
if (!fbo.IsValid()) {
throw opengl_error("");
}
}
enum BuildSquareStatus {
NOLOS = 0,
FREE = 1,
OBJECTBLOCKED = 2,
TERRAINBLOCKED = 3,
};
static const SColor buildColors[] = {
SColor( 0, 0, 0), // nolos
SColor( 0, 255, 0), // free
SColor( 0, 0, 255), // objblocked
SColor(254, 0, 0), // terrainblocked
};
static inline const SColor& GetBuildColor(const BuildSquareStatus& status) {
return buildColors[status];
}
static SColor GetSpeedModColor(const float sm) {
SColor col(255, 0, 0);
if (sm > 0.0f) {
col.r = 255 - std::min(sm * 255.0f, 255.0f);
col.g = 255 - col.r;
} else {
col.b = 255;
}
return col;
}
const MoveDef* CPathTexture::GetSelectedMoveDef()
{
if (forcedPathType >= 0)
return moveDefHandler->GetMoveDefByPathType(forcedPathType);
const MoveDef* md = nullptr;
const auto& unitSet = selectedUnitsHandler.selectedUnits;
if (!unitSet.empty()) {
const CUnit* unit = unitHandler->GetUnit(*unitSet.begin());
md = unit->moveDef;
}
return md;
}
const UnitDef* CPathTexture::GetCurrentBuildCmdUnitDef()
{
if (forcedUnitDef >= 0) {
return unitDefHandler->GetUnitDefByID(forcedUnitDef);
}
if ((unsigned)guihandler->inCommand > guihandler->commands.size())
return nullptr;
if (guihandler->commands[guihandler->inCommand].type != CMDTYPE_ICON_BUILDING)
return nullptr;
return unitDefHandler->GetUnitDefByID(-guihandler->commands[guihandler->inCommand].id);
}
GLuint CPathTexture::GetTexture()
{
lastUsage = spring_gettime();
return texture;
}
bool CPathTexture::ShowMoveDef(const int pathType)
{
forcedUnitDef = -1;
forcedPathType = pathType;
updateProcess = 0;
return true; // TODO: unused
}
bool CPathTexture::ShowUnitDef(const int udefid)
{
forcedUnitDef = udefid;
forcedPathType = -1;
updateProcess = 0;
return true; // TODO: unused
}
bool CPathTexture::IsUpdateNeeded()
{
// don't update when not rendered/used
if ((spring_gettime() - lastUsage).toSecsi() > 2) {
forcedUnitDef = forcedPathType = -1;
return false;
}
// newly build cmd active?
const UnitDef* ud = GetCurrentBuildCmdUnitDef();
if (ud) {
const unsigned int buildDefID = ud ? -(ud->id + 1) : 0;
if (buildDefID != lastSelectedPathType) {
lastSelectedPathType = buildDefID;
updateProcess = 0;
return true;
}
} else {
// newly unit/moveType active?
const MoveDef* md = GetSelectedMoveDef();
if (md) {
const unsigned int pathType = md ? (md->pathType + 1) : 0;
if (pathType != lastSelectedPathType) {
lastSelectedPathType = pathType;
updateProcess = 0;
return true;
}
}
}
// nothing selected nor any build cmd active -> don't update
if (lastSelectedPathType == 0 && isCleared) {
return false;
}
return true;
}
void CPathTexture::Update()
{
const MoveDef* md = GetSelectedMoveDef();
const UnitDef* ud = GetCurrentBuildCmdUnitDef();
// just clear
if (!(ud || md)) {
isCleared = true;
updateProcess = 0;
fbo.Bind();
glViewport(0,0, texSize.x, texSize.y);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(globalRendering->viewPosX,0,globalRendering->viewSizeX,globalRendering->viewSizeY);
FBO::Unbind();
return;
}
// spread update across time
if (updateProcess >= texSize.y) updateProcess = 0;
int start = updateProcess;
const int updateLines = std::max((64*64) / texSize.x, ThreadPool::GetNumThreads());
updateProcess += updateLines;
updateProcess = std::min(updateProcess, texSize.y);
// map PBO
infoTexPBO.Bind();
const size_t offset = start * texSize.x;
SColor* infoTexMem = reinterpret_cast<SColor*>(infoTexPBO.MapBuffer(offset * sizeof(SColor), (updateProcess - start) * texSize.x * sizeof(SColor)));
//FIXME make global func
const bool losFullView = ((gu->spectating && gu->spectatingFullView) || losHandler->globalLOS[gu->myAllyTeam]);
if (ud != nullptr) {
// CGameHelper::TestUnitBuildSquare accesses QuadField which is not re-entrant
// for_mt(start, updateProcess, [&](const int y) {
for (int y = start; y < updateProcess; y++) {
for (int x = 0; x < texSize.x; ++x) {
const float3 pos = float3(x << 1, 0.0f, y << 1) * SQUARE_SIZE;
const int idx = y * texSize.x + x;
BuildSquareStatus status = FREE;
BuildInfo bi(ud, pos, guihandler->buildFacing);
bi.pos = CGameHelper::Pos2BuildPos(bi, false);
CFeature* f = nullptr;
if (CGameHelper::TestUnitBuildSquare(bi, f, gu->myAllyTeam, false)) {
if (f != nullptr) {
status = OBJECTBLOCKED;
}
} else {
status = TERRAINBLOCKED;
}
infoTexMem[idx - offset] = GetBuildColor(status);
}
}
} else if (md != NULL) {
for_mt(start, updateProcess, [&](const int y) {
for (int x = 0; x < texSize.x; ++x) {
const int2 sq = int2(x << 1, y << 1);
const int idx = y * texSize.x + x;
float scale = 1.0f;
if (losFullView || losHandler->InLos(SquareToFloat3(sq), gu->myAllyTeam)) {
if (CMoveMath::IsBlocked(*md, sq.x, sq.y , NULL) & CMoveMath::BLOCK_STRUCTURE) { scale -= 0.25f; }
if (CMoveMath::IsBlocked(*md, sq.x + 1, sq.y , NULL) & CMoveMath::BLOCK_STRUCTURE) { scale -= 0.25f; }
if (CMoveMath::IsBlocked(*md, sq.x, sq.y + 1, NULL) & CMoveMath::BLOCK_STRUCTURE) { scale -= 0.25f; }
if (CMoveMath::IsBlocked(*md, sq.x + 1, sq.y + 1, NULL) & CMoveMath::BLOCK_STRUCTURE) { scale -= 0.25f; }
}
// NOTE: raw speedmods are not necessarily clamped to [0, 1]
const float sm = CMoveMath::GetPosSpeedMod(*md, sq.x, sq.y);
infoTexMem[idx - offset] = GetSpeedModColor(sm * scale);
}
});
}
infoTexPBO.UnmapBuffer();
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, start, texSize.x, updateProcess - start, GL_RGBA, GL_UNSIGNED_BYTE, infoTexPBO.GetPtr(offset * sizeof(SColor)));
infoTexPBO.Unbind();
isCleared = false;
}
|