Main loop for the game

Results 1 to 7 of 7
  1. #1
    Dbo Dev Daneos is offline
    MemberRank
    Sep 2009 Join Date
    GermanyLocation
    933Posts

    Main loop for the game

    Since there is no main-loop for the game and I currently need it. I would like to talk about it.

    What do you think would be the best way to create the main loop? Loop for every session or only loop for the main game?

    I see on other games, that the mobs and npcs are moving on the same time and same direction, so Im sure the main loop is for the game and not session.

    But there is the problem.. I dont get it how to create the main loop with session etc.

    Questions:
    Where/When should the loop start?
    Which time-frame should the loop run?


  2. #2
    Don't quote me Arnie36 is offline
    MemberRank
    Jun 2006 Join Date
    207Posts

    Re: Main loop for the game

    I would do player actions in player sessions and npc spawning de-spawning maybe every second depends on the spawning distance in the main thread
    mob spawning de spawning and actions(moving attacking) also in the main thread continuously, just put a sleep(1) at the end of the loop to keep the cpu load low

    and when a player is in a fight, do the updates for that also in the main thread


    and I suppose you already have a main thread, I haven't looked at your source but there must be a while loop some where after initializing everything which waits for the program to end

    in the sample server it looks like

    Code:
    while( IsRunnable() )
    {		
    	dwTickCur = ::GetTickCount();
    	if( dwTickCur - dwTickOld >= 100000 )
    	{
    		NTL_PRINT(PRINT_APP, "GameServer Run()");
    		dwTickOld = dwTickCur;
    	}
    	Sleep( 1 );
    }
    Last edited by Arnie36; 24-06-14 at 07:37 PM.

  3. #3
    Dbo Dev Daneos is offline
    MemberRank
    Sep 2009 Join Date
    GermanyLocation
    933Posts

    Re: Main loop for the game

    Yea I just ordered all things and made source more clear.

    But the problem is, for example create attack combat..

    When you click on mob, then it will save data into list and that list:
    Code:
    void CClientSession::SendAttackBegin(CNtlPacket * pPacket, CGameServer * app)
    {
    	printf("--- ATTACK BEGIN --- \n");
    	sUG_CHAR_ATTACK_BEGIN* req = (sUG_CHAR_ATTACK_BEGIN *)pPacket->GetPacketData();
    
    	if(req->byType == 0)
    	{
    		app->AddAttackBegin(this->GetTargetSerialId());
    	}
    	else if(req->byType == 1)
    	{
    		printf("ATTACK FOR TYPE 1 NOT EXIST \n");
    	}
    
    }
    AddAttackBegin -> I have inside main thread

    Code:
    void CGameServer::AddAttackBegin(RwUInt32 uiSerialId)
    {
    	SBattleData *pBattleData = new SBattleData;
    
    	printf("ADDATTACKBEGIN SERIAL %i \n", uiSerialId);
    
    	pBattleData->uiSerialId			= uiSerialId;
    	pBattleData->uiTargetSerialId	= 0xffffffff;
    	pBattleData->bAttackMode		= TRUE;
    	pBattleData->dwCurrTime			= timeGetTime();
    
    	m_listAttackBegin.push_back(pBattleData);
    
    }
    now the work from the loop should appear and do this:
    Code:
    SBattleData *pBattleData;
    	ListAttackBegin::iterator it;
    	for(it = m_listAttackBegin.begin(); it != m_listAttackBegin.end(); it++)
    	{
    		pBattleData = (*it);
    		if(timeGetTime() - pBattleData->dwCurrTime >= 1000)
    		{
    			SendCharActionAttack(pBattleData->uiSerialId);
    			pBattleData->dwCurrTime = timeGetTime();
    		}
    	}
    but the problem: for
    Code:
    SendCharActionAttack(pBattleData->uiSerialId);
    I need current char session...

    SendCharActionAttack:
    Code:
    void CClientSession::SendCharActionAttack(RwUInt32 uiSerialId)
    {
    	static RwUInt8 byChainAttack = 0;
    	RwBool bDamageApply = TRUE;
    
    	CNtlPacket packet(sizeof(sGU_CHAR_ACTION_ATTACK));
    	sGU_CHAR_ACTION_ATTACK * res = (sGU_CHAR_ACTION_ATTACK *)packet.GetPacketData();
    
    	res->wOpCode = GU_CHAR_ACTION_ATTACK;
    	res->hSubject = this->GetavatarHandle();
    	res->hTarget = this->GetTargetSerialId();
    	res->bChainAttack = TRUE;
    
    	res->wAttackResultValue = 150;
    	
    	res->byAttackSequence = rand()%2;
    
    	if(res->byAttackSequence == 6)
    	{
    		if(rand()%2)
    			res->byAttackResult = BATTLE_ATTACK_RESULT_KNOCKDOWN;
    		else
    			res->byAttackResult = BATTLE_ATTACK_RESULT_SLIDING;
    	}
    	else
    	{
    		RwInt32 iRandValue = rand()%5;
    		if(iRandValue <= 2)
    			res->byAttackResult = BATTLE_ATTACK_RESULT_HIT;
    		else if(iRandValue == 3)
    		{
    			bDamageApply = FALSE;
    			res->byAttackResult = BATTLE_ATTACK_RESULT_DODGE;
    		}
    		else
    		{
    			bDamageApply = FALSE;
    			res->byAttackResult = BATTLE_ATTACK_RESULT_BLOCK;
    		}
    	}
    
    	packet.SetPacketLen( sizeof(sGU_CHAR_ACTION_ATTACK) );
    	int rc = g_pApp->Send( this->GetHandle(), &packet );
    
    	byChainAttack++;
    
    }
    - - - Updated - - -

    Well here are 2 screens.. Maybe you will understand the error I have with the main loop and why I opened this thread



  4. #4
    Don't quote me Arnie36 is offline
    MemberRank
    Jun 2006 Join Date
    207Posts

    Re: Main loop for the game

    your m_listattackbigin list should contain info about the attacker and the target and using that info iterate the session list to find that session or make that info the pSession

  5. #5
    Dbo Dev Daneos is offline
    MemberRank
    Sep 2009 Join Date
    GermanyLocation
    933Posts

    Re: Main loop for the game

    Working on that right now, but the problem is -> how do I get the current session from the attacker (uiSerialId)?

  6. #6
    Don't quote me Arnie36 is offline
    MemberRank
    Jun 2006 Join Date
    207Posts

    Re: Main loop for the game

    Quote Originally Posted by Daneos View Post
    Working on that right now, but the problem is -> how do I get the current session from the attacker (uiSerialId)?
    if you want to use session at that spot

    you have to start iterating the player list at that spot to get the session

    and than session would be it->first maybe

    but I would do it like

    int UpdateClient(void)
    {
    for( USERIT pSession = m_userList.begin(); pSession != m_userList.end(); pSession++ )
    Last edited by Arnie36; 24-06-14 at 10:21 PM.

  7. #7
    Dbo Dev Daneos is offline
    MemberRank
    Sep 2009 Join Date
    GermanyLocation
    933Posts

    Re: Main loop for the game

    Didnt need the it->first ..
    it was enough to add everywhere CClientSession * pSession



Advertisement