📄 player.cpp
字号:
/*
Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the University of Amsterdam nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! \file Player.cpp
<pre>
<b>File:</b> Player.cpp
<b>Project:</b> Robocup Soccer Simulation Team: UvA Trilearn
<b>Authors:</b> Jelle Kok
<b>Created:</b> 03/03/2001
<b>Last Revision:</b> $ID$
<b>Contents:</b> This file contains the definitions for the Player class,
which is a superclass from BasicPlayer and contains the
decision procedure to select the skills from the
BasicPlayer.
<hr size=2>
<h2><b>Changes</b></h2>
<b>Date</b> <b>Author</b> <b>Comment</b>
03/03/2001 Jelle Kok Initial version created
</pre>
*/
#include <stdlib.h>
#include "Player.h"
#include "Parse.h"
#include "Offclient.h"
#include "SenseHandler.h"
#include "CommunicationSystem.h"
#ifndef WIN32
#include <unistd.h>
#include <sys/poll.h> // needed for 'poll'
#endif
extern Logger LogDraw;
/*!This is the constructor the Player class and calls the constructor of the
superclass BasicPlayer.
\param act ActHandler to which the actions can be sent
\param wm WorldModel which information is used to determine action
\param ss ServerSettings that contain parameters used by the server
\param ps PlayerSettings that contain parameters important for the client
\param strTeamName team name of this player
\param dVersion version this basicplayer corresponds to
\param iReconnect integer that defines player number (-1 when new player) */
Player::Player( ActHandler* act, WorldModel *wm, ServerSettings *ss,
PlayerSettings *ps,
Formations *fs, SenseHandler *sh, char* strTeamName, double dVersion, int iReconnect )
{
char str[MAX_MSG];
ACT = act;
WM = wm;
SS = ss;
PS = ps;
formations = fs;
SH = sh;
bContLoop = true;
m_iPenaltyNr = -1;
WM->setTeamName( strTeamName );
m_timeLastSay = -5;
m_objMarkOpp = OBJECT_ILLEGAL;
m_actionPrev = ACT_ILLEGAL;
// wait longer as role number increases, to make sure players appear at the
// field in the correct order
#ifdef WIN32
Sleep( formations->getPlayerInFormation()*500 );
#else
poll( 0, 0, formations->getPlayerInFormation()*500 );
#endif
// create initialisation string
if( iReconnect != -1 )
sprintf( str, "(reconnect %s %d)", strTeamName, iReconnect );
else if( formations->getPlayerType() == PT_GOALKEEPER )
sprintf( str, "(init %s (version %f) (goalie))", strTeamName, dVersion );
else
sprintf( str, "(init %s (version %f))", strTeamName, dVersion );
ACT->sendMessage( str );
}
/*! This is the main loop of the agent. This method calls the update methods
of the world model after it is indicated that new information has arrived.
After this, the correct main loop of the player type is called, which
puts the best soccer command in the queue of the ActHandler. */
void Player::mainLoop( )
{
#ifndef WIN32
sleep(1);
#endif
char str[MAX_MSG];
Timing timer;
FILE *from=NULL;
char Message[MAX_MSG];
char *buf = Message;
formations->setFormation( FT_433_OFFENSIVE );
//首先分析最初的消息包
if ( offclient.is_run_offclient() ){
//The default offclient log file is "c:\zjudebug\offclient1.log".
//The offclient usually runs on Windows,
//if you want to run it on Linux, please change this part of code.
char offclient_filename[128];
sprintf(offclient_filename,
"c:\\zjudebug\\offclient%d.log",
offclient.get_offclient_number());
from = fopen(offclient_filename,"r");
fgets(Message, MAX_MSG, from);
SH->analyzeMessage(buf+5);
}
//往下开始第一次更新和决策
// wait for new information from the server
// cannot say bContLoop=WM->wait... since bContLoop can be changed elsewhere
if ( !offclient.is_run_offclient() ){ //正常情况 //这里外面套一层if
if( WM->waitForNewInformation() == false )
bContLoop = false;
}
// and set the clang version
sprintf( str, "(clang (ver 8 8))" );
ACT->sendMessage( str );
while( bContLoop ) // as long as server alive
{
Log.logWithTime( 3, " start update_all" );
Log.setHeader( WM->getCurrentCycle() );
LogDraw.setHeader( WM->getCurrentCycle() );
if( WM->updateAll( ) == true )
{
timer.restartTime();
SoccerCommand soc;
if( ( WM->isPenaltyUs( ) || WM->isPenaltyThem() ) )
performPenalty();
else if( WM->getPlayMode() == PM_FROZEN )
ACT->putCommandInQueue( turnBodyToObject( OBJECT_BALL ) );
else if( WM->getTimeLastSeeMessage() == WM->getCurrentTime() )
{
switch( formations->getPlayerType( ) ) // determine right loop
{
case PT_GOALKEEPER: soc = goalieMainLoop( ); break;
case PT_DEFENDER_SWEEPER:
case PT_DEFENDER_CENTRAL:
case PT_DEFENDER_WING: soc = defenderMainLoop( ); break;
case PT_MIDFIELDER_CENTER:
case PT_MIDFIELDER_WING: soc = midfielderMainLoop( ); break;
case PT_ATTACKER:
case PT_ATTACKER_WING: soc = attackerMainLoop( ); break;
case PT_ILLEGAL:
default: soc = attackerMainLoop( ); break;
}
/* old communication
if( shallISaySomething(soc) == true ) // shall I communicate
{
m_timeLastSay = WM->getCurrentTime();
char strMsg[MAX_SAY_MSG];
if( WM->getPlayerNumber() == 6 &&
WM->getBallPos().getX() < - PENALTY_X + 4.0 )
sayOppAttackerStatus( strMsg );
else
sayBallStatus( strMsg );
if( strlen( strMsg ) != 0 )
Log.log( 600, "send communication string: %s", strMsg );
WM->setCommunicationString( strMsg );
}
*/
if(coms.msg_num_stored>0)
WM->setCommunicationString( coms.GenerateMsgString() );
else
WM->setCommunicationString( "" );
coms.Reset();
}
Log.logWithTime( 3, " determined action; waiting for new info" );
// directly after see message, will nog get better info, so send commands
if( WM->getTimeLastSeeMessage() == WM->getCurrentTime() ||
(SS->getSynchMode() == true && WM->getRecvThink() == true ))
{
Log.logWithTime( 3, " send messages directly" );
ACT->sendCommands( );
Log.logWithTime( 3, " sent messages directly" );
if( SS->getSynchMode() == true )
{
WM->processRecvThink( false );
ACT->sendMessageDirect( "(done)" );
}
}
}
else
Log.logWithTime( 3, " HOLE no action determined; waiting for new info");
if( WM->getCurrentCycle()%(SS->getHalfTime()*SS->getSimulatorStep()) != 0 )
{
if( LogDraw.isInLogLevel( 600 ) )
{
WM->logDrawInfo( 600 );
}
if( LogDraw.isInLogLevel( 601 ) )
WM->logDrawBallInfo( 601 );
if( LogDraw.isInLogLevel( 700 ) )
WM->logCoordInfo( 700 );
}
Log.logWithTime( 604, "time for action: %f", timer.getElapsedTime()*1000 );
if ( !offclient.is_run_offclient() ){ //正常情况
// wait for new information from the server
// cannot say bContLoop=WM->wait... since bContLoop can be changed elsewhere
if( WM->waitForNewInformation() == false )
bContLoop = false;
}
else { //调试模式, 以读文件来代替
while(!feof(from)){
fgets(Message, MAX_MSG, from);
if ( !strncmp(Message, "recv", 4 ) ){
SH->analyzeMessage(buf+5);
break;
}
}
if ( feof(from) ){
bContLoop = false;
fclose(from);
}
}
}
// shutdow, print hole and number of players seen statistics
printf("Shutting down player %d\n", WM->getPlayerNumber() );
printf(" Number of holes: %d (%f)\n", WM->iNrHoles,
((double)WM->iNrHoles/WM->getCurrentCycle())*100 );
printf(" Teammates seen: %d (%f)\n", WM->iNrTeammatesSeen,
((double)WM->iNrTeammatesSeen/WM->getCurrentCycle()));
printf(" Opponents seen: %d (%f)\n", WM->iNrOpponentsSeen,
((double)WM->iNrOpponentsSeen/WM->getCurrentCycle()));
}
/*! This is the main decision loop for the goalkeeper. */
SoccerCommand Player::goalieMainLoop( )
{
return deMeer5_goalie();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -