-
Notifications
You must be signed in to change notification settings - Fork 0
/
phaser.go
263 lines (242 loc) · 5.53 KB
/
phaser.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
package main
import (
"fmt"
"math"
)
const (
ALPHA = 3.0 /* spread */
BETA = 3.0 /* franf() */
GAMMA = 0.30 /* cos(angle) */
EPSILON = 150.0 /* dist ** 2 */
OMEGA = 10.596 /* overall scaling factor */
)
var matab = []cvntab{
{abrev: "m", full: "manual", boolValue: true},
{abrev: "a", full: "automatic", boolValue: false},
}
type banks struct {
units int
angle float64
spread float64
}
func phaser(v int) {
var (
i, j int
k *kling
dx, dy float64
anglefactor, distfactor float64
b *banks
flag, extra int
manual bool
hit int
tot float64
n int
hitreqd [NBANKS]int
bank [NBANKS]banks
ptr *cvntab
)
if ship.cond == DOCKED {
fmt.Printf("Phasers cannot fire through starbase shields\n")
return
}
if damaged(PHASER) {
out(PHASER)
return
}
if ship.shldup {
fmt.Printf("Sulu: Captain, we cannot fire through shields.\n")
return
}
if ship.cloaked {
fmt.Printf("Sulu: Captain, surely you must realize that we cannot fire\n")
fmt.Printf(" phasers with the cloaking device up.\n")
return
}
/* decide if we want manual or automatic mode */
manual = false
if testnl() {
if damaged(COMPUTER) {
fmt.Printf("%s", devices[COMPUTER].name)
manual = true
} else if damaged(SRSCAN) {
fmt.Printf("%s", devices[SRSCAN].name)
manual = true
}
if manual {
fmt.Printf(" damaged, manual mode selected\n")
}
}
if !manual {
ptr = getcodpar("Manual or automatic", matab)
manual = ptr.boolValue
}
if !manual && damaged(COMPUTER) {
fmt.Printf("Computer damaged, manual selected\n")
skiptonl()
manual = true
}
flag = 1
if manual {
for flag != 0 {
fmt.Printf("%d units available\n", ship.energy)
extra = 0
flag = 0
for i := 0; i < NBANKS; i++ {
b = &bank[i]
fmt.Printf("\nBank %d:\n", i)
hit = getintpar("units")
if hit < 0 {
return
}
extra += hit
if extra > ship.energy {
fmt.Printf("available energy exceeded. ")
skiptonl()
flag++
break
}
b.units = hit
hit = getintpar("course")
if hit < 0 || hit > 360 {
return
}
b.angle = float64(hit) * 0.0174532925
b.spread = getfltpar("spread")
if b.spread < 0 || b.spread > 1 {
return
}
}
ship.energy -= extra
}
extra = 0
} else {
/* automatic distribution of power */
if etc.nkling <= 0 {
fmt.Printf("Sulu: But there are no Klingons in this quadrant\n")
return
}
fmt.Printf("Phasers locked on target. ")
for flag != 0 {
fmt.Printf("%d units available\n", ship.energy)
hit = getintpar("Units to fire")
if hit <= 0 {
return
}
if hit > ship.energy {
fmt.Printf("available energy exceeded. ")
skiptonl()
continue
}
flag = 0
ship.energy -= hit
extra = hit
n = etc.nkling
if n > NBANKS {
n = NBANKS
}
tot = float64(n * (n + 1) / 2)
for i = 0; i < n; i++ {
k = &etc.klingon[i]
b = &bank[i]
distfactor = k.dist
anglefactor = ALPHA * BETA * OMEGA / (distfactor*distfactor + EPSILON)
anglefactor *= GAMMA
distfactor = float64(k.power)
distfactor /= anglefactor
hitreqd[i] = int(distfactor + 0.5)
dx = float64(ship.sectx - k.x)
dy = float64(k.y - ship.secty)
b.angle = math.Atan2(dy, dx)
b.spread = 0.0
b.units = int((float64(n-i) / tot) * float64(extra))
tracef("phasers: b%d hr%d u%d df%.2f af%.2f a%.2f", i, hitreqd[i], b.units, distfactor, anglefactor, b.angle)
extra -= b.units
hit = b.units - hitreqd[i]
if hit > 0 {
extra += hit
b.units -= hit
}
}
/* give out any extra energy we might have around */
if extra > 0 {
for i := 0; i < n; i++ {
b = &bank[i]
hit = hitreqd[i] - b.units
if hit <= 0 {
continue
}
if hit >= extra {
b.units += extra
extra = 0
break
}
b.units = hitreqd[i]
extra -= hit
}
if extra > 0 {
fmt.Printf("%d units overkill\n", extra)
}
}
}
}
for i := 0; i < NBANKS; i++ {
b = &bank[i]
if b.units > 0 {
tracef("b%d u%d a%.2f s%.f", i, b.units, b.angle, b.spread)
} else {
tracef("b%d u%d", i, b.units)
}
}
move.free = false
for i = 0; i < NBANKS; i++ {
b = &bank[i]
if b.units <= 0 {
continue
}
fmt.Printf("\nPhaser bank %d fires:\n", i)
n = etc.nkling
kidx := 0
for j = 0; j < n; j++ {
if b.units <= 0 {
break
}
k = &etc.klingon[kidx]
// TODO: copy documentation
distfactor = BETA + franf()
distfactor *= ALPHA + b.spread
distfactor *= OMEGA
anglefactor = k.dist
distfactor /= anglefactor*anglefactor + EPSILON
distfactor *= float64(b.units)
dx = float64(ship.sectx - k.x)
dy = float64(k.y - ship.secty)
anglefactor = math.Atan2(dy, dx) - b.angle
anglefactor = math.Cos((anglefactor * b.spread) + GAMMA)
tracef("fire: b%d k%d af%.2f d%.2f sp%.2f", i, j, anglefactor, distfactor, b.spread)
if anglefactor < 0.0 {
kidx++
continue
}
hit = int(anglefactor*distfactor + 0.5)
k.power -= hit
fmt.Printf("%d unit hit on Klingon", hit)
if !damaged(SRSCAN) {
fmt.Printf(" at %d,%d", k.x, k.y)
}
fmt.Printf("\n")
b.units -= hit
if k.power <= 0 {
killk(k.x, k.y)
continue
}
kidx++
}
}
/* computer overkill */
for i = 0; i < NBANKS; i++ {
extra += bank[i].units
}
if extra > 0 {
fmt.Printf("\n%d units expended on empty space\n", extra)
}
}