📄 shoot.cpp
字号:
/* * Copyright 2002-2005, Mersad Team, Allameh Helli High School (NODET). * * This program is free software, you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * This file is created by: Mostafa Rokooey * * Released on Monday 1 August 2005, 10 Mordad 1384 by Mersad RoboCup Team. * For more information please read README file.*/#include <cmath>#include <Command.h>#include <Degree.h>#include <Line.h>#include <Shoot.h>#include <Logger.h>#include <Basics.h>#include <Defines.h>#include <BasicKick.h>#include <AdvancedAgent.h>#define SHOOT_LOG_LEVEL 0using namespace std;Shoot::Shoot(const WorldModel *worldModel): AdvancedAction(worldModel){ int i; for (i = 0;i <= NUMBEROFSHOOTPOINTS;i++) { shootPoints[i].x = SHOOTPOINTSX; shootPoints[i].y = -6.3 + (12.6 / NUMBEROFSHOOTPOINTS) * (i); // Sassan } shootLogLevel = SHOOT_LOG_LEVEL;}Shoot::~Shoot(){}float Shoot::getValue(const Library &library){ LOG<< "Shoot::getValue" << endl; int i; if (worldModel->getBody().getPos().getX() <= 25.0) // Sassan 30 return AD_MIN_VALUE; setShootRates(); for (i = 0; i <= NUMBEROFSHOOTPOINTS; i++) { if (shootLogLevel >= 2) LOG << " --------------- Shoot Number " << i << " ---------------" << endl; if (!getFastest(i)) { shootWeights[i] = 0; if (shootLogLevel >= 2) LOG << "I cant't kick the ball." << endl; continue; } shootWeights[i] = getCatchWeight(i) * catchRate; shootWeights[i] += getNoiseWeight(i) * noiseRate; shootWeights[i] += getFarToGoalieWeight(i) * farToGoalieRate; shootWeights[i] += getGoalieMistakeWeight() * goalieMistakeRate; shootWeights[i] += getBallFastMovingWeight(library) * ballFastMovingRate; shootWeights[i] += getGoaliePosDeviationWeight() * goaliePosDeviationRate; shootWeights[i] += getGoalSeenWeight(i) * goalSeenRate; if (shootLogLevel >= 2) { LOG << "catchWeight = " << getCatchWeight(i) << endl; LOG << "noiseWeight = " << getNoiseWeight(i) << endl; LOG << "farToGoalieWeight = " << getFarToGoalieWeight(i) << endl; LOG << "goalieMistakeWeight = " << getGoalieMistakeWeight() << endl; LOG << "ballFastMovingWeight = " << getBallFastMovingWeight(library) << endl; LOG << "goaliePosDeviationWeight = " << getGoaliePosDeviationWeight() << endl; LOG << "goalSeenWeight = " << getGoalSeenWeight(i) << endl; LOG << "shootWeight is = " << shootWeights[i] << endl; } if (getCatchWeight(i) <= 10) { if (shootLogLevel >= 2) LOG << "shootWeight changed to 0." << endl; shootWeights[i] = 0; } if (getGoalSeenWeight(i) <= 10) { if (shootLogLevel >= 2) LOG << "shootWeight changed to 0. (goalSeen)" << endl; shootWeights[i] = 0; } } float weightsHistory[NUMBEROFSHOOTPOINTS]; for (i = 0; i <= NUMBEROFSHOOTPOINTS; i++) weightsHistory[i] = shootWeights[i]; for (i = 0; i <= NUMBEROFSHOOTPOINTS; i++) { if (i > 0) shootWeights[i] += weightsHistory[i - 1] / 10; else shootWeights[i] += weightsHistory[i + 1] / 10; if (i < NUMBEROFSHOOTPOINTS) shootWeights[i] += weightsHistory[i + 1] / 10; else shootWeights[i] += weightsHistory[i - 1] / 10; } float bestShootWeight = -0xFFFF; int bestShootNum = NOVALUE; for (i = 0; i <= NUMBEROFSHOOTPOINTS; i++) { if (shootLogLevel >= 1) LOG << "shootWeights[" << i << "] = " << shootWeights[i] << endl; if (shootWeights[i] > bestShootWeight) { bestShootWeight = shootWeights[i]; bestShootNum = i; } } if (bestShootNum != NOVALUE) { shootCommand = shootCommands[bestShootNum]; } else { shootCommand = new KickCommand(AT_SHOOT, 100, 0); } if (shootLogLevel >= 1) { LOG << " bestShootWeight = " << bestShootWeight << endl; LOG << " bestShootNum = " << bestShootNum << endl; LOG << " FINAL shootCommand = " << shootCommand->toString() << endl; } if (bestShootWeight >= 5000) return AD_ALWAYS_RUN_VALUE; else return AD_MIN_VALUE;}void Shoot::execute(Form &form, const Library &library){ LOG<< "Shoot::execute" << endl; command = shootCommand;}bool Shoot::getFastest(int shootNum){ shootIntercept.startSession(1, &worldModel->getBody()); if (shootLogLevel >= 3) shootIntercept.setLogging(true); // Setting goalie configurations shootIntercept.setTmmSlowDashPower(40); shootIntercept.setTmmFastDashPower(100); if (worldModel->getPlayMode() == PM_PENALTY_SETUP || worldModel->getPlayMode() == PM_PENALTY_SETUP_OPP || worldModel->getPlayMode() == PM_PENALTY_READY_OPP || worldModel->getPlayMode() == PM_PENALTY_READY) { shootIntercept.setTmmDirectDashPower(67); shootIntercept.setTmmGoalieDirectDashPower(67); } else { shootIntercept.setTmmDirectDashPower(60); shootIntercept.setTmmGoalieDirectDashPower(60); } shootIntercept.setTmmGoalieSeeBallDelay(0); shootIntercept.setTmmSeeBallDelay(0); if (worldModel->getPlayMode() == PM_PENALTY_SETUP || worldModel->getPlayMode() == PM_PENALTY_SETUP_OPP || worldModel->getPlayMode() == PM_PENALTY_READY_OPP || worldModel->getPlayMode() == PM_PENALTY_READY) { shootIntercept.setTmmPosDevRate(0.78); } else { shootIntercept.setTmmPosDevRate(0.75); } // Setting players configurations shootIntercept.setOppSlowDashPower(10); shootIntercept.setOppFastDashPower(50); shootIntercept.setOppDirectDashPower(20); shootIntercept.setOppSeeBallDelay(2); shootIntercept.setOppGoalieDirectDashPower(20); shootIntercept.setOppGoalieSeeBallDelay(2); shootIntercept.setOppPosDevRate(0.15); for (int i = 0; i < FULL_PLAYERS_NUM; i++) if (worldModel->getFullPlayer(TID_OPPONENT, i).isValid()) if (!worldModel->getFullPlayer(TID_OPPONENT, i).isGoalie()) { if (shootLogLevel >= 2) LOG << "full Opp added, num = " << i << endl; shootIntercept.addPlayer(worldModel-> getFullPlayer(TID_OPPONENT, i)); } for (int i = 0; i < HALF_PLAYERS_NUM; i++) if (worldModel->getHalfPlayer(TID_OPPONENT, i).isValid()) if (!worldModel->getHalfPlayer(TID_OPPONENT, i).isGoalie()) { if (shootLogLevel >= 2) LOG << "half Opp added, num = " << i << endl; shootIntercept.addPlayer(worldModel-> getHalfPlayer(TID_OPPONENT, i)); } if (worldModel->getOppGoalie() != NULL) { if (shootLogLevel >= 2) LOG << "OppGoalie added." << endl; Player opponentGoalie; opponentGoalie = *(worldModel->getOppGoalie()); opponentGoalie.setTeamId(TID_TEAMMATE); shootIntercept.addPlayer(opponentGoalie, false, false, false); } if (!kickMaxPower(shootNum)) return false; if ((getGoalSeenWeight((int)(NUMBEROFSHOOTPOINTS / 2)) < 84) && (shootIntercept.getPlayersNum() == 0)) return false; if (shootLogLevel >= 2) { LOG << "addedPlayersNum = " << shootIntercept.getPlayersNum(); LOG << "shootNum = " << shootNum << endl; LOG << "shootCommands[shootNum] = " << shootCommands[shootNum]->toString() << endl; } Ball shootedBall; shootedBall = worldModel->getBall(); shootedBall.simulateByAction(worldModel->getBody(), shootCommands[shootNum]); shootIntercept.setVirtualBall(shootedBall); shootIntercept.setKickCycles(1); shootIntercept.run(); return true;}bool Shoot::kickMaxPower(int shootNum){ Point targetShootPoint; float shootedBallFirstVelocity; bool iCantShoot = false; KickToPointVelocity *kickForShoot; Command *kickCommand; kickCommand = new KickCommand(AT_SHOOT, 100, 0); targetShootPoint = shootPoints[shootNum]; shootedBallFirstVelocity = 4.0; while(1) { if (shootedBallFirstVelocity < 0.0001) { iCantShoot = true; break; } kickForShoot = new KickToPointVelocity(AT_SHOOT, targetShootPoint, shootedBallFirstVelocity,worldModel->getBall(), worldModel->getBody()); delete kickCommand; kickCommand = kickForShoot->getCommand(); if (!kickForShoot->isPossible()) shootedBallFirstVelocity -= .1; else break; } shootCommands[shootNum] = new KickCommand(*dynamic_cast<KickCommand *>(kickCommand)); if(shootedBallFirstVelocity <= 0.05) return false; if(iCantShoot) return false; return true; }float Shoot::getCatchWeight(int shootNum){ Point shootPoint = shootPoints[shootNum]; Point catchPoint; if (!shootIntercept.existFastestPlayer()) { if (shootLogLevel >= 2) LOG << "don't exist fastest player." << endl; catchPoint = Point(55, 0); } else { if (shootLogLevel >= 2) LOG << "exist fastest player." << endl; catchPoint = shootIntercept.getFastestInterPlayer().getInterPoint(); } if (shootLogLevel >= 2) LOG << "catchPoint = " << catchPoint << endl; Vector shootVec; shootVec.setByPoints(worldModel->getBall().getPos(), catchPoint); float aveRange = 52.5 - (shootVec.getX() / 10); if (fabs(worldModel->getBody().getPos().getX()) >= 8.5) // Sassan: getX ? (or Y) aveRange = 52.7; if (shootLogLevel >= 2) LOG << "aveRange = " << aveRange << endl; if (catchPoint.x >= 55) return 100; else if (catchPoint.x >= 52.5) return 80; return Basics::reRate(catchPoint.x, aveRange, 52.5, 0, 20);}float Shoot::getNoiseWeight(int shootNum){ Point shootPoint = shootPoints[shootNum]; Vector shootVector; shootVector.setByPoints(worldModel->getBall().getPos(), shootPoint); if (shootVector.getMagnitude() <= 2) { if (fabs(shootPoint.y) >= 6) return 50; if (fabs(shootPoint.y) >= 5) return 70; return 100; } else if (shootVector.getMagnitude() <= 6) { if (fabs(shootPoint.y) <= 4) return 100; if (fabs(shootPoint.y) > 5.8) return 40; return Basics::reRate(fabs(shootPoint.y), 4, 6, 40, 100, RRM_REVERSE); } else { if (fabs(shootPoint.y) <= 4) return 100; if (fabs(shootPoint.y) > 5.8) return 25; return Basics::reRate(fabs(shootPoint.y), 4, 5.8, 25, 100, RRM_REVERSE); }}float Shoot::getGoalSeenWeight(int shootNum){ Point shootPoint = shootPoints[shootNum]; Point goalSeenCheckPoint; Vector meToCheckVector; float goalSeenValue; if(shootPoint.y <= -2.5 && shootPoint.y > -7) goalSeenCheckPoint.y = -5; else if(shootPoint.y <= 2.5 && shootPoint.y > -2.5) goalSeenCheckPoint.y = 0; else if(shootPoint.y <= 7 && shootPoint.y > 2.5) goalSeenCheckPoint.y = 5; else goalSeenCheckPoint.y = 0; goalSeenCheckPoint.x = SHOOTPOINTSX; meToCheckVector.setByPoints(worldModel->getBody().getPos(), goalSeenCheckPoint); goalSeenValue = worldModel->getSeeDeltaCycle(meToCheckVector.getDirection(), 0.2); if (getGoaliePosDeviationWeight() >= 75) return 85; if (goalSeenValue > 7) return 0; else if (goalSeenValue > 5) return 40; else return Basics::reRate(goalSeenValue, 0, 5, 60, 100, RRM_REVERSE);}float Shoot::getGoaliePosDeviationWeight(void){ if (worldModel->getOppGoalie() != NULL) return Basics::reRate(worldModel->getOppGoalie()->getPosDeviation(), 0.6, 7, 0, 100, RRM_REVERSE); return 60;}float Shoot::getGoalieMistakeWeight(void){ if (worldModel->getOppGoalie() != NULL) { Line upTir, downTir; Line halfMaker; Line goalieXLine; upTir.setByPoints(worldModel->getBall().getPos(), Point(52.5, 6.5)); downTir.setByPoints(worldModel->getBall().getPos(), Point(52.5, -6.5)); float halfDir = Degree::normalizeAngle((upTir.getDirection() + downTir.getDirection()) / 2); halfMaker.setBySourceDir(worldModel->getBall().getPos(), halfDir); goalieXLine.setBySourceDir(Point(51.5, 0), 90); Point goalieBestPoint; halfMaker.getLineIntersect(goalieXLine, goalieBestPoint); Vector goalieMistake; goalieMistake.setByPoints(worldModel->getOppGoalie()->getPos(), goalieBestPoint); return Basics::reRate(goalieMistake.getY(), 1.5, 7, 0, 100, RRM_DIRECT); } return 20;}float Shoot::getFarToGoalieWeight(int shootNum){ Point shootPoint = shootPoints[shootNum]; if (worldModel->getOppGoalie() != NULL) return Basics::reRate(worldModel->getOppGoalie()-> getDistance(shootPoint), 0, 14, 0, 100, RRM_DIRECT); return 60;}float Shoot::getBallFastMovingWeight(const Library &library){ float value; if (library.lastBodyCycleCommand->getCreator() == AT_INTERCEPT) { value = (fabs(worldModel->getBall().getVel().getX()) / 4) + (fabs(worldModel->getBall().getVel().getY()) * 1); return Basics::reRate(value, .75, 2.75, 0, 100, RRM_DIRECT); } return 0;}void Shoot::setShootRates(void){ catchRate = 30; noiseRate = 5; goaliePosDeviationRate = 5; farToGoalieRate = 25; goalSeenRate = 8; goalieMistakeRate = 10; ballFastMovingRate = 8;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -