File: LosMap.cpp

package info (click to toggle)
spring 105.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,860 kB
  • sloc: cpp: 467,785; ansic: 302,607; python: 12,925; java: 12,201; awk: 5,889; sh: 2,371; xml: 655; perl: 405; php: 276; objc: 194; makefile: 75; sed: 2
file content (814 lines) | stat: -rw-r--r-- 22,489 bytes parent folder | download | duplicates (3)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include <algorithm>
#include <array>

#include "LosMap.h"
#include "LosHandler.h"
#include "Map/ReadMap.h"
#include "System/SpringMath.h"
#include "System/float3.h"
#include "System/Log/ILog.h"
#include "System/StringUtil.h"
#include "System/Threading/ThreadPool.h"
#ifdef USE_UNSYNCED_HEIGHTMAP
	#include "Game/GlobalUnsynced.h" // for myAllyTeam
#endif

constexpr float LOS_BONUS_HEIGHT = 5.0f;



static std::array<std::vector<float>, ThreadPool::MAX_THREADS> RADIUS_ISQRT_TABLES;

static std::array<std::vector<float>, ThreadPool::MAX_THREADS> RAYCAST_ANGLE_TABLES;
static std::array<std::vector< char>, ThreadPool::MAX_THREADS> LOSRAY_SQUARE_TABLES; // visible squares per instance


static float isqrtTableLookup(unsigned r, int threadNum)
{
	assert(r < RADIUS_ISQRT_TABLES[threadNum].size());
	return RADIUS_ISQRT_TABLES[threadNum][r];
}

static void isqrtTableExpand(unsigned r, int threadNum)
{
	auto& isqrtTable = RADIUS_ISQRT_TABLES[threadNum];

	if (r < isqrtTable.size())
		return;
	if (isqrtTable.empty())
		isqrtTable.reserve((r + 1) * 4);

	for (unsigned i = isqrtTable.size(); i <= r; ++i) {
		isqrtTable.push_back(math::isqrt(std::max(i, 1u)));
	}
}



// Midpoint circle algorithm
// func() only get called for the lower top right octant.
// The others need to get by mirroring.
template<typename F>
void MidpointCircleAlgo(int radius, const F& func)
{
	int x = radius;
	int y = 0;
	int decisionOver2 = 1 - x;

	while (x >= y) {
		func(x, y);

		y++;
		if (decisionOver2 <= 0) {
			decisionOver2 += 2 * y + 1;
		} else {
			x--;
			decisionOver2 += 2 * (y - x) + 1;
		}
	}
}


// Calls func(half_line_width, y) for each line of the filled circle.
template<typename F>
void MidpointCircleAlgoPerLine(int radius, const F& func)
{
	int x = radius;
	int y = 0;
	int decisionOver2 = 1 - x;

	while (x >= y) {
		func(x, y);

		if (y != 0)
			func(x, -y);

		if (decisionOver2 <= 0) {
			y++;
			decisionOver2 += 2 * y + 1;
		} else {
			if (x != y) {
				func(y, x);

				if (x != 0)
					func(y, -x);
			}

			y++;
			x--;
			decisionOver2 += 2 * (y - x) + 1;
		}
	}
}






//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/// raycast precalculation helper

class CLosTableHelper
{
public:
	typedef std::vector<int2> LosLine;
	typedef std::vector<LosLine> LosTable;

	// only generates table if not in cache
	void GenerateForLosSize(size_t losSize);

	const int2 GetLosTableRaySquare(size_t losSize, size_t rayIndex, size_t squareIdx) {
		return losTables[losSize][rayIndex][squareIdx];
	}

	size_t GetLosTableRaySize(size_t losSize, size_t rayIndex) {
		return losTables[losSize][rayIndex].size();
	}

	size_t GetLosTableSize(size_t losSize) {
		return losTables[losSize].size();
	}

private:
	// [0] is the zero-radius table
	// NOTE:
	//   do we even need a table for *every* possible radius?
	//   why not precalculate only the largest and subsample?
	std::array<LosTable, MAX_UNIT_SENSOR_RADIUS + 1> losTables;

private:
	static LosLine GetRay(int x, int y);
	static LosTable GetLosRays(int radius);
	static std::vector<int2> GetCircleSurface(const int radius);
	static void AddMissing(LosTable& losRays, const std::vector<int2>& circlePoints, const int radius);
	static void Debug(const LosTable& losRays, const std::vector<int2>& points, int radius);
};

static std::array<CLosTableHelper, ThreadPool::MAX_THREADS> losTableHelpers;



void CLosTableHelper::GenerateForLosSize(size_t losSize)
{
	// guard against insane sight distances
	assert(losSize < losTables.size());

	if (losSize == 0)
		return;

	LosTable& table = losTables[losSize];

	if (!table.empty())
		return;

	table = std::move(GetLosRays(losSize));
}



/**
 * @brief Precalcs the rays for LineOfSight raytracing.
 * In LoS we raytrace all squares in a radius if they are in view
 * or obstructed by the heightmap. To do so we cast rays with the
 * given radius to the LoS circle's surface. But cause those rays
 * have no width, it happens that squares are missed inside of the
 * circle. So these squares get their own rays with length < radius.
 *
 * Note: We only return the rays for the upper right sector, the
 * others can be constructed by mirroring.
 */
CLosTableHelper::LosTable CLosTableHelper::GetLosRays(const int radius)
{
	std::vector<int2> circlePoints = std::move(GetCircleSurface(radius));

	LosTable losRays;
	losRays.reserve(2 * circlePoints.size()); // twice cause of AddMissing()

	for (const int2& p: circlePoints) {
		losRays.emplace_back(std::move(GetRay(p.x, p.y)));
	}

	AddMissing(losRays, circlePoints, radius);

	//if (radius == 30)
	//	Debug(losRays, circlePoints, radius);
	losRays.shrink_to_fit();
	return losRays;
}


/**
 * @brief returns the surface coords of a 2d circle.
 * Note, we only return the upper right part, the other 3 are generated via mirroring.
 */
std::vector<int2> CLosTableHelper::GetCircleSurface(const int radius)
{
	// Midpoint circle algorithm
	// returns the surface points of a circle (without duplicates)
	std::vector<int2> circlePoints;
	circlePoints.reserve(2 * radius);

	MidpointCircleAlgo(radius, [&](int x, int y) {
		// the upper 1/8th
		circlePoints.emplace_back(x, y);

		// the lower 1/8th, not added when:
		// first check prevents 45deg duplicates
		// second makes sure that only (0,radius) or (radius, 0) is generated (the other one is generated by mirroring later)
		if (y != x && y != 0)
			circlePoints.emplace_back(y, x);
	});

	assert(circlePoints.size() <= 2 * radius);
	return circlePoints;
}


/**
 * @brief Makes sure all squares in the radius are checked & adds rays to missing ones.
 */
void CLosTableHelper::AddMissing(LosTable& losRays, const std::vector<int2>& circlePoints, const int radius)
{
	std::vector<char> image((radius + 1) * (radius + 1), 0);

	const auto setpixel = [&](const int2 p) { image[p.y * (radius + 1) + p.x] = true; };
	const auto getpixel = [&](const int2 p) { return image[p.y * (radius + 1) + p.x]; };

	for (auto& line: losRays) {
		for (int2& p: line) {
			setpixel(p);
		}
	}

	// start the check from 45deg bisector and go from there to 0deg & 90deg
	// advantage is we only need to iterate once this time
	// note: we iterate the list in reverse!
	for (auto it = circlePoints.rbegin(); it != circlePoints.rend(); ++it) {
		const int2& p = *it;

		for (int a = p.x; a >= 1 && a >= p.y; --a) {
			const int2 t1(a, p.y);
			const int2 t2(p.y, a);

			if (!getpixel(t1)) {
				losRays.emplace_back(std::move(GetRay(t1.x, t1.y)));

				for (int2& p_: losRays.back()) {
					setpixel(p_);
				}
			}
			// (0, radius) is a mirror of (radius, 0) so don't add it
			if (!getpixel(t2) && t2 != int2(0, radius)) {
				losRays.emplace_back(std::move(GetRay(t2.x, t2.y)));

				for (int2& p_: losRays.back()) {
					setpixel(p_);
				}
			}
		}
	}
}


/**
 * @brief returns line coords of a ray with zero width to the coords (xf,yf)
 */
CLosTableHelper::LosLine CLosTableHelper::GetRay(int xf, int yf)
{
	assert(xf >= 0);
	assert(yf >= 0);

	LosLine losline;
	if (xf > yf) {
		// horizontal line
		const float m = (float) yf / (float) xf;
		losline.reserve(xf);
		for (int x = 1; x <= xf; x++) {
			losline.emplace_back(x, Round(m*x));
		}
	} else {
		// vertical line
		const float m = (float) xf / (float) yf;
		losline.reserve(yf);
		for (int y = 1; y <= yf; y++) {
			losline.emplace_back(Round(m*y), y);
		}
	}

	assert(losline.back() == int2(xf,yf));
	assert(!losline.empty());
	return losline;
}


void CLosTableHelper::Debug(const LosTable& losRays, const std::vector<int2>& points, int radius)
{
	// only one should be included (the other one is generated via mirroring)
	assert(losRays.front().back() == int2(radius, 0));
	assert(losRays.back().back() != int2(0, radius));

	// check for duplicated/included rays
	auto losRaysCopy = losRays;
	for (const auto& ray1: losRaysCopy) {
		if (ray1.empty())
			continue;

		for (auto& ray2: losRaysCopy) {
			if (ray2.empty())
				continue;

			if (&ray1 == &ray2)
				continue;

			// check if ray2 is part of ray1
			if (std::includes(ray1.begin(), ray1.end(), ray2.begin(), ray2.end())) {
				// prepare for deletion
				ray2.clear();
			}
		}
	}
	auto jt = std::remove_if(losRaysCopy.begin(), losRaysCopy.end(), [](LosLine& ray) { return ray.empty(); });
	assert(jt == losRaysCopy.end());

	// print the rays stats
	LOG("------------------------------------");

	// draw the sphere image
	LOG("- sketch -");
	std::vector<char> image((2*radius+1) * (2*radius+1), 0);
	auto setpixel = [&](int2 p, char value = 1) {
		image[p.y * (2*radius+1) + p.x] = value;
	};
	int2 midp = int2(radius, radius);
	for (auto& line: losRays) {
		for (int2 p: line) {
			setpixel(midp + p, 127);
			setpixel(midp - p, 127);
			setpixel(midp + int2(p.y, -p.x), 127);
			setpixel(midp + int2(-p.y, p.x), 127);
		}
	}
	for (int2 p: points) {
		setpixel(midp + p, 1);
		setpixel(midp - p, 2);
		setpixel(midp + int2(p.y, -p.x), 4);
		setpixel(midp + int2(-p.y, p.x), 8);
	}
	for (int y = 0; y <= 2*radius; y++) {
		std::string l;
		for (int x = 0; x <= 2*radius; x++) {
			if (image[y*(2*radius+1) + x] == 127) {
				l += ".";
			} else {
				l += IntToString(image[y*(2*radius+1) + x]);
			}
		}
		LOG("%s", l.c_str());
	}

	// points on the sphere surface
	LOG("- surface points -");
	std::string s;
	for (int2 p: points) {
		s += "(" + IntToString(p.x) + "," + IntToString(p.y) + ") ";
	}
	LOG("%s", s.c_str());

	// rays to those points
	LOG("- los rays -");
	for (auto& line: losRays) {
		std::string s;
		for (int2 p: line) {
			s += "(" + IntToString(p.x) + "," + IntToString(p.y) + ") ";
		}
		LOG("%s", s.c_str());
	}
	LOG_L(L_DEBUG, "------------------------------------");
}











//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/// CLosMap implementation

void CLosMap::AddCircle(SLosInstance* instance, int amount)
{
#ifdef USE_UNSYNCED_HEIGHTMAP
	//only AddRaycast supports UnsyncedHeightMap updates
#endif

	MidpointCircleAlgoPerLine(instance->radius, [&](int width, int y) {
		const unsigned y_ = instance->basePos.y + y;

		if (y_ < size.y) {
			const unsigned sx = Clamp(instance->basePos.x - width,     0, size.x);
			const unsigned ex = Clamp(instance->basePos.x + width + 1, 0, size.x);

			for (unsigned x_ = sx; x_ < ex; ++x_) {
				losmap[(y_ * size.x) + x_] += amount;
			}
		}
	});
}


void CLosMap::AddRaycast(SLosInstance* instance, int amount)
{
	const auto& losSquares = instance->squares;

	if (losSquares.empty() || losSquares[0].length == SLosInstance::EMPTY_RLE.length)
		return;

#ifdef USE_UNSYNCED_HEIGHTMAP
	// inform ReadMap when squares enter LoS
	const bool visibleInstanceSquares = (instance->allyteam >= 0 && (instance->allyteam == gu->myAllyTeam || gu->spectatingFullView));
	const bool updateUnsyncedHeightMap = sendReadmapEvents && visibleInstanceSquares;

	if ((amount > 0) && updateUnsyncedHeightMap) {
		for (const SLosInstance::RLE rle: losSquares) {
			for (int idx = rle.start, len = rle.length; len > 0; --len, ++idx) {
				losmap[idx] += amount;

				// skip if this los-square did not *enter* LOS
				if (losmap[idx] != amount)
					continue;

				const int2 lm = IdxToCoord(idx, size.x);
				const int2 p1 = (lm             ) * LOS2HEIGHT;
				const int2 p2 = (lm + int2(1, 1)) * LOS2HEIGHT;
				const int2 p3 = {std::min(p2.x, mapDims.mapxm1), std::min(p2.y, mapDims.mapym1)};

				readMap->UpdateLOS(SRectangle(p1.x, p1.y,  p3.x, p3.y));
			}
		}

		return;
	}
#endif

	for (const SLosInstance::RLE rle: losSquares) {
		for (int idx = rle.start, len = rle.length; len > 0; --len, ++idx) {
			losmap[idx] += amount;
		}
	}
}


void CLosMap::PrepareRaycast(SLosInstance* instance) const
{
	if (!instance->squares.empty())
		return;

	LosAdd(instance);

	if (!instance->squares.empty())
		return;

	instance->squares.push_back(SLosInstance::EMPTY_RLE);
}


#define MAP_SQUARE(pos) ((pos).y * size.x + (pos).x)


void CLosMap::LosAdd(SLosInstance* li) const
{
	const auto MAP_SQUARE_FULLRES = [&](int2 pos) {
		float2 fpos = pos;
		fpos += 0.5f;
		fpos /= float2(size);
		int2 ipos = fpos * float2(mapDims.mapx, mapDims.mapy);
		//assert(ipos.y * mapDims.mapx + ipos.x < (mapDims.mapx * mapDims.mapy));
		return ipos.y * mapDims.mapx + ipos.x;
	};

	const SRectangle fullRect(0, 0, size.x, size.y);
	const SRectangle safeRect(li->radius, li->radius, size.x - li->radius, size.y - li->radius);

	if (fullRect.Inside(li->basePos) && li->baseHeight <= ctrHeightMap[MAP_SQUARE_FULLRES(li->basePos)])
		return;

	// add all squares within the instance's sight radius
	if (safeRect.Inside(li->basePos)) {
		// we aren't touching the map borders -> we don't need to check for the map boundaries
		UnsafeLosAdd(li);
	} else {
		// we need to check each square if it's outside of the map boundaries
		SafeLosAdd(li);
	}
}


inline static constexpr size_t ToAngleMapIdx(const int2 p, const int radius)
{
	// [-radius, +radius]^2 -> [0, +2*radius]^2 -> idx
	return (p.y + radius) * (2 * radius + 1) + (p.x + radius);
}


inline void CastLos(
	float* prvAngle,
	float* maxAngle,
	const int2& off,
	std::vector<char>& losRaySquares,
	std::vector<float>& raycastAngles,
	int losRadius,
	int threadNum
) {
	const size_t oidx = ToAngleMapIdx(off, losRadius);

	// angle to square is smaller than current max-angle, so not visible
	if (raycastAngles[oidx] < *maxAngle) {
		losRaySquares[oidx] = false;
		return;
	}

	if (raycastAngles[oidx] < *prvAngle) {
		const float invR = isqrtTableLookup(off.x * off.x + off.y * off.y, threadNum);
		const float angle = *prvAngle - LOS_BONUS_HEIGHT * invR;

		if (raycastAngles[oidx] < (*maxAngle = angle)) {
			losRaySquares[oidx] = false;
			return;
		}
	}

	*prvAngle = raycastAngles[oidx];
}


void CLosMap::AddSquaresToInstance(SLosInstance* li, const std::vector<char>& losRaySquares) const
{
	const int2 pos   = li->basePos;
	const int radius = li->radius;

	const char* ptr = &losRaySquares[0];

	auto& losSquares = li->squares;

	for (int y = -radius; y <= radius; ++y) {
		SLosInstance::RLE rle = {MAP_SQUARE(pos + int2(-radius, y)), 0};

		for (int x = -radius; x <= radius; ++x) {
			if (*(ptr++)) {
				++rle.length;
			} else {
				if (rle.length > 0)
					losSquares.push_back(rle);

				rle.start  += (rle.length + 1);
				rle.length  = 0;
			}
		}

		if (rle.length > 0)
			losSquares.push_back(rle);
	}
}


void CLosMap::UnsafeLosAdd(SLosInstance* li) const
{
	// How does it work?
	// We spawn rays (those created by CLosTableHelper::GenerateForLosSize), and cast them
	// on the heightmap. Meaning we compute the angle to the given squares and compare them
	// with the highest cached one on that ray. When the new angle is higher the square is
	// visible and gets added to the squares array.
	//
	// How does prevAng optimisation work?
	// We don't really need to save every angle as the maximum, if we're going up a mountain
	// we can just mark them true and continue until we reach the top.
	// So now, only hilltops are cached in maxAng, and they're only cached when checking
	// the square after the hilltop, since otherwise we can't know that the ascent ended.
	const int threadNum = ThreadPool::GetThreadNum();

	const int2 pos   = li->basePos;
	const int radius = li->radius;
	const float losHeight = li->baseHeight;


	CLosTableHelper& helper = losTableHelpers[threadNum];

	std::vector<char>& losRaySquares = LOSRAY_SQUARE_TABLES[threadNum];
	std::vector<float>& raycastAngles = RAYCAST_ANGLE_TABLES[threadNum];

	helper.GenerateForLosSize(radius);

	losRaySquares.clear();
	losRaySquares.resize(Square((2 * radius) + 1), false);
	raycastAngles.clear();
	raycastAngles.resize(Square((2 * radius) + 1), -1e8);


	isqrtTableExpand((radius + 1) * (radius + 1), threadNum);

	// Optimization: precalculate all angles
	// 1. Center squares are accessed much more often by more rays than those on the border.
	// 2. The heightmap is much bigger than the circle, and won't fit into the L2/L3. So
	//    when we buffer the precalc in a vector just large enough for the processed data,
	//    we reduce the amount of cache misses.
	MidpointCircleAlgoPerLine(radius, [&](int width, int y) {
		const unsigned y_ = pos.y + y;
		const unsigned sx = pos.x - width;
		const unsigned ex = pos.x + width + 1;

		const size_t oidx = ToAngleMapIdx(int2(sx - pos.x, y), radius);

		float* raycastAnglesPtr = &raycastAngles[oidx];
		char* losRaySquaresPtr = &losRaySquares[oidx];

		int idx = MAP_SQUARE(int2(sx, y_));

		for (unsigned x_ = sx; x_ < ex; ++x_) {
			const int2 off(x_ - pos.x, y);

			if (off == int2(0, 0)) {
				++idx;
				++raycastAnglesPtr;
				++losRaySquaresPtr;
				continue;
			}

			const float invR = isqrtTableLookup(off.x*off.x + off.y*off.y, threadNum);
			const float dh = std::max(0.0f, mipHeightMap[idx++]) - losHeight;

			*(raycastAnglesPtr++) = (dh + LOS_BONUS_HEIGHT) * invR;
			*(losRaySquaresPtr++) = true;
		}
	});

	// cast the rays
	losRaySquares[ToAngleMapIdx(int2(0, 0), radius)] = true;

	const size_t numRays = helper.GetLosTableSize(radius);

	for (size_t i = 0; i < numRays; ++i) {
		float maxAngles[4] = {-1e7, -1e7, -1e7, -1e7};
		float prvAngles[4] = {-1e7, -1e7, -1e7, -1e7};

		const size_t numSquares = helper.GetLosTableRaySize(radius, i);

		for (size_t n = 0; n < numSquares; n++) {
			const int2 square = helper.GetLosTableRaySquare(radius, i, n);

			CastLos(&prvAngles[0], &maxAngles[0],       square              , losRaySquares, raycastAngles, radius, threadNum);
			CastLos(&prvAngles[1], &maxAngles[1],      -square              , losRaySquares, raycastAngles, radius, threadNum);
			CastLos(&prvAngles[2], &maxAngles[2], int2( square.y, -square.x), losRaySquares, raycastAngles, radius, threadNum);
			CastLos(&prvAngles[3], &maxAngles[3], int2(-square.y,  square.x), losRaySquares, raycastAngles, radius, threadNum);
		}
	}

	// translate visible square indices to map square idx + RLE
	AddSquaresToInstance(li, losRaySquares);
}


void CLosMap::SafeLosAdd(SLosInstance* li) const
{
	// see above
	const int threadNum = ThreadPool::GetThreadNum();

	const int2 pos   = li->basePos;
	const int radius = li->radius;
	const float losHeight = li->baseHeight;


	CLosTableHelper& helper = losTableHelpers[threadNum];

	std::vector< char>& losRaySquares = LOSRAY_SQUARE_TABLES[threadNum];
	std::vector<float>& raycastAngles = RAYCAST_ANGLE_TABLES[threadNum];

	helper.GenerateForLosSize(radius);

	losRaySquares.clear();
	losRaySquares.resize(Square((2 * radius) + 1), false);
	raycastAngles.clear();
	raycastAngles.resize(Square((2 * radius) + 1), -1e8);


	const SRectangle safeRect(0, 0, size.x, size.y);

	isqrtTableExpand((radius + 1) * (radius + 1), threadNum);

	// Optimization: precalc all angles
	MidpointCircleAlgoPerLine(radius, [&](int width, int y) {
		const unsigned y_ = pos.y + y;

		if (y_ < size.y) {
			const unsigned sx = Clamp(pos.x - width,     0, size.x);
			const unsigned ex = Clamp(pos.x + width + 1, 0, size.x);

			const size_t oidx = ToAngleMapIdx(int2(sx - pos.x, y), radius);

			float* raycastAnglesPtr = &raycastAngles[oidx];
			char* losRaySquaresPtr = &losRaySquares[oidx];

			int idx = MAP_SQUARE(int2(sx, y_));

			for (unsigned x_ = sx; x_ < ex; ++x_) {
				const int2 off(x_ - pos.x, y);

				if (off == int2(0, 0)) {
					++idx;
					++raycastAnglesPtr;
					++losRaySquaresPtr;
					continue;
				}

				const float invR = isqrtTableLookup(off.x*off.x + off.y*off.y, threadNum);
				const float dh = std::max(0.0f, mipHeightMap[idx++]) - losHeight;

				*(raycastAnglesPtr++) = (dh + LOS_BONUS_HEIGHT) * invR;
				*(losRaySquaresPtr++) = true;
			}
		}
	});


	// Cast the Rays
	const size_t numRays = helper.GetLosTableSize(radius);

	if (safeRect.Inside(pos)) {
		losRaySquares[ToAngleMapIdx(int2(0, 0), radius)] = true;

		for (size_t i = 0; i < numRays; ++i) {
			float maxAngles[4] = {-1e7, -1e7, -1e7, -1e7};
			float prvAngles[4] = {-1e7, -1e7, -1e7, -1e7};

			const size_t numSquares = helper.GetLosTableRaySize(radius, i);

			for (size_t n = 0; n < numSquares; n++) {
				const int2 square = helper.GetLosTableRaySquare(radius, i, n);

				if (!safeRect.Inside(pos + square))
					break;

				CastLos(&prvAngles[0], &maxAngles[0],  square,                   losRaySquares, raycastAngles, radius, threadNum);
			}
			for (size_t n = 0; n < numSquares; n++) {
				const int2 square = helper.GetLosTableRaySquare(radius, i, n);

				if (!safeRect.Inside(pos - square))
					break;

				CastLos(&prvAngles[1], &maxAngles[1], -square,                   losRaySquares, raycastAngles, radius, threadNum);
			}
			for (size_t n = 0; n < numSquares; n++) {
				const int2 square = helper.GetLosTableRaySquare(radius, i, n);

				if (!safeRect.Inside(pos + int2(square.y, -square.x)))
					break;

				CastLos(&prvAngles[2], &maxAngles[2], int2(square.y, -square.x), losRaySquares, raycastAngles, radius, threadNum);
			}
			for (size_t n = 0; n < numSquares; n++) {
				const int2 square = helper.GetLosTableRaySquare(radius, i, n);

				if (!safeRect.Inside(pos + int2(-square.y, square.x)))
					break;

				CastLos(&prvAngles[3], &maxAngles[3], int2(-square.y, square.x), losRaySquares, raycastAngles, radius, threadNum);
			}
		}
	} else {
		// emit position outside the map
		for (size_t i = 0; i < numRays; ++i) {
			float maxAngles[4] = {-1e7, -1e7, -1e7, -1e7};
			float prvAngles[4] = {-1e7, -1e7, -1e7, -1e7};

			const size_t numSquares = helper.GetLosTableRaySize(radius, i);

			for (size_t n = 0; n < numSquares; n++) {
				const int2 square = helper.GetLosTableRaySquare(radius, i, n);

				if (safeRect.Inside(pos + square))
					CastLos(&prvAngles[0], &maxAngles[0],  square,                   losRaySquares, raycastAngles, radius, threadNum);

				if (safeRect.Inside(pos - square))
					CastLos(&prvAngles[1], &maxAngles[1], -square,                   losRaySquares, raycastAngles, radius, threadNum);

				if (safeRect.Inside(pos + int2(square.y, -square.x)))
					CastLos(&prvAngles[2], &maxAngles[2], int2(square.y, -square.x), losRaySquares, raycastAngles, radius, threadNum);

				if (safeRect.Inside(pos + int2(-square.y, square.x)))
					CastLos(&prvAngles[3], &maxAngles[3], int2(-square.y, square.x), losRaySquares, raycastAngles, radius, threadNum);
			}
		}
	}

	// translate visible square indices to map square idx + RLE
	AddSquaresToInstance(li, losRaySquares);
}