// bot menus and tools

newmenu botmaps

const genbotmaps [
  looplist $arg1 listmapname [
    menuitemmapload $listmapname (concat "tmp_botmap =" $listmapname "; startbotmap")
  ]
]
// problems with quite a number of maps when playing against bots (waypoints simply flooded) have lead to generating lists to avoid these
//known issues on: (BTDM) ac_alcove ac_aqueous ac_avenue ac_coal ac_depot ac_kazemat ac_keller ac_outpost ac_power ac_shine ac_terros ac_urban
const __bot_maps_all [ac_africa ac_arabian ac_arctic ac_arctic2 ac_arid ac_complex ac_desert ac_desert2 ac_desert3 ac_douze ac_dusk ac_edifice ac_elevation ac_gothic ac_industrial ac_ingress ac_lainio ac_lotus ac_mines ac_nocturne ac_origin ac_rampart ac_rattrap ac_scaffold ac_snow ac_stellar ac_sunset ac_swamp ac_toxic ac_venison ac_wasteland ac_werk]
genbotmaps $__bot_maps_all

tempalias tmp_botskill good
tempalias tmp_numbots 1
tempalias tmp_mode ""
tempalias tmp_botmap ""
tempalias tmp_isbotteammode 0

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

const preparebotmatch [
  echo starting botmatch..
  tempalias 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
  ]
]

////  Main > Singleplayer  ////

newmenu singleplayer

// teamplay not mature yet
menuitem "Bot Team Deathmatch"         [ tmp_mode = 7; tmp_isbotteammode = 1; showmenu botskill ]
menuitem "Bot Deathmatch"              [ tmp_mode = 8; tmp_isbotteammode = 0; showmenu botskill ]
menuitem "Bot One Shot, One Kill"      [ tmp_mode = 12; tmp_isbotteammode = 0; showmenu botskill ]
menuitem "Bot Pistol Frenzy"           [ tmp_mode = 18; tmp_isbotteammode = 0; showmenu botskill ]
menuitem "Bot Last Swiss Standing"     [ tmp_mode = 19; tmp_isbotteammode = 0; showmenu botskill ]
menuitem "Bot Team Survivor"           [ tmp_mode = 20; tmp_isbotteammode = 1; showmenu botskill ]
menuitem "Bot Team One Shot, One Kill" [ tmp_mode = 21; tmp_isbotteammode = 1; showmenu botskill ]
menuitem "Bot Survival"                [ load_survival ; showmenu [Survival Mode] ]
menuitem "" -1
menuitem "Bot settings..." [ showmenu [Bot settings] ]
menuitem "" -1
menuitem "Empty map" emptymap
menuitem "Search map..." [ showmenu [search map] ]

searchmap = ""

newmenu [search official map]
menudirlist "packages/maps/official" cgz [map $arg1] 0 searchmap

newmenu [search custom map]
menurenderoffset -20 0
menudirlist "packages/maps" cgz [map $arg1] 0 searchmap

newmenu [search map]
menuitemtextinput "Part of map name or description: " "result $searchmap" [ searchmap = $arg1 ]
menuitem "\f2\t       [ Search in official maps ]" [ showmenu [search official map] ]
menuitem "\f2\t       [ Search in custom maps ]"   [ showmenu [search custom map] ]

const showmenu_enemies [ if ($tmp_isbotteammode) [ 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

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 [ ] ["Hide waypoints" "Show waypoints"] [ wpvisible $arg1 ]
menuitemslider "Select/place waypoints by: " 0 1 "($xhairwpsel)" ["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 "\f2\t\t     Apply!" [ if $delallwps wpclear [ showmenu [] ] ]

