| 19 | #define BONUS_PROBABILITY 0.7f |
| 20 | |
| 21 | void Obstacle::PutRandomBonus() { |
| 22 | if (Random(100) * 0.01f > BONUS_PROBABILITY) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | bool candidate[OBS_GRID_SIZE][OBS_GRID_SIZE]; |
| 27 | memset(candidate, 0, sizeof(candidate)); |
| 28 | |
| 29 | int r, c; |
| 30 | int i, j; |
| 31 | |
| 32 | // (This is the most deeply indented code I have written in my life) |
| 33 | // It goes through the grid and marks all the squares that are adjacent to |
| 34 | // a solid square as candidates for the bonus. |
| 35 | for (r = 0; r < OBS_GRID_SIZE; r++) { |
| 36 | for (c = 0; c < OBS_GRID_SIZE; c++) { |
| 37 | if (grid[c][r]) { |
| 38 | for (i = r - 1; i <= r + 1; i++) { |
| 39 | for (j = c - 1; j <= c + 1; j++) { |
| 40 | if (i >= 0 && i < OBS_GRID_SIZE && j >= 0 && j < OBS_GRID_SIZE) { |
| 41 | candidate[j][i] = true; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // now we randomly choose one of the candidates |
| 50 | int r0 = Random(0, OBS_GRID_SIZE); |
| 51 | int c0 = Random(0, OBS_GRID_SIZE); |
| 52 | int rd, cd; |
| 53 | bonusRow = bonusCol = -1; |
| 54 | for (rd = 0; rd < OBS_GRID_SIZE && bonusRow < 0; rd++) { |
| 55 | for (cd = 0; cd < OBS_GRID_SIZE; cd++) { |
| 56 | int my_r = (r0 + rd) % OBS_GRID_SIZE; |
| 57 | int my_c = (c0 + cd) % OBS_GRID_SIZE; |
| 58 | if (!grid[my_c][my_r] && candidate[my_c][my_r]) { |
| 59 | bonusRow = my_r; |
| 60 | bonusCol = my_c; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |