]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - trek/events.c
1 /* $NetBSD: events.c,v 1.7 2003/08/07 09:37:51 agc 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)events.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: events.c,v 1.7 2003/08/07 09:37:51 agc Exp $");
48 ** CAUSE TIME TO ELAPSE
50 ** This routine does a hell of a lot. It elapses time, eats up
51 ** energy, regenerates energy, processes any events that occur,
58 int warp
; /* set if called in a time warp */
67 struct event
*ev
= NULL
;
74 /* if nothing happened, just allow for any Klingons killed */
77 Now
.time
= Now
.resource
/ Now
.klings
;
81 /* indicate that the cloaking device is now working */
84 /* idate is the initial date */
87 /* schedule attacks if resting too long */
88 if (Move
.time
> 0.5 && Move
.resting
)
89 schedule(E_ATTACK
, 0.5, 0, 0, 0);
91 /* scan the event list */
96 /* xdate is the date of the current event */
97 xdate
= idate
+ Move
.time
;
99 /* find the first event that has happened */
100 for (i
= 0; i
< MAXEVENTS
; i
++)
103 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
114 /* find the time between events */
115 rtime
= xdate
- Now
.date
;
117 /* decrement the magic "Federation Resources" pseudo-variable */
118 Now
.resource
-= Now
.klings
* rtime
;
119 /* and recompute the time left */
120 Now
.time
= Now
.resource
/ Now
.klings
;
122 /* move us up to the next date */
125 /* check for out of time */
129 if (evnum
>= 0 && Trace
)
130 printf("xdate = %.2f, evcode %d params %d %d %d\n",
131 xdate
, e
->evcode
, e
->x
, e
->y
, e
->systemname
);
134 /* if evnum < 0, no events occurred */
138 /* otherwise one did. Find out what it is */
139 switch (e
->evcode
& E_EVENT
)
142 case E_SNOVA
: /* supernova */
143 /* cause the supernova to happen */
145 /* and schedule the next one */
146 xresched(e
, E_SNOVA
, 1);
149 case E_LRTB
: /* long range tractor beam */
150 /* schedule the next one */
151 xresched(e
, E_LRTB
, Now
.klings
);
152 /* LRTB cannot occur if we are docked */
153 if (Ship
.cond
!= DOCKED
)
155 /* pick a new quadrant */
156 i
= ranf(Now
.klings
) + 1;
157 for (ix
= 0; ix
< NQUADS
; ix
++)
159 for (iy
= 0; iy
< NQUADS
; iy
++)
163 if ((i
-= q
->klings
) <= 0)
170 /* test for LRTB to same quadrant */
171 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
174 /* nope, dump him in the new quadrant */
177 printf("\n%s caught in long range tractor beam\n", Ship
.shipname
);
178 printf("*** Pulled to quadrant %d,%d\n", Ship
.quadx
, Ship
.quady
);
179 Ship
.sectx
= ranf(NSECTS
);
180 Ship
.secty
= ranf(NSECTS
);
182 /* truncate the move time */
183 Move
.time
= xdate
- idate
;
187 case E_KATSB
: /* Klingon attacks starbase */
188 /* if out of bases, forget it */
195 /* check for starbase and Klingons in same quadrant */
196 for (i
= 0; i
< Now
.bases
; i
++)
200 /* see if a Klingon exists in this quadrant */
205 /* see if already distressed */
206 for (j
= 0; j
< MAXEVENTS
; j
++)
209 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
211 if (e
->x
== ix
&& e
->y
== iy
)
217 /* got a potential attack */
223 /* not now; wait a while and see if some Klingons move in */
224 reschedule(e
, 0.5 + 3.0 * franf());
227 /* schedule a new attack, and a destruction of the base */
228 xresched(e
, E_KATSB
, 1);
229 e
= xsched(E_KDESB
, 1, ix
, iy
, 0);
231 /* report it if we can */
232 if (!damaged(SSRADIO
))
234 printf("\nUhura: Captain, we have received a distress signal\n");
235 printf(" from the starbase in quadrant %d,%d.\n",
240 /* SSRADIO out, make it so we can't see the distress call */
241 /* but it's still there!!! */
242 e
->evcode
|= E_HIDDEN
;
245 case E_KDESB
: /* Klingon destroys starbase */
247 q
= &Quad
[e
->x
][e
->y
];
248 /* if the base has mysteriously gone away, or if the Klingon
249 got tired and went home, ignore this event */
250 if (q
->bases
<=0 || q
->klings
<= 0)
252 /* are we in the same quadrant? */
253 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
255 /* yep, kill one in this quadrant */
257 killb(Ship
.quadx
, Ship
.quady
);
260 /* kill one in some other quadrant */
264 case E_ISSUE
: /* issue a distress call */
265 xresched(e
, E_ISSUE
, 1);
266 /* if we already have too many, throw this one away */
267 if (Ship
.distressed
>= MAXDISTR
)
269 /* try a whole bunch of times to find something suitable */
270 for (i
= 0; i
< 100; i
++)
275 /* need a quadrant which is not the current one,
276 which has some stars which are inhabited and
277 not already under attack, which is not
278 supernova'ed, and which has some Klingons in it */
279 if (!((ix
== Ship
.quadx
&& iy
== Ship
.quady
) || q
->stars
< 0 ||
280 (q
->qsystemname
& Q_DISTRESSED
) ||
281 (q
->qsystemname
& Q_SYSTEM
) == 0 || q
->klings
<= 0))
285 /* can't seem to find one; ignore this call */
288 /* got one!! Schedule its enslavement */
290 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
291 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
293 /* tell the captain about it if we can */
294 if (!damaged(SSRADIO
))
296 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
297 Systemname
[e
->systemname
], ix
, iy
);
301 /* if we can't tell him, make it invisible */
302 e
->evcode
|= E_HIDDEN
;
305 case E_ENSLV
: /* starsystem is enslaved */
307 /* see if current distress call still active */
308 q
= &Quad
[e
->x
][e
->y
];
311 /* no Klingons, clean up */
312 /* restore the system name */
313 q
->qsystemname
= e
->systemname
;
317 /* play stork and schedule the first baby */
318 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
320 /* report the disaster if we can */
321 if (!damaged(SSRADIO
))
323 printf("\nUhura: We've lost contact with starsystem %s\n",
324 Systemname
[e
->systemname
]);
325 printf(" in quadrant %d,%d.\n",
329 e
->evcode
|= E_HIDDEN
;
332 case E_REPRO
: /* Klingon reproduces */
333 /* see if distress call is still active */
334 q
= &Quad
[e
->x
][e
->y
];
338 q
->qsystemname
= e
->systemname
;
341 xresched(e
, E_REPRO
, 1);
342 /* reproduce one Klingon */
345 if (Now
.klings
== 127)
346 break; /* full right now */
347 if (q
->klings
>= MAXKLQUAD
)
349 /* this quadrant not ok, pick an adjacent one */
350 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
352 if (i
< 0 || i
>= NQUADS
)
354 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
356 if (j
< 0 || j
>= NQUADS
)
359 /* check for this quad ok (not full & no snova) */
360 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
368 /* cannot create another yet */
373 /* deliver the child */
376 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
378 /* we must position Klingon */
380 Sect
[ix
][iy
] = KLINGON
;
381 k
= &Etc
.klingon
[Etc
.nkling
++];
384 k
->power
= Param
.klingpwr
;
386 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
389 /* recompute time left */
390 Now
.time
= Now
.resource
/ Now
.klings
;
393 case E_SNAP
: /* take a snapshot of the galaxy */
394 xresched(e
, E_SNAP
, 1);
395 p
= (char *) Etc
.snapshot
;
396 memcpy(p
, Quad
, sizeof (Quad
));
398 memcpy(p
, Event
, sizeof (Event
));
400 memcpy(p
, &Now
, sizeof (Now
));
404 case E_ATTACK
: /* Klingons attack during rest period */
418 /* de-damage the device */
419 printf("%s reports repair work on the %s finished.\n",
420 Device
[i
].person
, Device
[i
].name
);
422 /* handle special processing upon fix */
427 Ship
.reserves
= Param
.reserves
;
431 if (Ship
.cond
== DOCKED
)
433 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
434 printf(" but he has no standard base to calibrate to. Suggest you get\n");
435 printf(" to a starbase immediately so that you can properly recalibrate.\n");
440 restcancel
= dumpssradio();
449 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
450 Move
.time
= xdate
- idate
;
454 /* unschedule an attack during a rest period */
455 if ((e
= Now
.eventptr
[E_ATTACK
]) != NULL
)
460 /* eat up energy if cloaked */
462 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
464 /* regenerate resources */
465 rtime
= 1.0 - exp(-Param
.regenfac
* Move
.time
);
466 Ship
.shield
+= (Param
.shield
- Ship
.shield
) * rtime
;
467 Ship
.energy
+= (Param
.energy
- Ship
.energy
) * rtime
;
469 /* decrement life support reserves */
470 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
471 Ship
.reserves
-= Move
.time
;