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

📄 vcdll.cpp

📁 一个C编写的足球机器人比赛程序
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////////
// DUT 5vs5 RoboCup Team DLL FrameWork 2
// Specified Edition for M$ Visual C++
//
// Please don't modify any definition given by the DUT RoboCup Competition Committee.
// This is not compatible with the server of earlier version.
//
// Visit IUSI website(http://tianwei.dlut.edu.cn) to gain more details.
// Technology support E-Mail: brton.rei@gmail.com,guangbincui@gmail.com
//////////////////////////////////////////////////////////////////////////

// 注意:函数体的内容可以根据自己的需要改变
// 例如你在DLL中要使用OOP技术,你完全可以重写初始化和存储部分
// 但是请务必保持所有给定接口(函数)的声明部分

// 另:如果在VC++中出现如下编译问题 
// unexpected end of file while looking for precompiled header directive
// 请在Project->Settings->C/C++->Category->Precompiled Headers页
// 选择Not using precompiled headers

#include <windows.h>
#include "CppConst.h"
#include "vcdll.h"
#include "Geometry.h"
#include "Worldmodel.h"

// 以下两项名称中请勿使用圆括号"("和")"

// 球队的名称,请勿超过10个汉字(20个ASCII字符),否则多余部分会被忽略
#define  CTEAMNAME 	         "球队名字";  
// 作者的姓名,请勿超过7个汉字(14个ASCII字符),否则多余部分会被忽略
#define  CTEAMAUTHORS        "作者名字";  

MatchState		matchState;
RobotState		teamAState[5];
OpsState		teamBState[5];
BallState		ballState;
RobotState		*pMyTeam;
OpsState		*pOpsTeam;
int 			myTeamNo;
bool			bTransform;
int				iTime = 0;
WorldModel		WM;
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}


//////////////////////////////////////////////////////////////////////////
// 初始化球队信息
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) void _stdcall initdll(int initTeamNo)
{
	myTeamNo = initTeamNo;
    pMyTeam = teamAState;
	pOpsTeam = teamBState;
	if(1 == initTeamNo) 
    {
		bTransform = false;
	} 
    else 
    {
		bTransform = true;
	}
	iTime = 0;
}

//////////////////////////////////////////////////////////////////////////
// 比赛信息
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) void _stdcall tellmatchstate(MatchState ams,
                                                     RobotState atmAs[5], 
													 OpsState atmBs[5], 
                                                     BallState abs)
{
	memcpy(&matchState, &ams, sizeof(MatchState));
	memcpy(teamAState, atmAs, sizeof(RobotState)*5);
	memcpy(teamBState, atmBs, sizeof(OpsState)*5);
	memcpy(&ballState, &abs, sizeof(BallState));
	
	WM.ball.setPosition(VecPosition(ballState.pos.x,ballState.pos.y));
	WM.ball.setVelocity(VecPosition(ballState.vel.x,ballState.vel.y));

	for(int i = 0 ; i < 5 ; i ++)
	{
		WM.ownRobots[i].setPosition(VecPosition(pMyTeam[i].pos.x,pMyTeam[i].pos.y));
		WM.ownRobots[i].setVelocity(VecPosition(pMyTeam[i].vel.x,pMyTeam[i].vel.y));
		WM.ownRobots[i].SetDirection(pMyTeam[i].dir);
		WM.ownRobots[i].SetStamina(Stamina(pMyTeam[i].sta,pMyTeam[i].eff,pMyTeam[i].rec));
	}
	/*
	if(bTransform)
	{
		for(int i = 0 ; i < 5 ; i ++)
		{
			teamAState[i].dir  += 180.0;
			while( teamAState[i].dir > 180.0  ) teamAState[i].dir -= 360.0;
			while( teamAState[i].dir < -180.0 ) teamAState[i].dir += 360.0;
			teamAState[i].pos.x = CFLDLEN   - teamAState[i].pos.x;
			teamAState[i].pos.y = CFLDWIDTH - teamAState[i].pos.y;
			teamAState[i].vel.x = - teamAState[i].vel.x;
			teamAState[i].vel.y = - teamAState[i].vel.y;
			teamBState[i].dir  += 180.0;
			while( teamBState[i].dir > 180.0  ) teamBState[i].dir -= 360.0;
			while( teamBState[i].dir < -180.0 ) teamBState[i].dir += 360.0;
			teamBState[i].pos.x = CFLDLEN   - teamBState[i].pos.x;
			teamBState[i].pos.y = CFLDWIDTH - teamBState[i].pos.y;
			teamBState[i].vel.x = - teamBState[i].vel.x;
			teamBState[i].vel.y = - teamBState[i].vel.y;
		}		
	}*/
}

//////////////////////////////////////////////////////////////////////////
// 取得球队的名称
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) char* _stdcall teamname()
{
	return CTEAMNAME;
}

//////////////////////////////////////////////////////////////////////////
// 取得球队的作者名
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) char* _stdcall teamauthor()
{
	return CTEAMAUTHORS;
}

//////////////////////////////////////////////////////////////////////////
// 设定球员的动作
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) void _stdcall setaction(TCommand * pMyCMD)
{
	
	iTime++;	
	if((iTime%8) != 0 || true)
	{
			if(iTime%5 == 0)
			{
				for(int i=1; i< 5; i++)
				{
					pMyCMD[i].CommandType = Com_Turn;
					pMyCMD[i].ComParam0   = cos(iTime) * 180;
				}
			}
			else
			{
				for(int i=1; i< 5; i++)
				{
					pMyCMD[i].CommandType = Com_Dash;	
					pMyCMD[i].ComParam0   = cos(iTime) * 20;
				}
			}
	}
	else
	{
			for(int i=1; i< 5; i++)
			{
				pMyCMD[i].CommandType = Com_Kick;
				pMyCMD[i].ComParam0   = 0;
				pMyCMD[i].ComParam1   = 100;
			}
	}
	double tempdir =fabs(atan((ballState.pos.y - pMyTeam[4].pos.y)/(ballState.pos.x - pMyTeam[4].pos.x))-pMyTeam[4].dir*3.1415927/180.0);


	if(getDistBetween(ballState.pos,pMyTeam[0].pos) < CCATCHABLEMARGIN)
	{
		pMyCMD[0].CommandType = Com_Catch;
	}
	else
	{
		pMyCMD[0].CommandType = Com_Stay;
	}
	
	int    tempTime = 100;
	VecPosition tempPos = WM.ball.getPosition();
	ObjectT tempObject = WM.getFastestInSetTo(OBJECT_SET_TEAMMATES_NO_GOALIE,&tempPos,WM.ball.getVelocity(),CBALLDECAY,&tempTime);
	if(tempObject != O_Unknow)
	{
		AngDeg tempDir = WM.ownRobots[tempObject-1].getDirection();
		pMyCMD[tempObject-1] = WM.predictCommandToMoveToPos(tempObject,tempPos,100,&WM.ownRobots[tempObject-1].getPosition(),
			&WM.ownRobots[tempObject-1].getVelocity(),&tempDir);
	}
	for (int i = 1 ; i < 5 ; i++ )
	{
		if(getDistBetween(ballState.pos,pMyTeam[i].pos) < 1.0)
		{
			pMyCMD[i].CommandType = Com_Kick;
			pMyCMD[i].ComParam0   = cos(iTime) * 90;
			pMyCMD[i].ComParam1   = 100;
		}
		
	}
	/*
    (*pMyCMD)[0].ifResetMovement=1;
    (*pMyCMD)[0].targetV=CMAXV;
    (*pMyCMD)[0].targetX=pOpsTeam[3].x;
    (*pMyCMD)[0].targetY=pOpsTeam[3].y;
	*/
}

//////////////////////////////////////////////////////////////////////////
// 退出动态链接库,回收资源
//////////////////////////////////////////////////////////////////////////
__declspec( dllexport ) void _stdcall exitdll()
{
    // 如果有动态分配的内存,请在此释放
}


double getDistBetween(const Vector & pos1, const Vector & pos2)
{
    double tmp1 = (pos1.x - pos2.x);
    double tmp2 = (pos1.y - pos2.y);
    return sqrt(tmp1*tmp1 + tmp2*tmp2);
}



















⌨️ 快捷键说明

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