#! WISH -f
#
# tkxpilots Version 2.0pl0 - funky interface to xpilots
# Copyright (C) 1993-1994 Richard Oak.
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.

set tkxpilot_library "LIBDIR"
set auto_path "$tkxpilot_library $auto_path"

set tmp_dir "TMPDIR"
set maps_dir "MAPDIR"
set defaultsFileName "DEFAULTS"
set motd "MOTD"
set mapFileName "MAPNAME"
set com "xpilots"
set tkxpilotsrc $env(HOME)/.tkxpilotsrc
set maptoxbm "$tkxpilot_library/maptoxbm"

# A few procedures (may only be 1 in the end :-)

proc show_win page {
	global cur_page

	if {$page != $cur_page} {
	        pack forget .mainpanel.$cur_page

        	after 1 pack .mainpanel.$page  -in .mainpanel -side top -fill \
			both -expand yes
        	set cur_page $page
	}
}

proc play_the_game {} {
	global contactPort team_to_join

	set com [format "xpilot -join -team %d -port %d" $team_to_join \
		[.mainpanel.misc.a.ent.port.ent get]]
	eval exec $com &
	destroy .
}

# check for alternate maps directories and default filename

set alt_dir $maps_dir

if {[file exists $tkxpilotsrc] == 1} {
foreach i [exec cat $tkxpilotsrc | $tkxpilot_library/quote] {
        set comment [string first "#" $i]

                if {$comment != -1} {
                        set i [string range $i 0 [expr $comment - 1]]
                }

                set colon [string first ":" $i]

                if {$colon != -1} {
                        set option [string tolower [string trim [string range \
                                $i 0 [expr $colon - 1]]]]
			set value [string trim [string range $i \
				[expr $colon + 1] end ]]
                switch -glob $option {
                        mapfilename {
                                set mapFileName $value
                        }
                        map {
                                set mapFileName $value
                        }
                        alternativemapsdir {
                                set alt_dir [concat $maps_dir $value]
                        }
                        default {
                        }
                }
        }
}
}

# Now the xpilot options (for 3.1.0)

set gravity -14
set shipMass 200
set shotsMass 10
set shotspeed 210
set shotLife 60
set fireRepeatRate 2
set wantedNumRobots 4
set robotsLeave 1
set robotLeaveLife 50
set robotLeaveScore -90
set robotLeaveRatio -5
set maxPlayerShots 256
set shotsGravity 1
set idleRun 0
set noQuit 0
set mapWidth 100
set mapHeight 100
set mapName unknown
set mapAuthor anonymous
set contactPort 15345
set mapData xxxxx
set crashWithPlayer 1
set bounceWithPlayer 1
set playerKillings 1
set playerShielding 1
set playerStartsShielded 1
set itemsWallBounce 1
set shotsWallBounce 0
set ballsWallBounce 1
set minesWallBounce 0
set missilesWallBounce 0
set sparksWallBounce 0
set debrisWallBounce 0
set maxObjectWallBounceSpeed 40
set maxShieldedWallBounceSpeed 50
set maxUnshieldedWallBounceSpeed 20
set maxShieldedWallBounceAngle 90
set maxUnshieldedWallBounceAngle 30
set playerWallBrakeFactor 89
set objectWallBrakeFactor 95
set objectWallBounceLifeFactor 80
set wallBounceFuelDrainMult 1
set wallBounceDestroyItemProb 0
set reportToMetaServer 1
set limitedVisibility 0
set limitedLives 0
set worldLives 0
set teamPlay 0
set teamAssign 0
set teamImmunity 1
set playerMinimumStartFuel 400
set targetKillTeam 0
set targetTeamCollision 1
set treasureKillTeam 0
set treasureCollisionDestroys 1
set treasureCollisionMayKill 0
set onePlayerOnly 0
set timing 0
set edgeWrap 0
set edgeBounce 1
set extraBorder 0
set gravityPoint 0,0
set gravX 0
set gravY 0
set gravityAngle 90
set gravityPointSource 0
set gravityClockwise 0
set gravityAnticlockwise 0
set framesPerSecond 18
set allowNukes 0
set allowClusters 0
set allowModifiers 1
set allowLaserModifiers 0
set allowShipShapes 0
set playersOnRadar 1
set missilesOnRadar 1
set minesOnRadar 0
set nukesOnRadar 1
set distinguishMissiles 1
set shieldedItemPickup 0
set shieldedMining 0
set laserIsStunGun 0
set nukeMinSmarts 7
set nukeMinMines 4
set nukeClusterDamage 10
set mineFuseTime 0
set movingItemProb 20
set dropItemOnKillProb 50
set destroyItemInCollisionProb 0
set itemProbMult 1
set maxItemDensity 12
set itemEnergyPackProb 0
set itemTankProb 0
set itemECMProb 0
set itemMineProb 0
set itemMissileProb 0
set itemCloakProb 0
set itemSensorProb 0
set itemWideangleProb 0
set itemRearshotProb 0
set itemAfterburnerProb 0
set itemTransporterProb 0
set itemLaserProb 0
set itemEmergencyThrustProb 0
set itemTractorBeamProb 0
set itemAutopilotProb 0
set initialFuel 1000
set initialTanks 0
set initialECMs 0
set initialMines 0
set initialMissiles 0
set initialCloaks 0
set initialSensors 0
set initialWideangles 0
set initialRearshots 0
set initialAfterburners 0
set initialTransporters 0
set initialLasers 0
set initialEmergencyThrusts 0
set initialTractorBeams 0
set initialAutopilots 0
set gameDuration 0

# And now, some tkxpilot stuff

set cur_page maps
set team_to_join 1

# set up the window
#
# not all of the frames and stuff will be shown at once

frame .mainpanel

frame .mainpanel.grav
frame .mainpanel.grav.grav
frame .mainpanel.grav.grav.bounce
frame .mainpanel.items
frame .mainpanel.pickups
frame .mainpanel.misc
frame .mainpanel.maps
frame .mainpanel.metaserver

mk_maps
mk_bounce
mk_grav
mk_items
mk_misc

frame .buttons

frame .buttons.c1
frame .buttons.c2
frame .buttons.c3
frame .buttons.c4

button .buttons.c1.r1 -text "Maps" -command "show_win maps"
button .buttons.c1.r2 -text "Show Copyright" -command "show_copyright"
button .buttons.c1.r3 -text "Start Server" -command "start_server"
pack .buttons.c1.r1 .buttons.c1.r2 .buttons.c1.r3 -side top -expand yes -fill \
	both

button .buttons.c2.r1 -text "Gravity / robots / teams / bounce" -command "show_win grav"
button .buttons.c2.r2 -text "Show Warranty" -command "show_warranty"
button .buttons.c2.r3 -text "Play Game" -command "play_the_game"
pack .buttons.c2.r1 .buttons.c2.r2 .buttons.c2.r3 -side top -expand yes -fill \
        both

button .buttons.c3.r1 -text "Misc" -command "show_win misc"
button .buttons.c3.r2 -text "Default values" -command "set_defaults"
button .buttons.c3.r3 -text "Help" -command "show_help"
pack .buttons.c3.r1 .buttons.c3.r2 .buttons.c3.r3 -side top -expand yes -fill \
        both

button .buttons.c4.r1 -text "Items" -command "show_win items"
button .buttons.c4.r2 -text "Info" -command "showInfo"
button .buttons.c4.r3 -text "Quit" -command {destroy .}
pack .buttons.c4.r1 .buttons.c4.r2 .buttons.c4.r3 -side top -expand yes -fill \
        both

pack .buttons.c1 .buttons.c2 .buttons.c3 .buttons.c4 -side left \
	-fill both -expand yes

pack .mainpanel.maps

pack .mainpanel .buttons -side top -fill both -expand yes

# parse the defaults file

parse $defaultsFileName 1

parse $mapFileName 1

if {[file exists $tkxpilotsrc] == 1} {
	parse $tkxpilotsrc 1
}
