Guest User

Untitled

a guest
Feb 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. /*
  2. * Test of ready state for Mortals
  3. */
  4.  
  5. enum State {
  6. DEAD,
  7. ALIVE,
  8. ENGUARDE, // I am ready to attack!
  9. ATTACKING, // Short window when I have already come across my first victim and started attacking
  10. INJURED,
  11. READY,
  12. TEAM_A_START,
  13. TEAM_B_START,
  14. };
  15.  
  16. void setup() {
  17. // put your setup code here, to run once:
  18. setValueSentOnAllFaces(DEAD);
  19. }
  20.  
  21. void loop() {
  22. // put your main code here, to run repeatedly:
  23.  
  24. if(isBlinkInReadyConfiguration()) {
  25. setColor(GREEN);
  26. }
  27. else {
  28. setColor(RED);
  29. }
  30. }
  31.  
  32. /*
  33. * Determine if we are in the READY Mode
  34. */
  35.  
  36. bool isBlinkInReadyConfiguration() {
  37.  
  38. // first count neighbors, if we have more or less than 2, then we are not in the ready mode
  39. byte numNeighbors = 0;
  40.  
  41. FOREACH_FACE(f) {
  42. if(!isValueReceivedOnFaceExpired(f)) {
  43. numNeighbors++;
  44. }
  45. }
  46.  
  47. if(numNeighbors != 2) {
  48. return false;
  49. }
  50.  
  51. // we have 2 neighbors, let's make sure they are dead or ready
  52. FOREACH_FACE(f) {
  53. if(!isValueReceivedOnFaceExpired(f)) {
  54.  
  55. byte neighborMode = getLastValueReceivedOnFace(f);
  56.  
  57. if(neighborMode != DEAD && neighborMode != READY) {
  58. return false;
  59. }
  60. }
  61. }
  62.  
  63. // great we have 2 neighbors that are either dead or ready
  64. // let's check to see if they are adjacent to one another
  65. byte NO_FACE = 255;
  66.  
  67. byte firstNeighborFace = NO_FACE;
  68. byte secondNeighborFace = NO_FACE;
  69.  
  70. FOREACH_FACE(f) {
  71. if(!isValueReceivedOnFaceExpired(f)) {
  72.  
  73. if(firstNeighborFace == NO_FACE) {
  74. firstNeighborFace = f;
  75. }else {
  76. secondNeighborFace = f;
  77. }
  78. }
  79. }
  80.  
  81. // now that we have the two faces, are they adjacent?
  82. if( (firstNeighborFace == 0 && secondNeighborFace == 1 ) ||
  83. (firstNeighborFace == 1 && secondNeighborFace == 2 ) ||
  84. (firstNeighborFace == 2 && secondNeighborFace == 3 ) ||
  85. (firstNeighborFace == 3 && secondNeighborFace == 4 ) ||
  86. (firstNeighborFace == 4 && secondNeighborFace == 5 ) ||
  87. (firstNeighborFace == 0 && secondNeighborFace == 5 ) )
  88. {
  89. return true;
  90. }
  91.  
  92. return false;
  93. }
Add Comment
Please, Sign In to add comment