Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): cache defence reach drawing, shade a selected defence

Olcay Seygane1340730bfa0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 580c06b35..49e5893e6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -415,7 +415,9 @@ found and fixed — EA's own, not port damage.**
- The pointer keeps its build cursor while you place, even passing over your own buildings.
- GLA defences get a range ring while you site them.
- The ground a defence on your cursor can shoot into is painted red. It used to be the thin white guard circle, which read as the edge of a building's footprint more than as a field of fire; now it is a thin line in the owner's colour, and the line sits exactly where the game stops allowing the shot: the weapon's range measured from the edge of the building, not from its middle. While any building is on your cursor, or when you click a turret, every armed building you can see shows its reach as well, yours, your allies' and the enemy's, and one player's overlapping circles join into one outline, so the gap between two Patriot batteries is something you can see before you pay for the third. An enemy defence hidden under fog stays hidden.
-- Inside that red area, the ground the defence cannot see is shaded. A Patriot battery, a Gattling Cannon or a Fire Base does not shoot through a building - the game refuses the shot when a solid structure stands between the gun and its target - so from where your cursor is, each building in range throws a shadow that starts at its near wall and runs out to the edge of the reach, a wedge that swings as you move the defence round the corner of your War Factory. Hills count as well. The game will not pick a target when the ground rises between the top of the gun and the top of a tank, so the far side of a ridge goes dark too, and a valley beyond it lights up again where the gun can see down into it. The shade lies on the ground as one piece and follows its slopes. A corner one of your turrets, or an ally's, already sees into is not a hole, so it stays bright: the shade is only what nothing of yours can shoot. A Stinger Site or a bunker gets no shade, because a building in the way does not stop them.
+- Inside that red area, the ground the defence cannot see is shaded. A Patriot battery, a Gattling Cannon or a Fire Base does not shoot through a building - the game refuses the shot when a solid structure stands between the gun and its target - so from where your cursor is, each building in range throws a shadow that starts at its near wall and runs out to the edge of the reach, a wedge that swings as you move the defence round the corner of your War Factory. Hills count as well. The game will not pick a target when the ground rises between the top of the gun and the top of a tank, so the far side of a ridge goes dark too, and a valley beyond it lights up again where the gun can see down into it. The shade lies on the ground as one piece and follows its slopes. A corner one of your turrets, or an ally's, already sees into is not a hole, so it stays bright: the shade is only what nothing of yours can shoot. A Stinger Site or a bunker gets no shade, because a building in the way does not stop them. Click a defence that is already standing and it shows the same shade from where it stands, its own and nobody else's, so the corner behind your War Factory is there to see after the turret is built too.
+- Clicking a turret in a base full of them no longer drags the game down. Every circle's outline was worked out again against every other circle on every frame, so with 120 Patriot batteries on the map one selected battery took the game from 94 frames a second to 53. The outlines are worked out once and kept until a building goes up, comes down or comes out of the fog: the same base now holds 83 with the battery selected against 84 with nothing selected.
+- Selecting a GLA supply stash no longer puts reach circles round every defence on the map. The stash spawns workers, a worker carries a weapon for clearing mines, and that was enough to count the stash as armed.
- Two buildings can no longer be put down on the same spot online. A build order travels to the other players before anything appears on the ground, and every click made while it was in flight was answered by a map that still showed the square as empty - so a shift-held row on a bad connection came out as buildings standing inside each other. The ghost now knows about the orders you have already spent, and turns red over them or slides to the square beside; and the order itself is checked again where it lands, on every machine, after the first building is standing. A second one aimed at the same ground is refused there and costs you nothing.
- The right button drops the building on your cursor whether the click travelled or not. Sending a dozer off somewhere in the middle of a shift-held row moved it and left the ghost riding the cursor, because the cancel only counted a press that had not moved a pixel and a hand on the way to a destination always moves a few.
- A click clears a half-typed building shortcut.
diff --git a/GeneralsMD/Code/Data/Scenarios/reachshot.txt b/GeneralsMD/Code/Data/Scenarios/reachshot.txt
new file mode 100644
index 000000000..569ca67c8
--- /dev/null
+++ b/GeneralsMD/Code/Data/Scenarios/reachshot.txt
@@ -0,0 +1,6 @@
+# A selected defence and the cost of drawing its reach. One Patriot battery dead centre of
+# -camera 760 920, 120 more in a grid to the north, in Alpine Assault's rail yard where the rock and
+# the yard buildings throw blind spots. docs/reference/UI-MAP.md has the launch line and the click
+# that selects the battery without moving the real mouse.
+30 spawn 0 AmericaPatriotBattery 1 760 920
+30 spawn 0 AmericaPatriotBattery 120 760 1500 70
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 8298fbc92..da59d8710 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -1180,7 +1180,7 @@ protected:
Bool m_placementRangeRingUp; ///< the structure on the cursor is armed, so its reach is drawn
Real m_placementRingRadius; ///< how far from its centre it hits
void drawPlacementReach( void ); ///< while placing, the reach of every armed building in sight, as one outline
- void drawPlacementBlindSpots( void ); ///< shade the ground behind buildings a placed defence cannot shoot past
+ void drawBlindSpots( void ); ///< shade the ground a placed or selected defence cannot shoot into
DisplayString * m_hudDisplayString; ///< the ShowHudOverlay line (fps / clock)
DisplayString * m_peaceTimeDisplayString; ///< the peace time clock at the top of the screen
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index e32d9744c..024393a2f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1653,13 +1653,19 @@ static Real templateWeaponRange( const ThingTemplate *tmpl )
* Half the GLA's defences carry no gun at all: a stinger site is an empty shell with a
* SpawnBehavior that keeps three stinger soldiers alive next to it, and the soldiers own the
* missiles. Judged by its own template the site is unarmed, so no ring was ever drawn for the
- * one faction whose defences most need siting. */
+ * one faction whose defences most need siting.
+ *
+ * Only a structure the INI marks SPAWNS_ARE_THE_WEAPONS counts its spawns. A GLA supply stash
+ * spawns workers too, and a worker carries a mine-disarming weapon, so reading every spawner's
+ * spawns put a reach circle round the stash. */
//-------------------------------------------------------------------------------------------------
static Real templatePlacementRange( const ThingTemplate *tmpl )
{
Real range = templateWeaponRange( tmpl );
if( range > 0.0f || tmpl == NULL || TheThingFactory == NULL )
return range;
+ if( !tmpl->isKindOf( KINDOF_SPAWNS_ARE_THE_WEAPONS ) )
+ return range;
const ModuleInfo& modules = tmpl->getBehaviorModuleInfo();
for( Int i = 0; i < modules.getCount(); i++ )
@@ -2057,117 +2063,109 @@ static Bool reachViewHits( const ReachView &view, Real x, Real y )
}
//-------------------------------------------------------------------------------------------------
-/** The ground a defence on the cursor could not shoot into, darkened inside its red range area.
- *
- * A defence that needs a line of sight - a Patriot battery, a Gattling Cannon, a Fire Base - does
- * not fire through a building, and cannot pick a target a hill hides from it. Both rules are the
- * game's own. Target picking asks the terrain for a clear line from the top of the defence to the
- * target, and the pathfinder refuses a shot when a solid structure's cells lie between them; a
- * structure the art says can be seen through does not count.
- *
- * The range area is cut into a polar grid, a sector per ray and a ring per half pathfind cell, and
- * each cell of it is looked at from the defence. Along one sector the walk keeps the steepest
- * terrain seen so far, which is the horizon: a target whose top sits under that slope is behind a
- * hill. Everything from the first building cell outwards is behind that building, the building's
- * own ground included. The blocked cells are projected corner by corner onto the terrain and
- * filled as one shape, row by row, so the shade follows the ground and has no seams in it.
- *
- * A Stinger Site, a bunker and anything else that does not need the line of sight gets no shading,
- * because none of that ground is out of its reach.
- *
- * Ground the defence cannot see but one of the player's or an ally's defences already hits is left
- * bright: a turret beside the building covers the corner behind it, and that corner is not a hole.
- * Each of those defences gets the same polar grid from where it stands, with its own cells not
- * counted as a building in its way, and the shaded cell's middle is looked up in it. */
+/** Every structure on the map as the reach drawing sees it, gathered once a frame. A building going
+ * up or coming down changes what a defence can see past, and one coming out of the fog changes which
+ * circles are drawn, so every cache below is thrown away when this list differs from the last
+ * frame's. Working the circles and the blind spots out again every frame is what made clicking a
+ * turret in a base of forty defences slow: each circle's outline was tested against every other
+ * circle, every frame. */
//-------------------------------------------------------------------------------------------------
-void InGameUI::drawPlacementBlindSpots( void )
+struct StructureKey
{
- if( m_pendingPlaceType == NULL || !m_placementRangeRingUp )
- return;
- if( !templateNeedsLineOfSight( m_pendingPlaceType ) )
- return;
+ ObjectID id;
+ Coord3D position;
+ const Player *owner;
+ Bool reachShown; ///< armed and not hidden from the local player, so its circle is drawn
- const Coord3D center = *m_placeIcon[ 0 ]->getPosition();
- ReachView pending;
- pending.center.x = center.x;
- pending.center.y = center.y;
- pending.center.z = TheTerrainLogic->getGroundHeight( center.x, center.y );
- traceReachView( pending, m_pendingPlaceType );
- const Real eyeZ = pending.center.z + m_pendingPlaceType->getTemplateGeometryInfo().getMaxHeightAbovePosition();
- lookRoundReach( pending, eyeZ, INVALID_ID );
+ Bool operator==( const StructureKey &other ) const
+ {
+ return id == other.id && position.x == other.position.x && position.y == other.position.y
+ && position.z == other.position.z && owner == other.owner && reachShown == other.reachShown;
+ }
+};
- const Real range = pending.radius;
- const Int rings = pending.rings;
- std::vector< Bool > &blocked = pending.blocked;
+static std::vector< StructureKey > theStructureKeys;
+static UnsignedInt theStructureKeysFrame = 0;
+static Int theStructureGeneration = 0; ///< goes up every time theStructureKeys changes
+
+static void refreshStructureKeys( void )
+{
+ const UnsignedInt frame = TheGameClient->getFrame();
+ if( frame == theStructureKeysFrame && !theStructureKeys.empty() )
+ return;
+ theStructureKeysFrame = frame;
- std::vector< ReachView > guards;
const Player *local = ThePlayerList->getLocalPlayer();
+ std::vector< StructureKey > keys;
+ keys.reserve( theStructureKeys.size() );
for( Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject() )
{
if( !obj->isKindOf( KINDOF_STRUCTURE ) )
continue;
- if( obj->getControllingPlayer() != local && local->getRelationship( obj->getTeam() ) != ALLIES )
- continue;
- const Real flatReach = templateReach( obj->getTemplate() );
- const Real furthest = flatReach + Weapon_elevationRangeBonus( templatePlacementRange( obj->getTemplate() ), FLT_MAX );
- const Coord3D *position = obj->getPosition();
- if( flatReach <= 0.0f || sqr( position->x - center.x ) + sqr( position->y - center.y ) >= sqr( furthest + range ) )
- continue;
-
- ReachView guard;
- guard.center = *position;
- guard.rings = 0;
- traceReachView( guard, obj->getTemplate() );
-
- if( templateNeedsLineOfSight( obj->getTemplate() ) )
- lookRoundReach( guard, obj->getPosition()->z + obj->getGeometryInfo().getMaxHeightAbovePosition(), obj->getID() );
- guards.push_back( guard );
+ StructureKey key;
+ key.id = obj->getID();
+ key.position = *obj->getPosition();
+ key.owner = obj->getControllingPlayer();
+ key.reachShown = templateReach( obj->getTemplate() ) > 0.0f
+ && ( key.owner == local || obj->getShroudedStatus( local->getPlayerIndex() ) < OBJECTSHROUD_FOGGED );
+ keys.push_back( key );
}
- Bool anyBlocked = FALSE;
- for( Int ray = 0; ray < BLIND_SPOT_RAYS; ray++ )
- {
- const Real angle = 2.0f * PI * ( ray + 0.5f ) / BLIND_SPOT_RAYS;
- for( Int ring = 0; ring < rings; ring++ )
- {
- const Int index = ray * rings + ring;
- if( !blocked[ index ] )
- continue;
+ if( keys == theStructureKeys )
+ return;
+ theStructureKeys.swap( keys );
+ ++theStructureGeneration;
+}
- const Real along = min( ( ring + 0.5f ) * BLIND_SPOT_RING_WIDTH, range );
- const Real x = center.x + Cos( angle ) * along;
- const Real y = center.y + Sin( angle ) * along;
- for( size_t g = 0; g < guards.size() && blocked[ index ]; g++ )
- {
- if( reachViewHits( guards[ g ], x, y ) )
- blocked[ index ] = FALSE;
- }
- anyBlocked = anyBlocked || blocked[ index ];
- }
- }
+//-------------------------------------------------------------------------------------------------
+/** A defence's polar grid with the ground under every corner of it looked up: corner ( ray, ring )
+ * sits on the ray's leading edge at the ring's inner radius. */
+//-------------------------------------------------------------------------------------------------
+struct BlindSpotShade
+{
+ ReachView view;
+ std::vector< Coord3D > corners;
+};
- if( !anyBlocked )
- return;
+static void buildBlindSpotShade( BlindSpotShade &shade, const ThingTemplate *tmpl, const Coord3D &center, Real eyeZ, ObjectID self )
+{
+ shade.view.center = center;
+ traceReachView( shade.view, tmpl );
+ lookRoundReach( shade.view, eyeZ, self );
- // corner ( ray, ring ) sits on the ray's leading edge at the ring's inner radius
- const Int cornerRings = rings + 1;
- std::vector< ICoord2D > corners( BLIND_SPOT_RAYS * cornerRings );
- std::vector< Bool > onScreen( BLIND_SPOT_RAYS * cornerRings );
+ const Int cornerRings = shade.view.rings + 1;
+ shade.corners.resize( BLIND_SPOT_RAYS * cornerRings );
for( Int ray = 0; ray < BLIND_SPOT_RAYS; ray++ )
{
const Real angle = 2.0f * PI * ray / BLIND_SPOT_RAYS;
for( Int ring = 0; ring < cornerRings; ring++ )
{
- const Real along = min( ring * BLIND_SPOT_RING_WIDTH, range );
- Coord3D ground;
+ const Real along = min( ring * BLIND_SPOT_RING_WIDTH, shade.view.radius );
+ Coord3D &ground = shade.corners[ ray * cornerRings + ring ];
ground.x = center.x + Cos( angle ) * along;
ground.y = center.y + Sin( angle ) * along;
ground.z = TheTerrainLogic->getGroundHeight( ground.x, ground.y );
- const Int index = ray * cornerRings + ring;
- onScreen[ index ] = TheTacticalView->worldToScreenTriReturn( &ground, &corners[ index ] ) != View::WTS_INVALID;
}
}
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The blocked cells of a shade projected corner by corner and filled as one shape, row by row, so
+ * the shade follows the ground and has no seams in it. */
+//-------------------------------------------------------------------------------------------------
+static void fillBlindSpotShade( const BlindSpotShade &shade )
+{
+ const ReachView &view = shade.view;
+ if( std::find( view.blocked.begin(), view.blocked.end(), TRUE ) == view.blocked.end() )
+ return;
+
+ // only the corners of blocked cells are projected, each once
+ enum { CORNER_UNPROJECTED, CORNER_ON_SCREEN, CORNER_OFF_SCREEN };
+ const Int rings = view.rings;
+ const Int cornerRings = rings + 1;
+ std::vector< ICoord2D > corners( shade.corners.size() );
+ std::vector< UnsignedByte > projected( shade.corners.size(), CORNER_UNPROJECTED );
std::vector< std::vector< ICoord2D > > rows( TheDisplay->getHeight() );
for( Int ray = 0; ray < BLIND_SPOT_RAYS; ray++ )
@@ -2175,12 +2173,23 @@ void InGameUI::drawPlacementBlindSpots( void )
const Int next = ( ray + 1 ) % BLIND_SPOT_RAYS;
for( Int ring = 0; ring < rings; ring++ )
{
- if( !blocked[ ray * rings + ring ] )
+ if( !view.blocked[ ray * rings + ring ] )
continue;
const Int quad[ 4 ] = { ray * cornerRings + ring, next * cornerRings + ring,
next * cornerRings + ring + 1, ray * cornerRings + ring + 1 };
- if( !onScreen[ quad[ 0 ] ] || !onScreen[ quad[ 1 ] ] || !onScreen[ quad[ 2 ] ] || !onScreen[ quad[ 3 ] ] )
+ Bool allOnScreen = TRUE;
+ for( Int q = 0; q < 4; q++ )
+ {
+ UnsignedByte &state = projected[ quad[ q ] ];
+ if( state == CORNER_UNPROJECTED )
+ {
+ const Bool onScreen = TheTacticalView->worldToScreenTriReturn( &shade.corners[ quad[ q ] ], &corners[ quad[ q ] ] ) != View::WTS_INVALID;
+ state = onScreen ? CORNER_ON_SCREEN : CORNER_OFF_SCREEN;
+ }
+ allOnScreen = allOnScreen && state == CORNER_ON_SCREEN;
+ }
+ if( !allOnScreen )
continue;
const ICoord2D *quadCorners[ 4 ] = { &corners[ quad[ 0 ] ], &corners[ quad[ 1 ] ], &corners[ quad[ 2 ] ], &corners[ quad[ 3 ] ] };
@@ -2192,15 +2201,154 @@ void InGameUI::drawPlacementBlindSpots( void )
}
//-------------------------------------------------------------------------------------------------
-/** Where a ground point lands on the screen, FALSE when the camera cannot put it anywhere. */
+/** What each of the player's and the allies' defences hits from where it stands, kept until a
+ * structure somewhere changes. */
+//-------------------------------------------------------------------------------------------------
+static std::map< ObjectID, ReachView > theGuardViews;
+static Int theGuardViewsGeneration = -1;
+
+static const ReachView &guardView( const Object *obj )
+{
+ if( theGuardViewsGeneration != theStructureGeneration )
+ {
+ theGuardViews.clear();
+ theGuardViewsGeneration = theStructureGeneration;
+ }
+
+ std::map< ObjectID, ReachView >::iterator found = theGuardViews.find( obj->getID() );
+ if( found != theGuardViews.end() )
+ return found->second;
+
+ ReachView &guard = theGuardViews[ obj->getID() ];
+ guard.center = *obj->getPosition();
+ guard.rings = 0;
+ traceReachView( guard, obj->getTemplate() );
+ if( templateNeedsLineOfSight( obj->getTemplate() ) )
+ lookRoundReach( guard, obj->getPosition()->z + obj->getGeometryInfo().getMaxHeightAbovePosition(), obj->getID() );
+ return guard;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Ground a defence on the cursor cannot see but one of the player's or an ally's defences already
+ * hits is left bright: a turret beside the building covers the corner behind it, and that corner is
+ * not a hole. Each of those defences has the same polar grid from where it stands, with its own
+ * cells not counted as a building in its way, and the shaded cell's middle is looked up in it. */
+//-------------------------------------------------------------------------------------------------
+static void clearGuardedBlindSpots( ReachView &pending )
+{
+ const Player *local = ThePlayerList->getLocalPlayer();
+ std::vector< const ReachView * > guards;
+ for( size_t k = 0; k < theStructureKeys.size(); k++ )
+ {
+ const StructureKey &key = theStructureKeys[ k ];
+ const Object *obj = TheGameLogic->findObjectByID( key.id );
+ if( key.owner != local && local->getRelationship( obj->getTeam() ) != ALLIES )
+ continue;
+
+ const Real flatReach = templateReach( obj->getTemplate() );
+ const Real furthest = flatReach + Weapon_elevationRangeBonus( templatePlacementRange( obj->getTemplate() ), FLT_MAX );
+ if( flatReach <= 0.0f || sqr( key.position.x - pending.center.x ) + sqr( key.position.y - pending.center.y ) >= sqr( furthest + pending.radius ) )
+ continue;
+
+ guards.push_back( &guardView( obj ) );
+ }
+
+ for( Int ray = 0; ray < BLIND_SPOT_RAYS; ray++ )
+ {
+ const Real angle = 2.0f * PI * ( ray + 0.5f ) / BLIND_SPOT_RAYS;
+ for( Int ring = 0; ring < pending.rings; ring++ )
+ {
+ const Int index = ray * pending.rings + ring;
+ if( !pending.blocked[ index ] )
+ continue;
+
+ const Real along = min( ( ring + 0.5f ) * BLIND_SPOT_RING_WIDTH, pending.radius );
+ const Real x = pending.center.x + Cos( angle ) * along;
+ const Real y = pending.center.y + Sin( angle ) * along;
+ for( size_t g = 0; g < guards.size() && pending.blocked[ index ]; g++ )
+ {
+ if( reachViewHits( *guards[ g ], x, y ) )
+ pending.blocked[ index ] = FALSE;
+ }
+ }
+ }
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A selected defence's blind spots, worked out once and kept until a structure somewhere changes. */
+//-------------------------------------------------------------------------------------------------
+static std::map< ObjectID, BlindSpotShade > theSelectedShades;
+static Int theSelectedShadesGeneration = -1;
+
+static const BlindSpotShade &selectedBlindSpotShade( const Object *obj )
+{
+ if( theSelectedShadesGeneration != theStructureGeneration )
+ {
+ theSelectedShades.clear();
+ theSelectedShadesGeneration = theStructureGeneration;
+ }
+
+ std::map< ObjectID, BlindSpotShade >::iterator found = theSelectedShades.find( obj->getID() );
+ if( found != theSelectedShades.end() )
+ return found->second;
+
+ BlindSpotShade &shade = theSelectedShades[ obj->getID() ];
+ const Coord3D *position = obj->getPosition();
+ buildBlindSpotShade( shade, obj->getTemplate(), *position,
+ position->z + obj->getGeometryInfo().getMaxHeightAbovePosition(), obj->getID() );
+ return shade;
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The ground a defence could not shoot into, darkened inside its reach: the one on the cursor while
+ * it is being sited, and every selected one.
+ *
+ * A defence that needs a line of sight - a Patriot battery, a Gattling Cannon, a Fire Base - does
+ * not fire through a building, and cannot pick a target a hill hides from it. Both rules are the
+ * game's own. Target picking asks the terrain for a clear line from the top of the defence to the
+ * target, and the pathfinder refuses a shot when a solid structure's cells lie between them; a
+ * structure the art says can be seen through does not count.
+ *
+ * The range area is cut into a polar grid, a sector per ray and a ring per half pathfind cell, and
+ * each cell of it is looked at from the defence. Along one sector the walk keeps the steepest
+ * terrain seen so far, which is the horizon: a target whose top sits under that slope is behind a
+ * hill. Everything from the first building cell outwards is behind that building, the building's
+ * own ground included. The blocked cells are projected corner by corner onto the terrain and
+ * filled as one shape, row by row, so the shade follows the ground and has no seams in it.
+ *
+ * A Stinger Site, a bunker and anything else that does not need the line of sight gets no shading,
+ * because none of that ground is out of its reach. */
//-------------------------------------------------------------------------------------------------
-static Bool projectGroundPoint( Real x, Real y, ICoord2D *screen )
+void InGameUI::drawBlindSpots( void )
{
- Coord3D ground;
- ground.x = x;
- ground.y = y;
- ground.z = TheTerrainLogic->getGroundHeight( x, y );
- return TheTacticalView->worldToScreenTriReturn( &ground, screen ) != View::WTS_INVALID;
+ if( m_pendingPlaceType != NULL && m_placementRangeRingUp && templateNeedsLineOfSight( m_pendingPlaceType ) )
+ {
+ refreshStructureKeys();
+ Coord3D center = *m_placeIcon[ 0 ]->getPosition();
+ center.z = TheTerrainLogic->getGroundHeight( center.x, center.y );
+ const Real eyeZ = center.z + m_pendingPlaceType->getTemplateGeometryInfo().getMaxHeightAbovePosition();
+
+ BlindSpotShade pending;
+ buildBlindSpotShade( pending, m_pendingPlaceType, center, eyeZ, INVALID_ID );
+ clearGuardedBlindSpots( pending.view );
+ fillBlindSpotShade( pending );
+ }
+
+ if( !TheGlobalData->m_showPlacementRangeRing )
+ return;
+
+ // a selected defence shows its own blind spots only, not what its neighbours cover for it
+ for( DrawableListCIt it = m_selectedDrawables.begin(); it != m_selectedDrawables.end(); ++it )
+ {
+ const Object *obj = (*it)->getObject();
+ if( obj == NULL || !obj->isKindOf( KINDOF_STRUCTURE ) || templateReach( obj->getTemplate() ) <= 0.0f )
+ continue;
+ if( !templateNeedsLineOfSight( obj->getTemplate() ) )
+ continue;
+
+ refreshStructureKeys();
+ fillBlindSpotShade( selectedBlindSpotShade( obj ) );
+ }
}
struct ReachCircle
@@ -2233,27 +2381,154 @@ static Real reachAtAngle( const ReachCircle &circle, Real angle )
return circle.outline[ from ] * ( 1.0f - part ) + circle.outline[ ( from + 1 ) % REACH_OUTLINE_SEGMENTS ] * part;
}
-/// inside another circle of the same player's; two players' circles cross, each in its own colour
-static Bool insideOtherReach( const std::vector< ReachCircle > &circles, size_t self, Real x, Real y )
+static Bool insideReach( const ReachCircle &circle, Real x, Real y )
+{
+ const Real dx = x - circle.center.x;
+ const Real dy = y - circle.center.y;
+ const Real distanceSqr = sqr( dx ) + sqr( dy );
+ if( distanceSqr >= sqr( circle.radius ) )
+ return FALSE;
+
+ Real angle = atan2( dy, dx );
+ if( angle < 0.0f )
+ angle += 2.0f * PI;
+ return distanceSqr < sqr( reachAtAngle( circle, angle ) );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** What hides a stretch of one player's outline: another of that player's circles in circles, or
+ * extra when it is that player's. Two players' circles cross, each in its own colour. */
+//-------------------------------------------------------------------------------------------------
+struct ReachCover
{
- for( size_t c = 0; c < circles.size(); c++ )
+ const std::vector< ReachCircle > *circles; ///< NULL when only extra covers
+ size_t self; ///< the circle being cut, which never covers itself
+ const ReachCircle *extra; ///< NULL when there is none
+ const Player *owner;
+
+ Bool covers( Real x, Real y ) const
{
- if( c == self || circles[ c ].owner != circles[ self ].owner )
- continue;
+ for( size_t c = 0; circles != NULL && c < circles->size(); c++ )
+ {
+ const ReachCircle &other = (*circles)[ c ];
+ if( c != self && other.owner == owner && insideReach( other, x, y ) )
+ return TRUE;
+ }
+ return extra != NULL && extra->owner == owner && insideReach( *extra, x, y );
+ }
+};
+
+struct ReachSegment
+{
+ size_t circle;
+ Real angles[ 2 ];
+ Coord3D ends[ 2 ]; ///< on the ground
+};
+
+//-------------------------------------------------------------------------------------------------
+/** One stretch of a circle's outline between two angles, cut where it runs under cover: with both
+ * ends covered it is dropped, with one it walks the arc in halves to where it crosses and keeps the
+ * outside. */
+//-------------------------------------------------------------------------------------------------
+static void clipReachSegment( const ReachCircle &circle, size_t index, Real fromAngle, Real toAngle,
+ const ReachCover &cover, std::vector< ReachSegment > &out )
+{
+ ReachSegment segment;
+ segment.circle = index;
+ segment.angles[ 0 ] = fromAngle;
+ segment.angles[ 1 ] = toAngle;
+
+ Bool inside[ 2 ];
+ for( Int e = 0; e < 2; e++ )
+ {
+ const Real reach = reachAtAngle( circle, segment.angles[ e ] );
+ inside[ e ] = cover.covers( circle.center.x + Cos( segment.angles[ e ] ) * reach, circle.center.y + Sin( segment.angles[ e ] ) * reach );
+ }
+ if( inside[ 0 ] && inside[ 1 ] )
+ return;
+
+ if( inside[ 0 ] != inside[ 1 ] )
+ {
+ const Int cut = inside[ 0 ] ? 0 : 1;
+ Real outsideAngle = segment.angles[ 1 - cut ];
+ Real insideAngle = segment.angles[ cut ];
+ for( Int halving = 0; halving < REACH_CROSSING_HALVINGS; halving++ )
+ {
+ const Real middle = 0.5f * ( outsideAngle + insideAngle );
+ const Real middleReach = reachAtAngle( circle, middle );
+ if( cover.covers( circle.center.x + Cos( middle ) * middleReach, circle.center.y + Sin( middle ) * middleReach ) )
+ insideAngle = middle;
+ else
+ outsideAngle = middle;
+ }
+ segment.angles[ cut ] = outsideAngle;
+ }
+
+ for( Int e = 0; e < 2; e++ )
+ {
+ const Real reach = reachAtAngle( circle, segment.angles[ e ] );
+ segment.ends[ e ].x = circle.center.x + Cos( segment.angles[ e ] ) * reach;
+ segment.ends[ e ].y = circle.center.y + Sin( segment.angles[ e ] ) * reach;
+ segment.ends[ e ].z = TheTerrainLogic->getGroundHeight( segment.ends[ e ].x, segment.ends[ e ].y );
+ }
+ out.push_back( segment );
+}
+
+static void outlineReachCircle( const std::vector< ReachCircle > &circles, size_t index, const ReachCover &cover,
+ std::vector< ReachSegment > &out )
+{
+ for( Int segment = 0; segment < REACH_OUTLINE_SEGMENTS; segment++ )
+ {
+ clipReachSegment( circles[ index ], index, 2.0f * PI * segment / REACH_OUTLINE_SEGMENTS,
+ 2.0f * PI * ( segment + 1 ) / REACH_OUTLINE_SEGMENTS, cover, out );
+ }
+}
- const Real dx = x - circles[ c ].center.x;
- const Real dy = y - circles[ c ].center.y;
- const Real distanceSqr = sqr( dx ) + sqr( dy );
- if( distanceSqr >= sqr( circles[ c ].radius ) )
+//-------------------------------------------------------------------------------------------------
+/** The outlines of every armed building in sight, cut against each other, kept until a structure
+ * somewhere changes. Only the circle on the cursor is worked out every frame. */
+//-------------------------------------------------------------------------------------------------
+static std::vector< ReachCircle > theReachCircles;
+static std::vector< ReachSegment > theReachSegments;
+static Int theReachGeneration = -1;
+
+static void refreshReachOutlines( void )
+{
+ if( theReachGeneration == theStructureGeneration )
+ return;
+ theReachGeneration = theStructureGeneration;
+
+ theReachCircles.clear();
+ theReachSegments.clear();
+ for( size_t k = 0; k < theStructureKeys.size(); k++ )
+ {
+ const StructureKey &key = theStructureKeys[ k ];
+ if( !key.reachShown )
continue;
- Real angle = atan2( dy, dx );
- if( angle < 0.0f )
- angle += 2.0f * PI;
- if( distanceSqr < sqr( reachAtAngle( circles[ c ], angle ) ) )
- return TRUE;
+ ReachCircle placed;
+ placed.center = key.position;
+ placed.owner = key.owner;
+ traceReachCircle( placed, TheGameLogic->findObjectByID( key.id )->getTemplate() );
+ theReachCircles.push_back( placed );
}
- return FALSE;
+
+ for( size_t c = 0; c < theReachCircles.size(); c++ )
+ {
+ const ReachCover cover = { &theReachCircles, c, NULL, theReachCircles[ c ].owner };
+ outlineReachCircle( theReachCircles, c, cover, theReachSegments );
+ }
+}
+
+static void drawReachSegment( const ReachSegment &segment, const Player *owner )
+{
+ UnsignedByte red, green, blue, alpha;
+ GameGetColorComponents( clientPlayerColor( owner ), &red, &green, &blue, &alpha );
+
+ ICoord2D from, to;
+ if( TheTacticalView->worldToScreenTriReturn( &segment.ends[ 0 ], &from ) != View::WTS_INVALID
+ && TheTacticalView->worldToScreenTriReturn( &segment.ends[ 1 ], &to ) != View::WTS_INVALID )
+ TheDisplay->drawLine( from.x, from.y, to.x, to.y, REACH_OUTLINE_WIDTH, GameMakeColor( red, green, blue, REACH_OUTLINE_ALPHA ) );
}
//-------------------------------------------------------------------------------------------------
@@ -2282,82 +2557,50 @@ void InGameUI::drawPlacementReach( void )
if( m_pendingPlaceType == NULL && !armedSelected )
return;
- const Player *local = ThePlayerList->getLocalPlayer();
- std::vector< ReachCircle > circles;
- if( m_pendingPlaceType != NULL && m_placementRangeRingUp )
+ refreshStructureKeys();
+ refreshReachOutlines();
+
+ // the circle on the cursor moves every frame, so it alone is cut afresh against the kept ones
+ const Bool pendingUp = m_pendingPlaceType != NULL && m_placementRangeRingUp;
+ std::vector< ReachCircle > pendingCircles;
+ std::vector< ReachSegment > pendingSegments;
+ if( pendingUp )
{
ReachCircle pending;
pending.center.x = m_placeIcon[ 0 ]->getPosition()->x;
pending.center.y = m_placeIcon[ 0 ]->getPosition()->y;
pending.center.z = TheTerrainLogic->getGroundHeight( pending.center.x, pending.center.y );
- pending.owner = local;
+ pending.owner = ThePlayerList->getLocalPlayer();
traceReachCircle( pending, m_pendingPlaceType );
- circles.push_back( pending );
- }
-
- for( Object *obj = TheGameLogic->getFirstObject(); obj; obj = obj->getNextObject() )
- {
- if( !obj->isKindOf( KINDOF_STRUCTURE ) )
- continue;
- if( obj->getControllingPlayer() != local && obj->getShroudedStatus( local->getPlayerIndex() ) >= OBJECTSHROUD_FOGGED )
- continue;
+ pendingCircles.push_back( pending );
- if( templateReach( obj->getTemplate() ) <= 0.0f )
- continue;
- ReachCircle placed;
- placed.center = *obj->getPosition();
- placed.owner = obj->getControllingPlayer();
- traceReachCircle( placed, obj->getTemplate() );
- circles.push_back( placed );
+ const ReachCover cover = { &theReachCircles, theReachCircles.size(), NULL, pending.owner };
+ outlineReachCircle( pendingCircles, 0, cover, pendingSegments );
}
TheDisplay->beginBatch2D();
- for( size_t c = 0; c < circles.size(); c++ )
+ std::vector< ReachSegment > recut;
+ for( size_t s = 0; s < theReachSegments.size(); s++ )
{
- UnsignedByte red, green, blue, alpha;
- GameGetColorComponents( clientPlayerColor( circles[ c ].owner ), &red, &green, &blue, &alpha );
- const Color outline = GameMakeColor( red, green, blue, REACH_OUTLINE_ALPHA );
- const ReachCircle &circle = circles[ c ];
- for( Int segment = 0; segment < REACH_OUTLINE_SEGMENTS; segment++ )
- {
- const Real angles[ 2 ] = { 2.0f * PI * segment / REACH_OUTLINE_SEGMENTS, 2.0f * PI * ( segment + 1 ) / REACH_OUTLINE_SEGMENTS };
- Real ends[ 2 ][ 2 ];
- Bool inside[ 2 ];
- for( Int e = 0; e < 2; e++ )
- {
- const Real reach = reachAtAngle( circle, angles[ e ] );
- ends[ e ][ 0 ] = circle.center.x + Cos( angles[ e ] ) * reach;
- ends[ e ][ 1 ] = circle.center.y + Sin( angles[ e ] ) * reach;
- inside[ e ] = insideOtherReach( circles, c, ends[ e ][ 0 ], ends[ e ][ 1 ] );
- }
- if( inside[ 0 ] && inside[ 1 ] )
- continue;
-
- // one end inside another circle: walk the arc in halves to where it crosses, keep the outside
- if( inside[ 0 ] != inside[ 1 ] )
- {
- Real outsideAngle = inside[ 0 ] ? angles[ 1 ] : angles[ 0 ];
- Real insideAngle = inside[ 0 ] ? angles[ 0 ] : angles[ 1 ];
- for( Int halving = 0; halving < REACH_CROSSING_HALVINGS; halving++ )
- {
- const Real middle = 0.5f * ( outsideAngle + insideAngle );
- const Real middleReach = reachAtAngle( circle, middle );
- if( insideOtherReach( circles, c, circle.center.x + Cos( middle ) * middleReach, circle.center.y + Sin( middle ) * middleReach ) )
- insideAngle = middle;
- else
- outsideAngle = middle;
- }
- const Int cut = inside[ 0 ] ? 0 : 1;
- const Real cutReach = reachAtAngle( circle, outsideAngle );
- ends[ cut ][ 0 ] = circle.center.x + Cos( outsideAngle ) * cutReach;
- ends[ cut ][ 1 ] = circle.center.y + Sin( outsideAngle ) * cutReach;
- }
-
- ICoord2D from, to;
- if( projectGroundPoint( ends[ 0 ][ 0 ], ends[ 0 ][ 1 ], &from ) && projectGroundPoint( ends[ 1 ][ 0 ], ends[ 1 ][ 1 ], &to ) )
- TheDisplay->drawLine( from.x, from.y, to.x, to.y, REACH_OUTLINE_WIDTH, outline );
+ const ReachSegment &segment = theReachSegments[ s ];
+ const ReachCircle &circle = theReachCircles[ segment.circle ];
+ const Bool underPending = pendingUp && circle.owner == pendingCircles[ 0 ].owner
+ && ( insideReach( pendingCircles[ 0 ], segment.ends[ 0 ].x, segment.ends[ 0 ].y )
+ || insideReach( pendingCircles[ 0 ], segment.ends[ 1 ].x, segment.ends[ 1 ].y ) );
+ if( !underPending )
+ {
+ drawReachSegment( segment, circle.owner );
+ continue;
}
+
+ recut.clear();
+ const ReachCover cover = { NULL, segment.circle, &pendingCircles[ 0 ], circle.owner };
+ clipReachSegment( circle, segment.circle, segment.angles[ 0 ], segment.angles[ 1 ], cover, recut );
+ for( size_t r = 0; r < recut.size(); r++ )
+ drawReachSegment( recut[ r ], circle.owner );
}
+ for( size_t s = 0; s < pendingSegments.size(); s++ )
+ drawReachSegment( pendingSegments[ s ], pendingCircles[ 0 ].owner );
TheDisplay->endBatch2D();
}
@@ -6246,7 +6489,7 @@ void InGameUI::postDraw( void )
// drawHudOverlay is NOT called here - it goes on top of everything, see W3DInGameUI::draw
drawProductionStrip();
drawSkillStrip(); // the same shelf, the other end of it
- drawPlacementBlindSpots();
+ drawBlindSpots();
drawPlacementReach(); // after the shade, so the outline stays bright over it
drawHudToggles();