75 {
76 unsigned long age = millis() -
birthday;
77
79 return;
80
82
83
85
87 }
88
90#ifdef DEBUG_ADVANCEMENT
91 Serial.print("Ripple ");
93 Serial.println(" advancing:");
94#endif
95
100 }
101 else {
102#ifdef DEBUG_ADVANCEMENT
103 Serial.print(" Picking direction out of node ");
105 Serial.print(" with agr. ");
107#endif
108
109 int newDirection = -1;
110
111 int sharpLeft = (
position[1] + 1) % 6;
112 int wideLeft = (
position[1] + 2) % 6;
113 int forward = (
position[1] + 3) % 6;
114 int wideRight = (
position[1] + 4) % 6;
115 int sharpRight = (
position[1] + 5) % 6;
116
118
119
121
122 while (newDirection < 0) {
123 if (anger == 0) {
125
126 if (forwardConnection < 0) {
127
128#ifdef DEBUG_ADVANCEMENT
129 Serial.println(" Can't go straight - picking more agr. path");
130#endif
131 anger++;
132 }
133 else {
134#ifdef DEBUG_ADVANCEMENT
135 Serial.println(" Going forward");
136#endif
137 newDirection = forward;
138 }
139 }
140
141 if (anger == 1) {
144
145 if (leftConnection >= 0 && rightConnection >= 0) {
146#ifdef DEBUG_ADVANCEMENT
147 Serial.println(" Turning left or right at random");
148#endif
149 newDirection = random(2) ? wideLeft : wideRight;
150 }
151 else if (leftConnection >= 0) {
152#ifdef DEBUG_ADVANCEMENT
153 Serial.println(" Can only turn left");
154#endif
155 newDirection = wideLeft;
156 }
157 else if (rightConnection >= 0) {
158#ifdef DEBUG_ADVANCEMENT
159 Serial.println(" Can only turn right");
160#endif
161 newDirection = wideRight;
162 }
163 else {
164#ifdef DEBUG_ADVANCEMENT
165 Serial.println(" Can't make wide turn - picking more agr. path");
166#endif
167 anger++;
168 }
169 }
170
171 if (anger == 2) {
174
175 if (leftConnection >= 0 && rightConnection >= 0) {
176#ifdef DEBUG_ADVANCEMENT
177 Serial.println(" Turning left or right at random");
178#endif
179 newDirection = random(2) ? sharpLeft : sharpRight;
180 }
181 else if (leftConnection >= 0) {
182#ifdef DEBUG_ADVANCEMENT
183 Serial.println(" Can only turn left");
184#endif
185 newDirection = sharpLeft;
186 }
187 else if (rightConnection >= 0) {
188#ifdef DEBUG_ADVANCEMENT
189 Serial.println(" Can only turn right");
190#endif
191 newDirection = sharpRight;
192 }
193 else {
194#ifdef DEBUG_ADVANCEMENT
195 Serial.println(" Can't make tight turn - picking less agr. path");
196#endif
197 anger--;
198 }
199 }
200
201
202
203
204 }
205 }
207 for (int i = 1; i < 6; i++) {
208 int possibleDirection = (
position[1] + i) % 6;
209
211 newDirection = possibleDirection;
212 break;
213 }
214 }
215
216#ifdef DEBUG_ADVANCEMENT
217 Serial.println(" Turning as rightward as possible");
218#endif
219 }
221 for (int i = 5; i >= 1; i--) {
222 int possibleDirection = (
position[1] + i) % 6;
223
225 newDirection = possibleDirection;
226 break;
227 }
228 }
229
230#ifdef DEBUG_ADVANCEMENT
231 Serial.println(" Turning as leftward as possible");
232#endif
233 }
234
235#ifdef DEBUG_ADVANCEMENT
236 Serial.print(" Leaving node ");
238 Serial.print(" in direction ");
239 Serial.println(newDirection);
240#endif
241
243 }
244
246
247#ifdef DEBUG_ADVANCEMENT
248 Serial.print(" and entering segment ");
250#endif
251
253#ifdef DEBUG_ADVANCEMENT
254 Serial.println(" (starting at bottom)");
255#endif
258 }
259 else {
260#ifdef DEBUG_ADVANCEMENT
261 Serial.println(" (starting at top)");
262#endif
265 }
266 break;
267 }
268
271
273
274#ifdef DEBUG_ADVANCEMENT
275 Serial.print(" Reached top of seg. ");
277#endif
278
281 for (int i = 0; i < 6; i++) {
282
283
285 if (incomingConnection == segment)
287 }
288#ifdef DEBUG_ADVANCEMENT
289 Serial.print(" Entering node ");
291 Serial.print(" from direction ");
293#endif
295 }
296 else {
297#ifdef DEBUG_ADVANCEMENT
298 Serial.print(" Moved up to seg. ");
300 Serial.print(" LED ");
302#endif
303 }
304 break;
305 }
306
310
311#ifdef DEBUG_ADVANCEMENT
312 Serial.print(" Reached bottom of seg. ");
314#endif
315
318 for (int i = 0; i < 6; i++) {
319
320
322 if (incomingConnection == segment)
324 }
325#ifdef DEBUG_ADVANCEMENT
326 Serial.print(" Entering node ");
328 Serial.print(" from direction ");
330#endif
332 }
333 else {
334#ifdef DEBUG_ADVANCEMENT
335 Serial.print(" Moved down to seg. ");
337 Serial.print(" LED ");
339#endif
340 }
341 break;
342 }
343
344 default:
345 break;
346 }
347
349
351
353 }
354 }
355
356#ifdef DEBUG_ADVANCEMENT
357 Serial.print(" Age is now ");
358 Serial.print(age);
359 Serial.print('/');
361#endif
362
364
365#ifdef DEBUG_ADVANCEMENT
366 Serial.println(" Lifespan is up! Ripple is dead.");
367#endif
370 }
371 }
byte ledColors[40][14][3]
void renderLed(byte ledColors[40][14][3], unsigned long age)
int segmentConnections[40][2]
int nodeConnections[25][6]
float fmap(float x, float in_min, float in_max, float out_min, float out_max)