Merge from vendor branch OPENSSL:
[games.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  * @(#)kill.c   8.1 (Berkeley) 5/31/93
34  * $FreeBSD: src/games/trek/kill.c,v 1.4 1999/11/30 03:49:49 billf Exp $
35  * $DragonFly: src/games/trek/kill.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
36  */
37
38 # include       "trek.h"
39
40 /*
41 **  KILL KILL KILL !!!
42 **
43 **      This file handles the killing off of almost anything.
44 */
45
46 /*
47 **  Handle a Klingon's death
48 **
49 **      The Klingon at the sector given by the parameters is killed
50 **      and removed from the Klingon list.  Notice that it is not
51 **      removed from the event list; this is done later, when the
52 **      the event is to be caught.  Also, the time left is recomputed,
53 **      and the game is won if that was the last klingon.
54 */
55
56 killk(ix, iy)
57 int     ix, iy;
58 {
59         int             i, j;
60
61         printf("   *** Klingon at %d,%d destroyed ***\n", ix, iy);
62
63         /* remove the scoundrel */
64         Now.klings -= 1;
65         Sect[ix][iy] = EMPTY;
66         Quad[Ship.quadx][Ship.quady].klings -= 1;
67         /* %%% IS THIS SAFE???? %%% */
68         Quad[Ship.quadx][Ship.quady].scanned -= 100;
69         Game.killk += 1;
70
71         /* find the Klingon in the Klingon list */
72         for (i = 0; i < Etc.nkling; i++)
73                 if (ix == Etc.klingon[i].x && iy == Etc.klingon[i].y)
74                 {
75                         /* purge him from the list */
76                         Etc.nkling -= 1;
77                         for (; i < Etc.nkling; i++)
78                                 bmove(&Etc.klingon[i+1], &Etc.klingon[i], sizeof Etc.klingon[i]);
79                         break;
80                 }
81
82         /* find out if that was the last one */
83         if (Now.klings <= 0)
84                 win();
85
86         /* recompute time left */
87         Now.time = Now.resource / Now.klings;
88         return;
89 }
90
91
92 /*
93 **  handle a starbase's death
94 */
95
96 killb(qx, qy)
97 int     qx, qy;
98 {
99         struct quad     *q;
100         struct xy       *b;
101
102         q = &Quad[qx][qy];
103
104         if (q->bases <= 0)
105                 return;
106         if (!damaged(SSRADIO))
107                 /* then update starchart */
108                 if (q->scanned < 1000)
109                         q->scanned -= 10;
110                 else
111                         if (q->scanned > 1000)
112                                 q->scanned = -1;
113         q->bases = 0;
114         Now.bases -= 1;
115         for (b = Now.base; ; b++)
116                 if (qx == b->x && qy == b->y)
117                         break;
118         bmove(&Now.base[Now.bases], b, sizeof *b);
119         if (qx == Ship.quadx && qy == Ship.quady)
120         {
121                 Sect[Etc.starbase.x][Etc.starbase.y] = EMPTY;
122                 if (Ship.cond == DOCKED)
123                         undock();
124                 printf("Starbase at %d,%d destroyed\n", Etc.starbase.x, Etc.starbase.y);
125         }
126         else
127         {
128                 if (!damaged(SSRADIO))
129                 {
130                         printf("Uhura: Starfleet command reports that the starbase in\n");
131                         printf("   quadrant %d,%d has been destroyed\n", qx, qy);
132                 }
133                 else
134                         schedule(E_KATSB | E_GHOST, 1e50, qx, qy, 0);
135         }
136 }
137
138
139 /**
140  **     kill an inhabited starsystem
141  **/
142
143 kills(x, y, f)
144 int     x, y;   /* quad coords if f == 0, else sector coords */
145 int     f;      /* f != 0 -- this quad;  f < 0 -- Enterprise's fault */
146 {
147         struct quad     *q;
148         struct event    *e;
149         char            *name;
150         char                    *systemname();
151
152         if (f)
153         {
154                 /* current quadrant */
155                 q = &Quad[Ship.quadx][Ship.quady];
156                 Sect[x][y] = EMPTY;
157                 name = systemname(q);
158                 if (name == 0)
159                         return;
160                 printf("Inhabited starsystem %s at %d,%d destroyed\n",
161                         name, x, y);
162                 if (f < 0)
163                         Game.killinhab += 1;
164         }
165         else
166         {
167                 /* different quadrant */
168                 q = &Quad[x][y];
169         }
170         if (q->qsystemname & Q_DISTRESSED)
171         {
172                 /* distressed starsystem */
173                 e = &Event[q->qsystemname & Q_SYSTEM];
174                 printf("Distress call for %s invalidated\n",
175                         Systemname[e->systemname]);
176                 unschedule(e);
177         }
178         q->qsystemname = 0;
179         q->stars -= 1;
180 }
181
182
183 /**
184  **     "kill" a distress call
185  **/
186
187 killd(x, y, f)
188 int     x, y;           /* quadrant coordinates */
189 int     f;              /* set if user is to be informed */
190 {
191         struct event    *e;
192         int             i;
193         struct quad     *q;
194
195         q = &Quad[x][y];
196         for (i = 0; i < MAXEVENTS; i++)
197         {
198                 e = &Event[i];
199                 if (e->x != x || e->y != y)
200                         continue;
201                 switch (e->evcode)
202                 {
203                   case E_KDESB:
204                         if (f)
205                         {
206                                 printf("Distress call for starbase in %d,%d nullified\n",
207                                         x, y);
208                                 unschedule(e);
209                         }
210                         break;
211
212                   case E_ENSLV:
213                   case E_REPRO:
214                         if (f)
215                         {
216                                 printf("Distress call for %s in quadrant %d,%d nullified\n",
217                                         Systemname[e->systemname], x, y);
218                                 q->qsystemname = e->systemname;
219                                 unschedule(e);
220                         }
221                         else
222                         {
223                                 e->evcode |= E_GHOST;
224                         }
225                 }
226         }
227 }