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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
/* Copyright (C) 2022 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "CCmpRallyPointRenderer.h"
#include "ps/Profile.h"
#include "simulation2/components/ICmpRangeManager.h"
#include "simulation2/helpers/Los.h"
std::string CCmpRallyPointRenderer::GetSchema()
{
return
"<a:help>Displays a rally point marker where created units will gather when spawned</a:help>"
"<a:example>"
"<MarkerTemplate>special/rallypoint</MarkerTemplate>"
"<LineThickness>0.75</LineThickness>"
"<LineStartCap>round</LineStartCap>"
"<LineEndCap>square</LineEndCap>"
"<LineDashColor r='158' g='11' b='15'></LineDashColor>"
"<LinePassabilityClass>default</LinePassabilityClass>"
"</a:example>"
"<element name='MarkerTemplate' a:help='Template name for the rally point marker entity (typically a waypoint flag actor)'>"
"<text/>"
"</element>"
"<element name='LineTexture' a:help='Texture file to use for the rally point line'>"
"<text />"
"</element>"
"<element name='LineTextureMask' a:help='Texture mask to indicate where overlay colors are to be applied (see LineColor and LineDashColor)'>"
"<text />"
"</element>"
"<element name='LineThickness' a:help='Thickness of the marker line connecting the entity to the rally point marker'>"
"<data type='decimal'/>"
"</element>"
"<element name='LineDashColor'>"
"<attribute name='r'>"
"<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
"</attribute>"
"<attribute name='g'>"
"<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
"</attribute>"
"<attribute name='b'>"
"<data type='integer'><param name='minInclusive'>0</param><param name='maxInclusive'>255</param></data>"
"</attribute>"
"</element>"
"<element name='LineStartCap'>"
"<choice>"
"<value a:help='Abrupt line ending; line endings are not closed'>flat</value>"
"<value a:help='Semi-circular line end cap'>round</value>"
"<value a:help='Sharp, pointy line end cap'>sharp</value>"
"<value a:help='Square line end cap'>square</value>"
"</choice>"
"</element>"
"<element name='LineEndCap'>"
"<choice>"
"<value a:help='Abrupt line ending; line endings are not closed'>flat</value>"
"<value a:help='Semi-circular line end cap'>round</value>"
"<value a:help='Sharp, pointy line end cap'>sharp</value>"
"<value a:help='Square line end cap'>square</value>"
"</choice>"
"</element>"
"<element name='LinePassabilityClass' a:help='The pathfinder passability class to use for computing the rally point marker line path'>"
"<text />"
"</element>";
}
void CCmpRallyPointRenderer::Init(const CParamNode& paramNode)
{
m_Displayed = false;
m_SmoothPath = true;
m_LastOwner = INVALID_PLAYER;
m_LastMarkerCount = 0;
m_EnableDebugNodeOverlay = false;
UpdateLineColor();
// ---------------------------------------------------------------------------------------------
// Load some XML configuration data (schema guarantees that all these nodes are valid)
m_MarkerTemplate = paramNode.GetChild("MarkerTemplate").ToWString();
const CParamNode& lineDashColor = paramNode.GetChild("LineDashColor");
m_LineDashColor = CColor(
lineDashColor.GetChild("@r").ToInt()/255.f,
lineDashColor.GetChild("@g").ToInt()/255.f,
lineDashColor.GetChild("@b").ToInt()/255.f,
1.f
);
m_LineThickness = paramNode.GetChild("LineThickness").ToFixed().ToFloat();
m_LineTexturePath = paramNode.GetChild("LineTexture").ToWString();
m_LineTextureMaskPath = paramNode.GetChild("LineTextureMask").ToWString();
m_LineStartCapType = SOverlayTexturedLine::StrToLineCapType(paramNode.GetChild("LineStartCap").ToWString());
m_LineEndCapType = SOverlayTexturedLine::StrToLineCapType(paramNode.GetChild("LineEndCap").ToWString());
m_LinePassabilityClass = paramNode.GetChild("LinePassabilityClass").ToString();
// ---------------------------------------------------------------------------------------------
// Load some textures
if (CRenderer::IsInitialised())
{
CTextureProperties texturePropsBase(m_LineTexturePath);
texturePropsBase.SetAddressMode(
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_BORDER,
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE);
texturePropsBase.SetAnisotropicFilter(true);
m_Texture = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase);
CTextureProperties texturePropsMask(m_LineTextureMaskPath);
texturePropsMask.SetAddressMode(
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_BORDER,
Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE);
texturePropsMask.SetAnisotropicFilter(true);
m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask);
}
}
void CCmpRallyPointRenderer::ClassInit(CComponentManager& componentManager)
{
componentManager.SubscribeGloballyToMessageType(MT_PlayerColorChanged);
componentManager.SubscribeToMessageType(MT_OwnershipChanged);
componentManager.SubscribeToMessageType(MT_TurnStart);
componentManager.SubscribeToMessageType(MT_Destroy);
componentManager.SubscribeToMessageType(MT_PositionChanged);
}
void CCmpRallyPointRenderer::Deinit()
{
}
void CCmpRallyPointRenderer::Serialize(ISerializer& UNUSED(serialize))
{
// Do NOT serialize anything; this is a rendering-only component, it does not and should not affect simulation state
}
void CCmpRallyPointRenderer::Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
{
Init(paramNode);
// The dependent components have not been deserialized, so the color is loaded on first SetDisplayed
}
void CCmpRallyPointRenderer::HandleMessage(const CMessage& msg, bool UNUSED(global))
{
switch (msg.GetType())
{
case MT_PlayerColorChanged:
{
const CMessagePlayerColorChanged& msgData = static_cast<const CMessagePlayerColorChanged&> (msg);
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
if (!cmpOwnership || msgData.player != cmpOwnership->GetOwner())
break;
UpdateLineColor();
ConstructAllOverlayLines();
}
break;
case MT_RenderSubmit:
{
PROFILE("RallyPoint::RenderSubmit");
if (m_Displayed && IsSet())
{
const CMessageRenderSubmit& msgData = static_cast<const CMessageRenderSubmit&> (msg);
RenderSubmit(msgData.collector, msgData.frustum, msgData.culling);
}
}
break;
case MT_OwnershipChanged:
{
const CMessageOwnershipChanged& msgData = static_cast<const CMessageOwnershipChanged&> (msg);
// Ignore destroyed entities
if (msgData.to == INVALID_PLAYER)
break;
Reset();
// Required for both the initial and capturing players color
UpdateLineColor();
// Support capturing, even though RallyPoint is typically deleted then
UpdateMarkers();
ConstructAllOverlayLines();
}
break;
case MT_TurnStart:
{
UpdateOverlayLines(); // Check for changes to the SoD and update the overlay lines accordingly
}
break;
case MT_Destroy:
{
Reset();
}
break;
case MT_PositionChanged:
{
// Unlikely to happen in-game, but can occur in atlas
// Just recompute the path from the entity to the first rally point
RecomputeRallyPointPath_wrapper(0);
}
break;
}
}
void CCmpRallyPointRenderer::UpdateMessageSubscriptions()
{
GetSimContext().GetComponentManager().DynamicSubscriptionNonsync(MT_RenderSubmit, this, m_Displayed && IsSet());
}
void CCmpRallyPointRenderer::UpdateMarkers()
{
player_id_t previousOwner = m_LastOwner;
for (size_t i = 0; i < m_RallyPoints.size(); ++i)
{
if (i >= m_MarkerEntityIds.size())
m_MarkerEntityIds.push_back(INVALID_ENTITY);
if (m_MarkerEntityIds[i] == INVALID_ENTITY)
{
// No marker exists yet, create one first
CComponentManager& componentMgr = GetSimContext().GetComponentManager();
// Allocate a new entity for the marker
if (!m_MarkerTemplate.empty())
{
m_MarkerEntityIds[i] = componentMgr.AllocateNewLocalEntity();
if (m_MarkerEntityIds[i] != INVALID_ENTITY)
m_MarkerEntityIds[i] = componentMgr.AddEntity(m_MarkerTemplate, m_MarkerEntityIds[i]);
}
}
// The marker entity should be valid at this point, otherwise something went wrong trying to allocate it
if (m_MarkerEntityIds[i] == INVALID_ENTITY)
LOGERROR("Failed to create rally point marker entity");
CmpPtr<ICmpPosition> markerCmpPosition(GetSimContext(), m_MarkerEntityIds[i]);
if (markerCmpPosition)
{
if (m_Displayed && IsSet())
{
markerCmpPosition->MoveTo(m_RallyPoints[i].X, m_RallyPoints[i].Y);
}
else
{
markerCmpPosition->MoveOutOfWorld();
}
}
// Set rally point flag selection based on player civilization
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
if (!cmpOwnership)
continue;
player_id_t ownerId = cmpOwnership->GetOwner();
if (ownerId == INVALID_PLAYER || (ownerId == previousOwner && m_LastMarkerCount >= i))
continue;
m_LastOwner = ownerId;
CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
// cmpPlayerManager should not be null as long as this method is called on-demand instead of at Init() time
// (we can't rely on component initialization order in Init())
if (!cmpPlayerManager)
continue;
CmpPtr<ICmpIdentity> cmpIdentity(GetSimContext(), cmpPlayerManager->GetPlayerByID(ownerId));
if (!cmpIdentity)
continue;
CmpPtr<ICmpVisual> cmpVisualActor(GetSimContext(), m_MarkerEntityIds[i]);
if (cmpVisualActor)
cmpVisualActor->SetVariant("civ", CStrW(cmpIdentity->GetCiv()).ToUTF8());
}
m_LastMarkerCount = m_RallyPoints.size() - 1;
}
void CCmpRallyPointRenderer::UpdateLineColor()
{
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
if (!cmpOwnership)
return;
player_id_t owner = cmpOwnership->GetOwner();
if (owner == INVALID_PLAYER)
return;
CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
if (!cmpPlayerManager)
return;
CmpPtr<ICmpPlayer> cmpPlayer(GetSimContext(), cmpPlayerManager->GetPlayerByID(owner));
if (!cmpPlayer)
return;
m_LineColor = cmpPlayer->GetDisplayedColor();
}
void CCmpRallyPointRenderer::RecomputeAllRallyPointPaths()
{
m_Path.clear();
m_VisibilitySegments.clear();
m_TexturedOverlayLines.clear();
if (m_EnableDebugNodeOverlay)
m_DebugNodeOverlays.clear();
// No use computing a path if the rally point isn't set
if (!IsSet())
return;
CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
// No point going on if this entity doesn't have a position or is outside of the world
if (!cmpPosition || !cmpPosition->IsInWorld())
return;
CmpPtr<ICmpFootprint> cmpFootprint(GetEntityHandle());
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
for (size_t i = 0; i < m_RallyPoints.size(); ++i)
{
RecomputeRallyPointPath(i, cmpPosition, cmpFootprint, cmpPathfinder);
}
}
void CCmpRallyPointRenderer::RecomputeRallyPointPath_wrapper(size_t index)
{
// No use computing a path if the rally point isn't set
if (!IsSet())
return;
// No point going on if this entity doesn't have a position or is outside of the world
CmpPtr<ICmpPosition> cmpPosition(GetEntityHandle());
if (!cmpPosition || !cmpPosition->IsInWorld())
return;
CmpPtr<ICmpFootprint> cmpFootprint(GetEntityHandle());
CmpPtr<ICmpPathfinder> cmpPathfinder(GetSystemEntity());
RecomputeRallyPointPath(index, cmpPosition, cmpFootprint, cmpPathfinder);
}
void CCmpRallyPointRenderer::RecomputeRallyPointPath(size_t index, CmpPtr<ICmpPosition>& cmpPosition, CmpPtr<ICmpFootprint>& cmpFootprint, CmpPtr<ICmpPathfinder> cmpPathfinder)
{
while (index >= m_Path.size())
{
std::vector<CVector2D> tmp;
m_Path.push_back(tmp);
}
m_Path[index].clear();
while (index >= m_VisibilitySegments.size())
{
std::vector<SVisibilitySegment> tmp;
m_VisibilitySegments.push_back(tmp);
}
m_VisibilitySegments[index].clear();
// Find a long path to the goal point -- this uses the tile-based pathfinder, which will return a
// list of waypoints (i.e. a Path) from the goal to the foundation/previous rally point, where each
// waypoint is centered at a tile. We'll have to do some post-processing on the path to get it smooth.
WaypointPath path;
std::vector<Waypoint>& waypoints = path.m_Waypoints;
CFixedVector2D start(cmpPosition->GetPosition2D());
PathGoal goal = { PathGoal::POINT, m_RallyPoints[index].X, m_RallyPoints[index].Y };
if (index == 0)
GetClosestsEdgePointFrom(start,m_RallyPoints[index], cmpPosition, cmpFootprint);
else
{
start.X = m_RallyPoints[index-1].X;
start.Y = m_RallyPoints[index-1].Y;
}
cmpPathfinder->ComputePathImmediate(start.X, start.Y, goal, cmpPathfinder->GetPassabilityClass(m_LinePassabilityClass), path);
// Check if we got a path back; if not we probably have two markers less than one tile apart.
if (path.m_Waypoints.size() < 2)
{
m_Path[index].emplace_back(start.X.ToFloat(), start.Y.ToFloat());
m_Path[index].emplace_back(m_RallyPoints[index].X.ToFloat(), m_RallyPoints[index].Y.ToFloat());
return;
}
else if (index == 0)
{
// Sometimes this ends up not being optimal if you asked for a long path, so improve.
CFixedVector2D newend(waypoints[waypoints.size()-2].x,waypoints[waypoints.size()-2].z);
GetClosestsEdgePointFrom(newend,newend, cmpPosition, cmpFootprint);
waypoints.back().x = newend.X;
waypoints.back().z = newend.Y;
}
else
{
// Make sure we actually start at the rallypoint because the pathfinder moves us to a usable tile.
waypoints.back().x = m_RallyPoints[index-1].X;
waypoints.back().z = m_RallyPoints[index-1].Y;
}
// Pathfinder makes us go to the nearest passable cell which isn't always what we want
waypoints[0].x = m_RallyPoints[index].X;
waypoints[0].z = m_RallyPoints[index].Y;
// From here on, we choose to represent the waypoints as CVector2D floats to avoid to have to convert back and forth
// between fixed-point Waypoint/CFixedVector2D and various other float-based formats used by interpolation and whatnot.
// Since we'll only be further using these points for rendering purposes, using floats should be fine.
for (Waypoint& waypoint : waypoints)
m_Path[index].emplace_back(waypoint.x.ToFloat(), waypoint.z.ToFloat());
// Post-processing
// Linearize the path;
// Pass through the waypoints, averaging each waypoint with its next one except the last one. Because the path
// goes from the marker to this entity/the previous flag and we want to keep the point at the marker's exact position,
// loop backwards through the waypoints so that the marker waypoint is maintained.
// TODO: see if we can do this at the same time as the waypoint -> coord conversion above
for(size_t i = m_Path[index].size() - 2; i > 0; --i)
m_Path[index][i] = (m_Path[index][i] + m_Path[index][i-1]) / 2.0f;
// Eliminate some consecutive waypoints that are visible from eachother. Reduce across a maximum distance of approx. 6 tiles
// (prevents segments that are too long to properly stick to the terrain)
ReduceSegmentsByVisibility(m_Path[index], 6);
// Debug overlays
if (m_EnableDebugNodeOverlay)
{
while (index >= m_DebugNodeOverlays.size())
m_DebugNodeOverlays.emplace_back();
m_DebugNodeOverlays[index].clear();
}
if (m_EnableDebugNodeOverlay && m_SmoothPath)
{
// Create separate control point overlays so we can differentiate when using smoothing (offset them a little higher from the
// terrain so we can still see them after the interpolated points are added)
for (const CVector2D& point : m_Path[index])
{
SOverlayLine overlayLine;
overlayLine.m_Color = CColor(1.0f, 0.0f, 0.0f, 1.0f);
overlayLine.m_Thickness = 0.1f;
SimRender::ConstructSquareOnGround(GetSimContext(), point.X, point.Y, 0.2f, 0.2f, 1.0f, overlayLine, true);
m_DebugNodeOverlays[index].push_back(overlayLine);
}
}
if (m_SmoothPath)
// The number of points to interpolate goes hand in hand with the maximum amount of node links allowed to be joined together
// by the visibility reduction. The more node links that can be joined together, the more interpolated points you need to
// generate to be able to deal with local terrain height changes.
// no offset, keep line at its exact path
SimRender::InterpolatePointsRNS(m_Path[index], false, 0, 4);
// 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
GetVisibilitySegments(m_VisibilitySegments[index], index);
// Build overlay lines for the new path
ConstructOverlayLines(index);
}
void CCmpRallyPointRenderer::ConstructAllOverlayLines()
{
m_TexturedOverlayLines.clear();
for (size_t i = 0; i < m_Path.size(); ++i)
ConstructOverlayLines(i);
}
void CCmpRallyPointRenderer::ConstructOverlayLines(size_t index)
{
// We need to create a new SOverlayTexturedLine every time we want to change the coordinates after having passed it to the
// renderer, because it does some fancy vertex buffering thing and caches them internally instead of recomputing them on every
// pass (which is only sensible).
while (index >= m_TexturedOverlayLines.size())
{
std::vector<SOverlayTexturedLine> tmp;
m_TexturedOverlayLines.push_back(tmp);
}
m_TexturedOverlayLines[index].clear();
if (m_Path[index].size() < 2)
return;
SOverlayTexturedLine::LineCapType dashesLineCapType = SOverlayTexturedLine::LINECAP_ROUND; // line caps to use for the dashed segments (and any other segment's edges that border it)
for(const SVisibilitySegment& segment : m_VisibilitySegments[index])
{
if (segment.m_Visible)
{
// Does this segment border on the building or rally point flag on either side?
bool bordersBuilding = (segment.m_EndIndex == m_Path[index].size() - 1);
bool bordersFlag = (segment.m_StartIndex == 0);
// Construct solid textured overlay line along a subset of the full path points from startPointIdx to endPointIdx
SOverlayTexturedLine overlayLine;
overlayLine.m_Thickness = m_LineThickness;
overlayLine.m_SimContext = &GetSimContext();
overlayLine.m_TextureBase = m_Texture;
overlayLine.m_TextureMask = m_TextureMask;
overlayLine.m_Color = m_LineColor;
overlayLine.m_Closed = false;
// We should take care to only use m_LineXCap for the actual end points at the building and the rally point; any intermediate
// end points (i.e., that border a dashed segment) should have the dashed cap
// the path line is actually in reverse order as well, so let's swap out the start and end caps
overlayLine.m_StartCapType = (bordersFlag ? m_LineEndCapType : dashesLineCapType);
overlayLine.m_EndCapType = (bordersBuilding ? m_LineStartCapType : dashesLineCapType);
overlayLine.m_AlwaysVisible = true;
// Push overlay line coordinates
ENSURE(segment.m_EndIndex > segment.m_StartIndex);
// End index is inclusive here
for (size_t j = segment.m_StartIndex; j <= segment.m_EndIndex; ++j)
overlayLine.m_Coords.push_back(m_Path[index][j]);
m_TexturedOverlayLines[index].push_back(overlayLine);
}
else
{
// Construct dashed line from startPointIdx to endPointIdx; add textured overlay lines for it to the render list
std::vector<CVector2D> straightLine;
straightLine.push_back(m_Path[index][segment.m_StartIndex]);
straightLine.push_back(m_Path[index][segment.m_EndIndex]);
// 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
// area is visually obvious. This requires some calculations to see what size we should make the dashes and clears for them
// to fit exactly.
float maxDashSize = 3.f;
float maxClearSize = 3.f;
float dashSize = maxDashSize;
float clearSize = maxClearSize;
// Ratio of the dash's length to a (dash + clear) pair's length
float pairDashRatio = dashSize / (dashSize + clearSize);
// Straight-line distance between the points
float distance = (m_Path[index][segment.m_StartIndex] - m_Path[index][segment.m_EndIndex]).Length();
// See how many pairs (dash + clear) of unmodified size can fit into the distance. Then check the remaining distance; if it's not exactly
// a dash size's worth (which it probably won't be), then adjust the dash/clear sizes slightly so that it is.
int numFitUnmodified = floor(distance/(dashSize + clearSize));
float remainderDistance = distance - (numFitUnmodified * (dashSize + clearSize));
// Now we want to make remainderDistance equal exactly one dash size (i.e. maxDashSize) by scaling dashSize and clearSize slightly.
// We have (remainderDistance - maxDashSize) of space to distribute over numFitUnmodified instances of (dashSize + clearSize) to make
// it fit, so each (dashSize + clearSize) pair needs to adjust its length by (remainderDistance - maxDashSize)/numFitUnmodified
// (which will be positive or negative accordingly). This number can then be distributed further proportionally among the dash's
// length and the clear's length.
// We always want to have at least one dash/clear pair (i.e., "|===| |===|"); also, we need to avoid division by zero below.
numFitUnmodified = std::max(1, numFitUnmodified);
// Can be either positive or negative
float pairwiseLengthDifference = (remainderDistance - maxDashSize)/numFitUnmodified;
dashSize += pairDashRatio * pairwiseLengthDifference;
clearSize += (1 - pairDashRatio) * pairwiseLengthDifference;
// ------------------------------------------------------------------------------------------------
SDashedLine dashedLine;
SimRender::ConstructDashedLine(straightLine, dashedLine, dashSize, clearSize);
// Build overlay lines for dashes
size_t numDashes = dashedLine.m_StartIndices.size();
for (size_t i=0; i < numDashes; i++)
{
SOverlayTexturedLine dashOverlay;
dashOverlay.m_Thickness = m_LineThickness;
dashOverlay.m_SimContext = &GetSimContext();
dashOverlay.m_TextureBase = m_Texture;
dashOverlay.m_TextureMask = m_TextureMask;
dashOverlay.m_Color = m_LineDashColor;
dashOverlay.m_Closed = false;
dashOverlay.m_StartCapType = dashesLineCapType;
dashOverlay.m_EndCapType = dashesLineCapType;
dashOverlay.m_AlwaysVisible = true;
// TODO: maybe adjust the elevation of the dashes to be a little lower, so that it slides underneath the actual path
size_t dashStartIndex = dashedLine.m_StartIndices[i];
size_t dashEndIndex = dashedLine.GetEndIndex(i);
ENSURE(dashEndIndex > dashStartIndex);
for (size_t j = dashStartIndex; j < dashEndIndex; ++j)
dashOverlay.m_Coords.push_back(dashedLine.m_Points[j]);
m_TexturedOverlayLines[index].push_back(dashOverlay);
}
}
}
//// <DEBUG> //////////////////////////////////////////////
if (m_EnableDebugNodeOverlay)
{
while (index >= m_DebugNodeOverlays.size())
{
std::vector<SOverlayLine> tmp;
m_DebugNodeOverlays.push_back(tmp);
}
for (size_t j = 0; j < m_Path[index].size(); ++j)
{
SOverlayLine overlayLine;
overlayLine.m_Color = CColor(1.0f, 1.0f, 1.0f, 1.0f);
overlayLine.m_Thickness = 1;
SimRender::ConstructCircleOnGround(GetSimContext(), m_Path[index][j].X, m_Path[index][j].Y, 0.075f, overlayLine, true);
m_DebugNodeOverlays[index].push_back(overlayLine);
}
}
//// </DEBUG> //////////////////////////////////////////////
}
void CCmpRallyPointRenderer::UpdateOverlayLines()
{
// We should only do this if the rally point is currently being displayed and set inside the world, otherwise it's a massive
// waste of time to calculate all this stuff (this method is called every turn)
if (!m_Displayed || !IsSet())
return;
// See if there have been any changes to the SoD by grabbing the visibility edge points and comparing them to the previous ones
std::vector<std::vector<SVisibilitySegment> > newVisibilitySegments;
for (size_t i = 0; i < m_Path.size(); ++i)
{
std::vector<SVisibilitySegment> tmp;
newVisibilitySegments.push_back(tmp);
GetVisibilitySegments(newVisibilitySegments[i], i);
}
// Check if the full path changed, then reconstruct all overlay lines, otherwise check if a segment changed and update that.
if (m_VisibilitySegments.size() != newVisibilitySegments.size())
{
// Save the new visibility segments to compare against next time
m_VisibilitySegments = newVisibilitySegments;
ConstructAllOverlayLines();
}
else
{
for (size_t i = 0; i < m_VisibilitySegments.size(); ++i)
{
if (m_VisibilitySegments[i] != newVisibilitySegments[i])
{
// The visibility segments have changed, reconstruct the overlay lines to match. NOTE: The path itself doesn't
// change, only the overlay lines we construct from it.
// Save the new visibility segments to compare against next time
m_VisibilitySegments[i] = newVisibilitySegments[i];
ConstructOverlayLines(i);
}
}
}
}
void CCmpRallyPointRenderer::GetClosestsEdgePointFrom(CFixedVector2D& result, CFixedVector2D& start, CmpPtr<ICmpPosition> cmpPosition, CmpPtr<ICmpFootprint> cmpFootprint) const
{
ENSURE(cmpPosition);
ENSURE(cmpFootprint);
// Grab the shape and dimensions of the footprint
entity_pos_t footprintSize0, footprintSize1, footprintHeight;
ICmpFootprint::EShape footprintShape;
cmpFootprint->GetShape(footprintShape, footprintSize0, footprintSize1, footprintHeight);
// Grab the center of the footprint
CFixedVector2D center = cmpPosition->GetPosition2D();
switch (footprintShape)
{
case ICmpFootprint::SQUARE:
{
// In this case, footprintSize0 and 1 indicate the size along the X and Z axes, respectively.
// The building's footprint could be rotated any which way, so let's get the rotation around the Y axis
// and the rotated unit vectors in the X/Z plane of the shape's footprint
// (the Footprint itself holds only the outline, the Position holds the orientation)
// Sinus and cosinus of the Y axis rotation angle (aka the yaw)
fixed s, c;
fixed a = cmpPosition->GetRotation().Y;
sincos_approx(a, s, c);
// Unit vector along the rotated X axis
CFixedVector2D u(c, -s);
// Unit vector along the rotated Z axis
CFixedVector2D v(s, c);
CFixedVector2D halfSize(footprintSize0 / 2, footprintSize1 / 2);
CFixedVector2D footprintEdgePoint = Geometry::NearestPointOnSquare(start - center, u, v, halfSize);
result = center + footprintEdgePoint;
break;
}
case ICmpFootprint::CIRCLE:
{
// In this case, both footprintSize0 and 1 indicate the circle's radius
// Transform target to the point nearest on the edge.
CFixedVector2D centerVec2D(center.X, center.Y);
CFixedVector2D centerToLast(start - centerVec2D);
centerToLast.Normalize();
result = centerVec2D + (centerToLast.Multiply(footprintSize0));
break;
}
}
}
void CCmpRallyPointRenderer::ReduceSegmentsByVisibility(std::vector<CVector2D>& coords, unsigned maxSegmentLinks, bool floating) const
{
CmpPtr<ICmpPathfinder> cmpPathFinder(GetSystemEntity());
CmpPtr<ICmpTerrain> cmpTerrain(GetSystemEntity());
CmpPtr<ICmpWaterManager> cmpWaterManager(GetSystemEntity());
ENSURE(cmpPathFinder && cmpTerrain && cmpWaterManager);
if (coords.size() < 3)
return;
// 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
// 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
// process from there on until the entire line is checked. The output is the array of base nodes.
std::vector<CVector2D> newCoords;
StationaryOnlyObstructionFilter obstructionFilter;
entity_pos_t lineRadius = fixed::FromFloat(m_LineThickness);
pass_class_t passabilityClass = cmpPathFinder->GetPassabilityClass(m_LinePassabilityClass);
// Save the first base node
newCoords.push_back(coords[0]);
size_t baseNodeIdx = 0;
size_t curNodeIdx = 1;
float baseNodeY;
entity_pos_t baseNodeX;
entity_pos_t baseNodeZ;
// Set initial base node coords
baseNodeX = fixed::FromFloat(coords[baseNodeIdx].X);
baseNodeZ = fixed::FromFloat(coords[baseNodeIdx].Y);
baseNodeY = cmpTerrain->GetExactGroundLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y);
if (floating)
baseNodeY = std::max(baseNodeY, cmpWaterManager->GetExactWaterLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y));
while (curNodeIdx < coords.size())
{
// This needs to be true at all times, otherwise we're checking visibility between a point and itself.
ENSURE(curNodeIdx > baseNodeIdx);
entity_pos_t curNodeX = fixed::FromFloat(coords[curNodeIdx].X);
entity_pos_t curNodeZ = fixed::FromFloat(coords[curNodeIdx].Y);
float curNodeY = cmpTerrain->GetExactGroundLevel(coords[curNodeIdx].X, coords[curNodeIdx].Y);
if (floating)
curNodeY = std::max(curNodeY, cmpWaterManager->GetExactWaterLevel(coords[curNodeIdx].X, coords[curNodeIdx].Y));
// Find out whether curNode is visible from baseNode (careful; this is in 2D only; terrain height differences are ignored!)
bool curNodeVisible = cmpPathFinder->CheckMovement(obstructionFilter, baseNodeX, baseNodeZ, curNodeX, curNodeZ, lineRadius, passabilityClass);
// Since height differences are ignored by CheckMovement, let's call two points visible from one another only if they're at
// roughly the same terrain elevation
// TODO: this could probably use some tuning
curNodeVisible = curNodeVisible && (fabsf(curNodeY - baseNodeY) < 3.f);
if (maxSegmentLinks > 0)
// Max. amount of node-to-node links to be eliminated (unsigned subtraction is valid because curNodeIdx is always > baseNodeIdx)
curNodeVisible = curNodeVisible && ((curNodeIdx - baseNodeIdx) <= maxSegmentLinks);
if (!curNodeVisible)
{
// Current node is not visible from the base node, so the previous one was the last visible point from baseNode and should
// hence become the new base node for further iterations.
// If curNodeIdx is adjacent to the current baseNode (which is possible due to steep height differences, e.g. hills), then
// we should take care not to stay stuck at the current base node
if (curNodeIdx > baseNodeIdx + 1)
{
baseNodeIdx = curNodeIdx - 1;
}
else
{
// curNodeIdx == baseNodeIdx + 1
baseNodeIdx = curNodeIdx;
// Move the next candidate node one forward so that we don't test a point against itself in the next iteration
++curNodeIdx;
}
// Add new base node to output list
newCoords.push_back(coords[baseNodeIdx]);
// Update base node coordinates
baseNodeX = fixed::FromFloat(coords[baseNodeIdx].X);
baseNodeZ = fixed::FromFloat(coords[baseNodeIdx].Y);
baseNodeY = cmpTerrain->GetExactGroundLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y);
if (floating)
baseNodeY = std::max(baseNodeY, cmpWaterManager->GetExactWaterLevel(coords[baseNodeIdx].X, coords[baseNodeIdx].Y));
}
++curNodeIdx;
}
// 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
// base node, then the loop above just ends and no endpoint is ever added to the list.
ENSURE(curNodeIdx == coords.size());
newCoords.push_back(coords[coords.size() - 1]);
coords.swap(newCoords);
}
void CCmpRallyPointRenderer::GetVisibilitySegments(std::vector<SVisibilitySegment>& out, size_t index) const
{
out.clear();
if (m_Path[index].size() < 2)
return;
CmpPtr<ICmpRangeManager> cmpRangeMgr(GetSystemEntity());
player_id_t currentPlayer = static_cast<player_id_t>(GetSimContext().GetCurrentDisplayedPlayer());
CLosQuerier losQuerier(cmpRangeMgr->GetLosQuerier(currentPlayer));
// Go through the path node list, comparing each node's visibility with the previous one. If it changes, end the current segment and start
// a new one at the next point.
const float cellSize = static_cast<float>(LOS_TILE_SIZE);
bool lastVisible = losQuerier.IsExplored(
(fixed::FromFloat(m_Path[index][0].X / cellSize)).ToInt_RoundToNearest(),
(fixed::FromFloat(m_Path[index][0].Y / cellSize)).ToInt_RoundToNearest()
);
// Starting node index of the current segment
size_t curSegmentStartIndex = 0;
for (size_t k = 1; k < m_Path[index].size(); ++k)
{
// Grab tile indices for this coord
int i = (fixed::FromFloat(m_Path[index][k].X / cellSize)).ToInt_RoundToNearest();
int j = (fixed::FromFloat(m_Path[index][k].Y / cellSize)).ToInt_RoundToNearest();
bool nodeVisible = losQuerier.IsExplored(i, j);
if (nodeVisible != lastVisible)
{
// Visibility changed; write out the segment that was just completed and get ready for the new one
out.push_back(SVisibilitySegment(lastVisible, curSegmentStartIndex, k - 1));
curSegmentStartIndex = k - 1;
lastVisible = nodeVisible;
}
}
// Terminate the last segment
out.push_back(SVisibilitySegment(lastVisible, curSegmentStartIndex, m_Path[index].size() - 1));
MergeVisibilitySegments(out);
}
void CCmpRallyPointRenderer::MergeVisibilitySegments(std::vector<SVisibilitySegment>& segments)
{
// Scan for single-point segments; if they are inbetween two other segments, delete them and merge the surrounding segments.
// If they're at either end of the path, include them in their bordering segment (but only if those bordering segments aren't
// themselves single-point segments, because then we would want those to get absorbed by its surrounding ones first).
// First scan for absorptions of single-point surrounded segments (i.e. excluding edge segments)
size_t numSegments = segments.size();
// WARNING: FOR LOOP TRICKERY AHEAD!
for (size_t i = 1; i < numSegments - 1;)
{
SVisibilitySegment& segment = segments[i];
if (segment.IsSinglePoint())
{
// Since the segments' visibility alternates, the surrounding ones should have the same visibility
ENSURE(segments[i-1].m_Visible == segments[i+1].m_Visible);
// Make previous segment span all the way across to the next
segments[i-1].m_EndIndex = segments[i+1].m_EndIndex;
// Erase this segment
segments.erase(segments.begin() + i);
// And the next (we removed [i], so [i+1] is now at position [i])
segments.erase(segments.begin() + i);
// We removed 2 segments, so update the loop condition
numSegments -= 2;
// In the next iteration, i should still point to the segment right after the one that got expanded, which is now
// at position i; so don't increment i here
}
else
{
++i;
}
}
ENSURE(numSegments == segments.size());
// Check to see if the first segment needs to be merged with its neighbour
if (segments.size() >= 2 && segments[0].IsSinglePoint())
{
int firstSegmentStartIndex = segments.front().m_StartIndex;
ENSURE(firstSegmentStartIndex == 0);
// At this point, the second segment should never be a single-point segment
ENSURE(!segments[1].IsSinglePoint());
segments.erase(segments.begin());
segments.front().m_StartIndex = firstSegmentStartIndex;
}
// check to see if the last segment needs to be merged with its neighbour
if (segments.size() >= 2 && segments[segments.size()-1].IsSinglePoint())
{
int lastSegmentEndIndex = segments.back().m_EndIndex;
// At this point, the second-to-last segment should never be a single-point segment
ENSURE(!segments[segments.size()-2].IsSinglePoint());
segments.pop_back();
segments.back().m_EndIndex = lastSegmentEndIndex;
}
// --------------------------------------------------------------------------------------------------------
// At this point, every segment should have at least 2 points
for (size_t i = 0; i < segments.size(); ++i)
{
ENSURE(!segments[i].IsSinglePoint());
ENSURE(segments[i].m_EndIndex > segments[i].m_StartIndex);
}
}
void CCmpRallyPointRenderer::RenderSubmit(SceneCollector& collector, const CFrustum& frustum, bool culling)
{
// We only get here if the rally point is set and should be displayed
for(std::vector<SOverlayTexturedLine>& row : m_TexturedOverlayLines)
for (SOverlayTexturedLine& col : row) {
if (col.m_Coords.empty())
continue;
if (culling && !col.IsVisibleInFrustum(frustum))
continue;
collector.Submit(&col);
}
if (m_EnableDebugNodeOverlay && !m_DebugNodeOverlays.empty())
{
for (std::vector<SOverlayLine>& row : m_DebugNodeOverlays)
for (SOverlayLine& col : row)
if (!col.m_Coords.empty())
collector.Submit(&col);
}
}
void CCmpRallyPointRenderer::AddPosition_wrapper(const CFixedVector2D& pos)
{
AddPosition(pos, false);
}
void CCmpRallyPointRenderer::SetPosition(const CFixedVector2D& pos)
{
if (!(m_RallyPoints.size() == 1 && m_RallyPoints.front() == pos))
{
m_RallyPoints.clear();
AddPosition(pos, true);
// Don't need to UpdateMessageSubscriptions here since AddPosition already calls it
}
}
void CCmpRallyPointRenderer::UpdatePosition(u32 rallyPointId, const CFixedVector2D& pos)
{
if (rallyPointId >= m_RallyPoints.size())
return;
m_RallyPoints[rallyPointId] = pos;
UpdateMarkers();
// Compute a new path for the current, and if existing the next rally point
RecomputeRallyPointPath_wrapper(rallyPointId);
if (rallyPointId + 1 < m_RallyPoints.size())
RecomputeRallyPointPath_wrapper(rallyPointId + 1);
}
void CCmpRallyPointRenderer::SetDisplayed(bool displayed)
{
if (m_Displayed != displayed)
{
m_Displayed = displayed;
// Set color after all dependent components are deserialized
if (displayed && m_LineColor.r < 0)
{
UpdateLineColor();
ConstructAllOverlayLines();
}
// Move the markers out of oblivion and back into the real world, or vice-versa
UpdateMarkers();
// Check for changes to the SoD and update the overlay lines accordingly. We need to do this here because this method
// only takes effect when the display flag is active; we need to pick up changes to the SoD that might have occurred
// while this rally point was not being displayed.
UpdateOverlayLines();
UpdateMessageSubscriptions();
}
}
void CCmpRallyPointRenderer::Reset()
{
for (entity_id_t& componentId : m_MarkerEntityIds)
{
if (componentId != INVALID_ENTITY)
{
GetSimContext().GetComponentManager().DestroyComponentsSoon(componentId);
componentId = INVALID_ENTITY;
}
}
m_RallyPoints.clear();
m_MarkerEntityIds.clear();
m_LastOwner = INVALID_PLAYER;
m_LastMarkerCount = 0;
RecomputeAllRallyPointPaths();
UpdateMessageSubscriptions();
}
void CCmpRallyPointRenderer::UpdateColor()
{
UpdateLineColor();
ConstructAllOverlayLines();
}
void CCmpRallyPointRenderer::AddPosition(CFixedVector2D pos, bool recompute)
{
m_RallyPoints.push_back(pos);
UpdateMarkers();
if (recompute)
RecomputeAllRallyPointPaths();
else
RecomputeRallyPointPath_wrapper(m_RallyPoints.size() - 1);
UpdateMessageSubscriptions();
}
bool CCmpRallyPointRenderer::IsSet() const
{
return !m_RallyPoints.empty();
}
|