#!WISH -f
#
# tkxpilots Version 2.0pl0 - funky interface to xpilots(6)
# Copyright (C) 1993 Richard Oak.
# Copyright (C) 1994 Yves-Henri Berne.
# Copyright (C) 1994 Ramdane Ferhati
# Copyright (c) 1994 by Andrew Scherpbier. (for maps2image Program)
#
#    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 maps_dir MAPDIR
set temp_dir TEMPDIR
set defaults_file DEFAULT
set default_mapfile MAP
set telnet_arg "METASERVER"

##################################   PROCEDURES  ################################

proc mkitem_init {{w .items}} {
  global initialFuel    initialTanks     initialECMs \
    initialMissiles     initialCloaks    initialSensors \
    initialWideangles   initialRearshots initialAfterburners \
    initialTransporters initialLasers    initialMines

    message $w.msg1 -font -Adobe-times-medium-r-normal--*-180* -aspect 800 \
            -text "Initial Items" -borderwidth 10
    pack $w.msg1 -side top

    frame $w.init 
    frame $w.init.1

    frame $w.init.1.slider_fuelpods
    label $w.init.1.slider_fuelpods.txt -text "Fuel"
    scale $w.init.1.slider_fuelpods.scale -orient horizontal -from 0 -to 2600 \
    -command "set initialFuel" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_fuelpods.scale $w.init.1.slider_fuelpods.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_fuelpods.scale set $initialFuel

    frame $w.init.1.slider_fueltanks
    label $w.init.1.slider_fueltanks.txt -text "Tanks"
    scale $w.init.1.slider_fueltanks.scale -orient horizontal -from 0 -to 8 \
    -command "set initialTanks" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_fueltanks.scale $w.init.1.slider_fueltanks.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_fueltanks.scale set $initialTanks

    frame $w.init.1.slider_ecm
    label $w.init.1.slider_ecm.txt -text "ECMs"
    scale $w.init.1.slider_ecm.scale -orient horizontal -from 0 -to 10 \
    -command "set initialECMs" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_ecm.scale $w.init.1.slider_ecm.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_ecm.scale set $initialECMs

    frame $w.init.1.slider_mines
    label $w.init.1.slider_mines.txt -text "Mines"
    scale $w.init.1.slider_mines.scale -orient horizontal -from 0 -to 10  \
    -command "set initialMines" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_mines.scale $w.init.1.slider_mines.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_mines.scale set $initialMines

    frame $w.init.1.slider_missiles
    label $w.init.1.slider_missiles.txt -text "Missiles"
    scale $w.init.1.slider_missiles.scale -orient horizontal -from 0 -to 10  \
    -command "set initialMissiles" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_missiles.scale $w.init.1.slider_missiles.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_missiles.scale set $initialMissiles

    frame $w.init.1.slider_cloaks
    label $w.init.1.slider_cloaks.txt -text "Cloaks"
    scale $w.init.1.slider_cloaks.scale -orient horizontal -from 0 -to 10  \
    -command "set initialCloaks" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.1.slider_cloaks.scale $w.init.1.slider_cloaks.txt \
    -side left -expand no -anchor w

    $w.init.1.slider_cloaks.scale set $initialCloaks

    frame $w.init.2

    frame $w.init.2.slider_sensors
    label $w.init.2.slider_sensors.txt -text "Sensors"
    scale $w.init.2.slider_sensors.scale -orient horizontal -from 0 -to 10  \
    -command "set initialSensors" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_sensors.scale $w.init.2.slider_sensors.txt \
    -side left -expand no -anchor w

    $w.init.2.slider_sensors.scale set $initialSensors

    frame $w.init.2.slider_wide
    label $w.init.2.slider_wide.txt -text "Wide shots"
    scale $w.init.2.slider_wide.scale -orient horizontal -from 0 -to 10  \
    -command "set initialWideangles" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_wide.scale $w.init.2.slider_wide.txt \
    -side left -expand no -anchor w

    $w.init.2.slider_wide.scale set $initialWideangles

    frame $w.init.2.slider_rear
    label $w.init.2.slider_rear.txt -text "Rear shots"
    scale $w.init.2.slider_rear.scale -orient horizontal -from 0 -to 10  \
    -command "set initialRearshots" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_rear.scale $w.init.2.slider_rear.txt \
    -side left -expand no -anchor w

    $w.init.2.slider_rear.scale set $initialRearshots

    frame $w.init.2.slider_afterburners
    label $w.init.2.slider_afterburners.txt -text "Afterburners"
    scale $w.init.2.slider_afterburners.scale -orient horizontal -from 0 -to 10 \
     -command "set initialAfterburners" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_afterburners.scale $w.init.2.slider_afterburners.txt \
    -side left -expand no -anchor w

    $w.init.2.slider_afterburners.scale set $initialAfterburners

    frame $w.init.2.slider_transporters
    label $w.init.2.slider_transporters.txt -text "Transporters"
    scale $w.init.2.slider_transporters.scale -orient horizontal -from 0 -to 10 \
     -command "set initialTransporters" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_transporters.scale $w.init.2.slider_transporters.txt \
    -side left -expand no -anchor center

    $w.init.2.slider_transporters.scale set $initialTransporters

    frame $w.init.2.slider_laser
    label $w.init.2.slider_laser.txt -text "Lasers" -anchor w
    scale $w.init.2.slider_laser.scale -orient horizontal -from 0 -to 5  \
    -command "set initialLasers" -bg Gray -relief flat -length 150 -width 10
    pack $w.init.2.slider_laser.scale $w.init.2.slider_laser.txt \
    -side left -expand no -anchor w

    $w.init.2.slider_laser.scale set $initialLasers

   pack $w.init.1.slider_fuelpods $w.init.1.slider_fueltanks \
        $w.init.1.slider_ecm $w.init.1.slider_mines  \
        $w.init.1.slider_missiles $w.init.1.slider_cloaks  \
        -side top -expand no -fill both

   pack $w.init.2.slider_wide $w.init.2.slider_rear  \
        $w.init.2.slider_afterburners $w.init.2.slider_sensors  \
        $w.init.2.slider_transporters $w.init.2.slider_laser  \
    -side top -expand no -fill both

    button $w.ok -text OK -command "destroy $w"

    pack $w.init.1 $w.init.2 -anchor n -side left -expand yes -fill both
    pack $w.init $w.ok -side top -expand yes -fill both
}

proc mkitem_other {{w .items}} {
  global movingItemProb destroyItemIncollisionProb \
         maxItemDensity dropItemOnKillProb

    message $w.msg2 -font -Adobe-times-medium-r-normal--*-180* -aspect 800 \
            -text "Other values" -pady 10
    pack $w.msg2 -side top

    frame $w.other

    frame $w.other.1
    frame $w.other.1.moving
    label $w.other.1.moving.label -text "Items appear as moving" -width 21 -anchor c
    entry $w.other.1.moving.entry -width 15 -relief sunken \
        -textvariable movingItemProb
    pack $w.other.1.moving.label $w.other.1.moving.entry -side top
    bind $w.other.1.moving.entry <Return> " "

    frame $w.other.1.drop
    label $w.other.1.drop.label -text "Item drop when die" -width 21 -anchor c
    entry $w.other.1.drop.entry -width 15 -relief sunken -bd 2 \
        -textvariable dropItemOnKillProb
    pack $w.other.1.drop.label $w.other.1.drop.entry -side top
    bind $w.other.1.drop.entry <Return> " "

    pack $w.other.1.moving $w.other.1.drop  -side top

    frame $w.other.2
    frame $w.other.2.destroy
    label $w.other.2.destroy.label -text "Item destroy when die" -width 21 -anchor c
    entry $w.other.2.destroy.entry -width 15 -relief sunken -bd 2 \
        -textvariable destroyItemIncollisionProb
    pack $w.other.2.destroy.label $w.other.2.destroy.entry -side top
    bind $w.other.2.destroy.entry <Return> " "

    frame $w.other.2.maxItemDensity
    label $w.other.2.maxItemDensity.label -text "Item maxItemDensity" -width 21 -anchor c
    entry $w.other.2.maxItemDensity.entry -width 15 -relief sunken -bd 2 \
        -textvariable maxItemDensity
    pack $w.other.2.maxItemDensity.label $w.other.2.maxItemDensity.entry -side top
    bind $w.other.2.maxItemDensity.entry <Return> " "

    pack $w.other.2.destroy $w.other.2.maxItemDensity -side top

    pack $w.other.1 $w.other.2 -anchor n -side left -expand yes -fill both

    pack $w.other -anchor n -side top -expand yes -fill both
}

proc mkitem_probs {{w .items}} {
  global itemProbFact      itemEnergyPackProb itemTankProb  itemECMProb \
         itemMineProb      itemMissileProb    itemCloakProb itemSensorProb \
         itemWideangleProb itemRearshotProb   itemAfterburnerProb \
         itemLaserProb     itemTransporterProb 

    catch {destroy $w}
    toplevel $w
    wm title $w "Items Setup"
    wm iconname $w "Setup"

    message $w.msg -font -Adobe-times-medium-r-normal--*-180* -aspect 800 \
            -text "Item Probabilties" -borderwidth 10
    pack $w.msg -side top

    frame $w.frame 
    frame $w.frame.1

    frame $w.frame.1.fuelpods
    label $w.frame.1.fuelpods.label -text "Fuel" -width 11 -anchor w
    entry $w.frame.1.fuelpods.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemEnergyPackProb
    pack $w.frame.1.fuelpods.entry $w.frame.1.fuelpods.label -side left
    bind $w.frame.1.fuelpods.entry <Return> " "

    frame $w.frame.1.fueltanks
    label $w.frame.1.fueltanks.label -text "Tank" -width 11 -anchor w
    entry $w.frame.1.fueltanks.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemTankProb
    pack $w.frame.1.fueltanks.entry $w.frame.1.fueltanks.label -side left
    bind $w.frame.1.fueltanks.entry <Return> " "

    frame $w.frame.1.ecm
    label $w.frame.1.ecm.label -text "Ecm" -width 11 -anchor w
    entry $w.frame.1.ecm.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemECMProb
    pack $w.frame.1.ecm.entry $w.frame.1.ecm.label -side left
    bind $w.frame.1.ecm.entry <Return> " "

    frame $w.frame.1.mine
    label $w.frame.1.mine.label -text "Mine" -width 11 -anchor w
    entry $w.frame.1.mine.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemMineProb
    pack $w.frame.1.mine.entry $w.frame.1.mine.label -side left
    bind $w.frame.1.mine.entry <Return> "pickups_mine"

    frame $w.frame.1.missiles
    label $w.frame.1.missiles.label -text "Missile" -width 11 -anchor w
    entry $w.frame.1.missiles.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemMissileProb
    pack $w.frame.1.missiles.entry $w.frame.1.missiles.label -side left
    bind $w.frame.1.missiles.entry <Return> "pickups_missile"

    frame $w.frame.1.cloaks
    label $w.frame.1.cloaks.label -text "Cloak" -width 11 -anchor w
    entry $w.frame.1.cloaks.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemCloakProb
    pack $w.frame.1.cloaks.entry $w.frame.1.cloaks.label -side left
    bind $w.frame.1.cloaks.entry <Return> " "

    frame $w.frame.2

    frame $w.frame.2.sensor
    label $w.frame.2.sensor.label -text "Sensor" -width 11 -anchor w
    entry $w.frame.2.sensor.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemSensorProb
    pack $w.frame.2.sensor.entry $w.frame.2.sensor.label -side left
    bind $w.frame.2.sensor.entry <Return> " "

    frame $w.frame.2.wide
    label $w.frame.2.wide.label -text "Wide" -width 11 -anchor w
    entry $w.frame.2.wide.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemWideangleProb
    pack $w.frame.2.wide.entry $w.frame.2.wide.label -side left
    bind $w.frame.2.wide.entry <Return> " "

    frame $w.frame.2.rear
    label $w.frame.2.rear.label -text "Rear" -width 11 -anchor w
    entry $w.frame.2.rear.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemRearshotProb
    pack $w.frame.2.rear.entry $w.frame.2.rear.label -side left
    bind $w.frame.2.rear.entry <Return> " "

    frame $w.frame.2.after
    label $w.frame.2.after.label -text "Afterburner" -width 11 -anchor w
    entry $w.frame.2.after.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemAfterburnerProb
    pack $w.frame.2.after.entry $w.frame.2.after.label -side left
    bind $w.frame.2.after.entry <Return> " "

    frame $w.frame.2.trans
    label $w.frame.2.trans.label -text "Transporter" -width 11 -anchor w
    entry $w.frame.2.trans.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemTransporterProb
    pack $w.frame.2.trans.entry $w.frame.2.trans.label -side left
    bind $w.frame.2.trans.entry <Return> " "

    frame $w.frame.2.laser
    label $w.frame.2.laser.label -text "Laser" -width 11 -anchor w
    entry $w.frame.2.laser.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemLaserProb
    pack $w.frame.2.laser.entry $w.frame.2.laser.label -side left
    bind $w.frame.2.laser.entry <Return> "pickups_laser"

    frame $w.frame3
    label $w.frame3.label -text "Item Probability Factor Multiplier" -width 34 -anchor w
    entry $w.frame3.entry -width 15 -relief sunken -bd 2 \
        -textvariable itemProbFact
    pack $w.frame3.label $w.frame3.entry -side top
    bind $w.frame3.entry <Return> " "

   pack $w.frame.1.fuelpods $w.frame.1.fueltanks \
        $w.frame.1.ecm $w.frame.1.mine  \
        $w.frame.1.missiles $w.frame.1.cloaks  \
        -side top -expand yes -fill both

   pack $w.frame.2.wide $w.frame.2.rear  \
        $w.frame.2.after $w.frame.2.sensor  \
        $w.frame.2.trans $w.frame.2.laser  \
    -side top -expand yes -fill both

    pack $w.frame.1 $w.frame.2 -anchor n -side left -expand yes -fill both
    pack $w.frame $w.frame3 -side top -expand yes -fill both
}

proc shields w {
    global allowShields col

    if {$allowShields == 1} {
        $w.shieldedMining     configure -state normal
        $w.shieldedItemPickup configure -state normal
        $w.shieldedMining     configure -foreground black
        $w.shieldedItemPickup configure -foreground black
    } else {
        $w.shieldedMining     configure -state disabled
        $w.shieldedItemPickup configure -state disabled
        $w.shieldedMining     configure -foreground $col
        $w.shieldedItemPickup configure -foreground $col
    }
}

proc deal_with_robots num {
    global robotLeaveRatio robotsLeave     rawMode robots \
           robotLeaveLife  robotLeaveScore col

  set robots $num
  if {$robots == 0} {
        .mainpanel.sliders.robotLeaveRatio.scale configure -state disabled
        .mainpanel.sliders.robotLeaveLife.scale  configure -state disabled
        .mainpanel.sliders.robotLeaveScore.scale configure -state disabled
        .mainpanel.check.rawMode configure -state disabled
        .mainpanel.check.robotsLeave configure -state disabled

        .mainpanel.sliders.robotLeaveRatio.txt   configure -foreground $col
        .mainpanel.sliders.robotLeaveRatio.scale configure -foreground $col
        .mainpanel.sliders.robotLeaveLife.txt    configure -foreground $col
        .mainpanel.sliders.robotLeaveLife.scale  configure -foreground $col
        .mainpanel.sliders.robotLeaveScore.txt   configure -foreground $col
        .mainpanel.sliders.robotLeaveScore.scale configure -foreground $col
        .mainpanel.check.rawMode configure -foreground $col
        .mainpanel.check.robotsLeave configure -foreground $col
  } else {
        .mainpanel.sliders.robotLeaveRatio.scale configure -state normal
        .mainpanel.sliders.robotLeaveLife.scale  configure -state normal
        .mainpanel.sliders.robotLeaveScore.scale configure -state normal
        .mainpanel.check.rawMode configure -state normal
        .mainpanel.check.robotsLeave configure -state normal

        .mainpanel.sliders.robotLeaveRatio.txt   configure -foreground black
        .mainpanel.sliders.robotLeaveRatio.scale configure -foreground black
        .mainpanel.sliders.robotLeaveLife.txt    configure -foreground black
        .mainpanel.sliders.robotLeaveLife.scale  configure -foreground black
        .mainpanel.sliders.robotLeaveScore.txt   configure -foreground black
        .mainpanel.sliders.robotLeaveScore.scale configure -foreground black
        .mainpanel.check.rawMode configure -foreground black
        .mainpanel.check.robotsLeave configure -foreground black
  }
}

proc deal_with_robots_leaving {} {
    global robotsLeave col

  if {$robotsLeave == 0} {
        .mainpanel.sliders.robotLeaveRatio.scale configure -state disabled
        .mainpanel.sliders.robotLeaveLife.scale  configure -state disabled
        .mainpanel.sliders.robotLeaveScore.scale configure -state disabled

        .mainpanel.sliders.robotLeaveRatio.txt   configure -foreground $col
        .mainpanel.sliders.robotLeaveRatio.scale configure -foreground $col
        .mainpanel.sliders.robotLeaveLife.txt    configure -foreground $col
        .mainpanel.sliders.robotLeaveLife.scale  configure -foreground $col
        .mainpanel.sliders.robotLeaveScore.txt   configure -foreground $col
        .mainpanel.sliders.robotLeaveScore.scale configure -foreground $col
  } else {
        .mainpanel.sliders.robotLeaveRatio.scale configure -state normal
        .mainpanel.sliders.robotLeaveLife.scale  configure -state normal
        .mainpanel.sliders.robotLeaveScore.scale configure -state normal

        .mainpanel.sliders.robotLeaveRatio.txt   configure -foreground black
        .mainpanel.sliders.robotLeaveRatio.scale configure -foreground black
        .mainpanel.sliders.robotLeaveLife.txt    configure -foreground black
        .mainpanel.sliders.robotLeaveLife.scale  configure -foreground black
        .mainpanel.sliders.robotLeaveScore.txt   configure -foreground black
        .mainpanel.sliders.robotLeaveScore.scale configure -foreground black
  }
}

proc team_play {} {
    global teamPlay col

    if {$teamPlay == 1} {
        .mainpanel.check.targetKillTeam configure -state normal
        .mainpanel.check.targetKillTeam configure -foreground black

  } else {
        .mainpanel.check.targetKillTeam configure -state disabled
        .mainpanel.check.targetKillTeam configure -foreground $col
  }
}

proc missiles_nuke {} {
    global nukes col

  if {$nukes == 0} {
        .mainpanel.sliders.maxNuke.txt   configure -foreground $col
        .mainpanel.sliders.maxNuke.scale configure -foreground $col
        .mainpanel.sliders.maxNuke.scale configure -state disabled
  } else {
        .mainpanel.sliders.maxNuke.txt   configure -foreground black
        .mainpanel.sliders.maxNuke.scale configure -foreground black
        .mainpanel.sliders.maxNuke.scale configure -state normal
  }
}

proc lim_lives {} {
    global limitedLives col

  if {$limitedLives == 0} {
        .mainpanel.sliders.lives.txt   configure -foreground $col
        .mainpanel.sliders.lives.scale configure -foreground $col
        .mainpanel.sliders.lives.scale configure -state disabled
  } else {
        .mainpanel.sliders.lives.txt   configure -foreground black
        .mainpanel.sliders.lives.scale configure -foreground black
        .mainpanel.sliders.lives.scale configure -state normal
  }
}

proc one_player {} {
    global onePlayerOnly col

  if {$onePlayerOnly == 1} {
        .mainpanel.check.teamPlay configure -state disabled
        .mainpanel.check.teamPlay configure -foreground $col
        .mainpanel.check.targetKillTeam configure -state disabled
        .mainpanel.check.targetKillTeam configure -foreground $col
        .mainpanel.check.allowPlayerKilling configure -state disabled
        .mainpanel.check.allowPlayerKilling configure -foreground $col
        .mainpanel.check.allowPlayerCrashes configure -state disabled
        .mainpanel.check.allowPlayerCrashes configure -foreground $col
  } else {
        .mainpanel.check.teamPlay configure -state normal
        .mainpanel.check.teamPlay configure -foreground black
        .mainpanel.check.allowPlayerKilling configure -state normal
        .mainpanel.check.allowPlayerKilling configure -foreground black
        .mainpanel.check.allowPlayerCrashes configure -state normal
        .mainpanel.check.allowPlayerCrashes configure -foreground black
        team_play
  }
}

proc check_laser {} {
    global itemLaserProb col

  if {$itemLaserProb == 0} {
        .mainpanel.check.laserIsStunGun configure -foreground $col
        .mainpanel.check.laserIsStunGun configure -state disabled
  } else {
        .mainpanel.check.laserIsStunGun configure -foreground black
        .mainpanel.check.laserIsStunGun configure -state normal
  }
}

proc check_mines {} {
    global itemMineProb col

  if {$itemMineProb == 0} {
        .mainpanel.check.shieldedMining configure -foreground $col
        .mainpanel.check.shieldedMining configure -state disabled
  } else {
        .mainpanel.check.shieldedMining configure -foreground black
        .mainpanel.check.shieldedMining configure -state normal
  }
}

proc check_missiles {} {
    global itemMissileProb col

  if {$itemMissileProb == 0} {
        .mainpanel.check.nukes configure -foreground $col
        .mainpanel.check.nukes configure -state disabled
        .mainpanel.check.missilesOnRadar configure -foreground $col
        .mainpanel.check.missilesOnRadar configure -state disabled
  } else {
        .mainpanel.check.nukes configure -foreground black
        .mainpanel.check.nukes configure -state normal
        .mainpanel.check.missilesOnRadar configure -foreground black
        .mainpanel.check.missilesOnRadar configure -state normal
  }
}

proc no_visibility {} {
    global playersOnRadar col

  if {$playersOnRadar == 1} {
        .mainpanel.check.limitedVisibility configure -state normal
        .mainpanel.check.limitedVisibility configure -foreground black
  } else {
        .mainpanel.check.limitedVisibility configure -state disabled
        .mainpanel.check.limitedVisibility configure -foreground $col
  }
}

proc pickups_laser {} {
    global itemLaserProb col

    if {$itemLaserProb == 0} {
      foreach i {.mainpanel.check.laserIsStunGun} {
           $i configure -foreground $col
           $i configure -state disabled
      }
    } else {check_laser}
}

proc pickups_mine {} {
    global itemMineProb col

    if {$itemMineProb == 0} {
       foreach i {.mainpanel.check.shieldedMining} {
            $i configure -foreground $col
            $i configure -state disabled
      }
    } else {check_mines}
}

proc pickups_missile {} {
    global itemMissileProb col

    if {$itemMissileProb == 0} {
      foreach i {.mainpanel.check.nukes .mainpanel.check.missilesOnRadar} {
            $i configure -foreground $col
            $i configure -state disabled
      }
    } else {check_missiles}
}

proc gravity_entry {} {
  global gravityPointSource   gravityClockwise \
         gravityAnticlockwise gravityLine col

  if {$gravityLine == 1} { set gravityPointSource 0
  } else { set gravityPointSource 1 }
  if {$gravityPointSource == 0} {
        .mainpanel.check.a.2.gravityPoint.entry configure -state disabled
        .mainpanel.check.a.2.gravityPoint.label configure -foreground $col
        .mainpanel.check.a.3.gravityAngle.entry configure -state normal
        .mainpanel.check.a.3.gravityAngle.label configure -foreground black
  } else {
        .mainpanel.check.a.2.gravityPoint.entry configure -state normal
        .mainpanel.check.a.2.gravityPoint.label configure -foreground black
        .mainpanel.check.a.3.gravityAngle.entry configure -state disabled
        .mainpanel.check.a.3.gravityAngle.label configure -foreground $col
  }
}

proc gravity_check but {
    global gravityClockwise gravityAnticlockwise gravityLine

    if {$but == "gravityClockwise"} {
        set button1 .mainpanel.check.g.gravityAnticlockwise
        set button2 .mainpanel.check.g.gravityLine
    }
    if {$but == "gravityAnticlockwise"} {
        set button1 .mainpanel.check.g.gravityLine
        set button2 .mainpanel.check.g.gravityClockwise
    }
    if {$but == "gravityLine"} {
        set button1 .mainpanel.check.g.gravityClockwise
        set button2 .mainpanel.check.g.gravityAnticlockwise
    }
    upvar #0 $but plop
    if {$plop == 1} {
        $button1 deselect
        $button2 deselect
    }
    gravity_entry
}

proc edges but {
    global edgeBounce edgeWrap extraBorder

    if {$but == "edgeBounce"} {
        set button1 .mainpanel.check.e.edgeWrap
        set button2 .mainpanel.check.e.extraBorder
    }
    if {$but == "edgeWrap"} {
        set button1 .mainpanel.check.e.extraBorder
        set button2 .mainpanel.check.e.edgeBounce
    }
    if {$but == "extraBorder"} {
        set button1 .mainpanel.check.e.edgeBounce
        set button2 .mainpanel.check.e.edgeWrap
    }
    upvar #0 $but plop
    if {$plop == 1} {
        $button1 deselect
        $button2 deselect
    }
}

# Procedures to connect to the metaserver
proc get_metaserversort {{w .mainpanel.metaserver}} {
    global metsort metsens seenew

    catch {destroy $w}
    catch {destroy .buttons.meta2}
    toplevel $w
    wm geometry $w +300+500
    wm title $w "Meta-Server"
    wm iconname $w "Radiobuttons"
    message $w.msg -font -Adobe-times-medium-r-normal--*-180* -aspect 800 \
            -text "Current servers sorted by :"
    frame $w.frame -borderwidth 1
    frame $w.frame2
    pack $w.msg -side top
    pack $w.frame -side top 
    pack $w.frame2 -side bottom -fill both -expand yes

    set metsort 10
    set metsens 1
    set seenew 1

    frame $w.frame.left
    frame $w.frame.right
    pack $w.frame.left $w.frame.right -side left -expand yes

    radiobutton $w.frame.left.b1 -text "Frames per second" -variable metsort \
            -relief flat -value 10
    radiobutton $w.frame.left.b2 -text "Number of users" -variable metsort \
            -relief flat -value 4
    radiobutton $w.frame.left.b3 -text "Map names" -variable metsort \
            -relief flat -value 5
    radiobutton $w.frame.left.b4 -text "Version number" -variable metsort \
            -relief flat -value 1
    radiobutton $w.frame.left.b5 -text "Server names" -variable metsort \
            -relief flat -value 2
    radiobutton $w.frame.left.b6 -text "No sort" -variable metsort \
            -relief flat -value 0
    pack $w.frame.left.b1 $w.frame.left.b2 $w.frame.left.b3 $w.frame.left.b4 \
            $w.frame.left.b5 $w.frame.left.b6 -side top -pady 2 -anchor w

    radiobutton $w.frame.right.b1 -text "Ascending" -variable metsens \
            -relief flat -value 0
    radiobutton $w.frame.right.b2 -text "Descending" -variable metsens \
            -relief flat -value 1

    pack $w.frame.right.b1 $w.frame.right.b2 -side top -pady 2 -anchor w
    button $w.frame2.ok -text Quit -command "destroy $w" -width 12
    button $w.frame2.vars1 -text "Old Servers" -width 12\
        -command "  set seenew 0 ; update; destroy $w ;
    get_metaserver metsort metsens seenew"

    button $w.frame2.vars2 -text "New Servers" -width 12\
    -command "  $w.frame2.vars2 configure -relief sunken -text Connecting... ;  
        update; destroy $w ; get_metaserver metsort metsens seenew"

    pack $w.frame2.ok $w.frame2.vars1 $w.frame2.vars2 -side left -expand yes -fill both
}

proc get_metaserver {args} {
  global telnet_arg temp_dir metsort metsens seenew msg1

    if {$seenew == 1} {
      catch {eval exec telnet $telnet_arg | sed -e "s/\"//g" > $temp_dir/tkxpilots.tm} msg
      set x  [string first ":no:" [exec cat $temp_dir/tkxpilots.tm]]
      if {$x == -1} {set x  [string first ":yes:" [exec cat $temp_dir/tkxpilots.tm]]}
      if {$x != -1} {eval exec mv -f $temp_dir/tkxpilots.tm $temp_dir/tkxpilots.tmp}
    } else {
      if {[file exist $temp_dir/tkxpilots.tmp] == 0} {
        set x -1
        set msg "\"$temp_dir/tkxpilots.tmp\" doesn't exist you must first select \"New server\" button."
      } else {
        set x [string first ":no:" [exec cat $temp_dir/tkxpilots.tmp]]
        if {$x == -1} {set x  [string first ":yes:" [exec cat $temp_dir/tkxpilots.tmp]]}
        if {$x == -1}  {set msg [exec cat $temp_dir/tkxpilots.tmp]}
      }
    }
    set w .mainpanel.metaserver
    catch {destroy $w}
    toplevel $w

  if {$x != -1} {
    wm geometry $w +100+100
    wm title $w "Set of active servers"
    wm iconname $w "Canvas"
    wm minsize $w 300 700
    set c $w.frame.c

    frame $w.frame -relief raised -bd 2
    button $w.ok -text "OK" -command "eval exec rm -f $temp_dir/tkxpilots.tms ; destroy $w"
    pack $w.ok -side bottom -pady 5
    pack $w.frame -side top -expand yes -fill both

    set txt ""
    if {$metsort== 10} {set txt "Frames per second"
    } elseif {$metsort==4} {set txt "Number of users"
    } elseif {$metsort==5} {set txt "Map names"
    } elseif {$metsort==1} {set txt "Version number"
    } elseif {$metsort==2} {set txt "Server names"}

    set txt [concat "Sorted by " $txt]
    if {$metsens==0} {set txt [concat $txt " (Asc.)"]
    } else { set txt [concat $txt " (Desc.)"]}
    if {$metsort==0} {set txt "Server not sorted"}

    set sortsens ""
    if {($metsens > 0)} {set sortsens "-r"}
    if {($metsort > 0)} {
        # If HP-UX
        set num ""
        if { $metsort == 10 } {set num "-n" }

        set sortcom [concat "$sortsens -t: $num -k $metsort "] 
        set x [catch {eval exec sort $sortcom $temp_dir/tkxpilots.tmp > $temp_dir/tkxpilots.tms}]
       if { $x != 0 } {
             # If SUN-UX
             set sortcom [concat "$sortsens -t: $num +[expr $metsort -1]" ]
             set x [catch {eval exec sort $sortcom $temp_dir/tkxpilots.tmp > $temp_dir/tkxpilots.tms}]
       }
      if { $x != 0 } {
        # If Unknown sort options for unknown platform -> No Sort
        set metsort 0
        eval exec cp $temp_dir/tkxpilots.tmp $temp_dir/tkxpilots.tms
      }

  } else {
        eval exec cp $temp_dir/tkxpilots.tmp $temp_dir/tkxpilots.tms
  }
    set j 0
    set k 0
    set msg1 [exec cat $temp_dir/tkxpilots.tms | grep "3."]
    set msg1 [split $msg1 \n]
    canvas $c -scrollregion {0c 0c 50c 40 } -yscroll "$w.frame.vscroll set" -scrollincrement 20

    foreach i $msg1 {
          set is_serv [string first ":" $i]
          if {$is_serv != -1} {
          set serv_name2 [string range $i [expr $is_serv+1] end]
          set is_serv2 [string first ":" $serv_name2]
          set serv_title [string range $serv_name2 0 [expr $is_serv2-1]]
          $c create text 10 [expr $j] -text $serv_title -anchor nw -tags text \
              -fill black -font -Adobe-Times-Medium-R-Normal-*-180-*
          incr j  20
          incr k 1
          }
    }
    label $w.frame.sort -text "$k $txt" -relief groove
    scrollbar $w.frame.vscroll  -relief sunken -command "$c yview"
    pack $w.frame.vscroll -side right  -fill y

    $c config -scrollregion [$c bbox all]
    pack $w.frame.sort  -expand no -fill both
    pack $c -expand yes -fill both

    $c bind all <Any-Leave> "scrollLeave $c"
    $c bind all <Any-Enter> "ChoosenMeta  $c .buttons.meta2"
    $c bind all <Double-1> " eval exec rm -f $temp_dir/tkxpilots.tms ; destroy $w"
  } else {
    wm geometry $w +300+500
        wm title $w "Connection Error"
        wm iconname $w "Connection Error"

        frame $w.msg
    message $w.msg.text -text $msg -width 600
        pack $w.msg.text -side top -fill both
        button $w.ok -text OK -command "destroy $w"
    pack $w.msg -side top -fill both -expand yes
    pack $w.ok -side bottom -fill both -expand yes
  }
}

proc scrollLeave canvas {
  global oldFill
    set id [$canvas find withtag current]
    if {[lsearch [$canvas gettags current] text] >= 0} {
        set id [expr $id-1]
    }
    $canvas itemconfigure $id -fill $oldFill
    $canvas itemconfigure [expr $id+1] -fill black
}

proc ChoosenMeta {c w} {
  global oldFill server_name temp_dir port msg1

    set id [$c find withtag current]
    if {[lsearch [$c gettags current] text] < 0} {
        set id [expr $id+1]
  }
    set oldFill [lindex [$c itemconfig $id -fill] 4]
    if {[tk colormodel $c] == "color"} {
        $c itemconfigure $id -fill SeaGreen1
  } else { $c itemconfigure $id -fill white }

    catch {destroy $w}

    toplevel $w
    wm title $w "INFO  SERVER"
    wm geometry $w +500+100

  frame $w.lsg
  set c2 [lindex [$c itemconf $id -text] 4]
  set server_name $c2
  label $w.lsg.text0 -text "Server : $server_name" \
        -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 10

  set c2 [lindex $msg1 [expr $id-1]]
  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set com [format "Version : %s " $com]
  message $w.lsg.text1 -text $com  -anchor nw -width 600 -padx 10

  set fin [string first ":" $c2]
  set c2  [string range $c2 [expr $fin+1] end]


  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set port $com
  set com [format "Port : %s " $com]
        message $w.lsg.text7 -text $com  -anchor nw -width 600 -padx 10 

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]

  set nb  [format "%d" $com]
  set com [format "Number of players : %s " $com]
        message $w.lsg.text11 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set com [format "Map name : %s " $com]
        message $w.lsg.text3 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set com [format "Map size : %s " $com]
        message $w.lsg.text4 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set com [format "Author : %s " $com]
        message $w.lsg.text6 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2  [string range $c2 [expr $fin+1] end]
  set com [format "Status : %s " $com]
        message $w.lsg.text8 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2 [string range $c2 [expr $fin+1] end]
  set com  [format "Number of home bases : %s " $com]
        message $w.lsg.text5 -text $com  -anchor nw -width 600

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2 [string range $c2 [expr $fin+1] end]
  set com  [format "Frames per second : %s " $com]
        message $w.lsg.text2 -text $com  -anchor nw -width 600

  for {set i 1} {$i <= $nb} {incr i} {
    set j [expr $i+11]
    set sep ":"
    if {$i < $nb} then {set sep ","}
    set fin [string first $sep $c2]
    set com [string range $c2 0 [expr $fin-1]]
    set c2 [string range $c2 [expr $fin+1] end]
    set com  [format "%d) %s" $i $com]
    message $w.lsg.text$j -text $com  -anchor nw -width 600 
}

 if {$nb == 0} then {
  set fin [string first ":" $c2]
  set c2 [string range $c2 [expr $fin+1] end]
}

  set fin [string first ":" $c2]
  set com [string range $c2 0 [expr $fin-1]]
  set c2 [string range $c2 [expr $fin+1] end]
  set com  [format "Sound : %s " $com]
        message $w.lsg.text10 -text $com  -anchor nw -width 600
  set com  [format "Server uptime : %s " $c2]
        message $w.lsg.text9 -text $com  -anchor nw -width 600

  for {set i 0} {$i <= $nb+11} {incr i} {
        pack $w.lsg.text$i -side top -fill both
}

  button $w.ok -text OK -command "destroy $w"
  pack $w.lsg -side top -expand yes -anchor c
  pack $w.ok -side bottom -fill both
}

# Procedures for setting up the frames
proc mkmaps w {
  global map maps_dir temp_dir

    catch {destroy $w.scroll}    
    catch {destroy $w.list}
    catch {destroy $w.msgt}
    scrollbar  $w.scroll  -relief sunken -command "$w.list yview"
    listbox $w.list -yscroll "$w.scroll set" \
            -relief sunken -geometry 19x22 -setgrid 1
    tk_listboxSingleSelect $w.list

    set k 0
    foreach i [exec ls $maps_dir] {
        set is_map [string first ".map" $i]
        if {$is_map != -1} {
            set map_title [string range $i 0 [expr $is_map-1]]
            set map_name [concat "%s.map" $map_title]
            $w.list insert  end  $map_title 
            incr k 1
        }
    }
    label $w.msgt -text "$k Maps" -relief ridge
    pack  $w.msgt -side top -expand yes -fill both
    bind $w.list <Double-1> "maps_update \[selection get\].map"
    pack $w.list $w.scroll -side left -anchor n -expand no -fill both
}

# Read-Write a map file and launch the server
proc read_write_exec_mapfile {file rwei} {
  global robotsLeave   shotsGravity   rawMode noQuit      allowPlayerCrashes \
    allowPlayerKilling allowShields   playerStartShielded targetKillTeam \
    limitedVisibility  limitedLives   teamPlay onePlayerOnly race edgeWrap \
    edgeBounce         extraBorder    nukes playersOnRadar missilesOnRadar \
    shieldedItemPickup shieldedMining laserIsStunGun gravityPointSource \
    gravityClockwise   gravityLine    shotLife gravityAnticlockwise\
    fireRepeatRate     robots         robotLeaveLife robotLeaveScore robotLeaveRatio \
    maxPlayerShots     lives maxNuke  FPS initialFuel initialTanks initialECMs \
    initialMines       initialMissiles    initialCloaks initialSensors \
    initialWideangles  initialRearshots   initialAfterburners \
    initialLasers      itemEnergyPac      kProb itemTankProb initialTransporters \
    itemECMProb        itemMineProb       itemMissileProb itemCloakProb itemSensorProb \
    itemWideangleProb  itemRearshotProb   itemAfterburnerProb itemTransporterProb \
    itemLaserProb      itemProbFact time  movingItemProb dropItemOnKillProb \
    maxItemDensity     gravity gravityPoint destroyItemIncollisionProb \
    gravityAngle       shipMass shotMass shotSpeed server_name FPS port team \
    default_mapfile    map maps_dir

if {$rwei=="I"} { #Init All default values
    set map $default_mapfile
    set robotsLeave 1
    set shotsGravity 1
    set rawMode 0
    set noQuit 0
    set allowPlayerCrashes 1
    set allowPlayerKilling 1
    set allowShields 1
    set playerStartShielded 1
    set targetKillTeam 0
    set limitedVisibility 0
    set limitedLives 0
    set teamPlay 0
    set onePlayerOnly 0
    set race 0
    set edgeWrap 0
    set edgeBounce 1
    set extraBorder 0
    set nukes 0
    set playersOnRadar 1
    set missilesOnRadar 1
    set shieldedItemPickup 0
    set shieldedMining 0
    set laserIsStunGun 0
    set gravity -0.14
    set gravityPointSource 0
    set gravityClockwise 0
    set gravityAnticlockwise 0
    set gravityPoint 0,0
    set gravityAngle 90
    set gravityLine 1
    set shipMass 20.0
    set shotMass 0.1
    set shotSpeed 21.0

  # Slider options
    set shotLife 90
    set fireRepeatRate 2
    set robots 4
    set robotLeaveLife 50
    set robotLeaveScore -90
    set robotLeaveRatio -5
    set maxPlayerShots 256
    set lives 0
    set itemProbFact 1.0
    set time 0
    set maxNuke 16

  # Probabilities
    set itemEnergyPackProb 0.0
    set itemTankProb 0.0
    set itemECMProb 0.0
    set itemMineProb 0.0
    set itemMissileProb 0.0
    set itemCloakProb 0.0
    set itemSensorProb 0.0
    set itemWideangleProb 0.0
    set itemRearshotProb 0.0
    set itemAfterburnerProb 0.0
    set itemTransporterProb 0.0
    set itemLaserProb 0.0
    set movingItemProb 0.2
    set dropItemOnKillProb 0.5
    set destroyItemIncollisionProb  0.0
    set maxItemDensity 0.00012

  # Initial items
    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

  # Others
    set server_name 127.0.0.1
    set FPS FRAMES
    set port 15345
    set team 1
}
if {$rwei=="R"} { #Read the map file
    global mapAuthor mapName mapWidth mapHeight

    set mapAuthor "Anonymous"
    set mapName "Unknown"

    set msg [exec grep ":" $file | grep -v "#" | grep -v "\\\\" ]
    set x [catch {set mapAuthor [exec echo $msg | grep -i "mapAuthor" | cut -d: -f2 ]}]
    if { $x == 0 } { set mapAuthor [string trim $mapAuthor ] }
    set x [catch {set mapName [exec echo $msg | grep -i "mapName" | cut -d: -f2 ]}]
    if { $x == 0 } { set mapName [string trim $mapName ] }

    set com [string tolower $msg]
    set com [exec echo $com | sed -e "s/	//g" | sed -e "s/ //g" ]
    set com "DEBUT [split $com] FIN"

    # Lecture Taille et Auteur

    foreach i { mapWidth mapHeight } {
          set CM " [string tolower $i]:"
          set fin [string first $CM  $com]
          while {$fin > 0} {
              set deb [expr $fin-1]
              set fin [expr $fin+[string length $CM]]
              set fin2  [string first \  [string range $com [expr $fin+1] end]]
              set rep [string range $com $fin [expr $fin+$fin2+1]]
              set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
              upvar #0 $i j
              set j $rep
              set fin [string first $CM  $com]
          }
    }

    foreach i { mapName mapAuthor mapData } {
          set CM " [string tolower $i]:"
          set fin [string first $CM  $com]
          while {$fin > 0} {
              set deb [expr $fin-1]
              set fin [expr $fin+[string length $CM]]
              set fin2  [string first \  [string range $com [expr $fin+1] end]]
              set rep [string range $com $fin [expr $fin+$fin2+1]]
              set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
              set fin [string first $CM  $com]
          }
    }

    # BOOLEAN VALUES
    foreach i { robotsLeave shotsGravity noQuit targetKillTeam \
        limitedVisibility limitedLives onePlayerOnly \
        rawMode allowPlayerCrashes allowPlayerKilling allowShields \
        playerStartShielded teamPlay race nukes playersOnRadar \
        missilesOnRadar shieldedItemPickup shieldedMining laserIsStunGun \
        gravityPointSource gravityClockwise gravityAnticlockwise \
        edgeWrap edgeBounce extraBorder } {

          set CM " [string tolower $i]:"
          set fin [string first $CM $com]
          while {$fin > 0} {
              set deb [expr $fin-1]
              set fin [expr $fin+[string length $CM]]
              set fin2  [string first \  [string range $com [expr $fin+1] end]]
              set rep [string range $com $fin [expr $fin+1]]
              set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
              upvar #0 $i j
              if [string match *$rep* "noonfa"] {set j 0 } else { set j 1}
              set fin [string first $CM $com]
          }
    }

    #synonyme
    foreach i { rawMode=idlerun allowPlayerCrashes=crash \
        allowPlayerKilling=killings allowShields=shields \
        playerStartShielded=playerStartsShielded teamPlay=teams \
        race=timing nukes=allownukes playersOnRadar=playersradar \
        missilesOnRadar=missilesradar shieldedItemPickup=shielditem \
        shieldedMining=shieldmine laserIsStunGun=stungun } {
          set pos [string first "=" $i]
          set v1 [string range $i 0 [expr $pos-1]]
          set v2 [string range $i [expr $pos+1] end]
	  set CM " [string tolower $v2]:"
          set fin [string first $CM $com]
          while {$fin > 0} {
             set deb [expr $fin-1]
             set fin [expr $fin+[string length $CM]]
             set fin2  [string first \  [string range $com [expr $fin+1] end]]
             set rep [string range $com $fin [expr $fin+1]]
             set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
             upvar #0 $v1 j
             if [string match *$rep* "noonfa"] {set j 0 } else { set j 1}
             set fin [string first $CM  $com]
	  }
	}

    # INTEGER and Floating points VALUES
    set CM " gravitypoint:"
    set fin [string first $CM  $com]
    while {$fin > 0} {
        set deb [expr $fin-1]
        set fin [expr $fin+[string length $CM]]
        set fin2  [string first \  [string range $com [expr $fin+1] end]]
        set rep [string range $com $fin [expr $fin+$fin2+1]]
        set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
        set gravityPoint [format "%s" $rep]
        set fin [string first $CM  $com]
    }

    foreach i { shotLife robotLeaveLife robotLeaveScore robotLeaveRatio \
        maxNuke fireRepeatRate robots maxPlayerShots lives \
        FPS itemProbFact time initialFuel initialTanks initialECMs initialMines \
        initialMissiles initialCloaks initialSensors initialWideangles \
        initialRearshots initialAfterburners initialTransporters initialLasers \
        itemEnergyPackProb itemTankProb itemECMProb \
        itemMineProb itemMissileProb itemCloakProb itemSensorProb \
        itemWideangleProb itemRearshotProb itemAfterburnerProb \
        itemTransporterProb itemLaserProb \
        movingItemProb dropItemOnKillProb destroyItemIncollisionProb \
        maxItemDensity gravity gravityAngle shipMass \
        shotMass shotSpeed } {
          set CM " [string tolower $i]:"
          set fin [string first $CM  $com]
          while {$fin > 0} {
              set deb [expr $fin-1]
              set fin [expr $fin+[string length $CM]]
              set fin2  [string first \  [string range $com [expr $fin+1] end]]
              set rep [string range $com $fin [expr $fin+$fin2+1]]
              set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
              upvar #0 $i j
              set j [format "%g" $rep]
              set fin [string first $CM  $com]
          }
    }

    #synonyme
    foreach i { maxNuke=maxMissilesPerNuke fireRepeatRate=fireRepeat \
        robots=maxRobots maxPlayerShots=shots lives=worldLives \
        FPS=framesPerSecond itemProbFact=itemProbMult time=gameDuration } {
          set pos [string first "=" $i]
          set v1 [string range $i 0 [expr $pos-1]]
          set v2 [string range $i [expr $pos+1] end]
          set CM " [string tolower $v2]:"
          set fin [string first $CM  $com]
          while {$fin > 0} {
              set deb [expr $fin-1]
              set fin [expr $fin+[string length $CM]]
              set fin2  [string first \  [string range $com [expr $fin+1] end]]
              set rep [string range $com $fin [expr $fin+$fin2+1]]
              set com "[string range $com 0 $deb][string range $com [expr $fin+$fin2+1] end]"
              upvar #0 $v1 j
              set j [format "%g" $rep]
              set fin [string first $CM  $com]
          }
    }

    set com "[string trim [string range $com 5 [expr [string length $com]-4]]]"
    if { $com != "" } {
        set w .mainpanel.warnopt
        catch {destroy $w}
        toplevel $w

        wm geometry $w +300+500
        wm title $w "Warning"
        wm iconname $w "Warning"

      frame $w.msg
      label $w.msg.text1 -text " Warning : unknown option(s) in \"$file\" : "
      message $w.msg.text -text $com -width 600
      message $w.msg.text2 -text "Please, edit this map-file and supress these options." -width 600
      pack $w.msg.text1 $w.msg.text $w.msg.text2 -side top -fill both
      button $w.ok -text OK -command "destroy $w"
      pack $w.msg -side top -fill both -expand yes
      pack $w.ok -side bottom -fill both -expand yes
    }

}

if {$rwei=="W"} { #Write in the map file
  global mapWidth mapHeight

  if {[string first ".map" $file] == -1} {set file "$file.map"}
  set nolin  [exec grep -n -i "multiline" $maps_dir/$map]
  set fin [string first ":" $nolin]
  set nolin [string range $nolin 0 [expr $fin-1]]
  set head1 [exec grep -i mapName $maps_dir/$map ]
  set head2 [exec grep -i mapAuthor $maps_dir/$map ]
  set head3 [exec head -$nolin $maps_dir/$map | grep -i "#"]
  set head "$head1\n$head2\n$head3"
  set f [open $maps_dir/tkxpmap.tmp w]

    puts $f $head
    foreach i { mapWidth mapHeight } {
            upvar #0 $i j
            puts $f "$i: $j"
    }

    foreach i {robotsLeave allowPlayerCrashes allowPlayerKilling \
        allowShields playerStartShielded shieldedItemPickup \
        playersOnRadar missilesOnRadar onePlayerOnly race \
        limitedVisibility noQuit rawMode teamPlay targetKillTeam \
        shotsGravity laserIsStunGun shieldedMining \
        edgeWrap edgeBounce extraBorder limitedLives nukes \
        gravityPointSource gravityClockwise gravityAnticlockwise } {
            upvar #0 $i j
            if {$j == 1} { puts $f "$i: Yes"
            } else { puts $f "$i: No" }
    }

    foreach i { robots FPS itemProbFact shotLife fireRepeatRate maxPlayerShots \
        itemEnergyPackProb itemTankProb itemECMProb itemMineProb \
        itemMissileProb itemCloakProb itemSensorProb itemWideangleProb \
        itemRearshotProb itemAfterburnerProb itemTransporterProb itemLaserProb \
        initialFuel initialTanks initialECMs initialMines initialMissiles initialCloaks \
        initialSensors initialWideangles initialRearshots initialAfterburners \
        initialTransporters initialLasers movingItemProb dropItemOnKillProb \
        destroyItemIncollisionProb maxItemDensity shipMass shotMass shotSpeed \
        time lives maxNuke gravity gravityPoint gravityAngle } {
            upvar #0 $i j
            puts $f "$i: $j"
    }

  close $f
  eval [exec tail +$nolin $maps_dir/$map >> $maps_dir/tkxpmap.tmp]
  eval [exec mv $maps_dir/tkxpmap.tmp $maps_dir/$file]
}

if {$rwei=="E"} { #Start server with the map file
    set com [concat "xpilots -map " $map]
    if {$port > 0} { set com [concat $com " -port " $port] }

    # Boolean values
    foreach i {robotsLeave allowPlayerCrashes allowPlayerKilling \
        allowShields playerStartShielded shieldedItemPickup \
        playersOnRadar missilesOnRadar onePlayerOnly race \
        limitedVisibility noQuit rawMode teamPlay targetKillTeam \
        shotsGravity laserIsStunGun shieldedMining \
        edgeWrap edgeBounce extraBorder limitedLives nukes \
        gravityPointSource gravityClockwise gravityAnticlockwise } {
           upvar #0 $i j
           if {$j == 1} { set com [concat $com " -$i"]
        } else { set com [concat $com " +$i"] }
    }

  # Other values
    foreach i { robots FPS itemProbFact shotLife fireRepeatRate maxPlayerShots \
        itemEnergyPackProb itemTankProb itemECMProb itemMineProb \
        itemMissileProb itemCloakProb itemSensorProb itemWideangleProb \
        itemRearshotProb itemAfterburnerProb itemTransporterProb itemLaserProb \
        initialFuel initialTanks initialECMs initialMines initialMissiles initialCloaks \
        initialSensors initialWideangles initialRearshots initialAfterburners \
        initialTransporters initialLasers movingItemProb dropItemOnKillProb \
        destroyItemIncollisionProb maxItemDensity shipMass shotMass shotSpeed \
        time lives maxNuke gravity gravityPoint gravityAngle } {
           upvar #0 $i j
           set com [concat $com " -$i " $j]
    }

  eval exec $com &
}
}

proc maps_update {file} {
  global defaults_file maps_dir   map robots \
         edgeWrap      edgeBounce extraBorder

    catch { destroy .items }
    catch { destroy .mainpanel.save}
    catch { destroy .mainpanel.warnopt}

    read_write_exec_mapfile "NULL" "I"
    read_write_exec_mapfile $defaults_file "R"
    if {$file == "tkmap.init"} { set file $map 
    } else { set map $file }
    read_write_exec_mapfile $maps_dir/$file "R"

    shields .mainpanel.check
    deal_with_robots $robots
    deal_with_robots_leaving
    team_play
    missiles_nuke
    lim_lives
    one_player
    no_visibility
    pickups_missile
    pickups_laser
    pickups_mine
    gravity_entry
    edges edgeWrap
    edges edgeBounce
    edges extraBorder
    setsliders .mainpanel.sliders
    setcheck .mainpanel.check
    mkbitmap
}

proc mkbitmap {} {
   global map maps_dir temp_dir

    set fil "$maps_dir/$map"
    catch {destroy .mainpanel.whichmap.bitmap}
    set x [catch {eval exec maps2image -s 180 -b $fil > $temp_dir/tkxpilots.bmp}]
    if { $x == 0 } {
       label .mainpanel.whichmap.bitmap -bitmap @$temp_dir/tkxpilots.bmp -relief sunken
       eval exec rm -f $temp_dir/tkxpilots.bmp
    } else {
       frame .mainpanel.whichmap.bitmap
       label .mainpanel.whichmap.bitmap.1 -text $map -relief ridge
       label .mainpanel.whichmap.bitmap.2 -text "maps2image not found"  -relief groove
       pack .mainpanel.whichmap.bitmap.1 .mainpanel.whichmap.bitmap.2 -side top
    }
    pack .mainpanel.whichmap.map .mainpanel.whichmap.bitmap \
    -side top -anchor n -expand yes
}

proc mkcheck w {
  global robotsLeave    shotsGravity   rawMode noQuit allowPlayerCrashes \
    allowPlayerKilling  allowShields   playerStartShielded targetKillTeam \
    limitedVisibility   limitedLives   teamPlay onePlayerOnly race \
    edgeWrap edgeBounce extraBorder    nukes    playersOnRadar missilesOnRadar \
    shieldedItemPickup  shieldedMining laserIsStunGun gravity \
    port team shipMass  shotMass shotSpeed   gravityPointSource \
    gravityClockwise    gravityAnticlockwise gravityPoint gravityAngle gravityLine

    checkbutton $w.robotsLeave -text "Do robots leave the game" -variable \
        robotsLeave -anchor w -relief flat -command "deal_with_robots_leaving"

    checkbutton $w.shotsGravity -text "Are bullets affected by gravity" \
        -variable shotsGravity -anchor w -relief flat

    checkbutton $w.rawMode -text "The robots keep playing" -variable rawMode \
        -anchor w -relief flat

    checkbutton $w.noQuit -text "The server keeps running" -variable noQuit \
        -anchor w -relief flat

    checkbutton $w.allowPlayerCrashes -text "Players can crash into each\
        other" -variable allowPlayerCrashes -anchor w -relief flat

    checkbutton $w.allowPlayerKilling -text "Players can kill each other" \
        -variable allowPlayerKilling -anchor w -relief flat

    checkbutton $w.allowShields -text "Shields" -variable allowShields \
        -anchor w -relief flat -command "shields $w"

    checkbutton $w.shieldedItemPickup -text "Pick up with shields on" \
        -variable shieldedItemPickup -anchor w -relief flat

    checkbutton $w.shieldedMining -text "Drop/throw mine with shields on" \
        -variable shieldedMining -anchor w -relief flat

    checkbutton $w.playerStartShielded -text "Start shielded" -variable \
        playerStartShielded -anchor w -relief flat

    checkbutton $w.targetKillTeam -text "Team dies when target explodes" \
        -variable targetKillTeam -anchor w -relief flat

    checkbutton $w.limitedVisibility -text "Limited visibility" -variable \
        limitedVisibility -anchor w -relief flat

    checkbutton $w.limitedLives -text "Limited lives" -variable limitedLives \
        -anchor w -relief flat -command "lim_lives"

    checkbutton $w.teamPlay -text "Team play" -variable teamPlay -anchor w \
        -relief flat -command "team_play"

    checkbutton $w.onePlayerOnly -text "1 Player only" -variable \
        onePlayerOnly -anchor w -relief flat -command "one_player"

    checkbutton $w.nukes -text Nukes -variable nukes -anchor w -relief flat \
    -command "missiles_nuke"

    checkbutton $w.playersOnRadar -text "Players visible on radar" -variable \
        playersOnRadar -anchor w -relief flat -command "no_visibility"

    checkbutton $w.missilesOnRadar -text "Missiles visible on radar" \
        -variable missilesOnRadar -anchor w -relief flat

    checkbutton $w.laserIsStunGun -text "Laser is a stun gun" -variable \
        laserIsStunGun -anchor w -relief flat

    checkbutton $w.race -text "Race mode.   Gravity from a : " -variable race \
        -anchor w -relief flat

#   pack $w.shotsGravity $w.robotsLeave $w.rawMode $w.onePlayerOnly 

    pack $w.shotsGravity $w.noQuit $w.robotsLeave $w.rawMode $w.onePlayerOnly \
    $w.allowPlayerCrashes $w.allowPlayerKilling $w.teamPlay \
    $w.targetKillTeam $w.allowShields $w.shieldedItemPickup $w.shieldedMining \
    $w.playerStartShielded $w.playersOnRadar $w.limitedVisibility \
    $w.laserIsStunGun $w.missilesOnRadar -side top -fill both -expand yes

    frame $w.e
    checkbutton $w.e.edgeWrap -text "Edge wrap" -variable edgeWrap -anchor w \
        -relief flat -command "edges edgeWrap"

    checkbutton $w.e.edgeBounce -text "Edge bouncing" -variable edgeBounce \
        -anchor w -relief flat -command "edges edgeBounce"

    checkbutton $w.e.extraBorder -text "Extra border" -variable extraBorder \
        -anchor w -relief flat -command "edges extraBorder"

    pack $w.e.edgeWrap $w.e.edgeBounce $w.e.extraBorder \
        -side left -fill both -expand yes
    pack $w.e  -side top -fill both -expand yes

    pack $w.nukes $w.limitedLives $w.race -side top -fill both -expand yes
    frame $w.g
    checkbutton $w.g.gravityClockwise -text "Clockwise" -variable gravityClockwise \
        -anchor w -relief flat -command "gravity_check gravityClockwise"

    checkbutton $w.g.gravityAnticlockwise -text "Anticlockwise" \
	-variable gravityAnticlockwise \
        -anchor w -relief flat -command "gravity_check gravityAnticlockwise"

    checkbutton $w.g.gravityLine -text "Line" -variable gravityLine \
        -anchor w -relief flat -command "gravity_check gravityLine"

    pack $w.g.gravityClockwise $w.g.gravityAnticlockwise $w.g.gravityLine \
        -side left -fill both -expand yes
    pack $w.g  -side top -fill both -expand yes

    frame $w.a
    frame $w.a.2
    frame $w.a.2.gravityPoint
    label $w.a.2.gravityPoint.label -text "Point gravity coordinates (x,y)" \
            -width 30 -anchor c
    entry $w.a.2.gravityPoint.entry -width 6 -relief sunken -bd 2 \
            -textvariable gravityPoint
    pack $w.a.2.gravityPoint.entry $w.a.2.gravityPoint.label -side left
    bind $w.a.2.gravityPoint.entry <Return> " "

    frame $w.a.3
    frame $w.a.3.gravityAngle
    label $w.a.3.gravityAngle.label -text "Line Gravity angle" -width 18 -anchor c
    entry $w.a.3.gravityAngle.entry -width 6 -relief sunken -bd 2 \
            -textvariable gravityAngle
    pack $w.a.3.gravityAngle.entry $w.a.3.gravityAngle.label -side left
    bind $w.a.3.gravityAngle.entry <Return> " "

    pack $w.a.2.gravityPoint $w.a.3.gravityAngle -side left -fill x
    pack $w.a.2 $w.a.3 -side top -fill x
    pack $w.a -side top -fill x

    frame $w.0
    frame $w.0.gravity
    label $w.0.gravity.label -text "Gravity" -width 15 -anchor w
    entry $w.0.gravity.entry -width 6 -relief sunken -bd 2 \
            -textvariable gravity
    pack $w.0.gravity.entry $w.0.gravity.label -side left
    bind $w.0.gravity.entry <Return> " "

    frame $w.0.port
    label $w.0.port.label -text "Port to use" -width 15 -anchor w
    entry $w.0.port.entry -width 6 -relief sunken -bd 2 -textvariable port
    pack $w.0.port.entry $w.0.port.label -side left
    bind $w.0.port.entry <Return> " "

    frame $w.0.team
    label $w.0.team.label -text "Team to join" -width 15 -anchor w
    entry $w.0.team.entry -width 6 -relief sunken -bd 2 \
            -textvariable team
    pack $w.0.team.entry $w.0.team.label -side left
    bind $w.0.team.entry <Return> " "

    pack $w.0.gravity $w.0.team $w.0.port -side top -fill x
    pack $w.0 -side left -fill x

    frame $w.1
    frame $w.1.shipMass
    label $w.1.shipMass.label -text "Ship mass" -width 12 -anchor w
    entry $w.1.shipMass.entry -width 5 -relief sunken -bd 2 \
            -textvariable shipMass
    pack $w.1.shipMass.entry $w.1.shipMass.label -side left
    bind $w.1.shipMass.entry <Return> " "

    frame $w.1.shotMass
    label $w.1.shotMass.label -text "Shot mass" -width 11 -anchor w
    entry $w.1.shotMass.entry -width 5 -relief sunken -bd 2 -textvariable shotMass
    pack $w.1.shotMass.entry $w.1.shotMass.label -side left
    bind $w.1.shotMass.entry <Return> " "

    frame $w.1.shotSpeed
    label $w.1.shotSpeed.label -text "Shot speed" -width 12 -anchor w
    entry $w.1.shotSpeed.entry -width 5 -relief sunken -bd 2 \
            -textvariable shotSpeed
    pack $w.1.shotSpeed.entry $w.1.shotSpeed.label -side left
    bind $w.1.shotSpeed.entry <Return> " "

    pack $w.1.shipMass $w.1.shotSpeed $w.1.shotMass -side top -fill x
    pack $w.1  -side top -fill x
}

proc mksliders w {
  global shotLife   fireRepeatRate robots robotLeaveLife robotLeaveScore \
    robotLeaveRatio maxPlayerShots lives  itemProbFact   FPS time maxNuke

    frame $w.shotLife
    label $w.shotLife.txt -text "Life of bullets"
    scale $w.shotLife.scale -orient horizontal -from 10 -to 150 -command \
        "set shotLife" -bg Gray -relief flat -length 200 -width 10
    pack $w.shotLife.txt $w.shotLife.scale -side top -expand yes \
	-anchor center

    frame $w.fireRepeatRate
    label $w.fireRepeatRate.txt -text "Frames per automatic fire"
    scale $w.fireRepeatRate.scale -orient horizontal -from 0 -to 5 -command \
        "set fireRepeatRate" -bg Gray -relief flat -length 200 -width 10
    pack $w.fireRepeatRate.txt $w.fireRepeatRate.scale -side top -expand yes \
        -anchor center

    frame $w.robots
    label $w.robots.txt -text "Number of robots"
    scale $w.robots.scale -orient horizontal -from 0 -to 10 -command \
        "deal_with_robots" -bg Gray -relief flat -length 200 -width 10
    pack $w.robots.txt $w.robots.scale -side top -expand yes -anchor center

    frame $w.robotLeaveLife
    label $w.robotLeaveLife.txt -text "Max life per robot"
    scale $w.robotLeaveLife.scale -orient horizontal -from 0 -to 75 -command \
        "set robotLeaveLife" -bg Gray -relief flat -length 200 -width 10
    pack $w.robotLeaveLife.txt $w.robotLeaveLife.scale -side top -expand yes \
        -anchor center

    frame $w.robotLeaveScore
    label $w.robotLeaveScore.txt -text "Min score for a robot"
    scale $w.robotLeaveScore.scale -orient horizontal -from \-100 -to 0 \
        -command "set robotLeaveScore" -bg Gray -relief flat -length 200 -width 10
    pack $w.robotLeaveScore.txt $w.robotLeaveScore.scale -side top -expand \
        yes -anchor center

    frame $w.robotLeaveRatio
    label $w.robotLeaveRatio.txt -text "Min ratio for a robot"
    scale $w.robotLeaveRatio.scale -orient horizontal -from \-10 -to 0 \
        -command "set robotLeaveRatio" -bg Gray -relief flat -length 200 -width 10
    pack $w.robotLeaveRatio.txt $w.robotLeaveRatio.scale -side top -expand \
        yes -anchor center

    frame $w.maxPlayerShots
    label $w.maxPlayerShots.txt -text "Max player shots"
    scale $w.maxPlayerShots.scale -orient horizontal -from 1 -to 300 \
        -command "set maxPlayerShots" -bg Gray -relief flat -length 200 -width 10
    pack $w.maxPlayerShots.txt $w.maxPlayerShots.scale -side top -expand yes \
        -anchor center

    frame $w.maxNuke
    label $w.maxNuke.txt -text "Max missiles per nuke"
    scale $w.maxNuke.scale -orient horizontal -from 0 -to 100 \
        -command "set maxNuke" -bg Gray -relief flat -length 200 -width 10
    pack $w.maxNuke.txt $w.maxNuke.scale -side top -expand yes \
        -anchor center

    frame $w.lives
    label $w.lives.txt -text "Number of lives"
    scale $w.lives.scale -orient horizontal -from 1 -to 10 -command "set \
        lives" -bg Gray -relief flat -length 200 -width 10
    pack $w.lives.txt $w.lives.scale -side top -expand yes -anchor center

    frame $w.time
    label $w.time.txt -text "Game Duration (mn)"
    scale $w.time.scale -orient horizontal -from 0 -to 240 -command "set \
        time" -bg Gray -relief flat -length 200 -width 10
    pack $w.time.txt $w.time.scale -side top -expand yes -anchor center

    frame $w.fps
    label $w.fps.txt -text "Frames Per Second"
    scale $w.fps.scale -orient horizontal -from 5 -to 25 -command "set FPS" \
        -bg Gray -relief flat -length 200 -width 10
    pack $w.fps.txt $w.fps.scale -side top -expand yes -anchor center

    pack $w.shotLife $w.fireRepeatRate $w.robots $w.robotLeaveLife \
        $w.robotLeaveScore $w.robotLeaveRatio $w.maxPlayerShots $w.maxNuke \
        $w.lives $w.time $w.fps -side top -expand yes -fill both
}

proc setcheck w {
  global robotsLeave   shotsGravity   allowPlayerCrashes  rawMode       noQuit \
    allowPlayerKilling allowShields   playerStartShielded targetKillTeam \
    limitedVisibility  limitedLives   onePlayerOnly       teamPlay      race \
    edgeBounce         extraBorder    edgeWrap            playersOnRadar \
    missilesOnRadar    shieldedMining shieldedItemPickup laserIsStunGun nukes

    foreach i {robotsLeave shotsGravity rawMode noQuit allowPlayerCrashes \
        allowPlayerKilling allowShields playerStartShielded targetKillTeam \
        limitedVisibility limitedLives teamPlay onePlayerOnly race nukes \
        playersOnRadar missilesOnRadar shieldedItemPickup shieldedMining \
        laserIsStunGun} {
        upvar #0 $i j
        if {$j == 0} { $w.$i deselect
        } else { $w.$i select }
    }
    foreach i {edgeWrap edgeBounce extraBorder} {
        upvar #0 $i j
        if {$j == 0} { $w.e.$i deselect
        } else { $w.e.$i select }
    }

    foreach i {gravityLine gravityClockwise gravityAnticlockwise} {
        upvar #0 $i j
        if {$j == 0} { $w.g.$i deselect
        } else { $w.g.$i select }
    }
}
proc setsliders w {
  global shotLife   fireRepeatRate robots robotLeaveLife \
    robotLeaveScore robotLeaveRatio maxPlayerShots \
    itemProbFact    maxNuke FPS time lives

    $w.shotLife.scale set $shotLife
    $w.fireRepeatRate.scale set $fireRepeatRate
    $w.robots.scale set $robots
    $w.robotLeaveLife.scale set $robotLeaveLife
    $w.robotLeaveScore.scale set $robotLeaveScore
    $w.robotLeaveRatio.scale set $robotLeaveRatio
    $w.maxPlayerShots.scale set $maxPlayerShots
    $w.maxNuke.scale set $maxNuke
    $w.lives.scale set $lives
    $w.fps.scale set $FPS
    $w.time.scale set $time

}

proc save_delete_map {{w .mainpanel.save}} {
  global map maps_dir newmap mapWidth mapHeight mapAuthor mapName

    catch {destroy $w}
    toplevel $w
    wm title $w "Save the map"
    wm iconname $w "Save the map"

    frame $w.m
    label $w.m.text1 -text " World : $mapName" -anchor w 
    label $w.m.text2 -text " Author : $mapAuthor" -anchor w
    label $w.m.text3 -text " Size : $mapWidth x  $mapHeight" -anchor w
    pack $w.m.text1 $w.m.text2 $w.m.text3  -side top -fill both -pady 10 -padx 10

    set newmap [string range $map 0 [expr [string first ".map" $map] -1]]
    label $w.label -text "File Name : " -width 11 -anchor w
    entry $w.entry -width 25 -relief sunken -bd 2 -textvariable newmap
    pack $w.m $w.label $w.entry -side top

    bind $w.entry <Return> " "

    button $w.quit -text Cancel -command "destroy $w"
    button $w.ok -text Save -command " existing \"S\" "
    button $w.del -text Delete -command " existing \"D\" "
    pack $w.del $w.ok $w.quit -side left -fill both -pady 10 -padx 10 -expand yes
}

proc existing { VL {w .mainpanel.save}} {
  global map maps_dir newmap default_mapfile

    catch {destroy $w}
    set ex [file exist $maps_dir/$newmap.map]

    if { ($ex!=1) && ($VL=="S") } {
        read_write_exec_mapfile $newmap "W"
	maps_update $newmap.map
        mkmaps .mainpanel.whichmap.map
    } else {
      toplevel $w
      wm title $w "Warning"
      wm iconname $w "Warning"
      frame $w.msg
      if { ($ex==1) && ($VL=="S") } {
        label $w.msg.text1 -text " File \"$newmap\" already existes,"
        label $w.msg.text2 -text "Do you want to continue ?"
      } else {
           if { ($ex==1) && ($VL=="D") } {
	        label $w.msg.text1 -text " Delete file : $newmap "
                label $w.msg.text2 -text "Do you want to continue ?"
           } else {
                label $w.msg.text1 -text " File \"$newmap\" doesn't exist ! "
                label $w.msg.text2 -text " "
           }
        }
        set x 1
        if { $VL=="D"} { set x 0 }
        pack $w.msg.text1 $w.msg.text2 -padx 10 -pady 10 \
            -side top -fill both -expand yes -anchor center
        button $w.ok -text OK -command "
              destroy $w
              if { $x == 1 } {
                   read_write_exec_mapfile $newmap \"W\"
                   maps_update $newmap.map
              } else {
                  eval exec rm -f $maps_dir/$newmap.map
                  maps_update $default_mapfile
              }
              mkmaps .mainpanel.whichmap.map
        "
        button $w.cancel -text Cancel -command "destroy $w"
        pack $w.msg -side top -expand yes -anchor center
        pack $w.ok $w.cancel -side left -fill both -expand yes
    }
}

proc show_warranty {{w .warranty}} {
    catch {destroy $w}
    toplevel $w
    wm title $w "Warranty"
    wm iconname $w "Warranty"

    frame $w.msg
    label $w.msg.text1 -text "(c) 1993 Richard OAK" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100
    label $w.msg.text11 -text "(c) 1994 Yves-Henri BERNE" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100
    label $w.msg.text12 -text "(c) 1994 Ramdane FERHATI" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100
    message $w.msg.text2 -text "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." -font -*-lucida-medium-r-*-*-*-120-*-*-*-*-*-* -width 400

    message $w.msg.text3 -text "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." -font -*-lucida-medium-r-*-*-*-120-*-*-*-*-*-* -width 400

    pack $w.msg.text1 $w.msg.text11 $w.msg.text12 $w.msg.text2 $w.msg.text3 \
         -side top -fill both -expand yes
    button $w.ok -text OK -command "destroy $w"
    pack $w.msg -side top -expand yes -anchor center
    pack $w.ok -side bottom -fill both
}

proc show_copyright {{w .copyright}} {
    catch {destroy $w}
    toplevel $w
    wm title $w "Copyright"
    wm iconname $w "Copyright"

     frame $w.msg
    label $w.msg.text1 -text "(c) 1993 Richard OAK" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100
    label $w.msg.text11 -text "(c) 1994 Yves-Henri BERNE" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100
    label $w.msg.text12 -text "(c) 1994 Ramdane FERHATI" \
          -font -*-lucida-bold-i-*-*-*-140-*-*-*-*-*-* -padx 100

     message $w.msg.text2 -text " This program is free software; you can redistidute 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." -font -*-lucida-medium-r-*-*-*-120-*-*-*-*-*-* -width 400
     message $w.msg.text3 -text "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." -font -*-lucida-medium-r-*-*-*-120-*-*-*-*-*-* -width 400

    pack $w.msg.text1 $w.msg.text11 $w.msg.text12 $w.msg.text2 $w.msg.text3 -side top -fill both
        button $w.ok -text OK -command "destroy $w"
    pack $w.msg -side top -anchor center
    pack $w.ok -side bottom -fill both
}

##################################   Main Program  ################################

frame .mainpanel
if {[tk colormodel .mainpanel] == "color"} {
    set col "gray50"
} else {set col "black"}

frame .mainpanel.whichmap
frame .mainpanel.whichmap.map

mkmaps .mainpanel.whichmap.map

frame .mainpanel.check
mkcheck .mainpanel.check

frame .mainpanel.sliders
mksliders .mainpanel.sliders

pack .mainpanel.whichmap .mainpanel.check .mainpanel.sliders \
    -anchor n -fill both -expand yes -side left

maps_update tkmap.init

frame .buttons
button .buttons.start -text "Start server" -command "read_write_exec_mapfile $map \"E\""
button .buttons.warranty -text "Warranty" -command "show_warranty"
button .buttons.copyright -text "Copyright" -command "show_copyright"
button .buttons.items -text "Items setup" -command "mkitem_probs ; mkitem_other ; mkitem_init"
button .buttons.save -text "Map file" -command "save_delete_map"
button .buttons.meta -text "Meta-server"  -command "get_metaserversort"
button .buttons.join -text "Play the game" -command {

    .buttons.join configure -relief sunken -text Connecting... ;
    update;
    set com "xpilot -join"
    set com [concat $com "-team " $team]
    if {$port > 0} { set com [concat $com "-port " $port] }

    set com [concat $com $server_name]

    eval exec $com &
    destroy .
}

button .buttons.quit -text " Quit " -command {destroy .}

pack .buttons.start .buttons.meta .buttons.items .buttons.save .buttons.copyright  \
         .buttons.warranty .buttons.join .buttons.quit -side left -expand yes -fill both

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

######################################  The End #####################################
