diff --git a/src/ale/emucore/TIA.cxx b/src/ale/emucore/TIA.cxx index 3dad8f1d9..bc463051c 100644 --- a/src/ale/emucore/TIA.cxx +++ b/src/ale/emucore/TIA.cxx @@ -2746,30 +2746,13 @@ void TIA::poke(uint16_t addr, uint8_t value) myPOSM1 += ourCompleteMotionTable[x][myHMM1]; myPOSBL += ourCompleteMotionTable[x][myHMBL]; - if(myPOSP0 >= 160) - myPOSP0 -= 160; - else if(myPOSP0 < 0) - myPOSP0 += 160; - - if(myPOSP1 >= 160) - myPOSP1 -= 160; - else if(myPOSP1 < 0) - myPOSP1 += 160; - - if(myPOSM0 >= 160) - myPOSM0 -= 160; - else if(myPOSM0 < 0) - myPOSM0 += 160; - - if(myPOSM1 >= 160) - myPOSM1 -= 160; - else if(myPOSM1 < 0) - myPOSM1 += 160; - - if(myPOSBL >= 160) - myPOSBL -= 160; - else if(myPOSBL < 0) - myPOSBL += 160; + // Normalize all positions to valid range [0, 159] using modular arithmetic + myPOSP0 = ((myPOSP0 % 160) + 160) % 160; + myPOSP1 = ((myPOSP1 % 160) + 160) % 160; + myPOSM0 = ((myPOSM0 % 160) + 160) % 160; + myPOSM1 = ((myPOSM1 % 160) + 160) % 160; + myPOSBL = ((myPOSBL % 160) + 160) % 160; + myCurrentBLMask = &ourBallMaskTable[myPOSBL & 0x03] [(myCTRLPF & 0x30) >> 4][160 - (myPOSBL & 0xFC)];