' Plasma effect with color cycling using sine wave functions
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL k  AS LONG
    LOCAL frame  AS LONG
    LOCAL numFrames  AS LONG
    LOCAL w  AS LONG
    LOCAL h  AS LONG
    LOCAL plasma(400) AS LONG
    LOCAL palette(256) AS LONG
    LOCAL x  AS DOUBLE
    LOCAL y  AS DOUBLE
    LOCAL v1  AS DOUBLE
    LOCAL v2  AS DOUBLE
    LOCAL v3  AS DOUBLE
    LOCAL v4  AS DOUBLE
    LOCAL value  AS LONG
    LOCAL idx  AS LONG
    LOCAL r  AS LONG
    LOCAL g  AS LONG
    LOCAL b  AS LONG
    LOCAL cx  AS LONG
    LOCAL cy  AS LONG
    LOCAL time  AS LONG
    LOCAL dist  AS DOUBLE
    LOCAL dx  AS DOUBLE
    LOCAL dy  AS DOUBLE
    LOCAL screenX  AS LONG
    LOCAL screenY  AS LONG
    w = 20
    h = 20
    numFrames = 5

    PRINT "=== Plasma Effect ==="
    PRINT "Resolution: " & FORMAT$(w) & "x" & FORMAT$(h)
    PRINT "Frames: " & FORMAT$(numFrames)
    PRINT

    FOR i = 0 TO 255
        r = (128 + 127 * SIN(i * 3.14159 * 2 / 256)) / 1
        g = (128 + 127 * SIN(i * 3.14159 * 2 / 256 + 2.094)) / 1
        b = (128 + 127 * SIN(i * 3.14159 * 2 / 256 + 4.188)) / 1
        IF r < 0 THEN r = 0
        IF r > 255 THEN r = 255
        IF g < 0 THEN g = 0
        IF g > 255 THEN g = 255
        IF b < 0 THEN b = 0
        IF b > 255 THEN b = 255
        palette(i) = (r * 65536 + g * 256 + b)
    NEXT i

    PRINT "Palette generated: 256 colors"
    PRINT

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

        FOR screenY = 0 TO h - 1
            FOR screenX = 0 TO w - 1
                x = screenX
                y = screenY

                v1 = SIN(x * 0.2 + time * 0.1)
                v2 = SIN(y * 0.2 + time * 0.1)
                v3 = SIN((x + y) * 0.15 + time * 0.08)
                v4 = SIN(SQR(x * x + y * y) * 0.2 + time * 0.12)

                value = (v1 + v2 + v3 + v4) * 32 + 128
                IF value < 0 THEN value = 0
                IF value > 255 THEN value = 255
                idx = screenY * w + screenX
                plasma(idx) = palette(value)
            NEXT screenX
        NEXT screenY

        FOR i = 0 TO 9
            cx = (i * 7) MOD w
            cy = (i * 11) MOD h
            idx = cy * w + cx
            r = plasma(idx) / 65536
            g = (plasma(idx) MOD 65536) / 256
            b = plasma(idx) MOD 256
            PRINT "  Pixel (" & FORMAT$(cx) & "," & FORMAT$(cy) & ") = RGB(" & FORMAT$(r) & "," & FORMAT$(g) & "," & FORMAT$(b) & ")"
        NEXT i
        PRINT
    NEXT frame

    PRINT "--- Plasma Formula ---"
    PRINT "  value = sin(x*0.2 + t*0.1)"
    PRINT "        + sin(y*0.2 + t*0.1)"
    PRINT "        + sin((x+y)*0.15 + t*0.08)"
    PRINT "        + sin(sqrt(x^2+y^2)*0.2 + t*0.12)"
    PRINT "  palette index = value * 32 + 128"
    PRINT

    PRINT "--- Color Palette Cycle ---"
    PRINT "  RGB channels phase-shifted by 120 degrees"
    PRINT "  R phase = 0 degrees"
    PRINT "  G phase = 120 degrees"
    PRINT "  B phase = 240 degrees"
    PRINT "  Full cycle period = 256 steps"

    PRINT
    PRINT "Plasma effect complete."

    FUNCTION = 0
END FUNCTION
