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): snap a placed structure flush against its neighbour

Olcay Seyganaecbc0d757b2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f459c707a..5a0981fe9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -555,7 +555,7 @@ found and fixed — EA's own, not port damage.**
- Unbuildable ground is a soft red wash, crossed out square by square.
- Running out of money no longer strands the ghost on the map.
- Holding shift and clicking out a row of buildings no longer stops halfway and selects one.
-- Hold shift and drag, and the row is one stroke. Press where the first building goes, drag the way the row runs, and a ghost appears for every building that fits end to end along the line, all facing the way the first one was turned. The row goes straight or on the diagonal and packs the buildings as close as their shape allows: one turned onto the line of the row stands wall to wall with the next, a factory keeps its door lane clear, and a building standing square to the build grid stays on it. It stops at the last building your bank can pay for. A ghost that lands on a rock or someone's tank turns red and is left out when you let go; the rest go up. With two or three workers selected the buildings are shared out to the nearest free one, and the ones nobody is free for wait as plans until a worker comes off its job. A plain drag still turns the building, and walls still tile the way they always did.
+- Hold shift and drag, and the row is one stroke. Press where the first building goes, drag the way the row runs, and a ghost appears for every building that fits end to end along the line, all facing the way the first one was turned. The row goes straight or on the diagonal and packs the buildings as close as their shape allows: one turned onto the line of the row stands wall to wall with the next, and a factory keeps its door lane clear. A row of Power Plants turned square to the map packs 44 apart; it used to go down on whole grid squares, 50 apart, with a strip of dirt at every joint. It stops at the last building your bank can pay for. A ghost that lands on a rock or someone's tank turns red and is left out when you let go; the rest go up. With two or three workers selected the buildings are shared out to the nearest free one, and the ones nobody is free for wait as plans until a worker comes off its job. A plain drag still turns the building, and walls still tile the way they always did.
- Buildings need a worker selected. With nothing selected the command bar used to borrow one of your dozers and offer its buildings anyway. It stays empty now until you pick a worker, and a replay recorded with a building placed that way plays back without it.
- Turning a building no longer turns everything you build afterwards. A wheeled heading still carries from one wall to the next, but the next supply centre comes out facing the way it was designed to.
- A building you point at blocked ground slides to the nearest spot it fits, and lands there.
@@ -566,6 +566,7 @@ found and fixed — EA's own, not port damage.**
- Clicking a turret in a base full of them no longer drags the game down. Every circle's outline was worked out again against every other circle on every frame, so with 120 Patriot batteries on the map one selected battery took the game from 94 frames a second to 53. The outlines are worked out once and kept until a building goes up, comes down or comes out of the fog: the same base now holds 83 with the battery selected against 84 with nothing selected.
- Selecting a GLA supply stash no longer puts reach circles round every defence on the map. The stash spawns workers, a worker carries a weapon for clearing mines, and that was enough to count the stash as armed.
- Two buildings can no longer be put down on the same spot online. A build order travels to the other players before anything appears on the ground, and every click made while it was in flight was answered by a map that still showed the square as empty - so a shift-held row on a bad connection came out as buildings standing inside each other. The ghost now knows about the orders you have already spent, and turns red over them or slides to the square beside; and the order itself is checked again where it lands, on every machine, after the first building is standing. A second one aimed at the same ground is refused there and costs you nothing. What the ghost remembered of each order was the square around the building rather than the building, and a building standing at an angle, which is how most of them come out, fills barely half its square: a shift-clicked row of them found its neighbour in the way, slid off, and left a gap. It remembers the building's own outline now, and five Power Plants clicked side by side stand wall to wall.
+- A building you put down next to another snaps against it. Click a little short of the neighbour, or a little into it, and the ghost slides along the way you stepped until the two touch, lined up with it. Clicking out a row by eye used to leave a strip of dirt at every other joint, or zigzag when the buildings stood at an angle to the row: the grid only has room for a building every 10, a diagonal building touches its neighbour somewhere in between, and a ghost pushed back out of a neighbour was thrown a whole square too far. Leave more than a square and a half between them and the gap stays where you put it.
- The right button drops the building on your cursor whether the click travelled or not. Sending a dozer off somewhere in the middle of a shift-held row moved it and left the ghost riding the cursor, because the cancel only counted a press that had not moved a pixel and a hand on the way to a destination always moves a few.
- A click clears a half-typed building shortcut.
- The two-key building shortcut survives the command bar's redraw now.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 0ebcf44c0..6ad0c84ff 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -642,50 +642,52 @@ public: // ********************************************************************
return ((Real)REAL_TO_INT_FLOOR( (v - offset) / cell + 0.5f )) * cell + offset;
}
+ /** How far apart the centres of two boxes squared up with each other stand when one is moved off
+ * the other along the unit direction 'ux'/'uy' until they no longer share ground. 'c'/'s' is
+ * one box's heading; 'touchAlong' and 'touchAcross' are the two boxes' half-sizes added along
+ * that heading and across it. The row leaves the box the pair would share through whichever
+ * face it meets first. Plus a hair: the logic's clearance test counts a corner lying exactly on
+ * the other box's edge as inside it (testRotatedPointsAgainstRect's <=), so two boxes that
+ * touch to the unit are refused and the nudge throws the second a cell further off. */
+ static Real placementTouchDistance( Real ux, Real uy, Real c, Real s, Real touchAlong,
+ Real touchAcross )
+ {
+ const Real parallel = 0.0001f; // a projection this small is a face the move runs along, not into
+ const Real clearance = 0.5f; // world units left between two structures that touch
+
+ const Real intoAlong = (Real)fabs( ux * c + uy * s );
+ const Real intoAcross = (Real)fabs( uy * c - ux * s );
+ Real touch = 0.0f;
+ if( intoAlong > parallel )
+ touch = touchAlong / intoAlong;
+ if( intoAcross > parallel && ( touch == 0.0f || touchAcross / intoAcross < touch ) )
+ touch = touchAcross / intoAcross;
+ return touch + clearance;
+ }
+
/** A shift-dragged row: the step from one structure to the next, and how many fit between the
* anchor and a cursor 'dx'/'dy' away. The row runs along the nearest eighth of a turn - a
* component counts once it is more than tan 22.5 degrees of the other - and packs as tight as
* the footprint allows along it: the footprint is the box 'halfFacing' by 'halfSide' turned to
- * the heading, and the step is where the row leaves the box two of them would share. So a
- * structure turned onto the row's own line stands face to face with the next, and one turned
- * across it corner to corner, which is the closest a straight row of those can get. With
- * the heading on the grid's axes and the build grid on, the step goes up to whole cells and
- * every piece stays on the grid the first one was snapped to; anywhere else nothing lines two
- * edges up exactly, so a hair is left between them. Never fewer than one, never more than
- * 'most'. Inline and static so a test can reach it without linking the whole in-game UI. */
+ * the heading. So a structure turned onto the row's own line stands face to face with the
+ * next, and one turned across it corner to corner, which is the closest a straight row of those
+ * can get. The step is not rounded to the build grid: a Power Plant is 44 across and the grid
+ * is 10, and rounding it up left 6 of dirt at every joint. Never fewer than one, never more
+ * than 'most'. Inline and static so a test can reach it without linking the whole in-game UI. */
static Int placementRow( Real dx, Real dy, Real headingCos, Real headingSin, Real halfFacing,
- Real halfSide, Bool grid, Int most, Coord2D *step )
+ Real halfSide, Int most, Coord2D *step )
{
const Real slope = 0.41421356f; // tan 22.5 degrees
const Real diagonal = 0.70710678f; // each component of a unit step on a diagonal
- const Real cell = (Real)PLACEMENT_CELL;
- const Real slack = 0.01f; // Cos of a quarter turn is a hair off zero, not a cell's worth
- const Real parallel = 0.0001f; // a projection this small is a face the row runs along, not into
- const Real clearance = 0.5f; // world units left between two pieces off the grid
const Real signX = fabs( dx ) > fabs( dy ) * slope ? ( dx < 0.0f ? -1.0f : 1.0f ) : 0.0f;
const Real signY = fabs( dy ) > fabs( dx ) * slope ? ( dy < 0.0f ? -1.0f : 1.0f ) : 0.0f;
const Real along = ( signX != 0.0f && signY != 0.0f ) ? diagonal : 1.0f;
- const Real ux = signX * along;
- const Real uy = signY * along;
-
- const Real intoFacing = (Real)fabs( ux * headingCos + uy * headingSin );
- const Real intoSide = (Real)fabs( uy * headingCos - ux * headingSin );
- Real reach = 0.0f;
- if( intoFacing > parallel )
- reach = 2.0f * halfFacing / intoFacing;
- if( intoSide > parallel && ( reach == 0.0f || 2.0f * halfSide / intoSide < reach ) )
- reach = 2.0f * halfSide / intoSide;
-
- Real component = reach * along;
- const Bool onGridAxes = fabs( headingSin * headingCos ) < parallel;
- if( grid && onGridAxes )
- component = (Real)REAL_TO_INT_CEIL( component / cell - slack ) * cell;
- else
- component += clearance;
-
- step->x = signX * component;
- step->y = signY * component;
+
+ const Real touch = placementTouchDistance( signX * along, signY * along, headingCos, headingSin,
+ 2.0f * halfFacing, 2.0f * halfSide );
+ step->x = signX * along * touch;
+ step->y = signY * along * touch;
Int count = 1;
const Real stepSqr = step->x * step->x + step->y * step->y;
@@ -773,6 +775,83 @@ public: // ********************************************************************
return TRUE;
}
+ enum { PLACEMENT_FLUSH_REACH = 15 }; ///< world units of gap a structure is pulled across onto its neighbour - a cell and a half
+
+ /** Put 'mine' flush against 'neighbour' when that is plainly what the click meant: squared up
+ * with it (the same heading, or a quarter or half turn off), and either sinking into it by less
+ * than half the touching distance or short of touching by less than PLACEMENT_FLUSH_REACH. It
+ * slides along the line from the neighbour's centre to its own, keeping the direction the player
+ * stepped in, until the two stand touching: a row clicked out sideways past diagonal buildings
+ * nests them corner into corner and stays a line, where pushing each one off the face it met
+ * sent every other building up or down. A step within 15 degrees of an eighth of a turn, on
+ * the map or on the neighbour's own heading, is put on that eighth, so the row comes out
+ * straight. This is the gap the build grid cannot close by itself: a structure standing
+ * diagonal to the grid touches its neighbour at a spot that is no cell's centre. Returns
+ * whether it moved. Inline and static so a test can reach it without linking the whole
+ * in-game UI. */
+ static Bool flushAgainst( const PlacementBox *neighbour, PlacementBox *mine )
+ {
+ const Real squared = 0.01f; // how far off a quarter turn two headings may be and still line up
+ const Real diagonal = 0.70710678f;
+ const Real lineUp = 0.96592583f; // cos 15 degrees
+ const Real reach = (Real)PLACEMENT_FLUSH_REACH;
+ const Real eighths[ 8 ][ 2 ] = { { 1.0f, 0.0f }, { diagonal, diagonal }, { 0.0f, 1.0f },
+ { -diagonal, diagonal }, { -1.0f, 0.0f }, { -diagonal, -diagonal },
+ { 0.0f, -1.0f }, { diagonal, -diagonal } };
+
+ const Real turnCos = mine->c * neighbour->c + mine->s * neighbour->s;
+ const Real turnSin = mine->s * neighbour->c - mine->c * neighbour->s;
+ if( fabs( turnSin * turnCos ) > squared )
+ return FALSE;
+
+ const Real dx = mine->x - neighbour->x;
+ const Real dy = mine->y - neighbour->y;
+ const Real length = (Real)sqrt( dx * dx + dy * dy );
+ if( length < 1.0f )
+ return FALSE;
+
+ Real ux = dx / length;
+ Real uy = dy / length;
+ Real closest = lineUp;
+ for( Int k = 0; k < 8; k++ )
+ {
+ const Real onMap[ 2 ] = { eighths[ k ][ 0 ], eighths[ k ][ 1 ] };
+ const Real onNeighbour[ 2 ] = { eighths[ k ][ 0 ] * neighbour->c - eighths[ k ][ 1 ] * neighbour->s,
+ eighths[ k ][ 0 ] * neighbour->s + eighths[ k ][ 1 ] * neighbour->c };
+ const Real *candidates[ 2 ] = { onMap, onNeighbour };
+ for( Int i = 0; i < 2; i++ )
+ {
+ const Real cosine = ( candidates[ i ][ 0 ] * dx + candidates[ i ][ 1 ] * dy ) / length;
+ if( cosine > closest )
+ {
+ closest = cosine;
+ ux = candidates[ i ][ 0 ];
+ uy = candidates[ i ][ 1 ];
+ }
+ }
+ }
+
+ // mine's half-sizes along the neighbour's own two axes
+ const Real mineAlong = (Real)fabs( turnCos ) * mine->halfMajor + (Real)fabs( turnSin ) * mine->halfMinor;
+ const Real mineAcross = (Real)fabs( turnSin ) * mine->halfMajor + (Real)fabs( turnCos ) * mine->halfMinor;
+ const Real touch = placementTouchDistance( ux, uy, neighbour->c, neighbour->s,
+ neighbour->halfMajor + mineAlong,
+ neighbour->halfMinor + mineAcross );
+
+ const Real gap = dx * ux + dy * uy - touch;
+ if( gap >= reach || gap <= -0.5f * touch )
+ return FALSE;
+
+ mine->x = neighbour->x + ux * touch;
+ mine->y = neighbour->y + uy * touch;
+ return TRUE;
+ }
+
+ /** Pull the structure being placed flush against the nearest structure beside it, standing or
+ * ordered and still on its way (see flushAgainst). Walls tile from their own line and are
+ * left alone. */
+ void snapPlacementToNeighbour( Coord3D *world, const ThingTemplate *what, Real angle ) const;
+
/// remember a structure just ordered here, so the next click can see it
void recordPendingPlacement( const ThingTemplate *what, const Coord3D *world, Real angle );
/// drop the lot - a new game is not the old one's orders
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 7ebbf2d39..dff223264 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -3426,6 +3426,7 @@ void InGameUI::handleBuildPlacements( void )
if( !TheTacticalView->screenToTerrain( &loc, &world ) )
world = *m_placeIcon[ 0 ]->getPosition();
snapPlacementToGrid( &world, m_pendingPlaceType, angle );
+ snapPlacementToNeighbour( &world, m_pendingPlaceType, angle );
//
// NudgeBuildPlacement: the ghost sits where the last legality check found room, which is
@@ -3558,9 +3559,11 @@ void InGameUI::handleBuildPlacements( void )
!TheTacticalView->screenToTerrain( &screenEnd, &worldEnd ) )
return;
- // both ends, so a wall lands on the grid and tiles from a grid square
+ // both ends, so a wall lands on the grid and tiles from a grid square; a row starts where
+ // the ghost did, against its neighbour
snapPlacementToGrid( &worldStart, m_pendingPlaceType, angle );
snapPlacementToGrid( &worldEnd, m_pendingPlaceType, angle );
+ snapPlacementToNeighbour( &worldStart, m_pendingPlaceType, angle );
// get the builder object that will be constructing things
Object *builderObject = TheGameLogic->findObjectByID( TheInGameUI->getPendingPlaceSourceObjectID() );
@@ -6974,8 +6977,7 @@ void InGameUI::computePlacementRow( const ThingTemplate *what, Real angle, const
Coord2D step;
const Int count = placementRow( end->x - start->x, end->y - start->y, (Real)Cos( angle ),
- (Real)Sin( angle ), halfFacing, minor,
- TheGlobalData->m_gridBuildPlacement, most, &step );
+ (Real)Sin( angle ), halfFacing, minor, most, &step );
positions->clear();
for( Int i = 0; i < count; i++ )
@@ -7078,6 +7080,77 @@ Bool InGameUI::overlapsPendingPlacement( const Coord3D *world, const ThingTempla
} // end overlapsPendingPlacement
+//-------------------------------------------------------------------------------------------------
+/** Of every structure it could be pulled against, the one that moves it least wins - the one the
+ * click was plainly meant for. Standing structures come off the partition manager, ordered ones
+ * off the pending ring, and one that has since gone up is in both, the same box twice. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::snapPlacementToNeighbour( Coord3D *world, const ThingTemplate *what, Real angle ) const
+{
+ if( TheBuildAssistant->isLineBuildTemplate( what ) )
+ return;
+
+ const Real largestFootprint = 150.0f; // half-diagonal of the biggest structure it may lean on
+
+ PlacementBox mine;
+ placementFootprint( what, world, angle, &mine );
+
+ Coord2D best;
+ best.x = world->x;
+ best.y = world->y;
+ Real bestMoveSqr = -1.0f;
+
+ std::vector<PlacementBox> neighbours;
+ const UnsignedInt now = TheGameLogic->getFrame();
+ for( Int i = 0; i < PENDING_PLACEMENTS; i++ )
+ {
+ const PendingPlacement *pending = &m_pendingPlacement[ i ];
+ if( pending->frame != 0 && now >= pending->frame && now - pending->frame <= PENDING_PLACEMENT_FRAMES )
+ neighbours.push_back( pending->footprint );
+ }
+
+ const Real searchRadius = (Real)sqrt( mine.halfMajor * mine.halfMajor + mine.halfMinor * mine.halfMinor ) +
+ (Real)PLACEMENT_FLUSH_REACH + largestFootprint;
+ ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( world, searchRadius, FROM_CENTER_2D, NULL );
+ MemoryPoolObjectHolder holder( iter );
+ for( Object *obj = iter->first(); obj; obj = iter->next() )
+ {
+ if( !obj->isKindOf( KINDOF_STRUCTURE ) || obj->isEffectivelyDead() )
+ continue;
+
+ PlacementBox standing;
+ placementFootprint( obj->getTemplate(), obj->getPosition(), obj->getOrientation(), &standing );
+ neighbours.push_back( standing );
+ }
+
+ for( size_t i = 0; i < neighbours.size(); i++ )
+ {
+ PlacementBox moved = mine;
+ if( !flushAgainst( &neighbours[ i ], &moved ) )
+ continue;
+
+ // flush against this one and into the next is no help: the nudge would throw it off again
+ Bool blocked = FALSE;
+ for( size_t j = 0; j < neighbours.size() && !blocked; j++ )
+ blocked = j != i && footprintsOverlap( &moved, &neighbours[ j ] );
+ if( blocked )
+ continue;
+
+ const Real moveSqr = ( moved.x - mine.x ) * ( moved.x - mine.x ) + ( moved.y - mine.y ) * ( moved.y - mine.y );
+ if( bestMoveSqr < 0.0f || moveSqr < bestMoveSqr )
+ {
+ bestMoveSqr = moveSqr;
+ best.x = moved.x;
+ best.y = moved.y;
+ }
+ }
+
+ world->x = best.x;
+ world->y = best.y;
+ world->z = TheTerrainLogic->getGroundHeight( best.x, best.y );
+
+} // end snapPlacementToNeighbour
+
//-------------------------------------------------------------------------------------------------
void InGameUI::forgetPendingPlacements( void )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
index 5b016fe61..266954b9b 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
@@ -305,8 +305,10 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess
// translate the screen position of start to world target location
TheTacticalView->screenToTerrain( &anchorStart, &world );
- // and put it on the build grid, exactly as the ghost the player was looking at
+ // and put it on the build grid and against its neighbour, exactly as the ghost the
+ // player was looking at
TheInGameUI->snapPlacementToGrid( &world, build, angle );
+ TheInGameUI->snapPlacementToNeighbour( &world, build, angle );
// get the source object ID of the thing that is "building" the object
ObjectID builderID = INVALID_ID;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 31d7b3d97..ec2fb757f 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2270,58 +2270,54 @@ TEST(placement_row_packs_the_footprint_along_the_nearest_eighth)
Coord2D step;
const Real half = 0.70710678f; /* cos and sin of an eighth of a turn */
- /* facing along x, a 40 by 30 footprint: one piece per 40 dragged, the first free */
- CHECK_EQ(InGameUI::placementRow(119.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);
- CHECK_NEAR(step.x, 40.0f, 0.0001f);
+ /* facing along x, a 40 by 30 footprint: one piece per 40 dragged and the hair the logic's
+ * clearance test needs between them, the first free */
+ CHECK_EQ(InGameUI::placementRow(119.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);
+ CHECK_NEAR(step.x, 40.5f, 0.0001f);
CHECK_NEAR(step.y, 0.0f, 0.0001f);
- CHECK_EQ(InGameUI::placementRow(120.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 4);
+ CHECK_EQ(InGameUI::placementRow(122.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 4);
/* a hand-drawn line 20 degrees off still runs straight, and backwards runs backwards */
- CHECK_EQ(InGameUI::placementRow(-100.0f, 36.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);
- CHECK_NEAR(step.x, -40.0f, 0.0001f);
+ CHECK_EQ(InGameUI::placementRow(-100.0f, 36.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);
+ CHECK_NEAR(step.x, -40.5f, 0.0001f);
CHECK_NEAR(step.y, 0.0f, 0.0001f);
/* along y the step is the footprint's other side */
- InGameUI::placementRow(0.0f, -90.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step);
+ InGameUI::placementRow(0.0f, -90.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step);
CHECK_NEAR(step.x, 0.0f, 0.0001f);
- CHECK_NEAR(step.y, -30.0f, 0.0001f);
+ CHECK_NEAR(step.y, -30.5f, 0.0001f);
/* a diagonal slides each piece along the last one's long side instead of meeting it corner
- * to corner: 30 on each axis, where 40 by 30 left a triangle of ground between them */
- CHECK_EQ(InGameUI::placementRow(80.0f, 60.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);
- CHECK_NEAR(step.x, 30.0f, 0.0001f);
- CHECK_NEAR(step.y, 30.0f, 0.0001f);
+ * to corner: 30 on each axis and the hair, where 40 by 30 left a triangle of ground */
+ CHECK_EQ(InGameUI::placementRow(80.0f, 60.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);
+ CHECK_NEAR(step.x, 30.0f + 0.5f * half, 0.001f);
+ CHECK_NEAR(step.y, 30.0f + 0.5f * half, 0.001f);
- /* turned a quarter, the footprint's sides swap axes; Cos's float dust stays off the grid */
- InGameUI::placementRow(100.0f, 0.0f, -0.00000004f, 1.0f, 20.0f, 15.0f, TRUE, 50, &step);
- CHECK_NEAR(step.x, 30.0f, 0.0001f);
+ /* turned a quarter, the footprint's sides swap axes, Cos's float dust and all */
+ InGameUI::placementRow(100.0f, 0.0f, -0.00000004f, 1.0f, 20.0f, 15.0f, 50, &step);
+ CHECK_NEAR(step.x, 30.5f, 0.0001f);
/* turned an eighth, a row along the structure's own line stands face to face with the
- * next: a step 40 long, less the hair left off the grid */
- InGameUI::placementRow(100.0f, 100.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);
- CHECK_NEAR(step.x * step.x + step.y * step.y, 40.0f * 40.0f, 60.0f);
- CHECK(step.x * step.x + step.y * step.y > 40.0f * 40.0f);
- InGameUI::placementRow(-100.0f, 100.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);
- CHECK_NEAR(step.x * step.x + step.y * step.y, 30.0f * 30.0f, 60.0f);
+ * next: a step 40 long, and the hair */
+ InGameUI::placementRow(100.0f, 100.0f, half, half, 20.0f, 15.0f, 50, &step);
+ CHECK_NEAR(step.x * step.x + step.y * step.y, 40.5f * 40.5f, 0.01f);
+ InGameUI::placementRow(-100.0f, 100.0f, half, half, 20.0f, 15.0f, 50, &step);
+ CHECK_NEAR(step.x * step.x + step.y * step.y, 30.5f * 30.5f, 0.01f);
/* and across the structure's line the row can do no better than corner to corner: the
* shorter half-side's diagonal, 30 / cos 45 */
- InGameUI::placementRow(100.0f, 0.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);
+ InGameUI::placementRow(100.0f, 0.0f, half, half, 20.0f, 15.0f, 50, &step);
CHECK_NEAR(step.x, 30.0f / half + 0.5f, 0.001f);
CHECK_NEAR(step.y, 0.0f, 0.0001f);
- /* on the grid a size goes up to whole cells; off it the size stands, plus the hair */
- InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 21.0f, 15.0f, TRUE, 50, &step);
- CHECK_NEAR(step.x, 50.0f, 0.0001f);
- InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 20.00001f, 15.0f, TRUE, 50, &step);
- CHECK_NEAR(step.x, 40.0f, 0.0001f);
- InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 21.0f, 15.0f, FALSE, 50, &step);
- CHECK_NEAR(step.x, 42.5f, 0.0001f);
+ /* the step is the footprint, not the build grid's next whole cell */
+ InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 22.0f, 30.0f, 50, &step);
+ CHECK_NEAR(step.x, 44.5f, 0.0001f);
/* never more than the cap, never fewer than one, and no drag is one piece */
- CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 5, &step), 5);
- CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 0, &step), 1);
- CHECK_EQ(InGameUI::placementRow(0.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 1);
+ CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 5, &step), 5);
+ CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 0, &step), 1);
+ CHECK_EQ(InGameUI::placementRow(0.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 1);
}
@@ -11188,6 +11184,75 @@ TEST(two_structures_ordered_onto_the_same_ground_are_one_too_many)
CHECK( (Int)InGameUI::PENDING_PLACEMENTS >= 2 );
}
+/* A structure clicked out beside another by eye lands flush against it: a player's step is never
+ the building's width to the unit, and the build grid alone cannot close the difference for a
+ structure standing diagonal to it. */
+TEST(a_structure_clicked_beside_another_is_pulled_flush)
+{
+ const Real half = 0.70710678f; /* cos and sin of an eighth of a turn */
+ const InGameUI::PlacementBox standing = placementBox( 0.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ InGameUI::PlacementBox mine;
+
+ /* short of touching by a little: pulled across to touching and the logic's hair, and put on
+ * the line it was nearly on */
+ mine = placementBox( 45.0f, 3.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, 40.5f, 0.0001f );
+ CHECK_NEAR( mine.y, 0.0f, 0.0001f );
+
+ /* sunk into it by less than half: pushed back out */
+ mine = placementBox( 32.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, 40.5f, 0.0001f );
+
+ /* the other side of it works the same way */
+ mine = placementBox( 2.0f, -33.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, 0.0f, 0.0001f );
+ CHECK_NEAR( mine.y, -30.5f, 0.0001f );
+
+ /* a step off on the diagonal keeps to the diagonal, sliding along the long side */
+ mine = placementBox( 45.0f, 35.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, mine.y, 0.001f );
+ CHECK_NEAR( mine.y, 30.0f + 0.5f * half, 0.001f );
+
+ /* a gap of a cell and a half is a gap somebody meant */
+ mine = placementBox( 56.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( !InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, 56.0f, 0.0001f );
+
+ /* sunk in past half is a click on the structure itself, not beside it */
+ mine = placementBox( 15.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );
+ CHECK( !InGameUI::flushAgainst( &standing, &mine ) );
+
+ /* both turned an eighth: a step along their own faces goes face to face */
+ const InGameUI::PlacementBox diagonal = placementBox( 0.0f, 0.0f, half, half, 20.0f, 15.0f );
+ mine = placementBox( 45.0f * half - 4.0f * half, 45.0f * half + 4.0f * half, half, half, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &diagonal, &mine ) );
+ CHECK_NEAR( mine.x, 40.5f * half, 0.001f );
+ CHECK_NEAR( mine.y, 40.5f * half, 0.001f );
+
+ /* and a step sideways on the map, past structures standing diagonal to it, stays on the line
+ * and nests corner into corner - a Power Plant at its own heading, 44 by 60, a player's step
+ * 70 along and 3 off */
+ const InGameUI::PlacementBox plant = placementBox( 0.0f, 0.0f, half, -half, 22.0f, 30.0f );
+ mine = placementBox( 70.0f, 3.0f, half, -half, 22.0f, 30.0f );
+ CHECK( InGameUI::flushAgainst( &plant, &mine ) );
+ CHECK_NEAR( mine.x, 44.0f / half + 0.5f, 0.001f );
+ CHECK_NEAR( mine.y, 0.0f, 0.001f );
+ CHECK( !InGameUI::footprintsOverlap( &plant, &mine ) );
+
+ /* a quarter turn off still squares up, with its sides swapped */
+ mine = placementBox( 38.0f, 0.0f, 0.0f, 1.0f, 20.0f, 15.0f );
+ CHECK( InGameUI::flushAgainst( &standing, &mine ) );
+ CHECK_NEAR( mine.x, 35.5f, 0.0001f );
+
+ /* and one at some other angle is left where it was put */
+ mine = placementBox( 45.0f, 0.0f, 0.8660254f, 0.5f, 20.0f, 15.0f );
+ CHECK( !InGameUI::flushAgainst( &standing, &mine ) );
+}
+
TEST(option_catalog_rows_are_well_formed)
{
/* The catalog is a table, so the mistakes it invites are table mistakes: two rows claiming the