1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: #include <sys/cdefs.h>
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)events.c 8.1 (Berkeley) 5/31/93";
36: #else
37: __RCSID("$NetBSD: events.c,v 1.7 2003/08/07 09:37:51 agc Exp $");
38: #endif
39: #endif
40:
41: #include <stdio.h>
42: #include <string.h>
43: #include <math.h>
44: #include "getpar.h"
45: #include "trek.h"
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56: int
57: events(warp)
58: int warp;
59: {
60: int i;
61: char *p;
62: int j = 0;
63: struct kling *k;
64: double rtime;
65: double xdate;
66: double idate;
67: struct event *ev = NULL;
68: int ix, iy;
69: struct quad *q;
70: struct event *e;
71: int evnum;
72: int restcancel;
73:
74:
75: if (Move.time <= 0.0)
76: {
77: Now.time = Now.resource / Now.klings;
78: return (0);
79: }
80:
81:
82: Ship.cloakgood = 1;
83:
84:
85: idate = Now.date;
86:
87:
88: if (Move.time > 0.5 && Move.resting)
89: schedule(E_ATTACK, 0.5, 0, 0, 0);
90:
91:
92: while (1)
93: {
94: restcancel = 0;
95: evnum = -1;
96:
97: xdate = idate + Move.time;
98:
99:
100: for (i = 0; i < MAXEVENTS; i++)
101: {
102: e = &Event[i];
103: if (e->evcode == 0 || (e->evcode & E_GHOST))
104: continue;
105: if (e->date < xdate)
106: {
107: xdate = e->date;
108: ev = e;
109: evnum = i;
110: }
111: }
112: e = ev;
113:
114:
115: rtime = xdate - Now.date;
116:
117:
118: Now.resource -= Now.klings * rtime;
119:
120: Now.time = Now.resource / Now.klings;
121:
122:
123: Now.date = xdate;
124:
125:
126: if (Now.time <= 0.0)
127: lose(L_NOTIME);
128: # ifdef xTRACE
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);
132: # endif
133:
134:
135: if (evnum < 0)
136: break;
137:
138:
139: switch (e->evcode & E_EVENT)
140: {
141:
142: case E_SNOVA:
143:
144: snova(-1, 0);
145:
146: xresched(e, E_SNOVA, 1);
147: break;
148:
149: case E_LRTB:
150:
151: xresched(e, E_LRTB, Now.klings);
152:
153: if (Ship.cond != DOCKED)
154: {
155:
156: i = ranf(Now.klings) + 1;
157: for (ix = 0; ix < NQUADS; ix++)
158: {
159: for (iy = 0; iy < NQUADS; iy++)
160: {
161: q = &Quad[ix][iy];
162: if (q->stars >= 0)
163: if ((i -= q->klings) <= 0)
164: break;
165: }
166: if (i <= 0)
167: break;
168: }
169:
170:
171: if (Ship.quadx == ix && Ship.quady == iy)
172: break;
173:
174:
175: Ship.quadx = ix;
176: Ship.quady = iy;
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);
181: initquad(0);
182:
183: Move.time = xdate - idate;
184: }
185: break;
186:
187: case E_KATSB:
188:
189: if (Now.bases <= 0)
190: {
191: unschedule(e);
192: break;
193: }
194:
195:
196: for (i = 0; i < Now.bases; i++)
197: {
198: ix = Now.base[i].x;
199: iy = Now.base[i].y;
200:
201: q = &Quad[ix][iy];
202: if (q->klings <= 0)
203: continue;
204:
205:
206: for (j = 0; j < MAXEVENTS; j++)
207: {
208: e = &Event[j];
209: if ((e->evcode & E_EVENT) != E_KDESB)
210: continue;
211: if (e->x == ix && e->y == iy)
212: break;
213: }
214: if (j < MAXEVENTS)
215: continue;
216:
217:
218: break;
219: }
220: e = ev;
221: if (i >= Now.bases)
222: {
223:
224: reschedule(e, 0.5 + 3.0 * franf());
225: break;
226: }
227:
228: xresched(e, E_KATSB, 1);
229: e = xsched(E_KDESB, 1, ix, iy, 0);
230:
231:
232: if (!damaged(SSRADIO))
233: {
234: printf("\nUhura: Captain, we have received a distress signal\n");
235: printf(" from the starbase in quadrant %d,%d.\n",
236: ix, iy);
237: restcancel++;
238: }
239: else
240:
241:
242: e->evcode |= E_HIDDEN;
243: break;
244:
245: case E_KDESB:
246: unschedule(e);
247: q = &Quad[e->x][e->y];
248:
249:
250: if (q->bases <=0 || q->klings <= 0)
251: break;
252:
253: if (e->x == Ship.quadx && e->y == Ship.quady)
254: {
255:
256: printf("\nSpock: ");
257: killb(Ship.quadx, Ship.quady);
258: }
259: else
260:
261: killb(e->x, e->y);
262: break;
263:
264: case E_ISSUE:
265: xresched(e, E_ISSUE, 1);
266:
267: if (Ship.distressed >= MAXDISTR)
268: break;
269:
270: for (i = 0; i < 100; i++)
271: {
272: ix = ranf(NQUADS);
273: iy = ranf(NQUADS);
274: q = &Quad[ix][iy];
275:
276:
277:
278:
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))
282: break;
283: }
284: if (i >= 100)
285:
286: break;
287:
288:
289: Ship.distressed++;
290: e = xsched(E_ENSLV, 1, ix, iy, q->qsystemname);
291: q->qsystemname = (e - Event) | Q_DISTRESSED;
292:
293:
294: if (!damaged(SSRADIO))
295: {
296: printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
297: Systemname[e->systemname], ix, iy);
298: restcancel++;
299: }
300: else
301:
302: e->evcode |= E_HIDDEN;
303: break;
304:
305: case E_ENSLV:
306: unschedule(e);
307:
308: q = &Quad[e->x][e->y];
309: if (q->klings <= 0)
310: {
311:
312:
313: q->qsystemname = e->systemname;
314: break;
315: }
316:
317:
318: e = schedule(E_REPRO, Param.eventdly[E_REPRO] * franf(), e->x, e->y, e->systemname);
319:
320:
321: if (!damaged(SSRADIO))
322: {
323: printf("\nUhura: We've lost contact with starsystem %s\n",
324: Systemname[e->systemname]);
325: printf(" in quadrant %d,%d.\n",
326: e->x, e->y);
327: }
328: else
329: e->evcode |= E_HIDDEN;
330: break;
331:
332: case E_REPRO:
333:
334: q = &Quad[e->x][e->y];
335: if (q->klings <= 0)
336: {
337: unschedule(e);
338: q->qsystemname = e->systemname;
339: break;
340: }
341: xresched(e, E_REPRO, 1);
342:
343: ix = e->x;
344: iy = e->y;
345: if (Now.klings == 127)
346: break;
347: if (q->klings >= MAXKLQUAD)
348: {
349:
350: for (i = ix - 1; i <= ix + 1; i++)
351: {
352: if (i < 0 || i >= NQUADS)
353: continue;
354: for (j = iy - 1; j <= iy + 1; j++)
355: {
356: if (j < 0 || j >= NQUADS)
357: continue;
358: q = &Quad[i][j];
359:
360: if (q->klings >= MAXKLQUAD || q->stars < 0)
361: continue;
362: break;
363: }
364: if (j <= iy + 1)
365: break;
366: }
367: if (j > iy + 1)
368:
369: break;
370: ix = i;
371: iy = j;
372: }
373:
374: q->klings++;
375: Now.klings++;
376: if (ix == Ship.quadx && iy == Ship.quady)
377: {
378:
379: sector(&ix, &iy);
380: Sect[ix][iy] = KLINGON;
381: k = &Etc.klingon[Etc.nkling++];
382: k->x = ix;
383: k->y = iy;
384: k->power = Param.klingpwr;
385: k->srndreq = 0;
386: compkldist(Etc.klingon[0].dist == Etc.klingon[0].avgdist ? 0 : 1);
387: }
388:
389:
390: Now.time = Now.resource / Now.klings;
391: break;
392:
393: case E_SNAP:
394: xresched(e, E_SNAP, 1);
395: p = (char *) Etc.snapshot;
396: memcpy(p, Quad, sizeof (Quad));
397: p += sizeof(Quad);
398: memcpy(p, Event, sizeof (Event));
399: p += sizeof(Event);
400: memcpy(p, &Now, sizeof (Now));
401: Game.snap = 1;
402: break;
403:
404: case E_ATTACK:
405: if (!Move.resting)
406: {
407: unschedule(e);
408: break;
409: }
410: attack(1);
411: reschedule(e, 0.5);
412: break;
413:
414: case E_FIXDV:
415: i = e->systemname;
416: unschedule(e);
417:
418:
419: printf("%s reports repair work on the %s finished.\n",
420: Device[i].person, Device[i].name);
421:
422:
423: switch (i)
424: {
425:
426: case LIFESUP:
427: Ship.reserves = Param.reserves;
428: break;
429:
430: case SINS:
431: if (Ship.cond == DOCKED)
432: break;
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");
436: Ship.sinsbad = 1;
437: break;
438:
439: case SSRADIO:
440: restcancel = dumpssradio();
441: break;
442: }
443: break;
444:
445: default:
446: break;
447: }
448:
449: if (restcancel && Move.resting && getynpar("Spock: Shall we cancel our rest period"))
450: Move.time = xdate - idate;
451:
452: }
453:
454:
455: if ((e = Now.eventptr[E_ATTACK]) != NULL)
456: unschedule(e);
457:
458: if (!warp)
459: {
460:
461: if (Ship.cloaked)
462: Ship.energy -= Param.cloakenergy * Move.time;
463:
464:
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;
468:
469:
470: if (damaged(LIFESUP) && Ship.cond != DOCKED)
471: Ship.reserves -= Move.time;
472: }
473: return (0);
474: }