]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/events.c
avoid buffer overrun. fix from openbsd.
[bsdgames-darwin.git] / trek / events.c
1 /* $NetBSD: events.c,v 1.6 2001/06/12 15:17:12 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)events.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: events.c,v 1.6 2001/06/12 15:17:12 wiz Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <math.h>
48 #include "getpar.h"
49 #include "trek.h"
50
51 /*
52 ** CAUSE TIME TO ELAPSE
53 **
54 ** This routine does a hell of a lot. It elapses time, eats up
55 ** energy, regenerates energy, processes any events that occur,
56 ** and so on.
57 */
58
59
60 int
61 events(warp)
62 int warp; /* set if called in a time warp */
63 {
64 int i;
65 char *p;
66 int j = 0;
67 struct kling *k;
68 double rtime;
69 double xdate;
70 double idate;
71 struct event *ev = NULL;
72 int ix, iy;
73 struct quad *q;
74 struct event *e;
75 int evnum;
76 int restcancel;
77
78 /* if nothing happened, just allow for any Klingons killed */
79 if (Move.time <= 0.0)
80 {
81 Now.time = Now.resource / Now.klings;
82 return (0);
83 }
84
85 /* indicate that the cloaking device is now working */
86 Ship.cloakgood = 1;
87
88 /* idate is the initial date */
89 idate = Now.date;
90
91 /* schedule attacks if resting too long */
92 if (Move.time > 0.5 && Move.resting)
93 schedule(E_ATTACK, 0.5, 0, 0, 0);
94
95 /* scan the event list */
96 while (1)
97 {
98 restcancel = 0;
99 evnum = -1;
100 /* xdate is the date of the current event */
101 xdate = idate + Move.time;
102
103 /* find the first event that has happened */
104 for (i = 0; i < MAXEVENTS; i++)
105 {
106 e = &Event[i];
107 if (e->evcode == 0 || (e->evcode & E_GHOST))
108 continue;
109 if (e->date < xdate)
110 {
111 xdate = e->date;
112 ev = e;
113 evnum = i;
114 }
115 }
116 e = ev;
117
118 /* find the time between events */
119 rtime = xdate - Now.date;
120
121 /* decrement the magic "Federation Resources" pseudo-variable */
122 Now.resource -= Now.klings * rtime;
123 /* and recompute the time left */
124 Now.time = Now.resource / Now.klings;
125
126 /* move us up to the next date */
127 Now.date = xdate;
128
129 /* check for out of time */
130 if (Now.time <= 0.0)
131 lose(L_NOTIME);
132 # ifdef xTRACE
133 if (evnum >= 0 && Trace)
134 printf("xdate = %.2f, evcode %d params %d %d %d\n",
135 xdate, e->evcode, e->x, e->y, e->systemname);
136 # endif
137
138 /* if evnum < 0, no events occurred */
139 if (evnum < 0)
140 break;
141
142 /* otherwise one did. Find out what it is */
143 switch (e->evcode & E_EVENT)
144 {
145
146 case E_SNOVA: /* supernova */
147 /* cause the supernova to happen */
148 snova(-1, 0);
149 /* and schedule the next one */
150 xresched(e, E_SNOVA, 1);
151 break;
152
153 case E_LRTB: /* long range tractor beam */
154 /* schedule the next one */
155 xresched(e, E_LRTB, Now.klings);
156 /* LRTB cannot occur if we are docked */
157 if (Ship.cond != DOCKED)
158 {
159 /* pick a new quadrant */
160 i = ranf(Now.klings) + 1;
161 for (ix = 0; ix < NQUADS; ix++)
162 {
163 for (iy = 0; iy < NQUADS; iy++)
164 {
165 q = &Quad[ix][iy];
166 if (q->stars >= 0)
167 if ((i -= q->klings) <= 0)
168 break;
169 }
170 if (i <= 0)
171 break;
172 }
173
174 /* test for LRTB to same quadrant */
175 if (Ship.quadx == ix && Ship.quady == iy)
176 break;
177
178 /* nope, dump him in the new quadrant */
179 Ship.quadx = ix;
180 Ship.quady = iy;
181 printf("\n%s caught in long range tractor beam\n", Ship.shipname);
182 printf("*** Pulled to quadrant %d,%d\n", Ship.quadx, Ship.quady);
183 Ship.sectx = ranf(NSECTS);
184 Ship.secty = ranf(NSECTS);
185 initquad(0);
186 /* truncate the move time */
187 Move.time = xdate - idate;
188 }
189 break;
190
191 case E_KATSB: /* Klingon attacks starbase */
192 /* if out of bases, forget it */
193 if (Now.bases <= 0)
194 {
195 unschedule(e);
196 break;
197 }
198
199 /* check for starbase and Klingons in same quadrant */
200 for (i = 0; i < Now.bases; i++)
201 {
202 ix = Now.base[i].x;
203 iy = Now.base[i].y;
204 /* see if a Klingon exists in this quadrant */
205 q = &Quad[ix][iy];
206 if (q->klings <= 0)
207 continue;
208
209 /* see if already distressed */
210 for (j = 0; j < MAXEVENTS; j++)
211 {
212 e = &Event[j];
213 if ((e->evcode & E_EVENT) != E_KDESB)
214 continue;
215 if (e->x == ix && e->y == iy)
216 break;
217 }
218 if (j < MAXEVENTS)
219 continue;
220
221 /* got a potential attack */
222 break;
223 }
224 e = ev;
225 if (i >= Now.bases)
226 {
227 /* not now; wait a while and see if some Klingons move in */
228 reschedule(e, 0.5 + 3.0 * franf());
229 break;
230 }
231 /* schedule a new attack, and a destruction of the base */
232 xresched(e, E_KATSB, 1);
233 e = xsched(E_KDESB, 1, ix, iy, 0);
234
235 /* report it if we can */
236 if (!damaged(SSRADIO))
237 {
238 printf("\nUhura: Captain, we have received a distress signal\n");
239 printf(" from the starbase in quadrant %d,%d.\n",
240 ix, iy);
241 restcancel++;
242 }
243 else
244 /* SSRADIO out, make it so we can't see the distress call */
245 /* but it's still there!!! */
246 e->evcode |= E_HIDDEN;
247 break;
248
249 case E_KDESB: /* Klingon destroys starbase */
250 unschedule(e);
251 q = &Quad[e->x][e->y];
252 /* if the base has mysteriously gone away, or if the Klingon
253 got tired and went home, ignore this event */
254 if (q->bases <=0 || q->klings <= 0)
255 break;
256 /* are we in the same quadrant? */
257 if (e->x == Ship.quadx && e->y == Ship.quady)
258 {
259 /* yep, kill one in this quadrant */
260 printf("\nSpock: ");
261 killb(Ship.quadx, Ship.quady);
262 }
263 else
264 /* kill one in some other quadrant */
265 killb(e->x, e->y);
266 break;
267
268 case E_ISSUE: /* issue a distress call */
269 xresched(e, E_ISSUE, 1);
270 /* if we already have too many, throw this one away */
271 if (Ship.distressed >= MAXDISTR)
272 break;
273 /* try a whole bunch of times to find something suitable */
274 for (i = 0; i < 100; i++)
275 {
276 ix = ranf(NQUADS);
277 iy = ranf(NQUADS);
278 q = &Quad[ix][iy];
279 /* need a quadrant which is not the current one,
280 which has some stars which are inhabited and
281 not already under attack, which is not
282 supernova'ed, and which has some Klingons in it */
283 if (!((ix == Ship.quadx && iy == Ship.quady) || q->stars < 0 ||
284 (q->qsystemname & Q_DISTRESSED) ||
285 (q->qsystemname & Q_SYSTEM) == 0 || q->klings <= 0))
286 break;
287 }
288 if (i >= 100)
289 /* can't seem to find one; ignore this call */
290 break;
291
292 /* got one!! Schedule its enslavement */
293 Ship.distressed++;
294 e = xsched(E_ENSLV, 1, ix, iy, q->qsystemname);
295 q->qsystemname = (e - Event) | Q_DISTRESSED;
296
297 /* tell the captain about it if we can */
298 if (!damaged(SSRADIO))
299 {
300 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
301 Systemname[e->systemname], ix, iy);
302 restcancel++;
303 }
304 else
305 /* if we can't tell him, make it invisible */
306 e->evcode |= E_HIDDEN;
307 break;
308
309 case E_ENSLV: /* starsystem is enslaved */
310 unschedule(e);
311 /* see if current distress call still active */
312 q = &Quad[e->x][e->y];
313 if (q->klings <= 0)
314 {
315 /* no Klingons, clean up */
316 /* restore the system name */
317 q->qsystemname = e->systemname;
318 break;
319 }
320
321 /* play stork and schedule the first baby */
322 e = schedule(E_REPRO, Param.eventdly[E_REPRO] * franf(), e->x, e->y, e->systemname);
323
324 /* report the disaster if we can */
325 if (!damaged(SSRADIO))
326 {
327 printf("\nUhura: We've lost contact with starsystem %s\n",
328 Systemname[e->systemname]);
329 printf(" in quadrant %d,%d.\n",
330 e->x, e->y);
331 }
332 else
333 e->evcode |= E_HIDDEN;
334 break;
335
336 case E_REPRO: /* Klingon reproduces */
337 /* see if distress call is still active */
338 q = &Quad[e->x][e->y];
339 if (q->klings <= 0)
340 {
341 unschedule(e);
342 q->qsystemname = e->systemname;
343 break;
344 }
345 xresched(e, E_REPRO, 1);
346 /* reproduce one Klingon */
347 ix = e->x;
348 iy = e->y;
349 if (Now.klings == 127)
350 break; /* full right now */
351 if (q->klings >= MAXKLQUAD)
352 {
353 /* this quadrant not ok, pick an adjacent one */
354 for (i = ix - 1; i <= ix + 1; i++)
355 {
356 if (i < 0 || i >= NQUADS)
357 continue;
358 for (j = iy - 1; j <= iy + 1; j++)
359 {
360 if (j < 0 || j >= NQUADS)
361 continue;
362 q = &Quad[i][j];
363 /* check for this quad ok (not full & no snova) */
364 if (q->klings >= MAXKLQUAD || q->stars < 0)
365 continue;
366 break;
367 }
368 if (j <= iy + 1)
369 break;
370 }
371 if (j > iy + 1)
372 /* cannot create another yet */
373 break;
374 ix = i;
375 iy = j;
376 }
377 /* deliver the child */
378 q->klings++;
379 Now.klings++;
380 if (ix == Ship.quadx && iy == Ship.quady)
381 {
382 /* we must position Klingon */
383 sector(&ix, &iy);
384 Sect[ix][iy] = KLINGON;
385 k = &Etc.klingon[Etc.nkling++];
386 k->x = ix;
387 k->y = iy;
388 k->power = Param.klingpwr;
389 k->srndreq = 0;
390 compkldist(Etc.klingon[0].dist == Etc.klingon[0].avgdist ? 0 : 1);
391 }
392
393 /* recompute time left */
394 Now.time = Now.resource / Now.klings;
395 break;
396
397 case E_SNAP: /* take a snapshot of the galaxy */
398 xresched(e, E_SNAP, 1);
399 p = (char *) Etc.snapshot;
400 memcpy(p, Quad, sizeof (Quad));
401 p += sizeof(Quad);
402 memcpy(p, Event, sizeof (Event));
403 p += sizeof(Event);
404 memcpy(p, &Now, sizeof (Now));
405 Game.snap = 1;
406 break;
407
408 case E_ATTACK: /* Klingons attack during rest period */
409 if (!Move.resting)
410 {
411 unschedule(e);
412 break;
413 }
414 attack(1);
415 reschedule(e, 0.5);
416 break;
417
418 case E_FIXDV:
419 i = e->systemname;
420 unschedule(e);
421
422 /* de-damage the device */
423 printf("%s reports repair work on the %s finished.\n",
424 Device[i].person, Device[i].name);
425
426 /* handle special processing upon fix */
427 switch (i)
428 {
429
430 case LIFESUP:
431 Ship.reserves = Param.reserves;
432 break;
433
434 case SINS:
435 if (Ship.cond == DOCKED)
436 break;
437 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
438 printf(" but he has no standard base to calibrate to. Suggest you get\n");
439 printf(" to a starbase immediately so that you can properly recalibrate.\n");
440 Ship.sinsbad = 1;
441 break;
442
443 case SSRADIO:
444 restcancel = dumpssradio();
445 break;
446 }
447 break;
448
449 default:
450 break;
451 }
452
453 if (restcancel && Move.resting && getynpar("Spock: Shall we cancel our rest period"))
454 Move.time = xdate - idate;
455
456 }
457
458 /* unschedule an attack during a rest period */
459 if ((e = Now.eventptr[E_ATTACK]) != NULL)
460 unschedule(e);
461
462 if (!warp)
463 {
464 /* eat up energy if cloaked */
465 if (Ship.cloaked)
466 Ship.energy -= Param.cloakenergy * Move.time;
467
468 /* regenerate resources */
469 rtime = 1.0 - exp(-Param.regenfac * Move.time);
470 Ship.shield += (Param.shield - Ship.shield) * rtime;
471 Ship.energy += (Param.energy - Ship.energy) * rtime;
472
473 /* decrement life support reserves */
474 if (damaged(LIFESUP) && Ship.cond != DOCKED)
475 Ship.reserves -= Move.time;
476 }
477 return (0);
478 }