-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcrossfire.ino
More file actions
117 lines (103 loc) · 2.78 KB
/
Copy pathcrossfire.ino
File metadata and controls
117 lines (103 loc) · 2.78 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
//Colors
uint8_t white[3] = {255,255,255};
uint8_t dimWhite[3] = {75,75,75};
uint8_t green[3] = {0, 255, 0};
uint8_t immuneCounter = 0;
//Neighbor states
uint8_t neighbors[6];
uint8_t previous_neighbors[6];
//Team Colors, the first color indicates a neutral state
uint8_t teams[3][3] = { { 0, 0, 0 }, { 255, 0, 0 }, { 0, 0, 255 } };
// Dark team colors for team selection and attack indication
uint8_t darkTeams[3][3] = { { 0, 0, 0 }, { 10, 0, 0 }, { 0, 0, 10 } };
uint8_t pastState = 0;
//Has the tile been saved from being turned
boolean saved = false;
//States
boolean reset_state = false;
boolean attacking = false;
boolean underAttack = false;
void setup() {
setTimeout( 600000 );
setState( 15 );
setColor( teams[ 0 ] );
setButtonCallback( on_button_down );
setLongButtonCallback( reset_team_color, 4000 );
}
void on_button_down () {
if ( reset_state ) {
uint8_t teamState = getState() + 1;
if ( teamState > 2 ) {
teamState = 1;
}
setState( teamState );
setColor( darkTeams[ teamState ] );
setTimerCallbackTime( 5000 );
}
else if ( underAttack && getState() != 15 ) {
saved = true;
setState( pastState );
setColor( green );
setTimerCallbackTime( 2000 );
}
else if ( getState() != 15 ){
attacking = true;
setColor( darkTeams[ getState() ] );
setState( getState() + 2 );
setTimerCallback( done_attacking, 2000 );
}
}
void done_attacking () {
if ( getState() > 2 ) {
setState( getState() - 2 );
setColor( teams[ getState() ] );
attacking = false;
}
}
void reset_team_color () {
setState( 15 );
setColor( dimWhite );
reset_state = true;
attacking = false;
underAttack = false;
setTimerCallback( lock_team_color, 5000 );
}
void lock_team_color() {
reset_state = false;
if ( getState() == 15 ) {
setColor( white );
} else {
setColor( teams[ getState() ] );
}
}
void resolve_attack () {
if ( saved ) {
setState( pastState );
// setColor( teams[ pastState ] );
saved = false;
} else {
setColor( teams[ getState() ] );
}
underAttack = false;
}
void loop() {
getNeighborStates( neighbors );
for( uint8_t i = 0; i < 6; i++ ) {
if ( !saved && neighbors[ i ] != previous_neighbors[ i ] && ( neighbors[ i ] == 3 || neighbors[ i ] == 4 ) ) {
pastState = getState();
setState( neighbors[ i ] - 2 );
if ( pastState < 3 ) {
setColor( darkTeams[ neighbors[ i ] - 2 ] );
underAttack = true;
setTimerCallback( resolve_attack, 2000 );
} else {
setState( neighbors[ i ] - 2 );
setColor( teams[ neighbors[ i ] - 2 ] );
}
break;
}
}
for( uint8_t i = 0; i < 6; i++ ) {
previous_neighbors[i] = neighbors[i];
}
}