-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWHAM.ino
More file actions
590 lines (512 loc) · 17.4 KB
/
WHAM.ino
File metadata and controls
590 lines (512 loc) · 17.4 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
WHAM!
by Move38, Inc. 2019
Lead development by Dan King
original game by Dan King, Jonathan Bobrow
based on concept for Whack-A-Mole
Rules: https://github.com/Move38/WHAM/blob/master/README.md
--------------------
Blinks by Move38
Brought to life via Kickstarter 2018
@madewithblinks
www.move38.com
--------------------
*/
enum gameStates {SETUP, GAME, DEATH, VICTORY};//cycles through the game
byte gameState = SETUP;
byte grassHue = 70;
#define DIFFICULTY_MIN 1 //starting round
#define DIFFICULTY_MAX 15 //final difficulty level, though the game continues
byte difficultyLevel = 0;
#define VICTORY_ROUND_COUNT 30
enum goVictorySignals {INERT, WAVE, SETTLE};//used in game state
byte goVictorySignal = INERT;
byte roundCounter = 0;
Timer roundTimer;
bool roundActive = false;
byte lifeSignal = 0;
enum goStrikeSignals {INERT0, INERT1, INERT2, GO, RESOLVING};//used in game state
byte goStrikeSignal = INERT0;
bool isRippling = false;
#define RIPPLING_INTERVAL 500
Timer ripplingTimer;
#define SETUP_FADE_UP_INTERVAL 500
#define SETUP_RED_INTERVAL 1000
#define SETUP_FADE_DELAY 3000
byte setupFadeFace;
Timer setupFadeTimer;
word redTime;
#define EMERGE_INTERVAL_MAX 2000
#define EMERGE_INTERVAL_MIN 500
#define EMERGE_DRIFT 200
Timer emergeTimer;//triggered when the GO signal is received, interval shrinks as difficultyLevel increases
bool isAbove = false;
#define ABOVE_INTERVAL_MAX 3000
#define ABOVE_INTERVAL_MIN 1500
Timer aboveTimer;
bool isFlashing = false;
#define FLASHING_INTERVAL 500
Timer flashingTimer;
bool isStriking = false;
#define STRIKING_INTERVAL 200
Timer strikingTimer;
byte strikes = 0;//communicated in game mode, incremented which each strike
Color strikeColors[3] = {YELLOW, ORANGE, RED};
bool isSourceOfDeath;
long timeOfDeath;
#define DEATH_ANIMATION_INTERVAL 750
void setup() {
// put your setup code here, to run once:
randomize();
setupFadeFace = random(5);
redTime = SETUP_RED_INTERVAL + random(SETUP_RED_INTERVAL / 2);
setupFadeTimer.set(redTime + SETUP_FADE_UP_INTERVAL + random(SETUP_FADE_DELAY));
}
void loop() {
// put your main code here, to run repeatedly:
switch (gameState) {
case SETUP:
setupLoop();
setupDisplayLoop();
break;
case GAME:
gameLoop();
gameDisplayLoop();
break;
case DEATH:
deathLoop();
deathDisplayLoop();
break;
case VICTORY:
victoryLoop();
victoryDisplayLoop();
}
//dump button data
buttonSingleClicked();
buttonDoubleClicked();
buttonPressed();
//do communication (done here to avoid miscommunication)
byte sendData;
switch (gameState) {
case SETUP:
sendData = (gameState << 4);
break;
case GAME:
sendData = (gameState << 4) + (goStrikeSignal << 1) + (lifeSignal);
break;
case DEATH:
sendData = (gameState << 4);
break;
case VICTORY:
sendData = (gameState << 4) + (goVictorySignal << 2);
}
setValueSentOnAllFaces(sendData);
}
//////////////
//GAME LOOPS//
//////////////
void setupLoop() {
//listen for neighbors with higher player counts and conform
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//a neighbor!
byte neighborGameState = getGameState(getLastValueReceivedOnFace(f));
}
}
//listen for double-clicks to move into game mode
if (buttonDoubleClicked()) {
gameState = GAME;
roundActive = false;
roundTimer.set(EMERGE_INTERVAL_MAX);
isFlashing = true;
flashingTimer.set(FLASHING_INTERVAL);
}
//listen for neighbors in game mode to move to game mode myself and become receiver
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//a neighbor!
byte neighborGameState = getGameState(getLastValueReceivedOnFace(f));
if (neighborGameState == GAME) {
gameState = GAME;
roundActive = false;
roundTimer.set(EMERGE_INTERVAL_MAX);
isFlashing = true;
flashingTimer.set(FLASHING_INTERVAL);
}
}
}
}
void gameLoop() {
//start new round?
if (!roundActive) {
bool newRoundInitiated = false;
//look for neighbors commanding us to start a round
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) { //neighbor!
if (getGameState(getLastValueReceivedOnFace(f)) == GAME) { //a trusted neighbor
if (getGoStrikeSignal(getLastValueReceivedOnFace(f)) == GO) {//telling us to go
newRoundInitiated = true;
}
}
}
}
//listen for internal timer telling us to go
if (!roundActive && roundTimer.isExpired()) {
newRoundInitiated = true;
}
//we get to start a new round!
if (newRoundInitiated) {
roundCounter++;
if (roundCounter > VICTORY_ROUND_COUNT) {//GAME OVER: VICTORY
gameState = VICTORY;
// word emergeInterval = map(difficultyLevel, DIFFICULTY_MIN, DIFFICULTY_MAX, EMERGE_INTERVAL_MAX, EMERGE_INTERVAL_MIN);
word emergeInterval = EMERGE_INTERVAL_MAX - ((difficultyLevel * (EMERGE_INTERVAL_MAX - EMERGE_INTERVAL_MIN)) / (DIFFICULTY_MAX - DIFFICULTY_MIN));
word driftVal = (EMERGE_DRIFT / 3) * random(3);
roundTimer.set(emergeInterval + driftVal);
} else {//GAME IS STILL ON
if (difficultyLevel < DIFFICULTY_MAX) {
difficultyLevel++;
}
//we also need to declare out intention to go up
lifeSignal = random(1);//using this in place of real stuff for a moment
isRippling = true;
ripplingTimer.set(RIPPLING_INTERVAL);
goStrikeSignal = GO;
roundActive = true;
// word emergeInterval = map(difficultyLevel, DIFFICULTY_MIN, DIFFICULTY_MAX, EMERGE_INTERVAL_MAX, EMERGE_INTERVAL_MIN);
word emergeInterval = EMERGE_INTERVAL_MAX - (((difficultyLevel - DIFFICULTY_MIN) * (EMERGE_INTERVAL_MAX - EMERGE_INTERVAL_MIN)) / (DIFFICULTY_MAX - DIFFICULTY_MIN));
emergeTimer.set(emergeInterval + random(EMERGE_DRIFT));
// word aboveInterval = map(difficultyLevel, DIFFICULTY_MIN, DIFFICULTY_MAX, ABOVE_INTERVAL_MAX, ABOVE_INTERVAL_MIN);
word aboveInterval = ABOVE_INTERVAL_MAX - (((difficultyLevel - DIFFICULTY_MIN) * (ABOVE_INTERVAL_MAX - ABOVE_INTERVAL_MIN)) / (DIFFICULTY_MAX - DIFFICULTY_MIN));
word roundInterval = emergeInterval + EMERGE_DRIFT + aboveInterval + FLASHING_INTERVAL + emergeInterval;
roundTimer.set(roundInterval);
}
}
}
//resolve goStrikeSignal propogation
if (goStrikeSignal == GO) {//we are going. Do all our neighbors know this?
bool canResolve = true;
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//neighbor!
if (isGoStrikeInert(getGoStrikeSignal(getLastValueReceivedOnFace(f)))) {//this neighbor has not been told
canResolve = false;
}
}
}
if (canResolve) {
goStrikeSignal = RESOLVING;
}
} else if (goStrikeSignal == RESOLVING) {
bool canInert = true;
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//neighbor!
if (getGoStrikeSignal(getLastValueReceivedOnFace(f)) == GO) {//this neighbor still has work to do
canInert = false;
}
}
}
if (canInert) {
switch (strikes) {
case 0:
goStrikeSignal = INERT0;
break;
case 1:
goStrikeSignal = INERT1;
break;
case 2:
goStrikeSignal = INERT2;
break;
}
}
}
//PLAY THE GAME OF LIFE
if (isGoStrikeInert(goStrikeSignal) && roundActive) {
byte neighborsUp = 0;
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) { //neighbor!
if (getLifeSignal(getLastValueReceivedOnFace(f)) == 1) {
neighborsUp++;
}
}
}
if (neighborsUp == 0) {//too few up neighbors. ARISE!
lifeSignal = 1;
} else if (neighborsUp > 3) {//too many neighbors. DESCEND!
lifeSignal = 0;
}
}
//listen for my emerge timer to expire so I can go above
if (roundActive && emergeTimer.isExpired()) {
roundActive = false;
if (lifeSignal == 1) {
isAbove = true;
// word fadeTime = map(difficultyLevel, DIFFICULTY_MIN, DIFFICULTY_MAX, ABOVE_INTERVAL_MAX, ABOVE_INTERVAL_MIN);
word fadeTime = ABOVE_INTERVAL_MAX - (((difficultyLevel - DIFFICULTY_MIN) * (ABOVE_INTERVAL_MAX - ABOVE_INTERVAL_MIN)) / (DIFFICULTY_MAX - DIFFICULTY_MIN));
aboveTimer.set(fadeTime);
}
}
//listen for button presses
if (buttonPressed()) {
if (isAbove) { //there is a mole here
isAbove = false;//kill the mole
isFlashing = true;//start the flash
flashingTimer.set(FLASHING_INTERVAL);
roundActive = false;
} else {//there is no mole here
strikes++;
//we need to check if we are in an inert state and update that
if (isGoStrikeInert(goStrikeSignal)) { //update our INERT type
switch (strikes) {
case 0:
goStrikeSignal = INERT0;
break;
case 1:
goStrikeSignal = INERT1;
break;
case 2:
goStrikeSignal = INERT2;
break;
}
}
strikingTimer.set(STRIKING_INTERVAL);
isStriking = true;
if (strikes == 3) {
gameState = DEATH;
isSourceOfDeath = true;
}
}
}//end button press check
//listen for strikes from neighbors
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) { //neighbor!
byte neighborGameState = getGameState(getLastValueReceivedOnFace(f));
if (neighborGameState == GAME) { //this neighbor is in game state, so we can trust their communication
if (isGoStrikeInert(getGoStrikeSignal(getLastValueReceivedOnFace(f)))) {//this is an inert neighbror, they communicating
byte neighborStrikes = getStrikes(getLastValueReceivedOnFace(f));
if (neighborStrikes > strikes) { //that neighbor is reporting more strikes than me. Take that number
strikes = neighborStrikes;
if (isGoStrikeInert(goStrikeSignal)) { //update our INERT type
switch (strikes) {
case 0:
goStrikeSignal = INERT0;
break;
case 1:
goStrikeSignal = INERT1;
break;
case 2:
goStrikeSignal = INERT2;
break;
}
}
isStriking = true;
strikingTimer.set(STRIKING_INTERVAL);
}
}
}
}
}
//listen for my mole to cause death
if (isAbove && aboveTimer.isExpired()) { //my fade timer expired and I haven't been clicked, so...
gameState = DEATH;
isSourceOfDeath = true;
timeOfDeath = millis();
}
//listen for death
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//a neighbor!
byte neighborGameState = getGameState(getLastValueReceivedOnFace(f));
if (neighborGameState == DEATH) {
gameState = DEATH;
isSourceOfDeath = false;
timeOfDeath = millis();
}
}
}//end death check
}
void deathLoop() {
setupCheck();
}
void victoryLoop() {
//listen for neighbors in death, because that maybe could happen but probably won't
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//neighbor!
if (getGameState(getLastValueReceivedOnFace(f)) == DEATH) {
gameState = DEATH;
isSourceOfDeath = false;
}
}
}
//randomly flash
if (emergeTimer.isExpired()) {
isFlashing = true;
flashingTimer.set(FLASHING_INTERVAL / 2);
//reset the timer
emergeTimer.set((FLASHING_INTERVAL / 2) + random(FLASHING_INTERVAL / 2));
}
setupCheck();
}
void setupCheck() {
//listen for double clicks to go back to setup
if (buttonDoubleClicked()) {
gameState = SETUP;
resetAllVariables();
}
//listen for signal to go to setup
FOREACH_FACE(f) {
if (!isValueReceivedOnFaceExpired(f)) {//a neighbor!
byte neighborGameState = getGameState(getLastValueReceivedOnFace(f));
if (neighborGameState == SETUP) {
gameState = SETUP;
resetAllVariables();
}
}
}//end setup check
}
void resetAllVariables() {
//RESET ALL GAME VARIABLES
goStrikeSignal = INERT0;
goVictorySignal = INERT;
difficultyLevel = 0;
roundActive = false;
roundCounter = 0;
strikes = 0;
lifeSignal = 0;
isSourceOfDeath = false;
isAbove = false;
isFlashing = false;
isRippling = false;
isStriking = false;
}
/////////////////
//DISPLAY LOOPS//
/////////////////
void setupDisplayLoop() {
setColor(makeColorHSB(grassHue, 255, 255));
if (setupFadeTimer.isExpired()) {
setupFadeFace = (setupFadeFace + random(4)) % 6;
redTime = SETUP_RED_INTERVAL + random(SETUP_RED_INTERVAL / 2);
setupFadeTimer.set(redTime + SETUP_FADE_UP_INTERVAL + random(SETUP_FADE_DELAY));
}
Color fadeColor;
byte saturation;
if (setupFadeTimer.getRemaining() < redTime + SETUP_FADE_UP_INTERVAL) {//we are inside the animation
if (setupFadeTimer.getRemaining() < SETUP_FADE_UP_INTERVAL) {//we are fading from white to green
saturation = 255 - map(setupFadeTimer.getRemaining(), 0, SETUP_FADE_UP_INTERVAL, 0, 255);
fadeColor = makeColorHSB(grassHue, saturation, 255);
} else {//we are red
fadeColor = RED;
}
setColorOnFace(fadeColor, setupFadeFace);
}
}
void gameDisplayLoop() {
//do each animation
if (isFlashing) {//fade from white to green based on flashingTimer
// byte currentSaturation = map(flashingTimer.getRemaining(), 0, FLASHING_INTERVAL, 255, 0);
byte currentSaturation = 255 - ((255 * flashingTimer.getRemaining()) / FLASHING_INTERVAL);
setColor(makeColorHSB(grassHue, currentSaturation, 255));
} else if (isAbove) {//fade from [color] to off based on aboveTimer
long currentInterval = ABOVE_INTERVAL_MAX - (((difficultyLevel - DIFFICULTY_MIN) * (ABOVE_INTERVAL_MAX - ABOVE_INTERVAL_MIN) ) / (DIFFICULTY_MAX - DIFFICULTY_MIN));
byte currentFullPips = (aboveTimer.getRemaining()) / (currentInterval / 6);//6 >>> 0
byte dimmingPipBrightness = map(aboveTimer.getRemaining() - ((currentInterval / 6) * currentFullPips), 0, currentInterval / 6, 0, 255);
FOREACH_FACE(f) {
if (f < currentFullPips) {
setColorOnFace(RED, f);
} else if (f == currentFullPips) {
setColorOnFace(dim(RED, dimmingPipBrightness), f);
} else {
setColorOnFace(OFF, f);
}
//should my face be all the way on?
}
// long currentTime = aboveTimer.getRemaining();
// byte brightnessSubtraction = 255 - ((255 * currentTime) / currentInterval);
// brightnessSubtraction = (brightnessSubtraction * brightnessSubtraction) / 255;
// brightnessSubtraction = (brightnessSubtraction * brightnessSubtraction) / 255;
// byte currentBrightness = 255 - brightnessSubtraction;
// setColor(dim(currentColor, currentBrightness));
} else if (isStriking) {//flash [color] for a moment
//which color? depends on number of strikes
setColor(strikeColors[strikes - 1]);
} else if (isRippling) {//randomize green hue for a moment
FOREACH_FACE(f) {
setColorOnFace(makeColorHSB(grassHue, 255, random(50) + 205), f);
//setColorOnFace(makeColorHSB(grassHue + random(20), 255, 255), f);
}
} else {//just be green
setColor(makeColorHSB(grassHue, 255, 255));
}
//resolve non-death animation timers
if (flashingTimer.isExpired()) {
isFlashing = false;
}
if (strikingTimer.isExpired()) {
isStriking = false;
}
if (ripplingTimer.isExpired()) {
isRippling = false;
}
}
void deathDisplayLoop() {
long currentAnimationPosition = (millis() - timeOfDeath) % (DEATH_ANIMATION_INTERVAL * 2);
byte animationValue;
if (currentAnimationPosition < DEATH_ANIMATION_INTERVAL) { //we are in the down swing (255 >> 0)
// animationValue = map(currentAnimationPosition, 0, DEATH_ANIMATION_INTERVAL, 255, 0);
animationValue = 255 - ((255 * currentAnimationPosition) / DEATH_ANIMATION_INTERVAL);
} else {//we are in the up swing (0 >> 255)
// animationValue = map(currentAnimationPosition - DEATH_ANIMATION_INTERVAL, 0, DEATH_ANIMATION_INTERVAL, 0, 255);
animationValue = ((255 * (currentAnimationPosition - DEATH_ANIMATION_INTERVAL)) / DEATH_ANIMATION_INTERVAL);
}
if (isSourceOfDeath) {
setColor(makeColorHSB(0, animationValue, 255));
} else {
setColor(makeColorHSB(0, 255, animationValue));
}
}
void victoryDisplayLoop() {
if (isFlashing) {//fade from white to green based on flashingTimer
byte currentSaturation = 255 - map(flashingTimer.getRemaining(), 0, FLASHING_INTERVAL, 0, 255);
setColor(makeColorHSB(grassHue, currentSaturation, 255));
}
//resolve flashing animation timers
if (flashingTimer.isExpired()) {
isFlashing = false;
}
}
/////////////////
//COMMUNICATION//
/////////////////
byte getGameState(byte data) {//1st and 2nd bit
return (data >> 4);
}
byte getGoStrikeSignal(byte data) {
return ((data >> 1) & 7);
}
bool isGoStrikeInert (byte data) {//is this neighbor in an inert state?
if (data == INERT0 || data == INERT1 || data == INERT2) {
return true;
} else {
return false;
}
}
byte getStrikes(byte data) {//returns a number of strikes
byte s = getGoStrikeSignal(data);
switch (s) {
case INERT0:
return 0;
break;
case INERT1:
return 1;
break;
case INERT2:
return 2;
break;
default:
return 0;
break;
}
}
byte getLifeSignal(byte data) {//6th bit
return (data & 1);
}
byte getGoVictorySignal(byte data) {//3rd and 4th bit
return ((data >> 2) & 3);
}