/
strats.py
1126 lines (1040 loc) · 40.3 KB
/
strats.py
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
# coding=utf-8
from soccersimulator import Vector2D, SoccerBattle, SoccerPlayer, SoccerTeam, SoccerAction, SoccerStrategy, SoccerState
from soccersimulator import PygletObserver,ConsoleListener,LogListener
from soccersimulator import PLAYER_RADIUS, BALL_RADIUS, GAME_WIDTH, GAME_HEIGHT, GAME_GOAL_HEIGHT
import apprentissage
import outils
import random
import os
import pickle
import pdb
'''
Stratégie Vide
Le joueur ne fait rien
'''
class VideS(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return SoccerAction()
def create_strategy(self):
return VideS()
'''
Stratégie de mouvement
Se déplace vers un point indiqué
'''
class AllerVersLoc(SoccerStrategy):
def __init__(self, localisation):
self.loc = localisation
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
mouvement = self.loc - player.position
return SoccerAction(mouvement, Vector2D(0,0))
def create_strategy(self):
return AllerVersLoc()
'''
Stratégie de mouvement
Le joueur se déplacera vers le point indiqué
'''
class AllerVersBallon(SoccerStrategy):
def __init__(self):
self.strat= AllerVersLoc(Vector2D())
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
self.strat.loc= state.ball.position + state.ball.speed
#mouvement = state.ball.position - player.position
return self.strat.compute_strategy(state,player,teamid)
def create_strategy(self):
return AllerVersBallon()
"""
Stratégie de mouvement
Le joueur se place entre le ballon et son but
"""
class AllerButBallon(SoccerStrategy):
def __init__(self):
self.strat= AllerVersLoc(Vector2D())
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
self.strat.loc = state.ball.position + state.get_goal_center(teamid)
self.strat.loc.product = self.strat.loc.product(0.5)
#mouvement = state.ball.position - player.position
return self.strat.compute_strategy(state,player,teamid)
def create_strategy(self):
return AllerButBallon()
"""
Stratégie de tir
Le joueur frappe le ballon en face de lui dans un angle aléatoire
"""
class Degagement(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
if(test.canshoot()):
tir = Vector2D.create_polar(player.angle + 2.5, 100)
else:
tir = Vector2D(0,0)
return SoccerAction(Vector2D(0,0), tir)
def create_strategy(self):
return Degagement()
"""
Stratégie de tir
Le joueur dribble de façon imprévisible
"""
class Dribble(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
self.strat.locpass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
dri = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
test = Outils(state, teamid, player)
if(test.canshoot()):
drib = Vector2D.create_polar(dri.angle + random.random()*2-1,1)
else:
drib = Vector2D(0,0)
return SoccerAction(Vector2D(0,0), drib)
def create_strategy(self):
return Dribble()
"""
Stratégie de frappe
Le joueur tire vers le but de façon vicieuse
"""
class Tirv(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
tir = Vector2D(0,0)
tir = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
if(test.goalsupmid()):
tir.x = tir.x - (GAME_GOAL_HEIGHT / 2) * 0.75
tir.y = tir.y - (GAME_GOAL_HEIGHT / 2)*0.75
else:
tir.x = tir.x + (GAME_GOAL_HEIGHT / 2)*0.75
tir.y = tir.y + (GAME_GOAL_HEIGHT / 2)*0.75
if(test.canshoot()):
return SoccerAction(Vector2D(0,0), tir)
else:
return SoccerAction(Vector2D(0,0),Vector2D(0,0))
def create_strategy(self):
return Tirv()
"""
Stratégie de frappe
Le joueur tire vers le but
"""
class Tir(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
tir = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
tir.norm = tir.norm * 0.3
if(test.canshoot()):
return SoccerAction(Vector2D(0,0), tir)
else:
return SoccerAction(Vector2D(0,0),Vector2D(0,0))
def create_strategy(self):
return Tir()
'''
Stratégie de passe
Le joueur fait une passe
'''
class Passe(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
tir = test.moncopain() - player.position
tir.norm = tir.norm * 2
if(test.canshoot()):
return SoccerAction(Vector2D(0,0), tir)
else:
return SoccerAction(Vector2D(0,0),Vector2D(0,0))
def create_strategy(self):
return Tir()
"""
Fusionne deux starts, une de tir et une de mouvement
"""
class CompoStrat(SoccerStrategy):
def __init__(self, strat1, strat2):
self.s1 = strat1
self.s2 = strat2
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.s1.begin_battles(state, count, max_step)
self.s2.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
move = self.s1.compute_strategy(state, player, teamid)
shot = self.s2.compute_strategy(state, player, teamid)
return SoccerAction(move.acceleration, shot.shoot)
def create_strategy(self):
return CompoStrat()
"""
Stratégie fonceur
Utilise stratégie AllerVersBallon
"""
class Fonceur(SoccerStrategy):
def __init__(self):
self.fonceur = CompoStrat(AllerVersBallon(), Tirv())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.fonceur.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.fonceur.compute_strategy(state,player,teamid)
def create_strategy(self):
return Fonceur()
"""
Stratégie défenseur
Se place entre le ballon et le but et dégage le ballon
"""
class Defonceur(SoccerStrategy):
def __init__(self):
self.defense = CompoStrat(AllerButBallon(), Degagement())
self.urgence = Fonceur()
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.urgence.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
distance = state.ball.position - player.position
if(distance.norm < 10):
return self.urgence.compute_strategy(state,player,teamid)
else:
return self.defense.compute_strategy(state,player,teamid)
def create_strategy(self):
return Defenceur()
"""
Stratégie défenseurv0.1
Se place entre le ballon et le but et dégage le ballon
"""
class Defenseur(SoccerStrategy):
def __init__(self):
self.defense = CompoStrat(AllerButBallon(), Degagement())
self.urgence = Intercepteur()
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
out = Outils(state, teamid, player)
distance = state.ball.position - state.get_goal_center(teamid)
ball = state.ball.position - player.position
if(out.nbadvbalbut(False) <= 1):
return self.urgence.compute_strategy(state,player,teamid)
else:
return self.defense.compute_strategy(state,player,teamid)
def create_strategy(self):
return Defenseur()
"""
Strategie dribbleur
Essaye d'avoir un comportement imprévisible
"""
class Dribbleur(SoccerStrategy):
def __init__(self):
self.drib = CompoStrat(AllerVersBallon(), Dribble())
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.drib.compute_strategy(state,player,teamid)
def create_strategy(self):
return Dribbleur()
"""
Strategie interception
Se dirige devant le ballon
"""
class Interception(SoccerStrategy):
def __init__(self):
self.strat = AllerVersLoc(Vector2D())
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
goalcen = state.get_goal_center(teamid)
goalcen = Vector2D(goalcen.x *0.1 + state.ball.position.x*0.9,goalcen.y *0.1 + state.ball.position.y*0.9)
self.strat.loc = goalcen
return self.strat.compute_strategy(state,player,teamid)
def create_strategy(self):
return Interception()
"""
Srategie d'esquive
Tir de manière à éviter les adversaires
"""
class Esquive(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
out = Outils(state, teamid, player)
goal = state.get_goal_center(outils.IDTeamOp(teamid))
yadv = out.jpro().y
yme = player.position.y
if(yadv > yme):
dire = goal - player.position
go = Vector2D.create_polar(dire.angle - 0.35, 2)
else:
dire = goal - player.position
go = Vector2D.create_polar(dire.angle + 0.35, 2)
if(out.canshoot()):
return SoccerAction(Vector2D(0,0), go)
else:
return SoccerAction(Vector2D(0,0), Vector2D(0,0))
def create_strategy(self):
return Esquive()
"""
Stratégie de Dribble avancé
Le joueur évite l'adversaire et se déplace de façon adapté
"""
class Attaquant(SoccerStrategy):
def __init__(self):
self.but = CompoStrat(AllerVersBallon(), Tir())
self.atck = CompoStrat(AllerVersBallon(), Esquive())
self.defen = CompoStrat(AllerVersBallon(), Degagement())
self.aballon = 0
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.aballon = 0
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
# a = self.test.distballon(self.test, teamid, 1) #TEST DEBUG
dist = state.ball.position - player.position
but = state.get_goal_center(outils.IDTeamOp(teamid)).x - player.position.x
if(abs(but) < 15):
return self.but.compute_strategy(state, player, teamid)
if(dist.norm > 20):
self.aballon = 0
if(dist.norm < 3 or self.aballon):
self.aballon = 1
return self.atck.compute_strategy(state, player, teamid)
else:
return self.defen.compute_strategy(state,player,teamid)
def create_strategy(self):
return Intercepteur()
"""
Strategie d'intercepteur
Se place devant le ballon et le prend au joueur adverse
"""
class Intercepteur(SoccerStrategy):
def __init__(self):
self.inter = CompoStrat(Interception(), Degagement())
self.atck = CompoStrat(AllerVersBallon(), Esquive())
self.fonceur = CompoStrat(AllerVersBallon(), Tirv())
self.attente = random.random() * 100 + 150
def start_battle(self,state):
self.attente = random.random() * 100 + 150
def begin_battles(self,state,count,max_step):
self.attente = random.random() * 100 + 150
self.inter.begin_battles(state, count, max_step)
self.atck.begin_battles(state, count, max_step)
self.fonceur.begin_battles(state, count, max_step)
self.attente = random.random() * 100 + 150
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
dist = state.ball.position - player.position
but = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
butx = state.get_goal_center(outils.IDTeamOp(teamid)).x - player.position.x
if(test.nbadvbalbut(True) == 1 and self.attente >= 1):
self.attente = self.attente - 1
return self.inter.compute_strategy(state, player, teamid)
if((test.nbadvbalbut(True) == 1 or self.attente < 1) and abs(butx) > 15):
return self.atck.compute_strategy(state, player, teamid)
else:
return self.fonceur.compute_strategy(state,player,teamid)
def create_strategy(self):
return Intercepteur()
"""
Strategie d'intercepteur
Se place devant le ballon et le prend au joueur adverse V2.0
"""
class SurIntercepteur(SoccerStrategy):
def __init__(self):
self.inter = CompoStrat(Interception(), Degagement())
self.atck = CompoStrat(AllerVersBallon(), Esquive())
self.fonceur = CompoStrat(AllerVersBallon(), Tirv())
self.attente = random.random() * 100 + 150
def start_battle(self,state):
self.attente = random.random() * 100 + 150
def begin_battles(self,state,count,max_step):
self.attente = random.random() * 100 + 150
self.fonceur.begin_battles(state, count, max_step)
self.inter.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
dist = state.ball.position - player.position
but = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
butx = state.get_goal_center(outils.IDTeamOp(teamid)).x - player.position.x
if(test.nbadvbalbut(True) == 2 and self.attente >= 1):
self.attente = self.attente - 1
return self.inter.compute_strategy(state, player, teamid)
if((test.nbadvbalbut(True) == 1 or self.attente < 1) and abs(butx) > 15):
return self.atck.compute_strategy(state, player, teamid)
else:
return self.fonceur.compute_strategy(state,player,teamid)
def create_strategy(self):
return SurIntercepteur()
'''array
Intercepteur 4v4
'''
class TeamIntercepteur(SoccerStrategy):
def __init__(self):
self.inter = CompoStrat(Interception(), Tirv())
self.atck = CompoStrat(AllerVersBallon(), Esquive())
self.fonceur = CompoStrat(AllerVersBallon(), Tirv())
self.attente = random.random() * 100 + 150
def start_battle(self,state):
self.attente = random.random() * 100 + 150
def begin_battles(self,state,count,max_step):
self.attente = random.random() * 100 + 150
self.fonceur.begin_battles(state, count, max_step)
self.inter.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
test = Outils(state, teamid, player)
dist = state.ball.position - player.position
but = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position
butx = state.get_goal_center(outils.IDTeamOp(teamid)).x - player.position.x
if(test.nbadvbalbut(True) == 4 and self.attente >= 1):
self.attente = self.attente - 1
return self.inter.compute_strategy(state, player, teamid)
if((test.nbadvbalbut(True) >= 1 or self.attente < 1) and abs(butx) > 15):
return self.atck.compute_strategy(state, player, teamid)
else:
return self.fonceur.compute_strategy(state,player,teamid)
def create_strategy(self):
return TeamIntercepteur()
"""
Stratégie de mouvement
Attend un joueur pour lui prendre le ballon
"""
class DefMove(SoccerStrategy):
def __init__(self):
self.defense = CompoStrat(AllerButBallon(), VideS())
self.subti = CompoStrat(Fonceur(), VideS())
self.attente = CompoStrat(VideS(), VideS())
self.aggro = CompoStrat(AllerVersBallon(), Degagement())
self.revanche = CompoStrat(AllerVersBallon(), Tirv())
self.adefendu = False
def start_battle(self,state):
pass
def begin_battles(self, state,count,max_step):
self.adefendu = False
self.subti.begin_battles(state, count, max_step)
self.revanche.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
out = Outils(state, teamid, player)
distance = out.distadvballon()
distball = state.ball.position - player.position
if(self.adefendu):
return self.revanche.compute_strategy(state,player,teamid)
elif(PLAYER_RADIUS + BALL_RADIUS > distball.norm):
self.adefendu = True
return self.aggro.compute_strategy(state,player,teamid)
elif(distance < 3):
return self.subti.compute_strategy(state,player,teamid)
elif(distball.norm < GAME_WIDTH*0.1):
return self.attente.compute_strategy(state, player, teamid)
else:
return self.defense.compute_strategy(state,player,teamid)
def create_strategy(self):
return DefMove()
'''
Stratégie de mouvement
Suit l'allié le plus proche
'''
class Suivre(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
outil = Outils(state, teamid, player)
loc1 = outil.moncopain().copy()
loc2 = state.get_goal_center(teamid).copy()
loc1.scale(0.9)
loc2.scale(0.1)
mouvement = loc1 + loc2 - player.position
return SoccerAction(mouvement, Vector2D(0,0))
def create_strategy(self):
return Suivre()
'''
Stratégie d'attaque
Suit le possesseur du ballon, recupère le ballon s'il le perd
SI EQP BALLON ET + PROCHE => ATK
ELSE => SUIT
'''
class Follow(SoccerStrategy):
def __init__(self):
self.but = CompoStrat(AllerVersBallon(), Tirv())
self.esquive = CompoStrat(AllerVersBallon(), Esquive())
self.suivre = CompoStrat(Suivre(), Degagement())
def start_battle(self,state):
pass
def begin_battles(self, state,count,max_step):
self.but.begin_battles(state, count, max_step)
self.esquive.begin_battles(state, count, max_step)
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
outil = Outils(state, teamid, player)
allie = outil.jproall()
dist = state.ball.position - player.position.copy()
but = state.get_goal_center(outils.IDTeamOp(teamid)) - player.position.copy()
dist2 = state.ball.position - allie
if(not outil.equiperbal()):
but = state.get_goal_center(outils.IDTeamOp(teamid)).x - player.position.copy().x
if(abs(but) < 15 or outil.nbadvbalbut(True) < 1):
return self.but.compute_strategy(state, player, teamid)
else:
return self.esquive.compute_strategy(state, player, teamid)
else:
return self.suivre.compute_strategy(state, player, teamid)
def create_strategy(self):
return Follow()
'''
Stratégie de placement
Se place de façon optimal pour attendre une passe
'''
class Anticipation(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
outil = Outils(state, teamid, player)
loc1 = outil.moncopain().copy()
if(outil.hautterrain() and teamid == 1):
mouvement = loc1 + Vector2D(15,-15) - player.position
elif(outil.hautterrain() and teamid == 2):
mouvement = loc1 + Vector2D(-15,-15) - player.position
elif(not outil.hautterrain() and teamid == 1):
mouvement = loc1 + Vector2D(15,15) - player.position
else:
mouvement = loc1 + Vector2D(-15,15) - player.position
return SoccerAction(mouvement, Vector2D(0,0))
def create_strategy(self):
return Anticipation()
'''
Stratégie de placement
Se place de façon optimal pour attendre une passe
'''
class Contourne(SoccerStrategy):
def __init__(self):
pass
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
outil = Outils(state, teamid, player)
loc1 = outil.moncopain().copy()
if(outil.hautterrain()):
mouvement = state.get_goal_center(outils.IDTeamOp(teamid)) + Vector2D(0,14) - player.position
else:
mouvement = state.get_goal_center(outils.IDTeamOp(teamid)) + Vector2D(0,-14) - player.position
mouvement.norm = mouvement.norm * 0.4
if(outil.canshoot()):
return SoccerAction(Vector2D(0,0), mouvement)
else:
return SoccerAction(Vector2D(0,0), Vector2D(0,0))
def create_strategy(self):
return Contourne()
############################################################################################################################################################
############################################################################################################################################################
############################################################################################################################################################
#STRATEGIE APPRENTISSAGE
############################################################################################################################################################
############################################################################################################################################################
############################################################################################################################################################
'''
Stratégie apprentissage ; Subdivision des fonctions plus haut
'''
class AppFonceur(SoccerStrategy):
def __init__(self):
self.name = "fonceur"
self.fonceur = CompoStrat(AllerVersBallon(), Tir())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.fonceur.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.fonceur.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppFonceur()
class AppDefenseur(SoccerStrategy):
def __init__(self):
self.name = "defenseur"
self.defense = CompoStrat(AllerButBallon(), Degagement())
def start_battle(self,state):
pass
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.defense.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppDefenseur()
class AppSuivre(SoccerStrategy):
def __init__(self):
self.name = "suivre"
self.suivre = CompoStrat(Suivre(), Degagement())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppSuivre()
class AppPasse(SoccerStrategy):
def __init__(self):
self.name = "passe"
self.suivre = CompoStrat(AllerVersBallon(), Passe())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppPasse()
class AppAttente(SoccerStrategy):
def __init__(self):
self.name = "attente"
self.suivre = CompoStrat(Anticipation(), Tir())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppPasse()
class AppContourne(SoccerStrategy):
def __init__(self):
self.name = "contourne"
self.suivre = CompoStrat(AllerVersBallon(), Contourne())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppContourne()
class AppDegagement(SoccerStrategy):
def __init__(self):
self.name = "degagement"
self.suivre = CompoStrat(AllerVersBallon(), Degagement())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppDegagement()
class AppFrappe(SoccerStrategy):
def __init__(self):
self.name = "frappe"
self.suivre = CompoStrat(AllerVersBallon(), Tirv())
def start_battle(self,state):
pass
def begin_battles(self,state,count,max_step):
self.suivre.begin_battles(state, count, max_step)
def finish_battle(self,won):
pass
def compute_strategy(self,state,player,teamid):
return self.suivre.compute_strategy(state,player,teamid)
def create_strategy(self):
return AppDegagement()
############################################################################################################################################################
############################################################################################################################################################
############################################################################################################################################################
#OUTILS
############################################################################################################################################################
############################################################################################################################################################
############################################################################################################################################################
class Outils(SoccerState):
def __init__(self, state, team, player):
self.team = team
self.player = player
self.state = state
'''
Donne la distance de l'adversaire le plus proche au ballon
'''
def distadvballon(self):
dist = 9999
if(self.team == 2):
for p in self.state.team1 :
vec = self.state.ball.position - p.position
dist = min(vec.norm, dist)
else:
for p in self.state.team2 :
vec = self.state.ball.position - p.position
dist = min(vec.norm, dist)
return dist
'''
Donne la distance de l'allié (pas soi) le plus proche au ballon
'''
def distallballon(self):
dist = 9999
if(self.team == 1):
for p in self.state.team1 :
if(self.player.position != p.position):
vec = self.state.ball.position - p.position
dist = min(vec.norm, dist)
else:
for p in self.state.team2 :
if(self.player.position != p.position):
vec = self.state.ball.position - p.position
dist = min(vec.norm, dist)
return dist
'''
Donne le nombre d'adversaire entre la balle et le but (ou le nombre d'allié si adv false)
'''
def nbadvbalbut(self, adv):
nb = 0
if((self.team == 2 and adv) or (self.team == 1 and not adv)):
for p in self.state.team1 :
if(adv):
distun = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distdeux = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.state.ball.position
else:
distun = self.state.get_goal_center(self.team) - p.position
distdeux = self.state.get_goal_center(self.team) - self.state.ball.position
if(distun.norm < distdeux.norm):
nb = nb + 1
else:
for p in self.state.team2 :
if(adv):
distun = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distdeux = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.state.ball.position
else:
distun = self.state.get_goal_center(self.team) - p.position
distdeux = self.state.get_goal_center(self.team) - self.state.ball.position
if(distun.norm < distdeux.norm):
nb = nb+1
return nb
'''
Donne la position du joueur adverse le plus proche de moi et entre moi et mes buts
'''
def jpro(self):
dist = 9999
vec = Vector2D(0,0)
if(self.team == 2):
for p in self.state.team1 :
distmebut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.player.position
distluibut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distmelui = p.position - self.player.position
if(distluibut.norm < distmebut.norm and distmelui.norm < dist):
dist = distmelui.norm
vec = p.position
else:
for p in self.state.team2 :
distmebut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.player.position
distluibut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distmelui = p.position - self.player.position
if(distluibut.norm < distmebut.norm and distmelui.norm < dist):
dist = distmelui.norm
vec = p.position
return vec.copy()
'''
Donne la position du joueur allié le plus proche des buts
'''
def jproall(self):
dist = 9999
vec = Vector2D(0,0)
if(self.team == 1):
for p in self.state.team1 :
if(p.position != self.player.position):
distmebut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.player.position
distluibut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distmelui = p.position - self.player.position
if(distluibut.norm < distmebut.norm and distmelui.norm < dist):
dist = distmelui.norm
vec = p.position
else:
for p in self.state.team2 :
if(p.position != self.player.position):
distmebut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - self.player.position
distluibut = self.state.get_goal_center(outils.IDTeamOp(self.team)) - p.position
distmelui = p.position - self.player.position
if(distluibut.norm < distmebut.norm and distmelui.norm < dist):
dist = distmelui.norm
vec = p.position
return vec.copy()
'''
Rend la position du joueur adverse le plus proche du ballon
'''
def jball(self):
dist = 9999
vec = Vector2D(0,0)
if(self.team == 2):
for p in self.state.team1 :
distlui = self.state.ball.position - p.position
if(distlui.norm < dist):
dist = distlui.norm
vec = p.position
else:
for p in self.state.team2 :
distlui = self.state.ball.position - p.position
if(distlui.norm < dist):
dist = distlui.norm
vec = p.position
return vec.copy()
'''
Rend la position du joueur allié le plus proche du ballon
'''
def jballall(self):
dist = 9999
vec = Vector2D(0,0)
if(self.team == 1):
for p in self.state.team1 :
if(self.player.id != p.id):
distlui = self.state.ball.position - p.position
if(distlui.norm < dist):
dist = distlui.norm
vec = p.position
else:
for p in self.state.team2 :
if(self.player.id != p.id):
distlui = self.state.ball.position - p.position
if(distlui.norm < dist):
dist = distlui.norm
vec = p.position
return vec.copy()
'''
Rend True si son équipe a le ballon, False sinon
'''
def equipeballo(self):
dist = 9999
for p in self.state.team1 :
distballon = self.state.ball.position - p.position
if(distballon.norm < dist):
dist1 = distballon.norm
dist = dist1
dist = 9999
for q in self.state.team2 :
distballon2 = self.state.ball.position - q.position
if(distballon2.norm < dist):
dist2 = distballon2.norm
dist = dist2
if(((dist1 < dist2 and self.team == 1) or (dist1 > dist2 and self.team == 2)) and (dist2 < GAME_WIDTH * 0.09 or dist1 < GAME_WIDTH * 0.09)):
return True
else:
return False
'''
Rend True si son équipe (pas lui) a le ballon, False sinon
'''
def equiperbal(self):
dist = 9999
for p in self.state.team1 :
if(p.position != self.player.position):
distballon = self.state.ball.position - p.position
if(distballon.norm < dist):
dist1 = distballon.norm
dist = dist1
dist = 9999
for q in self.state.team2 :
if(q.position != self.player.position):
distballon2 = self.state.ball.position - q.position
if(distballon2.norm < dist):
dist2 = distballon2.norm
dist = dist2
if(((dist1 < dist2 and self.team == 1) or (dist1 > dist2 and self.team == 2)) and (dist2 < GAME_WIDTH * 0.09 or dist1 < GAME_WIDTH * 0.09)):
return True
else:
return False
'''
Rend True si le joueur peut tirer (par défault moi)
'''
def canshoot(self, player=None):
if not player:
player = self.player
dist = self.state.ball.position - player.position