' Starfield simulation with parallax scrolling layers
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numStars  AS LONG
    LOCAL starX(200) AS LONG
    LOCAL starY(200) AS LONG
    LOCAL starZ(200) AS LONG
    LOCAL starSpeed(200) AS LONG
    LOCAL screenW  AS LONG
    LOCAL screenH  AS LONG
    LOCAL frame  AS LONG
    LOCAL numFrames  AS LONG
    LOCAL sx  AS LONG
    LOCAL sy  AS LONG
    LOCAL sz  AS LONG
    LOCAL speed  AS LONG
    LOCAL layer  AS LONG
    LOCAL layerOffset  AS LONG
    LOCAL px  AS LONG
    LOCAL py  AS LONG
    screenW = 80
    screenH = 25
    numStars = 80
    numFrames = 10

    PRINT "=== Starfield Simulation ==="
    PRINT "Screen: " & FORMAT$(screenW) & "x" & FORMAT$(screenH)
    PRINT "Stars: " & FORMAT$(numStars)
    PRINT "Frames: " & FORMAT$(numFrames)
    PRINT

    FOR i = 0 TO numStars - 1
        starX(i) = (i * 137 + 50) MOD screenW
        starY(i) = (i * 97 + 30) MOD screenH
        starZ(i) = (i MOD 3) + 1
        starSpeed(i) = starZ(i)
    NEXT i

    FOR frame = 0 TO numFrames - 1
        PRINT "--- Frame " & FORMAT$(frame) & " ---"

        FOR i = 0 TO numStars - 1
            speed = starSpeed(i)
            starX(i) = starX(i) - speed
            IF starX(i) < 0 THEN
                starX(i) = starX(i) + screenW
                starY(i) = (starY(i) + (i MOD 5) - 2 + screenH) MOD screenH
            END IF
        NEXT i

        PRINT "  Star positions (layer view):"
        FOR layer = 1 TO 3
            layerOffset = 0
            FOR i = 0 TO numStars - 1
                IF starZ(i) = layer THEN
                    layerOffset = layerOffset + 1
                END IF
            NEXT i
            IF layer = 1 THEN PRINT "    Layer 1 (near):  " & STR$(layerOffset) & " stars, speed 1"
            IF layer = 2 THEN PRINT "    Layer 2 (mid):   " & STR$(layerOffset) & " stars, speed 2"
            IF layer = 3 THEN PRINT "    Layer 3 (far):   " & STR$(layerOffset) & " stars, speed 3"
        NEXT layer
        PRINT
    NEXT frame

    PRINT "--- Star Metrics ---"
    FOR i = 0 TO 9
        PRINT "  Star " & FORMAT$(i) & ": (" & FORMAT$(starX(i)) & "," & FORMAT$(starY(i)) & ") z=" & STR$(starZ(i))
    NEXT i
    PRINT

    PRINT "--- Parallax Effect ---"
    PRINT "  Near stars move fast (parallax shift = 3)"
    PRINT "  Mid stars move medium (parallax shift = 2)"
    PRINT "  Far stars move slow (parallax shift = 1)"
    PRINT "  Creates depth illusion on 2D screen"
    PRINT

    PRINT "--- Controls ---"
    PRINT "  Arrow keys: move viewpoint"
    PRINT "  +/-: adjust speed"
    PRINT "  Space: toggle pause"
    PRINT "  ESC: exit"

    PRINT
    PRINT "Starfield simulation complete."

    FUNCTION = 0
END FUNCTION
