2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)events.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/trek/events.c,v 1.4 1999/11/30 03:49:47 billf Exp $
35 * $DragonFly: src/games/trek/events.c,v 1.2 2003/06/17 04:25:25 dillon Exp $
41 ** CAUSE TIME TO ELAPSE
43 ** This routine does a hell of a lot. It elapses time, eats up
44 ** energy, regenerates energy, processes any events that occur,
50 int warp
; /* set if called in a time warp */
58 struct event
*ev
, *xsched(), *schedule();
65 /* if nothing happened, just allow for any Klingons killed */
68 Now
.time
= Now
.resource
/ Now
.klings
;
72 /* indicate that the cloaking device is now working */
75 /* idate is the initial date */
78 /* schedule attacks if resting too long */
79 if (Move
.time
> 0.5 && Move
.resting
)
80 schedule(E_ATTACK
, 0.5, 0, 0, 0);
82 /* scan the event list */
87 /* xdate is the date of the current event */
88 xdate
= idate
+ Move
.time
;
90 /* find the first event that has happened */
91 for (i
= 0; i
< MAXEVENTS
; i
++)
94 if (e
->evcode
== 0 || (e
->evcode
& E_GHOST
))
105 /* find the time between events */
106 rtime
= xdate
- Now
.date
;
108 /* decrement the magic "Federation Resources" pseudo-variable */
109 Now
.resource
-= Now
.klings
* rtime
;
110 /* and recompute the time left */
111 Now
.time
= Now
.resource
/ Now
.klings
;
113 /* move us up to the next date */
116 /* check for out of time */
120 if (evnum
>= 0 && Trace
)
121 printf("xdate = %.2f, evcode %d params %d %d %d\n",
122 xdate
, e
->evcode
, e
->x
, e
->y
, e
->systemname
);
125 /* if evnum < 0, no events occurred */
129 /* otherwise one did. Find out what it is */
130 switch (e
->evcode
& E_EVENT
)
133 case E_SNOVA
: /* supernova */
134 /* cause the supernova to happen */
136 /* and schedule the next one */
137 xresched(e
, E_SNOVA
, 1);
140 case E_LRTB
: /* long range tractor beam */
141 /* schedule the next one */
142 xresched(e
, E_LRTB
, Now
.klings
);
143 /* LRTB cannot occur if we are docked */
144 if (Ship
.cond
!= DOCKED
)
146 /* pick a new quadrant */
147 i
= ranf(Now
.klings
) + 1;
148 for (ix
= 0; ix
< NQUADS
; ix
++)
150 for (iy
= 0; iy
< NQUADS
; iy
++)
154 if ((i
-= q
->klings
) <= 0)
161 /* test for LRTB to same quadrant */
162 if (Ship
.quadx
== ix
&& Ship
.quady
== iy
)
165 /* nope, dump him in the new quadrant */
168 printf("\n%s caught in long range tractor beam\n", Ship
.shipname
);
169 printf("*** Pulled to quadrant %d,%d\n", Ship
.quadx
, Ship
.quady
);
170 Ship
.sectx
= ranf(NSECTS
);
171 Ship
.secty
= ranf(NSECTS
);
173 /* truncate the move time */
174 Move
.time
= xdate
- idate
;
178 case E_KATSB
: /* Klingon attacks starbase */
179 /* if out of bases, forget it */
186 /* check for starbase and Klingons in same quadrant */
187 for (i
= 0; i
< Now
.bases
; i
++)
191 /* see if a Klingon exists in this quadrant */
196 /* see if already distressed */
197 for (j
= 0; j
< MAXEVENTS
; j
++)
200 if ((e
->evcode
& E_EVENT
) != E_KDESB
)
202 if (e
->x
== ix
&& e
->y
== iy
)
208 /* got a potential attack */
214 /* not now; wait a while and see if some Klingons move in */
215 reschedule(e
, 0.5 + 3.0 * franf());
218 /* schedule a new attack, and a destruction of the base */
219 xresched(e
, E_KATSB
, 1);
220 e
= xsched(E_KDESB
, 1, ix
, iy
, 0);
222 /* report it if we can */
223 if (!damaged(SSRADIO
))
225 printf("\nUhura: Captain, we have recieved a distress signal\n");
226 printf(" from the starbase in quadrant %d,%d.\n",
231 /* SSRADIO out, make it so we can't see the distress call */
232 /* but it's still there!!! */
233 e
->evcode
|= E_HIDDEN
;
236 case E_KDESB
: /* Klingon destroys starbase */
238 q
= &Quad
[e
->x
][e
->y
];
239 /* if the base has mysteriously gone away, or if the Klingon
240 got tired and went home, ignore this event */
241 if (q
->bases
<=0 || q
->klings
<= 0)
243 /* are we in the same quadrant? */
244 if (e
->x
== Ship
.quadx
&& e
->y
== Ship
.quady
)
246 /* yep, kill one in this quadrant */
248 killb(Ship
.quadx
, Ship
.quady
);
251 /* kill one in some other quadrant */
255 case E_ISSUE
: /* issue a distress call */
256 xresched(e
, E_ISSUE
, 1);
257 /* if we already have too many, throw this one away */
258 if (Ship
.distressed
>= MAXDISTR
)
260 /* try a whole bunch of times to find something suitable */
261 for (i
= 0; i
< 100; i
++)
266 /* need a quadrant which is not the current one,
267 which has some stars which are inhabited and
268 not already under attack, which is not
269 supernova'ed, and which has some Klingons in it */
270 if (!((ix
== Ship
.quadx
&& iy
== Ship
.quady
) || q
->stars
< 0 ||
271 (q
->qsystemname
& Q_DISTRESSED
) ||
272 (q
->qsystemname
& Q_SYSTEM
) == 0 || q
->klings
<= 0))
276 /* can't seem to find one; ignore this call */
279 /* got one!! Schedule its enslavement */
281 e
= xsched(E_ENSLV
, 1, ix
, iy
, q
->qsystemname
);
282 q
->qsystemname
= (e
- Event
) | Q_DISTRESSED
;
284 /* tell the captain about it if we can */
285 if (!damaged(SSRADIO
))
287 printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
288 Systemname
[e
->systemname
], ix
, iy
);
292 /* if we can't tell him, make it invisible */
293 e
->evcode
|= E_HIDDEN
;
296 case E_ENSLV
: /* starsystem is enslaved */
298 /* see if current distress call still active */
299 q
= &Quad
[e
->x
][e
->y
];
302 /* no Klingons, clean up */
303 /* restore the system name */
304 q
->qsystemname
= e
->systemname
;
308 /* play stork and schedule the first baby */
309 e
= schedule(E_REPRO
, Param
.eventdly
[E_REPRO
] * franf(), e
->x
, e
->y
, e
->systemname
);
311 /* report the disaster if we can */
312 if (!damaged(SSRADIO
))
314 printf("\nUhura: We've lost contact with starsystem %s\n",
315 Systemname
[e
->systemname
]);
316 printf(" in quadrant %d,%d.\n",
320 e
->evcode
|= E_HIDDEN
;
323 case E_REPRO
: /* Klingon reproduces */
324 /* see if distress call is still active */
325 q
= &Quad
[e
->x
][e
->y
];
329 q
->qsystemname
= e
->systemname
;
332 xresched(e
, E_REPRO
, 1);
333 /* reproduce one Klingon */
336 if (Now
.klings
== 127)
337 break; /* full right now */
338 if (q
->klings
>= MAXKLQUAD
)
340 /* this quadrant not ok, pick an adjacent one */
341 for (i
= ix
- 1; i
<= ix
+ 1; i
++)
343 if (i
< 0 || i
>= NQUADS
)
345 for (j
= iy
- 1; j
<= iy
+ 1; j
++)
347 if (j
< 0 || j
>= NQUADS
)
350 /* check for this quad ok (not full & no snova) */
351 if (q
->klings
>= MAXKLQUAD
|| q
->stars
< 0)
359 /* cannot create another yet */
364 /* deliver the child */
367 if (ix
== Ship
.quadx
&& iy
== Ship
.quady
)
369 /* we must position Klingon */
371 Sect
[ix
][iy
] = KLINGON
;
372 k
= &Etc
.klingon
[Etc
.nkling
++];
375 k
->power
= Param
.klingpwr
;
377 compkldist(Etc
.klingon
[0].dist
== Etc
.klingon
[0].avgdist
? 0 : 1);
380 /* recompute time left */
381 Now
.time
= Now
.resource
/ Now
.klings
;
384 case E_SNAP
: /* take a snapshot of the galaxy */
385 xresched(e
, E_SNAP
, 1);
386 i
= (int) Etc
.snapshot
;
387 i
= bmove(Quad
, i
, sizeof (Quad
));
388 i
= bmove(Event
, i
, sizeof (Event
));
389 i
= bmove(&Now
, i
, sizeof (Now
));
393 case E_ATTACK
: /* Klingons attack during rest period */
407 /* de-damage the device */
408 printf("%s reports repair work on the %s finished.\n",
409 Device
[i
].person
, Device
[i
].name
);
411 /* handle special processing upon fix */
416 Ship
.reserves
= Param
.reserves
;
420 if (Ship
.cond
== DOCKED
)
422 printf("Spock has tried to recalibrate your Space Internal Navigation System,\n");
423 printf(" but he has no standard base to calibrate to. Suggest you get\n");
424 printf(" to a starbase immediately so that you can properly recalibrate.\n");
429 restcancel
= dumpssradio();
438 if (restcancel
&& Move
.resting
&& getynpar("Spock: Shall we cancel our rest period"))
439 Move
.time
= xdate
- idate
;
443 /* unschedule an attack during a rest period */
444 if (e
= Now
.eventptr
[E_ATTACK
])
449 /* eat up energy if cloaked */
451 Ship
.energy
-= Param
.cloakenergy
* Move
.time
;
453 /* regenerate resources */
454 rtime
= 1.0 - exp(-Param
.regenfac
* Move
.time
);
455 Ship
.shield
+= (Param
.shield
- Ship
.shield
) * rtime
;
456 Ship
.energy
+= (Param
.energy
- Ship
.energy
) * rtime
;
458 /* decrement life support reserves */
459 if (damaged(LIFESUP
) && Ship
.cond
!= DOCKED
)
460 Ship
.reserves
-= Move
.time
;