# (c) Copyright 1993, 1994, Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# Permission to use, copy, modify, and distribute this software for
# any purpose and without fee is hereby granted, provided that the above
# copyright notice appear in all copies and that both the copyright notice
# and this permission notice appear in supporting documentation, and that
# the name of Silicon Graphics, Inc. not be used in advertising
# or publicity pertaining to distribution of the software without specific,
# written prior permission.
#
# THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
# AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
# FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
# GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
# SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
# KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
# LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
# THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
# ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
# POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
#
# US Government Users Restricted Rights
# Use, duplication, or disclosure by the Government is subject to
# restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
# (c)(1)(ii) of the Rights in Technical Data and Computer Software
# clause at DFARS 252.227-7013 and/or in similar or successor
# clauses in the FAR or the DOD or NASA FAR Supplement.
# Unpublished-- rights reserved under the copyright laws of the
# United States.  Contractor/manufacturer is Silicon Graphics,
# Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
#
# OpenGL(TM) is a trademark of Silicon Graphics, Inc.
#

proc FishTransform { fishArr ind } {
    upvar 1 $fishArr fish

    glTranslatef $fish($ind,y) $fish($ind,z) [expr -1.0 * $fish($ind,x)]
    glRotatef [expr -1.0 * $fish($ind,psi)] 0.0 1.0 0.0
    glRotatef [expr $fish($ind,theta)] 1.0 0.0 0.0
    glRotatef [expr -1.0 * $fish($ind,phi)] 0.0 0.0 1.0
}

proc WhalePilot { fishArr ind } {
    upvar 1 $fishArr fish

    set fish($ind,phi) -20.0
    set fish($ind,theta)  0.0
    set fish($ind,psi) [expr $fish($ind,psi) - 0.5]

    set fish($ind,x) [expr $fish($ind,x) + $::WHALESPEED * $fish($ind,v) * cos($fish($ind,psi) / $::RAD) * cos($fish($ind,theta) / $::RAD)]
    set fish($ind,y) [expr $fish($ind,y) + $::WHALESPEED * $fish($ind,v) * sin($fish($ind,psi) / $::RAD) * cos($fish($ind,theta) / $::RAD)]
    set fish($ind,z) [expr $fish($ind,z) + $::WHALESPEED * $fish($ind,v) * sin($fish($ind,theta) / $::RAD)]
}

set sign 1

proc SharkPilot { fishArr ind } {
    upvar 1 $fishArr fish

    set fish($ind,xt) 60000.0
    set fish($ind,yt) 0.0
    set fish($ind,zt) 0.0

    set X [expr $fish($ind,xt) - $fish($ind,x)]
    set Y [expr $fish($ind,yt) - $fish($ind,y)]
    set Z [expr $fish($ind,zt) - $fish($ind,z)]

    set thetal $fish($ind,theta)

    set ttheta [expr $::RAD * atan($Z / (sqrt($X * $X + $Y * $Y)))]

    if { $ttheta > [expr $fish($ind,theta) + 0.25] } {
        set fish($ind,theta) [expr $fish($ind,theta) + 0.5]
    } elseif { $ttheta < [expr $fish($ind,theta) - 0.25] } {
        set fish($ind,theta) [expr $fish($ind,theta) - 0.5]
    }
    if { $fish($ind,theta) > 90.0 } {
        set fish($ind,theta) 90.0
    }
    if { $fish($ind,theta) < -90.0 } {
        set fish($ind,theta) -90.0
    }
    set fish($ind,dtheta) [expr $fish($ind,theta) - $thetal]

    set tpsi [expr $::RAD * atan2($Y, $X)]

    set fish($ind,attack) 0

    if { [poMisc:Abs [expr $tpsi - $fish($ind,psi)]] < 10.0 } {
        set fish($ind,attack) 1
    } elseif { [poMisc:Abs [expr $tpsi - $fish($ind,psi)]] < 45.0 } {
        if {$fish($ind,psi) > $tpsi} {
            set fish($ind,psi) [expr $fish($ind,psi) - 0.5]
            if {$fish($ind,psi) < -180.0} {
                set fish($ind,psi) [expr $fish($ind,psi) + 360.0]
            }
        } elseif {$fish($ind,psi) < $tpsi} {
            set fish($ind,psi) [expr $fish($ind,psi) + 0.5]
            if {$fish($ind,psi) > 180.0} {
                set fish($ind,psi) [expr $fish($ind,psi) - 360.0]
            }
        }
    } else {
        if {[expr int (101.0 * rand()) % 100] > 98} {
            set ::sign [expr 1 - $::sign]
        }
        set fish($ind,psi) [expr $fish($ind,psi) + $::sign]
        if {$fish($ind,psi) > 180.0} {
            set fish($ind,psi) [expr $fish($ind,psi) - 360.0]
        }
        if {$fish($ind,psi) < -180.0} {
            set fish($ind,psi) [expr $fish($ind,psi) + 360.0]
        }
    }

    if {$fish($ind,attack)} {
        if {$fish($ind,v) < 1.1} {
            set fish($ind,spurt) 1
        }
        if {$fish($ind,spurt)} {
            set fish($ind,v) [expr $fish($ind,v) + 0.2]
        }
        if {$fish($ind,v) > 5.0} {
            set fish($ind,spurt) 0
        }
        if {($fish($ind,v) > 1.0) && (!$fish($ind,spurt))} {
            set fish($ind,v) [expr $fish($ind,v) - 0.2]
        }
    } else {
        if {![expr int(401.0 * rand()) % 400] && (!$fish($ind,spurt))} {
            set fish($ind,spurt) 1
        }
        if {$fish($ind,spurt)} {
            set fish($ind,v) [expr $fish($ind,v) + 0.05]
        }
        if {$fish($ind,v) > 3.0} {
            set fish($ind,spurt) 0
        }
        if {($fish($ind,v) > 1.0) && (!$fish($ind,spurt))} {
            set fish($ind,v) [expr $fish($ind,v) - 0.05]
        }
    }

    set fish($ind,x) [expr $fish($ind,x) + $::SHARKSPEED * $fish($ind,v) * cos($fish($ind,psi) / $::RAD) * cos($fish($ind,theta) / $::RAD)]
    set fish($ind,y) [expr $fish($ind,y) + $::SHARKSPEED * $fish($ind,v) * sin($fish($ind,psi) / $::RAD) * cos($fish($ind,theta) / $::RAD)]
    set fish($ind,z) [expr $fish($ind,z) + $::SHARKSPEED * $fish($ind,v) * sin($fish($ind,theta) / $::RAD)]
}

proc SharkMiss { fishArr i } {
    upvar 1 $fishArr sharks

    for { set j 0 } { $j < $::NUM_SHARKS } { incr j } {
        if {$j != $i} {
            set X  [expr $sharks($j,x) - $sharks($i,x)]
            set Y  [expr $sharks($j,y) - $sharks($i,y)]
            set Z  [expr $sharks($j,z) - $sharks($i,z)]

            set R [expr sqrt($X * $X + $Y * $Y + $Z * $Z)]

            set avoid  1.0
            set thetal  $sharks($i,theta)

            if {$R < $::SHARKSIZE} {
                if {$Z > 0.0} {
                    set sharks($i,theta) [expr $sharks($i,theta) - $avoid]
                } else {
                    set sharks($i,theta) [expr $sharks($i,theta) + $avoid]
                }
            }
            set sharks($i,dtheta) [expr $sharks($i,dtheta) + ($sharks($i,theta) - $thetal)]
        }
    }
}
