📄 strategy.cpp
字号:
// Strategy.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <malloc.h>
#include "Strategy.h"
#include "Team.h"
#include <math.h>
#include "Computation.h"
#include "BasicAction.h"
#include "PenaltyAction.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
FILE *g_pLogFile;
extern "C" STRATEGY_API void Create ( Environment *env )
{
// allocate user data and assign to env->userData
// eg. env->userData = ( void * ) new MyVariables ();
g_pLogFile=fopen("c:\\yellow.log","w+b");
env->userData = (UserDefStruct *) new UserDefStruct;
// UserData = (UserDefStruct *) new UserDefStruct;
}
extern "C" STRATEGY_API void Destroy ( Environment *env )
{
// free any user data created in Create ( Environment * )
// eg. if ( env->userData != NULL ) delete ( MyVariables * ) env->userData;
fclose(g_pLogFile);
if(env->userData != NULL)
{
delete (UserDefStruct *) env->userData;
}
}
void StoreEnvToFile(Environment *env )
{
fwrite(env,1,sizeof(Environment),g_pLogFile);
}
extern "C" STRATEGY_API void Strategy (Environment *env)
{
static int closest;
static int actualstate;
static int AssignCounter = 999;
Team team(env);
CBasicAction BA(env);
CPenaltyAction PA(env);
//Start Of Program
zoning(env);
transferEnvironmentDataToUserData(env);
PredictBallOld(env,&env->home[4]);
static int sp_switchBackNormal = 0;
static int sp_switchBackNormal1 = 0;
StoreEnvToFile(env);
if (AssignCounter > 10)
{
team.assignRole(env);
AssignCounter = 0;
}
AssignCounter++;
switch (env->gameState)
{
case FREE_BALL://无人碰球
team.normalGame(env);
break;
case PLACE_KICK://定位球
team.normalGame(env);
break;
case PENALTY_KICK://点球
switch (env->whosBall)
{
case ANYONES_BALL:
team.normalGame(env);
break;
case BLUE_BALL:
team.normalGame(env);
break;
case YELLOW_BALL:
static int sp_switchBackNormal = 0;
if(sp_switchBackNormal<30)
{
PA.penaltyKick(env->userData->attacker);
sp_switchBackNormal++;
}
else
team.normalGame(env);
break;
}
break;
case FREE_KICK://任意球
switch (env->whosBall)
{
case ANYONES_BALL:
team.normalGame(env);
break;
case BLUE_BALL:
if(sp_switchBackNormal1<50)
{
team.freeKickMode(env);
sp_switchBackNormal1++;
}
else
team.normalGame(env);
break;
case YELLOW_BALL:
team.normalGame(env);
break;
}
break;
case GOAL_KICK://球门球
switch (env->whosBall)
{
case ANYONES_BALL:
team.normalGame(env);
break;
case BLUE_BALL:
team.normalGame(env);
break;
case YELLOW_BALL:
team.normalGame(env);
break;
}
break;
default:
sp_switchBackNormal = 0;
sp_switchBackNormal1 = 0;
// env->home[0].velocityLeft=125;
// env->home[0].velocityRight=-50;
// env->home[1].velocityLeft=50;
// env->home[1].velocityRight=125;
team.normalGame(env);
break;
}
}
/*void CTest::Spin()
{
CBasicAction BA(env, UserData);
Vector3D test;
test.x = env->home[0].pos.x;
if(env->home[0].pos.y > 57)
{
test.y = 0;
BA.Position(test,0);
}
if(env->home[0].pos.y < 25)
{
test.y = 100;
BA.Position(test,0);
}
return;
}
bool CTest::Push(int WhichRobot)
{
for(int i=0;i<5;i++)
{
/*
FILE *pFile;
pFile = fopen("C:\\STRATEGY\\Opponent Data For Yellow.txt","a+");
fprintf(pFile,"Robot[4] to oppt[%d] = %f\n",i, UserData->HomeRobotInfo[0].toOppo[i].hypo);
fclose(pFile);
*/
/* if (UserData->HomeRobotInfo[WhichRobot].toOppo[i].hypo<=ROBOTRADIUS*0.8)
{
//if (Direction(&env->home[WhichRobot]))
{
return true;
}
}
}
return false;
}
int CTest::Direction(Robot *robot)
{
static Vector3D oldPos;
static counter;
double dx, dy;
int quad;
if (counter==0) oldPos = robot->pos;
counter++;
if (counter==5) counter = 0;
dx = robot->pos.x - oldPos.x;
dy = robot->pos.y - oldPos.y;
if(dx>0 && dy>0) quad = 1;
if(dx<0 && dy>0) quad = 2;
if(dx<0 && dy<0) quad = 3;
if(dx>0 && dy<0) quad = 4;
/*
FILE *pFile;
pFile = fopen("C:\\STRATEGY\\Opponent Data For Yellow.txt","a+");
fprintf(pFile,"old = %f\n",oldPos.x);
fprintf(pFile,"now = %f\n",robot->pos.x);
fprintf(pFile,"dx = %f\n",dx);
fprintf(pFile,"dy = %f\n",dy);
fprintf(pFile,"quad = %d\n",quad);
fclose(pFile);
*/
/* return quad;
}
double CTest::Predict_Ball(int WhichRobot)
{
static Vector3D oldPos;
static counter;
double dx, dy;
static double distance;
double oldDistance, velocity;
if (counter==0) oldPos = env->home[WhichRobot].pos;
counter++;
if (counter==5) counter = 0;
dx = env->home[WhichRobot].pos.x - oldPos.x;
dy = env->home[WhichRobot].pos.y - oldPos.y;
oldDistance = distance;
distance = sqrt((dy * dy) + (dx * dx));
velocity = distance - oldDistance;
return velocity;
}
double CTest::Gradient(Vector3D p1, Vector3D p2)
{
double dx, dy, g;
dx = p1.x - p2.x;
dy = p1.y - p2.y;
g = dy/dx;
return g;
}
void CTest::Trap_Ball(int WhichRobot, int WhichRobot2)
{
CBasicAction BA(env, UserData);
static Vector3D oldBallPos;
Vector3D block_point, offset, offset2;
static counter;
double dx, dy;
//if(UserData->HomeRobotInfo[WhichRobot].toBall.hypo>ROBOTRADIUS)
{
if (counter==0) oldBallPos = env->currentBall.pos;
counter++;
if (counter==10) counter = 0;
dx = env->currentBall.pos.x - oldBallPos.x;
dy = env->currentBall.pos.y - oldBallPos.y;
block_point.x = 40;//env->currentBall.pos.x + dx*2;
block_point.y = 40;//env->currentBall.pos.x + dy*2;
offset.x = block_point.x - ROBOTRADIUS/2;
offset.y = block_point.y - ROBOTRADIUS/2;
offset2.x = block_point.x + ROBOTRADIUS/2;
offset2.y = block_point.y + ROBOTRADIUS/2;
BA.Position(offset, WhichRobot);
BA.Position(offset2, WhichRobot2);
}
//else
{
//BA.Brake(WhichRobot);
//BA.Brake(WhichRobot2);
}
/* FILE *pFile;
pFile = fopen("C:\\STRATEGY\\Opponent Data For Yellow.txt","a+");
fprintf(pFile,"x = %f, y = %f\n", block_point.x, block_point.y);
fclose(pFile);*/
/* return;
}
void CTest::Test(int WhichRobot)
{
CBasicAction BA(env, UserData);
//BA.Position(env->currentBall.pos, WhichRobot);
if (Push(WhichRobot)==true)
BA.Spin(0, 125, 1);
else
BA.Brake(0);
/*
if (Push(WhichRobot)==false)
{
BA.Position(env->currentBall.pos, WhichRobot);
}
else
{
//BA.Hook(&env->home[WhichRobot], 1);
BA.Brake(WhichRobot);
}
*/
/*
}
void CTest::TestSpin(int Which_Robot)//ACS
{
env->home[Which_Robot].velocityLeft=-125;
env->home[Which_Robot].velocityRight=125;
return ;
}
void CTest::PositionFunction(Vector3D FinalPosition, int Which_Robot) //ACS
{
//Position(dest,WhichRobot);
double DesiredAngle, AngleError;
double PI = 3.1415923;
double MaxAngleError = 90;
double DistanceError;
double AngularGap = 3;
double RobotFront;
double Direction;
double Vl, Vr;
double Limit_X_High =10.8;
double Limit_X_Low = 8.6;
double Limit_Y_High = 50;
double Limit_Y_Low = 31;
Vector3D Robot_Position;
Vector3D Ball;
Ball = env->currentBall.pos;
Robot_Position.x = env->home[0].pos.x;
Robot_Position.y = env->home[0].pos.y;
// dx = FinalPosition.x - env->home[0].pos.x; // ???.X ??? same name as Vector3D XXX , int Which_Robot)
// dy = FinalPosition.y - env->home[0].pos.y;
// DistanceError = (double)(sqrt(dx*dx + dy*dy));
if(env->home[0].rotation > 180.001)
{
env->home[0].rotation -= 360;
}
else
{
if (env->home[0].rotation < -180.001)
{
env->home[0].rotation += 360;
}
}
if(env->home[0].rotation > 0 && env->home[0].rotation < 180)
{
DesiredAngle = 90;
RobotFront = 1;
}
else
{
DesiredAngle = -90;
RobotFront = -1;
}
AngleError = DesiredAngle - env->home[0].rotation;
if(AngleError > 0)
{
Direction = -1; //turn anti-clockwise
}
else
{
Direction = 1;
}
AngleError = fabs(AngleError);
if(AngleError < 3)
{
AngleError = 0;
}
if(Ball.y > Limit_Y_High)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -