xref: /original-bsd/games/trek/help.c (revision c829ecf6)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)help.c	5.4 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 # include	"trek.h"
13 
14 /*
15 **  call starbase for help
16 **
17 **	First, the closest starbase is selected.  If there is a
18 **	a starbase in your own quadrant, you are in good shape.
19 **	This distance takes quadrant distances into account only.
20 **
21 **	A magic number is computed based on the distance which acts
22 **	as the probability that you will be rematerialized.  You
23 **	get three tries.
24 **
25 **	When it is determined that you should be able to be remater-
26 **	ialized (i.e., when the probability thing mentioned above
27 **	comes up positive), you are put into that quadrant (anywhere).
28 **	Then, we try to see if there is a spot adjacent to the star-
29 **	base.  If not, you can't be rematerialized!!!  Otherwise,
30 **	it drops you there.  It only tries five times to find a spot
31 **	to drop you.  After that, it's your problem.
32 */
33 
34 char	*Cntvect[3] =
35 {"first", "second", "third"};
36 
37 help()
38 {
39 	register int		i;
40 	double			dist, x;
41 	register int		dx, dy;
42 	int			j, l;
43 
44 	/* check to see if calling for help is reasonable ... */
45 	if (Ship.cond == DOCKED)
46 		return (printf("Uhura: But Captain, we're already docked\n"));
47 
48 	/* or possible */
49 	if (damaged(SSRADIO))
50 		return (out(SSRADIO));
51 	if (Now.bases <= 0)
52 		return (printf("Uhura: I'm not getting any response from starbase\n"));
53 
54 	/* tut tut, there goes the score */
55 	Game.helps += 1;
56 
57 	/* find the closest base */
58 	dist = 1e50;
59 	if (Quad[Ship.quadx][Ship.quady].bases <= 0)
60 	{
61 		/* there isn't one in this quadrant */
62 		for (i = 0; i < Now.bases; i++)
63 		{
64 			/* compute distance */
65 			dx = Now.base[i].x - Ship.quadx;
66 			dy = Now.base[i].y - Ship.quady;
67 			x = dx * dx + dy * dy;
68 			x = sqrt(x);
69 
70 			/* see if better than what we already have */
71 			if (x < dist)
72 			{
73 				dist = x;
74 				l = i;
75 			}
76 		}
77 
78 		/* go to that quadrant */
79 		Ship.quadx = Now.base[l].x;
80 		Ship.quady = Now.base[l].y;
81 		initquad(1);
82 	}
83 	else
84 	{
85 		dist = 0.0;
86 	}
87 
88 	/* dematerialize the Enterprise */
89 	Sect[Ship.sectx][Ship.secty] = EMPTY;
90 	printf("Starbase in %d,%d responds\n", Ship.quadx, Ship.quady);
91 
92 	/* this next thing acts as a probability that it will work */
93 	x = pow(1.0 - pow(0.94, dist), 0.3333333);
94 
95 	/* attempt to rematerialize */
96 	for (i = 0; i < 3; i++)
97 	{
98 		sleep(2);
99 		printf("%s attempt to rematerialize ", Cntvect[i]);
100 		if (franf() > x)
101 		{
102 			/* ok, that's good.  let's see if we can set her down */
103 			for (j = 0; j < 5; j++)
104 			{
105 				dx = Etc.starbase.x + ranf(3) - 1;
106 				if (dx < 0 || dx >= NSECTS)
107 					continue;
108 				dy = Etc.starbase.y + ranf(3) - 1;
109 				if (dy < 0 || dy >= NSECTS || Sect[dx][dy] != EMPTY)
110 					continue;
111 				break;
112 			}
113 			if (j < 5)
114 			{
115 				/* found an empty spot */
116 				printf("succeeds\n");
117 				Ship.sectx = dx;
118 				Ship.secty = dy;
119 				Sect[dx][dy] = Ship.ship;
120 				dock();
121 				compkldist(0);
122 				return;
123 			}
124 			/* the starbase must have been surrounded */
125 		}
126 		printf("fails\n");
127 	}
128 
129 	/* one, two, three strikes, you're out */
130 	lose(L_NOHELP);
131 }
132