| 1 | /* Copyright (C) 2012 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 | #include "precompiled.h" |
| 19 | #include "ICmpWayPointRenderer.h" |
| 20 | |
| 21 | #include "simulation2/MessageTypes.h" |
| 22 | #include "simulation2/components/ICmpFootprint.h" |
| 23 | #include "simulation2/components/ICmpObstructionManager.h" |
| 24 | #include "simulation2/components/ICmpOwnership.h" |
| 25 | #include "simulation2/components/ICmpPathfinder.h" |
| 26 | #include "simulation2/components/ICmpPlayer.h" |
| 27 | #include "simulation2/components/ICmpPlayerManager.h" |
| 28 | #include "simulation2/components/ICmpPosition.h" |
| 29 | #include "simulation2/components/ICmpRangeManager.h" |
| 30 | #include "simulation2/components/ICmpTerrain.h" |
| 31 | #include "simulation2/components/ICmpVisual.h" |
| 32 | #include "simulation2/components/ICmpWaterManager.h" |
| 33 | #include "simulation2/helpers/Render.h" |
| 34 | #include "simulation2/helpers/Geometry.h" |
| 35 | #include "simulation2/system/Component.h" |
| 36 | |
| 37 | #include "ps/CLogger.h" |
| 38 | #include "graphics/Overlay.h" |
| 39 | #include "graphics/TextureManager.h" |
| 40 | #include "renderer/Renderer.h" |
| 41 | |
| 42 | // TODO refactor |
| 43 | // Maybe create a common ancestor for this and RallyPointRenderer FlagPointRenderer |
| 44 | struct SVisibilitySegment |
| 45 | { |
| 46 | bool m_Visible; |
| 47 | size_t m_StartIndex; |
| 48 | size_t m_EndIndex; // inclusive |
| 49 | |
| 50 | SVisibilitySegment(bool visible, size_t startIndex, size_t endIndex) |
| 51 | : m_Visible(visible), m_StartIndex(startIndex), m_EndIndex(endIndex) |
| 52 | {} |
| 53 | |
| 54 | bool operator==(const SVisibilitySegment& other) const |
| 55 | { |
| 56 | return (m_Visible == other.m_Visible && m_StartIndex == other.m_StartIndex && m_EndIndex == other.m_EndIndex); |
| 57 | } |
| 58 | |
| 59 | bool operator!=(const SVisibilitySegment& other) const |
| 60 | { |
| 61 | return !(*this == other); |
| 62 | } |
| 63 | |
| 64 | bool IsSinglePoint() |
| 65 | { |
| 66 | return (m_StartIndex == m_EndIndex); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | class CCmpWayPointRenderer : public ICmpWayPointRenderer |
| 71 | { |
| 72 | // import some types for less verbosity |
| 73 | typedef ICmpRangeManager::CLosQuerier CLosQuerier; |
| 74 | typedef SOverlayTexturedLine::LineCapType LineCapType; |
| 75 | |
| 76 | public: |
| 77 | static void ClassInit(CComponentManager& componentManager) |
| 78 | { |
| 79 | componentManager.SubscribeToMessageType(MT_RenderSubmit); |
| 80 | componentManager.SubscribeToMessageType(MT_OwnershipChanged); |
| 81 | componentManager.SubscribeToMessageType(MT_TurnStart); |
| 82 | componentManager.SubscribeToMessageType(MT_Destroy); |
| 83 | componentManager.SubscribeToMessageType(MT_PositionChanged); |
| 84 | } |
| 85 | |
| 86 | DEFAULT_COMPONENT_ALLOCATOR(WayPointRenderer) |
| 87 | |
| 88 | protected: |
| 89 | |
| 90 | /// Display position of the way points. Note that this are merely the display positions; they not necessarily the same as the |
| 91 | /// actual positions used in the simulation at any given time. In particular, we need this separate copy to support |
| 92 | /// instantaneously rendering the way point markers/lines when the user sets one in-game (instead of waiting until the |
| 93 | /// network-synchronization code sets it on the WayPoint component, which might take up to half a second). |
| 94 | std::deque<CFixedVector2D> m_WayPoints; |
| 95 | // TODO are we using the pathfinder? / should we be using it that much? TODO wait for the pathfinder rewrite and use hopefully exported data |
| 96 | // from that to make a curved path that uses the same data as the moving unit. |
| 97 | /// Full path to the way points as returned by the pathfinder, with some post-processing applied to reduce zig/zagging. |
| 98 | std::deque<std::deque<CVector2D> > m_Path; |
| 99 | |
| 100 | /// Visibility segments of the way point paths; splits the path into SoD/non-SoD segments. |
| 101 | std::deque<std::deque<SVisibilitySegment> > m_VisibilitySegments; |
| 102 | |
| 103 | bool m_Displayed; ///< Should we render the way points and the path lines? (set from JS when e.g. the unit is selected/deselected) |
| 104 | |
| 105 | std::deque<entity_id_t> m_MarkerEntityIds; ///< Entity IDs of the way point markers. |
| 106 | size_t m_LastMarkerCount; |
| 107 | player_id_t m_LastOwner; ///< Last seen owner of this entity (used to keep track of ownership changes). |
| 108 | std::wstring m_MarkerTemplate; ///< Template name of the way point markers. |
| 109 | |
| 110 | /// Marker connector line settings (loaded from XML) |
| 111 | float m_LineThickness; |
| 112 | CColor m_LineColor; |
| 113 | CColor m_LineDashColor; |
| 114 | LineCapType m_LineStartCapType; |
| 115 | LineCapType m_LineEndCapType; |
| 116 | std::wstring m_LineTexturePath; |
| 117 | std::wstring m_LineTextureMaskPath; |
| 118 | std::string m_LinePassabilityClass; ///< Pathfinder passability class to use for computing the (long-range) marker line path. |
| 119 | std::string m_LineCostClass; ///< Pathfinder cost class to use for computing the (long-range) marker line path. |
| 120 | |
| 121 | CTexturePtr m_Texture; |
| 122 | CTexturePtr m_TextureMask; |
| 123 | |
| 124 | /// Textured overlay lines to be used for rendering the marker line. There can be multiple because we may need to render |
| 125 | /// dashes for segments that are inside the SoD. |
| 126 | std::list<std::list<SOverlayTexturedLine> > m_TexturedOverlayLines; |
| 127 | |
| 128 | public: |
| 129 | |
| 130 | static std::string GetSchema() |
| 131 | { |
| 132 | return |
| 133 | "<a:help>Displays a way point marker where units will go to when tasked to move</a:help>" |
| 134 | "<a:example>" |
| 135 | "<MarkerTemplate>special/WayPoint</MarkerTemplate>" |
| 136 | "<LineThickness>0.75</LineThickness>" |
| 137 | "<LineStartCap>round</LineStartCap>" |
| 138 | "<LineEndCap>square</LineEndCap>" |
| 139 | "<LineColour r='20' g='128' b='240'></LineColour>" |
| 140 | "<LineDashColour r='158' g='11' b='15'></LineDashColour>" |
| 141 | "<LineCostClass>default</LineCostClass>" |
| 142 | "<LinePassabilityClass>default</LinePassabilityClass>" |
| 143 | "</a:example>" |
| 144 | "<element name='MarkerTemplate' a:help='Template name for the way point marker entity (typically a waypoint flag actor)'>" |
| 145 | "<text/>" |
| 146 | "</element>" |
| 147 | "<element name='LineTexture' a:help='Texture file to use for the way point line'>" |
| 148 | "<text />" |
| 149 | "</element>" |
| 150 | "<element name='LineTextureMask' a:help='Texture mask to indicate where overlay colors are to be applied (see LineColour and LineDashColour)'>" |
| 151 | "<text />" |
| 152 | "</element>" |
| 153 | "<element name='LineThickness' a:help='Thickness of the marker line connecting the entity to the way point marker'>" |
| 154 | "<data type='decimal'/>" |
| 155 | "</element>" |
| 156 | "<element name='LineColour'>" |
| 157 | "<attribute name='r'>" |
| 158 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 159 | "</attribute>" |
| 160 | "<attribute name='g'>" |
| 161 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 162 | "</attribute>" |
| 163 | "<attribute name='b'>" |
| 164 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 165 | "</attribute>" |
| 166 | "</element>" |
| 167 | "<element name='LineDashColour'>" |
| 168 | "<attribute name='r'>" |
| 169 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 170 | "</attribute>" |
| 171 | "<attribute name='g'>" |
| 172 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 173 | "</attribute>" |
| 174 | "<attribute name='b'>" |
| 175 | "<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>" |
| 176 | "</attribute>" |
| 177 | "</element>" |
| 178 | "<element name='LineStartCap'>" |
| 179 | "<choice>" |
| 180 | "<value a:help='Abrupt line ending; line endings are not closed'>flat</value>" |
| 181 | "<value a:help='Semi-circular line end cap'>round</value>" |
| 182 | "<value a:help='Sharp, pointy line end cap'>sharp</value>" |
| 183 | "<value a:help='Square line end cap'>square</value>" |
| 184 | "</choice>" |
| 185 | "</element>" |
| 186 | "<element name='LineEndCap'>" |
| 187 | "<choice>" |
| 188 | "<value a:help='Abrupt line ending; line endings are not closed'>flat</value>" |
| 189 | "<value a:help='Semi-circular line end cap'>round</value>" |
| 190 | "<value a:help='Sharp, pointy line end cap'>sharp</value>" |
| 191 | "<value a:help='Square line end cap'>square</value>" |
| 192 | "</choice>" |
| 193 | "</element>" |
| 194 | "<element name='LinePassabilityClass' a:help='The pathfinder passability class to use for computing the way point marker line path'>" |
| 195 | "<text />" |
| 196 | "</element>" |
| 197 | "<element name='LineCostClass' a:help='The pathfinder cost class to use for computing the way point marker line path'>" |
| 198 | "<text />" |
| 199 | "</element>"; |
| 200 | } |
| 201 | |
| 202 | virtual void Init(const CParamNode& paramNode); |
| 203 | |
| 204 | virtual void Deinit() |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | virtual void Serialize(ISerializer& UNUSED(serialize)) |
| 209 | { |
| 210 | // do NOT serialize anything; this is a rendering-only component, it does not and should not affect simulation state |
| 211 | } |
| 212 | |
| 213 | virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) |
| 214 | { |
| 215 | Init(paramNode); |
| 216 | } |
| 217 | |
| 218 | virtual void HandleMessage(const CMessage& msg, bool UNUSED(global)) |
| 219 | { |
| 220 | switch (msg.GetType()) |
| 221 | { |
| 222 | case MT_RenderSubmit: |
| 223 | { |
| 224 | if (m_Displayed && IsSet()) |
| 225 | { |
| 226 | const CMessageRenderSubmit& msgData = static_cast<const CMessageRenderSubmit&> (msg); |
| 227 | RenderSubmit(msgData.collector); |
| 228 | } |
| 229 | } |
| 230 | break; |
| 231 | case MT_OwnershipChanged: |
| 232 | { |
| 233 | UpdateMarkers(); // update marker variation to new player's civilization |
| 234 | } |
| 235 | break; |
| 236 | case MT_TurnStart: |
| 237 | { |
| 238 | UpdateOverlayLines(); // check for changes to the SoD and update the overlay lines accordingly |
| 239 | } |
| 240 | break; |
| 241 | case MT_Destroy: |
| 242 | { |
| 243 | for (std::deque<entity_id_t>::iterator it = m_MarkerEntityIds.begin(); it < m_MarkerEntityIds.end(); ++it) |
| 244 | { |
| 245 | if (*it != INVALID_ENTITY) |
| 246 | { |
| 247 | GetSimContext().GetComponentManager().DestroyComponentsSoon(*it); |
| 248 | *it = INVALID_ENTITY; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | case MT_PositionChanged: |
| 254 | { |
| 255 | RecomputeWayPointPath_wrapper(0); |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | virtual void AddPosition_wrapper(CFixedVector2D pos) |
| 262 | { |
| 263 | AddPosition(pos, false); |
| 264 | } |
| 265 | |
| 266 | // Rename to Unshift??? |
| 267 | virtual void AddPositionFront(CFixedVector2D pos) |
| 268 | { |
| 269 | m_WayPoints.push_front(pos); |
| 270 | UpdateMarkers(); |
| 271 | |
| 272 | // TODO don't recompute everything (maybe just shift everything one back? and recompute first only |
| 273 | RecomputeAllWayPointPaths(); |
| 274 | } |
| 275 | |
| 276 | virtual void SetPosition(CFixedVector2D pos) |
| 277 | { |
| 278 | if (!(m_WayPoints.size() == 1 && m_WayPoints.front() == pos)) |
| 279 | { |
| 280 | m_WayPoints.clear(); |
| 281 | AddPosition(pos, true); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | virtual void SetDisplayed(bool displayed) |
| 286 | { |
| 287 | if (m_Displayed != displayed) |
| 288 | { |
| 289 | m_Displayed = displayed; |
| 290 | |
| 291 | // move the markers out of oblivion and back into the real world, or vice-versa |
| 292 | UpdateMarkers(); |
| 293 | |
| 294 | // Check for changes to the SoD and update the overlay lines accordingly. We need to do this here because this method |
| 295 | // only takes effect when the display flag is active; we need to pick up changes to the SoD that might have occurred |
| 296 | // while this way point was not being displayed. |
| 297 | UpdateOverlayLines(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | virtual void Shift() |
| 302 | { |
| 303 | LOGERROR(L"m_WayPoints %d \tm_Path %d \tm_VisibilitySegments %d \tm_TexturedOverlayLines %d\n",m_WayPoints.size(),m_Path.size(),m_VisibilitySegments.size(),m_TexturedOverlayLines.size()); |
| 304 | |
| 305 | // Still not sure why size is sometimes <= 0 |
| 306 | if (m_WayPoints.size() > 0) |
| 307 | { |
| 308 | m_WayPoints.pop_front(); |
| 309 | |
| 310 | m_Path.pop_front(); |
| 311 | m_VisibilitySegments.pop_front(); |
| 312 | |
| 313 | m_TexturedOverlayLines.pop_front(); |
| 314 | |
| 315 | // TODO should we skip this as this is already done on MT_PositionChanged? |
| 316 | RecomputeWayPointPath_wrapper(0); |
| 317 | |
| 318 | UpdateMarkers(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | private: |
| 323 | |
| 324 | /** |
| 325 | * Helper function for AddPosition_wrapper and SetPosition. |
| 326 | */ |
| 327 | void AddPosition(CFixedVector2D pos, bool recompute) |
| 328 | { |
| 329 | m_WayPoints.push_back(pos); |
| 330 | UpdateMarkers(); |
| 331 | |
| 332 | if (recompute) |
| 333 | RecomputeAllWayPointPaths(); |
| 334 | else |
| 335 | RecomputeWayPointPath_wrapper(m_WayPoints.size()-1); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Returns true iff at least one display way point is set; i.e., if we have a point to render our marker/line at. |
| 340 | */ |
| 341 | bool IsSet() |
| 342 | { |
| 343 | return !m_WayPoints.empty(); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Repositions the way point markers; moves them outside of the world (ie. hides them), or positions them at the currently |
| 348 | * set way points. Also updates the actor's variation according to the entity's current owning player's civilization. |
| 349 | * |
| 350 | * Should be called whenever either the position of a way point changes (including whether it is set or not), or the display |
| 351 | * flag changes, or the ownership of the entity changes. |
| 352 | */ |
| 353 | void UpdateMarkers(); |
| 354 | |
| 355 | /** |
| 356 | * Recomputes all the full paths from this entity to the way point and from the way point to the next, and does all the necessary |
| 357 | * post-processing to make them prettier. |
| 358 | * |
| 359 | * Should be called whenever all way points' position changes. |
| 360 | */ |
| 361 | void RecomputeAllWayPointPaths(); |
| 362 | |
| 363 | /** |
| 364 | * Recomputes the full path for m_Path[ @p index], and does all the necessary post-processing to make it prettier. |
| 365 | * |
| 366 | * Should be called whenever either the starting position or the way point's position changes. |
| 367 | */ |
| 368 | void RecomputeWayPointPath_wrapper(size_t index); |
| 369 | |
| 370 | /** |
| 371 | * Recomputes the full path from this entity/the previous way point to the next way point, and does all the necessary |
| 372 | * post-processing to make it prettier. This doesn't check if we have a valid position or if a way point is set. |
| 373 | * |
| 374 | * You shouldn't need to call this method directly. |
| 375 | */ |
| 376 | void RecomputeWayPointPath(size_t index, CmpPtr<ICmpPosition>& cmpPosition, CmpPtr<ICmpFootprint>& cmpFootprint, CmpPtr<ICmpPathfinder> cmpPathfinder); |
| 377 | |
| 378 | /** |
| 379 | * Checks for changes to the SoD to the previously saved state, and reconstructs the visibility segments and overlay lines to |
| 380 | * match if necessary. Does nothing if the way point lines are not currently set to be displayed, or if no way point is set. |
| 381 | */ |
| 382 | void UpdateOverlayLines(); |
| 383 | |
| 384 | /** |
| 385 | * Sets up all overlay lines for rendering according to the current full path and visibility segments. Splits the line into solid |
| 386 | * and dashed pieces (for the SoD). Should be called whenever the SoD has changed. If no full path is currently set, this method |
| 387 | * does nothing. |
| 388 | */ |
| 389 | void ConstructAllOverlayLines(); |
| 390 | |
| 391 | /** |
| 392 | * Sets up the overlay lines for rendering according to the full path and visibility segments at @p index. Splits the line into |
| 393 | * solid and dashed pieces (for the SoD). Should be called whenever the SoD of the path at @p index has changed. |
| 394 | */ |
| 395 | void ConstructOverlayLines(size_t index); |
| 396 | |
| 397 | /** |
| 398 | * Returns a list of indices of waypoints in the current path (m_Path[index]) where the LOS visibility changes, ordered from |
| 399 | * building/previous way point to way point. Used to construct the overlay line segments and track changes to the SoD. |
| 400 | */ |
| 401 | void GetVisibilitySegments(std::deque<SVisibilitySegment>& out, size_t index); |
| 402 | |
| 403 | /** |
| 404 | * Simplifies the path by removing waypoints that lie between two points that are visible from one another. This is primarily |
| 405 | * intended to reduce some unnecessary curviness of the path; the pathfinder returns a mathematically (near-)optimal path, which |
| 406 | * will happily curve and bend to reduce costs. Visually, it doesn't make sense for a way point path to curve and bend when it |
| 407 | * could just as well have gone in a straight line; that's why we have this, to make it look more natural. |
| 408 | * |
| 409 | * @p coords array of path coordinates to simplify |
| 410 | * @p maxSegmentLinks if non-zero, indicates the maximum amount of consecutive node-to-node links that can be joined into a |
| 411 | * single link. If this value is set to e.g. 1, then no reductions will be performed. A value of 3 means that |
| 412 | * at most 3 consecutive node links will be joined into a single link. |
| 413 | * @p floating whether to consider nodes who are under the water level as floating on top of the water |
| 414 | */ |
| 415 | void ReduceSegmentsByVisibility(std::deque<CVector2D>& coords, unsigned maxSegmentLinks = 0, bool floating = true); |
| 416 | |
| 417 | /** |
| 418 | * Helper function to GetVisibilitySegments, factored out for testing. Merges single-point segments with its neighbouring |
| 419 | * segments. You should not have to call this method directly. |
| 420 | */ |
| 421 | static void MergeVisibilitySegments(std::deque<SVisibilitySegment>& segments); |
| 422 | |
| 423 | void RenderSubmit(SceneCollector& collector); |
| 424 | }; |
| 425 | |
| 426 | REGISTER_COMPONENT_TYPE(WayPointRenderer) |
| 427 | |
| 428 | void CCmpWayPointRenderer::Init(const CParamNode& paramNode) |
| 429 | { |
| 430 | m_Displayed = false; |
| 431 | m_LastOwner = INVALID_PLAYER; |
| 432 | m_LastMarkerCount = 0; |
| 433 | |
| 434 | // --------------------------------------------------------------------------------------------- |
| 435 | // load some XML configuration data (schema guarantees that all these nodes are valid) |
| 436 | |
| 437 | m_MarkerTemplate = paramNode.GetChild("MarkerTemplate").ToString(); |
| 438 | |
| 439 | const CParamNode& lineColor = paramNode.GetChild("LineColour"); |
| 440 | m_LineColor = CColor( |
| 441 | lineColor.GetChild("@r").ToInt()/255.f, |
| 442 | lineColor.GetChild("@g").ToInt()/255.f, |
| 443 | lineColor.GetChild("@b").ToInt()/255.f, |
| 444 | 1.f |
| 445 | ); |
| 446 | |
| 447 | const CParamNode& lineDashColor = paramNode.GetChild("LineDashColour"); |
| 448 | m_LineDashColor = CColor( |
| 449 | lineDashColor.GetChild("@r").ToInt()/255.f, |
| 450 | lineDashColor.GetChild("@g").ToInt()/255.f, |
| 451 | lineDashColor.GetChild("@b").ToInt()/255.f, |
| 452 | 1.f |
| 453 | ); |
| 454 | |
| 455 | m_LineThickness = paramNode.GetChild("LineThickness").ToFixed().ToFloat(); |
| 456 | m_LineTexturePath = paramNode.GetChild("LineTexture").ToString(); |
| 457 | m_LineTextureMaskPath = paramNode.GetChild("LineTextureMask").ToString(); |
| 458 | m_LineStartCapType = SOverlayTexturedLine::StrToLineCapType(paramNode.GetChild("LineStartCap").ToString()); |
| 459 | m_LineEndCapType = SOverlayTexturedLine::StrToLineCapType(paramNode.GetChild("LineEndCap").ToString()); |
| 460 | m_LineCostClass = paramNode.GetChild("LineCostClass").ToUTF8(); |
| 461 | m_LinePassabilityClass = paramNode.GetChild("LinePassabilityClass").ToUTF8(); |
| 462 | |
| 463 | // --------------------------------------------------------------------------------------------- |
| 464 | // load some textures |
| 465 | |
| 466 | if (CRenderer::IsInitialised()) |
| 467 | { |
| 468 | CTextureProperties texturePropsBase(m_LineTexturePath); |
| 469 | texturePropsBase.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE); |
| 470 | texturePropsBase.SetMaxAnisotropy(4.f); |
| 471 | m_Texture = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase); |
| 472 | |
| 473 | CTextureProperties texturePropsMask(m_LineTextureMaskPath); |
| 474 | texturePropsMask.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE); |
| 475 | texturePropsMask.SetMaxAnisotropy(4.f); |
| 476 | m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void CCmpWayPointRenderer::UpdateMarkers() |
| 481 | { |
| 482 | player_id_t previousOwner = m_LastOwner; |
| 483 | for (size_t i = 0; i < m_WayPoints.size(); ++i) |
| 484 | { |
| 485 | if (i >= m_MarkerEntityIds.size()) |
| 486 | m_MarkerEntityIds.push_back(INVALID_ENTITY); |
| 487 | |
| 488 | if (m_MarkerEntityIds[i] == INVALID_ENTITY) |
| 489 | { |
| 490 | // no marker exists yet, create one first |
| 491 | CComponentManager& componentMgr = GetSimContext().GetComponentManager(); |
| 492 | |
| 493 | // allocate a new entity for the marker |
| 494 | if (!m_MarkerTemplate.empty()) |
| 495 | { |
| 496 | m_MarkerEntityIds[i] = componentMgr.AllocateNewLocalEntity(); |
| 497 | if (m_MarkerEntityIds[i] != INVALID_ENTITY) |
| 498 | m_MarkerEntityIds[i] = componentMgr.AddEntity(m_MarkerTemplate, m_MarkerEntityIds[i]); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // the marker entity should be valid at this point, otherwise something went wrong trying to allocate it |
| 503 | if (m_MarkerEntityIds[i] == INVALID_ENTITY) |
| 504 | LOGERROR(L"Failed to create way point marker entity"); |
| 505 | |
| 506 | CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), m_MarkerEntityIds[i]); |
| 507 | if (cmpPosition) |
| 508 | { |
| 509 | if (m_Displayed && IsSet()) |
| 510 | { |
| 511 | cmpPosition->JumpTo(m_WayPoints[i].X, m_WayPoints[i].Y); |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | cmpPosition->MoveOutOfWorld(); // hide it |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // set way point flag selection based on player civilization |
| 520 | CmpPtr<ICmpOwnership> cmpOwnership(GetSimContext(), GetEntityId()); |
| 521 | if (cmpOwnership) |
| 522 | { |
| 523 | player_id_t ownerId = cmpOwnership->GetOwner(); |
| 524 | if (ownerId != INVALID_PLAYER && (ownerId != previousOwner || m_LastMarkerCount <= i)) |
| 525 | { |
| 526 | m_LastOwner = ownerId; |
| 527 | CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSimContext(), SYSTEM_ENTITY); |
| 528 | // cmpPlayerManager should not be null as long as this method is called on-demand instead of at Init() time |
| 529 | // (we can't rely on component initialization order in Init()) |
| 530 | if (cmpPlayerManager) |
| 531 | { |
| 532 | CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(ownerId)); |
| 533 | if (cmpPlayer) |
| 534 | { |
| 535 | CmpPtr<ICmpVisual> cmpVisualActor(GetSimContext(), m_MarkerEntityIds[i]); |
| 536 | if (cmpVisualActor) |
| 537 | { |
| 538 | cmpVisualActor->SetUnitEntitySelection(CStrW(cmpPlayer->GetCiv()).ToUTF8()); |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Hide currently unused markers |
| 547 | for (size_t i = m_WayPoints.size(); i < m_LastMarkerCount; ++i) |
| 548 | { |
| 549 | CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), m_MarkerEntityIds[i]); |
| 550 | if (cmpPosition) |
| 551 | cmpPosition->MoveOutOfWorld(); |
| 552 | } |
| 553 | m_LastMarkerCount = m_WayPoints.size(); |
| 554 | } |
| 555 | |
| 556 | void CCmpWayPointRenderer::RecomputeAllWayPointPaths() |
| 557 | { |
| 558 | m_Path.clear(); |
| 559 | m_VisibilitySegments.clear(); |
| 560 | m_TexturedOverlayLines.clear(); |
| 561 | |
| 562 | if (!IsSet()) |
| 563 | return; // no use computing a path if the way point isn't set |
| 564 | |
| 565 | CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId()); |
| 566 | if (!cmpPosition || !cmpPosition->IsInWorld()) |
| 567 | return; // no point going on if this entity doesn't have a position or is outside of the world |
| 568 | |
| 569 | // Not used |
| 570 | CmpPtr<ICmpFootprint> cmpFootprint(GetSimContext(), GetEntityId()); |
| 571 | CmpPtr<ICmpPathfinder> cmpPathfinder(GetSimContext(), SYSTEM_ENTITY); |
| 572 | |
| 573 | for (size_t i = 0; i < m_WayPoints.size(); ++i) |
| 574 | { |
| 575 | RecomputeWayPointPath(i, cmpPosition, cmpFootprint, cmpPathfinder); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void CCmpWayPointRenderer::RecomputeWayPointPath_wrapper(size_t index) |
| 580 | { |
| 581 | if (!IsSet()) |
| 582 | return; // no use computing a path if the wayw point isn't set |
| 583 | |
| 584 | CmpPtr<ICmpPosition> cmpPosition(GetSimContext(), GetEntityId()); |
| 585 | if (!cmpPosition || !cmpPosition->IsInWorld()) |
| 586 | return; // no point going on if this entity doesn't have a position or is outside of the world |
| 587 | |
| 588 | // Not used |
| 589 | CmpPtr<ICmpFootprint> cmpFootprint(GetSimContext(), GetEntityId()); |
| 590 | CmpPtr<ICmpPathfinder> cmpPathfinder(GetSimContext(), SYSTEM_ENTITY); |
| 591 | |
| 592 | RecomputeWayPointPath(index, cmpPosition, cmpFootprint, cmpPathfinder); |
| 593 | } |
| 594 | |
| 595 | void CCmpWayPointRenderer::RecomputeWayPointPath(size_t index, CmpPtr<ICmpPosition>& cmpPosition, CmpPtr<ICmpFootprint>& UNUSED(cmpFootprint), CmpPtr<ICmpPathfinder> UNUSED(cmpPathfinder)) |
| 596 | { |
| 597 | while (index >= m_Path.size()) |
| 598 | { |
| 599 | std::deque<CVector2D> tmp; |
| 600 | m_Path.push_back(tmp); |
| 601 | } |
| 602 | m_Path[index].clear(); |
| 603 | |
| 604 | while (index >= m_VisibilitySegments.size()) |
| 605 | { |
| 606 | std::deque<SVisibilitySegment> tmp; |
| 607 | m_VisibilitySegments.push_back(tmp); |
| 608 | } |
| 609 | m_VisibilitySegments[index].clear(); |
| 610 | |
| 611 | entity_pos_t pathStartX; |
| 612 | entity_pos_t pathStartY; |
| 613 | |
| 614 | if (index == 0) |
| 615 | { |
| 616 | pathStartX = cmpPosition->GetPosition2D().X; |
| 617 | pathStartY = cmpPosition->GetPosition2D().Y; |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | pathStartX = m_WayPoints[index-1].X; |
| 622 | pathStartY = m_WayPoints[index-1].Y; |
| 623 | } |
| 624 | |
| 625 | // Find a long path to the goal point -- this uses the tile-based pathfinder, which will return a |
| 626 | // list of waypoints (i.e. a Path) from the building/previous way point to the goal, where each |
| 627 | // waypoint is centered at a tile. We'll have to do some post-processing on the path to get it smooth. |
| 628 | // TODO update comment |
| 629 | |
| 630 | m_Path[index].push_back(CVector2D(m_WayPoints[index].X.ToFloat(), m_WayPoints[index].Y.ToFloat())); |
| 631 | m_Path[index].push_back(CVector2D(pathStartX.ToFloat(), pathStartY.ToFloat())); |
| 632 | |
| 633 | // find which point is the last visible point before going into the SoD, so we have a point to compare to on the next turn |
| 634 | GetVisibilitySegments(m_VisibilitySegments[index], index); |
| 635 | |
| 636 | // build overlay lines for the new path |
| 637 | ConstructOverlayLines(index); |
| 638 | } |
| 639 | |
| 640 | void CCmpWayPointRenderer::ConstructAllOverlayLines() |
| 641 | { |
| 642 | m_TexturedOverlayLines.clear(); |
| 643 | |
| 644 | for (size_t i = 0; i < m_Path.size(); ++i) |
| 645 | ConstructOverlayLines(i); |
| 646 | } |
| 647 | |
| 648 | void CCmpWayPointRenderer::ConstructOverlayLines(size_t index) |
| 649 | { |
| 650 | // We need to create a new SOverlayTexturedLine every time we want to change the coordinates after having passed it to the |
| 651 | // renderer, because it does some fancy vertex buffering thing and caches them internally instead of recomputing them on every |
| 652 | // pass (which is only sensible). |
| 653 | while (index >= m_TexturedOverlayLines.size()) |
| 654 | { |
| 655 | std::list<SOverlayTexturedLine> tmp; |
| 656 | m_TexturedOverlayLines.push_back(tmp); |
| 657 | } |
| 658 | |
| 659 | std::list<std::list<SOverlayTexturedLine> >::iterator iter = m_TexturedOverlayLines.begin(); |
| 660 | size_t count = index; |
| 661 | while(count--) |
| 662 | iter++; |
| 663 | (*iter).clear(); |
| 664 | |
| 665 | if (m_Path[index].size() < 2) |
| 666 | return; |
| 667 | |
| 668 | CmpPtr<ICmpTerrain> cmpTerrain(GetSimContext(), SYSTEM_ENTITY); |
| 669 | LineCapType dashesLineCapType = SOverlayTexturedLine::LINECAP_ROUND; // line caps to use for the dashed segments (and any other segment's edges that border it) |
| 670 | |
| 671 | for (std::deque<SVisibilitySegment>::const_iterator it = m_VisibilitySegments[index].begin(); it != m_VisibilitySegments[index].end(); ++it) |
| 672 | { |
| 673 | const SVisibilitySegment& segment = (*it); |
| 674 | |
| 675 | if (segment.m_Visible) |
| 676 | { |
| 677 | // does this segment border on the building or way point flag on either side? |
| 678 | bool bordersBuilding = (segment.m_EndIndex == m_Path[index].size() - 1); |
| 679 | bool bordersFlag = (segment.m_StartIndex == 0); |
| 680 | |
| 681 | // construct solid textured overlay line along a subset of the full path points from startPointIdx to endPointIdx |
| 682 | SOverlayTexturedLine overlayLine; |
| 683 | overlayLine.m_Thickness = m_LineThickness; |
| 684 | overlayLine.m_SimContext = &GetSimContext(); |
| 685 | overlayLine.m_TextureBase = m_Texture; |
| 686 | overlayLine.m_TextureMask = m_TextureMask; |
| 687 | overlayLine.m_Color = m_LineColor; |
| 688 | overlayLine.m_Closed = false; |
| 689 | // we should take care to only use m_LineXCap for the actual end points at the building and the way point; any intermediate |
| 690 | // end points (i.e., that border a dashed segment) should have the dashed cap |
| 691 | // the path line is actually in reverse order as well, so let's swap out the start and end caps |
| 692 | overlayLine.m_StartCapType = (bordersFlag ? m_LineEndCapType : dashesLineCapType); |
| 693 | overlayLine.m_EndCapType = (bordersBuilding ? m_LineStartCapType : dashesLineCapType); |
| 694 | overlayLine.m_AlwaysVisible = true; |
| 695 | |
| 696 | // push overlay line coordinates |
| 697 | ENSURE(segment.m_EndIndex > segment.m_StartIndex); |
| 698 | for (size_t j = segment.m_StartIndex; j <= segment.m_EndIndex; ++j) // end index is inclusive here |
| 699 | { |
| 700 | overlayLine.m_Coords.push_back(m_Path[index][j].X); |
| 701 | overlayLine.m_Coords.push_back(m_Path[index][j].Y); |
| 702 | } |
| 703 | |
| 704 | (*iter).push_back(overlayLine); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | // construct dashed line from startPointIdx to endPointIdx; add textured overlay lines for it to the render list |
| 709 | std::vector<CVector2D> straightLine; |
| 710 | straightLine.push_back(m_Path[index][segment.m_StartIndex]); |
| 711 | straightLine.push_back(m_Path[index][segment.m_EndIndex]); |
| 712 | |
| 713 | // We always want to the dashed line to end at either point with a full dash (i.e. not a cleared space), so that the dashed |
| 714 | // area is visually obvious. This requires some calculations to see what size we should make the dashes and clears for them |
| 715 | // to fit exactly. |
| 716 | |
| 717 | float maxDashSize = 3.f; |
| 718 | float maxClearSize = 3.f; |
| 719 | |
| 720 | float dashSize = maxDashSize; |
| 721 | float clearSize = maxClearSize; |
| 722 | float pairDashRatio = (dashSize / (dashSize + clearSize)); // ratio of the dash's length to a (dash + clear) pair's length |
| 723 | |
| 724 | float distance = (m_Path[index][segment.m_StartIndex] - m_Path[index][segment.m_EndIndex]).Length(); // straight-line distance between the points |
| 725 | |
| 726 | // See how many pairs (dash + clear) of unmodified size can fit into the distance. Then check the remaining distance; if it's not exactly |
| 727 | // a dash size's worth (which it probably won't be), then adjust the dash/clear sizes slightly so that it is. |
| 728 | int numFitUnmodified = floor(distance/(dashSize + clearSize)); |
| 729 | float remainderDistance = distance - (numFitUnmodified * (dashSize + clearSize)); |
| 730 | |
| 731 | // Now we want to make remainderDistance equal exactly one dash size (i.e. maxDashSize) by scaling dashSize and clearSize slightly. |
| 732 | // We have (remainderDistance - maxDashSize) of space to distribute over numFitUnmodified instances of (dashSize + clearSize) to make |
| 733 | // it fit, so each (dashSize + clearSize) pair needs to adjust its length by (remainderDistance - maxDashSize)/numFitUnmodified |
| 734 | // (which will be positive or negative accordingly). This number can then be distributed further proportionally among the dash's |
| 735 | // length and the clear's length. |
| 736 | |
| 737 | // we always want to have at least one dash/clear pair (i.e., "|===| |===|"); also, we need to avoid division by zero below. |
| 738 | numFitUnmodified = std::max(1, numFitUnmodified); |
| 739 | |
| 740 | float pairwiseLengthDifference = (remainderDistance - maxDashSize)/numFitUnmodified; // can be either positive or negative |
| 741 | dashSize += pairDashRatio * pairwiseLengthDifference; |
| 742 | clearSize += (1 - pairDashRatio) * pairwiseLengthDifference; |
| 743 | |
| 744 | // ------------------------------------------------------------------------------------------------ |
| 745 | |
| 746 | SDashedLine dashedLine; |
| 747 | SimRender::ConstructDashedLine(straightLine, dashedLine, dashSize, clearSize); |
| 748 | |
| 749 | // build overlay lines for dashes |
| 750 | size_t numDashes = dashedLine.m_StartIndices.size(); |
| 751 | for (size_t i=0; i < numDashes; i++) |
| 752 | { |
| 753 | SOverlayTexturedLine dashOverlay; |
| 754 | |
| 755 | dashOverlay.m_Thickness = m_LineThickness; |
| 756 | dashOverlay.m_SimContext = &GetSimContext(); |
| 757 | dashOverlay.m_TextureBase = m_Texture; |
| 758 | dashOverlay.m_TextureMask = m_TextureMask; |
| 759 | dashOverlay.m_Color = m_LineDashColor; |
| 760 | dashOverlay.m_Closed = false; |
| 761 | dashOverlay.m_StartCapType = dashesLineCapType; |
| 762 | dashOverlay.m_EndCapType = dashesLineCapType; |
| 763 | dashOverlay.m_AlwaysVisible = true; |
| 764 | // TODO: maybe adjust the elevation of the dashes to be a little lower, so that it slides underneath the actual path |
| 765 | |
| 766 | size_t dashStartIndex = dashedLine.m_StartIndices[i]; |
| 767 | size_t dashEndIndex = dashedLine.GetEndIndex(i); |
| 768 | ENSURE(dashEndIndex > dashStartIndex); |
| 769 | |
| 770 | for (size_t n = dashStartIndex; n < dashEndIndex; n++) |
| 771 | { |
| 772 | dashOverlay.m_Coords.push_back(dashedLine.m_Points[n].X); |
| 773 | dashOverlay.m_Coords.push_back(dashedLine.m_Points[n].Y); |
| 774 | } |
| 775 | |
| 776 | (*iter).push_back(dashOverlay); |
| 777 | } |
| 778 | |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | void CCmpWayPointRenderer::UpdateOverlayLines() |
| 784 | { |
| 785 | // We should only do this if the way point is currently being displayed and set inside the world, otherwise it's a massive |
| 786 | // waste of time to calculate all this stuff (this method is called every turn) |
| 787 | if (!m_Displayed || !IsSet()) |
| 788 | return; |
| 789 | |
| 790 | // see if there have been any changes to the SoD by grabbing the visibility edge points and comparing them to the previous ones |
| 791 | std::deque<std::deque<SVisibilitySegment> > newVisibilitySegments; |
| 792 | for (size_t i = 0; i < m_Path.size(); ++i) |
| 793 | { |
| 794 | std::deque<SVisibilitySegment> tmp; |
| 795 | newVisibilitySegments.push_back(tmp); |
| 796 | GetVisibilitySegments(newVisibilitySegments[i], i); |
| 797 | } |
| 798 | |
| 799 | // Check if the full path changed, then reconstruct all overlay lines, otherwise check if a segment changed and update that. |
| 800 | if (m_VisibilitySegments.size() != newVisibilitySegments.size()) |
| 801 | { |
| 802 | m_VisibilitySegments = newVisibilitySegments; // save the new visibility segments to compare against next time |
| 803 | ConstructAllOverlayLines(); |
| 804 | } |
| 805 | else |
| 806 | { |
| 807 | for (size_t i = 0; i < m_VisibilitySegments.size(); ++i) |
| 808 | { |
| 809 | if (m_VisibilitySegments[i] != newVisibilitySegments[i]) |
| 810 | { |
| 811 | // The visibility segments have changed, reconstruct the overlay lines to match. NOTE: The path itself doesn't |
| 812 | // change, only the overlay lines we construct from it. |
| 813 | m_VisibilitySegments[i] = newVisibilitySegments[i]; // save the new visibility segments to compare against next time |
| 814 | ConstructOverlayLines(i); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // TODO remove pathfinder?; or remove this as a whole? |
| 821 | void CCmpWayPointRenderer::ReduceSegmentsByVisibility(std::deque<CVector2D>& coords, unsigned maxSegmentLinks, bool floating) |
| 822 | { |
| 823 | CmpPtr<ICmpPathfinder> cmpPathFinder(GetSimContext(), SYSTEM_ENTITY); |
| 824 | CmpPtr<ICmpTerrain> cmpTerrain(GetSimContext(), SYSTEM_ENTITY); |
| 825 | CmpPtr<ICmpWaterManager> cmpWaterManager(GetSimContext(), SYSTEM_ENTITY); |
| 826 | ENSURE(cmpPathFinder && cmpTerrain && cmpWaterManager); |
| 827 | |
| 828 | if (coords.size() < 3) |
| 829 | return; |
| 830 | |
| 831 | // The basic idea is this: starting from a base node, keep checking each individual point along the path to see if there's a visible |
| 832 | // line between it and the base point. If so, keep going, otherwise, make the last visible point the new base node and start the same |
| 833 | // process from there on until the entire line is checked. The output is the array of base nodes. |
| 834 | |
| 835 | std::deque<CVector2D> newCoords; |
| 836 | StationaryOnlyObstructionFilter obstructionFilter; |
| 837 | entity_pos_t lineRadius = fixed::FromFloat(m_LineThickness); |
| 838 | ICmpPathfinder::pass_class_t passabilityClass = cmpPathFinder->GetPassabilityClass(m_LinePassabilityClass); |
| 839 | |
| 840 | newCoords.push_back(coords[0]); // save the first base node |
| 841 | |
| 842 | size_t baseNodeIdx = 0; |
| 843 | size_t curNodeIdx = 1; |
| 844 | |
| 845 | float baseNodeY; |
| 846 | entity_pos_t baseNodeX; |
| 847 | entity_pos_t baseNodeZ; |
| 848 | |
| 849 | // set initial base node coords |
| 850 | baseNodeX = fixed::FromFloat(coords[baseNodeIdx].X); |
| 851 | baseNodeZ = fixed::FromFloat(coords[baseNodeIdx].Y); |
| 852 | baseNodeY = cmpTerrain->GetExactGroundLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y); |
| 853 | if (floating) |
| 854 | baseNodeY = std::max(baseNodeY, cmpWaterManager->GetExactWaterLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y)); |
| 855 | |
| 856 | while (curNodeIdx < coords.size()) |
| 857 | { |
| 858 | ENSURE(curNodeIdx > baseNodeIdx); // this needs to be true at all times, otherwise we're checking visibility between a point and itself |
| 859 | |
| 860 | entity_pos_t curNodeX = fixed::FromFloat(coords[curNodeIdx].X); |
| 861 | entity_pos_t curNodeZ = fixed::FromFloat(coords[curNodeIdx].Y); |
| 862 | float curNodeY = cmpTerrain->GetExactGroundLevel(coords[curNodeIdx].X, coords[curNodeIdx].Y); |
| 863 | if (floating) |
| 864 | curNodeY = std::max(curNodeY, cmpWaterManager->GetExactWaterLevel(coords[curNodeIdx].X, coords[curNodeIdx].Y)); |
| 865 | |
| 866 | // find out whether curNode is visible from baseNode (careful; this is in 2D only; terrain height differences are ignored!) |
| 867 | bool curNodeVisible = cmpPathFinder->CheckMovement(obstructionFilter, baseNodeX, baseNodeZ, curNodeX, curNodeZ, lineRadius, passabilityClass); |
| 868 | |
| 869 | // since height differences are ignored by CheckMovement, let's call two points visible from one another only if they're at |
| 870 | // roughly the same terrain elevation |
| 871 | curNodeVisible = curNodeVisible && (fabsf(curNodeY - baseNodeY) < 3.f); // TODO: this could probably use some tuning |
| 872 | if (maxSegmentLinks > 0) |
| 873 | // max. amount of node-to-node links to be eliminated (unsigned subtraction is valid because curNodeIdx is always > baseNodeIdx) |
| 874 | curNodeVisible = curNodeVisible && ((curNodeIdx - baseNodeIdx) <= maxSegmentLinks); |
| 875 | |
| 876 | if (!curNodeVisible) |
| 877 | { |
| 878 | // current node is not visible from the base node, so the previous one was the last visible point from baseNode and should |
| 879 | // hence become the new base node for further iterations. |
| 880 | |
| 881 | // if curNodeIdx is adjacent to the current baseNode (which is possible due to steep height differences, e.g. hills), then |
| 882 | // we should take care not to stay stuck at the current base node |
| 883 | if (curNodeIdx > baseNodeIdx + 1) |
| 884 | { |
| 885 | baseNodeIdx = curNodeIdx - 1; |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | // curNodeIdx == baseNodeIdx + 1 |
| 890 | baseNodeIdx = curNodeIdx; |
| 891 | curNodeIdx++; // move the next candidate node one forward so that we don't test a point against itself in the next iteration |
| 892 | } |
| 893 | |
| 894 | newCoords.push_back(coords[baseNodeIdx]); // add new base node to output list |
| 895 | |
| 896 | // update base node coordinates |
| 897 | baseNodeX = fixed::FromFloat(coords[baseNodeIdx].X); |
| 898 | baseNodeZ = fixed::FromFloat(coords[baseNodeIdx].Y); |
| 899 | baseNodeY = cmpTerrain->GetExactGroundLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y); |
| 900 | if (floating) |
| 901 | baseNodeY = std::max(baseNodeY, cmpWaterManager->GetExactWaterLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y)); |
| 902 | } |
| 903 | |
| 904 | curNodeIdx++; |
| 905 | } |
| 906 | |
| 907 | // we always need to add the last point back to the array; if e.g. all the points up to the last one are all visible from the current |
| 908 | // base node, then the loop above just ends and no endpoint is ever added to the list. |
| 909 | ENSURE(curNodeIdx == coords.size()); |
| 910 | newCoords.push_back(coords[coords.size() - 1]); |
| 911 | |
| 912 | coords.swap(newCoords); |
| 913 | } |
| 914 | |
| 915 | void CCmpWayPointRenderer::GetVisibilitySegments(std::deque<SVisibilitySegment>& out, size_t index) |
| 916 | { |
| 917 | out.clear(); |
| 918 | |
| 919 | if (m_Path[index].size() < 2) |
| 920 | return; |
| 921 | |
| 922 | CmpPtr<ICmpRangeManager> cmpRangeMgr(GetSimContext(), SYSTEM_ENTITY); |
| 923 | |
| 924 | player_id_t currentPlayer = GetSimContext().GetCurrentDisplayedPlayer(); |
| 925 | CLosQuerier losQuerier(cmpRangeMgr->GetLosQuerier(currentPlayer)); |
| 926 | |
| 927 | // go through the path node list, comparing each node's visibility with the previous one. If it changes, end the current segment and start |
| 928 | // a new one at the next point. |
| 929 | |
| 930 | bool lastVisible = losQuerier.IsExplored( |
| 931 | (fixed::FromFloat(m_Path[index][0].X) / (int) TERRAIN_TILE_SIZE).ToInt_RoundToNearest(), |
| 932 | (fixed::FromFloat(m_Path[index][0].Y) / (int) TERRAIN_TILE_SIZE).ToInt_RoundToNearest() |
| 933 | ); |
| 934 | size_t curSegmentStartIndex = 0; // starting node index of the current segment |
| 935 | |
| 936 | for (size_t k = 1; k < m_Path[index].size(); ++k) |
| 937 | { |
| 938 | // grab tile indices for this coord |
| 939 | int i = (fixed::FromFloat(m_Path[index][k].X) / (int)TERRAIN_TILE_SIZE).ToInt_RoundToNearest(); |
| 940 | int j = (fixed::FromFloat(m_Path[index][k].Y) / (int)TERRAIN_TILE_SIZE).ToInt_RoundToNearest(); |
| 941 | |
| 942 | bool nodeVisible = losQuerier.IsExplored(i, j); |
| 943 | if (nodeVisible != lastVisible) |
| 944 | { |
| 945 | // visibility changed; write out the segment that was just completed and get ready for the new one |
| 946 | out.push_back(SVisibilitySegment(lastVisible, curSegmentStartIndex, k - 1)); |
| 947 | |
| 948 | //curSegmentStartIndex = k; // new segment starts here |
| 949 | curSegmentStartIndex = k - 1; |
| 950 | lastVisible = nodeVisible; |
| 951 | } |
| 952 | |
| 953 | } |
| 954 | |
| 955 | // terminate the last segment |
| 956 | out.push_back(SVisibilitySegment(lastVisible, curSegmentStartIndex, m_Path[index].size() - 1)); |
| 957 | |
| 958 | MergeVisibilitySegments(out); |
| 959 | } |
| 960 | |
| 961 | void CCmpWayPointRenderer::MergeVisibilitySegments(std::deque<SVisibilitySegment>& segments) |
| 962 | { |
| 963 | // Scan for single-point segments; if they are inbetween two other segments, delete them and merge the surrounding segments. |
| 964 | // If they're at either end of the path, include them in their bordering segment (but only if those bordering segments aren't |
| 965 | // themselves single-point segments, because then we would want those to get absorbed by its surrounding ones first). |
| 966 | |
| 967 | // first scan for absorptions of single-point surrounded segments (i.e. excluding edge segments) |
| 968 | size_t numSegments = segments.size(); |
| 969 | |
| 970 | // WARNING: FOR LOOP TRICKERY AHEAD! |
| 971 | for (size_t i = 1; i < numSegments - 1;) |
| 972 | { |
| 973 | SVisibilitySegment& segment = segments[i]; |
| 974 | if (segment.IsSinglePoint()) |
| 975 | { |
| 976 | // since the segments' visibility alternates, the surrounding ones should have the same visibility |
| 977 | ENSURE(segments[i-1].m_Visible == segments[i+1].m_Visible); |
| 978 | |
| 979 | segments[i-1].m_EndIndex = segments[i+1].m_EndIndex; // make previous segment span all the way across to the next |
| 980 | segments.erase(segments.begin() + i); // erase this segment ... |
| 981 | segments.erase(segments.begin() + i); // and the next (we removed [i], so [i+1] is now at position [i]) |
| 982 | numSegments -= 2; // we removed 2 segments, so update the loop condition |
| 983 | // in the next iteration, i should still point to the segment right after the one that got expanded, which is now |
| 984 | // at position i; so don't increment i here |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | ++i; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | ENSURE(numSegments == segments.size()); |
| 993 | |
| 994 | // check to see if the first segment needs to be merged with its neighbour |
| 995 | if (segments.size() >= 2 && segments[0].IsSinglePoint()) |
| 996 | { |
| 997 | int firstSegmentStartIndex = segments.front().m_StartIndex; |
| 998 | ENSURE(firstSegmentStartIndex == 0); |
| 999 | ENSURE(!segments[1].IsSinglePoint()); // at this point, the second segment should never be a single-point segment |
| 1000 | |
| 1001 | segments.erase(segments.begin()); |
| 1002 | segments.front().m_StartIndex = firstSegmentStartIndex; |
| 1003 | |
| 1004 | } |
| 1005 | |
| 1006 | // check to see if the last segment needs to be merged with its neighbour |
| 1007 | if (segments.size() >= 2 && segments[segments.size()-1].IsSinglePoint()) |
| 1008 | { |
| 1009 | int lastSegmentEndIndex = segments.back().m_EndIndex; |
| 1010 | ENSURE(!segments[segments.size()-2].IsSinglePoint()); // at this point, the second-to-last segment should never be a single-point segment |
| 1011 | |
| 1012 | segments.erase(segments.end()); |
| 1013 | segments.back().m_EndIndex = lastSegmentEndIndex; |
| 1014 | } |
| 1015 | |
| 1016 | // -------------------------------------------------------------------------------------------------------- |
| 1017 | // at this point, every segment should have at least 2 points |
| 1018 | for (size_t i = 0; i < segments.size(); ++i) |
| 1019 | { |
| 1020 | ENSURE(!segments[i].IsSinglePoint()); |
| 1021 | ENSURE(segments[i].m_EndIndex > segments[i].m_StartIndex); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | void CCmpWayPointRenderer::RenderSubmit(SceneCollector& collector) |
| 1026 | { |
| 1027 | // we only get here if the way point is set and should be displayed |
| 1028 | for (std::list<std::list<SOverlayTexturedLine> >::iterator it = m_TexturedOverlayLines.begin(); |
| 1029 | it != m_TexturedOverlayLines.end(); ++it) |
| 1030 | { |
| 1031 | for (std::list<SOverlayTexturedLine>::iterator iter = (*it).begin(); iter != (*it).end(); ++iter) |
| 1032 | { |
| 1033 | if (!(*iter).m_Coords.empty()) |
| 1034 | collector.Submit(&(*iter)); |
| 1035 | } |
| 1036 | } |
| 1037 | } |