#############################################################################
#
#                                 EQ-tools.tcl
#                             -------------------                            
#    begin                : Fri Sept 14 2001
#    copyright            : Copyright (C) 2001 by Martijn Boerwinkel                   
#    email                : shug@xim.f2s.com
#    description          : public tools for Empirequest                     
#
#    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 the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    $Id: EQ-tools.tcl,v 1.12 2001/11/24 14:52:17 martijn Exp $
#    
#############################################################################

###################### BEGIN editting parameters here #######################

# EQ_tools(cmd_char) is what character should be used to define public 
# commands with. The default is "!". Make sure this doesn't conflict with
# other bots.
set EQ_tools(cmd_char) "!"

# EQ_tools(reponse) determines the way there should be responded to public
# commands. "1" for privmsg to channel, "2" for privmsg to nick, "3" for 
# notice to nick.
set EQ_tools(reponse) "3"

# EQ_tools(queue) determines which queue should be used for output.
# "1" for putquick, "2" for putserv, "3" for puthelp.
set EQ_tools(queue) "1"

# EQ_tools(ignore_chans) is a list of channels to ignore commands in. 
# Enter chans in lower case. Separate multiple channels with a space. 
set EQ_tools(ignore_chans) ""

# To prevent double replies list all bots who should reply instead of 
# this bot here.
set EQ_tools(other_bots) ""

# locations of files containing ship and defence info
set EQ_tools(ship_file) "scripts/ships.info"
set EQ_tools(odg_file) "scripts/odg.info"

# location of Mpexpr library
set EQ_tools(Mpexpr) "scripts/libMpexpr10.so"

# precision for Mpexpr library
set EQ_tools(precision) "25"

# Maximum length for integer type parameters
set EQ_tools(max_int) "1000000000"
####################### END editting parameters here ########################

set EQ_tools(version) "1.01"

load $EQ_tools(Mpexpr)
set mp_precision $EQ_tools(precision)

# creating ship and odg data from info files
proc EQ_tools:data {} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_class EQ_ship_tclass EQ_ship_armour EQ_ship_fireorder EQ_ship_cost_t EQ_ship_cost_k EQ_ship_tt EQ_ship_guns EQ_ship_damage EQ_ship_accuracy EQ_ship_agility EQ_ship_facility EQ_ship_cloak EQ_ship_special EQ_ship_desc EQ_odg EQ_odg_name EQ_odg_class EQ_odg_tclass EQ_odg_armour EQ_odg_fireorder EQ_odg_cost_t EQ_odg_cost_k EQ_odg_guns EQ_odg_damage EQ_odg_accuracy EQ_odg_facility EQ_odg_special EQ_odg_desc

	if { ![file exists $EQ_tools(ship_file)] } { 
		die "Can't find file containing ship info" 
	}
	if { [info exists EQ_ships] } {
		unset EQ_ships
	}
	set sf [open $EQ_tools(ship_file) r]
	gets $sf line
	while { ![eof $sf] } {
		if { [regexp {^\[([A-Z]*)\]} $line null abbrev] } {
			lappend EQ_ships $abbrev
			gets $sf line
			if { [regexp {^name=(.*)\s*$} $line null name] } { 
				set EQ_ship_name($abbrev) $name
				gets $sf line
			} else { die "name info not found for $abbrev type of ship" }
			if { [regexp {^class=(.*)\s*$} $line null class] } {
				set EQ_ship_class($abbrev) $class
				gets $sf line
			} else { die "class info not found for $abbrev type of ship" }
			if { [regexp {^tclass=(.*)\s*$} $line null tclass] } {
				set EQ_ship_tclass($abbrev) $tclass
				gets $sf line
			} else { die "tclass info not found for $abbrev type of ship" }
			if { [regexp {^armour=(.*)\s*$} $line null armour] } {
				set EQ_ship_armour($abbrev) $armour
				gets $sf line
			} else { die "armour info not found for $abbrev type of ship" }
			if { [regexp {^fireorder=(.*)\s*$} $line null fireorder] } {
				set EQ_ship_fireorder($abbrev) $fireorder
				gets $sf line
			} else { die "fireorder info not found for $abbrev type of ship" }
			if { [regexp {^t=(.*)\s*$} $line null t] } {
				set EQ_ship_cost_t($abbrev) $t
				gets $sf line
			} else { die "t info not found for $abbrev type of ship" }
			if { [regexp {^k=(.*)\s*$} $line null k] } {
				set EQ_ship_cost_k($abbrev) $k
				gets $sf line
			} else { die "k info not found for $abbrev type of ship" }
			if { [regexp {^traveltime=(.*)\s*$} $line null traveltime] } {
				set EQ_ship_tt($abbrev) $traveltime
				gets $sf line
			} else { die "traveltime info not found for $abbrev type of ship" }
			if { [regexp {^guns=(.*)\s*$} $line null guns] } {
				set EQ_ship_guns($abbrev) $guns
				gets $sf line
			} else { die "guns info not found for $abbrev type of ship" }
			if { [regexp {^damage=(.*)\s*$} $line null damage] } {
				set EQ_ship_damage($abbrev) $damage
				gets $sf line
			} else { die "damage info not found for $abbrev type of ship" }
			if { [regexp {^accuracy=(.*)\s*$} $line null accuracy] } {
				set EQ_ship_accuracy($abbrev) $accuracy
				gets $sf line
			} else { die "accuracy info not found for $abbrev type of ship" }
			if { [regexp {^agility=(.*)\s*$} $line null agility] } {
				set EQ_ship_agility($abbrev) $agility
				gets $sf line
			} else { die "agility info not found for $abbrev type of ship" }
			if { [regexp {^facility=(.*)\s*$} $line null facility] } {
				set EQ_ship_facility($abbrev) $facility
				gets $sf line
			} else { die "facility info not found for $abbrev type of ship" }
			if { [regexp {^cloak=(.*)\s*$} $line null cloak] } {
				set EQ_ship_cloak($abbrev) $cloak
				gets $sf line
			} else { die "cloak info not found for $abbrev type of ship" }
			if { [regexp {^special=(.*)\s*$} $line null special] } {
				set EQ_ship_special($abbrev) $special
				gets $sf line
			} else { die "special info not found for $abbrev type of ship" }
			if { [regexp {^desc=(.*)\s*$} $line null desc] } {
				set EQ_ship_desc($abbrev) $desc
			} else { die "desc info not found for $abbrev type of ship" }
		}	
		gets $sf line
	}
	close $sf
	
	if { ![file exists $EQ_tools(odg_file)] } { 
		die "Can't find file containing odg info" 
	}
	if { [info exists EQ_odg] } {
		unset EQ_odg
	}
	set of [open $EQ_tools(odg_file) r]
	gets $of line
	while { ![eof $of] } {
		if { [regexp {^\[([A-Z]*)\]} $line null abbrev] } {
			lappend EQ_odg $abbrev
			gets $of line
			if { [regexp {^name=(.*)\s*$} $line null name] } { 
				set EQ_odg_name($abbrev) $name
				gets $of line
			} else { die "name info not found for $abbrev type of odg" }
			if { [regexp {^class=(.*)\s*$} $line null class] } {
				set EQ_odg_class($abbrev) $class
				gets $of line
			} else { die "class info not found for $abbrev type of odg" }
			if { [regexp {^tclass=(.*)\s*$} $line null tclass] } {
				set EQ_odg_tclass($abbrev) $tclass
				gets $of line
			} else { die "tclass info not found for $abbrev type of odg" }
			if { [regexp {^armour=(.*)\s*$} $line null armour] } {
				set EQ_odg_armour($abbrev) $armour
				gets $of line
			} else { die "armour info not found for $abbrev type of odg" }
			if { [regexp {^fireorder=(.*)\s*$} $line null fireorder] } {
				set EQ_odg_fireorder($abbrev) $fireorder
				gets $of line
			} else { die "fireorder info not found for $abbrev type of odg" }
			if { [regexp {^t=(.*)\s*$} $line null t] } {
				set EQ_odg_cost_t($abbrev) $t
				gets $of line
			} else { die "t info not found for $abbrev type of odg" }
			if { [regexp {^k=(.*)\s*$} $line null k] } {
				set EQ_odg_cost_k($abbrev) $k
				gets $of line
			} else { die "k info not found for $abbrev type of odg" }
			if { [regexp {^guns=(.*)\s*$} $line null guns] } {
				set EQ_odg_guns($abbrev) $guns
				gets $of line
			} else { die "guns info not found for $abbrev type of odg" }
			if { [regexp {^damage=(.*)\s*$} $line null damage] } {
				set EQ_odg_damage($abbrev) $damage
				gets $of line
			} else { die "damage info not found for $abbrev type of odg" }
			if { [regexp {^accuracy=(.*)\s*$} $line null accuracy] } {
				set EQ_odg_accuracy($abbrev) $accuracy
				gets $of line
			} else { die "accuracy info not found for $abbrev type of odg" }
			if { [regexp {^facility=(.*)\s*$} $line null facility] } {
				set EQ_odg_facility($abbrev) $facility
				gets $of line
			} else { die "facility info not found for $abbrev type of odg" }
			if { [regexp {^special=(.*)\s*$} $line null special] } {
				set EQ_odg_special($abbrev) $special
				gets $of line
			} else { die "special info not found for $abbrev type of odg" }
			if { [regexp {^desc=(.*)\s*$} $line null desc] } {
				set EQ_odg_desc($abbrev) $desc
			} else { die "desc info not found for $abbrev type of odg" }
		}	
		gets $of line
	}
	close $of
	return 0
}
EQ_tools:data 

# creating some general purpose procedures used throughout the script
proc EQ_tools:cmd_output {nick chan arg} {
	global EQ_tools
	if { $EQ_tools(reponse) == "1" } {
		set msg "PRIVMSG $chan :$arg"
	} elseif { $EQ_tools(reponse) == "2" } {
		set msg "PRIVMSG $nick :$arg"
	} elseif { $EQ_tools(reponse) == "3" } {
		set msg "NOTICE $nick :$arg"
	}
	if { $EQ_tools(queue) == "1" } {
		putquick $msg
	} elseif { $EQ_tools(queue) == "2" } {
		putserv $msg
	} elseif { $EQ_tools(queue) == "3" } {
		puthelp $msg
	}
	return 0
}

proc EQ_tools:isnumber {arg} {
	if {[regexp {^([0-9]+)$} $arg]} {
		return 1
	} else {
		return 0
	}
	return 0
}

proc EQ_tools:number_format {number} {
	if { [mpexpr "$number >= 1000"] } {
		set headval [mpexpr "int($number / 1000)"]
    		set head [EQ_tools:number_format $headval]
    		set tail [mpexpr "$number - ($headval * 1000)"]
    		set tail [format {%03d} $tail]
		return "$head,$tail"
	}
	return $number
}

proc EQ_tools:de_number_format {number} {
	regsub -all {,} $number "" number
	return $number
}

#creating help stuff
bind pub - [string trim $EQ_tools(cmd_char)]help pub:help

proc pub:help {nick uhost handle chan arg} {
	global EQ_tools
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0"} { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}	
	set args [split $arg]
	set type [string tolower [lindex $args 0]]
	EQ_tools:help $nick $chan $type
	return 0
}

proc EQ_tools:help {nick chan type} {
	global EQ_tools
	if { [regexp -nocase {approx} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)APPROX\002 will calculate the amount of ships or ODG you can build given the amount of tellerium and krypton"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)APPROX \037shiptype\037 \037tellerium\037 \037krypton\037\002"
		EQ_tools:cmd_output $nick $chan"\002Example\002: $EQ_tools(cmd_char)APPROX af 10000 10000"
	} elseif { [regexp -nocase {class} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)CLASS\002 will return a list of ship and ODG types of the given class"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)CLASS\002 \037class\037"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)CLASS battleship"
	} elseif { [regexp -nocase {fireorder} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)FIREORDER\002 will return a list of ship and ODG types sorted by firing order"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)FIREORDER\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)FIREORDER"
	} elseif { [regexp -nocase {legend} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)LEGEND\002 will return a list of possible ship and ODG types"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)LEGEND\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)LEGEND"
	} elseif { [regexp -nocase {info} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)INFO\002 will return various info about the given ship or ODG type"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)INFO\002 \037shiptype\037"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)INFO afrig"
	} elseif { [regexp -nocase {mines} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)MINES\002 will determine how much it will cost to build a certain number of mines given the number of mines already present"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)MINES \037mines\037 \037ordered\037\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)MINES 100 10"
	} elseif { [regexp -nocase {probes} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)PROBES\002 will determine how much it will cost to build a certain number of probes given the number of probes already present"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)PROBES \037probes\037 \037ordered\037\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)PROBES 100 10"
	} elseif { [regexp -nocase {score} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)SCORE\002 will determine how much it will cost to build a certain number of ships"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)SCORE \037shiptype\037 \037ordered\037\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)SCORE m 100"
	} elseif { [regexp -nocase {traveltime} $type] } {
		EQ_tools:cmd_output $nick $chan "\002$EQ_tools(cmd_char)TRAVELTIME\002 will return a list of ship types sorted by travel time"
		EQ_tools:cmd_output $nick $chan "\002Syntax\002: \002$EQ_tools(cmd_char)TRAVELTIME\002"
		EQ_tools:cmd_output $nick $chan "\002Example\002: $EQ_tools(cmd_char)TRAVELTIME"
	} else  {
		EQ_tools:cmd_output $nick $chan "\002Commands available\002: $EQ_tools(cmd_char)approx, $EQ_tools(cmd_char)class, $EQ_tools(cmd_char)fireorder, $EQ_tools(cmd_char)help, $EQ_tools(cmd_char)info, $EQ_tools(cmd_char)legend, $EQ_tools(cmd_char)mines, $EQ_tools(cmd_char)probes, $EQ_tools(cmd_char)score, $EQ_tools(cmd_char)traveltime"
		EQ_tools:cmd_output $nick $chan "Use \002$EQ_tools(cmd_char)help \037command\037\002 for help on specific command"	
	}	
	EQ_tools:cmd_output $nick $chan "Copyright (C) \002XiM\002 (shug@xim.f2s.com). Version \002$EQ_tools(version)\002 (http://www.xim.f2s.com). "	
	return 0
}
	
# creating the score command
bind pub - [string trim $EQ_tools(cmd_char)]score pub:score

proc pub:score {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_cost_t EQ_ship_cost_k EQ_odg EQ_odg_name EQ_odg EQ_odg_cost_t EQ_odg_cost_k
	set args [split $arg]
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0" } { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [llength $args] < 2 } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Not enough parameters (try \002!help score\002 for help)"
		return 0
	}
	set type [string toupper [lindex $args 0]]
	set amount [EQ_tools:de_number_format [lindex $args 1]]
	if { [EQ_tools:isnumber $amount] == "0" || [mpexpr "$amount > $EQ_tools(max_int)"] == "1"} {
		EQ_tools:cmd_output $nick $chan "\002Error\002: The amount of ships/odg you entered is either not a number or too big (max: [EQ_tools:number_format $EQ_tools(max_int)])"
		return 0
	} 
	if { [lsearch -exact $EQ_ships $type] != "-1" } {
		set cost_t [mpexpr "$amount * $EQ_ship_cost_t($type)"]
		set cost_k [mpexpr "$amount * $EQ_ship_cost_k($type)"]
		set s_type "$EQ_ship_name($type)"
		set score [mpexpr "($cost_t+$cost_k)/10"]
		
	} elseif { [lsearch -exact $EQ_odg $type] != "-1" } {
		set cost_t [mpexpr "$amount * $EQ_odg_cost_t($type)"]
		set cost_k [mpexpr "$amount * $EQ_odg_cost_k($type)"]
		set s_type "$EQ_odg_name($type)"
		set score [mpexpr "($cost_t+$cost_k)/10"]
	} else {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Ship or ODG type not recognized. Use \002!legend\002 to see the list of possible ship and ODG types"
		return 0
	}
	EQ_tools:cmd_output $nick $chan "\002[EQ_tools:number_format $amount]\002 $s_type\(s) will cost \002[EQ_tools:number_format $cost_t]\002 Tellerium and \002[EQ_tools:number_format $cost_k]\002 Krypton (Score: \002[EQ_tools:number_format $score]\002)"
	return 0
}

# creating mines  and probes commands
bind pub - [string trim $EQ_tools(cmd_char)]mines pub:mines
proc pub:mines {nick uhost handle chan arg} {
	global EQ_tools
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0"} { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [llength [split $arg]] < 2 } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Not enough parameters (try \002!help mines\002 for help)"
		return 0
	}
	EQ_tools:resources $nick $chan [EQ_tools:de_number_format [lindex $arg 0]] [EQ_tools:de_number_format [lindex $arg 1]] mines
}

bind pub - [string trim $EQ_tools(cmd_char)]probes pub:probes
proc pub:probes {nick uhost handle chan arg} {
	global EQ_tools
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0"} { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [llength [split $arg]] < 2 } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Not enough parameters (try \002!help mines\002 for help)"
		return 0
	}
	EQ_tools:resources $nick $chan [EQ_tools:de_number_format [lindex $arg 0]] [EQ_tools:de_number_format [lindex $arg 1]] probes
}

proc EQ_tools:resources {nick chan present build type} {
	global EQ_tools EQ_resource_cost
	if {[EQ_tools:isnumber $present] == "0" || [EQ_tools:isnumber $build] == "0" || [mpexpr "$present > $EQ_tools(max_int)"] == "1" || [mpexpr "$build > $EQ_tools(max_int)"] == "1"} {
		EQ_tools:cmd_output $nick $chan "\002Error\002: The amount of $type you entered is either not a number or too big (max: [EQ_tools:number_format $EQ_tools(max_int)])"
	} else {
		if { $type == "mines" } {
			set t $EQ_resource_cost(high)
			set k $EQ_resource_cost(low)
		} elseif { $type == "probes" } {
			set t $EQ_resource_cost(low)
			set k $EQ_resource_cost(high)
		}
		set cost_t [mpexpr "((($present*2)+1+$build)*$t*($build)/4)"]
		set cost_k [mpexpr "((($present*2)+1+$build)*$k*($build)/4)"]
		EQ_tools:cmd_output $nick $chan "Ordering \002[EQ_tools:number_format $build]\002 $type with \002[EQ_tools:number_format $present]\002 already present would cost \002[EQ_tools:number_format $cost_t]\002 Tellerium and \002[EQ_tools:number_format $cost_k]\002 Krypton"
	} 
	return 0
}

bind pub - [string trim $EQ_tools(cmd_char)]legend pub:legend

proc pub:legend {nick uhost handle chan args} {
	global EQ_tools EQ_ships EQ_ship_name EQ_odg EQ_odg_name
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0"} { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	set i 1
	EQ_tools:cmd_output $nick $chan "\002Ships\002: "
	foreach ship [lsort $EQ_ships] {
		EQ_tools:cmd_output $nick $chan "$i: $EQ_ship_name($ship) \[\002$ship\002\]"
		incr i
	}
	EQ_tools:cmd_output $nick $chan "\002ODG\002: "
	set i 1
	foreach odg [lsort $EQ_odg] { 
		EQ_tools:cmd_output $nick $chan "$i: $EQ_odg_name($odg) \[\002$odg\002\]"
		incr i
	}
	return 0
}

bind pub - [string trim $EQ_tools(cmd_char)]approx pub:approx

proc pub:approx {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_cost_t EQ_ship_cost_k EQ_odg EQ_odg_name EQ_odg_cost_t EQ_odg_cost_k
	set args [split $arg]
	set t [EQ_tools:de_number_format [lindex $args 1]]
	set k [EQ_tools:de_number_format [lindex $args 2]]
	set type [string toupper [lindex $args 0]]
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0"} { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [EQ_tools:isnumber $t] == "0" || [EQ_tools:isnumber $k] == "0" || [mpexpr "$t > $EQ_tools(max_int)"] == "1" || [mpexpr "$k > $EQ_tools(max_int)"] == "1" } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: The amount of T or K is either not a number or too big (max: [EQ_tools:number_format $EQ_tools(max_int)])"
		return 0
	} 
	if { [lsearch -exact $EQ_ships $type] != "-1" } {
		if { $EQ_ship_cost_t($type) != "0" } { set t_max [mpexpr "$t / $EQ_ship_cost_t($type)"] } else { set t_max "-1" }
		if { $EQ_ship_cost_k($type) != "0" } { set k_max [mpexpr "$k / $EQ_ship_cost_k($type)"] } else { set k_max "-1" }
		set s_type "$EQ_ship_name($type)"
		if { $t_max == "-1" } { 
			set max $k_max 
		} elseif { $k_max == "-1" } {
			set max $t_max
		} else { 
			if { $t_max >= $k_max } { set max $k_max } else { set max $t_max } 
		}		
	} elseif { [lsearch -exact $EQ_odg $type] != "-1" } {
		if { $EQ_odg_cost_t($type) != "0" } { set t_max [mpexpr "$t / $EQ_odg_cost_t($type)"] } else { set t_max "-1" }
		if { $EQ_odg_cost_k($type) != "0" } { set k_max [mpexpr "$k / $EQ_odg_cost_k($type)"] } else { set k_max "-1" }
		set s_type "$EQ_odg_name($type)"
		if { $t_max == "-1" } { 
			set max $k_max 
		} elseif { $k_max == "-1" } {
			set max $t_max
		} else { 
			if { $t_max >= $k_max } { set max $k_max } else { set max $t_max } 
		}
	} else {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Ship or ODG type not recognized. Use \002!legend\002 to see the list of possible ship and ODG types"
		return 0
	}
	EQ_tools:cmd_output $nick $chan "You can buy \002[EQ_tools:number_format $max]\002 $s_type\(s) with \002[EQ_tools:number_format $t]\002 Tellerium and \002[EQ_tools:number_format $k]\002 Krypton"
	pub:score $nick $uhost $handle $chan "$type $max"
	return 0	 
}	

# creating the info command
bind pub - [string trim $EQ_tools(cmd_char)]info pub:info

proc pub:info {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_class EQ_ship_tclass EQ_ship_armour EQ_ship_fireorder EQ_ship_cost_t EQ_ship_cost_k EQ_ship_tt EQ_ship_guns EQ_ship_damage EQ_ship_accuracy EQ_ship_agility EQ_ship_facility EQ_ship_cloak EQ_ship_special EQ_ship_desc EQ_odg EQ_odg_name EQ_odg_class EQ_odg_tclass EQ_odg_armour EQ_odg_fireorder EQ_odg_cost_t EQ_odg_cost_k EQ_odg_guns EQ_odg_damage EQ_odg_accuracy EQ_odg_facility EQ_odg_special EQ_odg_desc
	set args [split $arg]
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0" } { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [llength $args] < 1 } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Not enough parameters (try \002!help info\002 for help)"
		return 0
	}
	set type [string toupper [lindex $args 0]]
	if { [lsearch -exact $EQ_ships $type] != "-1" } {
		EQ_tools:cmd_output $nick $chan "General info for \002$EQ_ship_name($type)\002 \[\002$type\002\]:"
		EQ_tools:cmd_output $nick $chan "Class         : \002$EQ_ship_class($type)\002"	
		EQ_tools:cmd_output $nick $chan "Target Class  : \002$EQ_ship_tclass($type)\002"
		EQ_tools:cmd_output $nick $chan "Armour        : \002$EQ_ship_armour($type)\002"
		EQ_tools:cmd_output $nick $chan "Firing Order  : \002$EQ_ship_fireorder($type)\002"
		EQ_tools:cmd_output $nick $chan "Tellerium     : \002$EQ_ship_cost_t($type)\002"
		EQ_tools:cmd_output $nick $chan "Krypton       : \002$EQ_ship_cost_k($type)\002"
		EQ_tools:cmd_output $nick $chan "Travel Time   : \002$EQ_ship_tt($type)\002"
		EQ_tools:cmd_output $nick $chan "Guns          : \002$EQ_ship_guns($type)\002"
		EQ_tools:cmd_output $nick $chan "Damage per gun: \002$EQ_ship_damage($type)\002"
		EQ_tools:cmd_output $nick $chan "Accuracy      : \002$EQ_ship_accuracy($type)\002"
		EQ_tools:cmd_output $nick $chan "Agility       : \002$EQ_ship_agility($type)\002"
		EQ_tools:cmd_output $nick $chan "Facility      : \002$EQ_ship_facility($type)\002"
		EQ_tools:cmd_output $nick $chan "Cloaked       : \002$EQ_ship_cloak($type)\002"
		EQ_tools:cmd_output $nick $chan "Special       : \002$EQ_ship_special($type)\002"
		EQ_tools:cmd_output $nick $chan "Description   : \002$EQ_ship_desc($type)\002"
	} elseif { [lsearch -exact $EQ_odg $type] != "-1" } {
		EQ_tools:cmd_output $nick $chan "General info for \002$EQ_odg_name($type)\002 \[\002$type\002\]:"
		EQ_tools:cmd_output $nick $chan "Class         : \002$EQ_odg_class($type)\002"	
		EQ_tools:cmd_output $nick $chan "Target Class  : \002$EQ_odg_tclass($type)\002"
		EQ_tools:cmd_output $nick $chan "Armour        : \002$EQ_odg_armour($type)\002"
		EQ_tools:cmd_output $nick $chan "Firing Order  : \002$EQ_odg_fireorder($type)\002"
		EQ_tools:cmd_output $nick $chan "Tellerium     : \002$EQ_odg_cost_t($type)\002"
		EQ_tools:cmd_output $nick $chan "Krypton       : \002$EQ_odg_cost_k($type)\002"
		EQ_tools:cmd_output $nick $chan "Guns          : \002$EQ_odg_guns($type)\002"
		EQ_tools:cmd_output $nick $chan "Damage per gun: \002$EQ_odg_damage($type)\002"
		EQ_tools:cmd_output $nick $chan "Accuracy      : \002$EQ_odg_accuracy($type)\002"
		EQ_tools:cmd_output $nick $chan "Facility      : \002$EQ_odg_facility($type)\002"
		EQ_tools:cmd_output $nick $chan "Special       : \002$EQ_odg_special($type)\002"
		EQ_tools:cmd_output $nick $chan "Description   : \002$EQ_odg_desc($type)\002"
	} else {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Ship or ODG type not recognized. Use \002!legend\002 to see the list of possible ship and ODG types"
		return 0
	}
	return 0
}

# creating the class command
bind pub - [string trim $EQ_tools(cmd_char)]class pub:class

proc pub:class {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_class EQ_odg EQ_odg_name EQ_odg_class
	set args [split $arg]
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0" } { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	if { [llength $args] < 1 } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Not enough parameters (try \002!help class\002 for help)"
		return 0
	}
	set uclass [string toupper [lindex $args 0]]
	set i "0"
	foreach ship $EQ_ships {
		if { [string toupper $EQ_ship_class($ship)] == $uclass } {
			lappend sfound $ship
			incr i
		}
	}
	set j "0"
	foreach odg $EQ_odg {
		if { [string toupper $EQ_odg_class($odg)] == $uclass } {
			lappend ofound $odg
			incr j
		}
	}
	if { $i == "0" && $j == "0" } {
		EQ_tools:cmd_output $nick $chan "\002Error\002: Can't find any ships with class \002[lindex $args 0]\002"
	} else {
		if { $i != "0" } {
			EQ_tools:cmd_output $nick $chan "Ships of \002[lindex $args 0]\002 class:"
			set i "1"
			foreach ship $sfound {
				EQ_tools:cmd_output $nick $chan "$i: $EQ_ship_name($ship) \[\002$ship\002\]"
				incr i
			}
		}
		if { $j != "0" } {
			EQ_tools:cmd_output $nick $chan "ODG of \002[lindex $args 0]\002 class:"
			set j "1"
			foreach odg $ofound {
				EQ_tools:cmd_output $nick $chan "$j: $EQ_odg_name($odg) \[\002$odg\002\]"
				incr j
			}
		}
	}
	return 0
}		
	
# creating the fireorder command
bind pub - [string trim $EQ_tools(cmd_char)]fireorder pub:fireorder

proc pub:fireorder {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_fireorder EQ_odg EQ_odg_name EQ_odg_fireorder
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0" } { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	EQ_tools:cmd_output $nick $chan "\002Firing Order\002"
	EQ_tools:cmd_output $nick $chan "ODG:"
	set odg [array get EQ_odg_fireorder]
	for {set i 0} {$i < [llength $odg]} {incr i} {
		set temp [lindex $odg $i] 
		lappend temp [lindex $odg [incr i]]
		lappend temp2 $temp 
	}
	set odg [lsort -index 1 $temp2]
	foreach def $odg {
		set type [lindex $def 0]
		EQ_tools:cmd_output $nick $chan "[lindex $def 1]: $EQ_odg_name($type) \[\002$type\002\]"
	}
	EQ_tools:cmd_output $nick $chan "Ships:"
	unset temp2
	set ships [array get EQ_ship_fireorder]
	for {set i 0} {$i < [llength $ships]} {incr i} {
		set temp [lindex $ships $i] 
		lappend temp [lindex $ships [incr i]]
		lappend temp2 $temp 
	}
	set ships [lsort -index 1 $temp2]
	foreach ship $ships {
		set type [lindex $ship 0]
		EQ_tools:cmd_output $nick $chan "[lindex $ship 1]: $EQ_ship_name($type) \[\002$type\002\]"
	}
	return 0
}

# creating the traveltime command
bind pub - [string trim $EQ_tools(cmd_char)]traveltime pub:traveltime

proc pub:traveltime {nick uhost handle chan arg} {
	global EQ_tools EQ_ships EQ_ship_name EQ_ship_tt
	if {[lsearch -exact $EQ_tools(ignore_chans) [string tolower $chan]] >= "0" } { return 0 }
	foreach name $EQ_tools(other_bots) { 
		if { [onchan $name $chan] == "1" } { return 0 }
	}
	EQ_tools:cmd_output $nick $chan "\002Travel Time:\002"
	set ships [array get EQ_ship_tt]
	for {set i 0} {$i < [llength $ships]} {incr i} {
		set temp [lindex $ships $i] 
		lappend temp [lindex $ships [incr i]]
		lappend temp2 $temp 
	}
	set ships [lsort -integer -index 1 $temp2]
	foreach ship $ships {
		set type [lindex $ship 0]
		EQ_tools:cmd_output $nick $chan "[lindex $ship 1]: $EQ_ship_name($type) \[\002$type\002\]"
	}
	return 0
}
putlog "*** EQ-tools.tcl by XiM loaded. (v$EQ_tools(version)) ***"
