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