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.

fix(water): keep windows.h's min, max and near out of water code

Richard Louie Orilla935cb9acc056
diff --git a/GeneralsMD/Code/Libraries/Include/Platform/WaterBedMaterial.h b/GeneralsMD/Code/Libraries/Include/Platform/WaterBedMaterial.h
index 57d923fd1..20cc81c1d 100644
--- a/GeneralsMD/Code/Libraries/Include/Platform/WaterBedMaterial.h
+++ b/GeneralsMD/Code/Libraries/Include/Platform/WaterBedMaterial.h
@@ -19,7 +19,7 @@ inline uint32_t Hash(int x, int y, uint32_t seed)
return n;
}
inline float Unit(uint32_t n) { return float(n >> 8) / 16777216.0f; }
-inline float Saturate(float v) { return std::max(0.0f, std::min(1.0f, v)); }
+inline float Saturate(float v) { return (std::max)(0.0f, (std::min)(1.0f, v)); }
inline float Smooth(float v) { v = Saturate(v); return v*v*(3.0f-2.0f*v); }
inline int Wrap(int n, int period) { return (n % period + period) % period; }
diff --git a/GeneralsMD/Code/Libraries/Include/Platform/WaterOpticalModel.h b/GeneralsMD/Code/Libraries/Include/Platform/WaterOpticalModel.h
index c33139962..44c7ee368 100644
--- a/GeneralsMD/Code/Libraries/Include/Platform/WaterOpticalModel.h
+++ b/GeneralsMD/Code/Libraries/Include/Platform/WaterOpticalModel.h
@@ -26,10 +26,10 @@ WATER_OPTICAL_RAY_CONSTANTS(WATER_OPTICAL_RAY_CPP_CONSTANTS)
// Comparisons also define a deterministic NaN/Infinity policy in both languages: NaN and negative
// path/weights become zero, positive infinity saturates, and invalid reference depth uses 0.25.
-inline float Unit(float value) { return value>=0.0f?std::min(value,1.0f):0.0f; }
-inline float Path(float value) { return value>=0.0f?std::min(value,MaximumFiniteInput):0.0f; }
+inline float Unit(float value) { return value>=0.0f?(std::min)(value,1.0f):0.0f; }
+inline float Path(float value) { return value>=0.0f?(std::min)(value,MaximumFiniteInput):0.0f; }
inline float ReferenceDepth(float value) {
- return value>=MinimumReferenceDepth?std::min(value,MaximumFiniteInput):MinimumReferenceDepth;
+ return value>=MinimumReferenceDepth?(std::min)(value,MaximumFiniteInput):MinimumReferenceDepth;
}
// Positive movement is toward the camera along its normalized view ray. A finer optical surface
// may correct raster interpolation, but must never pass the actual scene hit or the camera. The
@@ -39,14 +39,14 @@ inline float RayShift(float heightDelta,float viewZ,float offshoreFactor,float d
if (!(offshoreFactor>0.0f) || !(viewZ>0.0f) || !(viewZ<=MaximumFiniteInput)
|| !(std::abs(heightDelta)<=MaximumFiniteInput)) return 0.0f;
const float bound=RayHeightBoundScale*Path(displacementBound);
- const float shift=Unit(offshoreFactor)*std::max(-bound,std::min(heightDelta,bound))
- /std::max(viewZ,RayMinimumViewZ);
- const float sceneLimit=std::max(Path(sceneRayDistance)-RaySurfaceMargin,0.0f);
- const float cameraLimit=std::max(Path(cameraDistance)-RaySurfaceMargin,0.0f);
- return std::max(-sceneLimit,std::min(shift,cameraLimit));
+ const float shift=Unit(offshoreFactor)*(std::max)(-bound,(std::min)(heightDelta,bound))
+ /(std::max)(viewZ,RayMinimumViewZ);
+ const float sceneLimit=(std::max)(Path(sceneRayDistance)-RaySurfaceMargin,0.0f);
+ const float cameraLimit=(std::max)(Path(cameraDistance)-RaySurfaceMargin,0.0f);
+ return (std::max)(-sceneLimit,(std::min)(shift,cameraLimit));
}
inline float OffshoreExtinction(float transparentDepth,float minOpacity) {
- const float remaining=std::max(1.0f-Unit(minOpacity),MinimumReferenceTransmission);
+ const float remaining=(std::max)(1.0f-Unit(minOpacity),MinimumReferenceTransmission);
return -std::log(remaining)/ReferenceDepth(transparentDepth);
}
inline Rgb Transmission(float path,float riverBlend,float offshoreFactor,float referenceDepth,float opacity) {
@@ -55,9 +55,9 @@ inline Rgb Transmission(float path,float riverBlend,float offshoreFactor,float r
// Authored opacity describes extinction per physical world distance. Applying the legacy
// standing-water multiplier to this term a second time would obscure shallow seas too much.
const float authored=path*Unit(offshoreFactor)*OffshoreExtinction(referenceDepth,opacity);
- return {std::exp(-std::min(materialPath*BaseExtinction.r+authored,MaximumExponent)),
- std::exp(-std::min(materialPath*BaseExtinction.g+authored,MaximumExponent)),
- std::exp(-std::min(materialPath*BaseExtinction.b+authored,MaximumExponent))};
+ return {std::exp(-(std::min)(materialPath*BaseExtinction.r+authored,MaximumExponent)),
+ std::exp(-(std::min)(materialPath*BaseExtinction.g+authored,MaximumExponent)),
+ std::exp(-(std::min)(materialPath*BaseExtinction.b+authored,MaximumExponent))};
}
// Source-function radiances in linear RGB; these are not extinction coefficients.
#define WATER_OPTICAL_SOURCE_CPP(clearR,clearG,clearB,riverR,riverG,riverB,oceanR,oceanG,oceanB,ambient,faceMin,faceRange) \
@@ -68,7 +68,7 @@ WATER_OPTICAL_SOURCE_PRESETS(WATER_OPTICAL_SOURCE_CPP)
inline float SourceOffshore(float riverBlend,float effectiveOffshore) {
// The caller has already excluded the river share from offshore. Cap malformed overlap;
// multiplying by (1-river) here would apply that gate twice at continuous transitions.
- return std::min(Unit(effectiveOffshore),1.0f-Unit(riverBlend));
+ return (std::min)(Unit(effectiveOffshore),1.0f-Unit(riverBlend));
}
inline Rgb MaterialSource(float riverBlend,float effectiveOffshore) {
const float river=Unit(riverBlend),offshore=SourceOffshore(river,effectiveOffshore);
diff --git a/GeneralsMD/Code/Libraries/Include/Platform/WaterShoreSource.h b/GeneralsMD/Code/Libraries/Include/Platform/WaterShoreSource.h
index 25534cedd..b0e9e3c61 100644
--- a/GeneralsMD/Code/Libraries/Include/Platform/WaterShoreSource.h
+++ b/GeneralsMD/Code/Libraries/Include/Platform/WaterShoreSource.h
@@ -100,11 +100,11 @@ public:
for(int x=0;x<2*(width-1);++x) {
const int ix=x/2,iy=y/2;const float fx=(x%2)*.5f,fy=(y%2)*.5f;
// Include a one-terrain-cell margin around support for wet-only spreading.
- bool near=false;
- for(int yy=(std::max)(0,iy-1);yy<=(std::min)(height-1,iy+2)&&!near;++yy)
+ bool nearCoast=false;
+ for(int yy=(std::max)(0,iy-1);yy<=(std::min)(height-1,iy+2)&&!nearCoast;++yy)
for(int xx=(std::max)(0,ix-1);xx<=(std::min)(width-1,ix+2);++xx)
- near=near||f.coast[size_t(yy)*width+xx]!=0;
- if(!near||FieldSample(f.ocean,width,ix,iy,fx,fy)>0)continue;
+ nearCoast=nearCoast||f.coast[size_t(yy)*width+xx]!=0;
+ if(!nearCoast||FieldSample(f.ocean,width,ix,iy,fx,fy)>0)continue;
const double worldX=next.grid.originX+x*next.grid.spacing;
if(!Covered(row,worldX,worldX))continue;
const float terrain=Height(heights,width,height,x*.5,y*.5,heightScale);
diff --git a/GeneralsMD/Code/Tests/WaterBowShoulderTest.h b/GeneralsMD/Code/Tests/WaterBowShoulderTest.h
index 01b2a5ef2..b1537f796 100644
--- a/GeneralsMD/Code/Tests/WaterBowShoulderTest.h
+++ b/GeneralsMD/Code/Tests/WaterBowShoulderTest.h
@@ -32,13 +32,13 @@ double oracle(const WaterHullModel::Hull& h,double x,double y,double widthCap=12
double best=1e99,support=-1e99;bool inside=true;unsigned n=unsigned(h.data[0][3]);
auto vertex=[&](unsigned i,unsigned d){return double(h.data[4+i/2][2*(i%2)+d]);};
for(unsigned i=0;i<n;++i){unsigned k=(i+1)%n;double ax=vertex(i,0),ay=vertex(i,1),ex=vertex(k,0)-ax,ey=vertex(k,1)-ay;
- double t=std::max(0.,std::min(1.,((px-ax)*ex+(py-ay)*ey)/(ex*ex+ey*ey)));double qx=ax+t*ex,qy=ay+t*ey,d=(px-qx)*(px-qx)+(py-qy)*(py-qy);
- if(d<best){best=d;}inside=inside&&ex*(py-ay)-ey*(px-ax)>=0;support=std::max(support,ax*fx+ay*fy);}
- const double beam=h.data[1][3],width=std::max(3.,std::min(widthCap,beam*double(.45f))),length=std::max(9.,std::min(32.,6+double(1.2f)*beam));
- const double u=std::max(0.,std::min(250.,speed)-double(.3f)),amplitude=std::max(3.,std::min(8.,1.8*std::sqrt(beam)))*u*u/(u*u+26*26);
- auto profile=[](double a){a=std::abs(a);if(a>=3)return 0.;double t=std::max(0.,std::min(1.,(a-1.5)/1.5));return std::exp(-a*a)*(1-t*t*(3-2*t));};
- const double ridge=((inside?-1:1)*std::sqrt(best)-.5*width)/width,aft=std::max(0.,support-px*fx-py*fy)/length;
- const double t=std::max(0.,std::min(1.,(radius-(reach-30))/30));return amplitude*profile(ridge)*profile(aft)*(1-t*t*(3-2*t));
+ double t=(std::max)(0.,(std::min)(1.,((px-ax)*ex+(py-ay)*ey)/(ex*ex+ey*ey)));double qx=ax+t*ex,qy=ay+t*ey,d=(px-qx)*(px-qx)+(py-qy)*(py-qy);
+ if(d<best){best=d;}inside=inside&&ex*(py-ay)-ey*(px-ax)>=0;support=(std::max)(support,ax*fx+ay*fy);}
+ const double beam=h.data[1][3],width=(std::max)(3.,(std::min)(widthCap,beam*double(.45f))),length=(std::max)(9.,(std::min)(32.,6+double(1.2f)*beam));
+ const double u=(std::max)(0.,(std::min)(250.,speed)-double(.3f)),amplitude=(std::max)(3.,(std::min)(8.,1.8*std::sqrt(beam)))*u*u/(u*u+26*26);
+ auto profile=[](double a){a=std::abs(a);if(a>=3)return 0.;double t=(std::max)(0.,(std::min)(1.,(a-1.5)/1.5));return std::exp(-a*a)*(1-t*t*(3-2*t));};
+ const double ridge=((inside?-1:1)*std::sqrt(best)-.5*width)/width,aft=(std::max)(0.,support-px*fx-py*fy)/length;
+ const double t=(std::max)(0.,(std::min)(1.,(radius-(reach-30))/30));return amplitude*profile(ridge)*profile(aft)*(1-t*t*(3-2*t));
}
// Original captured PT contact slabs at frames80/201/381, not current clock poses.
diff --git a/GeneralsMD/Code/Tests/test_water_grid.cpp b/GeneralsMD/Code/Tests/test_water_grid.cpp
index 8869bbf93..667e01c0b 100644
--- a/GeneralsMD/Code/Tests/test_water_grid.cpp
+++ b/GeneralsMD/Code/Tests/test_water_grid.cpp
@@ -1381,8 +1381,8 @@ TEST(water_foam_horizon_window_preserves_near_water_across_yaw_pitch_and_resize)
CHECK(nearHits==(pitch<.5?2u:0u));
}
// A very distant centre/upper hit must not displace finite nearby lower hits.
- const Point far[5]={{1,0,-.000001},{1,-.4,-.2},{1,.4,-.2},{1,-.4,-.00001},{1,.4,-.00001}};
- const auto prioritized=Choose({0,0,30},far,0);
+ const Point farRays[5]={{1,0,-.000001},{1,-.4,-.2},{1,.4,-.2},{1,-.4,-.00001},{1,.4,-.00001}};
+ const auto prioritized=Choose({0,0,30},farRays,0);
CHECK(inside(prioritized,{150,-60,0},128) && inside(prioritized,{150,60,0},128));
CHECK(inside(prioritized,{0,0,0},128));
// Below-water and fully upward views remain finite and bounded without inventing
@@ -1408,17 +1408,17 @@ TEST(water_foam_horizon_window_preserves_near_water_across_yaw_pitch_and_resize)
TEST(water_foam_stern_spread_is_size_bounded_and_emission_remains_time_integrated)
{
using namespace WaterFoamHistory;
- Sample small,large,tiny,huge;
- auto hull=foamTestHull(0,0,30,0);CHECK(MakeSample(hull,0,small));
- hull.data[1][3]=74;CHECK(MakeSample(hull,0,large));
- hull.data[1][3]=1;CHECK(MakeSample(hull,0,tiny));
- hull.data[1][3]=4096;CHECK(MakeSample(hull,0,huge));
- CHECK(std::abs(small.width[0]-2.8)<1e-12 && tiny.width[0]==2.5);
- CHECK(large.width[0]>29 && large.width[0]<30 && huge.width[0]==30);
- CHECK(small.width[0]/small.halfWidth>large.width[0]/large.halfWidth);
- CHECK(small.width[1]==.65 && large.width[1]==3 && huge.width[1]==3);
- CHECK(std::abs(small.rate[0]-2*(.8+.25*std::sqrt(30.0)))<1e-12);
- CHECK(std::abs(small.rate[1]/small.rate[0]-.22)<1e-12);
+ Sample smallHull,largeHull,tinyHull,hugeHull;
+ auto hull=foamTestHull(0,0,30,0);CHECK(MakeSample(hull,0,smallHull));
+ hull.data[1][3]=74;CHECK(MakeSample(hull,0,largeHull));
+ hull.data[1][3]=1;CHECK(MakeSample(hull,0,tinyHull));
+ hull.data[1][3]=4096;CHECK(MakeSample(hull,0,hugeHull));
+ CHECK(std::abs(smallHull.width[0]-2.8)<1e-12 && tinyHull.width[0]==2.5);
+ CHECK(largeHull.width[0]>29 && largeHull.width[0]<30 && hugeHull.width[0]==30);
+ CHECK(smallHull.width[0]/smallHull.halfWidth>largeHull.width[0]/largeHull.halfWidth);
+ CHECK(smallHull.width[1]==.65 && largeHull.width[1]==3 && hugeHull.width[1]==3);
+ CHECK(std::abs(smallHull.rate[0]-2*(.8+.25*std::sqrt(30.0)))<1e-12);
+ CHECK(std::abs(smallHull.rate[1]/smallHull.rate[0]-.22)<1e-12);
// A short live section must accumulate by elapsed dose time. Repeating the same
// render cannot stamp another full-opacity blob, even with the stronger response.
const Mapping map={-12.5,-.5,1,1,1,1};History history;
@@ -3027,8 +3027,8 @@ TEST(water_bow_pressure_size_and_speed_parameters_are_independently_measured)
const auto a=BowPressure(beam,.301f),b=BowPressure(beam,.302f);
CHECK(a.amplitude<.000001 && b.amplitude>3.9f*a.amplitude && b.amplitude<4.1f*a.amplitude);
}
- const auto small=BowPressure(8.37f,70),large=BowPressure(54,70);
- CHECK(large.along>small.along && large.across>small.across && large.amplitude>small.amplitude);
+ const auto slowBow=BowPressure(8.37f,70),fastBow=BowPressure(54,70);
+ CHECK(fastBow.along>slowBow.along && fastBow.across>slowBow.across && fastBow.amplitude>slowBow.amplitude);
CHECK(std::abs(BowPressure(8.37f,14).amplitude-BowPressure(8.37f,70).amplitude)>2);
}
@@ -3554,9 +3554,9 @@ TEST(water_bow_refinement_protected_only_or_nonfresh_geometry_cannot_admit)
CHECK(HasRefinableDemand(mesh,view,corners,actual.prepared));
for(auto &vertex:mesh.vertices) vertex.lattice=-1;const auto nonfresh=mesh;
CHECK(!HasRefinableDemand(mesh,view,corners,actual.prepared));CHECK(bowTestSameMesh(mesh,nonfresh));
- const Point small[4]={{-20,-20},{20,-20},{20,20},{-20,20}};
- Mesh protectedMesh;CHECK(Build(small,view.projection,view.resolution,protectedMesh));CHECK(!protectedMesh.indices.empty());
- CHECK(!HasRefinableDemand(protectedMesh,view,small,actual.prepared));
+ const Point smallPatch[4]={{-20,-20},{20,-20},{20,20},{-20,20}};
+ Mesh protectedMesh;CHECK(Build(smallPatch,view.projection,view.resolution,protectedMesh));CHECK(!protectedMesh.indices.empty());
+ CHECK(!HasRefinableDemand(protectedMesh,view,smallPatch,actual.prepared));
}
#include "Platform/WaterShoreFoam.h"
@@ -4767,9 +4767,9 @@ TEST(water_bow_shoulder_large_hull_scale_cap_support_and_small_boat_identity)
// Small captured PT bodies, including reverse/rotation in the prior test, must
// retain the exact old6-cap scalar. No tolerance can hide a changed footprint.
for(unsigned pose=0;pose<3;++pose) {
- const auto h=T::CapturedHull(pose);const auto small=T::contour(h);CHECK(small.width<6);
+ const auto h=T::CapturedHull(pose);const auto contourShape=T::contour(h);CHECK(contourShape.width<6);
for(double x:{-45.31,20.31,38.17,43.13,46.21})for(double y:{-11.13,-2.17,6.19,10.23}) {
- const auto p=S::ToWorld(small,{x,y});
+ const auto p=S::ToWorld(contourShape,{x,y});
CHECK(T::oracle(h,p.x,p.y)==T::oracle(h,p.x,p.y,6));
}
}