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

📄 baseagent.cpp

📁 足球机器人仿真组SimuroSot11vs11的源程序。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "OS.h"
#include "stdafx.h"
#include "Agentsock.h"
#include <iostream.h>
#include "MicroClientDoc.h"
#include "TParseString.h"
#include "StrategySystem.h"
#include "BaseAgent.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern OurRobot r[2][11];
extern Opponent op;//存放对方队员的坐标数据
extern Ball theball;//存放球的坐标数据.
extern CStrategySystem* thePlannerR;
extern CStrategySystem* thePlannerL;

#define MAX_MSG_LENGTH 1500
CBaseAgent::CBaseAgent()
{
	m_iPositionX = 0;
	m_iPositionY = -30;
	m_iDirection = 0;
	m_bConnected = FALSE;
	m_iSide = LEFT_TO_RIGHT;
	m_iNumber = 0;
}

CBaseAgent::CBaseAgent(CMicroClientDoc* m_Doc)
{
	m_pDoc=m_Doc;
	m_iPositionX = 0;
	m_iPositionY = -30;
	m_iDirection = 0;
	m_bConnected = FALSE;
	m_iSide = LEFT_TO_RIGHT;
	m_iNumber = 0;
}
CBaseAgent::~CBaseAgent()
{
	DisconnectAgent();
}

BOOL CBaseAgent::ConnectAgent(LPCTSTR lpszTeam, LPCTSTR lpszServer, UINT nPort)
{
	// is the agent already connected (should not happen) 
	if (m_bConnected)
		return FALSE;

	// create the datagram socket
	if (!Create(nPort, lpszServer))
	{
		// socket could not be created
		OnErrorCreatingSocket();
		return FALSE;
	}
	else
	{
		m_bConnected = TRUE;
		m_strTeam = lpszTeam;
		OnCreatedSocket();
		Sleep(100);
		return TRUE;
	}
}
void CBaseAgent::OnCreatedSocket()
{
	CString	message;

	// send the (init team) command
	message.Format("(init %s (version 4.00))", (LPCTSTR) m_strTeam);
	SendMsg(message);
}

void CBaseAgent::DisconnectAgent()
{
	// is the socket active ?
	if (m_bConnected)
	{
		BYTE Buffer[50];

		ShutDown();
		while(Receive(Buffer,50) > 0);
		Close();
	}
	m_bConnected = FALSE;
}

void CBaseAgent::SendMsg(CString& strText)
{
	int		iLength;
	
	// send the buffer
	iLength = SendTo(strText, strText.GetLength(), GetPort(), GetAddress(), 0);
	if (iLength == strText.GetLength())
		OnSentMessage((LPCTSTR) strText);
	else
	{
		// an error occured
		#ifdef _DEBUG
			afxDump << "error writing to socket\n";
		#endif
		OnErrorSendingMessage();
	}
}

void CBaseAgent::OnReceive(int iError)
{
	char		buffer[MAX_MSG_LENGTH];
	int		iLength = 0;
	UINT		iPort = GetPort();
	int		iPosition = 0;

	// first call base classes method
	CAgentSocket::OnReceive(iError);

	// read a message from the socket
	iLength = ReceiveFrom(buffer, MAX_MSG_LENGTH, GetAddress(), iPort, 0);
	// the port may have changed
	SetPort(iPort);

	if (iLength > 0)
	{
		//OnReceivedMessage((LPCTSTR) buffer);
		ParseMessage((LPCTSTR) buffer);
		writetofile();
		if(m_iSide==LEFT_TO_RIGHT)
		{
			thePlannerL->ReceiveData(theball,r[0][0],r[0][1],r[0][2],r[0][3],r[0][4],
				r[0][5],r[0][6],r[0][7],r[0][8],r[0][9],r[0][10],op);
			thePlannerL->Action();
		}
		else if(m_iSide==RIGHT_TO_LEFT)
		{
			thePlannerR->ReceiveData(theball,r[0][0],r[0][1],r[0][2],r[0][3],r[0][4],
				r[0][5],r[0][6],r[0][7],r[0][8],r[0][9],r[0][10],op);
			thePlannerR->Action();

		}
		//下面取的每个机器人的轮速,发送给服务器。
		SendCommand();
	}
	else
	{
		// an error occured
		#ifdef _DEBUG
			afxDump << "error reading socket\n";
		#endif
		OnErrorReceivingMessage();
	}
}

void CBaseAgent::Move(int x, int y)
{
	CString	message;

	message.Format("(move %d %d)", x, y);
	SendMsg(message);

	m_iPositionX = x;
	m_iPositionY = y;
}

void CBaseAgent::Turn(int angle)
{
	CString	message;

	// angle may be -180 to 180
	message.Format("(turn %d)", angle);
	SendMsg(message);

	m_iDirection += angle;
	if (m_iDirection > 180)
		m_iDirection -= 360;
}

void CBaseAgent::Dash(int power)
{
	CString	message;

	// power may be -30 to 100
	message.Format("(dash %d)", power);
	SendMsg(message);
}

void CBaseAgent::Kick(int power, int direction)
{
	CString	message;
	
	// power -30 to 100
	// direction -180 to 180
	message.Format("(kick %d %d)", power, direction);
	SendMsg(message);
}

void CBaseAgent::Catch(int direction)
{
	CString	message;
	
	// direction -180 to 180
	message.Format("(catch %d)", direction);
	SendMsg(message);
}

void CBaseAgent::Say(CString msg)
{
	CString	message;

	// max 255 byte
	message.Format("(say ");
	message += msg;
	message += _T(")");
	SendMsg(message);
}

void CBaseAgent::ChangeView(int width, int quality)
{
	CString	message;

	message =_T("(change_view ");
	// set the view angle string
	if (width == 180)
		message += _T("wide ");
	else if (width == 90)
		message += _T("normal ");
	else
		message += _T("narrow ");
	// set the view quality string
	if (quality == 0)
		message += _T("low)");
	else
		message += _T("high)");

	SendMsg(message);
	OnChangedView(width, quality);
}

void CBaseAgent::SenseBody()
{
	CString	msg("(sense_body)");

	SendMsg(msg);
}


void CBaseAgent::MoveRel(int amount)
{
	int	x;
	int	y;

	x = m_iPositionX + (int) cos(m_iDirection * PI / 180);
	y = m_iPositionY + (int) sin(m_iDirection * PI / 180);
	// and move to the calculated position
	Move (x, y);		
}

BOOL CBaseAgent::Connect()
{
/*	DSetupDlg Dialog;

	// ask the user for the name of the team and the server
	Dialog.m_TeamName = _T("Team1");
	Dialog.m_ServerName = _T("ultra2");
	Dialog.m_Channel = 6000;

	// user can repeat with a different address
	while(TRUE)
	{
		// try to connect
		if (ConnectAgent(Dialog.m_TeamName, Dialog.m_ServerName, Dialog.m_Channel))
			return TRUE;
		
		if (Dialog.DoModal() != IDOK)
			return FALSE;
	}*/
//	AfxMessageBox("hello")
	return 0;
}

void CBaseAgent::ParseMessage(LPCTSTR str)
{
	switch(str[1])
	{
		case 'h':
			// something to hear
			//return 
//			ParseHear(str);
			break;
		
		case 's':
			// something to see or sense body
			if (str[3] == 'e')
				// a see message
				//return 
			ParseSee(str);
			break;
//			else
				// a sense body information
//				return ParseSenseBody(str);

		case 'i':
			// message at init
			//return 
			AfxMessageBox("Init successful");
			ParseInit(str);
			break;

		case 'r':
			// message at reconnect
			//return 
//			ParseInit(str);
			break;
		//default:
			//ParseSee(str);
	}
	//return TRUE;
}

void CBaseAgent::ParseInit(LPCTSTR str)
{

	TParseString	msg(str);
	CString			tmp;
//	int				number;
	int				mode;
	char*				strPlayMode[] = PLAYMODE_STRINGS;

	// get the side
	if ((msg.GetLength() > 6) && (msg.GetAt(6) == 'r'))
	{
		//thePlannerR=new CStrategySystem(0);
		AfxMessageBox("right right");
		SetSide(RIGHT_TO_LEFT);
	}
	else
	{
		//thePlannerR=new CStrategySystem(1);
		SetSide(LEFT_TO_RIGHT);//m_iSide=LEFT_TO_RIGHT;
	}

	// get the number of the player
	msg.SetPosition(8);
/*	number = msg.ParseInteger();
	if (msg.HasErrorOccured())
	{
		AfxMessageBox("Init error");
		return ;

	}*/
//	m_pAgent->SetNumber(number);

	// get the playmode
	msg.ParseString(tmp);
	mode = 0;
	while ((mode < NO_OF_PLAYMODES) && (tmp.Compare(strPlayMode[mode])))
		mode++;

	if (mode < NO_OF_PLAYMODES)
		m_iPlayMode = mode;

	//return TRUE;
}

void CBaseAgent::ParseSee(LPCTSTR str)
{
	TParseString	msg(str);
	TParseString	strObject;
	
	// get the time
	msg.SetPosition(5);
	m_iTime = msg.ParseInteger();
	if (msg.HasErrorOccured())
		return;
	
//	fprintf(fp,"%s length=%d\n",str,msg.GetLength());
	// get the visible objects
	while (msg.ParseBracket(strObject))
	{
		if (!ParseObject(strObject))
		{
			#ifdef _DEBUG
				CString error;
				error.Format(" : error parsing object info at position %d !\n", strObject.GetPosition());
				afxDump << error << strObject;
			#endif
		}
	}
}

BOOL CBaseAgent::ParseObject(LPCTSTR str)
{
	TParseString	msg(str);
	TParseString	name;
	CString		teamname;
	int team;
	int iNum;
	int x;
	int y;
	double angle;

	// read the objects name bracket
	if (!msg.ParseBracket(name))//name="player TeamName TeamNum"
		return FALSE;
	//获得这个队员的x,y坐标。
	x=msg.ParseInteger();
	if (msg.HasErrorOccured())

⌨️ 快捷键说明

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