Beispiel #1
0
int main(int argc, char *argv[]) {
    bool gameIsActive;

    if (!init()) {
        printf("error initializing");
        return 1;
    }
    printf("initialized\n");
    printf("Waiting for connection...\n");
    while (true) {
        acceptConnection();
        initGame();
        printf("Game initialized");
        gameIsActive = true;
        packetId=0;
        SDL_DetachThread(SDL_CreateThread(udpListener, "udpThread", NULL));
        while (1) {
            updateBullets();
            updateShips();
            createAndSendUDPPackets();
            SDL_Delay(20);
//            if (!ClientsAreReady()) {
//                gameIsActive = false;
//            }
        }
    }
    closeServer();
    return 0;
}
void 
getEntry(int *team, int *stype)
{


    int     switching = -1;	/* confirm switches 7/27/91 TC */
    inputMask = CP_OUTFIT;
    for (;;) {
	/* updateShips so he knows how many players on each team */
	if (blk_flag)
	    updateShips();
	sendMaskPacket(tournamentMask(me->p_team));
	if (blk_flag)
	    briefUpdateClient();
	flushSockBuf();
	/* Have we been busted? */
	if (me->p_status == PFREE) {
	    me->p_status = PDEAD;
	    me->p_explode = 600;
	}
	socketPause();
	readFromClient();
	if (isClientDead()) {
	    int     i;

	    if (noressurect)
		exitGame();
	    printf("Ack!  The client went away!\n");
	    printf("I will attempt to resurrect him!\n");

	    /* UDP fail-safe */
	    commMode = COMM_TCP;
	    if (udpSock >= 0)
		closeUdpConn();

	    /* For next two minutes, we try to restore connection */
	    shutdown(sock, 2);
	    sock = -1;
	    for (i = 0;; i++) {
		switch (me->p_status) {
		case PFREE:
		    me->p_status = PDEAD;
		    me->p_explode = 600;
		    break;
		case PALIVE:
		case POBSERVE:
		    me->p_ghostbuster = 0;
		    break;
		case PDEAD:
		    me->p_explode = 600;
		    break;
		default:
		    me->p_explode = 600;
		    me->p_ghostbuster = 0;
		    break;
		}
		sleep(5);
		if (connectToClient(host, nextSocket))
		    break;
		if (i == 23) {
		    printf("Oh well, maybe I'm getting rusty!\n");
		    switch (me->p_status) {
		    case PFREE:
			break;
		    case PALIVE:
		    case POBSERVE:
			me->p_ghostbuster = 100000;
			break;
		    case PDEAD:
			me->p_explode = 0;
			break;
		    default:
			me->p_explode = 0;
			me->p_ghostbuster = 100000;
			break;
		    }
		    exitGame();
		}
	    }
	    printf("A miracle!  He's alive!\n");
	    teamPick = -1;
	    updateSelf();
	    updateShips();
	    flushSockBuf();
	}
	if (teamPick != -1) {
	    if (teamPick < 0 || teamPick > 3) {
		warning("Get real!");
		sendPickokPacket(0);
		teamPick = -1;
		continue;
	    }
	    if (!(tournamentMask(me->p_team) & (1 << teamPick))) {
		warning("I cannot allow that.  Pick another team");
		sendPickokPacket(0);
		teamPick = -1;
		continue;
	    }

	    if (((1 << teamPick) != me->p_team) &&
		(me->p_team != ALLTEAM))	/* switching teams 7/27/91 TC */
		if ((switching != teamPick)
		    && (me->p_whydead != KGENOCIDE)
		    && !status2->league
		    ) {
		    switching = teamPick;
		    warning("Please confirm change of teams.  Select the new team again.");
		    sendPickokPacket(0);
		    teamPick = -1;
		    continue;
		}
	    /* His team choice is ok. */
	    if (shipPick < 0 || shipPick >= NUM_TYPES) {
		warning("That is an illegal ship type.  Try again.");
		sendPickokPacket(0);
		teamPick = -1;
		continue;
	    }
	    if (!allowed_ship(1 << teamPick, mystats->st_rank, mystats->st_royal, shipPick)) {
		sendPickokPacket(0);
		teamPick = -1;
		continue;
	    }
	    if (shipvals[shipPick].s_nflags & SFNMASSPRODUCED) {
		warning("o)utpost, u)tility, or s)tandard?");
		sendPickokPacket(0);
		tmpPick = shipPick;
		continue;
	    }
	    break;
	}
	if (me->p_status == PTQUEUE) {
	    if (status->tourn)
		detourneyqueue();
	    /* You don't time out on the tourney queue */
	    me->p_ghostbuster = 0;
	}
    }
    *team = teamPick;
    if ((shipvals[tmpPick].s_nflags & SFNMASSPRODUCED) &&
	((shipvals[shipPick].s_numports) || (shipPick == SCOUT))) {

	*stype = tmpPick;
	tmpPick = CRUISER;
    }
    else {
	*stype = shipPick;
	tmpPick = CRUISER;
    }
    sendPickokPacket(1);
    flushSockBuf();
}