xref: /csrg-svn/games/trek/help.c (revision 12344)
111676Smckusick #ifndef lint
2*12344Slayer static char sccsid[] = "@(#)help.c	4.2	(Berkeley)	05/09/83";
311676Smckusick #endif not lint
411676Smckusick 
511676Smckusick # include	"trek.h"
611676Smckusick 
711676Smckusick /*
811676Smckusick **  call starbase for help
911676Smckusick **
1011676Smckusick **	First, the closest starbase is selected.  If there is a
1111676Smckusick **	a starbase in your own quadrant, you are in good shape.
1211676Smckusick **	This distance takes quadrant distances into account only.
1311676Smckusick **
1411676Smckusick **	A magic number is computed based on the distance which acts
1511676Smckusick **	as the probability that you will be rematerialized.  You
1611676Smckusick **	get three tries.
1711676Smckusick **
1811676Smckusick **	When it is determined that you should be able to be remater-
1911676Smckusick **	ialized (i.e., when the probability thing mentioned above
2011676Smckusick **	comes up positive), you are put into that quadrant (anywhere).
2111676Smckusick **	Then, we try to see if there is a spot adjacent to the star-
2211676Smckusick **	base.  If not, you can't be rematerialized!!!  Otherwise,
2311676Smckusick **	it drops you there.  It only tries five times to find a spot
2411676Smckusick **	to drop you.  After that, it's your problem.
2511676Smckusick */
2611676Smckusick 
27*12344Slayer char	*Cntvect[3] =
2811676Smckusick {"first", "second", "third"};
2911676Smckusick 
3011676Smckusick help()
3111676Smckusick {
3211676Smckusick 	register int		i;
3311676Smckusick 	double			dist, x;
3411676Smckusick 	register int		dx, dy;
3511676Smckusick 	int			j, l;
3611676Smckusick 
3711676Smckusick 	/* check to see if calling for help is reasonable ... */
3811676Smckusick 	if (Ship.cond == DOCKED)
3911676Smckusick 		return (printf("Uhura: But Captain, we're already docked\n"));
4011676Smckusick 
4111676Smckusick 	/* or possible */
4211676Smckusick 	if (damaged(SSRADIO))
4311676Smckusick 		return (out(SSRADIO));
4411676Smckusick 	if (Now.bases <= 0)
4511676Smckusick 		return (printf("Uhura: I'm not getting any response from starbase\n"));
4611676Smckusick 
4711676Smckusick 	/* tut tut, there goes the score */
48*12344Slayer 	Game.helps += 1;
4911676Smckusick 
5011676Smckusick 	/* find the closest base */
5111676Smckusick 	dist = 1e50;
5211676Smckusick 	if (Quad[Ship.quadx][Ship.quady].bases <= 0)
5311676Smckusick 	{
5411676Smckusick 		/* there isn't one in this quadrant */
5511676Smckusick 		for (i = 0; i < Now.bases; i++)
5611676Smckusick 		{
5711676Smckusick 			/* compute distance */
5811676Smckusick 			dx = Now.base[i].x - Ship.quadx;
5911676Smckusick 			dy = Now.base[i].y - Ship.quady;
6011676Smckusick 			x = dx * dx + dy * dy;
6111676Smckusick 			x = sqrt(x);
6211676Smckusick 
6311676Smckusick 			/* see if better than what we already have */
6411676Smckusick 			if (x < dist)
6511676Smckusick 			{
6611676Smckusick 				dist = x;
6711676Smckusick 				l = i;
6811676Smckusick 			}
6911676Smckusick 		}
7011676Smckusick 
7111676Smckusick 		/* go to that quadrant */
7211676Smckusick 		Ship.quadx = Now.base[l].x;
7311676Smckusick 		Ship.quady = Now.base[l].y;
7411676Smckusick 		initquad(1);
7511676Smckusick 	}
7611676Smckusick 	else
7711676Smckusick 	{
7811676Smckusick 		dist = 0.0;
7911676Smckusick 	}
8011676Smckusick 
8111676Smckusick 	/* dematerialize the Enterprise */
8211676Smckusick 	Sect[Ship.sectx][Ship.secty] = EMPTY;
8311676Smckusick 	printf("Starbase in %d,%d responds\n", Ship.quadx, Ship.quady);
8411676Smckusick 
8511676Smckusick 	/* this next thing acts as a probability that it will work */
8611676Smckusick 	x = pow(1.0 - pow(0.94, dist), 0.3333333);
8711676Smckusick 
8811676Smckusick 	/* attempt to rematerialize */
8911676Smckusick 	for (i = 0; i < 3; i++)
9011676Smckusick 	{
9111676Smckusick 		sleep(2);
9211676Smckusick 		printf("%s attempt to rematerialize ", Cntvect[i]);
9311676Smckusick 		if (franf() > x)
9411676Smckusick 		{
9511676Smckusick 			/* ok, that's good.  let's see if we can set her down */
9611676Smckusick 			for (j = 0; j < 5; j++)
9711676Smckusick 			{
9811676Smckusick 				dx = Etc.starbase.x + ranf(3) - 1;
9911676Smckusick 				if (dx < 0 || dx >= NSECTS)
10011676Smckusick 					continue;
10111676Smckusick 				dy = Etc.starbase.y + ranf(3) - 1;
10211676Smckusick 				if (dy < 0 || dy >= NSECTS || Sect[dx][dy] != EMPTY)
10311676Smckusick 					continue;
10411676Smckusick 				break;
10511676Smckusick 			}
10611676Smckusick 			if (j < 5)
10711676Smckusick 			{
10811676Smckusick 				/* found an empty spot */
10911676Smckusick 				printf("succeeds\n");
11011676Smckusick 				Ship.sectx = dx;
11111676Smckusick 				Ship.secty = dy;
11211676Smckusick 				Sect[dx][dy] = Ship.ship;
11311676Smckusick 				dock();
11411676Smckusick 				compkldist(0);
11511676Smckusick 				return;
11611676Smckusick 			}
11711676Smckusick 			/* the starbase must have been surrounded */
11811676Smckusick 		}
11911676Smckusick 		printf("fails\n");
12011676Smckusick 	}
12111676Smckusick 
12211676Smckusick 	/* one, two, three strikes, you're out */
12311676Smckusick 	lose(L_NOHELP);
12411676Smckusick }
125