Home | History | Annotate | Line # | Download | only in trek
klmove.c revision 1.3
      1  1.3  cgd /*	$NetBSD: klmove.c,v 1.3 1995/04/22 10:59:07 cgd Exp $	*/
      2  1.3  cgd 
      3  1.1  cgd /*
      4  1.3  cgd  * Copyright (c) 1980, 1993
      5  1.3  cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1  cgd  *
      7  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1  cgd  * modification, are permitted provided that the following conditions
      9  1.1  cgd  * are met:
     10  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1  cgd  *    must display the following acknowledgement:
     17  1.1  cgd  *	This product includes software developed by the University of
     18  1.1  cgd  *	California, Berkeley and its contributors.
     19  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  cgd  *    may be used to endorse or promote products derived from this software
     21  1.1  cgd  *    without specific prior written permission.
     22  1.1  cgd  *
     23  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  cgd  * SUCH DAMAGE.
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #ifndef lint
     37  1.3  cgd #if 0
     38  1.3  cgd static char sccsid[] = "@(#)klmove.c	8.1 (Berkeley) 5/31/93";
     39  1.3  cgd #else
     40  1.3  cgd static char rcsid[] = "$NetBSD: klmove.c,v 1.3 1995/04/22 10:59:07 cgd Exp $";
     41  1.3  cgd #endif
     42  1.1  cgd #endif /* not lint */
     43  1.1  cgd 
     44  1.1  cgd # include	"trek.h"
     45  1.1  cgd 
     46  1.1  cgd /*
     47  1.1  cgd **  Move Klingons Around
     48  1.1  cgd **
     49  1.1  cgd **	This is a largely incomprehensible block of code that moves
     50  1.1  cgd **	Klingons around in a quadrant.  It was written in a very
     51  1.1  cgd **	"program as you go" fashion, and is a prime candidate for
     52  1.1  cgd **	rewriting.
     53  1.1  cgd **
     54  1.1  cgd **	The flag `fl' is zero before an attack, one after an attack,
     55  1.1  cgd **	and two if you are leaving a quadrant.  This serves to
     56  1.1  cgd **	change the probability and distance that it moves.
     57  1.1  cgd **
     58  1.1  cgd **	Basically, what it will try to do is to move a certain number
     59  1.1  cgd **	of steps either toward you or away from you.  It will avoid
     60  1.1  cgd **	stars whenever possible.  Nextx and nexty are the next
     61  1.1  cgd **	sector to move to on a per-Klingon basis; they are roughly
     62  1.1  cgd **	equivalent to Ship.sectx and Ship.secty for the starship.  Lookx and
     63  1.1  cgd **	looky are the sector that you are going to look at to see
     64  1.1  cgd **	if you can move their.  Dx and dy are the increment.  Fudgex
     65  1.1  cgd **	and fudgey are the things you change around to change your
     66  1.1  cgd **	course around stars.
     67  1.1  cgd */
     68  1.1  cgd 
     69  1.1  cgd klmove(fl)
     70  1.1  cgd int	fl;
     71  1.1  cgd {
     72  1.1  cgd 	int			n;
     73  1.1  cgd 	register struct kling	*k;
     74  1.1  cgd 	double			dx, dy;
     75  1.1  cgd 	int			nextx, nexty;
     76  1.1  cgd 	register int		lookx, looky;
     77  1.1  cgd 	int			motion;
     78  1.1  cgd 	int			fudgex, fudgey;
     79  1.1  cgd 	int			qx, qy;
     80  1.1  cgd 	double			bigger;
     81  1.1  cgd 	int			i;
     82  1.1  cgd 
     83  1.1  cgd #	ifdef xTRACE
     84  1.1  cgd 	if (Trace)
     85  1.1  cgd 		printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
     86  1.1  cgd #	endif
     87  1.1  cgd 	for (n = 0; n < Etc.nkling; k && n++)
     88  1.1  cgd 	{
     89  1.1  cgd 		k = &Etc.klingon[n];
     90  1.1  cgd 		i = 100;
     91  1.1  cgd 		if (fl)
     92  1.1  cgd 			i = 100.0 * k->power / Param.klingpwr;
     93  1.1  cgd 		if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
     94  1.1  cgd 			continue;
     95  1.1  cgd 		/* compute distance to move */
     96  1.1  cgd 		motion = ranf(75) - 25;
     97  1.1  cgd 		motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
     98  1.1  cgd 		/* compute direction */
     99  1.1  cgd 		dx = Ship.sectx - k->x + ranf(3) - 1;
    100  1.1  cgd 		dy = Ship.secty - k->y + ranf(3) - 1;
    101  1.1  cgd 		bigger = dx;
    102  1.1  cgd 		if (dy > bigger)
    103  1.1  cgd 			bigger = dy;
    104  1.1  cgd 		if (bigger == 0.0)
    105  1.1  cgd 			bigger = 1.0;
    106  1.1  cgd 		dx = dx / bigger + 0.5;
    107  1.1  cgd 		dy = dy / bigger + 0.5;
    108  1.1  cgd 		if (motion < 0)
    109  1.1  cgd 		{
    110  1.1  cgd 			motion = -motion;
    111  1.1  cgd 			dx = -dx;
    112  1.1  cgd 			dy = -dy;
    113  1.1  cgd 		}
    114  1.1  cgd 		fudgex = fudgey = 1;
    115  1.1  cgd 		/* try to move the klingon */
    116  1.1  cgd 		nextx = k->x;
    117  1.1  cgd 		nexty = k->y;
    118  1.1  cgd 		for (; motion > 0; motion--)
    119  1.1  cgd 		{
    120  1.1  cgd 			lookx = nextx + dx;
    121  1.1  cgd 			looky = nexty + dy;
    122  1.1  cgd 			if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
    123  1.1  cgd 			{
    124  1.1  cgd 				/* new quadrant */
    125  1.1  cgd 				qx = Ship.quadx;
    126  1.1  cgd 				qy = Ship.quady;
    127  1.1  cgd 				if (lookx < 0)
    128  1.1  cgd 					qx -= 1;
    129  1.1  cgd 				else
    130  1.1  cgd 					if (lookx >= NSECTS)
    131  1.1  cgd 						qx += 1;
    132  1.1  cgd 				if (looky < 0)
    133  1.1  cgd 					qy -= 1;
    134  1.1  cgd 				else
    135  1.1  cgd 					if (looky >= NSECTS)
    136  1.1  cgd 						qy += 1;
    137  1.1  cgd 				if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
    138  1.1  cgd 						Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
    139  1.1  cgd 					break;
    140  1.1  cgd 				if (!damaged(SRSCAN))
    141  1.1  cgd 				{
    142  1.1  cgd 					printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
    143  1.1  cgd 						k->x, k->y, qx, qy);
    144  1.1  cgd 					motion = Quad[qx][qy].scanned;
    145  1.1  cgd 					if (motion >= 0 && motion < 1000)
    146  1.1  cgd 						Quad[qx][qy].scanned += 100;
    147  1.1  cgd 					motion = Quad[Ship.quadx][Ship.quady].scanned;
    148  1.1  cgd 					if (motion >= 0 && motion < 1000)
    149  1.1  cgd 						Quad[Ship.quadx][Ship.quady].scanned -= 100;
    150  1.1  cgd 				}
    151  1.1  cgd 				Sect[k->x][k->y] = EMPTY;
    152  1.1  cgd 				Quad[qx][qy].klings += 1;
    153  1.1  cgd 				Etc.nkling -= 1;
    154  1.1  cgd 				bmove(&Etc.klingon[Etc.nkling], k, sizeof *k);
    155  1.1  cgd 				Quad[Ship.quadx][Ship.quady].klings -= 1;
    156  1.1  cgd 				k = 0;
    157  1.1  cgd 				break;
    158  1.1  cgd 			}
    159  1.1  cgd 			if (Sect[lookx][looky] != EMPTY)
    160  1.1  cgd 			{
    161  1.1  cgd 				lookx = nextx + fudgex;
    162  1.1  cgd 				if (lookx < 0 || lookx >= NSECTS)
    163  1.1  cgd 					lookx = nextx + dx;
    164  1.1  cgd 				if (Sect[lookx][looky] != EMPTY)
    165  1.1  cgd 				{
    166  1.1  cgd 					fudgex = -fudgex;
    167  1.1  cgd 					looky = nexty + fudgey;
    168  1.1  cgd 					if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
    169  1.1  cgd 					{
    170  1.1  cgd 						fudgey = -fudgey;
    171  1.1  cgd 						break;
    172  1.1  cgd 					}
    173  1.1  cgd 				}
    174  1.1  cgd 			}
    175  1.1  cgd 			nextx = lookx;
    176  1.1  cgd 			nexty = looky;
    177  1.1  cgd 		}
    178  1.1  cgd 		if (k && (k->x != nextx || k->y != nexty))
    179  1.1  cgd 		{
    180  1.1  cgd 			if (!damaged(SRSCAN))
    181  1.1  cgd 				printf("Klingon at %d,%d moves to %d,%d\n",
    182  1.1  cgd 					k->x, k->y, nextx, nexty);
    183  1.1  cgd 			Sect[k->x][k->y] = EMPTY;
    184  1.1  cgd 			Sect[k->x = nextx][k->y = nexty] = KLINGON;
    185  1.1  cgd 		}
    186  1.1  cgd 	}
    187  1.1  cgd 	compkldist(0);
    188  1.1  cgd }
    189