-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_communication.cpp
More file actions
2290 lines (1941 loc) · 72.5 KB
/
Copy pathsample_communication.cpp
File metadata and controls
2290 lines (1941 loc) · 72.5 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*-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_next_sender_unum = candidate_unum[next];
#ifdef DEBUG_PRINT
dlog.addText( Logger::COMMUNICATION,
__FILE__": (updateCurrentSender) time=%d size=%d index=%d current=%d next=%d",
wm.time().cycle(),
candidate_unum.size(),
current,
M_current_sender_unum,
M_next_sender_unum );
#endif
}
/*-------------------------------------------------------------------*/
/*!
*/
void
SampleCommunication::updatePlayerSendTime( const WorldModel & wm,
const SideID side,
const int unum )
{
if ( unum < 1 || 11 < unum )
{
std::cerr << __FILE__ << ':' << __LINE__
<< ": Illegal player number. " << unum
<< std::endl;
return;
}
if ( side == wm.ourSide() )
{
M_teammate_send_time[unum] = wm.time();
}
else
{
M_opponent_send_time[unum] = wm.time();
}
}
/*-------------------------------------------------------------------*/
/*!
*/
bool
SampleCommunication::shouldSayBall( const PlayerAgent * agent )
{
const WorldModel & wm = agent->world();
if ( wm.ball().seenPosCount() > 0
|| wm.ball().seenVelCount() > 2 )
{
return false;
}
if ( wm.gameMode().type() != GameMode::PlayOn )
{
if ( agent->effector().queuedNextBallVel().r2() < std::pow( 0.5, 2 ) )
{
return false;
}
}
if ( wm.existKickableTeammate() )
{
return false;
}
bool ball_vel_changed = false;
const double cur_ball_speed = wm.ball().vel().r();
const BallObject::State * prev_ball_state = wm.ball().getState( 1 );
if ( prev_ball_state )
{
const double prev_ball_speed = prev_ball_state->vel_.r();
double angle_diff = ( wm.ball().vel().th() - prev_ball_state->vel_.th() ).abs();
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) check ball vel" );
dlog.addText( Logger::COMMUNICATION,
"prev_vel=(%.2f %.2f) r=%.3f",
prev_ball_state->vel_.x, prev_ball_state->vel_.y,
prev_ball_speed );
dlog.addText( Logger::COMMUNICATION,
"cur_vel=(%.2f %.2f) r=%.3f",
wm.ball().vel().x, wm.ball().vel().y,
cur_ball_speed );
if ( cur_ball_speed > prev_ball_speed + 0.1
|| ( prev_ball_speed > 0.5
&& cur_ball_speed < prev_ball_speed * ServerParam::i().ballDecay() * 0.5 )
|| ( prev_ball_speed > 0.5 // Magic Number
&& angle_diff > 20.0 ) ) // Magic Number
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) ball vel changed." );
ball_vel_changed = true;
}
}
if ( ball_vel_changed
&& wm.lastKickerSide() != wm.ourSide()
&& ! wm.existKickableOpponent() )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) ball vel changed. opponent kicked. no opponent kicker" );
return true;
}
if ( wm.self().isKickable() )
{
if ( agent->effector().queuedNextBallKickable() )
{
if ( cur_ball_speed < 1.0 )
{
return false;
}
}
if ( ball_vel_changed
&& agent->effector().queuedNextBallVel().r() > 1.0 )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) kickable. ball vel changed" );
return true;
}
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) kickable, but no say" );
return false;
}
const PlayerObject * ball_nearest_teammate = NULL;
const PlayerObject * second_ball_nearest_teammate = NULL;
const PlayerPtrCont::const_iterator t_end = wm.teammatesFromBall().end();
for ( PlayerPtrCont::const_iterator t = wm.teammatesFromBall().begin();
t != t_end;
++t )
{
if ( (*t)->isGhost() || (*t)->posCount() >= 10 ) continue;
if ( ! ball_nearest_teammate )
{
ball_nearest_teammate = *t;
}
else if ( ! second_ball_nearest_teammate )
{
second_ball_nearest_teammate = *t;
break;
}
}
int our_min = std::min( wm.interceptTable()->selfReachCycle(),
wm.interceptTable()->teammateReachCycle() );
int opp_min = wm.interceptTable()->opponentReachCycle();
//
// I am the nearest player to ball
//
if ( ! ball_nearest_teammate
|| ( ball_nearest_teammate->distFromBall() > wm.ball().distFromSelf() - 3.0 ) )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) maybe nearest to ball" );
//return true;
if ( ball_vel_changed
|| ( opp_min <= 1 && cur_ball_speed > 1.0 ) )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) nearest to ball. ball vel changed?" );
return true;
}
}
if ( ball_nearest_teammate
&& wm.ball().distFromSelf() < 20.0
&& 1.0 < ball_nearest_teammate->distFromBall()
&& ball_nearest_teammate->distFromBall() < 6.0
&& ( opp_min <= our_min + 1
|| ball_vel_changed ) )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) support nearest player" );
return true;
}
#if 0
if ( ball_nearest_teammate
&& second_ball_nearest_teammate
&& ( ball_nearest_teammate->distFromBall() // the ball nearest player
> ServerParam::i().visibleDistance() - 0.5 ) // over vis dist
&& ( second_ball_nearest_teammate->distFromBall() > wm.ball().distFromSelf() - 5.0 ) // I am second
)
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (shouldSayBall) second nearest to ball" );
return true;
}
#endif
return false;
}
/*-------------------------------------------------------------------*/
/*!
*/
bool
SampleCommunication::shouldSayOpponentGoalie( const PlayerAgent * agent )
{
const WorldModel & wm = agent->world();
const PlayerObject * goalie = wm.getOpponentGoalie();
if ( ! goalie )
{
return false;
}
if ( goalie->seenPosCount() == 0
&& goalie->bodyCount() == 0
&& goalie->unum() != Unum_Unknown
&& goalie->unumCount() == 0
&& goalie->distFromSelf() < 25.0
&& 53.0 - 16.0 < goalie->pos().x
&& goalie->pos().x < 52.5
&& goalie->pos().absY() < 20.0 )
{
Vector2D goal_pos = ServerParam::i().theirTeamGoalPos();
Vector2D ball_next = wm.ball().pos() + wm.ball().vel();
if ( ball_next.dist2( goal_pos ) < std::pow( 18.0, 2 ) )
{
return true;
}
}
return false;
}
/*-------------------------------------------------------------------*/
/*!
*/
bool
SampleCommunication::goalieSaySituation( const rcsc::PlayerAgent * agent )
{
const WorldModel & wm = agent->world();
if ( ! wm.self().goalie() )
{
return false;
}
int player_count = 0;
const AbstractPlayerCont::const_iterator end = wm.allPlayers().end();
for ( AbstractPlayerCont::const_iterator p = wm.allPlayers().begin();
p != end;
++p )
{
if ( (*p)->unumCount() == 0 )
{
++player_count;
}
}
if ( player_count >= 5 )
{
return true;
}
return false;
}
/*-------------------------------------------------------------------*/
/*!
*/
bool
SampleCommunication::sayBallAndPlayers( PlayerAgent * agent )
{
const WorldModel & wm = agent->world();
const int current_len = agent->effector().getSayMessageLength();
const bool should_say_ball = shouldSayBall( agent );
const bool should_say_goalie = shouldSayOpponentGoalie( agent );
const bool goalie_say_situation = false; //goalieSaySituation( agent );
if ( ! should_say_ball
&& ! should_say_goalie
&& ! goalie_say_situation
&& currentSenderUnum() != wm.self().unum()
&& current_len == 0 )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": (sayBallAndPlayers) no send situation" );
return false;
}
const int available_len = ServerParam::i().playerSayMsgSize() - current_len;
const AudioMemory & am = wm.audioMemory();
//
// initialize object priority score with fixed value 1000
//
std::vector< ObjectScore > objects( 23 ); // 0: ball, 1-11: teammate, 12-23: opponent
for ( int i = 0; i < 23; ++i )
{
objects[i].number_ = i;
objects[i].score_ = 1000.0;
}
//
// as a base score, set time difference since last hear
//
objects[0].score_ = wm.time().cycle() - am.ballTime().cycle();
{
const AudioMemory::PlayerRecord::const_iterator end = am.playerRecord().end();
for ( AudioMemory::PlayerRecord::const_iterator p = am.playerRecord().begin();
p != end;
++p )
{
int num = p->second.unum_;
if ( num < 1 || 22 < num )
{
continue;
}
objects[num].score_ = wm.time().cycle() - p->first.cycle();
}
}
//
// opponent goalie
//
if ( 1 <= wm.theirGoalieUnum()
&& wm.theirGoalieUnum() <= 11 )
{
int num = wm.theirGoalieUnum() + 11;
double diff = wm.time().cycle() - am.goalieTime().cycle();
objects[num].score_ = std::min( objects[num].score_, diff );
}
#ifdef DEBUG_PRINT
for ( int i = 0; i < 23; ++i )
{
dlog.addText( Logger::COMMUNICATION,
__FILE__": base score: %s %d = %f",
( i == 0 ? "ball" : i <= 11 ? "teammate" : "opponent" ),
( i <= 11 ? i : i - 11 ),
objects[i].score_ );
}
#endif
//
// ball
//
if ( wm.self().isKickable() )
{
if ( agent->effector().queuedNextBallKickable() )
{
objects[0].score_ = -65535.0;
}
else
{
objects[0].score_ = 1000.0;
}
}
else if ( wm.ball().seenPosCount() > 0
|| wm.ball().seenVelCount() > 1
|| wm.existKickableTeammate() )
{
objects[0].score_ = -65535.0;
}
else if ( should_say_ball )
{
objects[0].score_ = 1000.0;
}
else if ( objects[0].score_ > 0.0 )
{
const BallObject::State & prev_state = wm.ball().stateRecord().front();
double angle_diff = ( wm.ball().vel().th() - prev_state.vel_.th() ).abs();
double prev_speed = prev_state.vel_.r();
double cur_speed = wm.ball().vel().r();
if ( cur_speed > prev_speed + 0.1
|| ( prev_speed > 0.5
&& cur_speed < prev_speed * ServerParam::i().ballDecay() * 0.5 )
|| ( prev_speed > 0.5 // Magic Number
&& angle_diff > 20.0 ) ) // Magic Number
{
objects[0].score_ = 1000.0;
}
else
{
objects[0].score_ *= 0.5;
}
}
//
// players:
// 1. set illegal value if player's uniform number has not been seen directry.
// 2. reduced the priority based on the distance from ball
//
{
const double variance = 30.0; // Magic Number
const double x_rate = 1.0; // Magic Number
const double y_rate = 0.5; // Magic Number
const int min_step = std::min( std::min( wm.interceptTable()->opponentReachCycle(),
wm.interceptTable()->teammateReachCycle() ),
wm.interceptTable()->selfReachCycle() );
const Vector2D ball_pos = wm.ball().inertiaPoint( min_step );
for ( int unum = 1; unum <= 11; ++unum )
{
{
const AbstractPlayerObject * t = wm.ourPlayer( unum );
if ( ! t
|| t->unumCount() >= 2 )
{
objects[unum].score_ = -65535.0;
}
else
{
double d = distance_from_ball( t, ball_pos, x_rate, y_rate );
objects[unum].score_ *= distance_rate( d, variance );
objects[unum].score_ *= std::pow( 0.3, t->unumCount() );
objects[unum].player_ = t;
}
}
{
const AbstractPlayerObject * o = wm.theirPlayer( unum );
if ( ! o
|| o->unumCount() >= 2 )
{
objects[unum + 11].score_ = -65535.0;
}
else
{
double d = distance_from_ball( o, ball_pos, x_rate, y_rate );
objects[unum + 11].score_ *= distance_rate( d, variance );
objects[unum].score_ *= std::pow( 0.3, o->unumCount() );
objects[unum + 11].player_ = o;
}
}
}
}
//
// erase illegal(unseen) objects
//
objects.erase( std::remove_if( objects.begin(),
objects.end(),
ObjectScore::IllegalChecker() ),
objects.end() );
//
// sorted by priority score
//
std::sort( objects.begin(), objects.end(),
ObjectScore::Compare() );
#ifdef DEBUG_PRINT
for ( std::vector< ObjectScore >::const_iterator it = objects.begin();
it != objects.end();
++it )
{
// if ( it->score_ < 0.0 ) break;
dlog.addText( Logger::COMMUNICATION,
__FILE__": rated score: %s %d = %f",
( it->number_ == 0 ? "ball" : it->number_ <= 11 ? "teammate" : "opponent" ),
( it->number_ <= 11 ? it->number_ : it->number_ - 11 ),
it->score_ );
}
#endif
bool can_send_ball = false;
bool send_ball_and_player = false;
std::vector< ObjectScore > send_players;
for ( std::vector< ObjectScore >::iterator it = objects.begin();
it != objects.end();
++it )
{
if ( it->number_ == 0 )
{
can_send_ball = true;
if ( send_players.size() == 1 )
{
send_ball_and_player = true;
break;
}
}
else
{
send_players.push_back( *it );
if ( can_send_ball )
{
send_ball_and_player = true;
break;
}
if ( send_players.size() >= 3 )
{
break;
}
}
}
if ( should_say_ball )
{
can_send_ball = true;
}
Vector2D ball_vel = agent->effector().queuedNextBallVel();
if ( wm.self().isKickable()
&& agent->effector().queuedNextBallKickable() )
{
// invalidate ball velocity
ball_vel.assign( 0.0, 0.0 );
dlog.addText( Logger::COMMUNICATION,
__FILE__": (sayBallAndPlayers) next cycle kickable." );
}
if ( wm.existKickableOpponent()
|| wm.existKickableTeammate() )
{
ball_vel.assign( 0.0, 0.0 );
}
//
// send ball only
//
if ( can_send_ball
&& ! send_ball_and_player
&& available_len >= BallMessage::slength() )
{
if ( available_len >= BallPlayerMessage::slength() )
{
agent->addSayMessage( new BallPlayerMessage( agent->effector().queuedNextBallPos(),
ball_vel,
wm.self().unum(),
agent->effector().queuedNextSelfPos(),
agent->effector().queuedNextSelfBody() ) );
updatePlayerSendTime( wm, wm.ourSide(), wm.self().unum() );
}
else
{
agent->addSayMessage( new BallMessage( agent->effector().queuedNextBallPos(),
ball_vel ) );
}
M_ball_send_time = wm.time();
dlog.addText( Logger::COMMUNICATION,
__FILE__": (sayBallAndPlayers) only ball" );
return true;
}
//
// send ball and player
//
if ( send_ball_and_player )
{
if ( should_say_goalie
&& available_len >= BallGoalieMessage::slength() )
{
const PlayerObject * goalie = wm.getOpponentGoalie();
agent->addSayMessage( new BallGoalieMessage( agent->effector().queuedNextBallPos(),
ball_vel,
goalie->pos() + goalie->vel(),
goalie->body() ) );
M_ball_send_time = wm.time();
updatePlayerSendTime( wm, goalie->side(), goalie->unum() );
dlog.addText( Logger::COMMUNICATION,
__FILE__": (sayBallAndPlayers) ball and goalie" );
return true;
}
if ( available_len >= BallPlayerMessage::slength() )
{
const AbstractPlayerObject * p0 = send_players[0].player_;
if ( p0->unum() == wm.self().unum() )
{
agent->addSayMessage( new BallPlayerMessage( agent->effector().queuedNextBallPos(),
ball_vel,
wm.self().unum(),
agent->effector().queuedNextSelfPos(),
agent->effector().queuedNextSelfBody() ) );
}
else
{
agent->addSayMessage( new BallPlayerMessage( agent->effector().queuedNextBallPos(),
ball_vel,
send_players[0].number_,
p0->pos() + p0->vel(),
p0->body() ) );
}
M_ball_send_time = wm.time();
updatePlayerSendTime( wm, p0->side(), p0->unum() );
dlog.addText( Logger::COMMUNICATION,
__FILE__": (sayBallAndPlayers) ball and player %c%d",
p0->side() == wm.ourSide() ? 'T' : 'O', p0->unum() );
return true;
}
}
//
// send players
//
if ( wm.ball().pos().x > 34.0
&& wm.ball().pos().absY() < 20.0 )
{
const PlayerObject * goalie = wm.getOpponentGoalie();
if ( goalie
&& goalie->seenPosCount() == 0
&& goalie->bodyCount() == 0
&& goalie->pos().x > 53.0 - 16.0
&& goalie->pos().absY() < 20.0
&& goalie->unum() != Unum_Unknown
&& goalie->distFromSelf() < 25.0 )
{
if ( available_len >= GoalieAndPlayerMessage::slength() )
{
const AbstractPlayerObject * player = static_cast< AbstractPlayerObject * >( 0 );
for ( std::vector< ObjectScore >::const_iterator it = send_players.begin();
it != send_players.end();
++it )
{
if ( it->player_->unum() != goalie->unum()
&& it->player_->side() != goalie->side() )
{
player = it->player_;
break;
}
}
if ( player )
{
Vector2D goalie_pos = goalie->pos() + goalie->vel();
goalie_pos.x = bound( 53.0 - 16.0, goalie_pos.x, 52.9 );
goalie_pos.y = bound( -19.9, goalie_pos.y, +19.9 );
agent->addSayMessage( new GoalieAndPlayerMessage( goalie->unum(),
goalie_pos,
goalie->body(),
( player->side() == wm.ourSide()
? player->unum()
: player->unum() + 11 ),
player->pos() + player->vel() ) );
updatePlayerSendTime( wm, goalie->side(), goalie->unum() );
updatePlayerSendTime( wm, player->side(), player->unum() );
dlog.addText( Logger::COMMUNICATION,
__FILE__": say goalie and player: goalie=%d (%.2f %.2f) body=%.1f,"
" player=%s[%d] (%.2f %.2f)",
goalie->unum(),
goalie->pos().x,
goalie->pos().y,
goalie->body().degree(),
( player->side() == wm.ourSide() ? "teammate" : "opponent" ),
player->unum() );
return true;
}
}
if ( available_len >= GoalieMessage::slength() )