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

📄 main.cpp

📁 MudMaster 2000 的C++源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		pBuf = strText.GetBuffer(strlen(pszText) + 10);
		nResult = sprintf(pBuf,"\n# %s\n",pszText);
		strText.ReleaseBuffer(nResult);
	}
	else
	{
		pBuf = strText.GetBuffer(strlen(pszText) + 10);
		nResult = sprintf(pBuf,"\n# %s",pszText);
		strText.ReleaseBuffer(nResult);
	}
	_terminal.Print(strText);
	_tiUserInput.SetFocus();
	if (bNoAction)
		CommandPrintOff();
}

void PrintError()
{
	CString strError;
	GetSocketError(strError);
	PrintMessage(CString("Error: ")+strError);
}

// Used only in the following 2 functions to save the text color
// and remember the action state.
WORD _wAttr = F_LIGHTGREY;
BOOL _bOldActionsState = TRUE;
void CommandPrintOn()
{
	if (!_config.bCommandPrintOn)
	{
		_config.bCommandPrintOn = TRUE;
		_bOldActionsState = _terminal.ActionsState();
		_terminal.ActionsOff();

		_wAttr = _terminal.GetColor();
		_terminal.SetColor(F_LIGHTGREY);
		_terminal.HideCursor();
	}
}

void CommandPrintOff()
{
	if (_config.bCommandPrintOn)
	{
		_config.bCommandPrintOn = FALSE;
		_terminal.SetColor(_wAttr);
		_terminal.ShowCursor();
		_tiUserInput.SetFocus();
		if (_bOldActionsState)
			_terminal.ActionsOn();
	}
}

BOOL IsWalkable(const char *pszText)
{
	static CString strWalkable("nsewud");
	BOOL bFoundAlpha = FALSE;
	while(*pszText)
	{
		// Not a speedwalk if there is a digit as the last
		// character.
		if (isdigit(*pszText) && *(pszText+1) == '\x0')
			return(FALSE);
		
		// Not a speedwalk if it isn't one of the movementcharacter
		// or not a digit.
		if (!isdigit(*pszText) && strWalkable.Find(*pszText) == -1)
			return(FALSE);

		// Keep track of if we found an alpha char.  If it is just
		// a string of numbers it isn't a speedwalk.
		if (isalpha(*pszText))
			bFoundAlpha = TRUE;

		*pszText++;
	}
	return(bFoundAlpha);
}

void DoSpeedWalk(const char *pszText)
{
	// Need to expand any numbers that exist in the speedwalk text.
	// 3nu2w would be expanded to: nnnuww
	CString strWalk;
	CString strNum;
	while(*pszText)
	{
		// If a digit, pull them off and save them until we hit
		// an alpha.
		if (isdigit(*pszText))
			strNum += *pszText;
		else
		{
			// If not building a number, just keep the character.
			if (strNum.IsEmpty())
				strWalk += *pszText;
			else
			{
				strWalk += CString(*pszText,atoi(strNum));
				strNum.Empty();
			}
		}

		pszText++;
	}

	int nWalkLen = strWalk.GetLength();
	int nIndex = 0;
	int nRecvLen;
	char szSend[2];
	szSend[1] = '\x0';
	clock_t nStart = clock();
	clock_t nGoal = (clock_t)((float)(_config.nPace / 1000.0) * CLOCKS_PER_SEC);
	clock_t nNow;
	while(!kbhit() && nIndex < nWalkLen)
	{
		nNow = clock();
		if (nNow - nStart >= nGoal)
		{
			szSend[0] = strWalk.GetAt(nIndex);
			if (_config.bEcho)
				_terminal.Print(szSend);
			SendText(szSend);
			_tiUserInput.SetFocus();
			nIndex++;
			nStart = clock();
		}

		if (_bConnected)
		{
			nRecvLen = recv(_hActiveSocket,_pWalkBuf,INBUF_SIZE,0);
			if (nRecvLen == SOCKET_ERROR)
			{
				nRecvLen = WSAGetLastError();
				if (nRecvLen != WSAEWOULDBLOCK)
				{
					PrintError();
					PrintMessage("Connection Lost.");
					_bConnected = FALSE;
					closesocket(_hActiveSocket);
					_notifyDisconnect.Notify();
					return;
				}
			}
			else
				if (nRecvLen > 0)
				{
					_pWalkBuf[nRecvLen] = '\x0';
					_terminal.Print(_pWalkBuf);
					_tiUserInput.SetFocus();
				}
				else
				{
					PrintMessage("Connection Lost.");
					_bConnected = FALSE;
					closesocket(_hActiveSocket);
					_notifyDisconnect.Notify();
					return;
				}
		}
	} 

	// Eat up any keyboard chars they hit to exit speedwalking.
	while(kbhit())
		getch();
}

BOOL IsPartial(const char *pszLookFor, const char *pszText)
{
	while(*pszLookFor && *pszText)
	{
		if (*pszLookFor != *pszText)
			return(FALSE);
		pszLookFor++;
		pszText++;
	}
	// if the loop terminated because pszText reached the end
	// before pszLookFor, then they cannot be matches.
	return(*pszLookFor == '\x0');
}

void GetIPAddress()
{
	char szHostName[40];
	if (!gethostname(szHostName,39))
		if (lstrcmp(szHostName,""))
		{
			_config.strHostName = szHostName;
			HOSTENT FAR *lpHostEnt = gethostbyname(szHostName);
			_config.strIPAddress = inet_ntoa(*(in_addr*)(*lpHostEnt->h_addr_list));
		}
}

void CloseListenSocket()
{
	SOCKET hIncoming;
	SOCKADDR_IN sockAddr;
	int nAddressLen;

	if (_hListenSocket == INVALID_SOCKET)
		return;
	nAddressLen = sizeof(sockAddr);
	while ((hIncoming = accept(_hListenSocket,(SOCKADDR*)&sockAddr,&nAddressLen)) != INVALID_SOCKET)
		closesocket(hIncoming);
	if (closesocket(_hListenSocket) == SOCKET_ERROR)
	{
		CString strError;
		if (ErrorToString(WSAGetLastError(),strError))
			PrintMessage(CString("Failed closing listen socket: ")+strError);
		else
			PrintMessage("Failed closing listen socket.");
	}
}

void InitListenSocket()
{
	_hListenSocket = socket(PF_INET,SOCK_STREAM,0);
	if (_hListenSocket == INVALID_SOCKET)
	{
		PrintMessage("Error creating chat listen socket.");
		PrintError();
		return;
	}

	// Need to bind the address and port number to the socket.
	SOCKADDR_IN sockAddr;
	memset(&sockAddr,0,sizeof(sockAddr));

	char szAddress[50];
	strcpy(szAddress,_config.strIPAddress);
	sockAddr.sin_family = AF_INET;
	LPSTR lpszAscii = szAddress;
	DWORD lResult = inet_addr(lpszAscii);
	if (lResult == INADDR_NONE)
	{
		PrintMessage("Failed setting chat listen socket address.");
		closesocket(_hListenSocket);
		_hListenSocket = INVALID_SOCKET;
		return;
	}
	sockAddr.sin_addr.s_addr = lResult;
	sockAddr.sin_port = htons((u_short)_nListenPort);
	if (bind(_hListenSocket,(SOCKADDR*)&sockAddr,sizeof(sockAddr)) == SOCKET_ERROR)
	{
		PrintMessage("Failed binding chat listen socket.");
		PrintError();
		closesocket(_hListenSocket);
		_hListenSocket = INVALID_SOCKET;
		return;
	}

	// Make sure it is a non-blocking socket.
	unsigned long lNonBlocking = 1;
	ioctlsocket(_hListenSocket,FIONBIO,&lNonBlocking);
	
	if (listen(_hListenSocket,5) == SOCKET_ERROR)
	{
		PrintMessage("Socket function listen failed.");
		PrintError();
		closesocket(_hListenSocket);
		_hListenSocket = INVALID_SOCKET;
		return;
	}

	_bChatListenEnabled = TRUE;
}

// This function will be called a lot of times.  Taking as much
// off the stack as I can.
SOCKET _hIncoming;
SOCKADDR_IN _sockAddr;
int _nAddressLen;
void CheckForChatConnections()
{
	if (_hListenSocket == INVALID_SOCKET)
		return;
	_nAddressLen = sizeof(_sockAddr);
	_hIncoming = accept(_hListenSocket,(SOCKADDR*)&_sockAddr,&_nAddressLen);
	if (_hIncoming != INVALID_SOCKET)
	{
		if (_chat.Accept(_hIncoming,_sockAddr))
			PrintMessage("Chat established.");
		else
			PrintMessage("Chat failed.");
	} 
}

CHAT *_pChat;
int _nRecvLen;
void CheckForChatText()
{
	_pChat = _chat.GetFirst();
	while(_pChat != NULL)
	{
		_nRecvLen = recv(_pChat->hSocket,_pChatBuf,INBUF_SIZE,0);
		if (_nRecvLen == SOCKET_ERROR)
		{
			_nRecvLen = WSAGetLastError();
			if (_nRecvLen != WSAEWOULDBLOCK)
			{
				CString strText;

				// Keep track of the name and socket for the notification.  Need to
				// make sure the notification comes after the chat object has been
				// removed from the list or the DLL might invoke something that 
				// tries to use it.
				CString strName(_pChat->strName);
				SOCKET hSocket = _pChat->hSocket;

				strText.Format("Chat connection with %s lost.",
					(const char *)_pChat->strName);
				PrintMessage(strText);
				_chat.Remove(_pChat->strName);
				_notifyChatDisconnect.Notify(hSocket,strName);
				return;
			}
		}
		else
			if (_nRecvLen > 0)
				_chat.DoCommands(_pChat,_pChatBuf,_nRecvLen);
			else
			{
				CString strText;

				// Keep track of the name and socket for the notification.  Need to
				// make sure the notification comes after the chat object has been
				// removed from the list or the DLL might invoke something that 
				// tries to use it.
				CString strName(_pChat->strName);
				SOCKET hSocket = _pChat->hSocket;

				strText.Format("Chat connection with %s lost.",
					(const char *)_pChat->strName);
				PrintMessage(strText);
				_chat.Remove(_pChat->strName);
				_notifyChatDisconnect.Notify(hSocket,strName);
				return;
			}

		_pChat = _chat.GetNext();
	}
}

void ParseCommandLine(int nArgc, char *pszArgv[])
{
	CString strCommand;
	for (int i=1;i<nArgc;i++)
	{
		strCommand = pszArgv[i];
		strCommand.MakeLower();

		if (strCommand == "modem")
		{
			_bUsingModem = TRUE;
			continue;
		}

		if (strCommand.Left(2) == "l:")
		{
			_nListenPort = atoi(strCommand.Right(strCommand.GetLength()-2));
			continue;
		}

		if (strCommand == "bootlog")
		{
			_bBootLog = TRUE;
			continue;
		}
	}
}

void WriteIniString(const char *pszSection, const char *pszEntry, const char *pszValue)
{
	WritePrivateProfileString(pszSection,pszEntry,pszValue,_strIniName);
}

void WriteIniInt(const char *pszSection, const char *pszEntry, int nValue)
{
	CString strValue;
	strValue.Format("%d",nValue);
	WriteIniString(pszSection,pszEntry,strValue);
}

// Global for ini buf so I don't have to keep allocating and freeing
// up memory.
char _szIniBuf[501];
CString GetIniString(const char *pszSection, const char *pszEntry, const char *pszDefault)
{
	GetPrivateProfileString(pszSection,pszEntry,pszDefault,_szIniBuf,500,_strIniName);
	return(_szIniBuf);
}

int GetIniInt(const char *pszSection, const char *pszEntry, int nDefault)
{
	CString strDefault;
	strDefault.Format("%d",nDefault);
	return(atoi(GetIniString(pszSection,pszEntry,strDefault)));
}

void GetOptions()
{
	CString strSection;
	int nFore;
	int nBack;

	_config.nVersionMajor	= 2;
	_config.nVersionMinor	= 6;
	_config.nVersionBuild	= 0;
	_config.nVersionBeta		= 0;

	_config.strVersion.Format("%d.%d.%d",
		_config.nVersionMajor,
		_config.nVersionMinor,
		_config.nVersionBuild);
	if (_config.nVersionBeta)
	{
		strSection.Format(" (Beta %d)",
			_config.nVersionBeta);
		_config.strVersion += strSection;
	}

	_config.chCommand			= '/';
	_config.chSeparator		= ';';
	_config.chEscape			= '\\';
	_config.chBlockStart		= '{';
	_config.chBlockEnd		= '}';

	_config.bIgnoreAliases	= FALSE;

	// These options should always load with the following defaults.
	// If not it could seriously confuse users.
	_config.bDoEvents			= TRUE;
	_config.bCommandPrintOn = FALSE;
	_config.bDoNotDisturb	= FALSE;

	_config.nHistory			= GetIniInt("History Buffer","Lines",50);
	_config.nScrollBack		= GetIniInt("Scrollback Buffer","Lines",600);

	strSection = "Messages";
	_config.bVarMessages		= GetIniInt(strSection,"Variables",FALSE);
	_config.bActionMessages	= GetIniInt(strSection,"Actions",TRUE);
	_config.bAliasMessages	= GetIniInt(strSection,"Aliases",TRUE);
	_config.bTabMessages		= GetIniInt(strSection,"Tabs",TRUE);
	_config.bMacroMessages	= GetIniInt(strSection,"Macros",TRUE);
	_config.bListMessages	= GetIniInt(strSection,"Lists",FALSE);
	_config.bItemMessages	= GetIniInt(strSection,"List Items",FALSE);
	_config.bEventMessages	= GetIniInt(strSection,"Events",FALSE);
	_config.bBarMessages		= GetIniInt(strSection,"Bar Items",TRUE);
	_config.bArrayMessages	= GetIniInt(strSection,"Arrays",FALSE);
	_config.bGagMessages		= GetIniInt(strSection,"Gags",TRUE);
	_config.bHighMessages	= GetIniInt(strSection,"Highlights",TRUE);
	_config.bSubMessages		= GetIniInt(strSection,"Subs",TRUE);
	_config.bEnableMessages = GetIniInt(strSection,"EnableGroup",TRUE);

	strSection = "Edit Bar";
	nFore = GetIniInt(strSection,"Fore Color",15);
	nBack = GetIniInt(strSection,"Back Color",1);
	_colors.wInput = _tiUserInput.BackColorAttribute(nBack) | _tiUserInput.ForeColorAttribute(nFore);

	strSection = "Status Bar";
	_config.bStatusBar		= GetIniInt(strSection,"Visible",FALSE);
	_config.nStatusBarPos	= GetIniInt(strSection,"Position",STATUS_BAR_BELOW);
	_statusBar.SetBarFore(GetIniInt(strSection,"Fore Color",14));
	_statusBar.SetBarBack(GetIniInt(strSection,"Back Color",5));

	strSection = "Miscellaneous";
	_config.bPause				= GetIniInt(strSection,"Screen Pause",TRUE);
	_config.bEcho				= GetIniInt(strSection,"Echo",(!_bUsingModem ? TRUE : FALSE));
	_config.bShowInfo			= GetIniInt(strSection,"Show Info",FALSE);
	_config.strDefaultFile	= GetIniString(strSection,"Default File","");
	_config.bMultipleActions= GetIniInt(strSection,"Multiple Actions",TRUE);
	_config.nUndo				= GetIniInt(strSection,"Undo",100);
	_config.bPreSub			= GetIniInt(strSection,"PreSub",FALSE);
	_config.bAltGr				= GetIniInt(strSection,"AltGr",FALSE);
	_config.nDebugDepth		= GetIniInt(strSection,"Debug Depth",10);
	_config.bDebugMessages	= GetIniInt(strSection,"Debug Messages",TRUE);
	_config.bShowMismatches = GetIniInt(strS

⌨️ 快捷键说明

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