1*32cded6cSdholland /* $NetBSD: snova.c,v 1.10 2018/02/08 09:05:16 dholland Exp $ */
2887dd216Scgd
361f28255Scgd /*
4887dd216Scgd * Copyright (c) 1980, 1993
5887dd216Scgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32ef383c95Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34887dd216Scgd #if 0
35887dd216Scgd static char sccsid[] = "@(#)snova.c 8.1 (Berkeley) 5/31/93";
36887dd216Scgd #else
37*32cded6cSdholland __RCSID("$NetBSD: snova.c,v 1.10 2018/02/08 09:05:16 dholland Exp $");
38887dd216Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
41ef383c95Schristos #include <stdio.h>
42ef383c95Schristos #include <unistd.h>
4361f28255Scgd #include "trek.h"
4461f28255Scgd
4561f28255Scgd /*
4661f28255Scgd ** CAUSE SUPERNOVA TO OCCUR
4761f28255Scgd **
4861f28255Scgd ** A supernova occurs. If 'ix' < 0, a random quadrant is chosen;
4961f28255Scgd ** otherwise, the current quadrant is taken, and (ix, iy) give
5061f28255Scgd ** the sector quadrants of the star which is blowing up.
5161f28255Scgd **
5261f28255Scgd ** If the supernova turns out to be in the quadrant you are in,
5361f28255Scgd ** you go into "emergency override mode", which tries to get you
5461f28255Scgd ** out of the quadrant as fast as possible. However, if you
5561f28255Scgd ** don't have enough fuel, or if you by chance run into something,
5661f28255Scgd ** or some such thing, you blow up anyway. Oh yeh, if you are
5761f28255Scgd ** within two sectors of the star, there is nothing that can
5861f28255Scgd ** be done for you.
5961f28255Scgd **
6061f28255Scgd ** When a star has gone supernova, the quadrant becomes uninhab-
6161f28255Scgd ** itable for the rest of eternity, i.e., the game. If you ever
6261f28255Scgd ** try stopping in such a quadrant, you will go into emergency
6361f28255Scgd ** override mode.
6461f28255Scgd */
6561f28255Scgd
66ef383c95Schristos void
snova(int x,int y)67bf0917b6Sdholland snova(int x, int y)
6861f28255Scgd {
6961f28255Scgd int qx, qy;
70ef383c95Schristos int ix, iy = 0;
7161f28255Scgd int f;
7261f28255Scgd int dx, dy;
7361f28255Scgd int n;
74ef383c95Schristos struct quad *q;
7561f28255Scgd
7661f28255Scgd f = 0;
7761f28255Scgd ix = x;
7850b862e2Sdholland if (ix < 0) {
7961f28255Scgd /* choose a quadrant */
8050b862e2Sdholland while (1) {
8161f28255Scgd qx = ranf(NQUADS);
8261f28255Scgd qy = ranf(NQUADS);
8361f28255Scgd q = &Quad[qx][qy];
8461f28255Scgd if (q->stars > 0)
8561f28255Scgd break;
8661f28255Scgd }
8750b862e2Sdholland if (Ship.quadx == qx && Ship.quady == qy) {
8861f28255Scgd /* select a particular star */
8961f28255Scgd n = ranf(q->stars);
9050b862e2Sdholland for (ix = 0; ix < NSECTS; ix++) {
9161f28255Scgd for (iy = 0; iy < NSECTS; iy++)
9261357867Sdholland if (Sect[ix][iy] == STAR ||
9361357867Sdholland Sect[ix][iy] == INHABIT)
9461f28255Scgd if ((n -= 1) <= 0)
9561f28255Scgd break;
9661f28255Scgd if (n <= 0)
9761f28255Scgd break;
9861f28255Scgd }
9961f28255Scgd f = 1;
10061f28255Scgd }
10150b862e2Sdholland } else {
10261f28255Scgd /* current quadrant */
10361f28255Scgd iy = y;
10461f28255Scgd qx = Ship.quadx;
10561f28255Scgd qy = Ship.quady;
10661f28255Scgd q = &Quad[qx][qy];
10761f28255Scgd f = 1;
10861f28255Scgd }
10950b862e2Sdholland if (f) {
11061f28255Scgd /* supernova is in same quadrant as Enterprise */
111*32cded6cSdholland printf("\a\nRED ALERT: supernova occurring at %d,%d\n", ix, iy);
11261f28255Scgd dx = ix - Ship.sectx;
11361f28255Scgd dy = iy - Ship.secty;
11450b862e2Sdholland if (dx * dx + dy * dy <= 2) {
11561f28255Scgd printf("*** Emergency override attem");
11661f28255Scgd sleep(1);
11761f28255Scgd printf("\n");
11861f28255Scgd lose(L_SNOVA);
11961f28255Scgd }
12061f28255Scgd q->scanned = 1000;
12150b862e2Sdholland } else {
12250b862e2Sdholland if (!damaged(SSRADIO)) {
12361f28255Scgd q->scanned = 1000;
12461357867Sdholland printf("\nUhura: Captain, Starfleet Command reports "
12561357867Sdholland "a supernova\n");
12661357867Sdholland printf(" in quadrant %d,%d. Caution is advised\n",
12761357867Sdholland qx, qy);
12861f28255Scgd }
12961f28255Scgd }
13061f28255Scgd
13161f28255Scgd /* clear out the supernova'ed quadrant */
13261f28255Scgd dx = q->klings;
13361f28255Scgd dy = q->stars;
13461f28255Scgd Now.klings -= dx;
13550b862e2Sdholland if (x >= 0) {
13661f28255Scgd /* Enterprise caused supernova */
13761f28255Scgd Game.kills += dy;
13861f28255Scgd if (q->bases)
139ef383c95Schristos killb(qx, qy);
14061f28255Scgd Game.killk += dx;
14150b862e2Sdholland } else {
14261f28255Scgd if (q->bases)
143ef383c95Schristos killb(qx, qy);
14450b862e2Sdholland }
14561f28255Scgd killd(qx, qy, (x >= 0));
14661f28255Scgd q->stars = -1;
14761f28255Scgd q->klings = 0;
14850b862e2Sdholland if (Now.klings <= 0) {
14961357867Sdholland printf("Lucky devil, that supernova destroyed the last "
15061357867Sdholland "klingon\n");
15161f28255Scgd win();
15261f28255Scgd }
15361f28255Scgd return;
15461f28255Scgd }
155