: score.ayaan — score + high-score tracking
:
: High-score is persisted to a file under the user's config dir so it
: survives across game runs.

: Initialize the current game's score to 0.
func score_reset
    set __gt_score = 0
endfunc

: Add N points to the score.
func score_add n
    set __gt_score = {__gt_score + n}
endfunc

: Returns the current score.
func score_get
    set result = __gt_score
endfunc

: Load the high score for a named game (e.g. "geometry_dash") from disk.
: Result placed in __gt_highscore as well as `result`.
func highscore_load game
    set path = "~/.config/aspkg_highscore_{game}.txt"
    file_exists path -> ex
    if ex == 1
        readall path -> raw
        set __gt_highscore = {int(raw)}
    else
        set __gt_highscore = 0
    endif
    set result = __gt_highscore
endfunc

: Save the current score as the high score if it beats the saved one.
: Returns 1 if a new high score was set, 0 otherwise.
func highscore_maybe_save game
    set path = "~/.config/aspkg_highscore_{game}.txt"
    file_exists path -> ex
    set old = 0
    if ex == 1
        readall path -> raw
        set old = {int(raw)}
    endif
    set result = 0
    if __gt_score > old
        writefile path __gt_score
        set result = 1
    endif
endfunc
