#!/usr/bin/tclsh
# OpenVerse Server Program
# Modified by Andy Goth <unununium@openverse.org>
# Now it runs a GothChess room!
# 
#
# Module Name		- GothChess display procs
# Current Maintainter 	- Andy Goth <unununium@openverse.org>
# Sourced By		- gothchess.tcl
#
# Copyright (C) 1999 David Gale <cruise@openverse.org>
# For more information visit http://OpenVerse.org/
#
# 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.

global chess MVS

# show_button who which
#
# Send $who $which button
proc show_button {who which} {
    global MVS

    # Determine which button to show
    switch -exact -- $which {
	"start" {
	    set msg "MOUSEOVER startstop 520 210 start_default.gif [file size "$MVS(images)/start_default.gif"] start_mouseover.gif [file size "$MVS(images)/start_mouseover.gif"] 0"
	 }
	"start_active" {
	    set msg "MOUSEOVER startstop 520 210 start_active_default.gif [file size "$MVS(images)/start_active_default.gif"] start_active_mouseover.gif [file size "$MVS(images)/start_active_mouseover.gif"] 0"
	}
	"start_disabled" {
	    set msg "MOUSEOVER startstop 520 210 start_disabled.gif [file size "$MVS(images)/start_disabled.gif"] start_disabled.gif [file size "$MVS(images)/start_disabled.gif"] 0"
	}
	"stop" {
	    set msg "MOUSEOVER startstop 520 210 stop_default.gif [file size "$MVS(images)/stop_default.gif"] stop_mouseover.gif [file size "$MVS(images)/stop_mouseover.gif"] 0"
	}
	"stop_active" {
	    set msg "MOUSEOVER startstop 520 210 stop_active_default.gif [file size "$MVS(images)/stop_active_default.gif"] stop_active_mouseover.gif [file size "$MVS(images)/stop_active_mouseover.gif"] 0"
	}
	"exit" {
	    set msg "MOUSEOVER exit 599 210 exit_default.gif [file size "$MVS(images)/exit_default.gif"] exit_mouseover.gif [file size "$MVS(images)/exit_mouseover.gif"] 0"
	}
	"rook" {
	    set msg "MOUSEOVER rook 500 240 rook_default.gif [file size "$MVS(images)/rook_default.gif"] rook_mouseover.gif [file size "$MVS(images)/rook_mouseover.gif"] 0"
	}
	"rook_disabled" {
	    set msg "MOUSEOVER rook 500 240 rook_disabled.gif [file size "$MVS(images)/rook_disabled.gif"] rook_disabled.gif [file size "$MVS(images)/rook_disabled.gif"] 0"
	}
	"knight" {
	    set msg "MOUSEOVER knight 540 240 knight_default.gif [file size "$MVS(images)/knight_default.gif"] knight_mouseover.gif [file size "$MVS(images)/knight_mouseover.gif"] 0"
	}
	"knight_disabled" {
	    set msg "MOUSEOVER knight 540 240 knight_disabled.gif [file size "$MVS(images)/knight_disabled.gif"] knight_disabled.gif [file size "$MVS(images)/knight_disabled.gif"] 0"
	}
	"bishop" {
	    set msg "MOUSEOVER bishop 579 240 bishop_default.gif [file size "$MVS(images)/bishop_default.gif"] bishop_mouseover.gif [file size "$MVS(images)/bishop_mouseover.gif"] 0"
	}
	"bishop_disabled" {
	    set msg "MOUSEOVER bishop 579 240 bishop_disabled.gif [file size "$MVS(images)/bishop_disabled.gif"] bishop_disabled.gif [file size "$MVS(images)/bishop_disabled.gif"] 0"
	}
	"queen" {
	    set msg "MOUSEOVER queen 619 240 queen_default.gif [file size "$MVS(images)/queen_default.gif"] queen_mouseover.gif [file size "$MVS(images)/queen_mouseover.gif"] 0"
	}
	"queen_disabled" {
	    set msg "MOUSEOVER queen 619 240 queen_disabled.gif [file size "$MVS(images)/queen_disabled.gif"] queen_disabled.gif [file size "$MVS(images)/queen_disabled.gif"] 0"
	}
    }

    # Send it!
    if {[string compare $who "all"] == 0} {
	send_to_all_users $msg
    } else {
	send_to_user $who $msg
    }

    unset who which msg
}

# show_message who color message
#
# Send $who $message in $color color
proc show_message {who color message} {
    if {[string compare $who "all"] == 0} {
	send_to_all_users "TEXT message 560 270 $color 2 0 |$message"
    } else {
	send_to_user $who "TEXT message 560 270 $color 2 0 |$message"
    }

    unset who color message
}

# show_names who
#
# Send $who the names of the players
proc show_names {who} {
    global chess

    if {$chess(players.1.is_missing) == 1} {
	set player_1 "yellow 2 0 |($chess(players.1.name))"
    } elseif {$chess(players.1.sock) == 0} {
	set player_1 "red 2 0 |Waiting for player 1..."
    } else {
	set player_1 "white 2 0 |$chess(players.1.name)"
    }

    if {$chess(players.2.is_missing) == 1} {
	set player_2 "yellow 2 0 |($chess(players.2.name))"
    } elseif {$chess(players.2.sock) == 0} {
	set player_2 "red 2 0 |Waiting for player 2..."
    } else {
	set player_2 "white 2 0 |$chess(players.2.name)"
    }

    if {[string compare $who "all"] == 0} {
	send_to_all_users "TEXT player_1_name 560 170 $player_1"
	send_to_all_users "TEXT player_2_name 560 310 $player_2"
    } else {
	send_to_user $who "TEXT player_1_name 560 170 $player_1"
	send_to_user $who "TEXT player_2_name 560 310 $player_2"
    }

    unset who player_1 player_2
}

# show_active_player who
#
# Indicates to $who which player is active
proc show_active_player {who} {
    global chess MVS

    # Decide what images to show
    if {$chess(turn) != 1} {
	set msg_1 "IMAGE bar_name_1 560 170 panel_bar.gif [file size "$MVS(images)/panel_bar.gif"] 0"
    } else {
	set msg_1 "IMAGE bar_name_1 560 170 active_panel_bar.gif [file size "$MVS(images)/active_panel_bar.gif"] 0"
    }
    if {$chess(turn) != 2} {
	set msg_2 "IMAGE bar_name_2 560 310 panel_bar.gif [file size "$MVS(images)/panel_bar.gif"] 0"
    } else {
	set msg_2 "IMAGE bar_name_2 560 310 active_panel_bar.gif [file size "$MVS(images)/active_panel_bar.gif"] 0"
    }

    # Send it!
    if {[string compare $who "all"] == 0} {
	send_to_all_users $msg_1
	send_to_all_users $msg_2
    } else {
	send_to_user $who $msg_1
	send_to_user $who $msg_2
    }

    unset who msg_1 msg_2
}

# initialize_screen who player
#
# Send $who all the objects in the game screen
proc initialize_screen {who} {
    global MVS chess

    # Disallow movement
    send_to_user $who "TELL no_move_zone_1 0 480 641 481 0"
    send_to_user $who "TELL no_move_zone_2 480 0 641 480 0"

    # Send mouseovers and tells for each square in the board
    set color "light"
    for {set y 0} {$y < 8} {incr y} {
	for {set x 0} {$x < 8} {incr x} {
	    set loc "$x"
	    append loc "_$y"

	    if {[expr ($x & 1) ^ ($y & 1)] == 0} {
		set img "light_default.gif"
	    } else {
		set img "dark_default.gif"
	    }

	    send_to_user $who "IMAGE square_$loc [expr $x * 60 + 30] [expr $y * 60 + 30] $img [file size "$MVS(images)/$img"] 0"
	    send_to_user $who "TELL tell_square_$loc [expr $x * 60] [expr $y * 60] [expr $x * 60 + 59] [expr $y * 60 + 59] 0"
  	}
    }

    # Send tells for everything in the panel
    send_to_user $who "TELL tell_startstop 481 201 558 219 0"
    send_to_user $who "TELL tell_exit 560 201 638 219 0"
    send_to_user $who "TELL tell_rook 481 221 519 259 0"
    send_to_user $who "TELL tell_knight 521 221 558 259 0"
    send_to_user $who "TELL tell_bishop 560 221 598 259 0"
    send_to_user $who "TELL tell_queen 600 221 638 259 0"

    # Send the button objects
    show_button $who "exit"
    show_button $who "rook_disabled"
    show_button $who "knight_disabled"
    show_button $who "bishop_disabled"
    show_button $who "queen_disabled"

    # Show the various panel bars
    show_active_player $who
    send_to_user $who "IMAGE clock_bar_1 560 190 panel_bar.gif [file size "$MVS(images)/panel_bar.gif"] 0"
    send_to_user $who "IMAGE message_bar 560 270 panel_bar.gif [file size "$MVS(images)/panel_bar.gif"] 0"
    send_to_user $who "IMAGE clock_bar_2 560 290 panel_bar.gif [file size "$MVS(images)/panel_bar.gif"] 0"

    # Show the squares for the players
    send_to_user $who "IMAGE player_1_box 560 80 player_1_panel.gif [file size "$MVS(images)/player_1_panel.gif"] 0"
    send_to_user $who "IMAGE player_2_box 560 400 player_2_panel.gif [file size "$MVS(images)/player_2_panel.gif"] 0"

    # Put the players on top of all this mess
    send_to_user $who "ABOVE $chess(players.[name_of $who].name)"
    if {$MVS(users) == 2} {
	send_to_user $who "ABOVE $chess(players.[expr 3 - [name_of $who]].name)"
    }

    # Greet the new guy
    if {$chess(turn) == 0} {
	show_message $who "green" "Welcome to GothChess!"
    } elseif {$chess(turn) == [name_of $who]} {
	show_message $who "green" "Welcome; it's your turn!"
    } else {
	show_message $who "green" "Welcome back to the game"
    }

    unset who x y color loc img
}

# show_all_pieces who
#
# Show $who all the pieces on the board
proc show_all_pieces {who} {
    global chess MVS

    for {set owner 1} {$owner < 3} {incr owner} {
	for {set index 0} {$index < 16} {incr index} {
	    set name "piece_$owner"
	    append name "_$index"
	    if {$chess(pieces.$owner.$index.in_play) == 1} {
		# The piece is on the board; show it on its square
		set msg "IMAGE $name [expr $chess(pieces.$owner.$index.x) * 60 + 30] [expr $chess(pieces.$owner.$index.y) * 60 + 30] $chess(pieces.$owner.$index.img) [file size "$MVS(images)/$chess(pieces.$owner.$index.img)"] 0"
	    } else {
		# The piece has been taken; show it under the player who took it
		set msg "IMAGE $name [expr $chess(pieces.$owner.$index.x) * 39 + 500] [expr $chess(pieces.$owner.$index.y) * 39 + 19 + (2 - $owner) * 320] $chess(pieces.$owner.$index.img) [file size "$MVS(images)/$chess(pieces.$owner.$index.img)"] 0"
	    }
	    # Send it!
	    if {[string compare $who "all"] == 0} {
		send_to_all_users $msg
	    } else {
		send_to_user $who $msg
	    }
	}
    }

    unset who owner index name msg
}

# delete_all_pieces who
#
# Delete all of the pieces for $who
proc delete_all_pieces {who} {
    global MVS

    for {set owner 1} {$owner < 3} {incr owner} {
	for {set index 0} {$index < 16} {incr index} {
	    set name "piece_$owner"
	    append name "_$index"
	    set msg "IMAGE $name 0 0 blank.gif [file size "$MVS(images)/blank.gif"] 1"

	    # Send it!
	    if {[string compare $who "all"] == 0} {
		send_to_all_users $msg
	    } else {
		send_to_user $who $msg
	    }
	}
    }

    unset who owner index name msg
}
