view_tactical.cpp 6.88 KB
Newer Older
Matthew Hausknecht's avatar
Matthew Hausknecht committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
// -*-c++-*-

/*
 *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 "view_tactical.h"

#include <rcsc/action/basic_actions.h>
#include <rcsc/action/view_synch.h>

#include <rcsc/player/player_agent.h>
#include <rcsc/player/intercept_table.h>

#include <rcsc/common/server_param.h>
#include <rcsc/common/logger.h>

using namespace rcsc;

/*-------------------------------------------------------------------*/
/*!

 */
bool
View_Tactical::execute( PlayerAgent * agent )
{
    dlog.addText( Logger::TEAM,
                  __FILE__": View_Tactical" );

    // when our free kick mode, view width is set to WIDE
    switch ( agent->world().gameMode().type() ) {
    case GameMode::BeforeKickOff:
    case GameMode::AfterGoal_:
    case GameMode::PenaltySetup_:
    case GameMode::PenaltyReady_:
    case GameMode::PenaltyMiss_:
    case GameMode::PenaltyScore_:
        dlog.addText( Logger::TEAM,
                      __FILE__": doNormalChange. wide view" );
        return View_Wide().execute( agent );

    case GameMode::PenaltyTaken_:
        return View_Synch().execute( agent );

    case GameMode::GoalieCatch_:
        // our goalie kick
        if ( agent->world().gameMode().side() == agent->world().ourSide() )
        {
            return doOurGoalieFreeKick( agent );
        }

    case GameMode::BackPass_:
    case GameMode::FreeKickFault_:
    case GameMode::CatchFault_:
    case GameMode::GoalKick_:
    case GameMode::KickOff_:
    case GameMode::KickIn_:
    case GameMode::CornerKick_:
    case GameMode::FreeKick_:
    case GameMode::OffSide_:
    case GameMode::IndFreeKick_:
    default:
        break;
    }

    return doDefault( agent );
}

/*-------------------------------------------------------------------*/
/*!

 */
bool
View_Tactical::doDefault( PlayerAgent * agent )
{
    const WorldModel & wm = agent->world();

    if ( ! wm.ball().posValid() )
    {
        // for field scan
        return agent->doChangeView( ViewWidth::WIDE );
    }

    int self_min = wm.interceptTable()->selfReachCycle();
    int mate_min = wm.interceptTable()->teammateReachCycle();
    int opp_min = wm.interceptTable()->opponentReachCycle();
    int ball_reach_cycle = std::min( self_min, std::min( mate_min, opp_min ) );

    const Vector2D ball_pos = wm.ball().inertiaPoint( ball_reach_cycle );
    const double ball_dist = agent->effector().queuedNextSelfPos().dist( ball_pos );

    if ( wm.self().goalie()
         && ! wm.self().isKickable() )
    {
        const Vector2D goal_pos( - ServerParam::i().pitchHalfLength(), 0.0 );
        if ( ball_dist > 10.0
             || ball_pos.x > ServerParam::i().ourPenaltyAreaLineX()
             || ball_pos.dist( goal_pos ) > 20.0 )
        {
            AngleDeg ball_angle
                = agent->effector().queuedNextAngleFromBody( agent->effector().queuedNextBallPos() );
            double angle_diff = ball_angle.abs() - ServerParam::i().maxNeckAngle();
            if ( angle_diff > ViewWidth::width( ViewWidth::NORMAL ) * 0.5 - 3.0 )
            {
                dlog.addText( Logger::TEAM,
                              __FILE__": (goalie) ball_angle=%.1f angle_diff=%.1f change to wide",
                              ball_angle.degree(), angle_diff );
                return agent->doChangeView( ViewWidth::WIDE );
            }
            else if ( angle_diff > ViewWidth::width( ViewWidth::NARROW ) * 0.5 - 3.0 )
            {
                dlog.addText( Logger::TEAM,
                              __FILE__": (goalie) ball_angle=%.1f angle_diff=%.1f change to normal",
                              ball_angle.degree(), angle_diff );
                return agent->doChangeView( ViewWidth::NORMAL );
            }

            dlog.addText( Logger::TEAM,
                          __FILE__": (goalie) ball_angle=%.1f angle_diff=%.1f default",
                          ball_angle.degree(), angle_diff );
        }
    }

    // ball dist is too far
    if ( ball_dist > 40.0 )
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": far ball_dist=%.2f wide",
                      ball_dist );
        return agent->doChangeView( ViewWidth::WIDE );
    }

    if ( ball_dist > 20.0 )
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": far ball_dist=%.2f normal",
                      ball_dist );
        return agent->doChangeView( ViewWidth::NORMAL );
    }

    if ( ball_dist > 10.0 )
    {
        AngleDeg ball_angle
            = agent->effector().queuedNextAngleFromBody( agent->effector().queuedNextBallPos() );
        if ( ball_angle.abs() > 120.0 )
        {
            dlog.addText( Logger::TEAM,
                          __FILE__": big angle_diff=%.1f wide",
                          ball_angle.abs() );
            return agent->doChangeView( ViewWidth::WIDE );
        }
    }

    //const bool teammate_kickable = agent->world().existKickableTeammate();
    //const bool opponent_kickable = agent->world().existKickableOpponent();

    double teammate_ball_dist = 1000.0;
    double opponent_ball_dist = 1000.0;

    if ( ! agent->world().teammatesFromBall().empty() )
    {
        teammate_ball_dist
            = agent->world().teammatesFromBall().front()->distFromBall();
    }

    if ( ! agent->world().opponentsFromBall().empty() )
    {
        opponent_ball_dist
            = agent->world().opponentsFromBall().front()->distFromBall();
    }

    if ( ! wm.self().goalie()
         && teammate_ball_dist > 5.0
         && opponent_ball_dist > 5.0
         && ball_dist > 10.0
         && wm.ball().distFromSelf() > 10.0 )
    {
        dlog.addText( Logger::TEAM,
                      __FILE__": teammate_dist=%.2f opponent_dist=%.2f normal",
                      teammate_ball_dist, opponent_ball_dist );
        return agent->doChangeView( ViewWidth::NORMAL );
    }

    dlog.addText( Logger::TEAM,
                  __FILE__": default synch" );
    return View_Synch().execute( agent );

}

/*-------------------------------------------------------------------*/
/*!

 */
bool
View_Tactical::doOurGoalieFreeKick( PlayerAgent * agent )
{
    if ( agent->world().self().goalie() )
    {
        return View_Wide().execute( agent );
    }

    return doDefault( agent );
}