void PlayerInterface::disconnectPlayerCleanup( PlayerID &player_id ) { PlayerAllianceUpdate allie_update; unsigned short player_index; unsigned short disconnect_player_index; disconnect_player_index = player_id.getIndex(); for ( player_index = 0; player_index < max_players; player_index++ ) { if ( isAllied( disconnect_player_index, player_index ) == true ) { allie_update.set( disconnect_player_index, player_index, _player_break_alliance ); SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 ); } if ( isAllied( player_index, disconnect_player_index ) == true ) { allie_update.set( player_index, disconnect_player_index, _player_break_alliance ); SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 ); } } // ** for player_lists[ disconnect_player_index ].setStatus( _player_state_free ); PlayerState *player_state = getPlayerState( player_id ); PlayerStateSync player_state_update(player_state->getNetworkPlayerState()); SERVER->sendMessage( &player_state_update, sizeof( PlayerStateSync ), 0 ); }
void syncPlayerAndPlaylist() { if(playlist_state!=PLAYLIST_STATE_PLAY) return; if(getPlayerState()==PLAYER_STATE_STOP) playPlaylistIfPlayerStopped(); else syncPlaylistWithQueue(!playlist_queueError); syncCurrentPlayerDecodeMetadata(); }
void PlayerInterface::netMessageAllianceRequest( NetMessage *message ) { PlayerAllianceRequest *allie_request; PlayerState *player_state; allie_request = (PlayerAllianceRequest *) message; if ( allie_request->alliance_request_type == _player_make_alliance ) { setAlliance( allie_request->allie_by_player_index, allie_request->allie_with_player_index ); if( (allie_request->allie_by_player_index == local_player_index) ) { player_state = getPlayerState( allie_request->allie_with_player_index ); ConsoleInterface::postMessage( "Alliance created with %s.", player_state->getName().c_str() ); } else if( (allie_request->allie_with_player_index == local_player_index) ) { player_state = getPlayerState( allie_request->allie_by_player_index ); ConsoleInterface::postMessage( "%s has allied with you.", player_state->getName().c_str() ); } } else { clearAlliance( allie_request->allie_by_player_index, allie_request->allie_with_player_index ); if( (allie_request->allie_by_player_index == local_player_index) ) { player_state = getPlayerState( allie_request->allie_with_player_index ); ConsoleInterface::postMessage( "Alliance broken with %s.", player_state->getName().c_str() ); } else if( (allie_request->allie_with_player_index == local_player_index) ) { player_state = getPlayerState( allie_request->allie_by_player_index ); ConsoleInterface::postMessage( "%s has broken their alliance with you.", player_state->getName().c_str() ); } } PlayerAllianceUpdate allie_update; allie_update.allie_by_player_index = allie_request->allie_by_player_index; allie_update.allie_with_player_index = allie_request->allie_with_player_index; allie_update.alliance_update_type = allie_request->alliance_request_type; SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 ); }
int CheckersGame::nextMove(int from, int to){ if (setPlayerMouseDown(from)) return 0; if (setPlayerMouseDown(to)) return 0; prevState = getPlayerState(); curState = getComputerState(); return 1; }
void savePlaylistState() { if(playlist_stateFile) { FILE * fp; char tmp[MAXPATHLEN+1]; snprintf(tmp,MAXPATHLEN,"%s.%05d.%ld",playlist_stateFile, getpid(),time(NULL)); while(!(fp = fopen(tmp,"w")) && errno==EINTR); if(!fp) { ERROR("problems opening state file \"%s\" for " "writing\n",playlist_stateFile); return; } myfprintf(fp,"%s",PLAYLIST_STATE_FILE_STATE); switch(playlist_state) { case PLAYLIST_STATE_PLAY: switch(getPlayerState()) { case PLAYER_STATE_PAUSE: myfprintf(fp,"%s\n", PLAYLIST_STATE_FILE_STATE_PAUSE); break; default: myfprintf(fp,"%s\n", PLAYLIST_STATE_FILE_STATE_PLAY); } myfprintf(fp,"%s%i\n",PLAYLIST_STATE_FILE_CURRENT, playlist.order[playlist.current]); myfprintf(fp,"%s%i\n",PLAYLIST_STATE_FILE_TIME, getPlayerElapsedTime()); break; default: myfprintf(fp,"%s\n",PLAYLIST_STATE_FILE_STATE_STOP); break; } myfprintf(fp,"%s%i\n",PLAYLIST_STATE_FILE_RANDOM, playlist.random); myfprintf(fp,"%s%i\n",PLAYLIST_STATE_FILE_REPEAT, playlist.repeat); myfprintf(fp,"%s%i\n",PLAYLIST_STATE_FILE_CROSSFADE, (int)(getPlayerCrossFade())); myfprintf(fp,"%s\n",PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); showPlaylist(fp); myfprintf(fp,"%s\n",PLAYLIST_STATE_FILE_PLAYLIST_END); while(fclose(fp) && errno==EINTR); rename(tmp,playlist_stateFile); } }
void PlayerInterface::netMessageAllianceUpdate( NetMessage *message ) { PlayerAllianceUpdate *allie_update; PlayerState *player_state; allie_update = (PlayerAllianceUpdate *) message; if ( allie_update->alliance_update_type == _player_make_alliance ) { setAlliance( allie_update->allie_by_player_index, allie_update->allie_with_player_index ); if( (allie_update->allie_by_player_index == local_player_index) ) { player_state = getPlayerState( allie_update->allie_with_player_index ); ConsoleInterface::postMessage( "Alliance created with %s.", player_state->getName().c_str() ); } else if( (allie_update->allie_with_player_index == local_player_index) ) { player_state = getPlayerState( allie_update->allie_by_player_index ); ConsoleInterface::postMessage( "%s has allied with you.", player_state->getName().c_str() ); } } else { clearAlliance( allie_update->allie_by_player_index, allie_update->allie_with_player_index ); if( (allie_update->allie_by_player_index == local_player_index) ) { player_state = getPlayerState( allie_update->allie_with_player_index ); ConsoleInterface::postMessage( "Alliance broken with %s.", player_state->getName().c_str() ); } else if( (allie_update->allie_with_player_index == local_player_index) ) { player_state = getPlayerState( allie_update->allie_by_player_index ); ConsoleInterface::postMessage( "%s has broken their alliance with you.", player_state->getName().c_str() ); } } }
void playPlaylistIfPlayerStopped() { if(getPlayerState()==PLAYER_STATE_STOP) { int error = getPlayerError(); if(error==PLAYER_ERROR_NOERROR) playlist_errorCount = 0; else playlist_errorCount++; if(playlist_state==PLAYLIST_STATE_PLAY && ( (playlist_stopOnError && error!=PLAYER_ERROR_NOERROR) || error==PLAYER_ERROR_AUDIO || error==PLAYER_ERROR_SYSTEM || playlist_errorCount>=playlist.length)) { stopPlaylist(stderr); } else if(playlist_noGoToNext) currentSongInPlaylist(stderr); else nextSongInPlaylist(stderr); } }
void Player::frameEvent(float time) { for (int i = 0; i < NUM_POWERUP_BOARDS; i++) { // this method will return quickly if the powerup in question is already disabled if ( mPowerupStates[i].timeElapsed(mPowerupBars[i], time) ) { reLayoutPowerupBoards(); } } // Remove any powerups that have expired for( int i = 0; i < POWERUP_COUNT; i ++ ) { if( getCar() == NULL ) continue; if( powerupTimers[i] != 0 && RakNet::GreaterThan( RakNet::GetTimeMS(), powerupTimers[i] ) ) { switch( i ) { case POWERUP_MASS: getCar()->resetMass(); getCar()->resetEngineForce(); break; case POWERUP_SPEED: getCar()->resetEngineForce(); break; } powerupTimers[i] = 0; } } if( this == GameCore::mPlayerPool->getLocalPlayer() && getPlayerState() != PLAYER_STATE_SPECTATE && mLastKiller != NULL && RakNet::GreaterThan( RakNet::GetTimeMS(), mTimeLastKilled+3000 ) ) { setPlayerState( PLAYER_STATE_SPECTATE ); GameCore::mPlayerPool->setSpectating( mLastKiller->getPlayerGUID() ); mLastKiller = NULL; } }
static void idle(void) { static float lastT = SDL_GetTicks(); float ticks; ticks = SDL_GetTicks() - lastT; lastT = SDL_GetTicks(); /* * WORLD HANDLING */ ptl::tickParticles(); prjctl::tickProjectiles(ticks / 100.0); rck::tickRocks(ticks / 1000.0); rect4f plLimits; switch(getPlayerState()) { case PLST_DEAD: setPlayerState(PLST_NEEDSPAWN); playerLives --; break; case PLST_NEEDSPAWN: plLimits = player.getLimits(); for(int i=0; i < 10; i ++) { float x = getRndFloat(plLimits.x1, plLimits.x2); float y = getRndFloat(plLimits.y1, plLimits.y2); if(rck::closestRockDistance(pos2f(x, y)) > 4.0) { player.setPos(pos2f(x, y)); player.setSpeed(vector2f(0.0, 0.0)); setPlayerState(PLST_NORMAL); } } break; default: case PLST_NORMAL: if(player.checkCollide()) { setPlayerState(PLST_DEAD); } player.updatePos(ticks / 10.0); player.dampenSpeed(ticks / 50000.0); break; } if(rck::getLiveRocks() == 0) { curLvl ++; snd::playSound(snd::NEXT_LEVEL); loadLevel(curLvl); } /* * KEY HANDLING */ Uint8 *keys; keys = SDL_GetKeyState(NULL); if ( keys[SDLK_ESCAPE] ) { mainloopDone = 1; } if ( keys[SDLK_UP] ) { player.thrust(ticks / 10000.0); snd::startSoundOnce(snd::ENGINE); std::deque<ptl::cParticle> newPtls; for(int i = 0; i < 50; i ++) { float rndFloat = getRndFloat((player.getAngle()+200.0)*M_PI/180.0, (player.getAngle()+340.0)*M_PI/180.0); ptl::cParticle newPtl( getRndFloat(0.2, 0.4), 1.0, getRndFloat(0.0, 0.5), 0.0, cVertex(player.getPos().x + cos((player.getAngle()+90.0)*M_PI/180.0) * getRndFloat(1.1, 1.5), player.getPos().y + sin((player.getAngle()-90.0)*M_PI/180.0) * getRndFloat(1.1, 1.5), - 0.5), cVertex(-player.getSpeed().x*10.0 + cos(rndFloat+M_PI) * 5, -player.getSpeed().y*10.0 + sin(rndFloat)*5, getRndFloat(-2.0, 0.0)), cVertex(0.0, 0.0, 0) ); newPtls.push_back(newPtl); } ptl::createParticles(newPtls); } if ( !keys[SDLK_UP] ) { snd::stopSound(snd::ENGINE); } if ( keys[SDLK_DOWN] ) { } if ( keys[SDLK_LEFT] ) { player.turn(LEFT, ticks / 5.0); } if ( keys[SDLK_RIGHT] ) { player.turn(RIGHT, ticks / 5.0); } if ( keys[SDLK_SPACE] ) { static float shotTimer = 0; if(SDL_GetTicks() - shotTimer > 500) { snd::playSound(snd::LASER); prjctl::cProjectile newPrjctl; std::deque<prjctl::cProjectile> newPrjctls; newPrjctl.setPos(pos2f(player.getPos().x - cos((player.getAngle()+90.0)*M_PI/180.0) * 1.3, player.getPos().y - sin((player.getAngle()-90.0)*M_PI/180.0) * 1.3)); newPrjctl.setTtl(20.0); newPrjctl.setSpeed(vector2f(cos((player.getAngle()-90.0)*M_PI/180.0), sin((player.getAngle()+90.0)*M_PI/180.0))); newPrjctl.setAngle(player.getAngle()); newPrjctls.push_back(newPrjctl); prjctl::createProjectiles(newPrjctls); shotTimer = SDL_GetTicks(); } } if ( keys[SDLK_z] ) { } }
int main(int argc, char *argv[]) { char **playersNameList; int totalPlayersNumber; int turn, i; char buffer[BUFF_SIZE]; FILE *configFile; /* legge gli argomenti */ char **name1, **name2; if (argc < 4) { fprintf(stderr, "ERROR: Wrong number of arguments. \n USAGE: %s\n", USAGE); exit(EXIT_FAILURE); } playersNameList = argv + 3; totalPlayersNumber = argc - 3; /* controlla se ci sono due giocatori con lo stesso nome */ for (name1 = playersNameList; *name1; name1++) for (name2 = name1 + 1; *name2; name2++) if (strcmp(*name1, *name2) == 0) { fprintf(stderr, "ERROR: found two player with the" "same name \"%s\"\n", *name1); exit(EXIT_FAILURE); } initIoInterface(argv[2]); /* crea e inizializza le strutture dati per i giocatori */ initPlayersManager(totalPlayersNumber); for (; *playersNameList; playersNameList++) addPlayer(*playersNameList); initBoard(); /* * legge il file di configurazione secondo il formato: * numero_casella:descrizione della prova\n * e aggiunge le descrizioni al tabellone */ if ((configFile = fopen(argv[1], "r")) == NULL) { printErr("ERROR: error while opening configuration file\n"); exit(EXIT_FAILURE); } while (fgets(buffer, BUFF_SIZE, configFile)) { char *description; int boxNumber; /* legge il numero di casella */ if ((boxNumber = atoi(buffer)) <= 0) { printErr("ERROR:invalid box num(\"%s\") in" " configuration file\n", buffer); exit(EXIT_FAILURE); } /* aggiunge una nuova casella con la relativa descrizione */ if ((description = strchr(buffer, ':')) == NULL) { printErr("ERROR: missing ':' in configuration file\n"); exit(EXIT_FAILURE); } addBox(boxNumber, description + 1); } if (getTotalBoxesNumber() == 0) { printErr("ERROR: invalid configuration file\n"); exit(EXIT_FAILURE); } fclose(configFile); printBoard(); showGame(); /* avvia la simulazione del gioco */ srand(time(NULL)); for (turn = 0; !allPlayersDone(); turn++) { if (!nextStep()) return EXIT_SUCCESS; printMessage("\n**************************************\n"); printMessage("turno %d", turn + 1); printMessage("\n**************************************\n"); showGame(); /* * per ogni giocatore G che non ha terminato il gioco: * 1. se G e' fermo per un turno cambia il suo stato in * modo che al turno successivo venga rimesso in gioco * 2. altrimenti viene lanciato il dado, mosso il giocatore * e visualizzata la sua prova */ while (nextPlayer()) { int state = getPlayerState(); if (state == ACTIVE || state == TEST_PASSED || state == TO_BE_ACTIVATED) { if (state != ACTIVE) setPlayerState(ACTIVE, 0); movePlayer((rand() % 6) + 1); if (getPlayerBox() > getTotalBoxesNumber()) setPlayerState(DONE, turn); else printMessage("player %s: \"%s\"\n", getPlayerName(), getDescription(getPlayerBox())); } else if (state == OUT_OF_TURN) setPlayerState(TO_BE_ACTIVATED, 0); } showGame(); /* * Legge e registra l'esito di tutte le prove sostenute nel * turno corrente dai giocatori */ for (i = getActivePlayersNumber(); i > 0; i--) { int playerNumber; bool result; do { result = askPlayerResult(&playerNumber); if (playerNumber > totalPlayersNumber) printErr("WARNING: player number %d out of " "bounds [1; %d]\n", playerNumber, totalPlayersNumber); else { setCurrentPlayer(playerNumber); if (getPlayerState() != ACTIVE) printErr("WARNING: player number %d not " "valid because player:" "\n\t-won" "\n\t-is out of turn" "\n\t-already passed the test\n", playerNumber); } } while (playerNumber > totalPlayersNumber || getPlayerState() != ACTIVE); if (result) setPlayerState(TEST_PASSED, 0); else setPlayerState(OUT_OF_TURN, 0); } } printScore(); closeIoInterface(); return EXIT_SUCCESS; }