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