Xcode/TemplatesForXcodeTiger/SDL OpenGL Application/atlantis/swim.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Sep 2011 14:44:37 -0400
branchSDL-1.2
changeset 5895 402eac854030
permissions -rw-r--r--
Unpacked Xcode.tar.gz.
icculus@5895
     1
/**
icculus@5895
     2
 * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
icculus@5895
     3
 * ALL RIGHTS RESERVED
icculus@5895
     4
 * Permission to use, copy, modify, and distribute this software for
icculus@5895
     5
 * any purpose and without fee is hereby granted, provided that the above
icculus@5895
     6
 * copyright notice appear in all copies and that both the copyright notice
icculus@5895
     7
 * and this permission notice appear in supporting documentation, and that
icculus@5895
     8
 * the name of Silicon Graphics, Inc. not be used in advertising
icculus@5895
     9
 * or publicity pertaining to distribution of the software without specific,
icculus@5895
    10
 * written prior permission.
icculus@5895
    11
 *
icculus@5895
    12
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
icculus@5895
    13
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
icculus@5895
    14
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
icculus@5895
    15
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
icculus@5895
    16
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
icculus@5895
    17
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
icculus@5895
    18
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
icculus@5895
    19
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
icculus@5895
    20
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
icculus@5895
    21
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
icculus@5895
    22
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
icculus@5895
    23
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
icculus@5895
    24
 *
icculus@5895
    25
 * US Government Users Restricted Rights
icculus@5895
    26
 * Use, duplication, or disclosure by the Government is subject to
icculus@5895
    27
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
icculus@5895
    28
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
icculus@5895
    29
 * clause at DFARS 252.227-7013 and/or in similar or successor
icculus@5895
    30
 * clauses in the FAR or the DOD or NASA FAR Supplement.
icculus@5895
    31
 * Unpublished-- rights reserved under the copyright laws of the
icculus@5895
    32
 * United States.  Contractor/manufacturer is Silicon Graphics,
icculus@5895
    33
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
icculus@5895
    34
 *
icculus@5895
    35
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
icculus@5895
    36
 */
icculus@5895
    37
#include <math.h>
icculus@5895
    38
#include <stdlib.h>  /* For rand(). */
icculus@5895
    39
#include <GLUT/glut.h>
icculus@5895
    40
#include "atlantis.h"
icculus@5895
    41
icculus@5895
    42
void
icculus@5895
    43
FishTransform(fishRec * fish)
icculus@5895
    44
{
icculus@5895
    45
icculus@5895
    46
    glTranslatef(fish->y, fish->z, -fish->x);
icculus@5895
    47
    glRotatef(-fish->psi, 0.0, 1.0, 0.0);
icculus@5895
    48
    glRotatef(fish->theta, 1.0, 0.0, 0.0);
icculus@5895
    49
    glRotatef(-fish->phi, 0.0, 0.0, 1.0);
icculus@5895
    50
}
icculus@5895
    51
icculus@5895
    52
void
icculus@5895
    53
WhalePilot(fishRec * fish)
icculus@5895
    54
{
icculus@5895
    55
icculus@5895
    56
    fish->phi = -20.0;
icculus@5895
    57
    fish->theta = 0.0;
icculus@5895
    58
    fish->psi -= 0.5;
icculus@5895
    59
icculus@5895
    60
    fish->x += WHALESPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
icculus@5895
    61
    fish->y += WHALESPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
icculus@5895
    62
    fish->z += WHALESPEED * fish->v * sin(fish->theta / RAD);
icculus@5895
    63
}
icculus@5895
    64
icculus@5895
    65
void
icculus@5895
    66
SharkPilot(fishRec * fish)
icculus@5895
    67
{
icculus@5895
    68
    static int sign = 1;
icculus@5895
    69
    float X, Y, Z, tpsi, ttheta, thetal;
icculus@5895
    70
icculus@5895
    71
    fish->xt = 60000.0;
icculus@5895
    72
    fish->yt = 0.0;
icculus@5895
    73
    fish->zt = 0.0;
icculus@5895
    74
icculus@5895
    75
    X = fish->xt - fish->x;
icculus@5895
    76
    Y = fish->yt - fish->y;
icculus@5895
    77
    Z = fish->zt - fish->z;
icculus@5895
    78
icculus@5895
    79
    thetal = fish->theta;
icculus@5895
    80
icculus@5895
    81
    ttheta = RAD * atan(Z / (sqrt(X * X + Y * Y)));
icculus@5895
    82
icculus@5895
    83
    if (ttheta > fish->theta + 0.25) {
icculus@5895
    84
        fish->theta += 0.5;
icculus@5895
    85
    } else if (ttheta < fish->theta - 0.25) {
icculus@5895
    86
        fish->theta -= 0.5;
icculus@5895
    87
    }
icculus@5895
    88
    if (fish->theta > 90.0) {
icculus@5895
    89
        fish->theta = 90.0;
icculus@5895
    90
    }
icculus@5895
    91
    if (fish->theta < -90.0) {
icculus@5895
    92
        fish->theta = -90.0;
icculus@5895
    93
    }
icculus@5895
    94
    fish->dtheta = fish->theta - thetal;
icculus@5895
    95
icculus@5895
    96
    tpsi = RAD * atan2(Y, X);
icculus@5895
    97
icculus@5895
    98
    fish->attack = 0;
icculus@5895
    99
icculus@5895
   100
    if (fabs(tpsi - fish->psi) < 10.0) {
icculus@5895
   101
        fish->attack = 1;
icculus@5895
   102
    } else if (fabs(tpsi - fish->psi) < 45.0) {
icculus@5895
   103
        if (fish->psi > tpsi) {
icculus@5895
   104
            fish->psi -= 0.5;
icculus@5895
   105
            if (fish->psi < -180.0) {
icculus@5895
   106
                fish->psi += 360.0;
icculus@5895
   107
            }
icculus@5895
   108
        } else if (fish->psi < tpsi) {
icculus@5895
   109
            fish->psi += 0.5;
icculus@5895
   110
            if (fish->psi > 180.0) {
icculus@5895
   111
                fish->psi -= 360.0;
icculus@5895
   112
            }
icculus@5895
   113
        }
icculus@5895
   114
    } else {
icculus@5895
   115
        if (rand() % 100 > 98) {
icculus@5895
   116
            sign = 1 - sign;
icculus@5895
   117
        }
icculus@5895
   118
        fish->psi += sign;
icculus@5895
   119
        if (fish->psi > 180.0) {
icculus@5895
   120
            fish->psi -= 360.0;
icculus@5895
   121
        }
icculus@5895
   122
        if (fish->psi < -180.0) {
icculus@5895
   123
            fish->psi += 360.0;
icculus@5895
   124
        }
icculus@5895
   125
    }
icculus@5895
   126
icculus@5895
   127
    if (fish->attack) {
icculus@5895
   128
        if (fish->v < 1.1) {
icculus@5895
   129
            fish->spurt = 1;
icculus@5895
   130
        }
icculus@5895
   131
        if (fish->spurt) {
icculus@5895
   132
            fish->v += 0.2;
icculus@5895
   133
        }
icculus@5895
   134
        if (fish->v > 5.0) {
icculus@5895
   135
            fish->spurt = 0;
icculus@5895
   136
        }
icculus@5895
   137
        if ((fish->v > 1.0) && (!fish->spurt)) {
icculus@5895
   138
            fish->v -= 0.2;
icculus@5895
   139
        }
icculus@5895
   140
    } else {
icculus@5895
   141
        if (!(rand() % 400) && (!fish->spurt)) {
icculus@5895
   142
            fish->spurt = 1;
icculus@5895
   143
        }
icculus@5895
   144
        if (fish->spurt) {
icculus@5895
   145
            fish->v += 0.05;
icculus@5895
   146
        }
icculus@5895
   147
        if (fish->v > 3.0) {
icculus@5895
   148
            fish->spurt = 0;
icculus@5895
   149
        }
icculus@5895
   150
        if ((fish->v > 1.0) && (!fish->spurt)) {
icculus@5895
   151
            fish->v -= 0.05;
icculus@5895
   152
        }
icculus@5895
   153
    }
icculus@5895
   154
icculus@5895
   155
    fish->x += SHARKSPEED * fish->v * cos(fish->psi / RAD) * cos(fish->theta / RAD);
icculus@5895
   156
    fish->y += SHARKSPEED * fish->v * sin(fish->psi / RAD) * cos(fish->theta / RAD);
icculus@5895
   157
    fish->z += SHARKSPEED * fish->v * sin(fish->theta / RAD);
icculus@5895
   158
}
icculus@5895
   159
icculus@5895
   160
void
icculus@5895
   161
SharkMiss(int i)
icculus@5895
   162
{
icculus@5895
   163
    int j;
icculus@5895
   164
    float avoid, thetal;
icculus@5895
   165
    float X, Y, Z, R;
icculus@5895
   166
icculus@5895
   167
    for (j = 0; j < NUM_SHARKS; j++) {
icculus@5895
   168
        if (j != i) {
icculus@5895
   169
            X = sharks[j].x - sharks[i].x;
icculus@5895
   170
            Y = sharks[j].y - sharks[i].y;
icculus@5895
   171
            Z = sharks[j].z - sharks[i].z;
icculus@5895
   172
icculus@5895
   173
            R = sqrt(X * X + Y * Y + Z * Z);
icculus@5895
   174
icculus@5895
   175
            avoid = 1.0;
icculus@5895
   176
            thetal = sharks[i].theta;
icculus@5895
   177
icculus@5895
   178
            if (R < SHARKSIZE) {
icculus@5895
   179
                if (Z > 0.0) {
icculus@5895
   180
                    sharks[i].theta -= avoid;
icculus@5895
   181
                } else {
icculus@5895
   182
                    sharks[i].theta += avoid;
icculus@5895
   183
                }
icculus@5895
   184
            }
icculus@5895
   185
            sharks[i].dtheta += (sharks[i].theta - thetal);
icculus@5895
   186
        }
icculus@5895
   187
    }
icculus@5895
   188
}