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