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.

feat(gameengine): quantize the camera heading live instead of on release

Olcay Seyganaac818f086b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b65e71ea7..950b6a351 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -728,6 +728,15 @@ camera went somewhere else instead. The spot under your pointer is now
held there for the whole glide. Roll the wheel over the far corner of your base and you end up at
that corner, not in the middle of the screen.
+**The camera now turns in whole steps, the way a castle-builder's does.** With 45-degree snapping on,
+the view used to swing freely while you held the key or dragged the mouse and then jump to the
+nearest of the eight headings when you let go — so you spent the turn looking at an angle you were
+never going to keep, and the jump at the end came as a surprise. It turns one eighth at a time now,
+instantly, and holding the key keeps stepping. There is no in-between angle to see and nothing left
+to settle when you release. Turning the other way past north also used to skip a notch: a heading a
+few degrees left of north rounded a whole step further left instead of back to north. Free rotation
+is still there for anyone who wants it — turn the snapping off.
+
**A skirmish opens zoomed all the way out.** The game used to drop you at the map author's own
camera height, which is tighter than the wheel will go, so the first thing anyone did on every
single map was roll the wheel back to see their base and the ground around it. It now opens at the
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 6cdac8eb8..9b8c80b75 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -610,8 +610,10 @@ public: // ********************************************************************
Bool isInAttackMoveToMode( void ) const { return m_attackMoveToMode; }
void clearAttackMoveToMode( void ) { m_attackMoveToMode = FALSE; }
- void setCameraRotateLeft( Bool set ) { m_cameraRotatingLeft = set; }
- void setCameraRotateRight( Bool set ) { m_cameraRotatingRight = set; }
+ // zeroing the repeat clock makes the first quantized step happen on the very next update, so a
+ // tap of the key is one eighth and a hold is one eighth every CAMERA_SNAP_REPEAT_MS.
+ void setCameraRotateLeft( Bool set ) { m_cameraRotatingLeft = set; m_cameraSnapRepeatMs = 0; }
+ void setCameraRotateRight( Bool set ) { m_cameraRotatingRight = set; m_cameraSnapRepeatMs = 0; }
void setCameraZoomIn( Bool set ) { m_cameraZoomingIn = set; }
void setCameraZoomOut( Bool set ) { m_cameraZoomingOut = set; }
void setCameraTrackingDrawable( Bool set ) { m_cameraTrackingDrawable = set; }
@@ -971,6 +973,9 @@ protected:
// time instead of by render frames. 0 = no previous update yet.
UnsignedInt m_cameraKeyLastMs;
+ // wall clock of the last quantized rotate step under SnapCameraRotateTo45. 0 = step now.
+ UnsignedInt m_cameraSnapRepeatMs;
+
Bool m_cameraRotatingLeft;
Bool m_cameraRotatingRight;
Bool m_cameraZoomingIn;
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/LookAtXlat.h b/GeneralsMD/Code/GameEngine/Include/GameClient/LookAtXlat.h
index 04d7183c4..8fd2ffb03 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/LookAtXlat.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/LookAtXlat.h
@@ -80,6 +80,7 @@ private:
ICoord2D m_currentPos;
Bool m_isScrolling; // set to true if we are in the act of RMB scrolling
Bool m_isRotating; // set to true if we are in the act of MMB rotating
+ Real m_freeRotateAngle; // heading the drag has asked for, before SnapCameraRotateTo45 quantizes it
Bool m_isPitching; // set to true if we are in the act of ALT pitch rotation
Bool m_isChangingFOV; // set to true if we are in the act of changing the field of view
UnsignedInt m_timestamp; // set when button goes down
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
index 2aeed5171..3706cf116 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/View.h
@@ -335,6 +335,13 @@ class ViewLocation
}
};
+// CAMERA HEADING QUANTIZATION ////////////////////////////////////////////////////////////////////
+// SnapCameraRotateTo45 keeps the camera heading on the eighths. Free functions rather than View
+// methods so the arithmetic can be tested without a View, and so the keyboard rotate and the
+// middle-drag rotate share exactly one copy of it.
+extern Real View_snapAngleToEighth( Real angle ); ///< nearest multiple of 45 degrees
+extern Real View_stepAngleByEighths( Real angle, Int steps ); ///< snap, then move that many eighths
+
// EXTERNALS //////////////////////////////////////////////////////////////////////////////////////
extern View *TheTacticalView; ///< the main tactical interface to the game world
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index a90d02d5e..cdc4f41a6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1065,6 +1065,7 @@ InGameUI::InGameUI()
m_hudLastSampleFrame = 0;
m_hudLastSampleMs = 0;
m_cameraKeyLastMs = 0;
+ m_cameraSnapRepeatMs = 0;
m_subtitleFreezeStartMs = 0;
m_subtitleFreezeSteps = 0;
m_hudFps = 0.0f;
@@ -2118,15 +2119,36 @@ void InGameUI::update( void )
if( cameraSteps > 4.0f )
cameraSteps = 4.0f;
- if( m_cameraRotatingLeft && !m_cameraRotatingRight )
+ //
+ // SnapCameraRotateTo45 makes the heading discrete rather than smoothed-then-settled: the camera
+ // stands on an eighth and jumps to the next one, so there is no in-between heading to look at
+ // and nothing left to settle when the key comes up. The angle is therefore not accumulated per
+ // frame at all - a press turns the view one eighth and holding the key repeats that on a fixed
+ // interval, which is what the rotate keys do in Stronghold.
+ //
+ const UnsignedInt CAMERA_SNAP_REPEAT_MS = 250;
+ if( TheGlobalData->m_snapCameraRotateTo45 )
{
- //Keyboard rotate left
- TheTacticalView->setAngle( TheTacticalView->getAngle() - TheGlobalData->m_keyboardCameraRotateSpeed * cameraSteps );
+ if( (m_cameraRotatingLeft || m_cameraRotatingRight) && m_cameraRotatingLeft != m_cameraRotatingRight
+ && cameraNowMs - m_cameraSnapRepeatMs >= CAMERA_SNAP_REPEAT_MS )
+ {
+ m_cameraSnapRepeatMs = cameraNowMs;
+ TheTacticalView->setAngle( View_stepAngleByEighths( TheTacticalView->getAngle(),
+ m_cameraRotatingRight ? 1 : -1 ) );
+ }
}
- if( m_cameraRotatingRight && !m_cameraRotatingLeft )
+ else
{
- //Keyboard rotate right
- TheTacticalView->setAngle( TheTacticalView->getAngle() + TheGlobalData->m_keyboardCameraRotateSpeed * cameraSteps );
+ if( m_cameraRotatingLeft && !m_cameraRotatingRight )
+ {
+ //Keyboard rotate left
+ TheTacticalView->setAngle( TheTacticalView->getAngle() - TheGlobalData->m_keyboardCameraRotateSpeed * cameraSteps );
+ }
+ if( m_cameraRotatingRight && !m_cameraRotatingLeft )
+ {
+ //Keyboard rotate right
+ TheTacticalView->setAngle( TheTacticalView->getAngle() + TheGlobalData->m_keyboardCameraRotateSpeed * cameraSteps );
+ }
}
if( m_cameraZoomingIn && !m_cameraZoomingOut )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
index b0a8a8832..62a5bc86d 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
@@ -2361,21 +2361,6 @@ GameMessage::Type CommandTranslator::evaluateContextCommand( Drawable *draw,
* The Command Translator translates mouse events into object command messages
* such as move_to, attack, etc.
*/
-//-------------------------------------------------------------------------------------------------
-/** SnapCameraRotateTo45: settle the camera heading on the nearest eighth when a rotate ends.
- * Applies to the keyboard rotate as well as the middle-drag one, because MiddleMousePans takes
- * the drag away and the snap would otherwise have nothing left to fire on. */
-//-------------------------------------------------------------------------------------------------
-static void snapCameraHeadingTo45( void )
-{
- if( !TheGlobalData->m_snapCameraRotateTo45 || TheTacticalView == NULL )
- return;
-
- const Real step = PI / 4.0f;
- Real a = TheTacticalView->getAngle();
- TheTacticalView->setAngle( ((Real)REAL_TO_INT_FLOOR( a / step + (a >= 0.0f ? 0.5f : -0.5f) )) * step );
-}
-
GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage *msg)
{
GameMessage::Type t = msg->getType();
@@ -3360,7 +3345,6 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
case GameMessage::MSG_META_END_CAMERA_ROTATE_LEFT:
DEBUG_ASSERTCRASH(TheInGameUI->isCameraRotatingLeft(), ("Clearing rotate camera left, but it's already clear!"));
TheInGameUI->setCameraRotateLeft( false );
- snapCameraHeadingTo45();
break;
case GameMessage::MSG_META_BEGIN_CAMERA_ROTATE_RIGHT:
DEBUG_ASSERTCRASH(!TheInGameUI->isCameraRotatingRight(), ("Setting rotate camera right, but it's already set!"));
@@ -3369,7 +3353,6 @@ GameMessageDisposition CommandTranslator::translateGameMessage(const GameMessage
case GameMessage::MSG_META_END_CAMERA_ROTATE_RIGHT:
DEBUG_ASSERTCRASH(TheInGameUI->isCameraRotatingRight(), ("Clearing rotate camera right, but it's already clear!"));
TheInGameUI->setCameraRotateRight( false );
- snapCameraHeadingTo45();
break;
case GameMessage::MSG_META_BEGIN_CAMERA_ZOOM_IN:
DEBUG_ASSERTCRASH(!TheInGameUI->isCameraZoomingIn(), ("Setting zoom camera in, but it's already set!"));
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
index ab0cb8b69..918f2608e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
@@ -105,6 +105,7 @@ void LookAtTranslator::stopScrolling( void )
LookAtTranslator::LookAtTranslator() :
m_isScrolling(false),
m_isRotating(false),
+ m_freeRotateAngle(0.0f),
m_isPitching(false),
m_isChangingFOV(false),
m_timestamp(0),
@@ -331,6 +332,9 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
else
{
m_isRotating = true;
+ // the drag turns this, and under SnapCameraRotateTo45 the camera stands on whichever
+ // eighth it is nearest - so start it where the camera already is.
+ m_freeRotateAngle = TheTacticalView->getAngle();
}
break;
}
@@ -343,7 +347,6 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
const UnsignedInt CLICK_DURATION = 5;
const UnsignedInt PIXEL_OFFSET = 5;
- Bool wasRotating = m_isRotating;
m_isRotating = false;
if (m_scrollType == SCROLL_MMB)
stopScrolling();
@@ -357,14 +360,8 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
TheTacticalView->setAngleAndPitchToDefault();
TheTacticalView->setZoomToDefault();
}
- else if (wasRotating && TheGlobalData->m_snapCameraRotateTo45)
- {
- // SnapCameraRotateTo45: let go of a free rotate and the heading clicks to the
- // nearest eighth, so the map lines up with the screen again.
- const Real step = PI / 4.0f;
- Real a = TheTacticalView->getAngle();
- TheTacticalView->setAngle( ((Real)REAL_TO_INT_FLOOR( a / step + (a >= 0.0f ? 0.5f : -0.5f) )) * step );
- }
+ // nothing to settle on release: under SnapCameraRotateTo45 the heading was already on an
+ // eighth for the whole drag.
break;
}
@@ -414,7 +411,17 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
Real angle = FACTOR * (m_currentPos.x - m_anchor.x);
- TheTacticalView->setAngle( TheTacticalView->getAngle() + angle );
+ if (TheGlobalData->m_snapCameraRotateTo45)
+ {
+ // discrete heading: the drag turns an angle we keep to ourselves and the camera
+ // jumps to the eighth it is nearest, as the mouse crosses each halfway point.
+ m_freeRotateAngle += angle;
+ TheTacticalView->setAngle( View_snapAngleToEighth( m_freeRotateAngle ) );
+ }
+ else
+ {
+ TheTacticalView->setAngle( TheTacticalView->getAngle() + angle );
+ }
m_anchor = msg->getArgument( 0 )->pixel;
}
@@ -821,6 +828,7 @@ void LookAtTranslator::resetModes()
{
m_isScrolling = FALSE;
m_isRotating = FALSE;
+ m_freeRotateAngle = 0.0f;
m_isPitching = FALSE;
m_isChangingFOV = FALSE;
}
\ No newline at end of file
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/View.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/View.cpp
index a991400a6..0e5c02fde 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/View.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/View.cpp
@@ -161,6 +161,26 @@ void View::scrollBy( Coord2D *delta )
m_pos.y += delta->y;
}
+/**
+ * SnapCameraRotateTo45: the nearest multiple of 45 degrees. floor(x + 0.5) is the round for both
+ * signs - the earlier copies of this used floor(x - 0.5) below zero, which is not the nearest one
+ * but the next one away from zero, so a heading of -5 degrees snapped to -45 instead of 0.
+ */
+Real View_snapAngleToEighth( Real angle )
+{
+ const Real step = PI / 4.0f;
+ return ((Real)REAL_TO_INT_FLOOR( angle / step + 0.5f )) * step;
+}
+
+/**
+ * SnapCameraRotateTo45: the eighth that is 'steps' away from the one this angle is nearest to.
+ */
+Real View_stepAngleByEighths( Real angle, Int steps )
+{
+ const Real step = PI / 4.0f;
+ return View_snapAngleToEighth( angle ) + steps * step;
+}
+
/**
* Rotate the view around the up axis by the given angle.
*/
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 93f14c393..c8a7a352f 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -57,6 +57,7 @@
#include "GameClient/ParticleSys.h"
#include "GameClient/ControlBar.h"
#include "GameClient/InGameUI.h"
+#include "GameClient/View.h"
#include "GameLogic/IncomingDamage.h"
#include "GameLogic/AIPlayer.h"
#include "GameLogic/AIPathfind.h"
@@ -4644,3 +4645,55 @@ TEST(a_placed_structures_exit_point_turns_with_the_structure)
// the two points are distinct, which is what tells the renderer to draw a line and not just a puck
CHECK( !(exitLoc == rallyLoc) );
}
+
+/** SnapCameraRotateTo45 quantizes the camera heading instead of easing to it, so the arithmetic is
+ the whole feature: the heading is always the nearest eighth, and a rotate key moves it exactly one
+ eighth from wherever it stands. Both signs matter - the old copies of this rounded the wrong way
+ below zero, which put a heading of a few degrees left of north a whole eighth further left. */
+TEST(camera_heading_snaps_to_the_nearest_eighth_on_both_sides_of_zero)
+{
+ const Real step = PI / 4.0f;
+
+ CHECK_NEAR( View_snapAngleToEighth( 0.0f ), 0.0f, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( step ), step, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -step ), -step, 0.0001f );
+
+ // just off an eighth, either way, stays on it
+ CHECK_NEAR( View_snapAngleToEighth( 0.1f ), 0.0f, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -0.1f ), 0.0f, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( step + 0.1f ), step, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -step - 0.1f ), -step, 0.0001f );
+
+ // past the halfway point it belongs to the next one, on both sides
+ CHECK_NEAR( View_snapAngleToEighth( step * 0.6f ), step, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -step * 0.6f ), -step, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( step * 0.4f ), 0.0f, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -step * 0.4f ), 0.0f, 0.0001f );
+
+ CHECK_NEAR( View_snapAngleToEighth( 3.0f * step - 0.05f ), 3.0f * step, 0.0001f );
+ CHECK_NEAR( View_snapAngleToEighth( -3.0f * step + 0.05f ), -3.0f * step, 0.0001f );
+}
+
+/** One press of a rotate key is one eighth, from whatever the heading happens to be - including a
+ heading a script left off the grid, which is snapped first so the key never lands between two
+ eighths. */
+TEST(a_rotate_key_press_moves_the_camera_exactly_one_eighth)
+{
+ const Real step = PI / 4.0f;
+
+ CHECK_NEAR( View_stepAngleByEighths( 0.0f, 1 ), step, 0.0001f );
+ CHECK_NEAR( View_stepAngleByEighths( 0.0f, -1 ), -step, 0.0001f );
+ CHECK_NEAR( View_stepAngleByEighths( step, 1 ), 2.0f * step, 0.0001f );
+ CHECK_NEAR( View_stepAngleByEighths( -step, -1 ), -2.0f * step, 0.0001f );
+
+ // eight presses come back to where they started
+ Real a = 0.0f;
+ for( Int i = 0; i < 8; ++i )
+ a = View_stepAngleByEighths( a, 1 );
+ CHECK_NEAR( a, 2.0f * PI, 0.0001f );
+
+ // off the grid: snapped first, then stepped, so the result is still an eighth
+ CHECK_NEAR( View_stepAngleByEighths( 0.1f, 1 ), step, 0.0001f );
+ CHECK_NEAR( View_stepAngleByEighths( -0.1f, -1 ), -step, 0.0001f );
+ CHECK_NEAR( View_stepAngleByEighths( 0.0f, 0 ), 0.0f, 0.0001f );
+}