// bot menus and tools

persistidents 0

newmenu botmaps

const genbotmaps [
  loop gbm (listlen $arg1) [
    listmapname = (at $arg1 $gbm)
    menutext = $listmapname (concat map $listmapname)
    menuitemmapload $listmapname (concat "tmp_botmap =" $listmapname "; startbotmap")
  ]
]
genbotmaps $defaultmaps

tmp_botskill = good
tmp_numbots = 1
tmp_mode =
tmp_botmap =

const isbotteammode [ return (&& (curmodeattr bot) (curmodeattr team)) ]

// add bots
const startbotmatch [
  if (isbotteammode) [ // team botmode
    numrvsf = (div $tmp_numbots 2)
    numcla = (div $tmp_numbots 2)
    if (= (player1 team) 0) [ -- numcla ] [
      if (= (player1 team) 1) [ -- numrvsf ]
    ]
    if (&& (= $numrvsf $numcla) $needsswitch) [ -- numrvsf ]
    addnbot $numrvsf RVSF $tmp_botskill
    addnbot $numcla CLA $tmp_botskill
  ] [ // bot FFA
    addnbot $tmp_numbots random $tmp_botskill
  ]
]

const preparebotmatch [
  echo starting botmatch..
  needsswitch = 0
  if (&& (!= (player1 team) 0) (!= (player1 team) 1)) [ needsswitch = 1; team RVSF ]
  // register event that is fired during mapload
  addOnLoadOnce [
    kickallbots
    startbotmatch // add bots
    mode 0 // reset mode for next maps
  ]
  mode $tmp_mode
  map $tmp_botmap $menuBotGameLength
]

const afterdisconnect [
  if $connected [
    addOnLoadOnce $arg1
    disconnect
  ] [
    if (watchingdemo) [
      addOnLoadOnce $arg1
      stopdemo
    ] $arg1
  ]
]

// register events and change map
const startbotmap [ afterdisconnect preparebotmatch ]

const emptymap [
  afterdisconnect [
    mode 0
    showmenu maps
  ]
]

newmenu singleplayer

// teamplay not mature yet
menuitem (_ [Bot Team Deathmatch])         [ tmp_mode = 7; showmenu botskill ]
menuitem (_ [Bot Deathmatch])              [ tmp_mode = 8; showmenu botskill ]
menuitem (_ [Bot One Shot, One Kill])      [ tmp_mode = 12; showmenu botskill ]
menuitem (_ [Bot Pistol Frenzy])           [ tmp_mode = 18; showmenu botskill ]
menuitem (_ [Bot Last Swiss Standing])     [ tmp_mode = 19; showmenu botskill ]
menuitem (_ [Bot Team Survivor])           [ tmp_mode = 20; showmenu botskill ]
menuitem (_ [Bot Team One Shot, One Kill]) [ tmp_mode = 21; showmenu botskill ]
menuitem (_ [Bot Survival])                [ showmenu [Survival Mode] ]
menuitem [] -1
menuitem (_ [Bot settings...]) [ showmenu [Bot settings] ]
menuitem (_ [Empty Map]) emptymap

const showmenu_enemies [ if (isbotteammode $tmp_mode) [ showmenu [team enemies] ] [ showmenu enemies ] ]

newmenu botskill

menuitem (_ Best)           [ tmp_botskill = best; showmenu_enemies ]
menuitem (_ "Good (default)") [ tmp_botskill = good; showmenu_enemies ]
menuitem (_ Medium)         [ tmp_botskill = medium; showmenu_enemies ]
menuitem (_ Worse)          [ tmp_botskill = worse; showmenu_enemies ]
menuitem (_ Bad)            [ tmp_botskill = bad; showmenu_enemies ]

const genenemyitems [
  loop i (listlen $arg1) [
    numplayers = (at $arg1 $i)
    if (= $arg2 0) [ // DM
      -= numplayers 1 // don't count yourself as enemy
      menutext = (concat (+ $numplayers 1) players)
    ] [ // TDM
      teamsize = (div $numplayers 2)
      menutext = (concatword $teamsize vs $teamsize)
    ]
    menuitem $menutext (concat [tmp_numbots =] $numplayers [; showmenu botmaps])
  ]
]

newmenu enemies
genenemyitems [2 4 8 12 16 24 32] 0

newmenu [team enemies]
genenemyitems [4 8 12 16 24 32] 1

const survSetsList [survNumBots survBotsToAdd timeBetChkPts timeUntilTele survMaxBots]
const survDifList ["6 1 60000 90000 20" "8 2 45000 60000 30" "10 3 30000 45000 40" "12 5 10000 20000 50"]
const survSetDif [ if (checkrange $arg1 0 3) [ survDifficulty = $arg1; loop sdl 5 [ (at $survSetsList $sdl) = (at (at $survDifList $survDifficulty) $sdl) ] ] [ sleep 1000 [ echo (c 3)Error: (c 5)invalid difficulty, defaulting to (genSurvDifStr 0) ] 1; survSetDif 0 ]; nonSurvDefaults = 0 ]

newmenu [Survival Mode]
menuitemslider (_ [Map: ]) 0 (- (listlen $defaultmaps) 1) "$survMap" 1 [ac_aqueous ac_arabian ac_arctic ac_arid ac_avenue ac_cavern ac_complex ac_depot ac_desert ac_desert2 ac_desert3 ac_douze ac_edifice ac_elevation ac_gothic ac_iceroad ac_industrial ac_ingress ac_keller ac_lainio ac_mines ac_outpost ac_power ac_rattrap ac_scaffold ac_shine ac_snow ac_stellar ac_sunset ac_swamp ac_terros ac_toxic ac_urban ac_venison ac_wasteland ac_werk] [ survMap = $arg1 ]
menuitemslider (_ [Difficulty: ]) 0 3 "$survDifficulty" 1 [easy intermediate hard impossible] [ survSetDif $arg1 ]
menuitem [] -1
menuitem (concatword [                       ] (_ Customize)) [ showmenu [Survival Mode - Customize] ]
menuitem (concatword [                         ] (_ Start!))  [ survival (at $defaultmaps $survMap) ]

newmenu [Survival Mode - Customize]
menuitemcheckbox (_ [Enable survival damage effects (uses gamespeed/gamma)]) "$survDamageEffects" [ survDamageEffects = $arg1 ]
menuitemslider (_ [# of bots to spawn on start: ]) 6 20 "$survNumBots" 1 []         [ survNumBots = $arg1; nonSurvDefaults = 1 ]
menuitemslider (_ [# of bots to spawn at checkpoints: ]) 1 5 "$survBotsToAdd" 1 []  [ survBotsToAdd = $arg1; nonSurvDefaults = 1 ]
menuitemslider (_ [# of milliseconds between checkpoints: ]) 10000 120000 "$timeBetChkPts" 10000 [] [ timeBetChkPts = $arg1; nonSurvDefaults = 1 ]
menuitemslider (_ [# of milliseconds before teleporting: ]) 10000 180000 "$timeUntilTele" 10000 []  [ timeUntilTele = $arg1; nonSurvDefaults = 1 ]
menuitemslider (_ [Maximum # of bots in survival mode: ]) 10 50 "$survMaxBots" 2 [] [ survMaxBots = $arg1; nonSurvDefaults = 1 ]
menuitemslider (_ [Spam survival mode messages: ]) 0 1 "$spamSurvMsgs" 1 [ no yes ] [ spamSurvMsgs = $arg1 ]
menuitem [] -1
menuitem [                                   Back] [ closemenu [Survival Mode - Customize] ]

newmenu [Bot settings]
menuitemslider (_ [Bot match length in minutes: ]) 1 60 "$menuBotGameLength" 1 [] [ menuBotGameLength = $arg1 ]
menuitemcheckbox (_ [Idle bots?]) "(idlebots 2)" [ idlebots $arg1 ]
menuitemcheckbox (_ [Bots attack?]) "(botsshoot 2)" [ botsshoot $arg1 ]

newmenu [Creating bot waypoints]

menuitem (_ [Waypoints...])             [ showmenu Waypoints ]
menuitem (_ [Paths...])                 [ showmenu Paths ]
menuitem (_ [Other operations...])      [ showmenu [Other bot operations] ]
menuitem [] -1
menuitem (_ [Flood map with waypoints]) wpflood
menuitem (_ [Save bot waypoints])       wpsave

newmenu Waypoints

menuitem (_ [Add a waypoint])               [ addwp 0 ]
menuitem (_ [Add auto-connecting waypoint]) [ addwp 1 ]
menuitem [] -1
menuitem (_ [Delete nearest waypoint])      delwp
menuitem (_ [Delete all waypoints])         [ showmenu [Delete all waypoints] ]

newmenu Paths

menuitem (_ [Create Start of 2-way path]) addpath2way1
menuitem (_ [Create End of 2-way path])   addpath2way2
menuitem (_ [Delete Start of 2-way path]) delpath2way1
menuitem (_ [Delete End of 2-way path])   delpath2way2
menuitem [] -1
menuitem (_ [Create Start of 1-way path]) addpath1way1
menuitem (_ [Create End of 1-way path])   addpath1way2
menuitem (_ [Delete Start of 1-way path]) delpath1way1
menuitem (_ [Delete End of 1-way path])   delpath1way2

newmenu [Other bot operations]

menuitem (_ [Set waypoint as jumpable])     setjumpwp
menuitem (_ [Unset waypoint as jumpable])   unsetjumpwp
menuitem (_ "Set waypoint direction (yaw)") setwpyaw
menuitem [] -1
menuitemslider (_ [Waypoint visibility: ]) 0 1 [ ] 1 ["Hide waypoints" "Show waypoints"] [ wpvisible $arg1 ]
menuitemslider (_ [Select/place waypoints by: ]) 0 1 "($xhairwpsel)" 1 ["Your current location" "Clicking a point on the grid"] [ xhairwpsel $arg1 ]

newmenu [Delete all waypoints]
menuinit [ delallwps = 0 ] // Ensures this menu works correctly.
menuitemcheckbox (_ [Yes, delete all waypoints: ]) [delallwps = 1] [delallwps = $arg1]
menuitem (concatword (c 2) [   	   ] (_ Apply!)) [ if $delallwps wpclear [ showmenu [] ] ]

persistidents 1
