// Decides whether to open the admin or normal in-game menu.
const ingamemenu [ if (player1 role) [ showmenu [In-game admin] ] [ showmenu In-game ] ]

const ifauthsetup [
    if (authsetup) [
        $arg1
    ] [
        checkaccount
    ]
]

// "Join server" menu.
personalserverlist = []
const joinservermenu [
      searchlan 1
      showallservers 1
      showonlyfavourites 0
      updatefrommaster
      personalserverlist
      showmenu server
]

const joinlanservermenu [
      searchlan 2
      showallservers 0
      showonlyfavourites 0
      showmenu server 
]

const customconnectmenu [ showmenu [Custom connect] ]
const adminconnectmenu [ showmenu [Admin connect] ]


// Common parts of in-game menus
tempalias __ingamemenu_1 [
    push t (player1 team)
    menuitemtextinput "Nickname: " "result $curname" [ name $arg1 ]
    menuitem "Change weapon\i\3"        [ showmenu Weapons ]                  [ __ingamemenu_showmodel 0 ]
    menuitem "Change player skin\i\3"   [showmenu @(at [CLA RVSF CLA RVSF ANY ANY ANY] (&b $t 1))] [ __ingamemenu_showmodel 1 ]
    menuitem "Change to the enemy team" changeteam                            [ __ingamemenu_showmodel 0 ]
    menuitem [Change to @(at "spectate active" (> $t 1)) mode] spectate
    pop t
]

tempalias __ingamemenu_2 [
    menuitem "Get additional server information" [ showmenu serverinfo ]
    menuitem "Download a demo\i\3"                [ listdemos; showmenu [Download demo] ]
    menuitem "" -1
    menuitem "\f4Main menu\i\3"                   [ closecurmenu; showmenu main ]
    menuitem "\f4Browse servers\i\3"              [ closecurmenu; ifauthsetup joinservermenu ]
    menuitem "\f3Disconnect from server"           disconnect
    menuitem "\f3Quit AssaultCube"                 quit
]

const __ingamemenu_showmodel [
  if (&& (< (player1 team) 4) $arg1) [
    menumdl (curmenu) [playermodels/@(at [CLA RVSF CLA RVSF] (player1 team))/0@(+ 1 (player1 skin))] idle 15 3
  ] [
    menumdl (curmenu)
  ]
]

////  In-Game  //// (Normal in-game menu that opens when pressing escape and connected to a game.)

newmenu [In-game]
menuinit [
    resetcurmenu
    __ingamemenu_1
    menuitem "" -1
    menuitem "Set mode/map\i\3"        [ showmenu Gamemode ]
    // menuitem "Kick/ban player\i\3"     [ showmenu kick/ban ] // only noobish vote abusers use this, if you really want it, just uncomment
    menuitem "Ignore/Unignore all\i\3" [ if $ingall [ unignoreall ] [ ignoreall ] ]
    menuitem "Adjust teams\i\3"        [ showmenu [Team adjustment] ]
    __ingamemenu_2
]
menuinitselection 13

////  In-Game admin //// (In-game menu that opens if when you press escape if you're admin state and connected to a game.)

newmenu [In-game admin]
menuinit [
    resetcurmenu
    menuitem "Set mode/map\i\3"                  [ showmenu Gamemode ]
    menuitem "Kick/ban player\i\3"               [ showmenu kick/ban ]
    menuitem "Adjust teams\i\3"                  [ showmenu [Team adjustment] ]
    menuitem "Change mastermode\i\3"             [ showmenu Mastermode ]
    menuitem "Change the server description\i\3" [ saycommand "/serverdesc " ]
    menuitem "Find a players IP address\i\3"     [ saycommand "/whois " ]
    menuitem "Give admin\i\3"                    [ showmenu [give admin] ]
    menuitem "" -1
    __ingamemenu_1
    __ingamemenu_2
]
menuinitselection 17

////  In-Game > Gamemode  ////

newmenu Gamemode
menuitem [Team Deathmatch]          [ mode 0; showmenu maps_most ]
menuitem [Deathmatch]               [ mode 2; showmenu maps ]
menuitem [Team Survivor]            [ mode 4; showmenu maps_most ]
menuitem [Survivor]                 [ mode 3; showmenu maps ]
menuitem [Capture the Flag]         [ mode 5; showmenu maps_flags ]
menuitem [Hunt the Flag]            [ mode 13; showmenu maps_most ]
menuitem [Keep the Flag]            [ mode 15; showmenu maps_flags ]
menuitem [Team Keep the Flag]       [ mode 14; showmenu maps_tktf ]
menuitem [Team Pistol Frenzy]       [ mode 16; showmenu maps_most ]
menuitem [Pistol Frenzy]            [ mode 6; showmenu maps ]
menuitem [Team Last Swiss Standing] [ mode 17; showmenu maps_most ]
menuitem [Last Swiss Standing]      [ mode 9; showmenu maps ]
menuitem [One Shot, One Kill]       [ mode 10; showmenu maps ]
menuitem [Team One Shot, One Kill]  [ mode 11; showmenu maps_most ]
menuitem [Cooperative Editing]      [ mode 1; showmenu all_custom_maps ]

////  In-Game > kick/ban  ////

newmenu kick/ban
menuitem "Kick a player\i\3" [ _kickbanaction = kick; showmenu reason ]
menuitem "Ban a player\i\3"  [ _kickbanaction = ban; showmenu reason ]
//menuitem "Kick a player" [ showmenu "kick player" ]
//menuitem "Ban a player"  [ showmenu "ban player" ]
menuitem "Remove all bans" removebans

////  In-Game > kick/ban > reason  ////

newmenu reason
tempalias _kickbanaction kick
menuinit [ _kickbanreason = None; if (! (strcmp $_kickbanaction kick)) [ if (! (strcmp $_kickbanaction ban)) [ _kickbanaction = ban ] ] ]
menuitem [intentional TK]    [ _kickbanreason = [intentional TK]; showmenu (concat $_kickbanaction player) ]
menuitem [abusive behaviour] [ _kickbanreason = [abusive behaviour]; showmenu (concat $_kickbanaction player) ]
menuitem [bad language]      [ _kickbanreason = [bad language]; showmenu (concat $_kickbanaction player) ]
menuitemtextinput [Reason: ] [] [ _kickbanreason = $arg1 ] [] 255
menuitem $_kickbanaction [ showmenu (concat $_kickbanaction player) ]
menuinitselection 2

////  In-Game > kick/ban > reason > kick player ////
////  In-Game > kick/ban > reason > ban player ////
// hardcoded

////  In-Game > Team adjustment  ////

newmenu [Team adjustment]
menuitem [Initiate shuffleteams] shuffleteams
menuitem [Force a team change]   [ showmenu [force team] ]
menuitem [Force yourself to enemy team] forceme
menuitem [Initiate autoteam] [ showmenu Auto-team ]

////  In-Game > Team adjustment > force team  ////
// hardcoded

////  In-Game > Team adjustment > Auto-team  ////

newmenu Auto-team
menuitem [Turn auto-team on]  [ autoteam 1 ]
menuitem [Turn auto-team off] [ autoteam 0 ]

////  In-Game admin > Mastermode  ////

newmenu Mastermode
menuitem [Unlock the server (open)]     [ mastermode 0 ]
menuitem [Lock the server (private)]    [ mastermode 1 ]
menuitem [Lock the server (match mode)] [ mastermode 2 ]

////  In-Game admin > give admin  ////
// hardcoded


////  Main > Multiplayer  //// (Multiplayer menu only opens through the main menu (when disconnected).)

newmenu multiplayer
menuitem [Join a server]     [ ifauthsetup joinservermenu ]
menuitem [Join a LAN server] [ ifauthsetup joinlanservermenu ]
menuitem [Custom connect]    [ ifauthsetup customconnectmenu ]
menuitem [Connect as Admin]  [ ifauthsetup adminconnectmenu ]
menuitem [] -1
menuitem [Auth settings] [ showmenu [auth] ]
menuitem [Force serverlist update] [ updatefrommaster 1; ifauthsetup joinservermenu ]

////  Main > Multiplayer > Custom connect  ////

newmenu [Custom connect]
menuinit [ tempalias cc1 []; tempalias cc2 []; tempalias cc3 [] ] // Resets the aliases
menuitemtextinput "Server IP/DNS: \t\t" []           [ cc1 = $arg1 ] [] 255
menuitemtextinput "Server port (optional): \t" []     [ cc2 = $arg1 ] [] 6
menuitemtextinput "Server password (optional): \t" [] [ cc3 = $arg1 ] [] 128 1
menuitem "" -1
menuitem "\f2\t\t     [ Connect ]\t\t\t" [ connect $cc1 $cc2 $cc3 ]

////  Main > Multiplayer > Admin connect  ////

newmenu [Admin connect]
menuinit [ tempalias acc1 []; tempalias acc2 []; tempalias acc3 []; echo "\f3To use this menu to connect as administrator, you will need to know the admin password for the server." ] // Resets the aliases
menuitemtextinput "Server IP/DNS: \t\t" []       [ acc1 = $arg1 ] [] 255
menuitemtextinput "Server port (optional):\t" [] [ acc2 = $arg1 ] [] 6
menuitemtextinput "Server admin password: \t" [] [ acc3 = $arg1 ] [] 128 1
menuitem "" -1
menuitem "\f2\t\t  [ Admin connect ]\t\t" [ connectadmin $acc1 $acc2 $acc3 ]

////  Main > Multiplayer > server  ////
// hardcoded

////  Main > Multiplayer > server > serverbrowser help  ////

newmenu [serverbrowser help]
menuinitselection 1
menuinit [
    resetcurmenu
    menuitem "F1: Display this help menu" -1
    menuitem "F3: Search for players by nickname\i\3"           [ showmenu [search player] ]
    menuitem "Search for servers\i\3"                           [ showmenu [search server] ]
    menuitem "F4: Edit favourites\i\3"                          [ showmenu [edit favourites] ]
    menuitem "F5: Update the serverlist from the masterserver"  [ updatefrommaster 1; closecurmenu ]
    menuitem "F9: Get additional server information\i\3"        [ showmenu serverinfo ]
    menuitem "Edit alternative views in serverbrowser\i\3"      [ showmenu [serverbrowser alternative views] ]
    menuitem "" -1
    menuitem "Options:" -1
    menuitemcheckbox "F6: Reverse the sort order:" "$serversortdir"          [ serversortdir $arg1 ]
    menuitemcheckbox "F7: Only show good servers:" "$showonlygoodservers"    [ showonlygoodservers $arg1 ]
    menuitemcheckbox "F8: Show minutes remaining:" "$showminremain"          [ showminremain $arg1 ]
    menuitemcheckbox "Sort official maps ontop:" "$serversortpreferofficial" [ serversortpreferofficial $arg1 ]
    menuitemcheckbox "Use gamemode acronyms:" "$modeacronyms"                [ modeacronyms $arg1 ]
    menuitemcheckbox "Show number of all players on the servers:" "$showallplayersnumber" [ showallplayersnumber $arg1 ]
    menuitemslider "Favourites tag column:\t" 0 2 "$serverbrowserhidefavtag" ["Show always" "Show when used" "Hide always"] [ serverbrowserhidefavtag $arg1 ] 1
    menuitemslider "Server IP & port:\t\t" 0 2 "$serverbrowserhideip" [Show "Show in grey" Hide] [ serverbrowserhideip $arg1 ] 1
    menuitemslider "Sort servers by:\t\t" 0 7 "$serversort" [Ping "Number of players" "Max players" "Minutes remaining" "Map name" Gamemode "IP address" "Server description"] [ serversort $arg1 ] 1
    if (strlen (listfavcats)) [
        menuitem "" -1
        menuitem "Keys to add/remove servers from categories:" -1
        looplisti (listfavcats) fav [
            if (< $i 10) [
                menuitem [@(+ $i 1):  Toggle category @[sbfavourite_@[fav]_desc] @"<\fs"@[sbfavourite_@[fav]_tag]@"\fr>"] -1
            ]
        ]
    ]
]

////  Main > Multiplayer > server > serverbrowser help > search player  ////

newmenu [search player]
alias __lastsearchname ""
menuitemtextinput "Search servers for a player: " "result $__lastsearchname" [ __lastsearchname = $arg1 ]
menuitem "\f2\t\t       [ Search ]" [ closecurmenu; searchnickname $__lastsearchname]
menuinitselection 1

////  Main > Multiplayer > server > serverbrowser help > search server  ////

newmenu [search server]
menuitemtextinput " Search for server: " "result $sbfavourite_SEARCH_keys" [ sbfavourite_SEARCH_keys = $arg1 ]
menuitem "\f2\t\t[ Search ]" [ showonlyfavourites (addfavcategory SEARCH); showmenu server ] [ showonlyfavourites 0 ]
menuinitselection 1

////  Main > Multiplayer > server > serverbrowser help > edit favourites  ////

tempalias __curfav 1
newmenu [edit favourites]
menuinit [
    resetcurmenu
    looplisti (listfavcats) fav [
        if (< $i 10) [
            menuitem [@(+ 1 (mod $i 10)): @[sbfavourite_@[fav]_desc] @"<\fs"@[sbfavourite_@[fav]_tag]@"\fr>"] [__curfav = @i; showmenu [edit favourites category]]
        ] [
            menuitem [@[sbfavourite_@[fav]_desc] @"<\fs"@[sbfavourite_@[fav]_tag]@"\fr>"] [__curfav = @i; showmenu [edit favourites category]]
        ]
    ]
]

////  Main > Multiplayer > server > serverbrowser help > edit favourites > edit favourites category  ////

newmenu [edit favourites category]
menuinit [
    resetcurmenu
    push fav (at (listfavcats) $__curfav)
    push n (+ 1 $__curfav)
    menuitem [Category #@n@", name: \f2"@fav] -1
    menuitem "" -1
    menuitemtextinput "Tag:  "         [escapenoquotes $sbfavourite_@[fav]_tag]  [sbfavourite_@[fav]_tag  = (at (addpunct $arg1) 0)] [] 15
    menuitemtextinput "Description:  " [escapenoquotes $sbfavourite_@[fav]_desc] [sbfavourite_@[fav]_desc = (at (addpunct $arg1) 0)] [] 45
    menuitem "" -1
    menuitemslider "Background red:  \t" 0 100 $[sbfavourite_@[fav]_red]   10 [sbfavourite_@[fav]_red = $arg1] 1
    menuitemslider "Background green:\t" 0 100 $[sbfavourite_@[fav]_green] 10 [sbfavourite_@[fav]_green = $arg1] 1
    menuitemslider "Background blue: \t" 0 100 $[sbfavourite_@[fav]_blue]  10 [sbfavourite_@[fav]_blue = $arg1] 1
    menuitemslider "Background alpha:\t" 0 100 $[sbfavourite_@[fav]_alpha] 10 [sbfavourite_@[fav]_alpha = $arg1] 1
    menuitem "" -1
    menuitemtextinput "Keyword list:" [result $sbfavourite_@[fav]_keys] [sbfavourite_@[fav]_keys = $arg1] [] 255
    menuitem "" -1
    menuitemtextinput "Weight:" [result $sbfavourite_@[fav]_weight] [sbfavourite_@[fav]_weight = $arg1] [] 10
    menuitem "" -1
    menuitemradio "" 0 1 $[sbfavourite_@[fav]_ignore] ["Enable this category " "Ignore this category"] [sbfavourite_@[fav]_ignore = $arg1]
    menuitem "" -1
    menuitem "Show servers of this category\i\3" [showonlyfavourites @n ; showmenu server] [ showonlyfavourites 0 ]
    pop fav n
]

////  Main > Multiplayer > server > serverbrowser help > serverinfo  ////
// hardcoded

////  Main > Multiplayer > server > serverbrowser help > serverbrowser alternative views  ////

newmenu [serverbrowser alternative views]
menuitem "Alt + Mouse wheel (Alt + Up/Down) toggles views in serverbrowser." -1
menuitem "" -1
menuitemtextinput "Categories of favourites in alternative views: " [getalias serverbrowseraltviews] [alias serverbrowseraltviews $arg1] [] 255
menuitem "(type just names of categories, and not tags)." -1
menuinitselection 2


// connecting from the serverbrowser to a password-protected server
// called by sbconnect

newmenu [password required]
menuinit [
    resetcurmenu
    sbpwd = ""
    menuitem $sbmsg -1
    menuitem "You need a password to join it." -1
    menuitem "" -1
    menuitemtextinput "Password: \t" [] [ sbpwd = $arg1 ] [] 128 1
    menuitem "" -1
    menuitem "\f2\t\t  [ Connect ]\t\t" [ echo connecting to $sbdesc ; connect $sbdns $sbport $sbpwd ]
    menuitem "\f2\t\t  [ Connect without password ]\t\t" [ echo connecting to $sbdesc ; connect $sbdns $sbport ]
    menuinitselection 3
]

newmenu getmap
menuinit [
    resetcurmenu
    menuitem "      This server is using a different version of this map..." -1
    menuitem "" -1
    menuitem $__getmaprevisions
    menuitem "" -1
    menuitem "\f2    [ Download the servers map version and join the game ]"     [ getmap ]
    menuitem "\f3 [ Keep the local map version and disconnect from the server ]" [ showmenu ""; disconnect ]
    menuinitselection 4
]
