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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(gameclient): observer director weighs fights by stake and tours sights when idle
Olcay Seyganed9f22793642
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 29ca209c5..21ac6d635 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -607,7 +607,7 @@ A third pass went through the generals' powers, promotions and unit abilities.- Every row and every countdown stands in real metal, cut from the same tray the general's powers sit in down in the corner - the side your command bar is showing. Watching a match there is no bar of your own, so the superweapon countdowns used to fall back to a flat black box for the whole match; now they wear the metal of the player you are watching, and follow it when you switch seats.- 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 wherever the most things were hit in the last five seconds, stays with that fight at least six seconds, and cuts away only for one clearly bigger, so a 4v4 is watched like a broadcast rather than hunted for; with a player picked it keeps to the fights he is in. 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 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 at least six seconds, and cuts away sooner only for one three times the size; after twenty seconds on the same fight, any bigger one elsewhere takes it. So a 4v4 is watched like a broadcast rather than hunted for. 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 eight 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, 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.hindex fdd7bb6fd..f5454380c 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ObserverCamera.h@@ -20,9 +20,11 @@// Who drives a watcher's camera, and whose fog his screen is drawn in.//// The mode and the followed player are picked apart, from two lists. Free is the camera in the-// watcher's own hands. Director goes wherever the most things have been hit in the last few-// seconds and stays there a while before it looks for a hotter fight; with a player picked it-// counts only the fights that player is in. Player shows what the followed player's own screen+// watcher's own hands. Director goes to the fight of the last few seconds with the most at stake,+// each hit counted by what the thing hit cost and more for a kill or a superweapon, and stays there+// a while before it looks for a bigger fight. 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// AI, which has no camera, gets the narrowed director, and with nobody picked it does nothing.// Scrolling with the keys or a drag, turning the camera or clicking the radar hands it back to the@@ -68,6 +70,14 @@ Bool ObserverCamera_hottestPlace( const std::vector< DirectorHeat > &hits, CoordReal ObserverCamera_heatAround( const std::vector< DirectorHeat > &hits, const Coord2D &around, Coord2D *middle );/// whether a director holding a place with heatHere for framesHere should cut to one with heatThereBool ObserverCamera_shouldMove( Real heatHere, Real heatThere, UnsignedInt framesHere );+/// what one recent hit counts for: more the dearer the thing hit, more again if it died or is a+/// superweapon+Real ObserverCamera_hitWeight( Int cost, Bool killed, Bool superweapon );+/// what one thing is worth looking at with no fight on: its cost, doubled while it marches or is+/// being built, halved for a building that is only standing there, tripled for a superweapon+Real ObserverCamera_sightWeight( Int cost, Bool structure, Bool busy, Bool superweapon );+/// the best place among the sights away from the ones in seen; FALSE when every sight was seen+Bool ObserverCamera_nextSight( const std::vector< DirectorHeat > &sights, const std::vector< Coord2D > &seen, Coord2D *place );/// a step of the camera towards where it is going, easing with timeConstant, or the jump there when/// the two are further apart than a pan should crossViewLocation ObserverCamera_approach( const ViewLocation &from, const ViewLocation &to, Real elapsedSeconds, Real timeConstant );@@ -129,6 +139,8 @@ private:UnsignedInt m_placeSince; ///< the logic frame it went thereUnsignedInt m_placeScanned; ///< the logic frame the hits were last countedconst Player *m_placeFor; ///< whose fights the place was picked from, NULL for everybody's+ Bool m_placeIsFight; ///< the place is a fight, not a sight picked while nothing was hit+ std::vector< Coord2D > m_seen; ///< the last few sights, oldest first, not gone back to while there is another};extern ObserverCamera TheObserverCamera;diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cppindex 03bdabb56..30d8210f3 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ObserverCamera.cpp@@ -25,6 +25,7 @@#include "Common/Player.h"#include "Common/PlayerList.h"+#include "Common/ThingTemplate.h"#include "GameClient/Drawable.h"#include "GameClient/GameClient.h"#include "GameClient/LookAtXlat.h"@@ -32,6 +33,7 @@#include "GameLogic/Damage.h"#include "GameLogic/GameLogic.h"#include "GameLogic/GhostObject.h"+#include "GameLogic/Module/AIUpdate.h"#include "GameLogic/Module/BodyModule.h"#include "GameLogic/Object.h"#include "GameLogic/PartitionManager.h"@@ -50,6 +52,22 @@ static const UnsignedInt DIRECTOR_SCAN_FRAMES = LOGICFRAMES_PER_SECOND / 2;static const UnsignedInt DIRECTOR_HOLD_FRAMES = 6 * LOGICFRAMES_PER_SECOND;/// how much hotter somewhere else has to be to be worth leaving a fight that is still goingstatic const Real DIRECTOR_SWITCH_MARGIN = 1.5f;+/// no cut sooner than this after the last one, however big the other fight+static const UnsignedInt DIRECTOR_SETTLE_FRAMES = 2 * LOGICFRAMES_PER_SECOND;+/// this much hotter elsewhere and the director goes before its hold is up+static const Real DIRECTOR_BIG_MARGIN = 3.0f;+/// after this long on one fight any hotter one elsewhere will do+static const UnsignedInt DIRECTOR_TIRED_FRAMES = 20 * LOGICFRAMES_PER_SECOND;+static const Real DIRECTOR_TIRED_MARGIN = 1.1f;+/// a hit on a thing that cost this much counts twice what a free one does+static const Real DIRECTOR_COST_PER_WEIGHT = 500.0f;+/// a kill counts this many times a hit, and anything on a superweapon this many times again+static const Real DIRECTOR_KILL_FACTOR = 2.0f;+static const Real DIRECTOR_SUPERWEAPON_FACTOR = 3.0f;+/// with no fight on, how long the director looks at one army, base or building site+static const UnsignedInt DIRECTOR_SIGHT_FRAMES = 8 * LOGICFRAMES_PER_SECOND;+/// how many of the last sights the director will not go back to while there is another+static const size_t DIRECTOR_SEEN_COUNT = 3;/// further apart than this the camera cuts rather than pans: a pan across the map shows nothingstatic const Real CUT_DISTANCE = 700.0f;/// the camera found further than this from where it was put was moved by something else, the radar@@ -63,6 +81,54 @@ static const Real PLAYER_PAN_SECONDS = 0.12f;static const UnsignedInt LONGEST_STEP_MILLISECONDS = 100;static const Real MILLISECONDS_PER_SECOND = 1000.0f;+//-------------------------------------------------------------------------------------------------+static Bool sameFight( const Coord2D &a, const Coord2D &b )+{+ const Real dx = a.x - b.x;+ const Real dy = a.y - b.y;+ return dx * dx + dy * dy <= DIRECTOR_GATHER_RADIUS * DIRECTOR_GATHER_RADIUS;+}++//-------------------------------------------------------------------------------------------------+Real ObserverCamera_hitWeight( Int cost, Bool killed, Bool superweapon )+{+ Real weight = 1.0f + cost / DIRECTOR_COST_PER_WEIGHT;+ if( killed )+ weight *= DIRECTOR_KILL_FACTOR;+ if( superweapon )+ weight *= DIRECTOR_SUPERWEAPON_FACTOR;+ return weight;+}++//-------------------------------------------------------------------------------------------------+Real ObserverCamera_sightWeight( Int cost, Bool structure, Bool busy, Bool superweapon )+{+ Real weight = cost / DIRECTOR_COST_PER_WEIGHT;+ if( busy )+ weight *= 2.0f;+ else if( structure )+ weight *= 0.5f;+ if( superweapon )+ weight *= DIRECTOR_SUPERWEAPON_FACTOR;+ return weight;+}++//-------------------------------------------------------------------------------------------------+Bool ObserverCamera_nextSight( const std::vector< DirectorHeat > &sights, const std::vector< Coord2D > &seen, Coord2D *place )+{+ std::vector< DirectorHeat > fresh;+ for( size_t index = 0; index < sights.size(); index++ )+ {+ Bool wasSeen = FALSE;+ for( size_t look = 0; look < seen.size() && !wasSeen; look++ )+ wasSeen = sameFight( sights[ index ].position, seen[ look ] );+ if( !wasSeen )+ fresh.push_back( sights[ index ] );+ }+ Real heat = 0.0f;+ return ObserverCamera_hottestPlace( fresh, place, &heat );+}+//-------------------------------------------------------------------------------------------------Real ObserverCamera_heatAround( const std::vector< DirectorHeat > &hits, const Coord2D &around, Coord2D *middle ){@@ -71,9 +137,7 @@ Real ObserverCamera_heatAround( const std::vector< DirectorHeat > &hits, const Cfor( size_t index = 0; index < hits.size(); index++ ){const DirectorHeat &hit = hits[ index ];- const Real dx = hit.position.x - around.x;- const Real dy = hit.position.y - around.y;- if( dx * dx + dy * dy > DIRECTOR_GATHER_RADIUS * DIRECTOR_GATHER_RADIUS )+ if( !sameFight( hit.position, around ) )continue;heat += hit.weight;@@ -114,7 +178,14 @@ Bool ObserverCamera_shouldMove( Real heatHere, Real heatThere, UnsignedInt frame{if( heatHere <= 0.0f )return heatThere > 0.0f;- return framesHere >= DIRECTOR_HOLD_FRAMES && heatThere > heatHere * DIRECTOR_SWITCH_MARGIN;+ if( framesHere < DIRECTOR_SETTLE_FRAMES )+ return FALSE;+ if( heatThere > heatHere * DIRECTOR_BIG_MARGIN )+ return TRUE;+ if( framesHere < DIRECTOR_HOLD_FRAMES )+ return FALSE;+ const Real margin = framesHere >= DIRECTOR_TIRED_FRAMES ? DIRECTOR_TIRED_MARGIN : DIRECTOR_SWITCH_MARGIN;+ return heatThere > heatHere * margin;}//-------------------------------------------------------------------------------------------------@@ -174,6 +245,8 @@ void ObserverCamera::reset( void )m_placeSince = 0;m_placeScanned = 0;m_placeFor = NULL;+ m_placeIsFight = FALSE;+ m_seen.clear();}//-------------------------------------------------------------------------------------------------@@ -256,9 +329,11 @@ Bool ObserverCamera::takenByHand( const ViewLocation ¤t ) const}//--------------------------------------------------------------------------------------------------/** Where the director looks: the hottest fight, held for a while, followed as it moves, and left- * for a clearly hotter one. Narrowed to one player it counts only the hits on his things and the- * hits his things made, and with none of those it goes home to his command centre. */+/** Where the director looks: the fight with the most at stake, held for a while, followed as it+ * moves, and left for a clearly bigger one. With no fight anywhere it goes round what is worth+ * seeing instead, an army on the move, a base going up, a superweapon, a few seconds each and not+ * straight back to one it has just shown. Narrowed to one player it counts only the hits on his+ * things, the hits his things made and his own sights. It only reads the logic. *///-------------------------------------------------------------------------------------------------Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place ){@@ -276,57 +351,89 @@ Bool ObserverCamera::directorPlace( const Player *narrowTo, Coord2D *place )m_placeScanned = frame;std::vector< DirectorHeat > hits;- const Object *home = NULL;+ std::vector< DirectorHeat > sights;for( Object *obj = TheGameLogic->getFirstObject(); obj != NULL; obj = obj->getNextObject() ){- if( narrowTo != NULL && home == NULL && obj->getControllingPlayer() == narrowTo- && obj->isKindOf( KINDOF_COMMANDCENTER ) )- home = obj;+ const Int cost = obj->getTemplate()->friend_getBuildCost();+ const Bool superweapon = obj->isKindOf( KINDOF_FS_SUPERWEAPON );+ const Bool mine = narrowTo == NULL || obj->getControllingPlayer() == narrowTo;+ DirectorHeat heat;+ heat.position.x = obj->getPosition()->x;+ heat.position.y = obj->getPosition()->y;++ if( mine && cost > 0 && !obj->isEffectivelyDead() )+ {+ const AIUpdateInterface *ai = obj->getAIUpdateInterface();+ const Bool marching = ai != NULL && ai->isMoving() && obj->isAbleToAttack();+ const Bool busy = marching || obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION );+ heat.weight = ObserverCamera_sightWeight( cost, obj->isKindOf( KINDOF_STRUCTURE ), busy, superweapon );+ sights.push_back( heat );+ }const BodyModuleInterface *body = obj->getBodyModule();const UnsignedInt hitAt = body->getLastDamageTimestamp();if( hitAt == 0 || frame >= hitAt + DIRECTOR_HEAT_FRAMES )continue;- if( narrowTo != NULL && obj->getControllingPlayer() != narrowTo- && ( body->getLastDamageInfo()->in.m_sourcePlayerMask & narrowTo->getPlayerMask() ) == 0 )+ if( !mine && ( body->getLastDamageInfo()->in.m_sourcePlayerMask & narrowTo->getPlayerMask() ) == 0 )continue;- DirectorHeat hit;- hit.position.x = obj->getPosition()->x;- hit.position.y = obj->getPosition()->y;- hit.weight = 1.0f;- hits.push_back( hit );+ heat.weight = ObserverCamera_hitWeight( cost, obj->isEffectivelyDead(), superweapon );+ hits.push_back( heat );}Coord2D hottest;Real hottestHeat = 0.0f;ObserverCamera_hottestPlace( hits, &hottest, &hottestHeat );+ const UnsignedInt held = frame - m_placeSince;Coord2D followed = m_place;- const Real heatHere = m_placeValid ? ObserverCamera_heatAround( hits, m_place, &followed ) : 0.0f;- if( !m_placeValid || ObserverCamera_shouldMove( heatHere, hottestHeat, frame - m_placeSince ) )+ if( m_placeValid && m_placeIsFight ){- if( hottestHeat > 0.0f )+ const Real heatHere = ObserverCamera_heatAround( hits, m_place, &followed );+ if( heatHere > 0.0f && ( sameFight( hottest, followed ) || !ObserverCamera_shouldMove( heatHere, hottestHeat, held ) ) ){- DEBUG_LOG(( "OBSCAM frame %u director to (%.0f,%.0f) heat %.0f, was %.0f here\n",- frame, hottest.x, hottest.y, hottestHeat, heatHere ));- m_place = hottest;- m_placeSince = frame;- m_placeValid = TRUE;+ m_place = followed;+ *place = m_place;+ return TRUE;}- else if( home != NULL )+ }+ else if( m_placeValid && hottestHeat <= 0.0f && held < DIRECTOR_SIGHT_FRAMES )+ {+ if( ObserverCamera_heatAround( sights, m_place, &followed ) > 0.0f )+ m_place = followed;+ *place = m_place;+ return TRUE;+ }++ if( hottestHeat > 0.0f )+ {+ DEBUG_LOG(( "OBSCAM frame %u director to fight (%.0f,%.0f) heat %.1f\n", frame, hottest.x, hottest.y, hottestHeat ));+ m_place = hottest;+ m_placeIsFight = TRUE;+ }+ else+ {+ Coord2D sight;+ if( !ObserverCamera_nextSight( sights, m_seen, &sight ) ){- m_place.x = home->getPosition()->x;- m_place.y = home->getPosition()->y;- m_placeSince = frame;- m_placeValid = TRUE;+ m_seen.clear();+ if( !ObserverCamera_nextSight( sights, m_seen, &sight ) )+ {+ m_placeValid = FALSE;+ return FALSE;+ }}+ DEBUG_LOG(( "OBSCAM frame %u director to sight (%.0f,%.0f)\n", frame, sight.x, sight.y ));+ m_place = sight;+ m_placeIsFight = FALSE;+ m_seen.push_back( sight );+ if( m_seen.size() > DIRECTOR_SEEN_COUNT )+ m_seen.erase( m_seen.begin() );}- else- m_place = followed;-+ m_placeSince = frame;+ m_placeValid = TRUE;*place = m_place;- return m_placeValid;+ return TRUE;}//-------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/Tests/test_observer_camera.inc b/GeneralsMD/Code/Tests/test_observer_camera.incindex 90f051151..3880d9c81 100644--- a/GeneralsMD/Code/Tests/test_observer_camera.inc+++ b/GeneralsMD/Code/Tests/test_observer_camera.inc@@ -41,6 +41,71 @@ TEST(the_director_holds_a_live_fight_and_leaves_a_dead_one)CHECK( ObserverCamera_shouldMove( 4.0f, 7.0f, longHeld ) );}+TEST(the_director_leaves_early_for_a_much_bigger_fight_and_late_for_any_bigger_one)+{+ const UnsignedInt settled = 3 * LOGICFRAMES_PER_SECOND;+ const UnsignedInt tired = 25 * LOGICFRAMES_PER_SECOND;+ CHECK( ObserverCamera_shouldMove( 4.0f, 13.0f, settled ) );+ CHECK( !ObserverCamera_shouldMove( 4.0f, 7.0f, settled ) );+ CHECK( ObserverCamera_shouldMove( 4.0f, 5.0f, tired ) );+ CHECK( !ObserverCamera_shouldMove( 4.0f, 4.2f, tired ) );+}++TEST(the_director_goes_where_more_is_at_stake_not_where_more_was_hit)+{+ // six rifles plinked at one end, two Overlords killed at the other+ std::vector< DirectorHeat > hits;+ for( Int each = 0; each < 6; each++ )+ hits.push_back( directorHit( 1000.0f + each * 10.0f, 1000.0f ) );+ for( Int each = 0; each < 2; each++ )+ {+ DirectorHeat hit = directorHit( 3000.0f + each * 10.0f, 3000.0f );+ hit.weight = ObserverCamera_hitWeight( 2000, TRUE, FALSE );+ hits.push_back( hit );+ }+ for( Int each = 0; each < 6; each++ )+ hits[ each ].weight = ObserverCamera_hitWeight( 225, FALSE, FALSE );++ Coord2D place;+ Real heat = 0.0f;+ CHECK( ObserverCamera_hottestPlace( hits, &place, &heat ) );+ CHECK_NEAR( place.y, 3000.0f, 0.001f );+ CHECK( ObserverCamera_hitWeight( 5000, FALSE, TRUE ) > ObserverCamera_hitWeight( 5000, FALSE, FALSE ) );+}++TEST(the_director_with_no_fight_goes_round_the_sights_and_never_parks)+{+ std::vector< DirectorHeat > sights;+ DirectorHeat army = directorHit( 1000.0f, 1000.0f );+ army.weight = ObserverCamera_sightWeight( 4000, FALSE, TRUE, FALSE );+ DirectorHeat base = directorHit( 3000.0f, 3000.0f );+ base.weight = ObserverCamera_sightWeight( 6000, TRUE, FALSE, FALSE );+ DirectorHeat silo = directorHit( 5000.0f, 1000.0f );+ silo.weight = ObserverCamera_sightWeight( 5000, TRUE, FALSE, TRUE );+ sights.push_back( base );+ sights.push_back( army );+ sights.push_back( silo );++ // the marching army first, then the superweapon, then the base, then nothing new+ std::vector< Coord2D > seen;+ Coord2D place;+ CHECK( ObserverCamera_nextSight( sights, seen, &place ) );+ CHECK_NEAR( place.x, 1000.0f, 0.001f );+ seen.push_back( place );+ CHECK( ObserverCamera_nextSight( sights, seen, &place ) );+ CHECK_NEAR( place.x, 5000.0f, 0.001f );+ seen.push_back( place );+ CHECK( ObserverCamera_nextSight( sights, seen, &place ) );+ CHECK_NEAR( place.x, 3000.0f, 0.001f );+ seen.push_back( place );+ CHECK( !ObserverCamera_nextSight( sights, seen, &place ) );++ // with the oldest dropped the army comes round again+ seen.erase( seen.begin() );+ CHECK( ObserverCamera_nextSight( sights, seen, &place ) );+ CHECK_NEAR( place.x, 1000.0f, 0.001f );+}+TEST(the_observer_camera_pans_a_short_way_and_cuts_a_long_one){ViewLocation from;