xref: /original-bsd/games/trek/snova.c (revision 262b24ac)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)snova.c	5.3 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 # include	"trek.h"
23 
24 /*
25 **  CAUSE SUPERNOVA TO OCCUR
26 **
27 **	A supernova occurs.  If 'ix' < 0, a random quadrant is chosen;
28 **	otherwise, the current quadrant is taken, and (ix, iy) give
29 **	the sector quadrants of the star which is blowing up.
30 **
31 **	If the supernova turns out to be in the quadrant you are in,
32 **	you go into "emergency override mode", which tries to get you
33 **	out of the quadrant as fast as possible.  However, if you
34 **	don't have enough fuel, or if you by chance run into something,
35 **	or some such thing, you blow up anyway.  Oh yeh, if you are
36 **	within two sectors of the star, there is nothing that can
37 **	be done for you.
38 **
39 **	When a star has gone supernova, the quadrant becomes uninhab-
40 **	itable for the rest of eternity, i.e., the game.  If you ever
41 **	try stopping in such a quadrant, you will go into emergency
42 **	override mode.
43 */
44 
45 snova(x, y)
46 int	x, y;
47 {
48 	int			qx, qy;
49 	register int		ix, iy;
50 	int			f;
51 	int			dx, dy;
52 	int			n;
53 	register struct quad	*q;
54 
55 	f = 0;
56 	ix = x;
57 	if (ix < 0)
58 	{
59 		/* choose a quadrant */
60 		while (1)
61 		{
62 			qx = ranf(NQUADS);
63 			qy = ranf(NQUADS);
64 			q = &Quad[qx][qy];
65 			if (q->stars > 0)
66 				break;
67 		}
68 		if (Ship.quadx == qx && Ship.quady == qy)
69 		{
70 			/* select a particular star */
71 			n = ranf(q->stars);
72 			for (ix = 0; ix < NSECTS; ix++)
73 			{
74 				for (iy = 0; iy < NSECTS; iy++)
75 					if (Sect[ix][iy] == STAR || Sect[ix][iy] == INHABIT)
76 						if ((n -= 1) <= 0)
77 							break;
78 				if (n <= 0)
79 					break;
80 			}
81 			f = 1;
82 		}
83 	}
84 	else
85 	{
86 		/* current quadrant */
87 		iy = y;
88 		qx = Ship.quadx;
89 		qy = Ship.quady;
90 		q = &Quad[qx][qy];
91 		f = 1;
92 	}
93 	if (f)
94 	{
95 		/* supernova is in same quadrant as Enterprise */
96 		printf("\nRED ALERT: supernova occuring at %d,%d\n", ix, iy);
97 		dx = ix - Ship.sectx;
98 		dy = iy - Ship.secty;
99 		if (dx * dx + dy * dy <= 2)
100 		{
101 			printf("***  Emergency override attem");
102 			sleep(1);
103 			printf("\n");
104 			lose(L_SNOVA);
105 		}
106 		q->scanned = 1000;
107 	}
108 	else
109 	{
110 		if (!damaged(SSRADIO))
111 		{
112 			q->scanned = 1000;
113 			printf("\nUhura: Captain, Starfleet Command reports a supernova\n");
114 			printf("  in quadrant %d,%d.  Caution is advised\n", qx, qy);
115 		}
116 	}
117 
118 	/* clear out the supernova'ed quadrant */
119 	dx = q->klings;
120 	dy = q->stars;
121 	Now.klings -= dx;
122 	if (x >= 0)
123 	{
124 		/* Enterprise caused supernova */
125 		Game.kills += dy;
126 		if (q->bases)
127 			killb(qx, qy, -1);
128 		Game.killk += dx;
129 	}
130 	else
131 		if (q->bases)
132 			killb(qx, qy, 0);
133 	killd(qx, qy, (x >= 0));
134 	q->stars = -1;
135 	q->klings = 0;
136 	if (Now.klings <= 0)
137 	{
138 		printf("Lucky devil, that supernova destroyed the last klingon\n");
139 		win();
140 	}
141 	return;
142 }
143