cuju-engine 0.3.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cuju/__init__.py +217 -0
- cuju/__main__.py +262 -0
- cuju/_batch/__init__.py +4 -0
- cuju/_batch/allocator.py +52 -0
- cuju/_batch/assets.py +68 -0
- cuju/_batch/environment.py +1890 -0
- cuju/_batch/frame_stack.py +114 -0
- cuju/_batch/library.py +190 -0
- cuju/_batch/log.py +103 -0
- cuju/_batch/memory.py +70 -0
- cuju/_batch/operators.py +55 -0
- cuju/_batch/pinned.py +39 -0
- cuju/_batch/pixels.py +207 -0
- cuju/_batch/state.py +77 -0
- cuju/_checksums.py +60 -0
- cuju/_cli_contract.py +244 -0
- cuju/_cli_parsers.py +191 -0
- cuju/_completion.py +277 -0
- cuju/_dump_to_txt.py +66 -0
- cuju/_grf_config.py +331 -0
- cuju/_library_path.py +141 -0
- cuju/_media.py +113 -0
- cuju/_pixels.py +276 -0
- cuju/_plugins.py +106 -0
- cuju/_python_players.py +224 -0
- cuju/_replay_command.py +109 -0
- cuju/_replay_player.py +42 -0
- cuju/_replays.py +414 -0
- cuju/_script_helpers.py +109 -0
- cuju/_version.py +6 -0
- cuju/action_log.py +371 -0
- cuju/check.py +596 -0
- cuju/cuju_cpu.dll +0 -0
- cuju/cuju_cuda.dll +0 -0
- cuju/cuju_render.dll +0 -0
- cuju/data/SHA256SUMS +20 -0
- cuju/data/animations.bin +0 -0
- cuju/data/assets/11_vs_11_competition_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_easy_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_hard_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_kaggle_l0_r0.cjb +0 -0
- cuju/data/assets/11_vs_11_stochastic_l0_r0.cjb +0 -0
- cuju/data/assets/1_vs_1_easy_l0_r0.cjb +0 -0
- cuju/data/assets/5_vs_5_l0_r0.cjb +0 -0
- cuju/data/assets/academy_3_vs_1_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_corner_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_easy_l0_r0.cjb +0 -0
- cuju/data/assets/academy_counterattack_hard_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_close_l0_r0.cjb +0 -0
- cuju/data/assets/academy_empty_goal_l0_r0.cjb +0 -0
- cuju/data/assets/academy_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_pass_and_shoot_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_l0_r0.cjb +0 -0
- cuju/data/assets/academy_run_to_score_with_keeper_l0_r0.cjb +0 -0
- cuju/data/assets/academy_single_goal_versus_lazy_l0_r0.cjb +0 -0
- cuju/data/assets/test_example_multiagent_l0_r0.cjb +0 -0
- cuju/data/digests.json +186 -0
- cuju/data/render_digests.json.z +0 -0
- cuju/data/scenarios/11_vs_11_competition.py +45 -0
- cuju/data/scenarios/11_vs_11_easy_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_hard_stochastic.py +44 -0
- cuju/data/scenarios/11_vs_11_kaggle.py +46 -0
- cuju/data/scenarios/11_vs_11_stochastic.py +44 -0
- cuju/data/scenarios/1_vs_1_easy.py +25 -0
- cuju/data/scenarios/5_vs_5.py +33 -0
- cuju/data/scenarios/academy_3_vs_1_with_keeper.py +28 -0
- cuju/data/scenarios/academy_corner.py +45 -0
- cuju/data/scenarios/academy_counterattack_easy.py +45 -0
- cuju/data/scenarios/academy_counterattack_hard.py +45 -0
- cuju/data/scenarios/academy_empty_goal.py +25 -0
- cuju/data/scenarios/academy_empty_goal_close.py +25 -0
- cuju/data/scenarios/academy_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_pass_and_shoot_with_keeper.py +27 -0
- cuju/data/scenarios/academy_run_to_score.py +30 -0
- cuju/data/scenarios/academy_run_to_score_with_keeper.py +30 -0
- cuju/data/scenarios/academy_single_goal_versus_lazy.py +49 -0
- cuju/data/scenarios/test_example_multiagent.py +28 -0
- cuju/dumps.py +942 -0
- cuju/engine_status.py +106 -0
- cuju/env.py +1168 -0
- cuju/errors.py +108 -0
- cuju/gfootball/GRF_SOURCES +86 -0
- cuju/gfootball/__init__.py +60 -0
- cuju/gfootball/_cv2.py +43 -0
- cuju/gfootball/_gym/__init__.py +88 -0
- cuju/gfootball/_gym/_vendored/GYM_SOURCES +22 -0
- cuju/gfootball/_gym/_vendored/__init__.py +43 -0
- cuju/gfootball/_gym/_vendored/core.py +361 -0
- cuju/gfootball/_gym/_vendored/envs/__init__.py +19 -0
- cuju/gfootball/_gym/_vendored/envs/registration.py +284 -0
- cuju/gfootball/_gym/_vendored/error.py +230 -0
- cuju/gfootball/_gym/_vendored/logger.py +56 -0
- cuju/gfootball/_gym/_vendored/spaces/__init__.py +30 -0
- cuju/gfootball/_gym/_vendored/spaces/box.py +171 -0
- cuju/gfootball/_gym/_vendored/spaces/dict.py +162 -0
- cuju/gfootball/_gym/_vendored/spaces/discrete.py +41 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_binary.py +61 -0
- cuju/gfootball/_gym/_vendored/spaces/multi_discrete.py +77 -0
- cuju/gfootball/_gym/_vendored/spaces/space.py +94 -0
- cuju/gfootball/_gym/_vendored/spaces/tuple.py +96 -0
- cuju/gfootball/_gym/_vendored/spaces/utils.py +228 -0
- cuju/gfootball/_gym/_vendored/utils/__init__.py +13 -0
- cuju/gfootball/_gym/_vendored/utils/closer.py +72 -0
- cuju/gfootball/_gym/_vendored/utils/colorize.py +36 -0
- cuju/gfootball/_gym/_vendored/utils/ezpickle.py +37 -0
- cuju/gfootball/_gym/_vendored/utils/json_utils.py +27 -0
- cuju/gfootball/_gym/_vendored/utils/seeding.py +101 -0
- cuju/gfootball/_gym/_vendored/version.py +5 -0
- cuju/gfootball/_gym/_vendored/wrappers/__init__.py +8 -0
- cuju/gfootball/_gym/_vendored/wrappers/order_enforcing.py +20 -0
- cuju/gfootball/_gym/_vendored/wrappers/time_limit.py +31 -0
- cuju/gfootball/dump_to_txt.py +49 -0
- cuju/gfootball/dump_to_video.py +33 -0
- cuju/gfootball/env/__init__.py +271 -0
- cuju/gfootball/env/config.py +163 -0
- cuju/gfootball/env/constants.py +33 -0
- cuju/gfootball/env/controller_base.py +114 -0
- cuju/gfootball/env/event_queue.py +54 -0
- cuju/gfootball/env/football_action_set.py +220 -0
- cuju/gfootball/env/football_action_set_test.py +76 -0
- cuju/gfootball/env/football_env.py +225 -0
- cuju/gfootball/env/football_env_core.py +447 -0
- cuju/gfootball/env/football_env_test.py +692 -0
- cuju/gfootball/env/gym_test.py +47 -0
- cuju/gfootball/env/observation_preprocessing.py +94 -0
- cuju/gfootball/env/observation_processor.py +526 -0
- cuju/gfootball/env/observation_rotation.py +163 -0
- cuju/gfootball/env/observation_rotation_test.py +84 -0
- cuju/gfootball/env/player_base.py +46 -0
- cuju/gfootball/env/players/__init__.py +16 -0
- cuju/gfootball/env/players/agent.py +39 -0
- cuju/gfootball/env/players/bot.py +217 -0
- cuju/gfootball/env/players/gamepad.py +81 -0
- cuju/gfootball/env/players/keyboard.py +72 -0
- cuju/gfootball/env/players/lazy.py +31 -0
- cuju/gfootball/env/players/ppo2_cnn.py +119 -0
- cuju/gfootball/env/players/replay.py +40 -0
- cuju/gfootball/env/remote_football_env.py +189 -0
- cuju/gfootball/env/scenario_builder.py +125 -0
- cuju/gfootball/env/script_helpers.py +130 -0
- cuju/gfootball/env/script_helpers_test.py +107 -0
- cuju/gfootball/env/wrappers.py +440 -0
- cuju/gfootball/env/wrappers_test.py +150 -0
- cuju/gfootball/eval_server/__init__.py +17 -0
- cuju/gfootball/eval_server/config.py +30 -0
- cuju/gfootball/eval_server/example_client.py +107 -0
- cuju/gfootball/eval_server/proto/game_server.proto +62 -0
- cuju/gfootball/eval_server/proto/game_server_pb2.py +501 -0
- cuju/gfootball/eval_server/proto/game_server_pb2_grpc.py +113 -0
- cuju/gfootball/eval_server/proto/master.proto +25 -0
- cuju/gfootball/eval_server/proto/master_pb2.py +184 -0
- cuju/gfootball/eval_server/proto/master_pb2_grpc.py +62 -0
- cuju/gfootball/eval_server/utils.py +52 -0
- cuju/gfootball/examples/__init__.py +16 -0
- cuju/gfootball/examples/models.py +66 -0
- cuju/gfootball/examples/run_multiagent_rllib.py +140 -0
- cuju/gfootball/examples/run_ppo2.py +123 -0
- cuju/gfootball/play_game.py +73 -0
- cuju/gfootball/replay.py +43 -0
- cuju/gfootball/scenarios/11_vs_11_competition.py +59 -0
- cuju/gfootball/scenarios/11_vs_11_easy_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_hard_stochastic.py +57 -0
- cuju/gfootball/scenarios/11_vs_11_kaggle.py +60 -0
- cuju/gfootball/scenarios/11_vs_11_stochastic.py +57 -0
- cuju/gfootball/scenarios/1_vs_1_easy.py +39 -0
- cuju/gfootball/scenarios/5_vs_5.py +46 -0
- cuju/gfootball/scenarios/__init__.py +31 -0
- cuju/gfootball/scenarios/academy_3_vs_1_with_keeper.py +42 -0
- cuju/gfootball/scenarios/academy_corner.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_easy.py +59 -0
- cuju/gfootball/scenarios/academy_counterattack_hard.py +59 -0
- cuju/gfootball/scenarios/academy_empty_goal.py +39 -0
- cuju/gfootball/scenarios/academy_empty_goal_close.py +39 -0
- cuju/gfootball/scenarios/academy_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_pass_and_shoot_with_keeper.py +41 -0
- cuju/gfootball/scenarios/academy_run_to_score.py +44 -0
- cuju/gfootball/scenarios/academy_run_to_score_with_keeper.py +44 -0
- cuju/gfootball/scenarios/academy_single_goal_versus_lazy.py +63 -0
- cuju/gfootball/scenarios/test_example_multiagent.py +42 -0
- cuju/gfootball/scenarios/tests/11_vs_11_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_easy_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_hard_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_deterministic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_11_single_goal_stochastic.py +58 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_deterministic.py +47 -0
- cuju/gfootball/scenarios/tests/11_vs_4_offence_stochastic.py +47 -0
- cuju/gfootball/scenarios/tests/__init__.py +31 -0
- cuju/gfootball/scenarios/tests/corner_test.py +38 -0
- cuju/gfootball/scenarios/tests/goal_test.py +35 -0
- cuju/gfootball/scenarios/tests/keeper_test.py +38 -0
- cuju/gfootball/scenarios/tests/kickoff_for_goal_loosing_team_test.py +34 -0
- cuju/gfootball/scenarios/tests/multiagent_wrapper.py +60 -0
- cuju/gfootball/scenarios/tests/offside_test.py +39 -0
- cuju/gfootball/scenarios/tests/penalty.py +34 -0
- cuju/gfootball/scenarios/tests/second_half.py +33 -0
- cuju/gfootball/scenarios/tests/symmetric.py +53 -0
- cuju/gfootball_engine/__init__.py +31 -0
- cuju/gfootball_engine/_gameplayfootball.py +1624 -0
- cuju/gfootball_engine/_native.py +193 -0
- cuju/gfootball_engine/_pickle_names.py +114 -0
- cuju/gfootball_engine/_render_hooks.py +156 -0
- cuju/kaggle.py +425 -0
- cuju/multi_device.py +401 -0
- cuju/players.py +260 -0
- cuju/raw.py +93 -0
- cuju/scenarios.py +661 -0
- cuju/state.py +211 -0
- cuju/wrappers.py +325 -0
- cuju_engine-0.3.0.dist-info/METADATA +325 -0
- cuju_engine-0.3.0.dist-info/RECORD +222 -0
- cuju_engine-0.3.0.dist-info/WHEEL +5 -0
- cuju_engine-0.3.0.dist-info/entry_points.txt +4 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSE +202 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +31 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/BSL-1.0.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/LGPL-2.1-or-later.txt +502 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-Mesa.txt +26 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT-gym.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/MIT.txt +23 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/SunPro.txt +8 -0
- cuju_engine-0.3.0.dist-info/licenses/LICENSES/Zlib.txt +20 -0
- cuju_engine-0.3.0.dist-info/licenses/NOTICE +56 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Rotate observation by 180 degrees.
|
|
19
|
+
|
|
20
|
+
Context: Agents are trained to play left to right.
|
|
21
|
+
If one needs the same agent play right to left, a simple way
|
|
22
|
+
is to rotate the observation by 180 degrees and pass this representation
|
|
23
|
+
to the agent.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import absolute_import
|
|
27
|
+
from __future__ import division
|
|
28
|
+
from __future__ import print_function
|
|
29
|
+
|
|
30
|
+
from cuju.gfootball.env import football_action_set
|
|
31
|
+
|
|
32
|
+
import numpy as np
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def rotate_3d_point(point):
|
|
36
|
+
"""Rotate 3d point around the center of the field.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
points: [x, y, z] point.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
The rotated points.
|
|
43
|
+
"""
|
|
44
|
+
# This assumes the center of the field is the origin: (0, 0)
|
|
45
|
+
return np.array([-point[0], -point[1], point[2]])
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def rotate_points(points):
|
|
49
|
+
"""Rotate the points around the center of the field.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
points: Numpy array holding one or several points.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
The rotated points.
|
|
56
|
+
"""
|
|
57
|
+
# This assumes the center of the field is the origin: (0, 0)
|
|
58
|
+
return -points
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def rotate_sticky_actions(sticky_actions_state, config):
|
|
62
|
+
"""Rotate the sticky bits of directional actions.
|
|
63
|
+
|
|
64
|
+
This is used to make a policy believe it is playing from left to right
|
|
65
|
+
although it is actually playing from right to left.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
sticky_actions_state: Array of bits corresponding to the active actions.
|
|
69
|
+
config: config used by the environment
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
Array of bits corresponding to the same active actions for a player
|
|
73
|
+
who would play from the opposite side.
|
|
74
|
+
"""
|
|
75
|
+
sticky_actions = football_action_set.get_sticky_actions(config)
|
|
76
|
+
assert len(sticky_actions) == len(sticky_actions_state), len(sticky_actions)
|
|
77
|
+
action_to_state = {}
|
|
78
|
+
for i in range(len(sticky_actions)):
|
|
79
|
+
action_to_state[sticky_actions[i]] = sticky_actions_state[i]
|
|
80
|
+
rotated_sticky_actions = []
|
|
81
|
+
for i in range(len(sticky_actions)):
|
|
82
|
+
rotated_sticky_actions.append(action_to_state[flip_single_action(
|
|
83
|
+
sticky_actions[i], config)])
|
|
84
|
+
return rotated_sticky_actions
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def flip_team_observation(observation, result, config, from_team, to_team):
|
|
88
|
+
"""Rotates team-specific observations."""
|
|
89
|
+
result['{}_team'.format(to_team)] = rotate_points(
|
|
90
|
+
observation['{}_team'.format(from_team)])
|
|
91
|
+
result['{}_team_direction'.format(to_team)] = rotate_points(
|
|
92
|
+
observation['{}_team_direction'.format(from_team)])
|
|
93
|
+
result['{}_team_tired_factor'.format(to_team)] = observation[
|
|
94
|
+
'{}_team_tired_factor'.format(from_team)]
|
|
95
|
+
result['{}_team_active'.format(to_team)] = observation[
|
|
96
|
+
'{}_team_active'.format(from_team)]
|
|
97
|
+
result['{}_team_yellow_card'.format(to_team)] = observation[
|
|
98
|
+
'{}_team_yellow_card'.format(from_team)]
|
|
99
|
+
result['{}_team_roles'.format(to_team)] = observation['{}_team_roles'.format(
|
|
100
|
+
from_team)]
|
|
101
|
+
result['{}_team_active'.format(to_team)] = observation[
|
|
102
|
+
'{}_team_active'.format(from_team)]
|
|
103
|
+
result['{}_team_designated_player'.format(to_team)] = observation[
|
|
104
|
+
'{}_team_designated_player'.format(from_team)]
|
|
105
|
+
if '{}_agent_controlled_player'.format(from_team) in observation:
|
|
106
|
+
result['{}_agent_controlled_player'.format(to_team)] = observation[
|
|
107
|
+
'{}_agent_controlled_player'.format(from_team)]
|
|
108
|
+
if '{}_agent_sticky_actions'.format(from_team) in observation:
|
|
109
|
+
result['{}_agent_sticky_actions'.format(to_team)] = [
|
|
110
|
+
rotate_sticky_actions(sticky, config)
|
|
111
|
+
for sticky in observation['{}_agent_sticky_actions'.format(from_team)]
|
|
112
|
+
]
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def flip_observation(observation, config):
|
|
116
|
+
"""Observation corresponding to the field rotated by 180 degrees."""
|
|
117
|
+
flipped_observation = {}
|
|
118
|
+
flipped_observation['ball'] = rotate_3d_point(observation['ball'])
|
|
119
|
+
flipped_observation['ball_direction'] = rotate_3d_point(
|
|
120
|
+
observation['ball_direction'])
|
|
121
|
+
flipped_observation['ball_rotation'] = observation['ball_rotation']
|
|
122
|
+
flipped_observation['ball_owned_team'] = 1 - observation[
|
|
123
|
+
'ball_owned_team'] if observation['ball_owned_team'] > -1 else -1
|
|
124
|
+
flipped_observation['ball_owned_player'] = observation['ball_owned_player']
|
|
125
|
+
flipped_observation['score'] = [
|
|
126
|
+
observation['score'][1], observation['score'][0]
|
|
127
|
+
]
|
|
128
|
+
flipped_observation['game_mode'] = observation['game_mode']
|
|
129
|
+
flipped_observation['steps_left'] = observation['steps_left']
|
|
130
|
+
flip_team_observation(observation, flipped_observation, config, 'left',
|
|
131
|
+
'right')
|
|
132
|
+
flip_team_observation(observation, flipped_observation, config, 'right',
|
|
133
|
+
'left')
|
|
134
|
+
return flipped_observation
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def flip_single_action(action, config):
|
|
138
|
+
"""Actions corresponding to the field rotated by 180 degrees."""
|
|
139
|
+
action = football_action_set.named_action_from_action_set(
|
|
140
|
+
football_action_set.get_action_set(config), action)
|
|
141
|
+
if action == football_action_set.action_left:
|
|
142
|
+
return football_action_set.action_right
|
|
143
|
+
if action == football_action_set.action_top_left:
|
|
144
|
+
return football_action_set.action_bottom_right
|
|
145
|
+
if action == football_action_set.action_top:
|
|
146
|
+
return football_action_set.action_bottom
|
|
147
|
+
if action == football_action_set.action_top_right:
|
|
148
|
+
return football_action_set.action_bottom_left
|
|
149
|
+
if action == football_action_set.action_right:
|
|
150
|
+
return football_action_set.action_left
|
|
151
|
+
if action == football_action_set.action_bottom_right:
|
|
152
|
+
return football_action_set.action_top_left
|
|
153
|
+
if action == football_action_set.action_bottom:
|
|
154
|
+
return football_action_set.action_top
|
|
155
|
+
if action == football_action_set.action_bottom_left:
|
|
156
|
+
return football_action_set.action_top_right
|
|
157
|
+
return action
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def flip_action(action, config):
|
|
161
|
+
if isinstance(action, np.ndarray) or isinstance(action, list):
|
|
162
|
+
return [flip_single_action(a, config) for a in action]
|
|
163
|
+
return flip_single_action(action, config)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Test for observation_rotation.py."""
|
|
19
|
+
|
|
20
|
+
from __future__ import absolute_import
|
|
21
|
+
from __future__ import division
|
|
22
|
+
from __future__ import print_function
|
|
23
|
+
|
|
24
|
+
from absl.testing import absltest
|
|
25
|
+
|
|
26
|
+
from cuju.gfootball.env import config
|
|
27
|
+
from cuju.gfootball.env import football_action_set
|
|
28
|
+
from cuju.gfootball.env import observation_rotation
|
|
29
|
+
import numpy as np
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ObservationRotationTest(absltest.TestCase):
|
|
33
|
+
|
|
34
|
+
def testObservationFlipping(self):
|
|
35
|
+
cfg = config.Config()
|
|
36
|
+
num_players = 11
|
|
37
|
+
observation = {}
|
|
38
|
+
observation['left_team'] = np.random.rand(num_players * 2) - 0.5
|
|
39
|
+
observation['left_team_roles'] = np.random.rand(num_players)
|
|
40
|
+
observation['left_team_direction'] = np.random.rand(num_players * 2) - 0.5
|
|
41
|
+
observation['left_team_tired_factor'] = np.random.rand(num_players)
|
|
42
|
+
observation['left_team_yellow_card'] = np.random.rand(num_players)
|
|
43
|
+
observation['left_team_active'] = [3]
|
|
44
|
+
observation['left_team_designated_player'] = 3
|
|
45
|
+
observation['right_team'] = np.random.rand(num_players * 2) - 0.5
|
|
46
|
+
observation['right_team_roles'] = np.random.rand(num_players)
|
|
47
|
+
observation['right_team_direction'] = np.random.rand(num_players * 2) - 0.5
|
|
48
|
+
observation['right_team_tired_factor'] = np.random.rand(num_players)
|
|
49
|
+
observation['right_team_yellow_card'] = np.random.rand(num_players)
|
|
50
|
+
observation['right_team_active'] = [0]
|
|
51
|
+
observation['right_team_designated_player'] = 0
|
|
52
|
+
observation['ball'] = np.array([1, -1, 0])
|
|
53
|
+
observation['ball_direction'] = np.random.rand(3) - 0.5
|
|
54
|
+
observation['ball_rotation'] = np.random.rand(3) - 0.5
|
|
55
|
+
observation['ball_owned_team'] = 0
|
|
56
|
+
observation['ball_owned_player'] = 7
|
|
57
|
+
observation['left_agent_controlled_player'] = [4]
|
|
58
|
+
observation['right_agent_controlled_player'] = [6]
|
|
59
|
+
observation['game_mode'] = 123
|
|
60
|
+
observation['left_agent_sticky_actions'] = [
|
|
61
|
+
[np.random.rand(2) for _ in range(10)]]
|
|
62
|
+
observation['right_agent_sticky_actions'] = [
|
|
63
|
+
[np.random.rand(2) for _ in range(10)]]
|
|
64
|
+
observation['score'] = [3, 5]
|
|
65
|
+
observation['steps_left'] = 45
|
|
66
|
+
# Flipping twice the observation is the identity.
|
|
67
|
+
flipped_observation = observation_rotation.flip_observation(
|
|
68
|
+
observation, cfg)
|
|
69
|
+
original_observation = observation_rotation.flip_observation(
|
|
70
|
+
flipped_observation, cfg)
|
|
71
|
+
self.assertEqual(str(tuple(sorted(original_observation.items()))),
|
|
72
|
+
str(tuple(sorted(observation.items()))))
|
|
73
|
+
|
|
74
|
+
def testActionFlipping(self):
|
|
75
|
+
cfg = config.Config()
|
|
76
|
+
for action in football_action_set.full_action_set:
|
|
77
|
+
# Flipping twice should give the same action.
|
|
78
|
+
action_id = observation_rotation.flip_single_action(
|
|
79
|
+
observation_rotation.flip_single_action(action, cfg), cfg)
|
|
80
|
+
self.assertEqual(action, action_id)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == '__main__':
|
|
84
|
+
absltest.main()
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Base player class."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class PlayerBase(object):
|
|
22
|
+
"""Base player class."""
|
|
23
|
+
|
|
24
|
+
def __init__(self, player_config=None):
|
|
25
|
+
self._num_left_controlled_players = 1
|
|
26
|
+
self._num_right_controlled_players = 0
|
|
27
|
+
self._can_play_right = False
|
|
28
|
+
if player_config:
|
|
29
|
+
self._num_left_controlled_players = int(player_config['left_players'])
|
|
30
|
+
self._num_right_controlled_players = int(player_config['right_players'])
|
|
31
|
+
|
|
32
|
+
def num_controlled_left_players(self):
|
|
33
|
+
return self._num_left_controlled_players
|
|
34
|
+
|
|
35
|
+
def num_controlled_right_players(self):
|
|
36
|
+
return self._num_right_controlled_players
|
|
37
|
+
|
|
38
|
+
def num_controlled_players(self):
|
|
39
|
+
return (self._num_left_controlled_players +
|
|
40
|
+
self._num_right_controlled_players)
|
|
41
|
+
|
|
42
|
+
def reset(self):
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
def can_play_right(self):
|
|
46
|
+
return self._can_play_right
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Agent player controlled by the training policy and using step/reset API."""
|
|
19
|
+
from __future__ import absolute_import
|
|
20
|
+
from __future__ import division
|
|
21
|
+
from __future__ import print_function
|
|
22
|
+
|
|
23
|
+
import copy
|
|
24
|
+
|
|
25
|
+
from cuju.gfootball.env import player_base
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Player(player_base.PlayerBase):
|
|
29
|
+
|
|
30
|
+
def __init__(self, player_config, env_config):
|
|
31
|
+
player_base.PlayerBase.__init__(self, player_config)
|
|
32
|
+
assert player_config['player_agent'] == 0, 'Only one \'agent\' player allowed'
|
|
33
|
+
self._action = None
|
|
34
|
+
|
|
35
|
+
def set_action(self, action):
|
|
36
|
+
self._action = action
|
|
37
|
+
|
|
38
|
+
def take_action(self, observations):
|
|
39
|
+
return copy.deepcopy(self._action)
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Sample bot player."""
|
|
19
|
+
from __future__ import absolute_import
|
|
20
|
+
from __future__ import division
|
|
21
|
+
from __future__ import print_function
|
|
22
|
+
|
|
23
|
+
from cuju.gfootball.env import football_action_set
|
|
24
|
+
from cuju.gfootball.env import player_base
|
|
25
|
+
import numpy as np
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Player(player_base.PlayerBase):
|
|
29
|
+
|
|
30
|
+
def __init__(self, player_config, env_config):
|
|
31
|
+
assert env_config["action_set"] == 'full'
|
|
32
|
+
player_base.PlayerBase.__init__(self, player_config)
|
|
33
|
+
self._observation = None
|
|
34
|
+
self._last_action = football_action_set.action_idle
|
|
35
|
+
self._shoot_distance = 0.15
|
|
36
|
+
self._pressure_enabled = False
|
|
37
|
+
|
|
38
|
+
def _object_distance(self, object1, object2):
|
|
39
|
+
"""Computes distance between two objects."""
|
|
40
|
+
return np.linalg.norm(np.array(object1) - np.array(object2))
|
|
41
|
+
|
|
42
|
+
def _direction_action(self, delta):
|
|
43
|
+
"""For required movement direction vector returns appropriate action."""
|
|
44
|
+
all_directions = [
|
|
45
|
+
football_action_set.action_top,
|
|
46
|
+
football_action_set.action_top_left,
|
|
47
|
+
football_action_set.action_left,
|
|
48
|
+
football_action_set.action_bottom_left,
|
|
49
|
+
football_action_set.action_bottom,
|
|
50
|
+
football_action_set.action_bottom_right,
|
|
51
|
+
football_action_set.action_right,
|
|
52
|
+
football_action_set.action_top_right
|
|
53
|
+
]
|
|
54
|
+
all_directions_vec = [(0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1),
|
|
55
|
+
(1, 0), (1, -1)]
|
|
56
|
+
all_directions_vec = [
|
|
57
|
+
np.array(v) / np.linalg.norm(np.array(v)) for v in all_directions_vec
|
|
58
|
+
]
|
|
59
|
+
best_direction = np.argmax([np.dot(delta, v) for v in all_directions_vec])
|
|
60
|
+
return all_directions[best_direction]
|
|
61
|
+
|
|
62
|
+
def _closest_opponent_to_object(self, o):
|
|
63
|
+
"""For a given object returns the closest opponent.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
o: Source object.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Closest opponent."""
|
|
70
|
+
min_d = None
|
|
71
|
+
closest = None
|
|
72
|
+
for p in self._observation['right_team']:
|
|
73
|
+
d = self._object_distance(o, p)
|
|
74
|
+
if min_d is None or d < min_d:
|
|
75
|
+
min_d = d
|
|
76
|
+
closest = p
|
|
77
|
+
assert closest is not None
|
|
78
|
+
return closest
|
|
79
|
+
|
|
80
|
+
def _closest_front_opponent(self, o, target):
|
|
81
|
+
"""For an object and its movement direction returns the closest opponent.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
o: Source object.
|
|
85
|
+
target: Movement direction.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
Closest front opponent."""
|
|
89
|
+
delta = target - o
|
|
90
|
+
min_d = None
|
|
91
|
+
closest = None
|
|
92
|
+
for p in self._observation['right_team']:
|
|
93
|
+
delta_opp = p - o
|
|
94
|
+
if np.dot(delta, delta_opp) <= 0:
|
|
95
|
+
continue
|
|
96
|
+
d = self._object_distance(o, p)
|
|
97
|
+
if min_d is None or d < min_d:
|
|
98
|
+
min_d = d
|
|
99
|
+
closest = p
|
|
100
|
+
|
|
101
|
+
# May return None!
|
|
102
|
+
return closest
|
|
103
|
+
|
|
104
|
+
def _score_pass_target(self, active, player):
|
|
105
|
+
"""Computes score of the pass between players.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
active: Player doing the pass.
|
|
109
|
+
player: Player receiving the pass.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
Score of the pass.
|
|
113
|
+
"""
|
|
114
|
+
opponent = self._closest_opponent_to_object(player)
|
|
115
|
+
dist = self._object_distance(player, opponent)
|
|
116
|
+
trajectory = player - active
|
|
117
|
+
dist_closest_traj = None
|
|
118
|
+
for i in range(10):
|
|
119
|
+
position = active + (i + 1) / 10.0 * trajectory
|
|
120
|
+
opp_traj = self._closest_opponent_to_object(position)
|
|
121
|
+
dist_traj = self._object_distance(position, opp_traj)
|
|
122
|
+
if dist_closest_traj is None or dist_traj < dist_closest_traj:
|
|
123
|
+
dist_closest_traj = dist_traj
|
|
124
|
+
return -dist_closest_traj
|
|
125
|
+
|
|
126
|
+
def _best_pass_target(self, active):
|
|
127
|
+
"""Computes best pass a given player can do.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
active: Player doing the pass.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
Best target player receiving the pass.
|
|
134
|
+
"""
|
|
135
|
+
best_score = None
|
|
136
|
+
best_target = None
|
|
137
|
+
for player in self._observation['left_team']:
|
|
138
|
+
if self._object_distance(player, active) > 0.3:
|
|
139
|
+
continue
|
|
140
|
+
score = self._score_pass_target(active, player)
|
|
141
|
+
if best_score is None or score > best_score:
|
|
142
|
+
best_score = score
|
|
143
|
+
best_target = player
|
|
144
|
+
return best_target
|
|
145
|
+
|
|
146
|
+
def _avoid_opponent(self, active, opponent, target):
|
|
147
|
+
"""Computes movement action to avoid a given opponent.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
active: Active player.
|
|
151
|
+
opponent: Opponent to be avoided.
|
|
152
|
+
target: Original movement direction of the active player.
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
Action to perform to avoid the opponent.
|
|
156
|
+
"""
|
|
157
|
+
# Choose a perpendicular direction to the opponent, towards the target.
|
|
158
|
+
delta = opponent - active
|
|
159
|
+
delta_t = target - active
|
|
160
|
+
new_delta = [delta[1], -delta[0]]
|
|
161
|
+
if delta_t[0] * new_delta[0] < 0:
|
|
162
|
+
new_delta = [-new_delta[0], -new_delta[1]]
|
|
163
|
+
|
|
164
|
+
return self._direction_action(new_delta)
|
|
165
|
+
|
|
166
|
+
def _get_action(self):
|
|
167
|
+
"""Returns action to perform for the current observations."""
|
|
168
|
+
active = self._observation['left_team'][self._observation['active']]
|
|
169
|
+
# Corner etc. - just pass the ball
|
|
170
|
+
if self._observation['game_mode'] != 0:
|
|
171
|
+
return football_action_set.action_long_pass
|
|
172
|
+
|
|
173
|
+
if self._observation['ball_owned_team'] == 1:
|
|
174
|
+
if self._last_action == football_action_set.action_pressure:
|
|
175
|
+
return football_action_set.action_sprint
|
|
176
|
+
self._pressure_enabled = True
|
|
177
|
+
return football_action_set.action_pressure
|
|
178
|
+
|
|
179
|
+
if self._pressure_enabled:
|
|
180
|
+
self._pressure_enabled = False
|
|
181
|
+
return football_action_set.action_release_pressure
|
|
182
|
+
target_x = 0.85
|
|
183
|
+
|
|
184
|
+
if (self._shoot_distance >
|
|
185
|
+
np.linalg.norm(self._observation['ball'][:2] - [target_x, 0])):
|
|
186
|
+
return football_action_set.action_shot
|
|
187
|
+
|
|
188
|
+
move_target = [target_x, 0]
|
|
189
|
+
# Compute run direction.
|
|
190
|
+
move_action = self._direction_action(move_target - active)
|
|
191
|
+
|
|
192
|
+
closest_front_opponent = self._closest_front_opponent(active, move_target)
|
|
193
|
+
if closest_front_opponent is not None:
|
|
194
|
+
dist_front_opp = self._object_distance(active, closest_front_opponent)
|
|
195
|
+
else:
|
|
196
|
+
dist_front_opp = 2.0
|
|
197
|
+
|
|
198
|
+
# Maybe avoid opponent on your way?
|
|
199
|
+
if dist_front_opp < 0.08:
|
|
200
|
+
best_pass_target = self._best_pass_target(active)
|
|
201
|
+
if np.array_equal(best_pass_target, active):
|
|
202
|
+
move_action = self._avoid_opponent(active, closest_front_opponent,
|
|
203
|
+
move_target)
|
|
204
|
+
else:
|
|
205
|
+
delta = best_pass_target - active
|
|
206
|
+
direction_action = self._direction_action(delta)
|
|
207
|
+
if self._last_action == direction_action:
|
|
208
|
+
return football_action_set.action_short_pass
|
|
209
|
+
else:
|
|
210
|
+
return direction_action
|
|
211
|
+
return move_action
|
|
212
|
+
|
|
213
|
+
def take_action(self, observations):
|
|
214
|
+
assert len(observations) == 1, 'Bot does not support multiple player control'
|
|
215
|
+
self._observation = observations[0]
|
|
216
|
+
self._last_action = self._get_action()
|
|
217
|
+
return self._last_action
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
# Copyright 2019 Google LLC
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# Modified by the cuju Authors in 2026, in the import lines that ci/port_gfootball.py lists.
|
|
15
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
"""Player with actions coming from gamepad."""
|
|
19
|
+
|
|
20
|
+
from absl import logging
|
|
21
|
+
import pygame
|
|
22
|
+
|
|
23
|
+
from cuju.gfootball.env import controller_base
|
|
24
|
+
from cuju.gfootball.env import football_action_set
|
|
25
|
+
from cuju.gfootball.env import event_queue
|
|
26
|
+
|
|
27
|
+
BUTTON_TO_ACTIONS = {
|
|
28
|
+
0: [football_action_set.action_short_pass,
|
|
29
|
+
football_action_set.action_pressure],
|
|
30
|
+
1: [football_action_set.action_shot,
|
|
31
|
+
football_action_set.action_team_pressure],
|
|
32
|
+
2: [football_action_set.action_high_pass,
|
|
33
|
+
football_action_set.action_sliding],
|
|
34
|
+
3: [football_action_set.action_long_pass,
|
|
35
|
+
football_action_set.action_keeper_rush],
|
|
36
|
+
4: [football_action_set.action_switch],
|
|
37
|
+
5: [football_action_set.action_dribble],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Player(controller_base.Controller):
|
|
42
|
+
"""Player with actions coming from gamepad."""
|
|
43
|
+
|
|
44
|
+
def __init__(self, player_config, env_config):
|
|
45
|
+
controller_base.Controller.__init__(self, player_config, env_config)
|
|
46
|
+
self._can_play_right = True
|
|
47
|
+
pygame.init()
|
|
48
|
+
self._index = player_config['player_gamepad']
|
|
49
|
+
event_queue.add_controller('gamepad', self._index)
|
|
50
|
+
pygame.joystick.init()
|
|
51
|
+
if pygame.joystick.get_count() < self._index:
|
|
52
|
+
logging.error("You need %d physical controller(s) connected" % self._index)
|
|
53
|
+
exit(1)
|
|
54
|
+
self._joystick = pygame.joystick.Joystick(self._index)
|
|
55
|
+
self._joystick.init()
|
|
56
|
+
|
|
57
|
+
def take_action(self, observations):
|
|
58
|
+
assert len(observations) == 1, 'Gamepad does not support multiple player control'
|
|
59
|
+
x_axis = self._joystick.get_axis(0)
|
|
60
|
+
y_axis = self._joystick.get_axis(1)
|
|
61
|
+
left = x_axis < -0.5
|
|
62
|
+
right = x_axis > 0.5
|
|
63
|
+
top = y_axis < -0.5
|
|
64
|
+
bottom = y_axis > 0.5
|
|
65
|
+
active_buttons = {}
|
|
66
|
+
for event in event_queue.get('gamepad', self._index):
|
|
67
|
+
if event.type == pygame.JOYBUTTONDOWN:
|
|
68
|
+
actions = BUTTON_TO_ACTIONS.get(event.button, [])
|
|
69
|
+
for a in actions:
|
|
70
|
+
active_buttons[a] = 1
|
|
71
|
+
if (event.type == pygame.JOYAXISMOTION and event.axis == 5 and
|
|
72
|
+
event.value > 0):
|
|
73
|
+
active_buttons[football_action_set.action_sprint] = 1
|
|
74
|
+
|
|
75
|
+
for button, actions in BUTTON_TO_ACTIONS.items():
|
|
76
|
+
if self._joystick.get_button(button):
|
|
77
|
+
for a in actions:
|
|
78
|
+
active_buttons[a] = 1
|
|
79
|
+
if self._joystick.get_axis(5) > 0:
|
|
80
|
+
active_buttons[football_action_set.action_sprint] = 1
|
|
81
|
+
return self.get_env_action(left, right, top, bottom, active_buttons)
|