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