-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.go
399 lines (364 loc) · 11.6 KB
/
portal.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
397
398
399
package animation
import (
"fmt"
"image/color"
"math/rand"
"os"
"strconv"
"time"
"github.com/TeamNorCal/animation/model"
ingressModel "github.com/TeamNorCal/mawt/model"
)
// Enacapsulates a model of a portal from the perpsective of animations.
// Provides an API semantically meaningful to Ingress
const (
// Number of pixels in each window
windowSize = 30
// Number of resonators
numResos = 8
// Number of windows in the tower
numShaftWindows = 16
// How much do we dim the reso by?
resoDimRatio = 0.7
// Length of reso pulse
resoPulseDuration = 3 * time.Second
)
// resoPositionToIndex maps a resonator position string (from Techthulu perspective)
// to an index, 0-7 into the internal status resonator array
var resoPositionToIndex = map[string]int{
"N": 0,
"NE": 1,
"E": 2,
"SE": 3,
"S": 4,
"SW": 5,
"W": 6,
"NW": 7,
}
// resonatorLevelColors is an array of colors of resonators of various levels, 0-8
var resonatorLevelColors = []uint32{
0x000000, // L0
0xEE8800, // L1
0xFF6600, // L2
0xCC3300, // L3
0x990000, // L4
0xFF0033, // L5
0xCC0066, // L6
0x990066, //0x660066, // L7
0x660066, //0x330033, // L8
}
func init() {
// logger.Println("Initializing...")
// Set up universes
Universes = make(map[string]Universe)
idx := 0
// The resonators at the base
for reso := 1; reso <= 8; reso++ {
name := "base" + strconv.Itoa(reso)
Universes[name] = Universe{
Index: idx,
Size: windowSize,
}
idx++
}
// The, umm, shaft
for level := 1; level <= 8; level++ {
for window := 1; window <= 2; window++ {
name := "towerLevel" + strconv.Itoa(level) + "Window" + strconv.Itoa(window)
Universes[name] = Universe{
Index: idx,
Size: windowSize,
}
idx++
}
}
}
// NewPortal creates a new portal structure
func NewPortal() *Portal {
sizes := make([]uint, numShaftWindows)
for idx := range sizes {
sizes[idx] = windowSize
}
frameBuf := make([]model.ChannelData, numResos+numShaftWindows)
for idx := range frameBuf {
frameBuf[idx] = model.ChannelData{model.OpcChannel(idx + 1), make([]color.RGBA, windowSize)}
}
resoBufs := make([]animCircBuf, 0, numResos)
for idx := 0; idx < numResos; idx++ {
resoBufs = append(resoBufs, newAnimCircBuf())
}
return &Portal{
currentStatus: &PortalStatus{NEU, 0.0, 0.0, make([]ResonatorStatus, numResos)},
sr: NewSequenceRunner(sizes),
seqBuf: newSeqCircBuf(),
resonators: resoBufs,
frameBuf: frameBuf,
}
}
func externalStatusToInternal(external *ingressModel.Status) (status *PortalStatus) {
status = &PortalStatus{
Faction: NEU,
Level: external.Level,
Health: external.Health,
Resonators: make([]ResonatorStatus, numResos),
}
switch external.Faction {
case "E":
status.Faction = ENL
case "R":
status.Faction = RES
case "N":
default:
fmt.Printf("Treating unexpected faction in external status as neutral: '%s'\n", external.Faction)
}
resos := make([]ResonatorStatus, numResos)
resosByIndex := make(map[int]*ingressModel.Resonator)
for resoIdx, reso := range external.Resonators {
index, isPresent := resoPositionToIndex[reso.Position]
if isPresent {
resosByIndex[index] = &external.Resonators[resoIdx]
} else {
fmt.Fprintf(os.Stderr, "Resonator with unknown position %v ignored: %v",
reso.Position, reso)
}
}
for idx := range resos {
if reso, isPresent := resosByIndex[idx]; isPresent {
status.Resonators[idx] = ResonatorStatus{
Health: reso.Health,
Level: int(reso.Level),
}
} else {
// Undeployed resonator
status.Resonators[idx] = ResonatorStatus{
Health: 0.0,
Level: 0,
}
}
}
return status
}
// UpdateFromCanonicalStatus updates the animation with the status of the portal,
// using the canonical Status type
func (p *Portal) UpdateFromCanonicalStatus(status *ingressModel.Status) {
p.UpdateStatus(externalStatusToInternal(status))
}
// UpdateStatus updates the status of the portal from an animation perspective
func (p *Portal) UpdateStatus(status *PortalStatus) {
newStatus := status.deepCopy()
if p.currentStatus.Faction != newStatus.Faction || p.currentStatus.Level != newStatus.Level {
p.updatePortal(status)
}
for idx, status := range newStatus.Resonators {
if status != p.currentStatus.Resonators[idx] {
p.updateResonator(idx, &status)
}
}
p.currentStatus = newStatus.deepCopy()
}
// GetFrame gets frame data for the portal, returning an array of frame data
// for each universe in the portal. Indices into this array are specified in the
// Universes map
// The returned buffers will typically be reused between frames, so callers
// should not hold onto references to them nor modify them!
func (p *Portal) GetFrame(frameTime time.Time) []model.ChannelData {
// Update resonators
for idx := 0; idx < numResos; idx++ {
p.getResoFrame(idx, frameTime)
}
seqDone := p.sr.ProcessFrame(frameTime)
for idx := 0; idx < numShaftWindows; idx++ {
p.frameBuf[numResos+idx].Data = p.sr.UniverseData(uint(idx))
// Scale owned portal's brightness by health, to within the top half of the brightness range
if p.currentStatus.Faction != NEU {
applyBrightness(p.frameBuf[numResos+idx].Data, p.currentStatus.Health/200.0+0.5)
}
}
if seqDone {
if nextSeq := p.seqBuf.dequeue(); nextSeq != nil {
p.sr.InitSequence(nextSeq, frameTime)
}
}
return p.frameBuf
}
func createFadePulseSeq(c color.Color, pulseDuration time.Duration) *Sequence {
seq := NewSequence()
for uniID := 0; uniID < numShaftWindows; uniID++ {
idStr := strconv.Itoa(uniID)
fadeOut := &Step{
UniverseID: uint(uniID),
Effect: NewInterpolateToHexRGB(0x000000, time.Second),
}
seq.AddInitialStep("fadeOut"+idStr, fadeOut)
pulse := &Step{
UniverseID: uint(uniID),
Effect: NewPulse(RGBAFromRGBHex(0x000000), c, pulseDuration, true),
}
seq.AddStep("pulse"+idStr, pulse)
fadeOut.ThenDoImmediately("pulse" + idStr)
}
return seq
}
func (p *Portal) createOwnedPortalSeq(newStatus *PortalStatus) {
var c uint32
switch newStatus.Faction {
case NEU:
c = 0xffffff
case ENL:
c = 0x00ff00
case RES:
c = 0x0000ff
}
stepMap := make(map[string]*Step)
for uniID := 0; uniID < numShaftWindows; uniID++ {
createWindowFadeInOut(stepMap, uniID, c, time.Duration(125.0*newStatus.Level)*time.Millisecond)
}
seq := NewSequence()
for name, step := range stepMap {
seq.AddStep(name, step)
}
// Create dependencies between steps, to create a cycle
for uniID := 0; uniID < numShaftWindows; uniID++ {
idStr := strconv.Itoa(uniID)
// Link the three phases within each universe
stepMap["in"+idStr].ThenDoImmediately("solid" + idStr)
stepMap["solid"+idStr].ThenDoImmediately("out" + idStr)
// Then do cross-universe linking, with fades overlapping
nextID := (uniID + 2) % numShaftWindows
stepMap["in"+idStr].ThenDoImmediately("in" + strconv.Itoa(nextID))
}
// Add the initial operation to kick it off - two cycles at once
seq.AddInitialOperation(Operation{StepName: "in0"})
seq.AddInitialOperation(Operation{StepName: "in1"})
p.seqBuf.clear() // Clear any still-pending sequences from before
p.seqBuf.enqueue(seq)
takeOverPulse := createFadePulseSeq(RGBAFromRGBHex(c), 1500*time.Millisecond)
p.sr.InitSequence(takeOverPulse, time.Now())
}
func (p *Portal) createNeutralPortalSeq(newStatus *PortalStatus) {
seq := NewSequence()
for uniID := 0; uniID < numShaftWindows; uniID++ {
idStr := strconv.Itoa(uniID)
fadeOut := &Step{
UniverseID: uint(uniID),
Effect: NewInterpolateToHexRGB(0x000000, time.Second),
}
seq.AddInitialStep("fadeOut"+idStr, fadeOut)
pulseIn := &Step{
UniverseID: uint(uniID),
Effect: NewInterpolateToHexRGB(0xff0000, 250*time.Millisecond),
}
seq.AddStep("pulseIn"+idStr, pulseIn)
fadeOut.ThenDoImmediately("pulseIn" + idStr)
pulseOut := &Step{
UniverseID: uint(uniID),
Effect: NewInterpolateToHexRGB(0x000000, 1500*time.Millisecond),
}
seq.AddStep("pulseOut"+idStr, pulseOut)
pulseIn.ThenDoImmediately("pulseOut" + idStr)
fadeIn := &Step{
UniverseID: uint(uniID),
Effect: NewInterpolateToHexRGB(0xaaaaaa, time.Second),
}
seq.AddStep("fadeIn"+idStr, fadeIn)
pulseOut.ThenDo("fadeIn"+idStr, time.Duration(rand.Intn(3000))*time.Millisecond)
solid := &Step{
UniverseID: uint(uniID),
Effect: NewSolid(RGBAFromRGBHex(0xaaaaaa)),
}
seq.AddStep("solid"+idStr, solid)
fadeIn.ThenDoImmediately("solid" + idStr)
}
p.seqBuf.clear()
p.sr.InitSequence(seq, time.Now())
}
func (p *Portal) updatePortal(newStatus *PortalStatus) {
if p.currentStatus.Faction != newStatus.Faction {
// Faction change
if newStatus.Faction == ENL || newStatus.Faction == RES {
p.createOwnedPortalSeq(newStatus)
} else {
p.createNeutralPortalSeq(newStatus)
}
} else if p.currentStatus.Level != newStatus.Level {
if newStatus.Faction == ENL || newStatus.Faction == RES {
updateHoldTime(&p.sr.currSeq, time.Duration(125.0*newStatus.Level)*time.Millisecond)
}
}
}
func createWindowFadeInOut(stepMap map[string]*Step, uniID int, color uint32, holdTime time.Duration) {
idStr := strconv.Itoa(uniID)
in := &Step{
Effect: NewInterpolateToHexRGB(color, 250*time.Millisecond),
UniverseID: uint(uniID),
}
stepMap["in"+idStr] = in
solid := &Step{
Effect: NewTimedSolid(RGBAFromRGBHex(color), holdTime),
UniverseID: uint(uniID),
}
stepMap["solid"+idStr] = solid
out := &Step{
Effect: NewInterpolateToHexRGB(0x000000, 500*time.Millisecond),
UniverseID: uint(uniID),
}
stepMap["out"+idStr] = out
}
func updateHoldTime(seq *Sequence, holdTime time.Duration) {
for uniID := 0; uniID < numShaftWindows; uniID++ {
if step := seq.steps["solid"+strconv.Itoa(uniID)]; step != nil {
step.Effect.(*Solid).duration = holdTime
}
}
}
func (p *Portal) updateResonator(index int, newStatus *ResonatorStatus) {
currStatus := p.currentStatus.Resonators[index]
if currStatus.Level != newStatus.Level {
// A change
if newStatus.Level == 0 {
// Clear current animations, then fade to black and stay there
p.resonators[index].clear()
p.resonators[index].enqueue(NewInterpolateToHexRGB(0x000000, time.Second))
p.resonators[index].enqueue(NewSolid(RGBAFromRGBHex(0x000000)))
} else {
// Clear current animations, then fade to new nominal reso color and pulse
resoColor := resonatorLevelColors[newStatus.Level]
p.resonators[index].clear()
// logger.Printf("Enqueuing 2 animations for index %d\n", index)
p.resonators[index].enqueue(NewInterpolateToHexRGB(resoColor, time.Second))
p.resonators[index].enqueue(NewDimmingPulse(RGBAFromRGBHex(resoColor), resoDimRatio, resoPulseDuration))
}
p.resonators[index].peek().Start(time.Now())
}
}
// getResoFrame updates the frame buffer for the specified resonator with data
// for the current frame, with specified frame time
func (p *Portal) getResoFrame(index int, frameTime time.Time) {
currAnim := p.resonators[index].peek()
if currAnim == nil {
return
}
buf, done := currAnim.Frame(p.frameBuf[index].Data, frameTime)
applyBrightness(p.frameBuf[index].Data, p.currentStatus.Resonators[index].Health/100.0)
p.frameBuf[index].Data = buf
// Resonator animations run in a continuous loop, so restart if done
if done {
if p.resonators[index].size() == 1 {
// Only 1 animation - restart it
currAnim.Start(frameTime)
} else {
// More animations queued - move on to the Next
// We know it's > 1 because we returned early if there were 0
// logger.Printf("Dequeuing animation for index %d\n", index)
p.resonators[index].dequeue()
p.resonators[index].peek().Start(frameTime)
}
}
}
func applyBrightness(buf []color.RGBA, brightness float32) {
for idx := range buf {
buf[idx].R = uint8(float32(buf[idx].R) * brightness)
buf[idx].G = uint8(float32(buf[idx].G) * brightness)
buf[idx].B = uint8(float32(buf[idx].B) * brightness)
}
}