void new_connection(TCPsocket sock) { TCPsocket tmp; struct socket_node *socket; IPaddress *ip; /* accept the connection temporarily */ tmp = SDLNet_TCP_Accept(sock); if (!tmp) do_error(SDLNet_GetError()); /* are we full or game already started? */ if (get_active_players() + 1 > NUM_PLAYERS || current_turn) SDLNet_TCP_Close(tmp); else { /* nope! */ socket = new_socket(SOCKET_CLIENT); socket->sock = tmp; if (SDLNet_TCP_AddSocket(sockset, tmp) < 0) do_error(SDLNet_GetError()); ip = SDLNet_TCP_GetPeerAddress(tmp); if (!ip) do_error(SDLNet_GetError()); if (SDLNet_ResolveIP(ip)) strcpy(socket->host, SDLNet_ResolveIP(ip)); else sprintf(socket->host, "Unknown IP"); join_player(socket); /* add player to game */ } }
int get_free_player() { int i = 0; if (get_active_players() < NUM_PLAYERS) { while (IS_ACTIVE(i)) i++; return i; } return -1; /* no free player slots */ }
int main(void) { unsigned char * players; int i; // Want to initialize the game init_game(); // Enter the waiting state for other players network_with_other_players(); // If we're the master player, we need to send out the begin messages to all other players #if (THIS_PLAYER == MASTER_PLAYER) players = get_active_players(); for (i = 1; i < NUM_PLAYERS; i++) { if (players[i] == PLAYER_PLAYING) { display_write_line(1, "sending begin to 1"); send_message(MSG_BEGIN, 0, THIS_PLAYER, i, 0); __delay_ms(100); } } #endif // Made it to while loop! // display_write_line(1, "game begun!"); // Just loop for now while(1){}; display_write_line(1, "game over!"); return 0; }
void init_player(int player, int option) { int i, num; struct action_node *next; if (option != INIT_PLAYER_INACTIVE) players[player].active = 1; else players[player].active = 0; if (option != INIT_PLAYER_RESET) { switch (option) { case INIT_PLAYER_HUMAN: sprintf(GET_NAME(player), "Player %d", get_active_players()); players[player].ai = 0; players[player].portrait = LEADER_RONNIE; break; case INIT_PLAYER_AI: players[player].ai = 1; while (1) { /* select a random leader but check so it doesn't exist already */ num = randomize(0, NUM_LEADERS - 1); if (player_name_available((char *)leader_names[num])) break; /* end while-loop */ } strcpy(GET_NAME(player), leader_names[num]); players[player].portrait = num; break; } memset(players[player].msg, 0, MAX_MESSAGE_LENGTH+1); players[player].msg_timer = 0; } /* population */ if (option != INIT_PLAYER_RESET || server) { for (i = 0; i < NUM_CITIES; i++) { if (option == INIT_PLAYER_INACTIVE) GET_POPULATION(player, i) = 0; else GET_POPULATION(player, i) = randomize(10, 35); GET_WORLD(player, i) = GET_POPULATION(player, i); /* world state */ } } /* weapons */ for (i = 0; i < NUM_WEAPONS; i++) GET_STOCK(player, i) = 0; GET_STOCK(player, WEAPON_MISSILE_10MT) = 1; GET_STOCK(player, WEAPON_WARHEAD_10MT) = 1; GET_STOCK(player, WEAPON_LNDS) = 1; /* free action list */ while (players[player].action_list != NULL) { next = players[player].action_list->next; free(players[player].action_list); players[player].action_list = next; } GET_PREVIOUS(player) = ACTION_NOTHING; GET_MISSILE(player) = -1; GET_BOMBER(player) = -1; GET_WORLD_MISSILE(player) = 0; GET_WORLD_BOMBER(player) = 0; }