00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifndef RCSC_PLAYER_WORLD_MODEL_H
00033 #define RCSC_PLAYER_WORLD_MODEL_H
00034
00035 #include <rcsc/player/self_object.h>
00036 #include <rcsc/player/ball_object.h>
00037 #include <rcsc/player/player_object.h>
00038 #include <rcsc/player/view_area.h>
00039
00040 #include <rcsc/geom/vector_2d.h>
00041 #include <rcsc/game_mode.h>
00042 #include <rcsc/game_time.h>
00043 #include <rcsc/types.h>
00044
00045 #include <boost/shared_ptr.hpp>
00046
00047 #include <string>
00048
00049 namespace rcsc {
00050
00051 class AudioMemory;
00052 class ActionEffector;
00053 class BodySensor;
00054 class FullstateSensor;
00055 class InterceptTable;
00056 class Localization;
00057 class PlayerPredicate;
00058 class PlayerType;
00059 class PenaltyKickState;
00060 class VisualSensor;
00061
00066 class WorldModel {
00067 public:
00068
00069 enum {
00070 DIR_CONF_DIVS = 72
00071 };
00072
00073 static const std::size_t MAX_RECORD;
00074 static const double DIR_STEP;
00075
00076 private:
00077
00078 Localization * M_localize;
00079 InterceptTable * M_intercept_table;
00080 boost::shared_ptr< AudioMemory > M_audio_memory;
00081 PenaltyKickState * M_penalty_kick_state;
00082
00084 std::string M_teamname;
00085 SideID M_our_side;
00086
00087 std::string M_opponent_teamname;
00088
00090
00091
00092 GameTime M_time;
00093 GameTime M_sense_body_time;
00094 GameTime M_see_time;
00095 GameTime M_fullstate_time;
00096
00097 GameTime M_last_set_play_start_time;
00098 long M_setplay_count;
00099
00100 GameMode M_game_mode;
00101
00103
00104 SelfObject M_self;
00105 BallObject M_ball;
00106 BallRecord M_ball_record;
00107
00108 PlayerCont M_teammates;
00109 PlayerCont M_opponents;
00110 PlayerCont M_unknown_players;
00111
00113
00114
00115
00116
00117
00119 PlayerPtrCont M_teammates_from_self;
00121 PlayerPtrCont M_opponents_from_self;
00123 PlayerPtrCont M_teammates_from_ball;
00125 PlayerPtrCont M_opponents_from_ball;
00126
00128 int M_opponent_goalie_unum;
00129
00130 AbstractPlayerCont M_all_players;
00131 AbstractPlayerCont M_all_teammates;
00132 AbstractPlayerCont M_all_opponents;
00133
00134 AbstractPlayerObject * M_known_teammates[12];
00135 AbstractPlayerObject * M_known_opponents[12];
00136
00138
00139
00140
00141 double M_offside_line_x;
00142 int M_offside_line_count;
00143
00144 double M_defense_line_x;
00145
00146 bool M_exist_kickable_teammate;
00147 bool M_exist_kickable_opponent;
00148
00150
00151
00152 int M_teammate_types[11];
00153 int M_opponent_types[11];
00154
00156
00157
00159 int M_dir_count[DIR_CONF_DIVS];
00160
00162 ViewAreaCont M_view_area_cont;
00163
00165
00167 WorldModel( const WorldModel & );
00169 WorldModel & operator=( const WorldModel & );
00170
00171 public:
00175 WorldModel();
00176
00180 ~WorldModel();
00181
00186 const
00187 InterceptTable * interceptTable() const;
00188
00193 const
00194 PenaltyKickState * penaltyKickState() const;
00195
00200 const
00201 AudioMemory & audioMemory() const
00202 {
00203 return *M_audio_memory;
00204 }
00205
00216 bool initTeamInfo( const std::string & teamname,
00217 const SideID ourside,
00218 const int my_unum,
00219 const bool my_goalie );
00220
00226 void setAudioMemory( boost::shared_ptr< AudioMemory > memory );
00227
00233 void setTeammatePlayerType( const int unum,
00234 const int id );
00235
00241 void setOpponentPlayerType( const int unum,
00242 const int id );
00243
00244
00245 private:
00253 void update( const ActionEffector & act,
00254 const GameTime & current );
00255 public:
00264 void updateAfterSense( const BodySensor & sense,
00265 const ActionEffector & act,
00266 const GameTime & current );
00267
00277 void updateAfterSee( const VisualSensor & see,
00278 const BodySensor & sense,
00279 const ActionEffector & act,
00280 const GameTime & current );
00281
00290 void updateAfterFullstate( const FullstateSensor & fullstate,
00291 const ActionEffector & act,
00292 const GameTime & current );
00293
00301 void updateGameMode( const GameMode & game_mode,
00302 const GameTime & current );
00308 void setViewMode( const ViewWidth & w,
00309 const ViewQuality & q )
00310 {
00311 M_self.setViewMode( w, q );
00312 }
00313
00322 void updateJustBeforeDecision( const ActionEffector & act,
00323 const GameTime & current );
00324
00331 void setCommandEffect( const ActionEffector & act );
00332
00333 private:
00339 void localizeSelf( const VisualSensor & see,
00340 const GameTime & current );
00341
00348 void localizeBall( const VisualSensor & see,
00349 const ActionEffector & act,
00350 const GameTime & current );
00351
00363 void estimateBallVelByPosDiff( const VisualSensor & see,
00364 const ActionEffector & act,
00365 const GameTime & current,
00366 const Vector2D & rpos,
00367 const Vector2D & rpos_error,
00368 Vector2D & vel,
00369 Vector2D & vel_error,
00370 int & vel_count );
00371
00377 void localizePlayers( const VisualSensor & see );
00378
00388 void checkTeamPlayer( const SideID side,
00389 const Localization::PlayerT & player,
00390 const double & seen_dist,
00391 PlayerCont & old_known_players,
00392 PlayerCont & old_unknown_players,
00393 PlayerCont & new_known_players );
00394
00406 void checkUnknownPlayer( const Localization::PlayerT & player,
00407 const double & seen_dist,
00408 PlayerCont & old_teammates,
00409 PlayerCont & old_opponent,
00410 PlayerCont & old_unknown_players,
00411 PlayerCont & new_teammates,
00412 PlayerCont & new_opponents,
00413 PlayerCont & new_unknown_players );
00414
00418 void updateCollision();
00419
00424 void checkGhost( const ViewArea & varea );
00425
00430 void updateDirCount( const ViewArea & varea );
00431
00435 void updateBallByHear();
00436
00440 void updateGoalieByHear();
00441
00445 void updatePlayerByHear();
00446
00450 void updatePlayerType();
00451
00455 void updatePlayerMatrix();
00456
00460 void updateOffsideLine();
00461
00465 void updateDefenseLine();
00466
00467 public:
00468
00473 const
00474 std::string & teamName() const
00475 {
00476 return M_teamname;
00477 }
00478
00483 SideID ourSide() const
00484 {
00485 return M_our_side;
00486 }
00487
00492 const
00493 std::string & opponentTeamName() const
00494 {
00495 return M_opponent_teamname;
00496 }
00497
00502 SideID theirSide() const
00503 {
00504 return M_our_side == LEFT ? RIGHT : LEFT;
00505 }
00506
00511 bool isOurLeft() const
00512 {
00513 return M_our_side == LEFT;
00514 }
00515
00520 bool isOurRight() const
00521 {
00522 return M_our_side == RIGHT;
00523 }
00524
00529 const
00530 GameTime & time() const
00531 {
00532 return M_time;
00533 }
00534
00539 const
00540 GameTime & seeTime() const
00541 {
00542 return M_see_time;
00543 }
00544
00549 const
00550 GameTime & fullstateTime() const
00551 {
00552 return M_fullstate_time;
00553 }
00554
00563 const
00564 GameTime & lastSetPlayStartTime() const
00565 {
00566 return M_last_set_play_start_time;
00567 }
00568
00573 const
00574 long & setplayCount() const
00575 {
00576 return M_setplay_count;
00577 }
00578
00583 const
00584 GameMode & gameMode() const
00585 {
00586 return M_game_mode;
00587 }
00588
00593 const
00594 SelfObject & self() const
00595 {
00596 return M_self;
00597 }
00598
00603 const
00604 BallObject & ball() const
00605 {
00606 return M_ball;
00607 }
00608
00613 const
00614 BallRecord & ballRecord() const
00615 {
00616 return M_ball_record;
00617 }
00618
00623 const
00624 PlayerCont & teammates() const
00625 {
00626 return M_teammates;
00627 }
00628
00633 const
00634 PlayerCont & opponents() const
00635 {
00636 return M_opponents;
00637 }
00638
00643 const
00644 PlayerCont & unknownPlayers() const
00645 {
00646 return M_unknown_players;
00647 }
00648
00649
00650
00655 const
00656 PlayerPtrCont & teammatesFromSelf() const
00657 {
00658 return M_teammates_from_self;
00659 }
00660
00665 const
00666 PlayerPtrCont & opponentsFromSelf() const
00667 {
00668 return M_opponents_from_self;
00669 }
00670
00675 const
00676 PlayerPtrCont & teammatesFromBall() const
00677 {
00678 return M_teammates_from_ball;
00679 }
00680
00685 const
00686 PlayerPtrCont & opponentsFromBall() const
00687 {
00688 return M_opponents_from_ball;
00689 }
00690
00695 int opponentGoalieUnum() const
00696 {
00697 return M_opponent_goalie_unum;
00698 }
00699
00704 const
00705 AbstractPlayerCont & allPlayers() const
00706 {
00707 return M_all_players;
00708 }
00709
00714 const
00715 AbstractPlayerCont & allTeammates() const
00716 {
00717 return M_all_teammates;
00718 }
00719
00724 const
00725 AbstractPlayerCont & allOpponents() const
00726 {
00727 return M_all_opponents;
00728 }
00729
00731
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 const
00753 AbstractPlayerObject * teammate( const int unum ) const
00754 {
00755 if ( unum <= 0 || 11 < unum ) return M_known_teammates[0];
00756 return M_known_teammates[unum];
00757 }
00758
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 const
00780 AbstractPlayerObject * opponent( const int unum ) const
00781 {
00782 if ( unum <= 0 || 11 < unum ) return M_known_opponents[0];
00783 return M_known_opponents[unum];
00784 }
00785
00794 const
00795 PlayerObject * getFirstPlayer( PlayerPtrCont::const_iterator first,
00796 PlayerPtrCont::const_iterator last,
00797 const int count_thr ) const
00798 {
00799 while ( first != last )
00800 {
00801 if ( ! (*first)->isGhost()
00802 && (*first)->posCount() <= count_thr )
00803 {
00804 return *first;
00805 }
00806 ++first;
00807 }
00808 return static_cast< PlayerObject * >( 0 );
00809 }
00810
00816 const
00817 PlayerObject * getTeammateNearestToSelf( const int count_thr ) const
00818 {
00819 return getFirstPlayer( teammatesFromSelf().begin(),
00820 teammatesFromSelf().end(),
00821 count_thr );
00822 }
00823
00829 const
00830 PlayerObject * getOpponentNearestToSelf( const int count_thr ) const
00831 {
00832 return getFirstPlayer( opponentsFromSelf().begin(),
00833 opponentsFromSelf().end(),
00834 count_thr );
00835 }
00836
00842 double getDistTeammateNearestToSelf( const int count_thr ) const
00843 {
00844 const PlayerObject * p = getTeammateNearestToSelf( count_thr );
00845 return ( p ? p->distFromSelf() : 65535.0 );
00846 }
00847
00853 double getDistOpponentNearestToSelf( const int count_thr ) const
00854 {
00855 const PlayerObject * p = getOpponentNearestToSelf( count_thr );
00856 return ( p ? p->distFromSelf() : 65535.0 );
00857 }
00858
00864 const
00865 PlayerObject * getTeammateNearestToBall( const int count_thr ) const
00866 {
00867 return getFirstPlayer( teammatesFromBall().begin(),
00868 teammatesFromBall().end(),
00869 count_thr );
00870 }
00871
00877 const
00878 PlayerObject * getOpponentNearestToBall( const int count_thr ) const
00879 {
00880 return getFirstPlayer( opponentsFromBall().begin(),
00881 opponentsFromBall().end(),
00882 count_thr );
00883 }
00884
00890 double getDistTeammateNearestToBall( const int count_thr ) const
00891 {
00892 const PlayerObject * p = getTeammateNearestToSelf( count_thr );
00893 return ( p ? p->distFromBall() : 65535.0 );
00894 }
00895
00901 double getDistOpponentNearestToBall( const int count_thr ) const
00902 {
00903 const PlayerObject * p = getOpponentNearestToBall( count_thr );
00904 return ( p ? p->distFromBall() : 65535.0 );
00905 }
00906
00907
00908
00913 const
00914 double & offsideLineX() const
00915 {
00916 return M_offside_line_x;
00917 }
00918
00923 int offsideLineCount() const
00924 {
00925 return M_offside_line_count;
00926 }
00927
00932 const
00933 double & defenseLineX() const
00934 {
00935 return M_defense_line_x;
00936 }
00937
00942 bool existKickableTeammate() const
00943 {
00944 return M_exist_kickable_teammate;
00945 }
00946
00951 bool existKickableOpponent() const
00952 {
00953 return M_exist_kickable_opponent;
00954 }
00955
00961 int teammateHeteroID( const int unum ) const
00962 {
00963 if ( unum < 1 || 11 < unum )
00964 {
00965 std::cerr << "WorldModel::teammateHeteroID. Illegal unum "
00966 << unum << std::endl;
00967 return Hetero_Default;
00968 }
00969 return M_teammate_types[ unum - 1 ];
00970 }
00971
00977 const
00978 PlayerType * teammatePlayerType( const int unum ) const;
00979
00985 int opponentHeteroID( const int unum ) const
00986 {
00987 if ( unum < 1 || 11 < unum )
00988 {
00989 std::cerr << "WorldModel::opponentHeteroID. Illegal unum "
00990 << unum << std::endl;
00991 return Hetero_Unknown;
00992 }
00993 return M_opponent_types[ unum - 1 ];
00994 }
00995
01001 const
01002 PlayerType * opponentPlayerType( const int unum ) const;
01003
01004
01005
01011 int dirCount( const AngleDeg & angle ) const
01012 {
01013 int idx = static_cast< int >( ( angle.degree() - 0.5 + 180.0 )
01014 / DIR_STEP );
01015 if ( idx < 0 || DIR_CONF_DIVS - 1 < idx )
01016 {
01017 std::cerr << "WorldModel::getDirConf. index over flow"
01018 << std::endl;
01019 idx = 0;
01020 }
01021 return M_dir_count[idx];
01022 }
01023
01033 int dirRangeCount( const AngleDeg & angle,
01034 const double & width,
01035 int * max_count,
01036 int * sum_count,
01037 int * ave_count ) const;
01038
01043 const
01044 ViewAreaCont & viewAreaCont() const
01045 {
01046 return M_view_area_cont;
01047 }
01048
01054 int getPointCount( const Vector2D & point,
01055 const double & dir_thr ) const;
01056
01057
01058
01059
01060
01066 AbstractPlayerCont getPlayerCont( const PlayerPredicate * predicate ) const;
01067
01073 void getPlayerCont( AbstractPlayerCont & cont,
01074 const PlayerPredicate * predicate ) const;
01075
01076
01081 const
01082 PlayerObject * getOpponentGoalie() const;
01083
01092 const
01093 PlayerObject * getTeammateNearestTo( const Vector2D & point,
01094 const int count_thr,
01095 double * dist_to_point ) const;
01096
01105 const
01106 PlayerObject * getTeammateNearestTo( const PlayerObject * p,
01107 const int count_thr,
01108 double * dist_to_point ) const
01109 {
01110 if ( ! p ) return static_cast< const PlayerObject * >( 0 );
01111 return getTeammateNearestTo( p->pos(), count_thr, dist_to_point );
01112 }
01113
01122 const
01123 PlayerObject * getOpponentNearestTo( const Vector2D & point,
01124 const int count_thr,
01125 double * dist_to_point ) const;
01126
01127
01136 const
01137 PlayerObject * getOpponentNearestTo( const PlayerObject * p,
01138 const int count_thr,
01139 double * dist_to_point ) const
01140 {
01141 if ( ! p ) return static_cast< const PlayerObject * >( 0 );
01142 return getOpponentNearestTo( p->pos(), count_thr, dist_to_point );
01143 }
01144
01151 template < typename REGION >
01152 bool existTeammateIn( const REGION & region,
01153 const int count_thr,
01154 const bool with_goalie ) const
01155 {
01156 const PlayerPtrCont::const_iterator end = teammatesFromSelf().end();
01157 for ( PlayerPtrCont::const_iterator it = teammatesFromSelf().begin();
01158 it != end;
01159 ++it )
01160 {
01161 if ( (*it)->posCount() > count_thr
01162 || (*it)->isGhost() )
01163 {
01164 continue;
01165 }
01166 if ( (*it)->goalie() && ! with_goalie )
01167 {
01168 continue;
01169 }
01170 if ( region.contains( (*it)->pos() ) )
01171 {
01172 return true;
01173 }
01174 }
01175 return false;
01176 }
01177
01185 template < typename REGION >
01186 bool existOpponentIn( const REGION & region,
01187 const int count_thr,
01188 const bool with_goalie ) const
01189 {
01190 const PlayerPtrCont::const_iterator end = opponentsFromSelf().end();
01191 for ( PlayerPtrCont::const_iterator it = opponentsFromSelf().begin();
01192 it != end;
01193 ++it )
01194 {
01195 if ( (*it)->posCount() > count_thr
01196 || (*it)->isGhost() )
01197 {
01198 continue;
01199 }
01200 if ( (*it)->goalie() && ! with_goalie )
01201 {
01202 continue;
01203 }
01204 if ( region.contains( (*it)->pos() ) )
01205 {
01206 return true;
01207 }
01208 }
01209 return false;
01210 }
01211
01219 template < typename REGION >
01220 int countTeammatesIn( const REGION & region,
01221 const int count_thr,
01222 const bool with_goalie ) const
01223 {
01224 int count = 0;
01225 const PlayerPtrCont::const_iterator end = teammatesFromSelf().end();
01226 for ( PlayerPtrCont::const_iterator it = teammatesFromSelf().begin();
01227 it != end;
01228 ++it )
01229 {
01230 if ( (*it)->posCount() > count_thr
01231 || (*it)->isGhost()
01232 || ( (*it)->goalie() && ! with_goalie )
01233 )
01234 {
01235 continue;
01236 }
01237 if ( region.contains( (*it)->pos() ) )
01238 {
01239 ++count;
01240 }
01241 }
01242 return count;
01243 }
01244
01252 template < typename REGION >
01253 int countOpponentsIn( const REGION & region,
01254 const int count_thr,
01255 const bool with_goalie ) const
01256 {
01257 int count = 0;
01258 const PlayerPtrCont::const_iterator end = opponentsFromSelf().end();
01259 for ( PlayerPtrCont::const_iterator it = opponentsFromSelf().begin();
01260 it != end;
01261 ++it )
01262 {
01263 if ( (*it)->posCount() > count_thr
01264 || (*it)->isGhost()
01265 || ( (*it)->goalie() && ! with_goalie )
01266 )
01267 {
01268 continue;
01269 }
01270 if ( region.contains( (*it)->pos() ) )
01271 {
01272 ++count;
01273 }
01274 }
01275 return count;
01276 }
01277
01278 };
01279
01280 }
01281
01282 #endif