bool update_game() { int in = get_input(get_map_window()); if(in == INPUT_ACTION && get_last_action() == ACTION_QUIT) return false; else if(in == INPUT_ACTION && (get_last_action() == ACTION_SCROLL_UP || get_last_action() == ACTION_SCROLL_DOWN)) { log_scroll(get_last_action() == ACTION_SCROLL_UP); draw_log(); } else if(!dead) { update_player(); if(get_current_floor() != current_level) { set_current_map(world[get_current_floor()], current_level < get_current_floor(), current_level > get_current_floor()); current_level = get_current_floor(); } update_map(1, world[current_level]); if(is_dead()) { add_message(COLOR_HP_CRIT, "Oh dear, you've died!"); add_message(COLOR_DEFAULT, "Press q to return to the main menu"); dead = true; } if(game_won()) dead = true; draw_log(); } return true; }
turn_command_t player_turn(game_t * game) { int input; while (1) { input = getch(); if(input == ERR) { draw_log(game->input_type, &(game->log), game->level); continue; } clear_msg(); if(input == '\t') { if(game->input_type == input_type_log) { timeout(-1); game->input_type = input_type_map; } else { timeout(500); game->input_type = input_type_log; } draw_map(game->input_type, game->level); draw_stats(&(game->player), game->level); draw_log(game->input_type, &(game->log), game->level); continue; } turn_command_t command; if(game->input_type == input_type_map) { command = process_input_map(game, input); } else { command = process_input_log(game, input); draw_log(game->input_type, &(game->log), game->level); } if(command == turn_command_void) { continue; } return command; } }
/* Puts robot in (0,0) * * * LOGS the movement */ void move_robot_origin() { draw_info(ENERGY,ENERGY_STAT, 0, 0, DIRECTION); draw_log("--Ir al origen "); draw_robot(0, 0, DIRECTION); origin(); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X=0; Y=0; if( DIRECTION == UP) { BOARD[X][Y] = UROBOT; } else if( DIRECTION == DOWN) { BOARD[X][Y] = DROBOT; } else if( DIRECTION == RIGHT) { BOARD[X][Y] = RROBOT; } else if( DIRECTION == LEFT) { BOARD[X][Y] = LROBOT; } }
void init_game() { WINDOW* map_win = newwin(24, 80, 0, 0); WINDOW* area_win = newwin(6, 10, 0, 80); WINDOW* stats_win = newwin(8, 10, 6, 80); WINDOW* examine_win = newwin(10, 10, 14, 80); WINDOW* hp_win = newwin(1, 90, 24, 0); WINDOW* log_win = newwin(10, 90, 25, 0); dead = false; init_log(log_win); init_map(map_win); init_items(); world = calloc(LEVEL_COUNT, sizeof(map*)); world[0] = load_map("data/maps/farm.map", 78, 22, false, true); for(int i = 1; i < 3; ++i) world[i] = load_map("data/maps/easy_cave.map", 80 + 10 * (i - 1), 24 + 4 * (i - 1), true, true); for(int i = 3; i < 5; ++i) world[i] = load_map("data/maps/mid_cave.map", 80 + 10 * (i - 1), 24 + 4 * (i - 1), true, true); for(int i = 5; i < 7; ++i) world[i] = load_map("data/maps/hard_cave.map", 80 + 15 * (i - 1), 24 + 6 * (i - 1), true, true); for(int i =7; i < 9; ++i) world[i] = load_map("data/maps/crazy_cave.map", 80 + 18 * (i - 1), 24 + 8 * (i - 1), true, true); world[LEVEL_COUNT - 1] = load_map("data/maps/final.map", 78, 22, true, false); init_player(map_win, stats_win, hp_win, area_win, examine_win, world[0]); int x, y; get_random_empty_tile(&x, &y, world[LEVEL_COUNT - 1]); spawn_item(x, y, "data/items/cat.item", world[LEVEL_COUNT - 1]); get_random_empty_tile(&x, &y, world[0]); player_set_position(x, y); draw_map(x, y, world[0]); add_message(COLOR_DEFAULT, "@ symbol, a brave young farmer, was out for a stroll on his farm when his cat Cuddles ran down into the gaping starcase to the deadly Caves of Consternation! @ symbol had been meaning to patch that up for a while, but hadn't gotten a chance yet. Don't judge."); if(ask_question(COLOR_SELECTION, "Will you help @ symbol retrieve his cat, Cuddles?")) add_message(COLOR_HP_GOOD, "Excellent! Get to it, then!"); else { add_message(COLOR_HP_CRIT, "Well that was unexpected. Okay then, press q to quit to the main menu."); dead = true; } draw_log(); }
void Plazza::start() { draw_menu(); draw_log(); char buff[4096]; std::string line; while (line != "quit") { printEcrased(); move(40, 2); clrtoeol(); getstr(buff); line.assign(buff); parsing(line); distrib(); wclrtoeol(_footer); } }
/* Empty the BOARD, fills it and draw with game elements * * Put Robot at 0,0 * Put N_BARRIERS barriers randomly * Put [1, ( (N_BARRIERS*10)/100 )] charge stations randomly * Put the exit at (EXIT_X, EXIT_Y) */ void new_game(int n_barriers, int exit_x, int exit_y) { int rand_x, rand_y; int barriers_allocated=0; int stations_allocated=0; int n_stations; // Adjust EXIT coordinates to [0-7] exit_x--; exit_y--; // Empty the BOARD and blank it reset_game_values(); /* Put Robot, always start at 0,0 ponting to the rigth */ BOARD[0][0] = RROBOT; draw_robot(0,0,DIRECTION); /* Put Exit */ BOARD[exit_x][exit_y] = EXIT; draw_object(exit_x, exit_y, 'e'); /* Restart energy, refresh INFO window and inform in LOG window */ draw_info(ENERGY,'g', 0, 0, DIRECTION); draw_log("*** NEW GAME ***"); /* Put Barriers */ while( barriers_allocated != n_barriers ) { rand_x = (rand()%8); rand_y = (rand()%8); // Barrires can't jail the robot if( !((rand_x==1 && rand_y==0) || (rand_x==0 && rand_y==1)) ) { if( BOARD[rand_x][rand_y] == EMPTY ) { BOARD[rand_x][rand_y] = BARRIER; draw_object(rand_x, rand_y, 'b'); barriers_allocated++; } } } /* Put Charge Stations */ n_stations = (n_barriers*40)/100; if( n_stations < 0) n_stations = 1; while( stations_allocated != n_stations ) { rand_x = (rand()%8); rand_y = (rand()%8); if( BOARD[rand_x][rand_y] == EMPTY ) { BOARD[rand_x][rand_y] = STATION; draw_object(rand_x, rand_y, 's'); stations_allocated++; } } // Enable runing game GAME_RUNING=1; // Init Hardware if possible serial_init(); origin(); }
/* Interchange robot coordinates * * If possible return 1, else return 0 * * LOGS the movement * Charge robot energy when arrives a charge station * Invokes WIN and NEW GAME routines whe robot arrive the exit */ int teleport_robot() { if( !GAME_RUNING ){ return 0; } int new_x, new_y; // Interchange coordinates new_x = Y; new_y = X; if( BOARD[new_x][new_y] == BARRIER ) { draw_log("[!] Imposible [!] "); return 0; } // Charge station found else if( BOARD[new_x][new_y] == STATION ) { draw_log("[!] Imposible [!] "); return 0; } // Exit found else if( BOARD[new_x][new_y] == EXIT ) { draw_log("[!] Imposible [!] "); return 0; } // Empty box found else if( BOARD[new_x][new_y] == EMPTY ) { draw_robot(new_x, new_y, DIRECTION); to(new_x, new_y); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X = new_x; Y = new_y; if( DIRECTION == UP) { BOARD[X][Y] = UROBOT; } else if( DIRECTION == DOWN) { BOARD[X][Y] = DROBOT; } else if( DIRECTION == RIGHT) { BOARD[X][Y] = RROBOT; } else if( DIRECTION == LEFT) { BOARD[X][Y] = LROBOT; } ENERGY--; if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else if( ENERGY > 0 ) { ENERGY_STAT = BAD; } else { draw_log("--Teletransportar "); draw_log("### PARTIDA PERDIDA ###"); // Show LOSE pop-up get_losepopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } draw_info(ENERGY,ENERGY_STAT, X, Y, DIRECTION); draw_log("--Teletransportar "); return 1; } }
/* Moves the robot 1 step * * If possible return 1, else return 0 * * LOGS the movement * Charge robot energy when arrives a charge station * Invokes WIN and NEW GAME routines whe robot arrive the exit */ int move_robot() { if( !GAME_RUNING ){ return 0; } // UP step if( DIRECTION == UP && Y<7 ) { // Barrier found if( BOARD[X][Y+1] == BARRIER ) { return 0; } // Charge station found else if( BOARD[X][Y+1] == STATION ) { draw_robot(X, Y+1, UP); // Hardware move step('y','f'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } Y++; BOARD[X][Y] = UROBOT; ENERGY+=ENERGY_STATIONS; draw_log("-| Cargando |- "); if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else { ENERGY_STAT = BAD; } is_station_below=1; draw_info(ENERGY,ENERGY_STAT, X, Y, UP); draw_log("--Avanzar "); return 1; } // Exit found else if( BOARD[X][Y+1] == EXIT ) { draw_log("--Avanzar "); draw_log("*** PARTIDA GANADA ***"); // Show WIN pop-up get_winpopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } // Empty box found else if( BOARD[X][Y+1] == EMPTY ) { draw_robot(X, Y+1, UP); // Hardware move step('y','f'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } Y++; BOARD[X][Y] = UROBOT; ENERGY--; if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else if( ENERGY > 0 ) { ENERGY_STAT = BAD; } else { draw_log("--Avanzar "); draw_log("### PARTIDA PERDIDA ###"); // Show LOSE pop-up get_losepopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } draw_info(ENERGY,ENERGY_STAT, X, Y, UP); draw_log("--Avanzar "); return 1; } return 0; } // DOWN step else if( DIRECTION == DOWN && Y>0 ) { // Barrier found if( BOARD[X][Y-1] == BARRIER ) { return 0; } // Charge station found else if( BOARD[X][Y-1] == STATION ) { draw_robot(X, Y-1, DOWN); // Hardware move step('y','b'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } Y--; BOARD[X][Y] = DROBOT; ENERGY+=ENERGY_STATIONS; draw_log("-| Cargando |- "); if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else { ENERGY_STAT = BAD; } is_station_below=1; draw_info(ENERGY,ENERGY_STAT, X, Y, DOWN); draw_log("--Avanzar "); return 1; } // Exit found else if( BOARD[X][Y-1] == EXIT ) { draw_log("--Avanzar "); draw_log("*** PARTIDA GANADA ***"); // Show WIN pop-up get_winpopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } // Empty box found else if( BOARD[X][Y-1] == EMPTY ) { draw_robot(X, Y-1, DOWN); // Hardware move step('y','b'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } Y--; BOARD[X][Y] = DROBOT; ENERGY--; if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else if( ENERGY > 0 ) { ENERGY_STAT = BAD; } else { draw_log("--Avanzar "); draw_log("### PARTIDA PERDIDA ###"); // Show LOSE pop-up get_losepopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } draw_info(ENERGY,ENERGY_STAT, X, Y, DOWN); draw_log("--Avanzar "); return 1; } return 0; } // RIGHT step else if( DIRECTION == RIGHT && X<7 ) { // Barrier found if( BOARD[X+1][Y] == BARRIER ) { return 0; } // Charge station found else if( BOARD[X+1][Y] == STATION ) { draw_robot(X+1, Y, RIGHT); // Hardware move step('x','f'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X++; BOARD[X][Y] = RROBOT; ENERGY+=ENERGY_STATIONS; draw_log("-| Cargando |- "); if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else { ENERGY_STAT = BAD; } is_station_below=1; draw_info(ENERGY,ENERGY_STAT, X, Y, RIGHT); draw_log("--Avanzar "); return 1; } // Exit found else if( BOARD[X+1][Y] == EXIT ) { draw_log("--Avanzar "); draw_log("*** PARTIDA GANADA ***"); // Show WIN pop-up get_winpopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } // Empty box found else if( BOARD[X+1][Y] == EMPTY ) { draw_robot(X+1, Y, RIGHT); // Hardware move step('x','f'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X++; BOARD[X][Y] = RROBOT; ENERGY--; if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else if( ENERGY > 0 ) { ENERGY_STAT = BAD; } else { draw_log("--Avanzar "); draw_log("### PARTIDA PERDIDA ###"); // Show LOSE pop-up get_losepopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } draw_info(ENERGY,ENERGY_STAT, X, Y, RIGHT); draw_log("--Avanzar "); return 1; } return 0; } // LEFT step else if( DIRECTION == LEFT && X>0 ) { // Barrier found if( BOARD[X-1][Y] == BARRIER ) { return 0; } // Charge station found else if( BOARD[X-1][Y] == STATION ) { draw_robot(X-1, Y, LEFT); // Hardware move step('x','b'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X--; BOARD[X][Y] = LROBOT; ENERGY+=ENERGY_STATIONS; draw_log("-| Cargando |- "); if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else { ENERGY_STAT = BAD; } is_station_below=1; draw_info(ENERGY,ENERGY_STAT, X, Y, LEFT); draw_log("--Avanzar "); return 1; } // Exit found else if( BOARD[X-1][Y] == EXIT ) { draw_log("--Avanzar "); draw_log("*** PARTIDA GANADA ***"); // Show WIN pop-up get_winpopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } // Empty box found else if( BOARD[X-1][Y] == EMPTY ) { draw_robot(X-1, Y, LEFT); // Hardware move step('x','b'); BOARD[X][Y] = EMPTY; if( is_station_below ) { BOARD[X][Y] = STATION; draw_object(X, Y, 's'); is_station_below=0; } X--; BOARD[X][Y] = LROBOT; ENERGY--; if(ENERGY>=INIT_ENERGY) { ENERGY_STAT = GOOD; } else if(ENERGY>=(INIT_ENERGY)/2) { ENERGY_STAT = MEDIUM; } else if( ENERGY > 0 ) { ENERGY_STAT = BAD; } else { draw_log("--Avanzar "); draw_log("### PARTIDA PERDIDA ###"); // Show LOSE pop-up get_losepopup(); // Reset game reset_game_values(); // Stop game GAME_RUNING=0; return 1; } draw_info(ENERGY,ENERGY_STAT, X, Y, LEFT); draw_log("--Avanzar "); return 1; } return 0; } }
/* Rotates the robot in DIR direction * * * LOGS the movement */ void rotate_robot(char dir) { if( !GAME_RUNING ){ return; } if( dir == RIGHT ) { draw_log("--Girar Derecha "); if( DIRECTION == UP ) { draw_robot(X, Y, RIGHT); draw_info(ENERGY,ENERGY_STAT, X, Y, RIGHT); DIRECTION = RIGHT; } else if( DIRECTION == DOWN ) { draw_robot(X, Y, LEFT); draw_info(ENERGY,ENERGY_STAT, X, Y, LEFT); DIRECTION = LEFT; } else if( DIRECTION == LEFT ) { draw_robot(X, Y, UP); draw_info(ENERGY,ENERGY_STAT, X, Y, UP); DIRECTION = UP; } else if( DIRECTION == RIGHT ) { draw_robot(X, Y, DOWN); draw_info(ENERGY,ENERGY_STAT, X, Y, DOWN); DIRECTION = DOWN; } } else if( dir == LEFT ) { draw_log("--Girar Izquierda "); if( DIRECTION == UP ) { draw_robot(X, Y, LEFT); draw_info(ENERGY,ENERGY_STAT, X, Y, LEFT); DIRECTION = LEFT; } else if( DIRECTION == DOWN ) { draw_robot(X, Y, RIGHT); draw_info(ENERGY,ENERGY_STAT, X, Y, RIGHT); DIRECTION = RIGHT; } else if( DIRECTION == LEFT ) { draw_robot(X, Y, DOWN); draw_info(ENERGY,ENERGY_STAT, X, Y, DOWN); DIRECTION = DOWN; } else if( DIRECTION == RIGHT ) { draw_robot(X, Y, UP); draw_info(ENERGY,ENERGY_STAT, X, Y, UP); DIRECTION = UP; } } }
int main() { // Initialize custom GUI stuff init_gui(); // Draw splash thingy //splash(); // Create player object Player player; // Set player's name set_character_name(player); // Set player's main attributes set_main_attributes(player); // Add base stuff to player inventory player.inventory.additem(new BattleAxe(), 1); player.inventory.additem(new Shortsword(), 1); player.inventory.additem(new Mace(), 1); player.inventory.additem(new Fork(), 8); /* BEGIN OLD TESTING STUFF // Display player stats window draw_character_stats(player); getch(); */ //END OLD TESTING STUFF // Load map Map current_map = Map(); load_map(current_map, "testmap"); log.write("Loaded testmap."); // Place Player onto map current_map.place_character(2, 2, player); log.write("Press 'h' for help."); /* Prepare for player input */ keypad(stdscr, true); int input = 0; // Do stuff with player input bool exit = false; while(true) { // Escape: exit game confirmation if(input == 27) { exit = confirm_exit(); } // Arrow keys: move player character if(input == KEY_UP || input == KEY_DOWN || input == KEY_LEFT || input == KEY_RIGHT) { current_map.move_character(player, input); } // I: open inventory if(input == 'i') { player_inventory(player); } // H: print help if(input == 'h') { log.write("Quit game: esc\n Open/close inventory: i\n Move character: arrow keys"); } // Draw the rest of the windows draw_map(current_map); draw_log(log); draw_character_equipslot(player); // Exit if exit flag was set if(exit) break; // Get new input input = getch(); } endwin(); return 0; }