int main (int argc, char* argv[]) { if (argc > 1) args_handle (argc, argv); if(tron.user_name[0] == 0) { char *tmp = getlogin(); if(tmp == NULL) { ifitron_abort("No username provided and attempts to fetch it automatically, failed.\n"); } strcpy(tron.user_name, tmp); } if(get_server_mode() == 2) { while(TRUE) { int result = play_multiplayer(); if(result == -1) break; } engine_exit(); ifitron_exit(); } if(client_host_addr != NULL) { //Connect to the server int sd = connect_to_server(); if(sd == -1) { ifitron_abort("Couldn't connect to the server.\n"); }else{ set_client_socket_descriptor(sd); } } engine_init (); engine_show_main_menu (); ifitron_init (); while (TRUE) { if (tron.is_alive == FALSE) ifitron_game_over (); engine_get_game_input(); player_update(get_tron()); player_increase_size (get_tron(), 1); player_increase_score (get_tron(), game.level); if (tron.score % 50 == 0 && game.level < 9) game.level++; if (player_hit_self(get_tron()) == TRUE || player_hit_borders(get_tron()) == TRUE) tron.is_alive = FALSE; engine_show_screen (); } return 0; }
/** The main function - contains the main loop of the game. * * * @note I tried to make this function as clear as possible, so anyone * could understaing the whole game logic starting by here. @n * Have fun with the source code! */ int main (int argc, char* argv[]) { if (argc > 1) args_handle (argc, argv); engine_init (); engine_show_main_menu (); nsnake_init (); while (TRUE == TRUE) { if (snake.is_alive == FALSE) nsnake_game_over (); engine_get_game_input (); player_update (); fruit_update_bonus (); if (player_hit_fruit () == TRUE) { // Is this score arbitrary? player_increase_score (game.level*3 + fruit.bonus); player_increase_size (2); fruit_init (); } if (player_hit_self () == TRUE) snake.is_alive = FALSE; if (player_collided_with_borders () == TRUE) snake.is_alive = FALSE; engine_show_screen (); } // Even though we have this here, the game always quits during // the main loop engine_exit (); nsnake_exit (); return 0; }
int play_multiplayer(void) { int sd = connect_to_server(); if(sd == -1) { ifitron_abort("Couldn't connect to the server.\n"); }else set_client_socket_descriptor(sd); engine_init(); engine_show_main_menu(); //initiate multiplayer_init(get_tron()); player_t *opponent = malloc(sizeof(player_t)); if(opponent == NULL) { ifitron_abort("Error allocating memory for the opponent in play_multiplayer().\n"); } multiplayer_init(opponent); if(send_game_init(sd, MULTIPLE_PLAYER_TYPE + (game.level - 1), 0, 0, 0, 0, 0, tron.user_name) == -1) ifitron_abort("send_game_init encountered an error.\n"); //Receive init. In case someone initiated sooner and level is something other //than what this player requested. byte game_type; int x, y; int ox, oy; char buf[25]; if(receive_game_init(sd, &game_type, &get_tron()->opponents, &x, &y, &ox, &oy, buf) == -1) ifitron_abort("Something went wrong receiving_game_init()\n"); game.level = game_type - MULTIPLE_PLAYER_TYPE; player_increase_size(&tron, 1); update_player_coordinates(&tron, x, y); player_increase_size(opponent, 1); update_player_coordinates(opponent, ox, oy); //wait for start. if(receive_game_start(sd) == -1) ifitron_abort("Something went wrong receiving game start.\n"); while(TRUE) { engine_get_game_input(); player_increase_size(get_tron(), 1); player_update(get_tron()); player_increase_score(get_tron(), game.level); if(tron.score % 50 == 0 && game.level < 9) game.level++; if(send_game_update(sd, get_tron()->body[0].x, get_tron()->body[0].y) == -1) ifitron_abort("send_game_update encountered an error.\n"); //Check if end or update is coming int size = 5; char buf[5]; if(read(sd, buf, size) == -1) ifitron_abort("Error reading end or update.\n"); if(buf[2] == GAME_UPDATE) { player_increase_size(opponent, 1); update_player_coordinates(opponent, buf[3], buf[4]); player_increase_score(opponent, game.level); }else if(buf[2] == GAME_END) { //free opponent's memory if(opponent->body != NULL) free(opponent->body); free(opponent); //close the connection close_server_connection(sd); ifitron_game_over(); return 0; } //Draw the map with the players multiplayer_show_screen (opponent); } }