Skip to content

Commit 082af88

Browse files
committed
fix(trek): integer decode for quadrant K/B/S counts (fixed-point hang)
EnterQuadrant decoded the packed quadrant code with float tricks: K3=int(G*.01) : B3=int(G*.1)-10*K3 : S3=G-100*K3-10*B3 Fine under the float interpreter, fatal when transpiled to cc65 fixed-point: 16.16 RGC_MUL mangles .01/.1 (keeps ~2 fractional bits) and G/100 overflows the 32-bit fixed range for G>=256, so K3 undercounts and S3 = G-100*K3-... OVERcounts. FindEmpty() (retry-until-empty-sector) then tries to place more stars than the 64-sector quadrant holds and spins forever — trek hung right after the mission briefing on a real C64. Decode with integer \ and MOD instead (G encodes K*100+B*10+S): K3=G\100 : B3=(G\10) MOD 10 : S3=G MOD 10 Exact, no overflow, cheap — and behaviour-identical in the interpreter. Lesson: integer logic wants integer ops, not float arithmetic, on 8-bit.
1 parent a9ceae8 commit 082af88

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

examples/trek-portable.bas

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,14 @@ function EnterQuadrant()
203203
print "ENTERING ";QN$;" QUADRANT..."
204204
end if
205205
end if
206-
' Decode G(quad): K3/B3/S3 = klingons / starbases / stars in this quadrant
207-
K3=int(G(QUADRANT_X,QUADRANT_Y)*.01)
208-
B3=int(G(QUADRANT_X,QUADRANT_Y)*.1)-10*K3
209-
S3=G(QUADRANT_X,QUADRANT_Y)-100*K3-10*B3
206+
' Decode G(quad): K3/B3/S3 = klingons / starbases / stars in this quadrant.
207+
' G encodes the 3 digits K*100+B*10+S. Use INTEGER divide/MOD, not float
208+
' tricks (int(G*.01)): on fixed-point targets (cc65) G*.01 loses precision
209+
' and G/100 overflows 16.16, mis-decoding the counts -> FindEmpty overfills
210+
' the quadrant and spins forever. Integer ops are exact and cheap.
211+
K3=G(QUADRANT_X,QUADRANT_Y)\100
212+
B3=(G(QUADRANT_X,QUADRANT_Y)\10) MOD 10
213+
S3=G(QUADRANT_X,QUADRANT_Y) MOD 10
210214
for I=1 to 3
211215
K(I,1)=0
212216
K(I,2)=0

0 commit comments

Comments
 (0)