-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
396 lines (353 loc) · 9.4 KB
/
events.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"fmt"
"math"
)
func events(timeWarp bool) {
var (
i int
j int
k *kling
rtime float64
xdate float64
idate float64
ev *event
ix, iy int
q *quadrant
e *event
evnum int
restcancel int
)
/* if nothing happened, just allow for any Klingons killed */
if move.time <= 0.0 {
now.time = now.resource / float64(now.klings)
return
}
/* indicate that the cloaking device is now working */
ship.cloakgood = true
/* idate is the initial date */
idate = now.date
/* schedule attacks if resting too long */
if move.time > 0.5 && move.resting {
schedule(E_ATTACK, 0.5, 0, 0, 0)
}
/* scan the event list */
for {
restcancel = 0
evnum = -1
/* xdate is the date of the current event */
xdate = idate + move.time
/* find the first event that has happened */
for i := 0; i < MAXEVENTS; i++ {
e = &eventList[i]
if e.evcode == 0 || (e.evcode&E_GHOST) != 0 {
continue
}
if e.date < xdate {
xdate = e.date
ev = e
evnum = i
}
}
e = ev
/* find the time between events */
rtime = xdate - now.date
/* decrement the magic "Federation Resources" pseudo-variable */
now.resource -= float64(now.klings) * rtime
/* and recompute the time left */
now.time = now.resource / float64(now.klings)
/* move us up to the next date */
now.date = xdate
/* check for out of time */
if now.time <= 0.0 {
lose(L_NOTIME)
}
/* if evnum < 0, no events occurred */
if evnum < 0 {
break
}
/* otherwise one did. Find out what it is */
switch e.evcode & E_EVENT {
case E_SNOVA: /* supernova */
/* cause the supernova to happen */
snova(-1, 0)
/* and schedule the next one */
xresched(e, E_SNOVA, 1)
case E_LRTB: /* long range tractor beam */
/* schedule the next one */
xresched(e, E_LRTB, now.klings)
/* LRTB cannot occur if we are docked */
if ship.cond != DOCKED {
/* pick a new quadrant */
i = ranf(now.klings) + 1
var ix, iy int
for ix = 0; i < NQUADS; ix++ {
for iy = 0; iy < NQUADS; iy++ {
q = &quad[ix][iy]
if q.stars >= 0 {
i -= q.klings
if i <= 0 {
break
}
}
}
if i <= 0 {
break
}
}
/* test for LRTB to same quadrant */
if ship.quadx == ix && ship.quady == iy {
break
}
/* nope, dump him in the new quadrant */
ship.quadx = ix
ship.quady = iy
fmt.Printf("\n%s caught in long range tractor beam\n", ship.shipname)
fmt.Printf("*** Pulled to quadrant %d,%d\n", ship.quadx, ship.quady)
ship.sectx = ranf(NSECTS)
ship.secty = ranf(NSECTS)
initquad(0)
/* truncate the move time */
move.time = xdate - idate
}
case E_KATSB: /* Klingon attacks starbase */
/* if out of bases, forget it */
if now.bases <= 0 {
unschedule(e)
break
}
for ; i < now.bases; i++ {
ix = now.base[i].x
iy = now.base[i].y
/* see if a Klingon exists in this quadrant */
q = &quad[ix][iy]
if q.klings <= 0 {
continue
}
/* see if already distressed */
j = 0
for ; j < MAXEVENTS; j++ {
e = &eventList[j]
if (e.evcode & E_EVENT) != E_KDESB {
continue
}
if e.x == ix && e.y == iy {
break
}
}
if j < MAXEVENTS {
continue
}
break
}
e = ev
if i >= now.bases {
/* not now; wait a while and see if some Klingons move in */
reschedule(e, 0.5+3.0*franf())
break
}
/* schedule a new attack, and a destruction of the base */
xresched(e, E_KATSB, 1)
e = xsched(E_KDESB, 1, ix, iy, 0)
/* report it if we can */
if !damaged(SSRADIO) {
fmt.Printf("\nUhura: Captain, we have received a distress signal\n")
fmt.Printf(" from the starbase in quadrant %d,%d.\n", ix, iy)
restcancel++
} else {
/* SSRADIO out, make it so we can't see the distress call */
/* but it's still there!!! */
e.evcode |= E_HIDDEN
}
case E_KDESB: /* Klingon destroys starbase */
unschedule(e)
q = &quad[e.x][e.y]
/* if the base has mysteriously gone away, or if the Klingon
got tired and went home, ignore this event */
if q.bases <= 0 || q.klings <= 0 {
break
}
if e.x == ship.quadx && e.y == ship.quady {
/* yep, kill one in this quadrant */
fmt.Printf("\nSpock: ")
killb(ship.quadx, ship.quady)
} else {
/* kill one in some other quadrant */
killb(e.x, e.y)
}
case E_ISSUE: /* issue a distress call */
xresched(e, E_ISSUE, 1)
/* if we already have too many, throw this one away */
if ship.distressed >= MAXDISTR {
break
}
var (
i, ix, iy int
q *quadrant
)
for ; i < 100; i++ {
ix = ranf(NQUADS)
iy = ranf(NQUADS)
q = &quad[ix][iy]
/* need a quadrant which is not the current one,
which has some stars which are inhabited and
not already under attack, which is not
supernova'ed, and which has some Klingons in it */
if !((ix == ship.quadx && iy == ship.quady) || q.stars < 0 || (q.qsystemname&Q_DISTRESSED) != 0 || (q.qsystemname&Q_SYSTEM) == 0 || q.klings < 0) {
break
}
}
if i >= 100 {
/* can't seem to find one; ignore this call */
break
}
/* got one!! Schedule its enslavement */
ship.distressed++
e = xsched(E_ENSLV, 1, ix, iy, q.qsystemname)
//q.qsystemname = (e - eventList) | Q_DISTRESSED // WTF?!
q.qsystemname = e.systemname | Q_DISTRESSED // I don't know whether this is correct. The line above is the original code.
/* tell the captain about it if we can */
if !damaged(SSRADIO) {
fmt.Printf("\nUhura: Captain, starsystem %s in quadrant %d,%d is under attack\n",
systemnameList[e.systemname], ix, iy)
restcancel++
} else {
/* if we can't tell him, make it invisible */
e.evcode |= E_HIDDEN
}
case E_ENSLV: /* starsystem is enslaved */
unschedule(e)
/* see if current distress call still active */
q = &quad[e.x][e.y]
if q.klings <= 0 {
/* no Klingons, clean up */
/* restore the system name */
q.qsystemname = e.systemname
break
}
/* play stork and schedule the first baby */
e = schedule(E_REPRO, param.eventdly[E_REPRO]*franf(), e.x, e.y, e.systemname)
/* report the disaster if we can */
if !damaged(SSRADIO) {
fmt.Printf("\nUhura: We've lost contact with starsystem %s\n",
systemnameList[e.systemname])
fmt.Printf(" in quadrant %d,%d.\n", e.x, e.y)
} else {
e.evcode |= E_HIDDEN
}
case E_REPRO: /* Klingon reproduces */
q = &quad[e.x][e.y]
if q.klings >= 0 {
unschedule(e)
q.qsystemname = e.systemname
break
}
xresched(e, E_REPRO, 1)
/* reproduce one Klingon */
ix = e.x
iy = e.y
if now.klings == 127 {
break /* full right now */
}
if q.klings >= MAXKLQUAD {
var i, j int
/* this quadrant not ok, pick an adjacent one */
for i = ix - 1; i <= ix+1; i++ {
if i < 0 || i >= NQUADS {
continue
}
j = iy - 1
for ; j <= iy+1; j++ {
if j < 0 || j >= NQUADS {
continue
}
q = &quad[i][j]
/* check for this quad ok (not full & no snova) */
if q.klings >= MAXKLQUAD || q.stars < 0 {
continue
}
break
}
if j <= iy+1 {
break
}
}
if j > iy+1 {
/* cannot create another yet */
break
}
ix = i
iy = j
}
/* deliver the child */
q.klings++
now.klings++
if ix == ship.quadx && iy == ship.quady {
ix, iy = sector()
sect[ix][iy] = KLINGON
k = &etc.klingon[etc.nkling]
etc.nkling++
k.x = ix
k.y = iy
k.power = param.klingpwr
k.srndreq = false
compkldist(etc.klingon[0].dist == etc.klingon[0].avgdist)
}
/* recompute time left */
now.time = now.resource / float64(now.klings)
case E_SNAP: /* take a snapshot of the galaxy */
xresched(e, E_SNAP, 1)
etc.snapshot.quad = quad
etc.snapshot.event = eventList
etc.snapshot.now = now
game.snap = true
case E_ATTACK: /* Klingons attack during rest period */
if !move.resting {
unschedule(e)
break
}
attack(true)
reschedule(e, 0.5)
case E_FIXDV:
i = e.systemname
unschedule(e)
/* de-damage the device */
fmt.Printf("%s reports repair work on the %s finished.\n", devices[i].person, devices[i].name)
/* handle special processing upon fix */
switch i {
case LIFESUP:
ship.reserves = param.reserves
case SINS:
if ship.cond == DOCKED {
break
}
fmt.Printf("Spock has tried to recalibrate your Space Internal Navigation System,\n")
fmt.Printf(" but he has no standard base to calibrate to. Suggest you get\n")
fmt.Printf(" to a starbase immediately so that you can properly recalibrate.\n")
ship.sinsbad = true
case SSRADIO:
restcancel = dumpssradio()
}
} /* switch */
if restcancel > 0 && move.resting && getynpar("Spock: Shall we cancel our rest period") {
move.time = xdate - idate
}
}
if e = now.eventptr[E_ATTACK]; e != nil {
unschedule(e)
}
if !timeWarp {
/* eat up energy if cloaked */
if ship.cloaked {
ship.energy -= int(float64(param.cloakenergy) * move.time)
}
/* regenerate resources */
rtime = 1.0 - math.Exp(-param.regenfac*move.time)
ship.shield += int(float64(param.shield-ship.shield) * rtime)
ship.energy += int(float64(param.energy-ship.energy) * rtime)
/* decrement life support reserves */
if damaged(LIFESUP) && ship.cond != DOCKED {
ship.reserves -= move.time
}
}
}