Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
CCmpRallyPointRenderer.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2* This file is part of 0 A.D.
3*
4* 0 A.D. is free software: you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation, either version 2 of the License, or
7* (at your option) any later version.
8*
9* 0 A.D. is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef INCLUDED_CCMPRALLYPOINTRENDERER
19#define INCLUDED_CCMPRALLYPOINTRENDERER
20
21#include "graphics/Color.h"
22#include "graphics/Overlay.h"
23#include "graphics/Texture.h"
24#include "maths/FixedVector2D.h"
25#include "maths/Vector2D.h"
30
31#include <cstddef>
32#include <string>
33#include <vector>
34
35class CFrustum;
36class ICmpFootprint;
37class ICmpPathfinder;
38class ICmpPosition;
39class SceneCollector;
40
42{
45 size_t m_EndIndex; // Inclusive
46
47 SVisibilitySegment(bool visible, size_t startIndex, size_t endIndex)
48 : m_Visible(visible), m_StartIndex(startIndex), m_EndIndex(endIndex)
49 {}
50
51 bool operator==(const SVisibilitySegment& other) const
52 {
53 return m_Visible == other.m_Visible && m_StartIndex == other.m_StartIndex && m_EndIndex == other.m_EndIndex;
54 }
55
56 bool operator!=(const SVisibilitySegment& other) const
57 {
58 return !(*this == other);
59 }
60
61 bool IsSinglePoint() const
62 {
63 return m_StartIndex == m_EndIndex;
64 }
65};
66
68{
69public:
70 static std::string GetSchema();
71 static void ClassInit(CComponentManager& componentManager);
72
73 void Init(const CParamNode& paramNode) override;
74 void Deinit() override;
75
76 void Serialize(ISerializer&) override;
77 void Deserialize(const CParamNode& paramNode, IDeserializer&) override;
78
79 void HandleMessage(const CMessage& msg, bool /*global*/) override;
80
81 /*
82 * Must be called whenever m_Displayed or the size of m_RallyPoints change,
83 * to determine whether we need to respond to render messages.
84 */
86
87 void AddPosition_wrapper(const CFixedVector2D& pos) override;
88
89 void SetPosition(const CFixedVector2D& pos) override;
90
91 void UpdatePosition(std::uint32_t rallyPointId, const CFixedVector2D& pos) override;
92
93 void SetDisplayed(bool displayed) override;
94
95 void Reset() override;
96
97 void UpdateColor() override;
98
99 /**
100 * Returns true if at least one display rally point is set; i.e., if we have a point to render our marker/line at.
101 */
102 bool IsSet() const override;
103
104 DEFAULT_COMPONENT_ALLOCATOR(RallyPointRenderer)
105
106protected:
107 /**
108 * Display position of the rally points. Note that this are merely the display positions; they not necessarily the same as the
109 * actual positions used in the simulation at any given time. In particular, we need this separate copy to support
110 * instantaneously rendering the rally point markers/lines when the user sets one in-game (instead of waiting until the
111 * network-synchronization code sets it on the RallyPoint component, which might take up to half a second).
112 */
113 std::vector<CFixedVector2D> m_RallyPoints;
114 /**
115 * Full path to the rally points as returned by the pathfinder, with some post-processing applied to reduce zig/zagging.
116 */
117 std::vector<std::vector<CVector2D> > m_Path;
118 /**
119 * Visibility segments of the rally point paths; splits the path into SoD/non-SoD segments.
120 */
121 std::vector<std::vector<SVisibilitySegment> > m_VisibilitySegments;
122 /**
123 * Should we render the rally points and the path lines? (set from JS when e.g. the unit is selected/deselected)
124 */
126 /**
127 * Smooth the path before rendering?
128 */
130 /**
131 * Entity IDs of the rally point markers.
132 */
133 std::vector<entity_id_t> m_MarkerEntityIds;
134
136 /**
137 * Last seen owner of this entity (used to keep track of ownership changes).
138 */
140 /**
141 * Template name of the rally point markers.
142 */
143 std::wstring m_MarkerTemplate;
144
145 /**
146 * Marker connector line settings (loaded from XML)
147 */
153 std::wstring m_LineTexturePath;
155 /**
156 * Pathfinder passability class to use for computing the (long-range) marker line path.
157 */
159
162
163 /**
164 * Textured overlay lines to be used for rendering the marker line. There can be multiple because we may need to render
165 * dashes for segments that are inside the SoD.
166 */
167 std::vector<std::vector<SOverlayTexturedLine> > m_TexturedOverlayLines;
168
169 /**
170 * Draw little overlay circles to indicate where the exact path points are.
171 */
173 std::vector<std::vector<SOverlayLine> > m_DebugNodeOverlays;
174
175private:
176 /**
177 * Helper function for AddPosition_wrapper and SetPosition.
178 */
179 void AddPosition(CFixedVector2D pos, bool recompute);
180
181 /**
182 * Helper function to set the line color to its owner's color.
183 */
184 void UpdateLineColor();
185
186 /**
187 * Repositions the rally point markers; moves them outside of the world (ie. hides them), or positions them at the currently
188 * set rally points. Also updates the actor's variation according to the entity's current owning player's civilization.
189 *
190 * Should be called whenever either the position of a rally point changes (including whether it is set or not), or the display
191 * flag changes, or the ownership of the entity changes.
192 */
193 void UpdateMarkers();
194
195 /**
196 * @brief Creates a rally point marker entity for the specified index and owner.
197 *
198 * @param index The index in the rally point list for which to create a marker.
199 * @param ownerId The ID of the player who currently owns the entity.
200 */
201 void CreateMarkerEntity(size_t index, player_id_t ownerId);
202
203 /**
204 * Recomputes all the full paths from this entity to the rally point and from the rally point to the next, and does all the necessary
205 * post-processing to make them prettier.
206 *
207 * Should be called whenever all rally points' position changes.
208 */
210
211 /**
212 * Recomputes the full path for m_Path[ @p index], and does all the necessary post-processing to make it prettier.
213 *
214 * Should be called whenever either the starting position or the rally point's position changes.
215 */
216 void RecomputeRallyPointPath_wrapper(size_t index);
217
218 /**
219 * Recomputes the full path from this entity/the previous rally point to the next rally point, and does all the necessary
220 * post-processing to make it prettier. This doesn't check if we have a valid position or if a rally point is set.
221 *
222 * You shouldn't need to call this method directly.
223 */
224 void RecomputeRallyPointPath(size_t index, CmpPtr<ICmpPosition>& cmpPosition, CmpPtr<ICmpFootprint>& cmpFootprint, CmpPtr<ICmpPathfinder> cmpPathfinder);
225
226 /**
227 * Checks for changes to the SoD to the previously saved state, and reconstructs the visibility segments and overlay lines to
228 * match if necessary. Does nothing if the rally point lines are not currently set to be displayed, or if no rally point is set.
229 */
230 void UpdateOverlayLines();
231
232 /**
233 * Sets up all overlay lines for rendering according to the current full path and visibility segments. Splits the line into solid
234 * and dashed pieces (for the SoD). Should be called whenever the SoD has changed. If no full path is currently set, this method
235 * does nothing.
236 */
238
239 /**
240 * Sets up the overlay lines for rendering according to the full path and visibility segments at @p index. Splits the line into
241 * solid and dashed pieces (for the SoD). Should be called whenever the SoD of the path at @p index has changed.
242 */
243 void ConstructOverlayLines(size_t index);
244
245 /**
246 * Get the point on the footprint edge that's as close from "start" as possible.
247 */
248 void GetClosestsEdgePointFrom(CFixedVector2D& result, CFixedVector2D& start, CmpPtr<ICmpPosition> cmpPosition, CmpPtr<ICmpFootprint> cmpFootprint) const;
249
250 /**
251 * Returns a list of indices of waypoints in the current path (m_Path[index]) where the LOS visibility changes, ordered from
252 * building/previous rally point to rally point. Used to construct the overlay line segments and track changes to the SoD.
253 */
254 void GetVisibilitySegments(std::vector<SVisibilitySegment>& out, size_t index) const;
255
256 /**
257 * Simplifies the path by removing waypoints that lie between two points that are visible from one another. This is primarily
258 * intended to reduce some unnecessary curviness of the path; the pathfinder returns a mathematically (near-)optimal path, which
259 * will happily curve and bend to reduce costs. Visually, it doesn't make sense for a rally point path to curve and bend when it
260 * could just as well have gone in a straight line; that's why we have this, to make it look more natural.
261 *
262 * @p coords array of path coordinates to simplify
263 * @p maxSegmentLinks if non-zero, indicates the maximum amount of consecutive node-to-node links that can be joined into a
264 * single link. If this value is set to e.g. 1, then no reductions will be performed. A value of 3 means that
265 * at most 3 consecutive node links will be joined into a single link.
266 * @p floating whether to consider nodes who are under the water level as floating on top of the water
267 */
268 void ReduceSegmentsByVisibility(std::vector<CVector2D>& coords, unsigned maxSegmentLinks = 0, bool floating = true) const;
269
270 /**
271 * Helper function to GetVisibilitySegments, factored out for testing. Merges single-point segments with its neighbouring
272 * segments. You should not have to call this method directly.
273 */
274 static void MergeVisibilitySegments(std::vector<SVisibilitySegment>& segments);
275
276 void RenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling);
277};
278
279REGISTER_COMPONENT_TYPE(RallyPointRenderer)
280
281#endif // INCLUDED_CCMPRALLYPOINTRENDERER
#define DEFAULT_COMPONENT_ALLOCATOR(cname)
Definition: Component.h:39
#define REGISTER_COMPONENT_TYPE(cname)
Definition: Component.h:32
int32_t player_id_t
valid player IDs are non-negative (see ICmpOwnership)
Definition: Player.h:26
Definition: CCmpRallyPointRenderer.h:68
bool m_SmoothPath
Smooth the path before rendering?
Definition: CCmpRallyPointRenderer.h:129
std::string m_LinePassabilityClass
Pathfinder passability class to use for computing the (long-range) marker line path.
Definition: CCmpRallyPointRenderer.h:158
void RecomputeRallyPointPath_wrapper(size_t index)
Recomputes the full path for m_Path[ index], and does all the necessary post-processing to make it pr...
Definition: CCmpRallyPointRenderer.cpp:370
player_id_t m_LastOwner
Last seen owner of this entity (used to keep track of ownership changes).
Definition: CCmpRallyPointRenderer.h:139
void ReduceSegmentsByVisibility(std::vector< CVector2D > &coords, unsigned maxSegmentLinks=0, bool floating=true) const
Simplifies the path by removing waypoints that lie between two points that are visible from one anoth...
Definition: CCmpRallyPointRenderer.cpp:745
std::vector< std::vector< CVector2D > > m_Path
Full path to the rally points as returned by the pathfinder, with some post-processing applied to red...
Definition: CCmpRallyPointRenderer.h:117
float m_LineThickness
Marker connector line settings (loaded from XML)
Definition: CCmpRallyPointRenderer.h:148
std::vector< std::vector< SOverlayTexturedLine > > m_TexturedOverlayLines
Textured overlay lines to be used for rendering the marker line.
Definition: CCmpRallyPointRenderer.h:167
void UpdateMarkers()
Repositions the rally point markers; moves them outside of the world (ie.
Definition: CCmpRallyPointRenderer.cpp:284
void GetClosestsEdgePointFrom(CFixedVector2D &result, CFixedVector2D &start, CmpPtr< ICmpPosition > cmpPosition, CmpPtr< ICmpFootprint > cmpFootprint) const
Get the point on the footprint edge that's as close from "start" as possible.
Definition: CCmpRallyPointRenderer.cpp:696
void RenderSubmit(SceneCollector &collector, const CFrustum &frustum, bool culling)
Definition: CCmpRallyPointRenderer.cpp:960
std::vector< std::vector< SOverlayLine > > m_DebugNodeOverlays
Definition: CCmpRallyPointRenderer.h:173
void AddPosition_wrapper(const CFixedVector2D &pos) override
Add another position at which a marker should be displayed, connected to the previous one.
Definition: CCmpRallyPointRenderer.cpp:981
void UpdateOverlayLines()
Checks for changes to the SoD to the previously saved state, and reconstructs the visibility segments...
Definition: CCmpRallyPointRenderer.cpp:657
void Deserialize(const CParamNode &paramNode, IDeserializer &) override
Definition: CCmpRallyPointRenderer.cpp:181
void HandleMessage(const CMessage &msg, bool) override
Definition: CCmpRallyPointRenderer.cpp:187
void ConstructAllOverlayLines()
Sets up all overlay lines for rendering according to the current full path and visibility segments.
Definition: CCmpRallyPointRenderer.cpp:504
CColor m_LineColor
Definition: CCmpRallyPointRenderer.h:149
void SetPosition(const CFixedVector2D &pos) override
Sets the position at which the rally point marker should be displayed.
Definition: CCmpRallyPointRenderer.cpp:986
void UpdateLineColor()
Helper function to set the line color to its owner's color.
Definition: CCmpRallyPointRenderer.cpp:322
bool IsSet() const override
Returns true if at least one display rally point is set; i.e., if we have a point to render our marke...
Definition: CCmpRallyPointRenderer.cpp:1073
void CreateMarkerEntity(size_t index, player_id_t ownerId)
Creates a rally point marker entity for the specified index and owner.
Definition: CCmpRallyPointRenderer.cpp:254
void Serialize(ISerializer &) override
Definition: CCmpRallyPointRenderer.cpp:176
static void ClassInit(CComponentManager &componentManager)
Definition: CCmpRallyPointRenderer.cpp:163
void Reset() override
Reset the positions of this rally point marker.
Definition: CCmpRallyPointRenderer.cpp:1036
std::vector< CFixedVector2D > m_RallyPoints
Display position of the rally points.
Definition: CCmpRallyPointRenderer.h:113
bool m_Displayed
Should we render the rally points and the path lines? (set from JS when e.g.
Definition: CCmpRallyPointRenderer.h:125
static void MergeVisibilitySegments(std::vector< SVisibilitySegment > &segments)
Helper function to GetVisibilitySegments, factored out for testing.
Definition: CCmpRallyPointRenderer.cpp:891
void UpdatePosition(std::uint32_t rallyPointId, const CFixedVector2D &pos) override
Updates the position of one given rally point marker.
Definition: CCmpRallyPointRenderer.cpp:996
bool m_EnableDebugNodeOverlay
Draw little overlay circles to indicate where the exact path points are.
Definition: CCmpRallyPointRenderer.h:172
void UpdateColor() override
Updates the line color.
Definition: CCmpRallyPointRenderer.cpp:1054
void Init(const CParamNode &paramNode) override
Definition: CCmpRallyPointRenderer.cpp:114
void Deinit() override
Definition: CCmpRallyPointRenderer.cpp:172
std::vector< std::vector< SVisibilitySegment > > m_VisibilitySegments
Visibility segments of the rally point paths; splits the path into SoD/non-SoD segments.
Definition: CCmpRallyPointRenderer.h:121
void SetDisplayed(bool displayed) override
Sets whether the rally point marker and line should be displayed.
Definition: CCmpRallyPointRenderer.cpp:1011
std::vector< entity_id_t > m_MarkerEntityIds
Entity IDs of the rally point markers.
Definition: CCmpRallyPointRenderer.h:133
std::wstring m_LineTextureMaskPath
Definition: CCmpRallyPointRenderer.h:154
void UpdateMessageSubscriptions()
Definition: CCmpRallyPointRenderer.cpp:249
CTexturePtr m_TextureMask
Definition: CCmpRallyPointRenderer.h:161
SOverlayTexturedLine::LineCapType m_LineStartCapType
Definition: CCmpRallyPointRenderer.h:151
void RecomputeAllRallyPointPaths()
Recomputes all the full paths from this entity to the rally point and from the rally point to the nex...
Definition: CCmpRallyPointRenderer.cpp:343
std::wstring m_MarkerTemplate
Template name of the rally point markers.
Definition: CCmpRallyPointRenderer.h:143
void ConstructOverlayLines(size_t index)
Sets up the overlay lines for rendering according to the full path and visibility segments at index.
Definition: CCmpRallyPointRenderer.cpp:512
size_t m_LastMarkerCount
Definition: CCmpRallyPointRenderer.h:135
std::wstring m_LineTexturePath
Definition: CCmpRallyPointRenderer.h:153
CColor m_LineDashColor
Definition: CCmpRallyPointRenderer.h:150
CTexturePtr m_Texture
Definition: CCmpRallyPointRenderer.h:160
void GetVisibilitySegments(std::vector< SVisibilitySegment > &out, size_t index) const
Returns a list of indices of waypoints in the current path (m_Path[index]) where the LOS visibility c...
Definition: CCmpRallyPointRenderer.cpp:844
static std::string GetSchema()
Definition: CCmpRallyPointRenderer.cpp:58
void RecomputeRallyPointPath(size_t index, CmpPtr< ICmpPosition > &cmpPosition, CmpPtr< ICmpFootprint > &cmpFootprint, CmpPtr< ICmpPathfinder > cmpPathfinder)
Recomputes the full path from this entity/the previous rally point to the next rally point,...
Definition: CCmpRallyPointRenderer.cpp:387
void AddPosition(CFixedVector2D pos, bool recompute)
Helper function for AddPosition_wrapper and SetPosition.
Definition: CCmpRallyPointRenderer.cpp:1060
SOverlayTexturedLine::LineCapType m_LineEndCapType
Definition: CCmpRallyPointRenderer.h:152
Definition: ComponentManager.h:46
Definition: FixedVector2D.h:25
Definition: Frustum.h:40
Definition: Message.h:31
An entity initialisation parameter node.
Definition: ParamNode.h:153
A simplified syntax for accessing entity components.
Definition: CmpPtr.h:58
Footprints - an approximation of the entity's shape, used for collision detection and for rendering s...
Definition: ICmpFootprint.h:37
Pathfinder algorithms.
Definition: ICmpPathfinder.h:63
Represents an entity's position in the world (plus its orientation).
Definition: ICmpPosition.h:65
Rally Point.
Definition: ICmpRallyPointRenderer.h:32
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:40
Serialization interface; see serialization overview.
Definition: ISerializer.h:126
This interface accepts renderable objects.
Definition: Scene.h:90
std::shared_ptr< CTexture > CTexturePtr
Definition: Texture.h:25
Definition: Color.h:46
LineCapType
Definition: Overlay.h:71
Definition: CCmpRallyPointRenderer.h:42
SVisibilitySegment(bool visible, size_t startIndex, size_t endIndex)
Definition: CCmpRallyPointRenderer.h:47
size_t m_StartIndex
Definition: CCmpRallyPointRenderer.h:44
bool m_Visible
Definition: CCmpRallyPointRenderer.h:43
bool operator!=(const SVisibilitySegment &other) const
Definition: CCmpRallyPointRenderer.h:56
bool operator==(const SVisibilitySegment &other) const
Definition: CCmpRallyPointRenderer.h:51
size_t m_EndIndex
Definition: CCmpRallyPointRenderer.h:45
bool IsSinglePoint() const
Definition: CCmpRallyPointRenderer.h:61
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407