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
fix(gameengine): curve missiles onto targets, bring scuds down on a sine
Olcay Seygana15ff5c8d58c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e4fd2827c..9f9cdb6e0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -295,6 +295,8 @@ found and fixed — EA's own, not port damage.**- Something flagged unselectable is unselectable. The flag existed and nothing read it.- A suicide unit pressed up against a building goes off. Touching the building's outline counted as having its view blocked, so it walked away to find a clear line of sight it never needed, came back, and did it again without ever detonating.- A nuclear missile hurts the whole of a big building. The blast measured to the middle of everything near it, so an airfield whose centre sat just outside the outer ring took nothing while half of it stood in the fire. Distance is now taken halfway between the nearest edge and the centre.+- Rockets curve into their target instead of snapping onto it. In the last few frames before impact a guided missile swung its nose straight at the target in one step, up to 40 degrees in a single frame, when the whole flight before that never turned more than 5. It now flies an arc from the heading it had into the target. In the same test battle the sharpest turn is 16 degrees, and the rockets still land.+- Scuds come down from the top of their climb in a smooth curve, level at the top and steepest at the end, instead of tipping into a straight dive 200 short of the target. Over those last 200 a Scud now stays within 14 of that curve where the dive strayed by up to 49, and every one lands on the spot it was fired at; one in the old test came down 22 away.- Cancelling an order of Red Guard after the first soldier has walked out no longer refunds the order. One order is two soldiers for one price, and the refund came back whole, so the first one was free.- Double-clicking to grab everything of a kind grabs what a box would, not things you can only click one at a time.- A bounty pays what the percentage says. Rounding up a fraction that was already a whole number paid a dollar extra on every kill.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.hindex af1e38879..2338297c2 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/Locomotor.h@@ -334,6 +334,13 @@ public:*/inline void setUsePreciseZPos(Bool u) { setFlag(PRECISE_Z_POS, u); }+ /**+ above zero, a thrust locomotor with a preferred height stops holding that height this far from its+ goal (measured flat) and comes down along a quarter sine from whatever height it has: level where+ the descent starts, steepest where it arrives. Missiles set it from DistanceToTargetBeforeDiving.+ */+ inline void setSineDescentDistance(Real dist) { m_sineDescentDistance = dist; }+/**when off (the default), units slow down as they approach their target.@@ -450,6 +457,7 @@ private:UnsignedInt m_flags;Real m_preferredHeight;Real m_preferredHeightDamping;+ Real m_sineDescentDistance;Real m_angleOffset;Real m_offsetIncrement;diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cppindex 86f2fde31..96e075dc6 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Locomotor.cpp@@ -63,6 +63,7 @@ static const Real DONUT_DISTANCE=4.0*PATHFIND_CELL_SIZE_F;LocomotorStore *TheLocomotorStore = NULL; ///< the Locomotor store definitionconst Real BIGNUM = 99999.0f;+const Real SINE_DESCENT_LOOKAHEAD_FRAMES = 4.0f;static const char *TheLocomotorPriorityNames[] ={@@ -189,6 +190,23 @@ static Real tryToOrientInThisDirection3D(Object* obj, Real maxTurnRate, const Vereturn relAngle;}+//-------------------------------------------------------------------------------------------------+// The turn this frame that keeps a projectile on the circle tangent to its nose through the goal. That+// circle turns through twice the angle between nose and goal over its length, so a step of `step`+// units turns 2*step*sin(angle)/dist, the same every frame for a goal that stands still. Past a right+// angle no such circle reaches the goal, and a loop of diameter dist brings the nose round instead.+// The step that arrives points straight at the goal.+static Real calcArcTurnToGoal(const Vector3& nose, const Vector3& toGoal, Real step)+{+ Real dist = toGoal.Length();+ if (step >= dist)+ return BIGNUM;++ Real cosine = Vector3::Dot_Product(nose, toGoal) / (nose.Length() * dist);+ Real sine = (cosine > 0.0f) ? sqrt(1.0f - sqr(cosine)) : 1.0f;+ return 2.0f * step * sine / dist;+}+//-------------------------------------------------------------------------------------------------inline Real tryToOrientInThisDirection3D(Object* obj, Real maxTurnRate, const Coord3D* dir){@@ -667,6 +685,7 @@ Locomotor::Locomotor(const LocomotorTemplate* tmpl)#endifm_preferredHeight = m_template->m_preferredHeight;m_preferredHeightDamping = m_template->m_preferredHeightDamping;+ m_sineDescentDistance = 0.0f;m_angleOffset = GameLogicRandomValueReal(-PI/6, PI/6);m_offsetIncrement = (PI/40) * (GameLogicRandomValueReal(0.8f, 1.2f)/m_template->m_wanderLengthFactor);@@ -698,6 +717,7 @@ Locomotor::Locomotor(const Locomotor& that)#endifm_preferredHeight = that.m_preferredHeight;m_preferredHeightDamping = that.m_preferredHeightDamping;+ m_sineDescentDistance = that.m_sineDescentDistance;m_angleOffset = that.m_angleOffset;m_offsetIncrement = that.m_offsetIncrement;}@@ -721,6 +741,7 @@ Locomotor& Locomotor::operator=(const Locomotor& that)#endifm_preferredHeight = that.m_preferredHeight;m_preferredHeightDamping = that.m_preferredHeightDamping;+ m_sineDescentDistance = that.m_sineDescentDistance;}return *this;}@@ -741,12 +762,13 @@ void Locomotor::crc( Xfer *xfer )// ------------------------------------------------------------------------------------------------/** Xfer method* Version Info:- * 1: Initial version */+ * 1: Initial version+ * 3: m_sineDescentDistance */// ------------------------------------------------------------------------------------------------void Locomotor::xfer( Xfer *xfer ){// version- const XferVersion currentVersion = 2;+ const XferVersion currentVersion = 3;XferVersion version = currentVersion;xfer->xferVersion( &version, currentVersion );@@ -771,6 +793,9 @@ void Locomotor::xfer( Xfer *xfer )xfer->xferReal(&m_angleOffset);xfer->xferReal(&m_offsetIncrement);+ if (version >= 3)+ xfer->xferReal(&m_sineDescentDistance);+} // end xfer// ------------------------------------------------------------------------------------------------@@ -1122,23 +1147,21 @@ void Locomotor::locoUpdate_moveTowardsPosition(Object* obj, const Coord3D& goalP{// Projectiles never stop braking once they start. jba.obj->setStatus( MAKE_OBJECT_STATUS_MASK( OBJECT_STATUS_BRAKING ) );- // Projectiles cheat in 3 dimensions.+ // Projectiles cheat in 3 dimensions, along the nose rather than straight at the goal, so a+ // missile flies the arc moveTowardsPositionThrust turns it onto instead of kinking at the lock.dist = sqrt(dx*dx+dy*dy+dz*dz);Real vel = physics->getVelocityMagnitude();if (vel < MIN_VEL)vel = MIN_VEL;if (vel > dist)vel = dist; // do not overcompensate!- // Normalize.- if (dist > 0.001f)+ if (dist > 0.001f){- dist = 1.0f / dist;- dx *= dist;- dy *= dist;- dz *= dist;- pos.x += dx * vel;- pos.y += dy * vel;- pos.z += dz * vel;+ Vector3 nose = obj->getTransformMatrix()->Get_X_Vector();+ nose.Normalize();+ pos.x += nose.X * vel;+ pos.y += nose.Y * vel;+ pos.z += nose.Z * vel;}}else@@ -1942,7 +1965,25 @@ void Locomotor::moveTowardsPositionThrust(Object* obj, PhysicsBehavior *physics,//out of the handleBehaviorZ() functionCoord3D pos = *obj->getPosition();- if( m_preferredHeight != 0.0f && !getFlag(PRECISE_Z_POS) )+ Real distToGoal2D = sqrt(sqr(goalPos.x - pos.x) + sqr(goalPos.y - pos.y));+ if( m_preferredHeight != 0.0f && !getFlag(PRECISE_Z_POS) && distToGoal2D < m_sineDescentDistance )+ {+ // Come down along height(d) = h * sin(PI/2 * d / descentDist), aiming a few frames ahead on the one+ // such curve that passes through the missile now. A Scud is still near the top of its climb here,+ // about 230 over the goal against a cruise height of 120, so the curve starts from where it is.+ // Aiming at the goal itself instead was a straight dive with a kink where it began.+ Real lookAhead = SINE_DESCENT_LOOKAHEAD_FRAMES * physics->getVelocityMagnitude();+ if (distToGoal2D > lookAhead)+ {+ Real aimDist = distToGoal2D - lookAhead;+ Real aimFraction = aimDist / distToGoal2D;+ Real heightNow = pos.z - goalPos.z;+ localGoalPos.x = goalPos.x + (pos.x - goalPos.x) * aimFraction;+ localGoalPos.y = goalPos.y + (pos.y - goalPos.y) * aimFraction;+ localGoalPos.z = goalPos.z + heightNow * Sin(PI / 2 * aimDist / m_sineDescentDistance) / Sin(PI / 2 * distToGoal2D / m_sineDescentDistance);+ }+ }+ else if( m_preferredHeight != 0.0f && !getFlag(PRECISE_Z_POS) ){// If we have a preferred flight height, and we haven't been told explicitly to ignore it...Real surfaceHt = getSurfaceHtAtPt(pos.x, pos.y);@@ -1995,7 +2036,18 @@ void Locomotor::moveTowardsPositionThrust(Object* obj, PhysicsBehavior *physics,// we are at target.adjust = false;}- maxTurnRate = 3*maxTurnRate;+ else if (obj->isKindOf(KINDOF_PROJECTILE))+ {+ // the projectile cheat moves along the nose, so this turn is the path it flies+ Real step = physics->getVelocityMagnitude();+ if (step < MIN_VEL)+ step = MIN_VEL;+ maxTurnRate = calcArcTurnToGoal(forwardDir, vel, step);+ }+ else+ {+ maxTurnRate = 3*maxTurnRate;+ }}#ifdef USE_ZDIR_DAMPINGif (zDirDamping != 0.0f)diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cppindex c587a8935..20bc00782 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/MissileAIUpdate.cpp@@ -555,18 +555,9 @@ void MissileAIUpdate::doAttackState(Bool turnOK)}}- if(curLoco && curLoco->getPreferredHeight() > 0)- {- // Am I close enough to the target to ignore my preferred height setting?- Real distanceToTargetSquared = ThePartitionManager->getDistanceSquared( getObject(), getGoalPosition(), FROM_CENTER_2D );- Real diveDistanceSquared = d->m_diveDistance;- if (curLoco && curLoco->getPreferredHeight()) {- diveDistanceSquared *= diveDistanceSquared;- if( distanceToTargetSquared < diveDistanceSquared )- curLoco->setUsePreciseZPos( true );- }-- }+ // a missile with a cruise height leaves it this far from the goal along a quarter sine+ if (curLoco)+ curLoco->setSineDescentDistance(d->m_diveDistance);if (m_noTurnDistLeft <= 0.0f){