// Makes very old cubescripts compatible with newer versions of the game
// (write "load_compatibility" into your autoexec.cfg to load)

const alive         [ if (&& $numargs (isclient $arg1)) [ result (player $arg1 alive) ] [ result (player1 alive) ] ]
const currole       [ if (&& $numargs (isclient $arg1)) [ result (player $arg1 role) ] [ result (player1 role) ] ]
const curmode       [ result $gamemode ]
const getclientmode [ result $gamemode ]
const curteam       [ if (&& $numargs (isclient $arg1)) [ result (player $arg1 team) ] [ result (player1 team) ] ]
const findpn        [ if (isclient $arg1) [ result (player $arg1 name) ] [ result [] ] ]
const isSpect       [ if (&& $numargs (isclient $arg1)) [ result (player $arg1 spect) ] [ result (player1 spect) ] ]

const pstat_score [
  if (isclient $arg1) [
    result (concat (player $arg1 flags) (player $arg1 frags) (player $arg1 deaths) (player $arg1 points) (player $arg1 team) (player $arg1 tks) (player $arg1 name))
  ] [ result (concat [0 0 0 0 -1 0] (addpunct)) ]
]

const orderscorecolumns [
    if (= $arg1 1) [
        sc_clientnum 0;
        sc_name 1;
        sc_flags 2;
        sc_frags 3;
        sc_deaths 4;
        sc_score 5;
        sc_lag 6;
        sc_ratio -1;
    ] [
        sc_flags 0;
        sc_frags 1;
        sc_deaths 2;
        sc_ratio -1;
        sc_score 4;
        sc_lag 5;
        sc_clientnum 6;
        sc_name 7;
    ]
]

// deprecated functions

// Helper aliases for easy color insertion. (e.g. echo (red)Hello (blue)world! (white)My fov today is: (orange) $fov)
loop k 10 [ const (at [green blue yellow red gray white dbrown dred purple orange] $k) (format [if (= $arg1 -1) [ result %1 ] [ result %2 ]] $k (concatword "\f" $k)) ]

// makeshift "?" operator for cubescript
// ex: /? (> $maxroll 0) [voicecom negative D:] [voicecom yes :D]
//     /echo I am (? (>= (player1 health) 50) [result healthy] [result weak]) atm
? = [
  if (> $numargs 1) [
    if (> $numargs 2) [
      if $arg1 $arg2 $arg3
    ] [
      if $arg1 $arg2
    ]
  ]
]

const showedithide [
    if $edithideentmask [
        push n "Hidden entities:"
        push m 1
        looplist (listoptions ents) e [
            if (&b $edithideentmask $m) [
                n = (concat $n $e)
            ]
            += m $m
        ]
        echo (pop n m)
    ] [
        echo "all entities are visible"
    ]
]

const setedithide [
    edithideentmask 0
    looplist $arg1 e [
        push n (findlist (listoptions ents) $e)
        if (< $n 0) [
            echo "\f3" $e is not an entity type
        ] [
            edithideentmask (|b $edithideentmask (powf 2 $n))
        ]
        pop n
    ]
]

const seteditshow [
    push n (findlist (listoptions ents) $arg1)
    if (< $n 0) [
        echo "\f3" $arg1 is not an entity type
    ] [
        edithideentmask (- (- (powf 2 (listlen (listoptions ents))) 1) (powf 2 $n))
    ]
    pop n
]

// commands to increment/decrement an alias - or to fail silently, if the alias doesn't exist
const ++ [ if (checkalias $arg1) [ += $arg1 1 ] ]
const -- [ if (checkalias $arg1) [ -= $arg1 1 ] ]
const ++f [ if (checkalias $arg1) [ +=f $arg1 1 ] ]
const --f [ if (checkalias $arg1) [ -=f $arg1 1 ] ]
