⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gametimer.cpp

📁 国内著名网络游戏dragon的服务端完整源码 内附完整数据库结构
💻 CPP
字号:
//
// GameTimer.cpp
//

// --------------------
// 悼利 累诀盒且 矫胶袍
// --------------------
#include "GameTimer.h"
#include "..\stdafx.h"
#include <Mmsystem.h>
#include "monitor.h"
#include "mylog.h"
#include "..\HigherLayers\NPC_Pattern.h"
#include "..\HigherLayers\DefaultHeader.h"
#include "..\HigherLayers\Scrp_exe.h"
#include "..\HigherLayers\Citem.h"
#include "..\HigherLayers\Op_magic.h"
#include "..\HigherLayers\area.h"
#include "..\HigherLayers\SealStone.h"
#include "..\HigherLayers\QuestFunction.h"	// 021017 kyo
#include "..\HigherLayers\ArenaManager.h"
#include "..\HigherLayers\BattleManager.h"
#include "..\HigherLayers\UserManager.h"

void CheckElapsedTime_NK_Down( void );
void prepare(t_connection c[]);

CGameTimer GameTimerList[NUM_OF_GTES];	// GameTimerList 硅凯, NUM_OF_GAME_TIMERS 绰 霸烙鸥捞赣狼 眠啊, 昏力啊 乐阑锭 怖 绊媚拎具 茄促.(吝夸)
DWORD end_of_tick = 0;					// SetGameTimer()俊辑 磊悼 盎脚秦 林骨肺 绊磨 鞘夸 绝促.
DWORD dwTimerFlag = 0;
bool IsGameTimerWorking = false;

extern void UpdateEventMonster();		// LTS AI2


void NPCRegen( DWORD all, DWORD now )
{
	static DWORD lasttime = 0;
	static DWORD curtime = 0;
	int total = 40;

	int from;
	int to;

	static int increasement;

	curtime = timeGetTime();

	static counter = 0;

	static run = 0;

	if( all > 1 ) {
		if( now == 0 ) MyLog( LOG_IGNORE, "NPCRegen::Called %d(%ldms) (%d/%d) Started", counter, (curtime-lasttime), now+1, all);
		if( now == (all-1) ) MyLog( LOG_IGNORE, "NPCRegen::Called %d(%ldms) (%d/%d) Finished", counter, (curtime-lasttime), now+1, all);
		//MyLog( LOG_NORMAL, "NPCRegen::Called %d(%ldms) (%d/%d)", counter, (curtime-lasttime), now+1, all);
	} else {
		MyLog( LOG_NORMAL, "NPCRegen::Called %d(%ldms)", counter, (curtime-lasttime));
	}
	counter++;

	lasttime = curtime;

	if( now == 0 ) {
		increasement = (int)((float)total/(float)all);
	}

	from = now*increasement;
	// 付瘤阜浚 唱赣瘤甫 促 贸府
	if( now == (all-1) ) { to = total; run++; MyLog(LOG_IGNORE, "OKOKOKOKOK"); }

	else to = from+increasement;

	for( int i = from; i < to; i++ ) {
		if( run < 3 ) {
			Sleep(100);
		} else Sleep(10);
	}	
		
	return;
}		
		
// Modified by chan78 at 2000/11/29, 老窜 盒幅秦 狄.
void very_short_terms()		// 150 ms
{	//< CSD-HK-030829		
	prepare(connections);
	g_pUserManager->Execute();
}	//> CSD-HK-030829
		
void short_terms()		// 300 ms
{		
	MovingNPC();
	MovingCharacter();
	RunArea(connections);
	return;
}		
		
void normal_terms()		// 450 ms
{
	ReCallScript();
	CheckSkillTimeElapsed();
}
	
void long_terms()		// 600 ms
{
	NPC_CheckRemove();
	SendWeatherRoutine(connections);
	NPC_AutoCreate();
	SendAliveCheckRoutine(connections);
	return;
}		
	
extern void CheckTimeForMenu_Kein();		// 010419 YGI		
extern void UpdateDragonLordWarTime();		// LTS DRAGONLORD

void very_long_terms()	// 750 ms
{		
	g_pBattleManager->Elapse();    
	g_pArenaManager->Ready();   // CSD-030521
	g_pArenaManager->Execute(); // CSD-030516
	CheckNationWar();	
	CharacterAutoUpdate();
	CheckItemTimeElapsed();
	CheckTimeForMenu_Kein(); // 010419 YGI
	UpdateEventMonster();		 // LTS AI2
	UpdateDragonLordWarTime();		// LTS DRAGONLORD
	g_QuestInMap.RunQuestTimer();// 021104 kyos
	CheckElapsedTime_NK_Down();
	return;
}		
		
		
CGameTimer::CGameTimer()
{		
	sTimerProcessName[0] = '\0';
	dwTickSize = 0;
	bIsDividable = false;
	dwExpectedTime = 0;
	wNumOfDividedBy = 1;
	wNextNumOfDividedBy = 0;
	wCurrentProcessPos = 0;
	dwTotalTakeTime = 0;
	DividableFuncToCall = NULL;
	NormalFuncToCall = NULL;
}	
	
CGameTimer::~CGameTimer()
{	
}	
	
DWORD CGameTimer::GetExpectedTime()
{	
	return dwExpectedTime;
}		
void CGameTimer::SetGameTimer(char *name, DWORD ticksize, bool dividable, DWORD expecttime, MYFUNC func)
{	
	if( strlen(name) > TIMER_PROCESS_NAME_LENGTH ) {
		strncpy(sTimerProcessName, name, TIMER_PROCESS_NAME_LENGTH);
		sTimerProcessName[TIMER_PROCESS_NAME_LENGTH] = '\0';
	} else strcpy(sTimerProcessName, name);
	dwTickSize = ticksize;
	if( dwTickSize > end_of_tick ) end_of_tick = dwTickSize;
	bIsDividable = dividable;
	dwExpectedTime = expecttime;

	DividableFuncToCall = func;
	NormalFuncToCall = NULL;

	MyLog(LOG_IGNORE, "%s, (tick)%d, (able)%d, (expect)%d are ADDED", name, ticksize, dividable, expecttime);
	return;
}	
	
void CGameTimer::SetGameTimer(char *name, DWORD ticksize, bool dividable, DWORD expecttime, VOIDFUNCS func)
{	
	if( strlen(name) > TIMER_PROCESS_NAME_LENGTH ) {
		strncpy(sTimerProcessName, name, TIMER_PROCESS_NAME_LENGTH);
		sTimerProcessName[TIMER_PROCESS_NAME_LENGTH] = '\0';
	} else strcpy(sTimerProcessName, name);
	dwTickSize = ticksize;
	if( dwTickSize > end_of_tick ) end_of_tick = dwTickSize;
	bIsDividable = dividable;
	dwExpectedTime = expecttime;

	DividableFuncToCall = NULL;
	NormalFuncToCall = func;

	MyLog(LOG_IGNORE, "%s, (tick)%d, (able)%d, (expect)%d are ADDED", name, ticksize, dividable, expecttime);
	return;
}	
	
bool CGameTimer::IsTicToRun(DWORD tick)
{
	if( !bIsDividable && !(tick % dwTickSize) ) return true;
	if( bIsDividable ) {
		if( wCurrentProcessPos ) return true;
		// (tick%dwTickSzie) 唱赣瘤啊 
		else if( (dwTickSize-(tick%dwTickSize)) == (DWORD)(dwTickSize - (wNumOfDividedBy/2)) ) return true;
	}
	return false;
}

WORD CGameTimer::Run()
{
	DWORD StartTime, FinishTime, TakeTime;
	static counter = 0;
	StartTime = timeGetTime();							// 咯扁辑何磐 家夸 矫埃 眉农

	// Run
	if( bIsDividable ) {
		DividableFuncToCall( wNumOfDividedBy, wCurrentProcessPos );
		if ( wNumOfDividedBy > 1 ) {
			wCurrentProcessPos++;

			// 促 沁栏搁 付公府.
			if( wCurrentProcessPos == wNumOfDividedBy )
				wCurrentProcessPos = 0;
		}
	} else {
		NormalFuncToCall();
	}

	FinishTime = timeGetTime();							// 家夸矫埃 眉农 场
	TakeTime = FinishTime - StartTime;					// 吧赴 矫埃阑 拌魂茄促.

	// 家夸矫埃 扁废 巢扁扁
	if( !bIsDividable || wNumOfDividedBy == 1 ) {
		dwTotalTakeTime = TakeTime;						// 盒且 阂啊瓷茄 窃荐捞芭唱 盒且啊瓷窍瘤父 盒且登瘤 臼篮 累诀捞搁 捞锅俊 吧赴 矫埃阑 dwTotalTakeTime俊 持绰促.
	} else {
		counter++;
		dwTotalTakeTime = dwTotalTakeTime + TakeTime;	// 盒且等 累诀篮 茄窜困狼 累诀捞 场朝锭鳖瘤 吧赴矫埃阑 歹茄促.
		if (wCurrentProcessPos == 0 ) {
			MyLog( LOG_IGNORE, "TotalTakeTime(%d/%d)", counter, dwTotalTakeTime );
			counter = 0;
		}
	}

	if( bIsDividable && !wCurrentProcessPos ) {			// 盒且啊瓷茄 沏记捞绊, 窜困累诀捞 柳青吝捞 酒聪搁 盒且/钦魔 咯何甫 魄窜茄促.
		if ( wNumOfDividedBy > 1 ) {					// 盒且等 累诀狼 版快 茄 累诀寸 家夸矫埃狼 乞闭阑 备茄促.
			dwTotalTakeTime = dwTotalTakeTime / wNumOfDividedBy;
		}
		DWORD dwTakeTimeAvr = dwTotalTakeTime;			// 乞闭蔼栏肺 荤侩.
		dwTotalTakeTime = 0;							// 怖 努府绢秦霖促.

		bool bIsExcess;
		DWORD dwDiffTime;

		if( dwTakeTimeAvr > dwExpectedTime ) {
			if( wNumOfDividedBy == dwTickSize )			// 捞固 弥措蔼 捞惑狼 矫埃捞 吧府绊 乐栏搁...
				return (WORD)TakeTime;
			bIsExcess = true;
			dwDiffTime = dwTakeTimeAvr - dwExpectedTime;
		} else if( dwTakeTimeAvr < dwExpectedTime ) {
			if( wNumOfDividedBy <= 1 )					// 1 捞窍搁 歹 唱喘荐 绝促.
				return (WORD)TakeTime;
			bIsExcess = false;
			dwDiffTime = dwExpectedTime - dwTakeTimeAvr;
		} else {
			return (WORD)TakeTime;
		}

		float factor = (float)dwDiffTime / (float)dwExpectedTime;

		if( (bIsExcess &&(factor > 0)) || (!bIsExcess && (factor > 0.25)) ) {
			if( bIsExcess )
				factor = (float)1.0 + factor;
			else
				factor = (float)1.0 - factor;
			
			wNumOfDividedBy = (int)((float)wNumOfDividedBy * factor);
			MyLog( LOG_NORMAL, "TakeTimeAvr(%d), ExpectedTime(%d), Factor(%f), Changed to(%d)", dwTakeTimeAvr, dwExpectedTime, factor, wNumOfDividedBy);
			if( wNumOfDividedBy > dwTickSize ) {	// wNumOfDividedBy绰 dwTickSize焊促 努 荐 绝促.
				MyLog( LOG_IMPORTANT, "GTE(%s) takes too long time but can't be devided more(%d:%d)", sTimerProcessName, wNumOfDividedBy, dwTickSize);
				wNumOfDividedBy = (WORD)dwTickSize;
			}
		}
	}
	return (WORD)TakeTime;
}


// 怖 角青秦拎具 茄促...
void SettingGameTimers( void )
{
	// 捞抚, 角青且 平 逞滚, 悼利累诀盒且啊瓷咯何, 扁措矫埃, 龋免且 窃荐.
	// 林狼! 0何磐 NUM_OF_GAME_TIMERS-1 鳖瘤 硅凯捞 厚绢辑绰 例措 救凳.
	// 撤篮 牢郸胶俊 眠啊且荐废 贸府 快急鉴困啊 臭促. 老馆利栏肺 龋免平捞 陋阑荐废 撤篮 牢郸胶俊 殿废窍绰巴捞 亮促.

// Commected by chan78 at 2000/11/29
//	GameTimerList[0].SetGameTimer("Run Them all", 2, false, 400, imsi );
// 1寸 150 ms
	GameTimerList[0].SetGameTimer("Very Short Terms",	1, false,	100, very_short_terms	);
	GameTimerList[1].SetGameTimer("Short Terms",		2, false,	300, short_terms		);
	GameTimerList[2].SetGameTimer("Normal Terms",		3, false,	450, normal_terms		);
	GameTimerList[3].SetGameTimer("Long Terms",			4, false,	600, long_terms			);
	GameTimerList[4].SetGameTimer("Very Long Terms",	7, false,	1000, very_long_terms	);
	GameTimerList[5].SetGameTimer("Quest30Second",		10, false, 	100, QuestSecond	);

	StartGameTimers();

	return;
}

void StartGameTimers( void )
{
	IsGameTimerWorking = true;
	return;
}

void StopGameTimers( void )
{
	IsGameTimerWorking = false;
	return;
}

// GTE_SIZE 俊 茄锅究 龋免登绰 func,
// timer.cpp 曼绊
void __stdcall GameTimerProcess(DWORD dwValue)//020511 lsw
{
	static DWORD tick = 1;
	static LastProcess = 0;
	WORD TakeTime;

	if( !IsGameTimerWorking ) return;

	for( int i = LastProcess; i < NUM_OF_GTES; i++ ) {

		if( !GameTimerList[i].IsTicToRun(tick) )
			continue;

		// 咯扁辑 吧赴 矫埃捞 弥措 倾侩 矫埃阑 哈绢唱搁 酒流 且 老捞 巢酒乐绢档 皋牢静饭靛肺 汗蓖秦霖促.
		// tic篮 刘啊矫虐瘤 臼绊 LastProcess绰 i+1狼 蔼阑 爱霸登骨肺 巢酒乐绰 累诀何磐 贸府窍霸 等促.
		if( (TakeTime = GameTimerList[i].Run()) > GameTimerList[i].GetExpectedTime() ) {
			// 矫埃 坷滚甫 肺弊窍绊酵栏搁...
			//MyLog( LOG_IGNORE, "GameTimerList[%d].Run() > dwExpectedTime(%d)", i, TakeTime );

			LastProcess = i+1;
			if( LastProcess == NUM_OF_GTES ) {		// 绢瞒乔 捞锅 累诀捞 付瘤阜牢 版快...
				LastProcess = 0;
			tick++;
			}
			return;
		}

		// LastProcess啊 0捞 酒囱版快绰 历锅 龋免锭 且老捞 巢酒 tic捞 刘啊窍瘤 臼篮巴捞骨肺 平阑 刘啊矫虐绊
		// 盔贰 捞锅畔俊 秦具且 老甸阑 贸府秦霖促. 啊瓷窍搁 平捞 代 剐府档废 窍扁 困茄 贸府促.
		if( i == (NUM_OF_GTES-1) ) {
			if( LastProcess != 0 ) {
				LastProcess = 0;
				tick++;
				i = 0;
			}
		}

	}

	// 秦具且 葛电 累诀阑 扁措茄 矫埃救俊 场陈促.
	LastProcess = 0;
	tick++;

	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -