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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
/* based on original los code in LosHandler.{cpp,h} and RadarHandler.{cpp,h} */
#include "LosMap.h"
#include "Map/ReadMap.h"
#include "System/myMath.h"
#include "System/float3.h"
#ifdef USE_UNSYNCED_HEIGHTMAP
#include "Game/GlobalUnsynced.h" // for myAllyTeam
#endif
#include <algorithm>
#include <cstring>
CR_BIND(CLosMap, )
CR_REG_METADATA(CLosMap, (
CR_MEMBER(size),
CR_MEMBER(map),
CR_MEMBER(sendReadmapEvents)
))
CR_BIND(CLosAlgorithm, (int2(), 0.0f, 0.0f, NULL))
CR_REG_METADATA(CLosAlgorithm, (
CR_MEMBER(size),
CR_MEMBER(minMaxAng),
CR_MEMBER(extraHeight)//,
//CR_MEMBER(heightmap)
))
void CLosMap::SetSize(int2 newSize, bool newSendReadmapEvents)
{
size = newSize;
sendReadmapEvents = newSendReadmapEvents;
map.clear();
map.resize(size.x * size.y, 0);
}
void CLosMap::AddMapArea(int2 pos, int allyteam, int radius, int amount)
{
#ifdef USE_UNSYNCED_HEIGHTMAP
static const int LOS2HEIGHT_X = gs->mapx / size.x;
static const int LOS2HEIGHT_Z = gs->mapy / size.y;
const bool updateUnsyncedHeightMap = (sendReadmapEvents && allyteam >= 0 && (allyteam == gu->myAllyTeam || gu->spectatingFullView));
#endif
const int sx = std::max( 0, pos.x - radius);
const int ex = std::min(size.x - 1, pos.x + radius);
const int sy = std::max( 0, pos.y - radius);
const int ey = std::min(size.y - 1, pos.y + radius);
const int rr = (radius * radius);
for (int lmz = sy; lmz <= ey; ++lmz) {
const int rrx = rr - Square(pos.y - lmz);
for (int lmx = sx; lmx <= ex; ++lmx) {
const int losMapSquareIdx = (lmz * size.x) + lmx;
#ifdef USE_UNSYNCED_HEIGHTMAP
const bool squareEnteredLOS = (map[losMapSquareIdx] == 0 && amount > 0);
#endif
if (Square(pos.x - lmx) > rrx) {
continue;
}
map[losMapSquareIdx] += amount;
#ifdef USE_UNSYNCED_HEIGHTMAP
// update unsynced heightmap for all squares that
// cover LOSmap square <x, y> (LOSmap resolution
// is never greater than that of the heightmap)
//
// NOTE:
// CLosMap is also used by RadarHandler, so only
// update the unsynced heightmap from LosHandler
// (by checking if allyteam >= 0)
//
if (!updateUnsyncedHeightMap) { continue; }
if (!squareEnteredLOS) { continue; }
const int
x1 = lmx * LOS2HEIGHT_X,
z1 = lmz * LOS2HEIGHT_Z;
const int
x2 = std::min((lmx + 1) * LOS2HEIGHT_X, gs->mapxm1),
z2 = std::min((lmz + 1) * LOS2HEIGHT_Z, gs->mapym1);
readMap->UpdateLOS(SRectangle(x1, z1, x2, z2));
#endif
}
}
}
void CLosMap::AddMapSquares(const std::vector<int>& squares, int allyteam, int amount)
{
#ifdef USE_UNSYNCED_HEIGHTMAP
static const int LOS2HEIGHT_X = gs->mapx / size.x;
static const int LOS2HEIGHT_Z = gs->mapy / size.y;
const bool updateUnsyncedHeightMap = (sendReadmapEvents && allyteam >= 0 && (allyteam == gu->myAllyTeam || gu->spectatingFullView));
#endif
std::vector<int>::const_iterator lsi;
for (lsi = squares.begin(); lsi != squares.end(); ++lsi) {
const int losMapSquareIdx = *lsi;
#ifdef USE_UNSYNCED_HEIGHTMAP
const bool squareEnteredLOS = (map[losMapSquareIdx] == 0 && amount > 0);
#endif
map[losMapSquareIdx] += amount;
#ifdef USE_UNSYNCED_HEIGHTMAP
if (!updateUnsyncedHeightMap) { continue; }
if (!squareEnteredLOS) { continue; }
const int
lmx = losMapSquareIdx % size.x,
lmz = losMapSquareIdx / size.x;
const int
x1 = lmx * LOS2HEIGHT_X,
z1 = lmz * LOS2HEIGHT_Z,
x2 = std::min((lmx + 1) * LOS2HEIGHT_X, gs->mapxm1),
z2 = std::min((lmz + 1) * LOS2HEIGHT_Z, gs->mapym1);
readMap->UpdateLOS(SRectangle(x1, z1, x2, z2));
#endif
}
}
//////////////////////////////////////////////////////////////////////
namespace {
//////////////////////////////////////////////////////////////////////
#define MAX_LOS_TABLE 110
typedef std::vector<int2> TPoints;
typedef std::vector<int2> LosLine;
typedef std::vector<LosLine> LosTable;
class CLosTables
{
public:
static const LosTable& GetForLosSize(int losSize) {
static CLosTables instance;
const int tablenum = std::min(MAX_LOS_TABLE, losSize);
return instance.lostables[tablenum - 1];
}
private:
std::vector<LosTable> lostables;
CLosTables();
void DrawLine(char* PaintTable, int x,int y,int Size);
LosLine OutputLine(int x,int y,int line);
void OutputTable(int table);
};
CLosTables::CLosTables()
{
for (int a = 1; a <= MAX_LOS_TABLE; ++a) {
OutputTable(a);
}
}
struct int2_comparer
{
bool operator () (const int2& a, const int2& b) const
{
if (a.x != b.x)
return a.x < b.x;
else
return a.y < b.y;
}
};
void CLosTables::OutputTable(int Table)
{
TPoints Points;
LosTable lostable;
int Radius = Table;
char* PaintTable = new char[(Radius+1)*Radius];
memset(PaintTable, 0 , (Radius+1)*Radius);
int2 P(0, Radius);
Points.push_back(P);
// DrawLine(0, Radius, Radius);
for (float i=Radius; i>=1; i-=0.5f) {
const int r2 = (int)(i * i);
int x = 1;
int y = (int) (math::sqrt((float)r2 - 1) + 0.5f);
while (x < y) {
if (!PaintTable[x+y*Radius]) {
DrawLine(PaintTable, x, y, Radius);
P.x = x;
P.y = y;
Points.push_back(P);
}
if (!PaintTable[y+x*Radius]) {
DrawLine(PaintTable, y, x, Radius);
P.x = y;
P.y = x;
Points.push_back(P);
}
x += 1;
y = (int) (math::sqrt((float)r2 - x*x) + 0.5f);
}
if (x == y) {
if (!PaintTable[x+y*Radius]) {
DrawLine(PaintTable, x, y, Radius);
P.x = x;
P.y = y;
Points.push_back(P);
}
}
}
std::sort(Points.begin(), Points.end(), int2_comparer());
int Line = 1;
int Size = Points.size();
for (int j = 0; j < Size; j++) {
lostable.push_back(OutputLine(Points.back().x, Points.back().y, Line));
Points.pop_back();
Line++;
}
lostables.push_back(lostable);
delete[] PaintTable;
}
LosLine CLosTables::OutputLine(int x, int y, int Line)
{
LosLine losline;
int x0 = 0;
int y0 = 0;
int dx = x;
int dy = y;
if (abs(dx) > abs(dy)) { // slope <1
float m = (float) dy / (float) dx; // compute slope
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x) {
x0 += dx;
losline.push_back(int2(x0, Round(m*x0 + b)));
}
} else if (dy != 0) { // slope = 1
float m = (float) dx / (float) dy; // compute slope
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y) {
y0 += dy;
losline.push_back(int2(Round(m*y0 + b), y0));
}
}
return losline;
}
void CLosTables::DrawLine(char* PaintTable, int x, int y, int Size)
{
int x0 = 0;
int y0 = 0;
int dx = x;
int dy = y;
if (abs(dx) > abs(dy)) { // slope <1
float m = (float) dy / (float) dx; // compute slope
float b = y0 - m*x0;
dx = (dx < 0) ? -1 : 1;
while (x0 != x) {
x0 += dx;
PaintTable[x0+Round(m*x0 + b)*Size] = 1;
}
} else if (dy != 0) { // slope = 1
float m = (float) dx / (float) dy; // compute slope
float b = x0 - m*y0;
dy = (dy < 0) ? -1 : 1;
while (y0 != y) {
y0 += dy;
PaintTable[Round(m*y0 + b)+y0*Size] = 1;
}
}
}
//////////////////////////////////////////////////////////////////////
} // end of anon namespace
//////////////////////////////////////////////////////////////////////
void CLosAlgorithm::LosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares)
{
if (radius <= 0) { return; }
pos.x = Clamp(pos.x, 0, size.x - 1);
pos.y = Clamp(pos.y, 0, size.y - 1);
if ((pos.x - radius < radius) || (pos.x + radius >= size.x - radius) || // FIXME: This additional margin is due to a suspect bug in losalgorithm
(pos.y - radius < radius) || (pos.y + radius >= size.y - radius)) { // causing rare crash with big units such as arm Colossus
SafeLosAdd(pos, radius, baseHeight, squares);
} else {
UnsafeLosAdd(pos, radius, baseHeight, squares);
}
}
#define MAP_SQUARE(pos) ((pos).y * size.x + (pos).x)
#define LOS_ADD(_square, _maxAng) { \
const int square = _square; \
const float dh = heightmap[square] - baseHeight; \
float ang = (dh + extraHeight) * invR; \
\
if (ang > _maxAng) { \
squares.push_back(square); \
ang = dh * invR; \
if (ang > _maxAng) _maxAng = ang; \
} \
}
void CLosAlgorithm::UnsafeLosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares)
{
const int mapSquare = MAP_SQUARE(pos);
const LosTable& table = CLosTables::GetForLosSize(radius);
// NOTE: floating and flying units have their baseHeight adjusted in MoveType::SlowUpdate
baseHeight += heightmap[mapSquare];
size_t neededSpace = squares.size() + 1;
for(LosTable::const_iterator li = table.begin(); li != table.end(); ++li) {
neededSpace += li->size() * 4;
}
squares.reserve(neededSpace);
squares.push_back(mapSquare);
for(LosTable::const_iterator li = table.begin(); li != table.end(); ++li) {
const LosLine& line = *li;
float maxAng1 = minMaxAng;
float maxAng2 = minMaxAng;
float maxAng3 = minMaxAng;
float maxAng4 = minMaxAng;
float r = 1;
for(LosLine::const_iterator linei = line.begin(); linei != line.end(); ++linei) {
const float invR = 1.0f / r;
LOS_ADD(mapSquare + linei->x + linei->y * size.x, maxAng1);
LOS_ADD(mapSquare - linei->x - linei->y * size.x, maxAng2);
LOS_ADD(mapSquare - linei->x * size.x + linei->y, maxAng3);
LOS_ADD(mapSquare + linei->x * size.x - linei->y, maxAng4);
r++;
}
}
}
void CLosAlgorithm::SafeLosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares)
{
const int mapSquare = MAP_SQUARE(pos);
const LosTable& table = CLosTables::GetForLosSize(radius);
// NOTE: floating and flying units have their baseHeight adjusted in MoveType::SlowUpdate
baseHeight += heightmap[mapSquare];
squares.push_back(mapSquare);
for (LosTable::const_iterator li = table.begin(); li != table.end(); ++li) {
const LosLine& line = *li;
float maxAng1 = minMaxAng;
float maxAng2 = minMaxAng;
float maxAng3 = minMaxAng;
float maxAng4 = minMaxAng;
float r = 1;
for(LosLine::const_iterator linei = line.begin(); linei != line.end(); ++linei) {
const float invR = 1.0f / r;
if ((pos.x + linei->x < size.x) && (pos.y + linei->y < size.y)) {
LOS_ADD(mapSquare + linei->x + linei->y * size.x, maxAng1);
}
if ((pos.x - linei->x >= 0) && (pos.y - linei->y >= 0)) {
LOS_ADD(mapSquare - linei->x - linei->y * size.x, maxAng2);
}
if ((pos.x + linei->y < size.x) && (pos.y - linei->x >= 0)) {
LOS_ADD(mapSquare - linei->x * size.x + linei->y, maxAng3);
}
if ((pos.x - linei->y >= 0) && (pos.y + linei->x < size.y)) {
LOS_ADD(mapSquare + linei->x * size.x - linei->y, maxAng4);
}
r++;
}
}
}
|