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