| 73 | } |
| 74 | |
| 75 | void advance(byte ledColors[40][14][3]) { |
| 76 | unsigned long age = millis() - birthday; |
| 77 | |
| 78 | if (state == dead) |
| 79 | return; |
| 80 | |
| 81 | pressure += fmap(float(age), 0.0, float(lifespan), speed, 0.0); // Ripple slows down as it ages |
| 82 | // TODO: Motion of ripple is severely affected by loop speed. Make it time invariant |
| 83 | |
| 84 | if (pressure < 1 && (state == travelingUpwards || state == travelingDownwards)) { |
| 85 | // Ripple is visible but hasn't moved - render it to avoid flickering |
| 86 | renderLed(ledColors, age); |
| 87 | } |
| 88 | |
| 89 | while (pressure >= 1) { |
| 90 | #ifdef DEBUG_ADVANCEMENT |
| 91 | Serial.print("Ripple "); |
| 92 | Serial.print(rippleId); |
| 93 | Serial.println(" advancing:"); |
| 94 | #endif |
| 95 | |
| 96 | switch (state) { |
| 97 | case withinNode: { |
| 98 | if (justStarted) { |
| 99 | justStarted = false; |
| 100 | } |
| 101 | else { |
| 102 | #ifdef DEBUG_ADVANCEMENT |
| 103 | Serial.print(" Picking direction out of node "); |
| 104 | Serial.print(position[0]); |
| 105 | Serial.print(" with agr. "); |
| 106 | Serial.println(behavior); |
| 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 | |
| 117 | if (behavior <= 2) { // Semi-random aggressive turn mode |
| 118 | // The more aggressive a ripple, the tighter turns it wants to make. |
| 119 | // If there aren't any segments it can turn to, we need to adjust its behavior. |
| 120 | byte anger = behavior; |
| 121 | |
| 122 | while (newDirection < 0) { |
| 123 | if (anger == 0) { |
| 124 | int forwardConnection = nodeConnections[position[0]][forward]; |
| 125 | |
| 126 | if (forwardConnection < 0) { |
| 127 | // We can't go straight ahead - we need to take a more aggressive angle |
| 128 | #ifdef DEBUG_ADVANCEMENT |
| 129 | Serial.println(" Can't go straight - picking more agr. path"); |
| 130 | #endif |
| 131 | anger++; |
| 132 | } |