]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/events.c
1 /* $NetBSD: events.c,v 1.6 2001/06/12 15:17:12 wiz Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)events.c 8.1 (Berkeley) 5/31/93";
41 __RCSID("$NetBSD: events.c,v 1.6 2001/06/12 15:17:12 wiz Exp $");
52 ** CAUSE TIME TO ELAPSE
54 ** This routine does a hell of a lot. It elapses time, eats up
55 ** energy, regenerates energy, processes any events that occur,
62 int warp
; /* set if called in a time warp */
71 struct event
*ev
= NULL
;
78 /* if nothing happened, just allow for any Klingons killed */
81 Now
.time
= Now
.resource
/ Now
.klings
;
85 /* indicate that the cloaking device is now working */
88 /* idate is the initial date */
91 /* schedule attacks if resting too long */
92 if (Move
.time
> 0.5 && Move
.resting
)
93 schedule(E_ATTACK
, 0.5, 0, 0, 0);
95 /* scan the event list */
100 /* xdate is the date of the current event */
101 xdate
= idate
+ Move
.time
;
103 /* find the first event that has happened */
104 for (i
= 0; i
< MAXEVENTS
; i
++)
107 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
118 /* find the time between events */
119 rtime
= xdate
- Now
.date
;
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
;
126 /* move us up to the next date */
129 /* check for out of time */
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
);
138 /* if evnum < 0, no events occurred */
142 /* otherwise one did. Find out what it is */
143 switch (e
->evcode
& E_EVENT
)
146 case E_SNOVA
: /* supernova */
147 /* cause the supernova to happen */
149 /* and schedule the next one */
150 xresched(e
, E_SNOVA
, 1);
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
)
159 /* pick a new quadrant */
160 i
= ranf(Now
.klings
) + 1;
161 for (ix
= 0; ix
< NQUADS
; ix
++)
163 for (iy
= 0; iy
< NQUADS
; iy
++)
167 if ((i
-= q
->klings
) <= 0)
174 /* test for LRTB to same quadrant */
175 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
178 /* nope, dump him in the new quadrant */
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
);
186 /* truncate the move time */
187 Move
.time
= xdate
- idate
;
191 case E_KATSB
: /* Klingon attacks starbase */
192 /* if out of bases, forget it */
199 /* check for starbase and Klingons in same quadrant */
200 for (i
= 0; i
< Now
.bases
; i
++)
204 /* see if a Klingon exists in this quadrant */
209 /* see if already distressed */
210 for (j
= 0; j
< MAXEVENTS
; j
++)
213 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
215 if (e
->x
== ix
&& e
->y
== iy
)
221 /* got a potential attack */
227 /* not now; wait a while and see if some Klingons move in */
228 reschedule(e
, 0.5 + 3.0 * franf());
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);
235 /* report it if we can */
236 if (!damaged(SSRADIO
))
238 printf("\nUhura: Captain, we have received a distress signal\n");
239 printf(" from the starbase in quadrant %d,%d.\n",
244 /* SSRADIO out, make it so we can't see the distress call */
245 /* but it's still there!!! */
246 e
->evcode
|= E_HIDDEN
;
249 case E_KDESB
: /* Klingon destroys starbase */
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)
256 /* are we in the same quadrant? */
257 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
259 /* yep, kill one in this quadrant */
261 killb(Ship
.quadx
, Ship
.quady
);
264 /* kill one in some other quadrant */
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
)
273 /* try a whole bunch of times to find something suitable */
274 for (i
= 0; i
< 100; i
++)
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))
289 /* can't seem to find one; ignore this call */
292 /* got one!! Schedule its enslavement */
294 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
295 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
297 /* tell the captain about it if we can */
298 if (!damaged(SSRADIO
))
300 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
301 Systemname
[e
->systemname
], ix
, iy
);
305 /* if we can't tell him, make it invisible */
306 e
->evcode
|= E_HIDDEN
;
309 case E_ENSLV
: /* starsystem is enslaved */
311 /* see if current distress call still active */
312 q
= &Quad
[e
->x
][e
->y
];
315 /* no Klingons, clean up */
316 /* restore the system name */
317 q
->qsystemname
= e
->systemname
;
321 /* play stork and schedule the first baby */
322 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
324 /* report the disaster if we can */
325 if (!damaged(SSRADIO
))
327 printf("\nUhura: We've lost contact with starsystem %s\n",
328 Systemname
[e
->systemname
]);
329 printf(" in quadrant %d,%d.\n",
333 e
->evcode
|= E_HIDDEN
;
336 case E_REPRO
: /* Klingon reproduces */
337 /* see if distress call is still active */
338 q
= &Quad
[e
->x
][e
->y
];
342 q
->qsystemname
= e
->systemname
;
345 xresched(e
, E_REPRO
, 1);
346 /* reproduce one Klingon */
349 if (Now
.klings
== 127)
350 break; /* full right now */
351 if (q
->klings
>= MAXKLQUAD
)
353 /* this quadrant not ok, pick an adjacent one */
354 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
356 if (i
< 0 || i
>= NQUADS
)
358 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
360 if (j
< 0 || j
>= NQUADS
)
363 /* check for this quad ok (not full & no snova) */
364 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
372 /* cannot create another yet */
377 /* deliver the child */
380 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
382 /* we must position Klingon */
384 Sect
[ix
][iy
] = KLINGON
;
385 k
= &Etc
.klingon
[Etc
.nkling
++];
388 k
->power
= Param
.klingpwr
;
390 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
393 /* recompute time left */
394 Now
.time
= Now
.resource
/ Now
.klings
;
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
));
402 memcpy(p
, Event
, sizeof (Event
));
404 memcpy(p
, &Now
, sizeof (Now
));
408 case E_ATTACK
: /* Klingons attack during rest period */
422 /* de-damage the device */
423 printf("%s reports repair work on the %s finished.\n",
424 Device
[i
].person
, Device
[i
].name
);
426 /* handle special processing upon fix */
431 Ship
.reserves
= Param
.reserves
;
435 if (Ship
.cond
== DOCKED
)
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");
444 restcancel
= dumpssradio();
453 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
454 Move
.time
= xdate
- idate
;
458 /* unschedule an attack during a rest period */
459 if ((e
= Now
.eventptr
[E_ATTACK
]) != NULL
)
464 /* eat up energy if cloaked */
466 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
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
;
473 /* decrement life support reserves */
474 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
475 Ship
.reserves
-= Move
.time
;