/** * Create a bomb. */ Cube * cube_new_bomb() { Cube *cube; cube = cube_new(rand() % 4); cube->type = CTYPE_BOMB; return cube; }
/** * Create a medic. */ Cube * cube_new_medic() { Cube *cube; cube = cube_new(0); cube->type = CTYPE_MEDIC; return cube; }
/** * Create a rock, random color. */ Cube * cube_new_rock() { Cube *cube; cube = cube_new(rand() % 4); cube->type = CTYPE_ROCK; return cube; }
/** * Shortcut to assign the type directly. */ Cube * cube_new_type(byte start_pos, int type) { Cube *cube; cube = cube_new(start_pos); cube->type = type; return cube; }
static void add_cube(struct labyrinth *l, char *type) { l->cubes = realloc(l->cubes, (l->cube_cnt + 1) * sizeof(*(l->cubes))); if (l->cubes == NULL) { perror("realloc"); exit(EXIT_FAILURE); } l->cubes[l->cube_cnt] = cube_new(type); l->cube_cnt++; }
/** * Core function for generating cubes. It takes an argument to know the * maximum type to allow in the pick. */ Cube * cube_new_random_max(int max) { Cube *cube; int r; /* Start at a random position. */ r = rand(); cube = cube_new(r % 4); /* Random type. (skipping the first blank) */ r = rand(); cube->type = 1 + r % max; return cube; }
/** * Core function for generating cubes. It takes an argument to know the * maximum type to allow in the pick. */ Cube * cube_new_random_mask(unsigned int mask) { Cube *cube; int r; /* Start at a random position. */ r = rand(); cube = cube_new(r % 4); /* Random type, according to the mask */ do { r = crm[rand() % crm_len]; } while ((mask & 1 << r) == 0); cube->type = r; return cube; }