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