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(gameclient): director camera crash, rewind, silo shot, map edge and zoom

Olcay Seyganc029d7810b90
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9c0dcc052..c8c3bc5ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -747,6 +747,7 @@ A third pass went through the generals' powers, promotions and unit abilities.
- Watching, the command bar is down to the radar. The list of players it held is the board on the right and the Tab scoreboard now. Pick a player to follow in the camera panel and you are looking at him, his money and his side's steel, the way his button in the list did; pick nobody for everybody. Picking drops whatever you had selected, so the promotions on screen are that player's and not the owner of the tank you last clicked. The portrait panel in the right corner comes up when you select something and goes when you click empty ground, so an empty match leaves the bottom of the screen to the battle.
- Watching, the bars over the buildings are everyone's too - what each factory is turning out, and how long the superweapons have left. An observer used to see none of it.
- Watching, the camera can drive itself. Two drop-downs in the camera panel on the right, above the board, say who has it and which player you follow, and you can follow nobody. The Director goes to the fight with the most on the line over the last five seconds, where a dead Overlord counts for more than six riflemen being shot at and anything happening to a superweapon counts three times over. It stays with that fight twelve seconds, leaves after six only for one four times the size, and after half a minute on the same fight a third bigger is enough. So a 4v4 is watched like a broadcast rather than hunted for, and it does not hop between spots every few seconds. Any general's power used on the map outranks every fight. A nuke is shown in its silo as it leaves, then the camera waits at the target for it to come down and stays through the blast. The particle cannon's beam is followed along its whole sweep, and a Scud Storm or a carpet bomb is watched until the last explosion has died down. Then it goes back to the fighting. The camera glides between places and slows into each one, and only cuts when the next one is across most of the map. When nobody is shooting it no longer parks on empty ground: it goes round what is worth seeing, an army on the march, a superweapon, a base going up, the rest of a base, the dearest first and fourteen seconds each, and does not come back to one of the last three while there is anything else to show. With a player picked it keeps to his fights and his own armies and buildings. Player camera shows his screen: a human player's camera comes over the network five times a second, smoothed in between, and a computer player, who has no camera, gets the Director narrowed to his own fights. Scroll with the keys or a drag, turn the view or click the radar and the camera is yours again, still following the same player. The edge of the screen does not scroll while it drives: the panel on the right stands on that edge, and reaching for it used to hand the camera back to you, so the Director stopped following the fighting the first time you went to change a setting. In a replay with the recorded camera switched on in the options, the player you follow still drives the camera the way his button in the old list did.
+- Watching, the Director climbs over a wide battle until the whole of it is on the screen, and comes back down to your own height when it moves on or you take the camera. The mouse wheel still zooms while it drives, and it keeps what you chose. It stops short of the map's border too, where a fight on the edge used to come with a black corner of nothing beside it. A nuke or a Scud Storm is shown leaving its silo even when the Director has only just arrived at another fight; before, the launch was over by the time it was allowed to move. Rewinding a replay leaves your camera settings as they were, where a rewind used to hand you back a free camera with nobody followed. And following one player while a superweapon wiped out everything he had at the spot could close the game. It no longer does.
- Watching, tick Fog of war under the camera and you see what the player you follow sees and nothing more: black where he has never been, grey where he has been, none of the enemy he cannot see, and an enemy base as he last saw it. Stealth goes by his eyes too: a Jarmen Kell he has not found is gone from the screen and the radar, and his own stealthed units show the way they show him. It stays on whoever drives the camera, until you switch it off or follow nobody. Switch to another player and his fog comes in whole, shadows included: a building he has never seen casts nothing on his grey ground. An observer used to have no fog at all: the whole map, all match, with no way to see the game the way one of its players was seeing it.
- An ally, and anyone watching, now sees a stealthed unit's muzzle flash, its promotion and the cash it earns - if they can see the unit at all. Those three asked 'is it mine' instead of 'can I see it', so allies and observers got nothing.
- Watching, the match opens on the middle of the map. An observer has no start position, so the game looked for the camera waypoint of "player zero", did not find it, and fell back to the corner of the map: every match watched from the stands began pointed at empty ground in the south west.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
index 936c623f1..ff98bf9ab 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h
@@ -25,7 +25,9 @@
// a while before it looks for a bigger fight. A special power used anywhere outranks any fight: a
// superweapon is shown leaving its silo, then where it lands until the dust settles, a laser
// followed along its sweep. The camera glides between places on a spring and only cuts across
-// most of a map. With no fight on it goes round the armies on the
+// most of a map, and stops short of a place that would put ground off the map on the screen. Over
+// a wide fight it rises above the watcher's own height, which the wheel still moves, and comes back
+// down to it when the camera goes elsewhere or is taken back. With no fight on it goes round the armies on the
// move, the bases going up and the superweapons, a few seconds each, so it never sits still; with a
// player picked it counts only that player's fights and things. Player shows what the followed player's own screen
// shows: a player's camera comes over the network a few times a second (MSG_SET_REPLAY_CAMERA), an
@@ -106,6 +108,21 @@ Bool ObserverCamera_nextSight( const std::vector< DirectorHeat > &sights, const
/// where an event is best watched on a frame: a superweapon's silo for its first few seconds, then
/// where it lands
Coord2D ObserverCamera_eventPlace( const DirectorEvent &event, UnsignedInt frame );
+/// whether the director stays on the event it is showing, current, rather than go to best, the
+/// biggest one it may show now. best is NULL when none may be shown: narrowed to a player whose
+/// things round the target are all gone, the current one is filtered out and is still kept
+Bool ObserverCamera_stayOnEvent( const DirectorEvent *current, const DirectorEvent *best, UnsignedInt held );
+/// whether an event takes the camera from a fight or a sight held for held frames. A superweapon
+/// still on its way out of the silo goes at once, or the launch is over before the settle is
+Bool ObserverCamera_eventCutsIn( const DirectorEvent &event, UnsignedInt frame, UnsignedInt held );
+/// how widely the hits within DIRECTOR_GATHER_RADIUS of a place lie round it, by weight
+Real ObserverCamera_spreadAround( const std::vector< DirectorHeat > &hits, const Coord2D &around );
+/// how much higher than the watcher's own the director takes the camera over a fight this spread
+Real ObserverCamera_fightHeight( Real spread );
+/// the place moved the least that keeps the screen's ground over the map: corners are where the
+/// screen's corners fall on the ground, taken from the point looked at. Where the screen is wider
+/// than the map, the place stays between the two edges
+Coord2D ObserverCamera_keepInMap( const Coord2D &place, const Coord2D *corners, Int count, const Region2D &map );
/// a step of the camera towards where it is going on a critically damped spring: it gathers speed,
/// never goes faster than topSpeed across the ground, and slows into place without overshooting.
/// smoothSeconds is roughly how long it takes to arrive. Further apart than a glide should cross,
@@ -148,12 +165,17 @@ public:
/// belongs to going a few seconds more, and with follow the event's target moves with it
void noteSuperweaponHit( const Player *owner, const Coord3D *at, Bool follow );
+ /// the watcher's own height back on the view, if the director had raised it over a fight
+ void releaseHeight( void );
+
enum { NO_PLAYER = -1 };
private:
void updateShroudViewer( void );
void holdHeight( Bool hold );
+ void driveHeight( Real extra );
Bool takenByHand( const ViewLocation &current ) const;
+ Coord2D keepInMap( const Coord2D &place, const ViewLocation &current ) const;
Bool isShowingPlayerView( void ) const;
Bool chooseTarget( const ViewLocation &current, ViewLocation *target );
Bool directorPlace( const Player *narrowTo, Coord2D *place );
@@ -168,7 +190,10 @@ private:
Int m_shroudViewer;
Bool m_driving; ///< the camera was put where it is by this, last frame
Bool m_holdingHeight; ///< the view's own height easing is off while a player's zoom is shown
- Coord3D m_drivenTo;
+ Bool m_heightDriven; ///< the director has the camera's height above the ground
+ Real m_handHeight; ///< the watcher's own height, the wheel's turns while driven added in
+ Real m_drivenHeight; ///< the height the director left the view at last frame
+ Coord3D m_drivenTo; ///< where this put the camera last frame, inside the view's constraint
UnsignedInt m_lastUpdate;
ObserverCameraVelocity m_velocity;
@@ -180,6 +205,7 @@ private:
UnsignedInt m_placeScanned; ///< the logic frame the hits were last counted
const Player *m_placeFor; ///< whose fights the place was picked from, NULL for everybody's
PlaceKind m_placeKind; ///< a fight, a special power, or a sight picked while nothing was hit
+ Real m_placeHeight; ///< how much higher than the watcher's own the place is watched from
UnsignedInt m_placeEvent; ///< the id of the event the place is, while it is one
std::vector< Coord2D > m_seen; ///< the last few sights, oldest first, not gone back to while there is another
std::vector< DirectorEvent > m_events; ///< the special powers still worth watching, oldest first
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
index 8dbc5a33c..8dc87f5f2 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
@@ -240,6 +240,7 @@ public:
virtual Real getFXPitch( void ) const { return 1.0f; } ///< returns the FX pitch angle
virtual void forceCameraConstraintRecalc(void) {}
+ virtual void applyCameraConstraint( void ) {} ///< move the look point inside the camera constraint now, where the next draw would
virtual void setGuardBandBias( const Coord2D *gb ) = 0;
/** The console's freecam, a photo mode: the camera flies on its own from an eye point with a
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 510391257..e6fd203a8 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -2487,8 +2487,9 @@ static void takeReplayCheckpoint( UnsignedInt frame )
}
/** Load the last checkpoint at or before the frame, or the first one if the frame is before it. The
- * camera, the speed and the observer's view stay as they were: the checkpoint carries the camera it
- * was taken with, which is not where the person watching is looking now. */
+ * camera, the speed and the observer's view stay as they were, the observer camera's mode, followed
+ * player and fog included: the checkpoint carries the camera it was taken with, which is not where
+ * the person watching is looking now. */
static void rewindReplay( UnsignedInt target )
{
std::map< UnsignedInt, ReplayCheckpoint >::const_iterator at = TheReplayCheckpoints.upper_bound( target );
@@ -2497,6 +2498,9 @@ static void rewindReplay( UnsignedInt target )
const ReplayCheckpoint &checkpoint = at->second;
const AsciiString replayFile = TheRecorder->getCurrentReplayFilename();
+ // the reset forgets that the director raised the view over a fight, and the view keeps the raise;
+ // given back first, so rewinds do not stack it
+ TheObserverCamera.releaseHeight();
Coord3D lookingAt;
TheTacticalView->getPosition( &lookingAt );
const Real angle = TheTacticalView->getAngle();
@@ -2504,11 +2508,22 @@ static void rewindReplay( UnsignedInt target )
const Real zoom = TheTacticalView->getZoom();
const Int framesPerSecond = TheGameEngine->getFramesPerSecondLimit();
const UnsignedInt startMs = Clock_Milliseconds();
+ // the load resets the client, the observer camera with it: who drives it, who is followed and
+ // whose fog is drawn are the watcher's choices, not the checkpoint's
+ const ObserverCameraMode cameraMode = TheObserverCamera.getMode();
+ const Int followed = TheObserverCamera.getFollowedPlayerIndex();
+ const Bool fog = TheObserverCamera.isFogOn();
TheGameState->loadCheckpoint( checkpoint.path,
[ & ]() { TheRecorder->resumePlayback( replayFile, checkpoint.cursor ); } );
SetGameLogicRandomState( checkpoint.random );
+ TheObserverCamera.setMode( cameraMode );
+ TheObserverCamera.followPlayer( followed );
+ TheObserverCamera.setFog( fog );
+ if( followed != ObserverCamera::NO_PLAYER )
+ TheControlBar->watchPlayer( ThePlayerList->getNthPlayer( followed ) );
+
for( size_t each = 0; each < checkpoint.postedCRCs.size(); each++ )
{
GameMessage *crc = TheMessageStream->appendMessage( GameMessage::MSG_LOGIC_CRC );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
index 298d47e52..9ef5f96db 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp
@@ -37,6 +37,7 @@
#include "GameLogic/Module/BodyModule.h"
#include "GameLogic/Object.h"
#include "GameLogic/PartitionManager.h"
+#include "GameLogic/TerrainLogic.h"
#include <math.h>
@@ -85,13 +86,20 @@ static const size_t DIRECTOR_SEEN_COUNT = 3;
/// long to sit through and shows nothing
static const Real CUT_DISTANCE = 1600.0f;
/// the camera found further than this from where it was put was moved by something else, a click on
-/// the production strip or a jump to a group; the edge of the map pulls it back by less. The radar
+/// the production strip or a jump to a group. Where it was put is taken after the view's own
+/// constraint, so the edge of the map pulling it back does not count. The radar
/// hands the camera over itself, since a click near the camera and a drag both move it by less
static const Real HAND_JUMP_DISTANCE = 400.0f;
/// roughly how long the director's glide takes to arrive, easing in and out
static const Real DIRECTOR_PAN_SECONDS = 1.4f;
/// the director's glide never crosses the ground faster than this, about two screens a second
static const Real DIRECTOR_TOP_SPEED = 900.0f;
+/// a fight this tight is watched from the watcher's own height; wider, the camera rises this much
+/// for every unit further, up to the most. A fight's hits lie within DIRECTOR_GATHER_RADIUS of one
+/// another, so the most is reached only by the widest
+static const Real DIRECTOR_TIGHT_SPREAD = 60.0f;
+static const Real DIRECTOR_HEIGHT_PER_SPREAD = 1.5f;
+static const Real DIRECTOR_MOST_EXTRA_HEIGHT = 300.0f;
/// a player's camera comes a few times a second, and this smooths the steps between
static const Real PLAYER_PAN_SECONDS = 0.15f;
static const Real PLAYER_TOP_SPEED = 4000.0f;
@@ -214,6 +222,77 @@ Coord2D ObserverCamera_eventPlace( const DirectorEvent &event, UnsignedInt frame
return event.target;
}
+//-------------------------------------------------------------------------------------------------
+Bool ObserverCamera_stayOnEvent( const DirectorEvent *current, const DirectorEvent *best, UnsignedInt held )
+{
+ if( current == NULL )
+ return FALSE;
+ return best == NULL || best == current || !ObserverCamera_shouldMove( current->weight, best->weight, held );
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool ObserverCamera_eventCutsIn( const DirectorEvent &event, UnsignedInt frame, UnsignedInt held )
+{
+ return held >= DIRECTOR_SETTLE_FRAMES || ( event.superweapon && frame < event.since + EVENT_LAUNCH_FRAMES );
+}
+
+//-------------------------------------------------------------------------------------------------
+Real ObserverCamera_spreadAround( const std::vector< DirectorHeat > &hits, const Coord2D &around )
+{
+ Real weight = 0.0f;
+ Real squares = 0.0f;
+ for( size_t index = 0; index < hits.size(); index++ )
+ {
+ const DirectorHeat &hit = hits[ index ];
+ if( !sameFight( hit.position, around ) )
+ continue;
+
+ const Real dx = hit.position.x - around.x;
+ const Real dy = hit.position.y - around.y;
+ weight += hit.weight;
+ squares += ( dx * dx + dy * dy ) * hit.weight;
+ }
+ return weight > 0.0f ? sqrtf( squares / weight ) : 0.0f;
+}
+
+//-------------------------------------------------------------------------------------------------
+Real ObserverCamera_fightHeight( Real spread )
+{
+ const Real extra = ( spread - DIRECTOR_TIGHT_SPREAD ) * DIRECTOR_HEIGHT_PER_SPREAD;
+ return min( max( extra, 0.0f ), DIRECTOR_MOST_EXTRA_HEIGHT );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Each axis on its own: the lowest corner may not go below the map's low edge, nor the highest
+ * past its high one. When the screen is wider than the map the two bounds cross, and anywhere
+ * between them shows as little off the map as the screen can. */
+//-------------------------------------------------------------------------------------------------
+static Real keepAxisInMap( Real place, Real lowestCorner, Real highestCorner, Real mapLow, Real mapHigh )
+{
+ const Real low = mapLow - lowestCorner;
+ const Real high = mapHigh - highestCorner;
+ return min( max( place, min( low, high ) ), max( low, high ) );
+}
+
+//-------------------------------------------------------------------------------------------------
+Coord2D ObserverCamera_keepInMap( const Coord2D &place, const Coord2D *corners, Int count, const Region2D &map )
+{
+ Coord2D lowest = corners[ 0 ];
+ Coord2D highest = corners[ 0 ];
+ for( Int index = 1; index < count; index++ )
+ {
+ lowest.x = min( lowest.x, corners[ index ].x );
+ lowest.y = min( lowest.y, corners[ index ].y );
+ highest.x = max( highest.x, corners[ index ].x );
+ highest.y = max( highest.y, corners[ index ].y );
+ }
+
+ Coord2D kept;
+ kept.x = keepAxisInMap( place.x, lowest.x, highest.x, map.lo.x, map.hi.x );
+ kept.y = keepAxisInMap( place.y, lowest.y, highest.y, map.lo.y, map.hi.y );
+ return kept;
+}
+
//-------------------------------------------------------------------------------------------------
/** An angle's shortest way round to another, so a camera facing just west of north turns a few
* degrees to just east of it rather than all the way back round. */
@@ -299,6 +378,9 @@ void ObserverCamera::reset( void )
m_shroudViewer = NO_PLAYER;
m_driving = FALSE;
m_holdingHeight = FALSE;
+ m_heightDriven = FALSE;
+ m_handHeight = 0.0f;
+ m_drivenHeight = 0.0f;
m_drivenTo.zero();
m_lastUpdate = 0;
m_velocity.x = m_velocity.y = m_velocity.z = m_velocity.angle = m_velocity.pitch = m_velocity.zoom = 0.0f;
@@ -310,6 +392,7 @@ void ObserverCamera::reset( void )
m_placeScanned = 0;
m_placeFor = NULL;
m_placeKind = PLACE_SIGHT;
+ m_placeHeight = 0.0f;
m_placeEvent = 0;
m_seen.clear();
m_events.clear();
@@ -454,6 +537,67 @@ void ObserverCamera::holdHeight( Bool hold )
TheTacticalView->setOkToAdjustHeight( !hold );
}
+//-------------------------------------------------------------------------------------------------
+/** The director's height over a fight is the watcher's own plus extra. It is set as the height
+ * the view wants, so the view's own easing carries the camera there rather than a jump. A turn
+ * of the wheel while it drives moves the height the view wants, and that turn is the watcher's:
+ * it is added to his own height, so the director never takes it back. */
+//-------------------------------------------------------------------------------------------------
+void ObserverCamera::driveHeight( Real extra )
+{
+ const Real now = TheTacticalView->getHeightAboveGround();
+ if( m_heightDriven )
+ m_handHeight += now - m_drivenHeight;
+ else
+ m_handHeight = now;
+ m_heightDriven = TRUE;
+
+ const Real wanted = m_handHeight + extra;
+ if( wanted != now )
+ TheTacticalView->setHeightAboveGround( wanted );
+ m_drivenHeight = TheTacticalView->getHeightAboveGround();
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The watcher's own height back, with any turn of the wheel since the last frame kept. */
+//-------------------------------------------------------------------------------------------------
+void ObserverCamera::releaseHeight( void )
+{
+ if( !m_heightDriven )
+ return;
+
+ m_heightDriven = FALSE;
+ TheTacticalView->setHeightAboveGround( m_handHeight + TheTacticalView->getHeightAboveGround() - m_drivenHeight );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The screen's corners on the ground round the point looked at now, so the place keeps the whole
+ * screen over the map. They are measured at the current zoom: while the camera rises the corners
+ * spread and the place is pulled further in, frame by frame. */
+//-------------------------------------------------------------------------------------------------
+Coord2D ObserverCamera::keepInMap( const Coord2D &place, const ViewLocation &current ) const
+{
+ const Coord3D &at = current.getPosition();
+ Coord3D world[ 4 ];
+ TheTacticalView->getScreenCornerWorldPointsAtZ( &world[ 0 ], &world[ 1 ], &world[ 2 ], &world[ 3 ],
+ TheTerrainLogic->getGroundHeight( at.x, at.y ) );
+ Coord2D corners[ 4 ];
+ for( Int index = 0; index < 4; index++ )
+ {
+ corners[ index ].x = world[ index ].x - at.x;
+ corners[ index ].y = world[ index ].y - at.y;
+ }
+
+ Region3D extent;
+ TheTerrainLogic->getExtent( &extent );
+ Region2D map;
+ map.lo.x = extent.lo.x;
+ map.lo.y = extent.lo.y;
+ map.hi.x = extent.hi.x;
+ map.hi.y = extent.hi.y;
+ return ObserverCamera_keepInMap( place, corners, 4, map );
+}
+
//-------------------------------------------------------------------------------------------------
Bool ObserverCamera::takenByHand( const ViewLocation &current ) const
{
@@ -509,7 +653,10 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
sights.push_back( heat );
}
+ // a thing with no body module takes no damage, so it has no hits to count
const BodyModuleInterface *body = obj->getBodyModule();
+ if( body == NULL )
+ continue;
const UnsignedInt hitAt = body->getLastDamageTimestamp();
if( hitAt == 0 || frame >= hitAt + DIRECTOR_HEAT_FRAMES )
continue;
@@ -524,7 +671,7 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
Real hottestHeat = 0.0f;
ObserverCamera_hottestPlace( hits, &hottest, &hottestHeat );
- const UnsignedInt held = frame - m_placeSince;
+ const UnsignedInt held = frame >= m_placeSince ? frame - m_placeSince : 0;
Coord2D followed = m_place;
// a special power outranks any fight. Narrowed to one player, only his own and the ones that
@@ -544,19 +691,21 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
best = &event;
}
DirectorEvent *current = m_placeValid && m_placeKind == PLACE_EVENT ? findEvent( m_placeEvent ) : NULL;
- if( current != NULL && ( best == current || !ObserverCamera_shouldMove( current->weight, best->weight, held ) ) )
+ if( ObserverCamera_stayOnEvent( current, best, held ) )
{
m_place = ObserverCamera_eventPlace( *current, frame );
*place = m_place;
return TRUE;
}
- // an event that just ended hands straight over to the next; anything else is given its settle first
- if( best != NULL && ( !m_placeValid || m_placeKind == PLACE_EVENT || held >= DIRECTOR_SETTLE_FRAMES ) )
+ // an event that just ended hands straight over to the next; anything else is given its settle
+ // first, except a superweapon still leaving its silo
+ if( best != NULL && ( !m_placeValid || m_placeKind == PLACE_EVENT || ObserverCamera_eventCutsIn( *best, frame, held ) ) )
{
DEBUG_LOG(( "OBSCAM frame %u director to special power %u at (%.0f,%.0f)%s\n", frame, best->id,
best->target.x, best->target.y, best->superweapon ? " superweapon" : "" ));
m_place = ObserverCamera_eventPlace( *best, frame );
m_placeKind = PLACE_EVENT;
+ m_placeHeight = 0.0f;
m_placeEvent = best->id;
m_placeSince = frame;
m_placeValid = TRUE;
@@ -573,6 +722,7 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
const Real dy = followed.y - m_place.y;
if( dx * dx + dy * dy > DIRECTOR_FOLLOW_SLACK * DIRECTOR_FOLLOW_SLACK )
m_place = followed;
+ m_placeHeight = ObserverCamera_fightHeight( ObserverCamera_spreadAround( hits, followed ) );
*place = m_place;
return TRUE;
}
@@ -590,6 +740,7 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
DEBUG_LOG(( "OBSCAM frame %u director to fight (%.0f,%.0f) heat %.1f\n", frame, hottest.x, hottest.y, hottestHeat ));
m_place = hottest;
m_placeKind = PLACE_FIGHT;
+ m_placeHeight = ObserverCamera_fightHeight( ObserverCamera_spreadAround( hits, hottest ) );
}
else
{
@@ -606,6 +757,7 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )
DEBUG_LOG(( "OBSCAM frame %u director to sight (%.0f,%.0f)\n", frame, sight.x, sight.y ));
m_place = sight;
m_placeKind = PLACE_SIGHT;
+ m_placeHeight = 0.0f;
m_seen.push_back( sight );
if( m_seen.size() > DIRECTOR_SEEN_COUNT )
m_seen.erase( m_seen.begin() );
@@ -642,6 +794,7 @@ Bool ObserverCamera::chooseTarget( const ViewLocation &current, ViewLocation *ta
Coord2D place;
if( !directorPlace( narrowTo, &place ) )
return FALSE;
+ place = keepInMap( place, current );
target->init( place.x, place.y, at.z, current.getAngle(), current.getPitch(), current.getZoom() );
return TRUE;
}
@@ -658,6 +811,7 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
{
m_driving = FALSE;
holdHeight( FALSE );
+ releaseHeight();
return;
}
@@ -670,6 +824,7 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
m_mode = OBSERVER_CAMERA_FREE;
m_driving = FALSE;
holdHeight( FALSE );
+ releaseHeight();
return;
}
@@ -678,9 +833,16 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
{
m_driving = FALSE;
holdHeight( FALSE );
+ releaseHeight();
return;
}
+ // a player's screen brings its own zoom; the director's height is the watcher's own plus what
+ // the fight's width asks for
+ if( isShowingPlayerView() )
+ releaseHeight();
+ else
+ driveHeight( m_placeHeight );
holdHeight( isShowingPlayerView() );
if( !m_driving )
m_velocity.x = m_velocity.y = m_velocity.z = m_velocity.angle = m_velocity.pitch = m_velocity.zoom = 0.0f;
@@ -688,6 +850,9 @@ void ObserverCamera::update( UnsignedInt nowMilliseconds )
const ViewLocation step = ObserverCamera_approach( current, target, elapsed / MILLISECONDS_PER_SECOND,
player ? PLAYER_PAN_SECONDS : DIRECTOR_PAN_SECONDS, player ? PLAYER_TOP_SPEED : DIRECTOR_TOP_SPEED, &m_velocity );
TheTacticalView->setLocation( &step );
- m_drivenTo = step.getPosition();
+ // the view keeps its look point inside its constraint when it draws; held there now, a cut to a
+ // place past the constraint is not mistaken next frame for the watcher moving the camera
+ TheTacticalView->applyCameraConstraint();
+ TheTacticalView->getPosition( &m_drivenTo );
m_driving = TRUE;
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
index ccd350476..a889780dc 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DView.h
@@ -235,6 +235,7 @@ public:
Bool updateCameraMovements(void);
virtual void forceCameraConstraintRecalc(void) { calcCameraConstraints(); }
+ virtual void applyCameraConstraint( void );
virtual void setGuardBandBias( const Coord2D *gb ) { m_guardBandBias.x = gb->x; m_guardBandBias.y = gb->y; }
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
index 5f6c519b6..2bee7d5d7 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp
@@ -573,6 +573,33 @@ void W3DView::buildCameraTransform( Matrix3D *transform )
}
}
+//-------------------------------------------------------------------------------------------------
+/** The look point inside the constraint, built first if a zoom, a pitch or a move of the ground
+ * threw it away. The draw does this every frame; the observer camera does it as soon as it has
+ * moved the view, to know where the draw will put it. */
+//-------------------------------------------------------------------------------------------------
+void W3DView::applyCameraConstraint( void )
+{
+ // the freecam owns the camera and is held to no constraint, as in setCameraTransform
+ if (m_freeCamera || !(TheGlobalData->m_useCameraConstraints || TheGlobalData->isClassicUI()))
+ return;
+
+ if (!m_cameraConstraintValid)
+ {
+ Matrix3D cameraTransform;
+ buildCameraTransform(&cameraTransform);
+ m_3DCamera->Set_Transform( cameraTransform );
+ calcCameraConstraints();
+ }
+ DEBUG_ASSERTLOG(m_cameraConstraintValid,("*** cam constraints are not valid!!!\n"));
+
+ if (m_cameraConstraintValid)
+ {
+ Coord3D pos = constrainCameraPosition(*getPosition(), m_cameraConstraint);
+ setPosition(&pos);
+ }
+}
+
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
void W3DView::calcCameraConstraints()
@@ -909,22 +936,7 @@ void W3DView::setCameraTransform( void )
}
m_3DCamera->Set_Clip_Planes(nearZ, farZ);
- if (TheGlobalData->m_useCameraConstraints || TheGlobalData->isClassicUI())
- {
- if (!m_cameraConstraintValid)
- {
- buildCameraTransform(&cameraTransform);
- m_3DCamera->Set_Transform( cameraTransform );
- calcCameraConstraints();
- }
- DEBUG_ASSERTLOG(m_cameraConstraintValid,("*** cam constraints are not valid!!!\n"));
-
- if (m_cameraConstraintValid)
- {
- Coord3D pos = constrainCameraPosition(*getPosition(), m_cameraConstraint);
- setPosition(&pos);
- }
- }
+ applyCameraConstraint();
#if defined(_DEBUG) || defined(_INTERNAL)
m_3DCamera->Set_View_Plane( m_FOV, -1 );
diff --git a/GeneralsMD/Code/Tests/test_observer_camera.inc b/GeneralsMD/Code/Tests/test_observer_camera.inc
index 93bf77b42..fdd78c0c1 100644
--- a/GeneralsMD/Code/Tests/test_observer_camera.inc
+++ b/GeneralsMD/Code/Tests/test_observer_camera.inc
@@ -132,6 +132,98 @@ TEST(the_director_with_no_fight_goes_round_the_sights_and_never_parks)
CHECK_NEAR( place.x, 1000.0f, 0.001f );
}
+static DirectorEvent directorEvent( UnsignedInt id, Real weight, Bool superweapon )
+{
+ DirectorEvent event;
+ event.id = id;
+ event.owner = NULL;
+ event.source.x = event.source.y = 500.0f;
+ event.target.x = 3000.0f;
+ event.target.y = 2000.0f;
+ event.since = 1000;
+ event.until = 2000;
+ event.weight = weight;
+ event.superweapon = superweapon;
+ return event;
+}
+
+TEST(the_director_keeps_its_event_when_none_may_be_shown_any_more)
+{
+ // narrowed to a player whose things at the target all died, the event it shows is filtered out
+ // and nothing is left to compare it with; this read a NULL best and crashed
+ const DirectorEvent nuke = directorEvent( 1, 5.0f, TRUE );
+ CHECK( ObserverCamera_stayOnEvent( &nuke, NULL, 0 ) );
+ CHECK( ObserverCamera_stayOnEvent( &nuke, &nuke, 0 ) );
+ CHECK( !ObserverCamera_stayOnEvent( NULL, &nuke, 0 ) );
+ CHECK( !ObserverCamera_stayOnEvent( NULL, NULL, 0 ) );
+
+ // a bomber's strike gives way to a superweapon once it has settled, not before
+ const DirectorEvent strike = directorEvent( 2, 1.0f, FALSE );
+ CHECK( ObserverCamera_stayOnEvent( &strike, &nuke, LOGICFRAMES_PER_SECOND ) );
+ CHECK( !ObserverCamera_stayOnEvent( &strike, &nuke, 7 * LOGICFRAMES_PER_SECOND ) );
+}
+
+TEST(a_superweapon_leaving_its_silo_takes_the_camera_before_the_settle_is_up)
+{
+ const DirectorEvent nuke = directorEvent( 1, 5.0f, TRUE );
+ const DirectorEvent strike = directorEvent( 2, 1.0f, FALSE );
+ const UnsignedInt justArrived = LOGICFRAMES_PER_SECOND;
+ CHECK( ObserverCamera_eventCutsIn( nuke, 1001, justArrived ) );
+ CHECK( !ObserverCamera_eventCutsIn( strike, 1001, justArrived ) );
+ // once the missile is out of the silo the nuke waits for the settle like anything else
+ CHECK( !ObserverCamera_eventCutsIn( nuke, 1000 + 5 * LOGICFRAMES_PER_SECOND, justArrived ) );
+ CHECK( ObserverCamera_eventCutsIn( strike, 1001, 7 * LOGICFRAMES_PER_SECOND ) );
+}
+
+TEST(the_director_rises_over_a_wide_fight_and_not_over_a_tight_one)
+{
+ std::vector< DirectorHeat > tight;
+ std::vector< DirectorHeat > wide;
+ for( Int each = 0; each < 4; each++ )
+ {
+ tight.push_back( directorHit( 1000.0f + ( each - 2 ) * 10.0f, 1000.0f ) );
+ wide.push_back( directorHit( 1000.0f + ( each % 2 ? 150.0f : -150.0f ), 1000.0f ) );
+ }
+ Coord2D middle;
+ middle.x = middle.y = 1000.0f;
+ CHECK_NEAR( ObserverCamera_spreadAround( wide, middle ), 150.0f, 0.01f );
+ CHECK_NEAR( ObserverCamera_fightHeight( ObserverCamera_spreadAround( tight, middle ) ), 0.0f, 0.001f );
+ CHECK_NEAR( ObserverCamera_fightHeight( ObserverCamera_spreadAround( wide, middle ) ), 135.0f, 0.01f );
+ CHECK( ObserverCamera_fightHeight( 10000.0f ) <= 300.0f );
+ std::vector< DirectorHeat > none;
+ CHECK_NEAR( ObserverCamera_spreadAround( none, middle ), 0.0f, 0.001f );
+}
+
+TEST(the_director_stops_short_of_a_place_that_shows_ground_off_the_map)
+{
+ // the screen's ground runs 300 to each side, 200 below the point looked at and 500 above it
+ Coord2D corners[ 4 ];
+ corners[ 0 ].x = -300.0f; corners[ 0 ].y = 500.0f;
+ corners[ 1 ].x = 300.0f; corners[ 1 ].y = 500.0f;
+ corners[ 2 ].x = -300.0f; corners[ 2 ].y = -200.0f;
+ corners[ 3 ].x = 300.0f; corners[ 3 ].y = -200.0f;
+ Region2D map;
+ map.lo.x = map.lo.y = 0.0f;
+ map.hi.x = map.hi.y = 4000.0f;
+
+ Coord2D place;
+ place.x = 2000.0f;
+ place.y = 2000.0f;
+ CHECK_NEAR( ObserverCamera_keepInMap( place, corners, 4, map ).x, 2000.0f, 0.001f );
+
+ // a fight in the map's corner is shown from as near it as the screen allows
+ place.x = 50.0f;
+ place.y = 3900.0f;
+ const Coord2D kept = ObserverCamera_keepInMap( place, corners, 4, map );
+ CHECK_NEAR( kept.x, 300.0f, 0.001f );
+ CHECK_NEAR( kept.y, 3500.0f, 0.001f );
+
+ // a map narrower than the screen: the place stays between the two edges' limits
+ map.hi.x = 400.0f;
+ place.x = 50.0f;
+ CHECK_NEAR( ObserverCamera_keepInMap( place, corners, 4, map ).x, 100.0f, 0.001f );
+}
+
static ObserverCameraVelocity atRest( void )
{
ObserverCameraVelocity velocity;