Initial import from FreeBSD RELENG_4:
[dragonfly.git] / games / trek / kill.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)kill.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/trek/kill.c,v 1.4 1999/11/30 03:49:49 billf Exp $";
40 #endif /* not lint */
41
42 # include       "trek.h"
43
44 /*
45 **  KILL KILL KILL !!!
46 **
47 **      This file handles the killing off of almost anything.
48 */
49
50 /*
51 **  Handle a Klingon's death
52 **
53 **      The Klingon at the sector given by the parameters is killed
54 **      and removed from the Klingon list.  Notice that it is not
55 **      removed from the event list; this is done later, when the
56 **      the event is to be caught.  Also, the time left is recomputed,
57 **      and the game is won if that was the last klingon.
58 */
59
60 killk(ix, iy)
61 int     ix, iy;
62 {
63         int             i, j;
64
65         printf("   *** Klingon at %d,%d destroyed ***\n", ix, iy);
66
67         /* remove the scoundrel */
68         Now.klings -= 1;
69         Sect[ix][iy] = EMPTY;
70         Quad[Ship.quadx][Ship.quady].klings -= 1;
71         /* %%% IS THIS SAFE???? %%% */
72         Quad[Ship.quadx][Ship.quady].scanned -= 100;
73         Game.killk += 1;
74
75         /* find the Klingon in the Klingon list */
76         for (i = 0; i < Etc.nkling; i++)
77                 if (ix == Etc.klingon[i].x && iy == Etc.klingon[i].y)
78                 {
79                         /* purge him from the list */
80                         Etc.nkling -= 1;
81                         for (; i < Etc.nkling; i++)
82                                 bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof Etc.klingon[i]);
83                         break;
84                 }
85
86         /* find out if that was the last one */
87         if (Now.klings <= 0)
88                 win();
89
90         /* recompute time left */
91         Now.time = Now.resource / Now.klings;
92         return;
93 }
94
95
96 /*
97 **  handle a starbase's death
98 */
99
100 killb(qx, qy)
101 int     qx, qy;
102 {
103         struct quad     *q;
104         struct xy       *b;
105
106         q = &Quad[qx][qy];
107
108         if (q->bases <= 0)
109                 return;
110         if (!damaged(SSRADIO))
111                 /* then update starchart */
112                 if (q->scanned < 1000)
113                         q->scanned -= 10;
114                 else
115                         if (q->scanned > 1000)
116                                 q->scanned = -1;
117         q->bases = 0;
118         Now.bases -= 1;
119         for (b = Now.base; ; b++)
120                 if (qx == b->x && qy == b->y)
121                         break;
122         bmove(&Now.base[Now.bases], b, sizeof *b);
123         if (qx == Ship.quadx && qy == Ship.quady)
124         {
125                 Sect[Etc.starbase.x][Etc.starbase.y] = EMPTY;
126                 if (Ship.cond == DOCKED)
127                         undock();
128                 printf("Starbase at %d,%d destroyed\n", Etc.starbase.x, Etc.starbase.y);
129         }
130         else
131         {
132                 if (!damaged(SSRADIO))
133                 {
134                         printf("Uhura: Starfleet command reports that the starbase in\n");
135                         printf("   quadrant %d,%d has been destroyed\n", qx, qy);
136                 }
137                 else
138                         schedule(E_KATSB | E_GHOST, 1e50, qx, qy, 0);
139         }
140 }
141
142
143 /**
144  **     kill an inhabited starsystem
145  **/
146
147 kills(x, y, f)
148 int     x, y;   /* quad coords if f == 0, else sector coords */
149 int     f;      /* f != 0 -- this quad;  f < 0 -- Enterprise's fault */
150 {
151         struct quad     *q;
152         struct event    *e;
153         char            *name;
154         char                    *systemname();
155
156         if (f)
157         {
158                 /* current quadrant */
159                 q = &Quad[Ship.quadx][Ship.quady];
160                 Sect[x][y] = EMPTY;
161                 name = systemname(q);
162                 if (name == 0)
163                         return;
164                 printf("Inhabited starsystem %s at %d,%d destroyed\n",
165                         name, x, y);
166                 if (f < 0)
167                         Game.killinhab += 1;
168         }
169         else
170         {
171                 /* different quadrant */
172                 q = &Quad[x][y];
173         }
174         if (q->qsystemname & Q_DISTRESSED)
175         {
176                 /* distressed starsystem */
177                 e = &Event[q->qsystemname & Q_SYSTEM];
178                 printf("Distress call for %s invalidated\n",
179                         Systemname[e->systemname]);
180                 unschedule(e);
181         }
182         q->qsystemname = 0;
183         q->stars -= 1;
184 }
185
186
187 /**
188  **     "kill" a distress call
189  **/
190
191 killd(x, y, f)
192 int     x, y;           /* quadrant coordinates */
193 int     f;              /* set if user is to be informed */
194 {
195         struct event    *e;
196         int             i;
197         struct quad     *q;
198
199         q = &Quad[x][y];
200         for (i = 0; i < MAXEVENTS; i++)
201         {
202                 e = &Event[i];
203                 if (e->x != x || e->y != y)
204                         continue;
205                 switch (e->evcode)
206                 {
207                   case E_KDESB:
208                         if (f)
209                         {
210                                 printf("Distress call for starbase in %d,%d nullified\n",
211                                         x, y);
212                                 unschedule(e);
213                         }
214                         break;
215
216                   case E_ENSLV:
217                   case E_REPRO:
218                         if (f)
219                         {
220                                 printf("Distress call for %s in quadrant %d,%d nullified\n",
221                                         Systemname[e->systemname], x, y);
222                                 q->qsystemname = e->systemname;
223                                 unschedule(e);
224                         }
225                         else
226                         {
227                                 e->evcode |= E_GHOST;
228                         }
229                 }
230         }
231 }