: AyaanScript+ — 3D Starfield with user input.
: Classic warp-speed starfield. Each star is a 3D point projected to 2D
: with perspective. Closer stars are bigger + brighter.
:
: CONTROLS:
:   Arrow keys  - steer left/right/up/down
:   SPACE       - hold for hyperspace (4x speed + streaks)
:   +/-         - increase/decrease base speed
:   R           - randomize star field
:   ESC         - quit

const W = 800
const H = 600
const CX = 400
const CY = 300
const FOCAL = 320
const Z_FAR = 1000
const N = 300

gui_native "AyaanScript+ - 3D Starfield" W H
gui_fps 60

: initialize star positions in 3D space
list xs = []
list ys = []
list zs = []
set i = 0
repeat N i
    rand sx -1200 1200
    rand sy -900 900
    rand sz 1 Z_FAR
    push xs sx
    push ys sy
    push zs sz
endrepeat

set speed = 6
set running = 1
set frame = 0
set tstart = {time()}
set last_plus = 0
set last_minus = 0
set last_r = 0

while running == 1
    gui_open running
    if running == 0
        break
    endif

    : starscape background — pure black for contrast
    gui_clear "#000005"

    : ===== INPUT =====
    gui_key_down "esc" -> esc
    if esc == 1
        set running = 0
    endif
    gui_key_down "left" -> kl
    gui_key_down "right" -> kr
    gui_key_down "up" -> ku
    gui_key_down "down" -> kd
    gui_key_down "space" -> ks
    gui_key_down "plus" -> kplus
    gui_key_down "minus" -> kminus
    gui_key_down "r" -> kr2

    : steering — accumulate per-frame offset
    set turn_x = 0
    set turn_y = 0
    if kl == 1
        set turn_x = -40
    endif
    if kr == 1
        set turn_x = 40
    endif
    if ku == 1
        set turn_y = -40
    endif
    if kd == 1
        set turn_y = 40
    endif

    : hyperspace
    set effective_speed = speed
    if ks == 1
        set effective_speed = {speed * 4}
    endif

    : speed adjust (edge-triggered)
    if kplus == 1
        if last_plus == 0
            set speed = {speed + 1}
        endif
        set last_plus = 1
    else
        set last_plus = 0
    endif
    if kminus == 1
        if last_minus == 0
            set speed = {speed - 1}
            if speed < 1
                set speed = 1
            endif
        endif
        set last_minus = 1
    else
        set last_minus = 0
    endif

    : randomize (edge-triggered)
    if kr2 == 1
        if last_r == 0
            set i = 0
            repeat N i
                set k = {i - 1}
                rand sx -1200 1200
                rand sy -900 900
                rand sz 1 Z_FAR
                lset xs k sx
                lset ys k sy
                lset zs k sz
            endrepeat
        endif
        set last_r = 1
    else
        set last_r = 0
    endif

    : ===== UPDATE + DRAW STARS =====
    set i = 0
    repeat N i
        set k = {i - 1}
        list_at xs k -> sx
        list_at ys k -> sy
        list_at zs k -> sz

        : steering
        set sx = {sx + turn_x}
        set sy = {sy + turn_y}

        : advance toward camera
        set sz = {sz - effective_speed}

        : respawn if passed camera or steered out of frustum
        if sz < 1
            rand sx -1200 1200
            rand sy -900 900
            set sz = Z_FAR
        endif

        : perspective project
        set px = {CX + sx * FOCAL / sz}
        set py = {CY + sy * FOCAL / sz}

        : closer = bigger + brighter
        set size = {int(6 - sz / 180)}
        if size < 1
            set size = 1
        endif

        : color by depth band
        if sz < 200
            gui_fill_circle px py size "white"
            : streak for hyperspace
            if ks == 1
                set tail_z = {sz + effective_speed * 4}
                set tx = {CX + sx * FOCAL / tail_z}
                set ty = {CY + sy * FOCAL / tail_z}
                gui_line px py tx ty "lightgray"
            endif
        endif
        if sz >= 200
            if sz < 500
                gui_fill_circle px py size "lightgray"
            endif
            if sz >= 500
                gui_pixel px py "gray"
            endif
        endif

        : persist
        lset xs k sx
        lset ys k sy
        lset zs k sz
    endrepeat

    : ===== HUD =====
    gui_text_big 20 20 "STARFIELD" "cyan" 2
    gui_text 20 55 "ARROWS steer  SPACE hyperspace  +/- speed  R reset  ESC quit" "lightgray"

    set y_status = {H - 40}
    gui_text 20 y_status "speed:" "white"
    set x_val = 90
    gui_text x_val y_status effective_speed "yellow"

    : fps counter
    set frame = {frame + 1}
    set elapsed = {time() - tstart}
    if elapsed > 0
        set fps = {int(frame / elapsed)}
        set y_fps = {H - 20}
        gui_text 20 y_fps "fps:" "white"
        gui_text 90 y_fps fps "yellow"
    endif

    gui_update
endwhile

gui_close
psyska -/ "Starfield closed."
