Skip to content

Commit 585ecc3

Browse files
committed
refactor(math): Migrate legacy floating point math to deterministic WWMath wrappers
Replaces non-deterministic trigonometric and generic floating point math function calls with deterministic counterparts from the WWMath library across the core codebase. This ensures mathematical parity to prevent frame desyncs during multiplayer lockstep execution, and includes build-fixes for missing WWMath namespaces and header removals.
1 parent 9bf56a6 commit 585ecc3

112 files changed

Lines changed: 496 additions & 892 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ target_include_directories(corei_main INTERFACE "Main")
1616
target_sources(corei_libraries_include PRIVATE
1717
Libraries/Include/Lib/BaseType.h
1818
Libraries/Include/Lib/BaseTypeCore.h
19-
Libraries/Include/Lib/trig.h
19+
2020
Libraries/Include/rts/debug.h
2121
Libraries/Include/rts/profile.h
2222
)

Core/GameEngine/Source/GameClient/RadiusDecal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void RadiusDecal::update()
194194
{
195195
UnsignedInt now = TheGameLogic->getFrame();
196196
Real theta = (2*PI) * (Real)(now % m_template->m_opacityThrobTime) / (Real)m_template->m_opacityThrobTime;
197-
Real percent = 0.5f * (Sin(theta) + 1.0f);
197+
Real percent = 0.5f * (WWMath::Sin(theta) + 1.0f);
198198
Int opac;
199199
if( TheGameLogic->getDrawIconUI() )
200200
{

Core/GameEngine/Source/GameClient/System/ParticleSys.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ void Particle::doWindMotion()
584584
(noForceDistance - fullForceDistance)));
585585

586586
// integrate the wind motion into the position
587-
m_pos.x += (Cos( windAngle ) * windForceStrength);
588-
m_pos.y += (Sin( windAngle ) * windForceStrength);
587+
m_pos.x += (WWMath::Cos( windAngle ) * windForceStrength);
588+
m_pos.y += (WWMath::Sin( windAngle ) * windForceStrength);
589589

590590
}
591591

@@ -3518,7 +3518,7 @@ static Real angleBetween(const Coord2D *vecA, const Coord2D *vecB)
35183518
return 0.0f;
35193519
}
35203520

3521-
Real theta = ACos( cosTheta );
3521+
Real theta = WWMath::Acos( cosTheta );
35223522

35233523
if (vecB->x > 0) {
35243524
return theta;

Core/GameEngine/Source/GameLogic/AI/AIPathfind.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,8 +3956,8 @@ Bool PathfindLayer::isPointOnWall(ObjectID *wallPieces, Int numPieces, const Coo
39563956
Real major = obj->getGeometryInfo().getMajorRadius();
39573957
Real minor = (obj->getGeometryInfo().getGeomType() == GEOMETRY_SPHERE) ? obj->getGeometryInfo().getMajorRadius() : obj->getGeometryInfo().getMinorRadius();
39583958

3959-
Real c = (Real)Cos(-obj->getOrientation());
3960-
Real s = (Real)Sin(-obj->getOrientation());
3959+
Real c = (Real)WWMath::Cos(-obj->getOrientation());
3960+
Real s = (Real)WWMath::Sin(-obj->getOrientation());
39613961

39623962
// convert to a delta relative to rect ctr
39633963
Real ptx = pt->x - obj->getPosition()->x;
@@ -4224,8 +4224,8 @@ void Pathfinder::classifyFence( Object *obj, Bool insert )
42244224
Real halfsizeY = PATHFIND_CELL_SIZE_F/10.0f;
42254225
Real fenceOffset = obj->getTemplate()->getFenceXOffset();
42264226

4227-
Real c = (Real)Cos(angle);
4228-
Real s = (Real)Sin(angle);
4227+
Real c = (Real)WWMath::Cos(angle);
4228+
Real s = (Real)WWMath::Sin(angle);
42294229

42304230
const Real STEP_SIZE = PATHFIND_CELL_SIZE_F * 0.5f; // in theory, should be PATHFIND_CELL_SIZE_F exactly, but needs to be smaller to avoid aliasing problems
42314231
Real ydx = s * STEP_SIZE;
@@ -4432,8 +4432,8 @@ void Pathfinder::internal_classifyObjectFootprint( Object *obj, Bool insert )
44324432
Real halfsizeX = obj->getGeometryInfo().getMajorRadius();
44334433
Real halfsizeY = obj->getGeometryInfo().getMinorRadius();
44344434

4435-
Real c = (Real)Cos(angle);
4436-
Real s = (Real)Sin(angle);
4435+
Real c = (Real)WWMath::Cos(angle);
4436+
Real s = (Real)WWMath::Sin(angle);
44374437

44384438
const Real STEP_SIZE = PATHFIND_CELL_SIZE_F * 0.5f; // in theory, should be PATHFIND_CELL_SIZE_F exactly, but needs to be smaller to avoid aliasing problems
44394439
Real ydx = s * STEP_SIZE;

Core/GameEngineDevice/Source/W3DDevice/Common/System/W3DRadar.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,16 @@ void W3DRadar::drawSingleBeaconEvent( Int pixelX, Int pixelY, Int width, Int hei
403403

404404
// create a triangle around the event
405405
angle = 0.0f - addAngle;
406-
tri[ 0 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
407-
tri[ 0 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
406+
tri[ 0 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
407+
tri[ 0 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
408408

409409
angle = 2.0f * PI / 3.0f - addAngle;
410-
tri[ 1 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
411-
tri[ 1 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
410+
tri[ 1 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
411+
tri[ 1 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
412412

413413
angle = -2.0f * PI / 3.0f - addAngle;
414-
tri[ 2 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
415-
tri[ 2 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
414+
tri[ 2 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
415+
tri[ 2 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
416416

417417
// translate radar coords to screen coords
418418
radarToPixel( &tri[ 0 ], &tri[ 0 ], pixelX, pixelY, width, height );
@@ -502,16 +502,16 @@ void W3DRadar::drawSingleGenericEvent( Int pixelX, Int pixelY, Int width, Int he
502502

503503
// create a triangle around the event
504504
angle = 0.0f - addAngle;
505-
tri[ 0 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
506-
tri[ 0 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
505+
tri[ 0 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
506+
tri[ 0 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
507507

508508
angle = 2.0f * PI / 3.0f - addAngle;
509-
tri[ 1 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
510-
tri[ 1 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
509+
tri[ 1 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
510+
tri[ 1 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
511511

512512
angle = -2.0f * PI / 3.0f - addAngle;
513-
tri[ 2 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( Cos( angle ) ) * eventSize) + event->radarLoc.x );
514-
tri[ 2 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( Sin( angle ) ) * eventSize) + event->radarLoc.y );
513+
tri[ 2 ].x = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Cos( angle ) ) * eventSize) + event->radarLoc.x );
514+
tri[ 2 ].y = REAL_TO_INT( (DOUBLE_TO_REAL( WWMath::Sin( angle ) ) * eventSize) + event->radarLoc.y );
515515

516516
// translate radar coords to screen coords
517517
radarToPixel( &tri[ 0 ], &tri[ 0 ], pixelX, pixelY, width, height );

Core/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DRopeDraw.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ void W3DRopeDraw::buildSegments()
8484
SegInfo info;
8585

8686
Real axis = GameClientRandomValueReal(0, 2*PI);
87-
info.wobbleAxisX = Cos(axis);
88-
info.wobbleAxisY = Sin(axis);
87+
info.wobbleAxisX = WWMath::Cos(axis);
88+
info.wobbleAxisY = WWMath::Sin(axis);
8989
info.line = NEW Line3DClass( Vector3(pos.x,pos.y,pos.z),
9090
Vector3(pos.x,pos.y,pos.z+eachLen),
9191
m_width * 0.5f, // width
@@ -186,7 +186,7 @@ void W3DRopeDraw::doDrawModule(const Matrix3D* transformMtx)
186186

187187
if (!m_segments.empty())
188188
{
189-
Real deflection = Sin(m_curWobblePhase) * m_wobbleAmp;
189+
Real deflection = WWMath::Sin(m_curWobblePhase) * m_wobbleAmp;
190190
const Coord3D* pos = getDrawable()->getPosition();
191191
Vector3 start(pos->x, pos->y, pos->z + m_curZOffset);
192192
Real eachLen = m_curLen / m_segments.size();

Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,10 @@ void W3DTreeBuffer::updateSway(const BreezeInfo& info)
351351
{
352352
Int i;
353353
for (i=0; i<NUM_SWAY_ENTRIES; i++) {
354-
Real factor = Cos(i*2.0f*PI/(NUM_SWAY_ENTRIES+1.0f));
354+
Real factor = WWMath::Cos(i*2.0f*PI/(NUM_SWAY_ENTRIES+1.0f));
355355
Real angle = info.m_lean + (info.m_intensity * factor);
356-
Real S = Sin(angle);
357-
Real C = Cos(angle);
356+
Real S = WWMath::Sin(angle);
357+
Real C = WWMath::Cos(angle);
358358
m_swayOffsets[i].X = info.m_directionVec.x * S;
359359
m_swayOffsets[i].Y = info.m_directionVec.y * S;
360360
m_swayOffsets[i].Z = C - 1.0f;

Core/Libraries/Include/Lib/BaseType.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#pragma once
3131

3232
#include "Lib/BaseTypeCore.h"
33-
#include "Lib/trig.h"
33+
#include "wwmath.h"
3434

3535
//-----------------------------------------------------------------------------
3636
typedef wchar_t WideChar; ///< multi-byte character representations
@@ -270,7 +270,7 @@ inline Real Coord2D::toAngle() const
270270
else if (c > 1.0)
271271
c = 1.0;
272272

273-
Real value = (Real)ACos( (Real)c );
273+
Real value = (Real)WWMath::ACos( (Real)c );
274274

275275
// Determine sign by checking Z component of dir cross vector
276276
// Note this is assumes 2D, and is identical to dotting the perpendicular of v with dir
@@ -295,7 +295,7 @@ inline Real Coord2D::toAngle() const
295295
else if (c > 1.0f)
296296
c = 1.0f;
297297

298-
return y < 0.0f ? -ACos(c) : ACos(c);
298+
return y < 0.0f ? -WWMath::ACos(c) : WWMath::ACos(c);
299299
#endif
300300
}
301301

Core/Libraries/Include/Lib/trig.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

Core/Libraries/Source/WWVegas/WW3D2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ set(WW3D2_SRC
230230
ww3dformat.cpp
231231
ww3dformat.h
232232
ww3dids.h
233-
ww3dtrig.h
233+
234234
)
235235

236236
add_library(corei_ww3d2 INTERFACE)

0 commit comments

Comments
 (0)