' Fire effect with pixel array, color palette, and decay simulation
#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 totalPixels  AS LONG
    LOCAL firePixels(800) AS LONG
    LOCAL firePalette(256) AS LONG
    LOCAL idx  AS LONG
    LOCAL below  AS LONG
    LOCAL spread  AS LONG
    LOCAL decay  AS LONG
    LOCAL r  AS LONG
    LOCAL g  AS LONG
    LOCAL b  AS LONG
    LOCAL heat  AS LONG
    LOCAL srcIdx  AS LONG
    LOCAL dstIdx  AS LONG
    LOCAL x  AS LONG
    LOCAL y  AS LONG
    LOCAL randVal AS LONG

    w = 20
    h = 20
    totalPixels = w * h
    numFrames = 5

    PRINT "=== Fire Effect ==="
    PRINT "Resolution: " & FORMAT$(w) & "x" & FORMAT$(h)
    PRINT "Total pixels: " & FORMAT$(totalPixels)
    PRINT

    FOR i = 0 TO 255
        IF i < 64 THEN
            r = i * 4
            g = 0
            b = 0
        ELSE
            IF i < 128 THEN
                r = 255
                g = (i - 64) * 4
                b = 0
            ELSE
                IF i < 192 THEN
                    r = 255
                    g = 255
                    b = (i - 128) * 4
                ELSE
                    r = 255
                    g = 255
                    b = 255
                END IF
            END IF
        END IF
        IF r > 255 THEN r = 255
        IF g > 255 THEN g = 255
        IF b > 255 THEN b = 255
        firePalette(i) = (r * 65536 + g * 256 + b)
    NEXT i

    PRINT "Fire palette: 256 colors (black -> red -> orange -> white)"
    PRINT

    FOR i = 0 TO totalPixels - 1
        firePixels(i) = 0
    NEXT i

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

        FOR x = 0 TO w - 1
            idx = (h - 1) * w + x
            randVal = (x * 37 + frame * 13) MOD 4
            IF randVal < 3 THEN
                firePixels(idx) = 200 + (x * 7 + frame * 3) MOD 55
            ELSE
                firePixels(idx) = 0
            END IF
            IF firePixels(idx) > 255 THEN firePixels(idx) = 255
            IF firePixels(idx) < 0 THEN firePixels(idx) = 0
        NEXT x

        FOR y = 0 TO h - 2
            FOR x = 0 TO w - 1
                srcIdx = (y + 1) * w + x
                dstIdx = y * w + x

                heat = firePixels(srcIdx)
                decay = 3 + (x + y + frame) MOD 5
                heat = heat - decay
                IF heat < 0 THEN heat = 0

                IF x > 0 THEN
                    below = firePixels(srcIdx - 1)
                    IF below > heat THEN heat = heat + (below - heat) / 4
                END IF
                IF x < w - 1 THEN
                    below = firePixels(srcIdx + 1)
                    IF below > heat THEN heat = heat + (below - heat) / 4
                END IF

                firePixels(dstIdx) = heat
            NEXT x
        NEXT y

        FOR i = 0 TO 9
            idx = (i * 23) MOD totalPixels
            heat = firePixels(idx)
            IF heat > 255 THEN heat = 255
            IF heat < 0 THEN heat = 0
            r = firePalette(heat) / 65536
            g = (firePalette(heat) MOD 65536) / 256
            b = firePalette(heat) MOD 256
            PRINT "  Pixel " & FORMAT$(i) & " (idx " & FORMAT$(idx) & "): heat=" & STR$(heat) & " RGB(" & FORMAT$(r) & "," & FORMAT$(g) & "," & FORMAT$(b) & ")"
        NEXT i
        PRINT
    NEXT frame

    PRINT "--- Fire Algorithm ---"
    PRINT "  1. Seed bottom row with random hot pixels"
    PRINT "  2. Each frame, heat rises upward"
    PRINT "  3. Heat decays by random amount (3-7)"
    PRINT "  4. Spread: blend with neighbors"
    PRINT "  5. Map heat value to fire palette"
    PRINT

    PRINT "--- Palette Zones ---"
    PRINT "  0-63: Black to Red"
    PRINT "  64-127: Red to Orange/Yellow"
    PRINT "  128-191: Yellow to White"
    PRINT "  192-255: White (hot core)"
    PRINT

    PRINT "--- Controls ---"
    PRINT "  More fuel = higher bottom heat values"
    PRINT "  Wind = horizontal shift of seed row"
    PRINT "  Cool rate = decay per frame"

    PRINT
    PRINT "Fire effect complete."

    FUNCTION = 0
END FUNCTION
