// bot menus and tools

newmenu botmaps

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

alias tmp_botskill good
alias tmp_numbots 1
alias tmp_mode
alias tmp_botmap

// add bots
alias startbotmatch [

	if (= $tmp_mode 7) [ // TDM
	    alias numrvsf (- (div $tmp_numbots 2) (curteam))
	    alias numcla (- (div $tmp_numbots 2) (= (curteam) 0))
        addnbot $numrvsf RVSF $tmp_botskill
	    addnbot $numcla CLA $tmp_botskill
	] [ // DM
	    addnbot $tmp_numbots
	]
]

alias preparebotmatch [
    echo starting botmatch..
    
    // 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
]

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

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


alias emptymap [
    afterdisconnect [
	    mode 0
	    showmenu maps 
    ]
]

newmenu singleplayer

// teamplay not mature yet
menuitem "Bot Team Deathmatch"				"alias tmp_mode 7; showmenu botskill"
menuitem "Bot Deathmatch"					"alias tmp_mode 8; showmenu botskill"
menuitem "Bot One Shot, One Kill"           "alias tmp_mode 12; showmenu botskill"
menuitem "Empty Map"                        "emptymap"

newmenu botskill

alias showmenu_enemies [ if (= $tmp_mode 7) [ showmenu "team enemies" ] [ showmenu enemies ] ]
menuitem "Best" "alias tmp_botskill best; showmenu_enemies"
menuitem "Good (default)" "alias tmp_botskill good; showmenu_enemies"
menuitem "Medium" "alias tmp_botskill medium; showmenu_enemies"
menuitem "Worse" "alias tmp_botskill worse; showmenu_enemies"
menuitem "Bad" "alias tmp_botskill bad; showmenu_enemies"


alias genenemyitems [
	loop i (listlen $arg1) [
		alias numplayers (at $arg1 $i)
		if (= $arg2 0) [ // DM
			alias numplayers (- $numplayers 1) // don't count yourself as enemy
			alias menutext (concat (+ $numplayers 1) players)
		] [ // TDM
			alias teamsize (div $numplayers 2)
			alias menutext (concatword $teamsize vs $teamsize)
		]
		menuitem $menutext (concat "alias 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 "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 [ alias delallwps 0 ] // Ensures this menu works correctly.
menuitemcheckbox "Yes, delete all waypoints: " "alias delallwps 1" "alias delallwps $arg1" 
menuitem "2   	   [ Apply! ]" [ if $delallwps [ wpclear ] [ showmenu "" ] ] 
