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