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 listen_for_connections(void) { int request_sd; int sds[2]; player_t *player[2]; player[0] = get_player(0); player[1] = get_player(1); byte game_type; int game_level; int clientaddrlen = sizeof(struct sockaddr); struct sockaddr_in serveraddr, clientaddr; //request-socket, lytter på innkommende forbindelser request_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); //adresse struct memset((void*) &serveraddr, 0, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = INADDR_ANY; if(listening_port != 0) { serveraddr.sin_port = htons(listening_port); printf("Listening for connections on port '%d'\n", listening_port); } else { serveraddr.sin_port = htons(9090); //default if no port is provided printf("Listening for connections on default port '9090'\n"); } //bind adressen til socketen bind(request_sd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); //start lytting listen(request_sd, SOMAXCONN); //Receive connection from players int i; for(i = 0; i < 2; i++) { sds[i] = accept(request_sd, (struct sockaddr *) &clientaddr, (socklen_t *) &clientaddrlen); } //Receive init from players for(i = 0; i < 2; i++) { int x, y, ox, oy; byte tmp; if(receive_game_init(sds[i], &tmp, &player[i]->opponents, &x, &y, &ox, &oy, player[i]->user_name) == -1) { perror("receive_game_init in listen_for_connection() encountered a error.\n"); return -1; } if(i == 0) { game_type = tmp; game_level = game_type - 0x10; } } //Send init to both players for(i = 0; i < 2; i++) { int opponent = (i == 0) ? 1 : 0; if(send_game_init(sds[i], game_type, player[i]->opponents, player[i]->body[0].x, player[i]->body[0].y, player[opponent]->body[0].x, player[opponent]->body[0].y, player[i]->user_name) == -1) { perror("send_game_init() encountered a problem.\n"); return -1; } } //Send game start to both players for(i = 0; i < 2; i++) { if(send_game_start(sds[i]) == -1) { perror("send_game_start() encountered a problem.\n"); return -1; } } //receive updates from players, check for collisons and respond with a update or //game_end if it was a collision. while(1) { for(i = 0; i < 2; i++) { int x, y; if(receive_game_update(sds[i], &x, &y) == -1) { perror("receive_game_update() encountered a problem.\n"); return -1; } player_increase_size(player[i], 1); update_player_coordinates(player[i], x, y); player_increase_score(player[i], game_level); if(player[i]->score % 50 == 0 && game_level < 9) game_level++; } //check for collisions for(i = 0; i < 2; i++) { int opponent = (i == 0) ? 1 : 0; int result = 0; if(player_hit_borders(player[i])) { result = 0x1; //Player collided with wall player[i]->result = result; player[opponent]->result = 0x7;//No remaining opponents } else if(player_hit_self(player[i])) { result = 0x2; // Player collided with himself player[i]->result = result; player[opponent]->result = 0x7; } else if(player_hit_opponent(player[i], player[opponent])) { result = 0x4; //Player hit opponent player[i]->result = result; player[opponent]->result = 0x7; player[opponent]->takeouts++; } if(result != 0) { //send stop for(i = 0; i < 2; i++) { if(send_game_end(sds[i]) == -1) { perror("send_game_end encountered a error.\n"); return -1; } } //send highscores if(client_host_addr != NULL) { int highscore_sd = get_client_socket_descriptor(); score *sl[2]; for(i = 0; i < 2; i++) { sl[i] = init_score(); sl[i]->game_type = game_type; get_score(player[i], sl[i]); } if(send_score_list(highscore_sd, sl, 2) == -1) { perror("Something went wrong sending score list.\n"); return -1; } } close(sds[0]); close(sds[0]); close(request_sd); return 0; } } //send updates for(i = 0; i < 2; i++) { int which = (i == 0) ? 1 : 0; if(send_game_update(sds[i], player[which]->body[0].x, player[which]->body[0].y) == -1) { perror("send_game_update encountered a error.\n"); return -1; } } } }