Skip to content

Commit

Permalink
Add pygymtest back
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed Nov 3, 2023
1 parent 160bfab commit f33b0c3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
71 changes: 71 additions & 0 deletions gym_renderer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from typing import Any, Dict

import rlviser_py as rlviser
import RocketSim as rsim

from rlgym.api import Renderer
from rlgym.rocket_league.api import Car, GameState
from rlgym.rocket_league.common_values import BOOST_LOCATIONS


class RLViserRenderer(Renderer[GameState]):

def __init__(self, tick_rate=120/8):
rlviser.set_boost_pad_locations(BOOST_LOCATIONS)
self.tick_rate = tick_rate
self.packet_id = 0

def render(self, state: GameState, shared_info: Dict[str, Any]) -> Any:
boost_pad_states = [bool(timer == 0) for timer in state.boost_pad_timers]

ball = rsim.BallState()
ball.pos = rsim.Vec(*state.ball.position)
ball.vel = rsim.Vec(*state.ball.linear_velocity)
ball.ang_vel = rsim.Vec(*state.ball.angular_velocity)

car_data = []
for idx, car in enumerate(state.cars.values()):
car_state = self._get_car_state(car)
car_data.append((idx + 1, car.team_num, rsim.CarConfig(car.hitbox_type), car_state))

self.packet_id += 1
rlviser.render(tick_count=self.packet_id, tick_rate=self.tick_rate, game_mode=rsim.GameMode.SOCCAR,
boost_pad_states=boost_pad_states, ball=ball, cars=car_data)

def close(self):
rlviser.quit()

# I stole this from RocketSimEngine
def _get_car_state(self, car: Car):
car_state = rsim.CarState()
car_state.pos = rsim.Vec(*car.physics.position)
car_state.vel = rsim.Vec(*car.physics.linear_velocity)
car_state.ang_vel = rsim.Vec(*car.physics.angular_velocity)
car_state.rot_mat = rsim.RotMat(*car.physics.rotation_mtx.transpose().flatten())

car_state.demo_respawn_timer = car.demo_respawn_timer
car_state.is_on_ground = car.on_ground
car_state.supersonic_time = car.supersonic_time
car_state.boost = car.boost_amount * 100
car_state.time_spent_boosting = car.boost_active_time
car_state.handbrake_val = car.handbrake

car_state.has_jumped = car.has_jumped
car_state.last_controls.jump = car.is_holding_jump
car_state.is_jumping = car.is_jumping
car_state.jump_time = car.jump_time

car_state.has_flipped = car.has_flipped
car_state.has_double_jumped = car.has_double_jumped
car_state.air_time_since_jump = car.air_time_since_jump
car_state.flip_time = car.flip_time
car_state.last_rel_dodge_torque = rsim.Vec(*car.flip_torque)

car_state.is_auto_flipping = car.is_autoflipping
car_state.auto_flip_timer = car.autoflip_timer
car_state.auto_flip_torque_scale = car.autoflip_direction

if car.bump_victim_id is not None:
car_state.car_contact_id = car.bump_victim_id

return car_state
61 changes: 61 additions & 0 deletions pygymtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import time
import numpy as np
from itertools import chain

from rlgym.api import RLGym
from rlgym.rocket_league.action_parsers import LookupTableAction, RepeatAction
from rlgym.rocket_league.done_conditions import GoalCondition, AnyCondition, TimeoutCondition, NoTouchTimeoutCondition
from rlgym.rocket_league.obs_builders import DefaultObs
from rlgym.rocket_league.reward_functions import CombinedReward, GoalReward, TouchReward
from rlgym.rocket_league.sim import RocketSimEngine
from rlgym.rocket_league.state_mutators import MutatorSequence, FixedTeamSizeMutator, KickoffMutator

from gym_renderer import RLViserRenderer

env = RLGym(
state_mutator=MutatorSequence(
FixedTeamSizeMutator(blue_size=2, orange_size=2),
KickoffMutator()
),
obs_builder=DefaultObs(zero_padding=None),
action_parser=RepeatAction(LookupTableAction(), repeats=8),
reward_fn=CombinedReward(
(GoalReward(), 10.),
(TouchReward(), 0.1)
),
termination_cond=GoalCondition(),
truncation_cond=AnyCondition(
TimeoutCondition(timeout=300.),
NoTouchTimeoutCondition(timeout=30.)
),
transition_engine=RocketSimEngine(),
renderer=RLViserRenderer()
)

# simulate 2 episodes
for _ in range(2):
obs_dict = env.reset()
steps = 0
ep_reward = {agent_id: 0. for agent_id in env.agents}
t0 = time.time()
while True:
env.render()

actions = {}
for agent_id, action_space in env.action_spaces.items():
# agent.act(obs) | Your agent should go here
actions[agent_id] = np.random.randint(action_space, size=(1,))

obs_dict, reward_dict, terminated_dict, truncated_dict = env.step(actions)
for agent_id, reward in reward_dict.items():
ep_reward[agent_id] += reward

if any(chain(terminated_dict.values(), truncated_dict.values())):
break

time.sleep(max(0, t0 + steps / 15 - time.time()))
steps += 1

ep_time = time.time() - t0
print("Steps per second: {:.0f} | Episode time: {:.2f} | Episode Reward: {:.2f}".format(
steps / ep_time, ep_time, max(ep_reward.values())))

0 comments on commit f33b0c3

Please sign in to comment.