int monster_move(struct monster* monster, struct map* map) { int x = monster->x; int y = monster->y; int move = 0; if (!map_is_inside(map, x, y)) return 0; switch (monster->current_direction) { case NORTH: if (monster_move_aux(monster, map, x, y - 1)) { map_set_cell_type(map,x,y,CELL_EMPTY); monster->y--; map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER); move = 1; } break; case SOUTH: if (monster_move_aux(monster, map, x, y + 1)) { map_set_cell_type(map,x,y,CELL_EMPTY); monster->y++; map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER); move = 1; } break; case WEST: if (monster_move_aux(monster, map, x - 1, y)) { map_set_cell_type(map,x,y,CELL_EMPTY); monster->x--; map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER); move = 1; } break; case EAST: if (monster_move_aux(monster, map, x + 1, y)) { map_set_cell_type(map,x,y,CELL_EMPTY); monster->x++; map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER); move = 1; } break; } return move; }
static int monster_move_aux(struct monster* monster, struct map* map, int x, int y) { if (!map_is_inside(map, x, y)){ return 0; } switch (map_get_cell_type(map, x, y)) { case CELL_SCENERY: return 0; break; case CELL_CASE: return 0; break; case CELL_BONUS: break; case CELL_GOAL: return 0; break; case CELL_MONSTER: return 0; break; case CELL_PLAYER: return 0; break; case CELL_BOMB: monster_die(monster); map_set_cell_type(map,x,y,CELL_EMPTY); return 0; break; default: break; } // Monster has moved return 1; }
void map_set_cell_type(struct map* map, int x, int y, enum cell_type type){ assert(map && map_is_inside(map,x,y)); map->grid[CELL(x,y)] = type; }
void map_set_cell_type(t_map map, int x, int y, t_cell_type type) { assert(map && map_is_inside(map, x, y)); map->grid[CELL(x, y)] = type; }
t_bonus_type map_get_bonus_type(t_map map, int x, int y) { assert(map && map_is_inside(map, x, y)); return map->grid[CELL(x, y)] >> 4; }
t_cell_type map_get_cell_type(t_map map, int x, int y) { assert(map && map_is_inside(map, x, y)); return map->grid[CELL(x, y)] & 15; }