//      cb.gib
//
//      CustomTF class builder for GIB in QuakeForge 0.5
//
//      Copyright (C) 2002  Brian Koropoff
//
//      This program is free software; you can redistribute it and/or
//      modify it under the terms of the GNU General Public License
//      as published by the Free Software Foundation; either version 2
//      of the License, or (at your option) any later version.
//
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
//      See the GNU General Public License for more details.
//
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to:
//
//              Free Software Foundation, Inc.
//              59 Temple Place - Suite 330
//              Boston, MA  02111-1307, USA
//
// $Id$

// Instructions:
// 
// First, you'll need to load a menu for whatever server you are on,
// otherwise it won't know what items are available and where to find
// them on the custom menu.  To use the provided prozac.menu, you would
// type:
// custom.load.menu prozac
//
// To load a class, get to the first page of the custom menu and use
// custom.load.class to load up a class file.  To use the provided
// warlock.class, you would type:
// custom.load.class warlock
//
// To record a class, again get to the first page of the custom menu.
// Now type custom.record at the menu.  This will rebind all your
// number keys to keep track of what selections you have made.
// If you decide to restart building your class, you will have to
// type custom.record again.  After you have built your class,
// press 0 as usual.  Instead of spawning, you will get a message
// that recording has finished.  To save your class with the name
// "engy" you would then type:
// custom.record.save engy
// You can now use custom.load.class to reload your class at any
// time.

global custom

custom.load.menu_f = {
	custom.menu.${custom.menu.size}.name = $1
	custom.menu.${custom.menu.size}.size = 0
	function "__menu" $2
	__menu
	custom.menu.size = (${custom.menu.size} + 1)
}
	
custom.load.item_f = {
	local i
	local n
	i = ${custom.menu.size}
	n = ${custom.menu.$i.size}
	custom.menu.$i.$n = $1
	custom.item.$1 = 1
	custom.item.$1.menu = $i
	custom.item.$1.number = $n
	custom.menu.$i.size = ($n + 1)
}

custom.load.buy_f = {
	local i
	local n
	ifnot ${custom.item.$1}	return
	i = ${custom.item.$1.menu}
	n = ${custom.item.$1.number}
	custom.menu.seek $i
	impulse ($n+1)
	wait; wait; wait
}
	

function custom.load.menu {
	local oldm
	local oldi
	oldm = `function.get menu`
	oldi = `function.get item`
	function "menu" ${custom.load.menu_f}
	function "item" ${custom.load.item_f}
	custom.menu.size = 0
	exec "custom/", $1, ".menu"
	function "menu" $oldm
	function "item" $oldi
}

function custom.load.class {
	local oldb
	oldb = `function.get buy`
	custom.menu.cur = 0
	function "buy" ${custom.load.buy_f}
	exec "custom/", $1, ".class"
	function "buy" $oldb
}

function custom.menu.seek {
	local fw
	fw = (${custom.menu.cur} < $1)
	while (${custom.menu.cur} != $1) {
		if $fw {
			custom.menu.cur = (${custom.menu.cur} + 1)
			impulse 9
	 		wait; wait;
		} else {
			custom.menu.cur = (${custom.menu.cur} - 1)
			impulse 8
	 		wait; wait;
		}
	}
}

function custom.record.item {
	local m
	m = ${custom.menu.cur}
	ifnot ($1 < ${custom.menu.$m.size}) return
	local line
	line = "buy \"", ${custom.menu.$m.$1}, "\""
	custom.recording = ${custom.recording}, "\n", $line
	impulse ($1+1)
}

function custom.record.seek {
	local new
	new = (${custom.menu.cur} + $1)
	if ($new < 0 || $new >= ${custom.menu.size}) return
	custom.menu.cur = $new
	if ($1 < 0) {
		impulse 8
	} else {
		impulse 9
	}
}

function custom.record.stop {
	bind "0" "impulse 10"
	for i in `range 1 9` {
		bind $i "impulse ", $i
	}
	echo "CustomBuild-> Recording finished"
	custom.recording = "// Script recorded by CustomTF class builder",${custom.recording}
}

function custom.record {
	custom.menu.cur = 0
	custom.recording = ""
	bind "0" "custom.record.stop"
	bind "8" "custom.record.seek -1"
	bind "9" "custom.record.seek 1"
	for i in `range 1 7` {
		bind $i "custom.record.item ",($i-1)
	}
	echo "CustomBuild-> Recording class.  Please build a class as usual."
	custom
}

function custom.record.save {
	local file
	file = "custom/", $1, ".class"
	echo "Saving class script to ", $file, "..."
	file.write $file ${custom.recording}
}

export custom.load.menu
export custom.load.class
export custom.record.item
export custom.record.seek
export custom.record.stop
export custom.record
export custom.record.save