Esempio n. 1
0
void SingleDuel::PlayerKick(DuelPlayer* dp, unsigned char pos) {
	if(pos > 1 || dp != host_player || dp == players[pos] || !players[pos])
		return;
	LeaveGame(players[pos]);
}
Esempio n. 2
0
/*
=================
idCommonLocal::Frame
=================
*/
void idCommonLocal::Frame() {
	try {
		SCOPED_PROFILE_EVENT( "Common::Frame" );

		// This is the only place this is incremented
		idLib::frameNumber++;

		// allow changing SIMD usage on the fly
		if ( com_forceGenericSIMD.IsModified() ) {
			idSIMD::InitProcessor( "doom", com_forceGenericSIMD.GetBool() );
			com_forceGenericSIMD.ClearModified();
		}

		// Do the actual switch between Doom 3 and the classics here so
		// that things don't get confused in the middle of the frame.
		PerformGameSwitch();

		// pump all the events
		Sys_GenerateEvents();

		// write config file if anything changed
		WriteConfiguration(); 

		eventLoop->RunEventLoop();

		// Activate the shell if it's been requested
		if ( showShellRequested && game ) {
			game->Shell_Show( true );
			showShellRequested = false;
		}

		// if the console or another gui is down, we don't need to hold the mouse cursor
		bool chatting = false;
		if ( console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() || ( game && game->InhibitControls() && !IsPlayingDoomClassic() ) ) {
			Sys_GrabMouseCursor( false );
			usercmdGen->InhibitUsercmd( INHIBIT_SESSION, true );
			chatting = true;
		} else {
			Sys_GrabMouseCursor( true );
			usercmdGen->InhibitUsercmd( INHIBIT_SESSION, false );
		}

		const bool pauseGame = ( !mapSpawned || ( !IsMultiplayer() && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() || ( game && game->Shell_IsActive() ) ) ) ) && !IsPlayingDoomClassic();

		// save the screenshot and audio from the last draw if needed
		if ( aviCaptureMode ) {
			idStr name = va("demos/%s/%s_%05i.tga", aviDemoShortName.c_str(), aviDemoShortName.c_str(), aviDemoFrameCount++ );
			renderSystem->TakeScreenshot( com_aviDemoWidth.GetInteger(), com_aviDemoHeight.GetInteger(), name, com_aviDemoSamples.GetInteger(), NULL );

			// remove any printed lines at the top before taking the screenshot
			console->ClearNotifyLines();

			// this will call Draw, possibly multiple times if com_aviDemoSamples is > 1
			renderSystem->TakeScreenshot( com_aviDemoWidth.GetInteger(), com_aviDemoHeight.GetInteger(), name, com_aviDemoSamples.GetInteger(), NULL );
		}

		//--------------------------------------------
		// wait for the GPU to finish drawing
		//
		// It is imporant to minimize the time spent between this
		// section and the call to renderSystem->RenderCommandBuffers(),
		// because the GPU is completely idle.
		//--------------------------------------------
		// this should exit right after vsync, with the GPU idle and ready to draw
		// This may block if the GPU isn't finished renderng the previous frame.
		frameTiming.startSyncTime = Sys_Microseconds();
		const emptyCommand_t * renderCommands = NULL;
		if ( com_smp.GetBool() ) {
			renderCommands = renderSystem->SwapCommandBuffers( &time_frontend, &time_backend, &time_shadows, &time_gpu );
		} else {
			// the GPU will stay idle through command generation for minimal
			// input latency
			renderSystem->SwapCommandBuffers_FinishRendering( &time_frontend, &time_backend, &time_shadows, &time_gpu );
		}
		frameTiming.finishSyncTime = Sys_Microseconds();

		//--------------------------------------------
		// Determine how many game tics we are going to run,
		// now that the previous frame is completely finished.
		//
		// It is important that any waiting on the GPU be done
		// before this, or there will be a bad stuttering when
		// dropping frames for performance management.
		//--------------------------------------------

		// input:
		// thisFrameTime
		// com_noSleep
		// com_engineHz
		// com_fixedTic
		// com_deltaTimeClamp
		// IsMultiplayer
		//
		// in/out state:
		// gameFrame
		// gameTimeResidual
		// lastFrameTime
		// syncNextFrame
		//
		// Output:
		// numGameFrames

		// How many game frames to run
		int numGameFrames = 0;

		for(;;) {
			const int thisFrameTime = Sys_Milliseconds();
			static int lastFrameTime = thisFrameTime;	// initialized only the first time
			const int deltaMilliseconds = thisFrameTime - lastFrameTime;
			lastFrameTime = thisFrameTime;

			// if there was a large gap in time since the last frame, or the frame
			// rate is very very low, limit the number of frames we will run
			const int clampedDeltaMilliseconds = Min( deltaMilliseconds, com_deltaTimeClamp.GetInteger() );

			gameTimeResidual += clampedDeltaMilliseconds * timescale.GetFloat();

			// don't run any frames when paused
			if ( pauseGame ) {
				gameFrame++;
				gameTimeResidual = 0;
				break;
			}

			// debug cvar to force multiple game tics
			if ( com_fixedTic.GetInteger() > 0 ) {
				numGameFrames = com_fixedTic.GetInteger();
				gameFrame += numGameFrames;
				gameTimeResidual = 0;
				break;
			}

			if ( syncNextGameFrame ) {
				// don't sleep at all
				syncNextGameFrame = false;
				gameFrame++;
				numGameFrames++;
				gameTimeResidual = 0;
				break;
			}

			for ( ;; ) {
				// How much time to wait before running the next frame,
				// based on com_engineHz
				const int frameDelay = FRAME_TO_MSEC( gameFrame + 1 ) - FRAME_TO_MSEC( gameFrame );
				if ( gameTimeResidual < frameDelay ) {
					break;
				}
				gameTimeResidual -= frameDelay;
				gameFrame++;
				numGameFrames++;
				// if there is enough residual left, we may run additional frames
			}

			if ( numGameFrames > 0 ) {
				// ready to actually run them
				break;
			}

			// if we are vsyncing, we always want to run at least one game
			// frame and never sleep, which might happen due to scheduling issues
			// if we were just looking at real time.
			if ( com_noSleep.GetBool() ) {
				numGameFrames = 1;
				gameFrame += numGameFrames;
				gameTimeResidual = 0;
				break;
			}

			// not enough time has passed to run a frame, as might happen if
			// we don't have vsync on, or the monitor is running at 120hz while
			// com_engineHz is 60, so sleep a bit and check again
			Sys_Sleep( 0 );
		}

		//--------------------------------------------
		// It would be better to push as much of this as possible
		// either before or after the renderSystem->SwapCommandBuffers(),
		// because the GPU is completely idle.
		//--------------------------------------------

		// Update session and syncronize to the new session state after sleeping
		session->UpdateSignInManager();
		session->Pump();
		session->ProcessSnapAckQueue();

		if ( session->GetState() == idSession::LOADING ) {
			// If the session reports we should be loading a map, load it!
			ExecuteMapChange();
			mapSpawnData.savegameFile = NULL;
			mapSpawnData.persistentPlayerInfo.Clear();
			return;
		} else if ( session->GetState() != idSession::INGAME && mapSpawned ) {
			// If the game is running, but the session reports we are not in a game, disconnect
			// This happens when a server disconnects us or we sign out
			LeaveGame();
			return;
		}

		if ( mapSpawned && !pauseGame ) {
			if ( IsClient() ) {
				RunNetworkSnapshotFrame();
			}
		}

		ExecuteReliableMessages();

		// send frame and mouse events to active guis
		GuiFrameEvents();

		//--------------------------------------------
		// Prepare usercmds and kick off the game processing
		// in a background thread
		//--------------------------------------------

		// get the previous usercmd for bypassed head tracking transform
		const usercmd_t	previousCmd = usercmdGen->GetCurrentUsercmd();

		// build a new usercmd
		int deviceNum = session->GetSignInManager().GetMasterInputDevice();
		usercmdGen->BuildCurrentUsercmd( deviceNum );
		if ( deviceNum == -1 ) {
			for ( int i = 0; i < MAX_INPUT_DEVICES; i++ ) {
				Sys_PollJoystickInputEvents( i );
				Sys_EndJoystickInputEvents();
			}
		}
		if ( pauseGame ) {
			usercmdGen->Clear();
		}

		usercmd_t newCmd = usercmdGen->GetCurrentUsercmd();

		// Store server game time - don't let time go past last SS time in case we are extrapolating
		if ( IsClient() ) {
			newCmd.serverGameMilliseconds = std::min( Game()->GetServerGameTimeMs(), Game()->GetSSEndTime() );
		} else {
			newCmd.serverGameMilliseconds = Game()->GetServerGameTimeMs();
		}

		userCmdMgr.MakeReadPtrCurrentForPlayer( Game()->GetLocalClientNum() );

		// Stuff a copy of this userCmd for each game frame we are going to run.
		// Ideally, the usercmds would be built in another thread so you could
		// still get 60hz control accuracy when the game is running slower.
		for ( int i = 0 ; i < numGameFrames ; i++ ) {
			newCmd.clientGameMilliseconds = FRAME_TO_MSEC( gameFrame-numGameFrames+i+1 );
			userCmdMgr.PutUserCmdForPlayer( game->GetLocalClientNum(), newCmd );
		}

		// If we're in Doom or Doom 2, run tics and upload the new texture.
		if ( ( GetCurrentGame() == DOOM_CLASSIC || GetCurrentGame() == DOOM2_CLASSIC ) && !( Dialog().IsDialogPausing() || session->IsSystemUIShowing() ) ) {
			RunDoomClassicFrame();
		}
		
		// start the game / draw command generation thread going in the background
		gameReturn_t ret = gameThread.RunGameAndDraw( numGameFrames, userCmdMgr, IsClient(), gameFrame - numGameFrames );

		if ( !com_smp.GetBool() ) {
			// in non-smp mode, run the commands we just generated, instead of
			// frame-delayed ones from a background thread
			renderCommands = renderSystem->SwapCommandBuffers_FinishCommandBuffers();
		}

		//----------------------------------------
		// Run the render back end, getting the GPU busy with new commands
		// ASAP to minimize the pipeline bubble.
		//----------------------------------------
		frameTiming.startRenderTime = Sys_Microseconds();
		renderSystem->RenderCommandBuffers( renderCommands );
		if ( com_sleepRender.GetInteger() > 0 ) {
			// debug tool to test frame adaption
			Sys_Sleep( com_sleepRender.GetInteger() );
		}
		frameTiming.finishRenderTime = Sys_Microseconds();

		// make sure the game / draw thread has completed
		// This may block if the game is taking longer than the render back end
		gameThread.WaitForThread();

		// Send local usermds to the server.
		// This happens after the game frame has run so that prediction data is up to date.
		SendUsercmds( Game()->GetLocalClientNum() );

		// Now that we have an updated game frame, we can send out new snapshots to our clients
		session->Pump(); // Pump to get updated usercmds to relay
		SendSnapshots();

		// Render the sound system using the latest commands from the game thread
		if ( pauseGame ) {
			soundWorld->Pause();
			soundSystem->SetPlayingSoundWorld( menuSoundWorld );
		} else {
			soundWorld->UnPause();
			soundSystem->SetPlayingSoundWorld( soundWorld );
		}
		soundSystem->Render();

		// process the game return for map changes, etc
		ProcessGameReturn( ret );

		idLobbyBase & lobby = session->GetActivePlatformLobbyBase();
		if ( lobby.HasActivePeers() ) {
			if ( net_drawDebugHud.GetInteger() == 1 ) {
				lobby.DrawDebugNetworkHUD();
			}
			if ( net_drawDebugHud.GetInteger() == 2 ) {
				lobby.DrawDebugNetworkHUD2();
			}
			lobby.DrawDebugNetworkHUD_ServerSnapshotMetrics( net_drawDebugHud.GetInteger() == 3 );
		}

		// report timing information
		if ( com_speeds.GetBool() ) {
			static int lastTime = Sys_Milliseconds();
			int	nowTime = Sys_Milliseconds();
			int	com_frameMsec = nowTime - lastTime;
			lastTime = nowTime;
			Printf( "frame:%d all:%3d gfr:%3d rf:%3lld bk:%3lld\n", idLib::frameNumber, com_frameMsec, time_gameFrame, time_frontend / 1000, time_backend / 1000 );
			time_gameFrame = 0;
			time_gameDraw = 0;
		}

		// the FPU stack better be empty at this point or some bad code or compiler bug left values on the stack
		if ( !Sys_FPU_StackIsEmpty() ) {
			Printf( Sys_FPU_GetState() );
			FatalError( "idCommon::Frame: the FPU stack is not empty at the end of the frame\n" );
		}

		mainFrameTiming = frameTiming;

		session->GetSaveGameManager().Pump();
	} catch( idException & ) {
		return;			// an ERP_DROP was thrown
	}
}
Esempio n. 3
0
File: IRoom.c Progetto: johnsie/IMR
int main( int pArgc, const char** pArgs )
{
   const char* lQueryPtr;
   int         lLock;
   int         lMemId;
   char        lQuery[4096];
   char        lOp[12];
   BOOL        lPrintTitle;


   #ifndef _NO_IPC_
      union semun lNULL;
   #endif

   #ifdef _NO_IPC_
 
      gGlobalState.Clear();
      if( InitLogFile() )
      {
         fprintf( gLogFile, "IMR Init version 0.13.13b %s\n", GetTime( time(NULL)) );
      }

   #endif


   #ifdef _FAST_CGI_
   while( FCGI_Accept() >= 0 )
   {
   #endif
      lPrintTitle = TRUE;

      /* Send the header required by the server */
      printf("Content-type: text/plain%c%c", 10, 10);


      lQueryPtr = getenv( "QUERY_STRING" );

      if( lQueryPtr == NULL )
      {
         printf( "No parameters\n" );
      }
      else
      {

         StrMaxCopy( lQuery, lQueryPtr, 4096 );

         UnpadQuery( lQuery );

         if( sscanf( lQuery, "=%11s", lOp ) == 1 )
         {
            #ifndef _FAST_CGI_
               /* Change the local directory */
               char* lPathEnd = strrchr( pArgs[0], '/' );

               if( lPathEnd != NULL )
               {
                  *lPathEnd = 0;
                  chdir( pArgs[0] );
               }
            #endif


            if( !strcmp( lOp, "RESET" ) )
            {
               printf( "RESET OK\n\n" );

               #ifdef _NO_IPC_
                  /*
                  // gGlobalState.Clear();
                  // sleep( 2 );
                  // return 0;
                  // break;
                  */
               #else

                  lLock  = semget( IR_SEM_KEY, 1, 0777 );
                  lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 );

                  if( lLock != -1 )
                  {
                     semctl( lLock, 0, IPC_RMID, lNULL );
                  }

                  if( lMemId != -1 )
                  {
                     shmctl( lMemId, IPC_RMID, NULL );
                  }

               #endif

               if( InitLogFile() )
               {
                  fprintf( gLogFile, "IMR Reset %s\n", GetTime( time(NULL)) );
               }

               #ifdef _FAST_CGI_
                  break;
               #endif

            }
            else if( !strcmp( lOp, "SET_MESSAGE" ) )
            {
            }
            /*
            else if( !strcmp( lOp, "STICK" ) )
            {
               // Fork and leave a child running for 1 hour

               int lCode = fork();
               if( lCode == 0 )
               {
                  if( InitLogFile() )
                  {
                     fprintf( gLogFile, "STICK Start %s\n", GetTime( time(NULL)) );
                     CloseLogFile();
                  }

                  close( 0 );
                  close( 1 );
                  close( 2 );
                  sleep( 3600 );

                  if( InitLogFile() )
                  {
                     fprintf( gLogFile, "STICK End %s\n", GetTime( time(NULL)) );
                     CloseLogFile();
                  }

               }
               
               else if( lCode == -1 )
               {
                  printf( "ERROR\n" );
               }
               else
               {
                  printf( "SUCCESS\n" );
               }
               
            }
            */
            else
            {
               IRState* lState = NULL;

               #ifdef _NO_IPC_
                  lState = &gGlobalState;

               #else

                  int      lLock;        /* Semaphore */
                  int      lMemId;       /* Shared memory */

                  struct sembuf lSemOp;

                  lSemOp.sem_flg = 0;  /*Avoid corruption but must not core-dump SEM_UNDO;  // Risky but prevents dead-lock */
                  lSemOp.sem_num  = 0;

                  /* First try to create the structure for the first time */
                  /* Lock the data struct */
                  lLock = semget( IR_SEM_KEY, 1, 0777|IPC_CREAT|IPC_EXCL );

                  if( lLock != -1 )
                  {
                     union semun lArg;
               
                     /* Initialize the newly created semaphore */
                     lArg.val = 1;
 
                     semctl( lLock, 0, SETVAL, lArg );
                  }
                  else
                  {
                     lLock = semget( IR_SEM_KEY, 1, 0777 );
                  }

                  if( lLock == -1 )
                  {
                     printf( "Unable to get semaphore\n" );
                  }
                  else
                  {
                     lSemOp.sem_op   = -1;

                     if( semop( lLock, &lSemOp, 1 ) == -1 )
                     {
                        printf( "Unable to decrement semaphore\n" );
                        lLock = -1;
                     }
                     else
                     {

                        /* From here we can work safely */

                        lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666|IPC_CREAT|IPC_EXCL );

                        if( lMemId != -1 )
                        {
                           lState = (IRState*)shmat( lMemId, NULL, 0 );

                           if( (int)lState == -1 )
                           {
                              lState = NULL;
                           }

                           if( lState == NULL )
                           {
                              printf( "Unable to attach shmem\n" );
                           }
                           else
                           {
                              Clear( lState );

                              if( InitLogFile() )
                              {
                                 fprintf( gLogFile, "IMR Init %s\n", GetTime( time(NULL)) );
                              }

                           }
                        }
                        else
                        {
                           lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 );

                           if( lMemId == -1 )
                           {
                              printf( "Unable to get shmem\n" );
                           }
                           else
                           {
                              lState = (IRState*)shmat( lMemId, NULL, 0 );

                              if( (int)lState == -1 )
                              {
                                 lState = NULL;
                              }

                              if( lState == NULL )
                              {
                                 printf( "Unable to attach shmem\n" );
                              }
                           }
                        }
                     }
                  }

               #endif
   
               if( lState != NULL )
               {

                  lPrintTitle = FALSE;

                  VerifyExpirations( lState );

                  if( !strcmp( lOp, "REFRESH" ) )
                  {
                     int lUserIndex;
                     int lUserId;
                     int lTimeStamp;

                     if( sscanf( lQuery, "%*s %d-%u %d", &lUserIndex, &lUserId, &lTimeStamp )==3 )
                     {
                        PrintStateChange( lState, lUserIndex, lUserId, lTimeStamp );
                     }
                  }
                  else if( !strcmp( lOp, "ADD_CHAT" ) )
                  {
                     int  lUserIndex;
                     int  lUserId;
                     char lChatMessage[200];

                     if( sscanf( lQuery, "%*s %d-%u %200s", &lUserIndex, &lUserId, lChatMessage )==3 )
                     {
                        Unpad( lChatMessage );
                        AddChatMessage( lState, lUserIndex, lUserId, lChatMessage );
                     }
                  }
                  /*   URL?=ADD_USER MAJOR-MINORID VERSION KEY2 KEY3 ALIAS */
                  else if( !strcmp( lOp, "ADD_USER" ) )
                  {
                     int lMajorID;
                     int lMinorID;
                     int  lVersion;
                     unsigned int lKey2;
                     unsigned int lKey3;
                     char lUserName[40];

                     #ifdef _EXPIRED_ 
                        AddUser(  lState, "User", 1,-1, -1, 0, 0  );
                     #else
                        if( sscanf( lQuery, "%*s %d-%d %d %d %d %40s", &lMajorID, &lMinorID, &lVersion, &lKey2, &lKey3, lUserName )==6 )
                        {
                           Unpad( lUserName );
                           AddUser( lState, lUserName, lVersion,lMajorID, lMinorID, lKey2, lKey3  );
                        }
                     #endif
                  }
                  /* URL?=ADD_GAME USER_ID GAME_NAME TRACK_NAME NBLAP WEAPON PORT */
                  else if( !strcmp( lOp, "ADD_GAME" ) )
                  {
                     int       lUserIndex;
                     int       lUserId;
                     int       lNbLap;
                     char      lGameName[40];
                     char      lTrackName[40];
                     int       lWeapon;
                     unsigned  lPort;
               
                     if( sscanf( lQuery, "%*s %d-%u %40s %40s %d %d %u", &lUserIndex, &lUserId, lGameName, lTrackName, &lNbLap, &lWeapon, &lPort )==7 )
                     {
                        const char* lRemoteAddr = getenv( "REMOTE_ADDR" );

                        if( (lRemoteAddr != NULL)&&(strlen(lRemoteAddr) != 0) )
                        {
                           Unpad( lTrackName );
                           Unpad( lGameName );
                           AddGame( lState, lGameName, lTrackName, lNbLap, lUserIndex, lUserId, lRemoteAddr, lPort, lWeapon  );
                        }
                     }
                  }
                  else if( !strcmp( lOp, "JOIN_GAME" ) )
                  {
                     int   lUserIndex;
                     int   lUserId;
                     int   lGameIndex;
                     int   lGameId;
               
                     if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 )
                     {
                        JoinGame( lState, lGameIndex, lGameId, lUserIndex, lUserId );
                     }
                  }
                  else if( !strcmp( lOp, "DEL_GAME" ) )
                  {
                     int   lUserIndex;
                     int   lUserId;
                     int   lGameIndex;
                     int   lGameId;
               
                     if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 )
                     {
                        DeleteGame( lState, lGameIndex, lGameId, lUserIndex, lUserId );
                     }
                  }
                  else if( !strcmp( lOp, "LEAVE_GAME" ) )
                  {
                     int   lUserIndex;
                     int   lUserId;
                     int   lGameIndex;
                     int   lGameId;
               
                     if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 )
                     {
                        LeaveGame( lState, lGameIndex, lGameId, lUserIndex, lUserId );
                     }
                  }
                  else if( !strcmp( lOp, "DEL_USER" ) )
                  {
                     int   lUserIndex;
                     int   lUserId;
               
                     if( sscanf( lQuery, "%*s %d-%u", &lUserIndex, &lUserId )==2 )
                     {
                        DeleteUser( lState, lUserIndex, lUserId );
                     }
                  }
                  else if( !strcmp( lOp, "START_GAME" ) )
                  {
                     int   lUserIndex;
                     int   lUserId;
                     int   lGameIndex;
                     int   lGameId;
               
                     if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 )
                     {
                        StartGame( lState, lGameIndex, lGameId, lUserIndex, lUserId );
                     }
                  }
                  else
                  {
                     lPrintTitle = TRUE;
                  }
               }

               #ifdef _NO_IPC_
                  lState = NULL;
               #else
                  /* Release lock */
                  if( lLock != -1 )
                  {
                     lSemOp.sem_op   = 1;

                     semop( lLock, &lSemOp, 1 );

                     /* Release memory */
                     if( lState != NULL )
                     {
                        shmdt( (char*)lState );
                     }
                  }
                  
               #endif
               
            }
         }
      }

      CloseLogFile();

      if( lPrintTitle )
      {
         printf( "Internet Meeting Room (c)1996,97 GrokkSoft inc.\n" );
      }
  #ifdef _FAST_CGI_
  }
  #endif

   return 0;
}