// -*-c++-*-
/*!
\file sample_communication.cpp
\brief sample communication planner Source File
*/
/*
*Copyright:
Copyright (C) Hidehisa AKIYAMA
This code 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; either version 3, or (at your option)
any later version.
This code 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this code; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*EndCopyright:
*/
/////////////////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "sample_communication.h"
#include "strategy.h"
#include <rcsc/formation/formation.h>
#include <rcsc/player/player_agent.h>
#include <rcsc/player/intercept_table.h>
#include <rcsc/player/say_message_builder.h>
#include <rcsc/player/freeform_parser.h>
#include <rcsc/common/logger.h>
#include <rcsc/common/server_param.h>
#include <rcsc/common/player_param.h>
#include <rcsc/common/audio_memory.h>
#include <rcsc/common/say_message_parser.h>
#include <cmath>
// #define DEBUG_PRINT
// #define DEBUG_PRINT_PLAYER_RECORD
using namespace rcsc;
namespace {
struct ObjectScore {
const AbstractPlayerObject * player_;
int number_;
double score_;
ObjectScore()
: player_( static_cast< AbstractPlayerObject * >( 0 ) ),
number_( -1 ),
score_( -65535.0 )
{ }
struct Compare {
bool operator()( const ObjectScore & lhs,
const ObjectScore & rhs ) const
{
return lhs.score_ > rhs.score_;
}
};
struct IllegalChecker {
bool operator()( const ObjectScore & val ) const
{
return val.score_ <= 0.1;
}
};
};
struct AbstractPlayerSelfDistCmp {
bool operator()( const AbstractPlayerObject * lhs,
const AbstractPlayerObject * rhs ) const
{
return lhs->distFromSelf() < rhs->distFromSelf();
}
};
struct AbstractPlayerBallDistCmp {
bool operator()( const AbstractPlayerObject * lhs,
const AbstractPlayerObject * rhs ) const
{
return lhs->distFromBall() < rhs->distFromBall();
}
};
struct AbstractPlayerBallXDiffCmp {
private:
AbstractPlayerBallXDiffCmp();
public:
const double ball_x_;
AbstractPlayerBallXDiffCmp( const double & ball_x )
: ball_x_( ball_x )
{ }
bool operator()( const AbstractPlayerObject * lhs,
const AbstractPlayerObject * rhs ) const
{
return std::fabs( lhs->pos().x - ball_x_ )
< std::fabs( rhs->pos().x - ball_x_ );
}
};
struct AbstractPlayerXCmp {
bool operator()( const AbstractPlayerObject * lhs,
const AbstractPlayerObject * rhs ) const
{
return lhs->pos().x < rhs->pos().x;
}
};
inline
double
distance_rate( const double & val,
const double & variance )
{
return std::exp( - std::pow( val, 2 ) / ( 2.0 * std::pow( variance, 2 ) ) );
}
inline
double
distance_from_ball( const AbstractPlayerObject * p,
const Vector2D & ball_pos,
const double & x_rate,
const double & y_rate )
{
//return p->distFromBall();
return std::sqrt( std::pow( ( p->pos().x - ball_pos.x ) * x_rate, 2 )
+ std::pow( ( p->pos().y - ball_pos.y ) * y_rate, 2 ) ); // Magic Number
}
}
/*-------------------------------------------------------------------*/
/*!
*/
SampleCommunication::SampleCommunication()
: M_current_sender_unum( Unum_Unknown ),
M_next_sender_unum( Unum_Unknown ),
M_ball_send_time( 0, 0 )
{
for ( int i = 0; i < 12; ++i )
{
M_teammate_send_time[i].assign( 0, 0 );
M_opponent_send_time[i].assign( 0, 0 );
}
}
/*-------------------------------------------------------------------*/
/*!
*/
SampleCommunication::~SampleCommunication()
{
}
/*-------------------------------------------------------------------*/
/*!
*/
bool
SampleCommunication::execute( PlayerAgent * agent )
{
if ( ! agent->config().useCommunication() )
{
return false;
}
updateCurrentSender( agent );
const WorldModel & wm = agent->world();
const bool penalty_shootout = wm.gameMode().isPenaltyKickMode();
bool say_recovery = false;
if ( wm.gameMode().type() != GameMode::PlayOn
&& ! penalty_shootout
&& currentSenderUnum() == wm.self().unum()
&& wm.self().recovery() < ServerParam::i().recoverInit() - 0.002 )
{
say_recovery = true;
sayRecovery( agent );
}
if ( wm.gameMode().type() == GameMode::BeforeKickOff
|| wm.gameMode().type() == GameMode::AfterGoal_
|| penalty_shootout )
{
return say_recovery;
}
#ifdef DEBUG_PRINT_PLAYER_RECORD
const AudioMemory::PlayerRecord::const_iterator end = agent->world().audioMemory().playerRecord().end();
for ( AudioMemory::PlayerRecord::const_iterator p = agent->world().audioMemory().playerRecord().begin();
p != end;
++p )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": PlayerRecord: time=[%d,%d] %s %d from %d",
p->first.cycle(), p->first.stopped(),
p->second.unum_ <= 11 ? "teammate" : "opponent",
p->second.unum_ <= 11 ? p->second.unum_ : p->second.unum_ - 11,
p->second.sender_ );
}
#endif
#if 1
sayBallAndPlayers( agent );
sayStamina( agent );
#else
sayBall( agent );
sayGoalie( agent );
saySelf( agent );
sayPlayers( agent );
#endif
attentiontoSomeone( agent );
return true;
}
/*-------------------------------------------------------------------*/
/*!
*/
void
SampleCommunication::updateCurrentSender( const PlayerAgent * agent )
{
const WorldModel & wm = agent->world();
if ( agent->effector().getSayMessageLength() > 0 )
{
M_current_sender_unum = wm.self().unum();
return;
}
// if ( wm.self().distFromBall() > ServerParam::i().audioCutDist() + 30.0 )
// {
// M_current_sender_unum = wm.self().unum();
// return;
// }
M_current_sender_unum = Unum_Unknown;
std::vector< int > candidate_unum;
candidate_unum.reserve( 11 );
if ( wm.ball().pos().x < -10.0
|| wm.gameMode().type() != GameMode::PlayOn )
{
//
// all players
//
for ( int unum = 1; unum <= 11; ++unum )
{
candidate_unum.push_back( unum );
}
}
else
{
//
// exclude goalie
//
const int goalie_unum = ( wm.ourGoalieUnum() != Unum_Unknown
? wm.ourGoalieUnum()
: Strategy::i().goalieUnum() );
for ( int unum = 1; unum <= 11; ++unum )
{
if ( unum != goalie_unum )
{
candidate_unum.push_back( unum );
#if 0
dlog.addText( Logger::COMMUNICATION,
__FILE__": (updateCurrentSender) field_player unum=%d",
candidate_unum.back() );
#endif
}
}
}
int val = ( wm.time().stopped() > 0
? wm.time().cycle() + wm.time().stopped()
: wm.time().cycle() );
int current = val % candidate_unum.size();
int next = ( val + 1 ) % candidate_unum.size();
M_current_sender_unum = candidate_unum[current];
M_ne
- 1
- 2
- 3
前往页