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): aim a building with the drag, and keep the cursor visible
Olcay Seygan3898781b8039
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.hindex 849e5b853..1b51dec55 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h@@ -440,6 +440,20 @@ public: // ********************************************************************virtual Bool isPlacementAnchored( void ); ///< is placement arrow anchor setvirtual void getPlacementPoints( ICoord2D *start, ICoord2D *end );///< get the placemnt arrow pointsvirtual Real getPlacementAngle( void ); ///< placement angle of drawable at cursor when placing down structures+ virtual Real computePlacementAngle( const ICoord2D *start, const ICoord2D *end ); ///< heading a drag from start to end aims a structure at++ /** Round a heading to the nearest eighth of a turn. Nearest, not toward or away from zero:+ * toAngle returns -PI..PI and the drag has to feel the same on both halves of the circle, so+ * every heading is at most 22.5 degrees of mouse travel from the one it snaps to. Inline so a+ * test can reach it without linking the whole in-game UI. */+ static Real snapAngleTo45( Real angle )+ {+ const Real step = PI / 4.0f;++ return ((Real)REAL_TO_INT_FLOOR( angle / step + 0.5f )) * step;+ }+ virtual void setPlacementAngle( Real angle ); ///< aim the structure on the cursor, and keep that heading for the next one+ virtual Bool rotatePendingPlacement( Int steps ); ///< turn the structure on the cursor by 45 degrees a step// Drawable selection mechanismsvirtual void selectDrawable( Drawable *draw ); ///< Mark given Drawable as "selected"@@ -748,6 +762,8 @@ protected:Bool m_placeAnchorInProgress; ///< is place angle interface for placement activeICoord2D m_placeAnchorStart; ///< place angle anchor startICoord2D m_placeAnchorEnd; ///< place angle anchor end+ Real m_placeAngleOffset; ///< wheel-chosen heading, added to every structure's own view angle+ Bool m_placementLegal; ///< last legality verdict for the spot under the structure being placedInt m_selectCount; ///< Number of objects currently "selected"Int m_maxSelectCount; ///< Max number of objects to selectUnsignedInt m_frameSelectionChanged; ///< Frame when the selection last changed.diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex 304f6c795..c3d7c1de4 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -103,6 +103,25 @@static const Real placementOpacity = 0.45f;static const RGBColor illegalBuildColor = { 1.0, 0.0, 0.0 };+//-------------------------------------------------------------------------------------------------+/** Pointer to show while a structure rides the cursor. Mouse::BUILD_PLACEMENT and+ * INVALID_BUILD_PLACEMENT are two names in CursorININames[] that no shipped Mouse.ini ever+ * defines, so asking for them left m_cursorInfo empty and drew nothing at all - the pointer+ * simply vanished for as long as something was being placed. Honour the build cursors when art+ * for them does exist (a mod may add it), and otherwise fall back to cursors that are always+ * there: the crosshair for a spot that can be built on, the no-go pointer for one that cannot. */+//-------------------------------------------------------------------------------------------------+static Mouse::MouseCursor placementCursor( Bool legal )+{+ Mouse::MouseCursor wanted = legal ? Mouse::BUILD_PLACEMENT : Mouse::INVALID_BUILD_PLACEMENT;++ if( TheMouse->m_cursorInfo[ wanted ].cursorName.isEmpty() == FALSE )+ return wanted;++ return legal ? Mouse::CROSS : Mouse::GENERIC_INVALID;++} // end placementCursor+//-------------------------------------------------------------------------------------------------/// The InGameUI singleton instance.InGameUI *TheInGameUI = NULL;@@ -1002,6 +1021,8 @@ InGameUI::InGameUI()m_placeAnchorStart.x = m_placeAnchorStart.y = 0;m_placeAnchorEnd.x = m_placeAnchorEnd.y = 0;m_placeAnchorInProgress = FALSE;+ m_placeAngleOffset = 0.0f;+ m_placementLegal = TRUE;m_videoStream = NULL;m_videoBuffer = NULL;@@ -1525,27 +1546,7 @@ void InGameUI::handleBuildPlacements( void )// only adjust angle if we've actually moved the mouseif( start.x != end.x || start.y != end.y )- {- Coord3D worldStart, worldEnd;-- // project the start and the end points of the line anchor into the 3D world- TheTacticalView->screenToTerrain( &start, &worldStart );- TheTacticalView->screenToTerrain( &end, &worldEnd );-- Coord2D v;- v.x = worldEnd.x - worldStart.x;- v.y = worldEnd.y - worldStart.y;- angle = v.toAngle();-- // optional 45 degree snap (SnapBuildPlacementTo45 in Options.ini) - lines walls and- // defenses up with the base instead of leaving them at whatever the drag produced.- if( TheGlobalData->m_snapBuildPlacementTo45 )- {- const Real step = PI / 4.0f;- angle = ((Real)REAL_TO_INT_FLOOR( angle / step + (angle >= 0.0f ? 0.5f : -0.5f) )) * step;- }-- } // end if+ angle = computePlacementAngle( &start, &end );} // end ifelse@@ -1591,6 +1592,9 @@ void InGameUI::handleBuildPlacements( void )BuildAssistant::IGNORE_STEALTHED,builderObject,NULL );+ // the cursor reads this too - see createCommandHint's MOUSEMODE_BUILD_PLACE case+ m_placementLegal = ( lbc == LBC_OK );+if( lbc != LBC_OK )m_placeIcon[ 0 ]->colorTint( &illegalBuildColor );else@@ -2050,6 +2054,7 @@ void InGameUI::reset( void )// remove any build available statusplaceBuildAvailable( NULL, NULL );+ m_placeAngleOffset = 0.0f;// free any message resources allocatedfreeMessageResources();@@ -2863,18 +2868,13 @@ void InGameUI::createCommandHint( const GameMessage *msg )setMouseCursor(Mouse::ARROW);return;}- switch (t)- {- case GameMessage::MSG_DO_MOVETO_HINT:- case GameMessage::MSG_DO_ATTACKMOVETO_HINT:- case GameMessage::MSG_ADD_WAYPOINT:- setMouseCursor(Mouse::BUILD_PLACEMENT);- break;- case GameMessage::MSG_DO_ATTACK_OBJECT_HINT:- case GameMessage::MSG_DO_ATTACK_OBJECT_AFTER_MOVING_HINT:- setMouseCursor(Mouse::INVALID_BUILD_PLACEMENT);- break;- }+ //+ // what is under the cursor does not matter while a structure is on it - whether the+ // spot can be built on does, and handleBuildPlacements already worked that out for+ // the tint on the ghost. Anything the hint said would have left the pointer with no+ // art at all: see placementCursor.+ //+ setMouseCursor( placementCursor( m_placementLegal ) );}break;case MOUSEMODE_GUI_COMMAND:@@ -3177,7 +3177,8 @@ void InGameUI::placeBuildAvailable( const ThingTemplate *build, Drawable *buildDif( build ){m_mouseMode = MOUSEMODE_BUILD_PLACE;- m_mouseModeCursor = Mouse::CROSS;+ m_placementLegal = TRUE;+ m_mouseModeCursor = placementCursor( TRUE );Drawable *draw;@@ -3185,7 +3186,7 @@ void InGameUI::placeBuildAvailable( const ThingTemplate *build, Drawable *buildDTheMouse->capture();// hack for changing cursor- setMouseCursor( Mouse::CROSS );+ setMouseCursor( (Mouse::MouseCursor)m_mouseModeCursor );// deselect all drawables, otherwise they move to the place we click///@ todo when message stream order more formalized eliminate this@@ -3213,6 +3214,10 @@ void InGameUI::placeBuildAvailable( const ThingTemplate *build, Drawable *buildD// don't forget to take into account the current view angle// angle += TheTacticalView->getAngle(); Don't do this - makes odd angled building placements. jba.+ // carry over whatever heading the player wheeled to last time, so a row of walls or+ // bunkers can be laid down all facing the same way (see rotatePendingPlacement)+ angle = normalizeAngle( angle + m_placeAngleOffset );+// set the angle in the icon we just createddraw->setOrientation( angle );@@ -3340,6 +3345,78 @@ Real InGameUI::getPlacementAngle( void )} // end getPlacementAngle+//-------------------------------------------------------------------------------------------------+/** The heading a drag from 'start' to 'end' (both in screen pixels) aims a structure at. The two+ * points are projected onto the terrain first, so the answer is a world heading and not a screen+ * one: dragging "up" the screen faces the building away from the camera whatever the camera is+ * turned to. */+//-------------------------------------------------------------------------------------------------+Real InGameUI::computePlacementAngle( const ICoord2D *start, const ICoord2D *end )+{+ Coord3D worldStart, worldEnd;++ TheTacticalView->screenToTerrain( start, &worldStart );+ TheTacticalView->screenToTerrain( end, &worldEnd );++ Coord2D v;+ v.x = worldEnd.x - worldStart.x;+ v.y = worldEnd.y - worldStart.y;++ Real angle = v.toAngle();++ // optional 45 degree snap (SnapBuildPlacementTo45 in Options.ini) - lines walls and+ // defenses up with the base instead of leaving them at whatever the drag produced.+ if( TheGlobalData->m_snapBuildPlacementTo45 )+ angle = snapAngleTo45( angle );++ return angle;++} // end computePlacementAngle++//-------------------------------------------------------------------------------------------------+/** Aim the structure sitting on the cursor at 'angle', and keep that heading for the placements+ * that follow - the same offset the wheel writes (see rotatePendingPlacement), so a wall aimed by+ * dragging carries on in the direction it was aimed instead of snapping back to the template's own+ * view angle on the next piece. */+//-------------------------------------------------------------------------------------------------+void InGameUI::setPlacementAngle( Real angle )+{+ if( m_pendingPlaceType == NULL )+ return;++ m_placeAngleOffset = normalizeAngle( angle - m_pendingPlaceType->getPlacementViewAngle() );++ for( Int i = 0; i < TheGlobalData->m_maxLineBuildObjects; i++ )+ if( m_placeIcon[ i ] )+ m_placeIcon[ i ]->setOrientation( angle );++} // end setPlacementAngle++//-------------------------------------------------------------------------------------------------+/** Turn the structure sitting on the cursor by 'steps' eighths of a turn. The chosen heading is+ * kept for the placements that follow, so a row of walls or a line of bunkers can be laid down+ * facing the same way without re-aiming each one - it is an offset rather than an absolute angle+ * so that each structure still starts from its own designed view angle. Does nothing while the+ * drag-to-aim anchor is down: that interface recomputes the angle from the drag every frame and+ * would throw this away on the next one. */+//-------------------------------------------------------------------------------------------------+Bool InGameUI::rotatePendingPlacement( Int steps )+{+ const Real step = PI / 4.0f;++ if( steps == 0 || m_pendingPlaceType == NULL || m_placeIcon[ 0 ] == NULL || isPlacementAnchored() )+ return FALSE;++ m_placeAngleOffset = normalizeAngle( m_placeAngleOffset + steps * step );++ for( Int i = 0; i < TheGlobalData->m_maxLineBuildObjects; i++ )+ if( m_placeIcon[ i ] )+ m_placeIcon[ i ]->setOrientation( normalizeAngle( m_placeIcon[ i ]->getOrientation() + steps * step ) );++ return TRUE;++} // end rotatePendingPlacement+//-------------------------------------------------------------------------------------------------/** Mark given Drawable as "selected". *///-------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cppindex 25815eb44..ebc13a86d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp@@ -44,6 +44,7 @@#include "GameClient/Shell.h"#include "GameClient/GameClient.h"#include "GameClient/KeyDefs.h"+#include "GameClient/Keyboard.h" // for the ctrl+wheel building rotation#include "GameClient/View.h"#include "GameClient/Drawable.h"#include "GameClient/LookAtXlat.h"@@ -385,6 +386,14 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessageInt spin = msg->getArgument( 1 )->integer;+ //+ // Ctrl+wheel turns the structure on the cursor by 45 degrees a notch instead of zooming.+ // The wheel is otherwise wasted while placing, and the drag-to-aim interface it replaces+ // cannot hit an exact eighth of a turn. Eaten so the same notch does not also zoom.+ //+ if (TheKeyboard->isCtrl() && TheInGameUI->rotatePendingPlacement( spin ))+ return DESTROY_MESSAGE;+//// ZoomToCursor: remember the world point under the cursor, zoom, then shift the camera// by however far that point moved, so it stays put. Doing it by measurement ratherdiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cppindex 871986c0d..e25caf2cf 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp@@ -40,6 +40,7 @@#include "GameClient/ControlBar.h"#include "GameClient/Drawable.h"#include "GameClient/Eva.h"+#include "GameClient/Mouse.h"#include "GameClient/PlaceEventTranslator.h"#include "GameLogic/GameLogic.h"@@ -54,6 +55,39 @@//#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")#endif+//-------------------------------------------------------------------------------------------------+/** How far from the anchor the cursor has to travel before the drag counts as aiming the structure+ * rather than as a plain click, in pixels. */+static const Int PLACEMENT_DRAG_THRESHOLD_DIST = 5;++//-------------------------------------------------------------------------------------------------+/** How far the cursor is from the placement anchor right now, in pixels. This reads the mouse+ * rather than the pixel carried by the message on purpose: Mouse::createStreamMessages appends+ * MSG_RAW_MOUSE_POSITION before it processes this frame's events, so that pixel is already one+ * batch of movement old, and the structure would be aimed at where the cursor had been rather than+ * where it was let go. */+//-------------------------------------------------------------------------------------------------+static Bool getPlacementDrag( ICoord2D *start, ICoord2D *end )+{+ ICoord2D mouse = TheMouse->getMouseStatus()->pos;++ TheInGameUI->getPlacementPoints( start, NULL );++ Int x = mouse.x - start->x;+ Int y = mouse.y - start->y;++ if( ( x * x ) + ( y * y ) < PLACEMENT_DRAG_THRESHOLD_DIST * PLACEMENT_DRAG_THRESHOLD_DIST )+ {+ // no drag: there is one point, not two+ *end = *start;+ return FALSE;+ }++ *end = mouse;+ return TRUE;++} // end getPlacementDrag+//-------------------------------------------------------------------------------------------------PlaceEventTranslator::PlaceEventTranslator() : m_frameOfUpButton(-1), m_stripClickTaken(FALSE){@@ -194,12 +228,27 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMessICoord2D anchorStart, anchorEnd;Bool isLineBuild = TheBuildAssistant->isLineBuildTemplate( build );- // get the angle of the drawable at the cursor to use as the initial angle- angle = TheInGameUI->getPlacementAngle();-// get start point from the anchor arrow used to place and select anglesTheInGameUI->getPlacementPoints( &anchorStart, &anchorEnd );+ //+ // The structure faces wherever the player dragged to. Take that from where the+ // cursor is at this instant rather than from the heading the ghost happens to be+ // wearing: the ghost is turned once a frame in InGameUI::update, which runs before+ // the message stream is propagated and off an anchor end that is itself a frame+ // behind, so a quick drag-and-release used to build the structure facing two frames+ // of mouse travel back. A press that never left the anchor is not aiming at+ // anything, so that one keeps the heading already on the ghost.+ //+ angle = TheInGameUI->getPlacementAngle();+ if( getPlacementDrag( &anchorStart, &anchorEnd ) )+ {+ angle = TheInGameUI->computePlacementAngle( &anchorStart, &anchorEnd );++ // and the next one goes down facing the same way, as with the wheel+ TheInGameUI->setPlacementAngle( angle );+ }+// translate the screen position of start to world target locationTheTacticalView->screenToTerrain( &anchorStart, &world );@@ -349,33 +398,26 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess}//---------------------------------------------------------------------------------------------+ case GameMessage::MSG_RAW_MOUSE_LEFT_DRAG:case GameMessage::MSG_RAW_MOUSE_POSITION:{// if a building placement is in progress update the destination positionif (TheInGameUI->isPlacementAnchored()){- const Int PLACEMENT_DRAG_THRESHOLD_DIST = 5; // in pixels away from anchor point- ICoord2D mouse = msg->getArgument(0)->pixel;-//// we will only process placement end point sets (clicking, and dragging to set angles)// if we have moved far enough away from the start point//- ICoord2D start;- TheInGameUI->getPlacementPoints( &start, NULL );-- Int x, y;- x = mouse.x - start.x;- y = mouse.y - start.y;- if( sqrt( (x * x) + (y * y) ) >= PLACEMENT_DRAG_THRESHOLD_DIST )+ ICoord2D start, end;+ if( getPlacementDrag( &start, &end ) ){-- TheInGameUI->setPlacementEnd(&mouse);++ TheInGameUI->setPlacementEnd( &end );disp = DESTROY_MESSAGE;} // end if- }+ }break;}}