125993Smckusick /*
2*60857Sbostic * Copyright (c) 1980, 1993
3*60857Sbostic * The Regents of the University of California. All rights reserved.
434205Sbostic *
542606Sbostic * %sccs.include.redist.c%
625993Smckusick */
725993Smckusick
811680Smckusick #ifndef lint
9*60857Sbostic static char sccsid[] = "@(#)klmove.c 8.1 (Berkeley) 05/31/93";
1034205Sbostic #endif /* not lint */
1111680Smckusick
1211680Smckusick # include "trek.h"
1311680Smckusick
1411680Smckusick /*
1511680Smckusick ** Move Klingons Around
1611680Smckusick **
1711680Smckusick ** This is a largely incomprehensible block of code that moves
1811680Smckusick ** Klingons around in a quadrant. It was written in a very
1911680Smckusick ** "program as you go" fashion, and is a prime candidate for
2011680Smckusick ** rewriting.
2111680Smckusick **
2211680Smckusick ** The flag `fl' is zero before an attack, one after an attack,
2311680Smckusick ** and two if you are leaving a quadrant. This serves to
2411680Smckusick ** change the probability and distance that it moves.
2511680Smckusick **
2611680Smckusick ** Basically, what it will try to do is to move a certain number
2711680Smckusick ** of steps either toward you or away from you. It will avoid
2811680Smckusick ** stars whenever possible. Nextx and nexty are the next
2911680Smckusick ** sector to move to on a per-Klingon basis; they are roughly
3011680Smckusick ** equivalent to Ship.sectx and Ship.secty for the starship. Lookx and
3111680Smckusick ** looky are the sector that you are going to look at to see
3211680Smckusick ** if you can move their. Dx and dy are the increment. Fudgex
3311680Smckusick ** and fudgey are the things you change around to change your
3411680Smckusick ** course around stars.
3511680Smckusick */
3611680Smckusick
klmove(fl)3711680Smckusick klmove(fl)
3811680Smckusick int fl;
3911680Smckusick {
4011680Smckusick int n;
4111680Smckusick register struct kling *k;
4211680Smckusick double dx, dy;
4311680Smckusick int nextx, nexty;
4411680Smckusick register int lookx, looky;
4511680Smckusick int motion;
4611680Smckusick int fudgex, fudgey;
4711680Smckusick int qx, qy;
4811680Smckusick double bigger;
4911680Smckusick int i;
5011680Smckusick
5111680Smckusick # ifdef xTRACE
5211680Smckusick if (Trace)
5311680Smckusick printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling);
5411680Smckusick # endif
5511680Smckusick for (n = 0; n < Etc.nkling; k && n++)
5611680Smckusick {
5711680Smckusick k = &Etc.klingon[n];
5811680Smckusick i = 100;
5911680Smckusick if (fl)
6011680Smckusick i = 100.0 * k->power / Param.klingpwr;
6111680Smckusick if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl])
6211680Smckusick continue;
6311680Smckusick /* compute distance to move */
6411680Smckusick motion = ranf(75) - 25;
6512738Slayer motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl];
6611680Smckusick /* compute direction */
6711680Smckusick dx = Ship.sectx - k->x + ranf(3) - 1;
6811680Smckusick dy = Ship.secty - k->y + ranf(3) - 1;
6911680Smckusick bigger = dx;
7011680Smckusick if (dy > bigger)
7111680Smckusick bigger = dy;
7211680Smckusick if (bigger == 0.0)
7311680Smckusick bigger = 1.0;
7411680Smckusick dx = dx / bigger + 0.5;
7511680Smckusick dy = dy / bigger + 0.5;
7611680Smckusick if (motion < 0)
7711680Smckusick {
7811680Smckusick motion = -motion;
7911680Smckusick dx = -dx;
8011680Smckusick dy = -dy;
8111680Smckusick }
8211680Smckusick fudgex = fudgey = 1;
8311680Smckusick /* try to move the klingon */
8411680Smckusick nextx = k->x;
8511680Smckusick nexty = k->y;
8611680Smckusick for (; motion > 0; motion--)
8711680Smckusick {
8811680Smckusick lookx = nextx + dx;
8911680Smckusick looky = nexty + dy;
9011680Smckusick if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS)
9111680Smckusick {
9211680Smckusick /* new quadrant */
9311680Smckusick qx = Ship.quadx;
9411680Smckusick qy = Ship.quady;
9511680Smckusick if (lookx < 0)
9612738Slayer qx -= 1;
9711680Smckusick else
9811680Smckusick if (lookx >= NSECTS)
9912738Slayer qx += 1;
10011680Smckusick if (looky < 0)
10112738Slayer qy -= 1;
10211680Smckusick else
10311680Smckusick if (looky >= NSECTS)
10412738Slayer qy += 1;
10511680Smckusick if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS ||
10611680Smckusick Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1)
10711680Smckusick break;
10811680Smckusick if (!damaged(SRSCAN))
10911680Smckusick {
11011680Smckusick printf("Klingon at %d,%d escapes to quadrant %d,%d\n",
11111680Smckusick k->x, k->y, qx, qy);
11211680Smckusick motion = Quad[qx][qy].scanned;
11311680Smckusick if (motion >= 0 && motion < 1000)
11412738Slayer Quad[qx][qy].scanned += 100;
11511680Smckusick motion = Quad[Ship.quadx][Ship.quady].scanned;
11611680Smckusick if (motion >= 0 && motion < 1000)
11712738Slayer Quad[Ship.quadx][Ship.quady].scanned -= 100;
11811680Smckusick }
11911680Smckusick Sect[k->x][k->y] = EMPTY;
12012738Slayer Quad[qx][qy].klings += 1;
12112738Slayer Etc.nkling -= 1;
12211680Smckusick bmove(&Etc.klingon[Etc.nkling], k, sizeof *k);
12312738Slayer Quad[Ship.quadx][Ship.quady].klings -= 1;
12411680Smckusick k = 0;
12511680Smckusick break;
12611680Smckusick }
12711680Smckusick if (Sect[lookx][looky] != EMPTY)
12811680Smckusick {
12911680Smckusick lookx = nextx + fudgex;
13011680Smckusick if (lookx < 0 || lookx >= NSECTS)
13111680Smckusick lookx = nextx + dx;
13211680Smckusick if (Sect[lookx][looky] != EMPTY)
13311680Smckusick {
13411680Smckusick fudgex = -fudgex;
13511680Smckusick looky = nexty + fudgey;
13611680Smckusick if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY)
13711680Smckusick {
13811680Smckusick fudgey = -fudgey;
13911680Smckusick break;
14011680Smckusick }
14111680Smckusick }
14211680Smckusick }
14311680Smckusick nextx = lookx;
14411680Smckusick nexty = looky;
14511680Smckusick }
14611680Smckusick if (k && (k->x != nextx || k->y != nexty))
14711680Smckusick {
14811680Smckusick if (!damaged(SRSCAN))
14911680Smckusick printf("Klingon at %d,%d moves to %d,%d\n",
15011680Smckusick k->x, k->y, nextx, nexty);
15111680Smckusick Sect[k->x][k->y] = EMPTY;
15211680Smckusick Sect[k->x = nextx][k->y = nexty] = KLINGON;
15311680Smckusick }
15411680Smckusick }
15511680Smckusick compkldist(0);
15611680Smckusick }
157