1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef IN_MAP_DRAW_MODEL_H
4 #define IN_MAP_DRAW_MODEL_H
5 
6 #include <string>
7 #include <vector>
8 #include <list>
9 
10 #include "System/float3.h"
11 #include "System/creg/creg_cond.h"
12 
13 class CPlayer;
14 class TeamController;
15 
16 /**
17  * The M in MVC for InMapDraw.
18  * @see CInMapDrawView for V
19  * @see CInMapDraw for C
20  */
21 class CInMapDrawModel
22 {
23 	CR_DECLARE_STRUCT(CInMapDrawModel)
24 	CR_DECLARE_SUB(MapDrawPrimitive)
25 	CR_DECLARE_SUB(MapPoint)
26 	CR_DECLARE_SUB(MapLine)
27 	CR_DECLARE_SUB(DrawQuad)
28 
29 public:
30 	static const size_t DRAW_QUAD_SIZE;
31 	static const float QUAD_SCALE;
32 
33 	CInMapDrawModel();
34 	~CInMapDrawModel();
35 
36 	void PostLoad();
37 
SetAllMarksVisible(bool newState)38 	void SetAllMarksVisible(bool newState) { drawAllMarks = newState; }
GetAllMarksVisible()39 	bool GetAllMarksVisible() const { return drawAllMarks; }
40 
41 	bool AddPoint(const float3& pos, const std::string& label, int playerID);
42 	bool AddLine(const float3& pos1, const float3& pos2, int playerID);
43 	void EraseNear(const float3& pos, int playerID);
44 	void EraseAll();
45 
GetNumPoints()46 	int GetNumPoints() const { return numPoints; }
GetNumLines()47 	int GetNumLines() const { return numLines; }
48 
49 
50 	struct MapDrawPrimitive {
CR_DECLAREMapDrawPrimitive51 		CR_DECLARE(MapDrawPrimitive)
52 
53 	public:
54 		MapDrawPrimitive(bool spectator, int teamID, const TeamController* teamController)
55 			: spectator(spectator)
56 			, teamID(teamID)
57 			, teamController(teamController)
58 		{}
59 
60 		bool IsLocalPlayerAllowedToSee(const CInMapDrawModel* inMapDraw) const;
61 
62 		/**
63 		 * Was the creator of this map-drawing spectator at the time it was
64 		 * created?
65 		 * @see #GetTeamController
66 		 */
IsBySpectatorMapDrawPrimitive67 		bool IsBySpectator() const { return spectator; }
68 		/**
69 		 * The team-id of the creator of this map-drawing at the time of
70 		 * creation.
71 		 * @see #GetTeamController
72 		 */
GetTeamIDMapDrawPrimitive73 		int GetTeamID() const { return teamID; }
74 		/**
75 		 * The team-controller that created this map-drawing.
76 		 */
GetTeamControllerMapDrawPrimitive77 		const TeamController* GetTeamController() const { return teamController; }
78 
79 	private:
80 		bool spectator;
81 		int teamID;
82 		const TeamController* teamController;
83 	};
84 
85 	struct MapPoint : public MapDrawPrimitive {
CR_DECLAREMapPoint86 		CR_DECLARE(MapPoint)
87 
88 	public:
89 		MapPoint(bool spectator, int teamID, const TeamController* teamController, const float3& pos, const std::string& label)
90 			: MapDrawPrimitive(spectator, teamID, teamController)
91 			, pos(pos)
92 			, label(label)
93 		{}
94 
GetPosMapPoint95 		const float3& GetPos() const { return pos; }
GetLabelMapPoint96 		const std::string& GetLabel() const { return label; }
97 
98 	private:
99 		float3 pos;
100 		std::string label;
101 	};
102 
103 	struct MapLine : public MapDrawPrimitive {
CR_DECLAREMapLine104 		CR_DECLARE(MapLine)
105 
106 	public:
107 		MapLine(bool spectator, int teamID, const TeamController* teamController, const float3& pos1, const float3& pos2)
108 			: MapDrawPrimitive(spectator, teamID, teamController)
109 			, pos1(pos1)
110 			, pos2(pos2)
111 		{}
112 
113 		/**
114 		 * The start position of the line.
115 		 */
GetPos1MapLine116 		const float3& GetPos1() const { return pos1; }
117 		/**
118 		 * The end position of the line.
119 		 */
GetPos2MapLine120 		const float3& GetPos2() const { return pos2; }
121 
122 	private:
123 		float3 pos1;
124 		float3 pos2;
125 	};
126 
127 	/**
128 	 * This is meant to be a QuadTree implementation, but in reality it is a
129 	 * cell of a grid structure.
130 	 */
131 	struct DrawQuad {
132 		CR_DECLARE_STRUCT(DrawQuad)
133 		std::list<CInMapDrawModel::MapPoint> points;
134 		std::list<CInMapDrawModel::MapLine> lines;
135 	};
136 
GetDrawQuadX()137 	int GetDrawQuadX() const { return drawQuadsX; }
GetDrawQuadY()138 	int GetDrawQuadY() const { return drawQuadsY; }
139 	const DrawQuad* GetDrawQuad(int x, int y) const;
140 
141 private:
142 	bool AllowedMsg(const CPlayer* sender) const;
143 
144 	int drawQuadsX;
145 	int drawQuadsY;
146 	std::vector<DrawQuad> drawQuads;
147 
148 	bool drawAllMarks;
149 
150 	/// total number of points
151 	int numPoints;
152 	/// total number of lines
153 	int numLines;
154 };
155 
156 extern CInMapDrawModel* inMapDrawerModel;
157 
158 #endif /* IN_MAP_DRAW_MODEL_H */
159