1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 /* based on original los code in LosHandler.{cpp,h} and RadarHandler.{cpp,h} */
4 
5 #ifndef LOS_MAP_H
6 #define LOS_MAP_H
7 
8 #include <vector>
9 #include "System/type2.h"
10 
11 /// map containing counts of how many units have Line Of Sight (LOS) to each square
12 class CLosMap
13 {
CR_DECLARE_STRUCT(CLosMap)14 	CR_DECLARE_STRUCT(CLosMap)
15 
16 public:
17 	CLosMap() : size(0, 0), sendReadmapEvents(false) {}
18 
19 	void SetSize(int2 size, bool sendReadmapEvents);
SetSize(int w,int h,bool sendReadmapEvents)20 	void SetSize(int w, int h, bool sendReadmapEvents) { SetSize(int2(w, h), sendReadmapEvents); }
21 
22 	/// circular area, for airLosMap, circular radar maps, jammer maps, ...
23 	void AddMapArea(int2 pos, int allyteam, int radius, int amount);
24 
25 	/// arbitrary area, for losMap, non-circular radar maps, ...
26 	void AddMapSquares(const std::vector<int>& squares, int allyteam, int amount);
27 
28 	int operator[] (int square) const { return map[square]; }
29 
At(int x,int y)30 	int At(int x, int y) const {
31 		x = std::max(0, std::min(size.x - 1, x));
32 		y = std::max(0, std::min(size.y - 1, y));
33 		return map[y * size.x + x];
34 	}
35 
36 	// FIXME temp fix for CBaseGroundDrawer and AI interface, which need raw data
front()37 	unsigned short& front() { return map.front(); }
38 
39 protected:
40 	int2 size;
41 	std::vector<unsigned short> map;
42 	bool sendReadmapEvents;
43 };
44 
45 
46 /// algorithm to calculate LOS squares using raycasting, taking terrain into account
47 class CLosAlgorithm
48 {
CR_DECLARE_STRUCT(CLosAlgorithm)49 	CR_DECLARE_STRUCT(CLosAlgorithm)
50 
51 public:
52 	CLosAlgorithm(int2 size, float minMaxAng, float extraHeight, const float* heightmap)
53 	: size(size), minMaxAng(minMaxAng), extraHeight(extraHeight), heightmap(heightmap) {}
54 
55 	void LosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares);
56 
57 private:
58 	void UnsafeLosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares);
59 	void SafeLosAdd(int2 pos, int radius, float baseHeight, std::vector<int>& squares);
60 
61 	int2 size;
62 	float minMaxAng;
63 	float extraHeight;
64 	const float* const heightmap;
65 };
66 
67 #endif // LOS_MAP_H
68