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(gameengine): give big particle clouds a soft blob on the ground
Olcay Seygandcb9e6016e19
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 49e780614..1663e6699 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -404,6 +404,13 @@ has stood in front of a skirmish full of soldiers to judge how they look, or meain frames. If they turn out to be ugly or expensive, `UseShadowVolumesForSkins = No` turns them offand this entry gets rewritten.+**The shade under smoke went missing between builds and was written a second time.** The feature+was described here and had tests written for it, and then the code itself was simply not in the+project any more — so the tests could not build, and the test runner kept passing because it was+still running yesterday's copy of itself. The notes were detailed enough to rebuild it from, which+is the whole argument for keeping them: same rules for which clouds earn a patch, same softness,+same tests, and this time both the tests and a screenshot of a dust plume on the ground say so.+**The missing tree shadows were found with screenshots, not with reasoning.** Three separateexplanations for them looked convincing on paper and were all wrong. What settled it was paintingthe shadows bright red for one build and looking: red under some trees and not others said thediff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.hindex 9b88d8c2e..587c4927e 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ParticleSys.h@@ -55,6 +55,7 @@ struct FieldParse;class INI;class DebugWindowDialog; // really ParticleEditorDialogclass RenderInfoClass; // ick+class Shadow;enum ParticleSystemID{@@ -297,6 +298,34 @@ static char *WindMotionNames[] =extern Bool particleGroundBounce( Coord3D *pos, Coord3D *vel, Real groundZ,const Coord3D *normal, Real bounce, Real friction );+/**+ * The soft blob a whole particle system drops on the terrain. A cloud of billboards has no+ * silhouette to project, so one decal stands in for the lot: accumulate every live particle's+ * footprint here, then ask Resolve where it goes, how big it is and how dark.+ *+ * Free of ParticleSystem for the same reason particleGroundBounce is - it needs no subsystems,+ * so the whole decision can be driven directly (test_gameengine).+ */+struct ParticleShadowBlob+{+ Int m_count; ///< particles accumulated+ Real m_minX, m_maxX; ///< world-space extent of the cloud+ Real m_minY, m_maxY;+ Real m_maxSize; ///< the widest particle in it+ Real m_sumAlpha; ///< summed opacity, the stand-in for optical depth+};++extern void particleShadowBlobReset( ParticleShadowBlob *blob );+extern void particleShadowBlobAdd( ParticleShadowBlob *blob, Real x, Real y, Real size, Real alpha );++/**+ * Turn the accumulated particles into a decal. Returns FALSE when this system has not earned+ * one - too few particles, particles too small (trails, sparks, muzzle flashes) or no alpha left -+ * which is also the signal to release a decal it already has.+ */+extern Bool particleShadowBlobResolve( const ParticleShadowBlob *blob, Real *centerX, Real *centerY,+ Real *sizeX, Real *sizeY, Int *opacity );+class ParticleSystemInfo : public Snapshot{@@ -659,6 +688,9 @@ public:Real getWindAngle( void ) { return m_windAngle; }WindMotion getWindMotion( void ) { return m_windMotion; }+ /// TRUE if this system is the kind of big soft cloud that should shade the ground under it+ Bool shouldCastGroundShadow( void ) const;+protected:// snapshot methods@@ -676,10 +708,17 @@ protected:const Coord3D *computeParticleVelocity( const Coord3D *pos ); ///< compute a velocity vector based on emission propertiesconst Coord3D *computePointOnUnitSphere( void ); ///< compute a random point on a unit sphere+protected:+ /// refresh (or release) the one decal that shades the ground under this system's particles+ void updateGroundShadow( const ParticleShadowBlob *blob );+ void releaseGroundShadow( void ); ///< give the ground blob back, if we have one+protected:Particle * m_systemParticlesHead;Particle * m_systemParticlesTail;+ Shadow * m_groundShadow; ///< client-only soft blob on the terrain, never xfered+UnsignedInt m_particleCount; ///< current count of particles for this systemParticleSystemID m_systemID; ///< unique id given to this system from the particle system managerdiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cppindex 713804aea..fceadc7e8 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/System/ParticleSys.cpp@@ -44,6 +44,7 @@#include "GameClient/GameClient.h"#include "GameClient/InGameUI.h"#include "GameClient/ParticleSys.h"+#include "GameClient/Shadow.h"#include "GameLogic/GameLogic.h"#include "GameLogic/Object.h"@@ -354,6 +355,83 @@ void Particle::applyForce( const Coord3D *force )m_accel.z += force->z;}+// ------------------------------------------------------------------------------------------------+/** The soft blob a whole particle system drops on the terrain. See ParticleSys.h - free of+ * ParticleSystem so the whole decision is testable without a renderer. */+// ------------------------------------------------------------------------------------------------+void particleShadowBlobReset( ParticleShadowBlob *blob )+{+ blob->m_count = 0;+ blob->m_minX = blob->m_maxX = 0.0f;+ blob->m_minY = blob->m_maxY = 0.0f;+ blob->m_maxSize = 0.0f;+ blob->m_sumAlpha = 0.0f;+}++// ------------------------------------------------------------------------------------------------+void particleShadowBlobAdd( ParticleShadowBlob *blob, Real x, Real y, Real size, Real alpha )+{+ if (blob->m_count == 0)+ {+ blob->m_minX = blob->m_maxX = x;+ blob->m_minY = blob->m_maxY = y;+ }+ else+ {+ if (x < blob->m_minX) blob->m_minX = x;+ if (x > blob->m_maxX) blob->m_maxX = x;+ if (y < blob->m_minY) blob->m_minY = y;+ if (y > blob->m_maxY) blob->m_maxY = y;+ }++ if (size > blob->m_maxSize)+ blob->m_maxSize = size;++ if (alpha > 0.0f)+ blob->m_sumAlpha += alpha;++ blob->m_count++;+}++// ------------------------------------------------------------------------------------------------+Bool particleShadowBlobResolve( const ParticleShadowBlob *blob, Real *centerX, Real *centerY,+ Real *sizeX, Real *sizeY, Int *opacity )+{+ // A handful of tiny motes is a bullet trail, a muzzle flash or a spark shower. A cloud that+ // has faded out is not drawn any more and must not leave a stain. None of them earn a decal,+ // and saying so here is also how a system that had one gives it back.+ const Int MIN_PARTICLES = 3;+ const Real MIN_PARTICLE_SIZE = 15.0f; ///< world units across+ const Real MAX_BLOB_SIZE = 300.0f; ///< a wind-blown system must not smear over the map+ const Real ALPHA_SATURATION = 6.0f; ///< summed alpha at which the blob is as dark as it gets+ const Real MAX_OPACITY = 80.0f; ///< smoke shades the ground, it never blacks it out++ if (blob->m_count < MIN_PARTICLES)+ return FALSE;++ if (blob->m_maxSize < MIN_PARTICLE_SIZE)+ return FALSE;++ if (blob->m_sumAlpha <= 0.0f)+ return FALSE;++ *centerX = (blob->m_minX + blob->m_maxX) * 0.5f;+ *centerY = (blob->m_minY + blob->m_maxY) * 0.5f;++ // the cloud's own spread, widened by one particle so the edge particles are covered too+ Real spreadX = (blob->m_maxX - blob->m_minX) + blob->m_maxSize;+ Real spreadY = (blob->m_maxY - blob->m_minY) + blob->m_maxSize;+ *sizeX = spreadX > MAX_BLOB_SIZE ? MAX_BLOB_SIZE : spreadX;+ *sizeY = spreadY > MAX_BLOB_SIZE ? MAX_BLOB_SIZE : spreadY;++ Real depth = blob->m_sumAlpha / ALPHA_SATURATION;+ if (depth > 1.0f)+ depth = 1.0f;+ *opacity = REAL_TO_INT( depth * MAX_OPACITY );++ return TRUE;+}+// ------------------------------------------------------------------------------------------------/** Collide a particle with the ground. The velocity is split into the part heading into the* surface and the part sliding along it: the first is reflected and scaled by 'bounce', the@@ -1237,6 +1315,7 @@ ParticleSystem::ParticleSystem( const ParticleSystemTemplate *sysTemplate,m_particleCount = 0;m_personalityStore = 0;m_controlParticle = NULL;+ m_groundShadow = NULL;TheParticleSystemManager->friend_addParticleSystem(this);@@ -1270,6 +1349,9 @@ ParticleSystem::~ParticleSystem()} // end if+ // give the ground blob back before the particles it was measured from go away+ releaseGroundShadow();+// destroy all particles "in the air"while (m_systemParticlesHead)m_systemParticlesHead->deleteInstance();@@ -2110,6 +2192,10 @@ Bool ParticleSystem::update( Int localPlayerIndex )//// Update all particles in the system//+ Bool castsGroundShadow = shouldCastGroundShadow();+ ParticleShadowBlob shadowBlob;+ particleShadowBlobReset( &shadowBlob );+Particle *p = m_systemParticlesHead;Particle *oldParticle;while (p)@@ -2131,10 +2217,21 @@ Bool ParticleSystem::update( Int localPlayerIndex )p = p->m_systemNext;oldParticle->deleteInstance();} else {+ // one decal stands in for the whole cloud, so measure the survivors as we pass them+ if (castsGroundShadow)+ {+ const Coord3D *ppos = p->getPosition();+ particleShadowBlobAdd( &shadowBlob, ppos->x, ppos->y, p->getSize(), p->getAlpha() );+ }p = p->m_systemNext;}}+ if (castsGroundShadow)+ updateGroundShadow( &shadowBlob );+ else+ releaseGroundShadow();+//// If we have been "destroyed", wait for all of our particles to die off,// then destroy ourselves (return false).@@ -2162,6 +2259,92 @@ Bool ParticleSystem::update( Int localPlayerIndex )return true;}+// ------------------------------------------------------------------------------------------------+/** Give the ground blob back. The decal's manager lives inside TheGameClient, and shutdownAll+ * runs in reverse init order, so TheGameClient - and with it every decal it was holding - is gone+ * before TheParticleSystemManager destroys the systems. Releasing then would follow a dangling+ * pointer, so the NULL manager is the signal that there is nothing left to give back. */+// ------------------------------------------------------------------------------------------------+void ParticleSystem::releaseGroundShadow( void )+{+ if (m_groundShadow)+ {+ if (TheProjectedShadowManager)+ m_groundShadow->release();+ m_groundShadow = NULL;+ }+}++// ------------------------------------------------------------------------------------------------+/** Is this system a big soft cloud - smoke, dust, a plume - rather than fire, a glow or a decal+ * that is already on the ground? Additive systems are light rather than shade, and a+ * ground-aligned one is drawn flat on the terrain already. */+// ------------------------------------------------------------------------------------------------+Bool ParticleSystem::shouldCastGroundShadow( void ) const+{+ // not defensive: test_gameengine_stubs.cpp defines the manager pointer as NULL+ if (TheProjectedShadowManager == NULL || TheGlobalData == NULL)+ return FALSE;++ if (!TheGlobalData->m_shadowsForParticles)+ return FALSE;++ if (m_particleType != PARTICLE && m_particleType != VOLUME_PARTICLE)+ return FALSE;++ if (m_shaderType != ALPHA && m_shaderType != ALPHA_TEST)+ return FALSE;++ if (m_isGroundAligned)+ return FALSE;++ return TRUE;+}++// ------------------------------------------------------------------------------------------------+/** Refresh - or release - the one decal that shades the ground under this system's particles. */+// ------------------------------------------------------------------------------------------------+void ParticleSystem::updateGroundShadow( const ParticleShadowBlob *blob )+{+ Real centerX, centerY, sizeX, sizeY;+ Int opacity;++ if (!particleShadowBlobResolve( blob, ¢erX, ¢erY, &sizeX, &sizeY, &opacity ))+ {+ // the cloud shrank, thinned out or blew away: take the stain with it+ releaseGroundShadow();+ return;+ }++ if (m_groundShadow == NULL)+ {+ Shadow::ShadowTypeInfo decalInfo;+ decalInfo.allowUpdates = FALSE; // the texture never changes+ decalInfo.allowWorldAlign = TRUE; // let it wrap over the terrain it lands on+ // SHADOW_ALPHA_DECAL, not SHADOW_DECAL: only the alpha/additive decals are honoured by+ // setOpacity/setColor and taken off m_decalList by removeShadow, and this one has to fade.+ decalInfo.m_type = SHADOW_ALPHA_DECAL;+ strcpy( decalInfo.m_ShadowName, "shadow" );+ // nonzero on purpose - this addDecal overload divides by the size directly+ decalInfo.m_sizeX = sizeX;+ decalInfo.m_sizeY = sizeY;+ decalInfo.m_offsetX = 0.0f;+ decalInfo.m_offsetY = 0.0f;++ m_groundShadow = TheProjectedShadowManager->addDecal( &decalInfo );+ if (m_groundShadow == NULL)+ return;++ m_groundShadow->setAngle( 0.0f );+ m_groundShadow->setColor( 0x00000000 ); // black: this is shade, not a tint+ }++ m_groundShadow->setSize( sizeX, sizeY );+ m_groundShadow->setPosition( centerX, centerY,+ TheTerrainLogic ? TheTerrainLogic->getGroundHeight( centerX, centerY ) : 0.0f );+ m_groundShadow->setOpacity( opacity );+}+// ------------------------------------------------------------------------------------------------/** Update the wind motion */// ------------------------------------------------------------------------------------------------diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 4a85185b6..26d9e150c 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -717,13 +717,129 @@ TEST(particle_already_leaving_the_surface_is_only_lifted_clear)CHECK_NEAR(vel.z, 2.0f, 1e-5f);CHECK_NEAR(vel.x, 1.0f, 1e-5f);}+//////////////////////////////////////////////////////////////////////////////-// The ground blob under a particle system used to be covered here, but the-// particleShadowBlob* implementation is in no source file any more - only the-// changelogs remember it. The tests went with it rather than keeping a block-// that cannot compile; bring them back with the code, not before.+// The ground blob under a particle system+//+// particleShadowBlob* is the whole of the decision: which systems earn a soft+// shadow on the terrain, where it goes, how big it is and how dark. Pure+// arithmetic over the live particles, so it can be driven directly.//////////////////////////////////////////////////////////////////////////////+static void blobFeed(ParticleShadowBlob *blob, Int count, Real size, Real alpha)+{+ for (Int i = 0; i < count; i++)+ particleShadowBlobAdd(blob, (Real)i, 0.0f, size, alpha);+}++TEST(blob_reset_leaves_nothing_to_resolve)+{+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ CHECK_EQ(blob.m_count, 0);+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+}++TEST(blob_rejects_a_bullet_trail_because_its_particles_are_tiny)+{+ // plenty of particles, all of them a couple of units across - this is the case that+ // keeps trails, sparks and muzzle flashes from staining the ground+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ blobFeed(&blob, 40, 3.0f, 1.0f);+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+}++TEST(blob_rejects_a_puff_of_one_or_two_particles)+{+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ blobFeed(&blob, 2, 50.0f, 1.0f);+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+}++TEST(blob_rejects_a_cloud_that_has_faded_to_nothing)+{+ // big particles, enough of them, but no alpha left: a decal here would be a black+ // smear under smoke that is no longer drawn+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ blobFeed(&blob, 10, 60.0f, 0.0f);+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+}++TEST(blob_centres_on_the_particles_and_covers_their_spread_plus_one_particle)+{+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ particleShadowBlobAdd(&blob, 100.0f, 200.0f, 20.0f, 0.5f);+ particleShadowBlobAdd(&blob, 140.0f, 200.0f, 30.0f, 0.5f);+ particleShadowBlobAdd(&blob, 120.0f, 260.0f, 10.0f, 0.5f);++ CHECK(particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+ CHECK_NEAR(x, 120.0f, 1e-4f); // midpoint of 100..140+ CHECK_NEAR(y, 230.0f, 1e-4f); // midpoint of 200..260+ CHECK_NEAR(sx, 40.0f + 30.0f, 1e-4f); // spread plus the biggest particle's width+ CHECK_NEAR(sy, 60.0f + 30.0f, 1e-4f);+ CHECK(op > 0);+}++TEST(blob_darkens_with_more_smoke_then_saturates_below_opaque)+{+ ParticleShadowBlob thin, thick, absurd;+ Real x, y, sx, sy;+ Int thinOp, thickOp, absurdOp;++ particleShadowBlobReset(&thin);+ blobFeed(&thin, 4, 40.0f, 0.25f);+ CHECK(particleShadowBlobResolve(&thin, &x, &y, &sx, &sy, &thinOp));++ particleShadowBlobReset(&thick);+ blobFeed(&thick, 8, 40.0f, 1.0f); // past the saturation point+ CHECK(particleShadowBlobResolve(&thick, &x, &y, &sx, &sy, &thickOp));++ particleShadowBlobReset(&absurd);+ blobFeed(&absurd, 200, 40.0f, 1.0f);+ CHECK(particleShadowBlobResolve(&absurd, &x, &y, &sx, &sy, &absurdOp));++ CHECK(thinOp < thickOp);+ CHECK(thickOp <= absurdOp);+ CHECK(absurdOp < 255); // smoke shades the ground, it never blacks it out+ CHECK_EQ(thickOp, absurdOp); // and past saturation more smoke changes nothing+}++TEST(blob_never_smears_wider_than_the_cap)+{+ // a wind-blown system whose particles have drifted right across the map+ ParticleShadowBlob blob;+ Real x, y, sx, sy;+ Int op;++ particleShadowBlobReset(&blob);+ particleShadowBlobAdd(&blob, 0.0f, 0.0f, 40.0f, 1.0f);+ particleShadowBlobAdd(&blob, 5000.0f, 4000.0f, 40.0f, 1.0f);+ particleShadowBlobAdd(&blob, 2500.0f, 2000.0f, 40.0f, 1.0f);++ CHECK(particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));+ CHECK(sx <= 300.0f);+ CHECK(sy <= 300.0f);+}+// ---------------------------------------------------------------------------------------------// The HUD income estimate (InGameUI.cpp). It samples the score keeper's cumulative earnings// into a ring of buckets and averages over every bucket it holds.