File size: 5,209 Bytes
2b3e863 | 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 | #!/usr/bin/env python3
"""
🥊 Street Fighter RL Environment - Based on nicknochnack/StreetFighterRL
Simple PPO-based implementation using Stable Baselines3
"""
import cv2
import numpy as np
import gymnasium as gym
from gymnasium import spaces
import retro
from discretizer import StreetFighter2Discretizer
# --- FIX for TypeError in retro.make ---
_original_retro_make = retro.make
def _patched_retro_make(game, state=None, **kwargs):
if not state:
state = "ken_bison_12.state"
return _original_retro_make(game=game, state=state, **kwargs)
retro.make = _patched_retro_make
print("🥊 Street Fighter RL Environment - Simple PPO Implementation")
class StreetFighter(gym.Env):
"""
Custom Street Fighter environment based on nicknochnack/StreetFighterRL
Simplified wrapper for PPO training
"""
def __init__(self):
# super init
super().__init__()
# Create the retro environment
try:
# retro make game env (headless - no rendering)
game = retro.make(
"StreetFighterIISpecialChampionEdition-Genesis",
state="ken_bison_12.state",
use_restricted_actions=retro.Actions.FILTERED,
render_mode=None, # Disable UI rendering
)
# Wrap with discretizer for special moves
self.game = StreetFighter2Discretizer(game)
print("✅ Street Fighter ROM found and loaded with special moves!")
except FileNotFoundError:
print("❌ Street Fighter ROM not found.")
print(" To use Street Fighter: python -m retro.import /path/to/rom/file")
raise
# Define observation space - 84x84 grayscale
self.observation_space = spaces.Box(
low=0, high=255, shape=(84, 84, 1), dtype=np.uint8
)
# Use discrete action space from discretizer (includes special moves)
self.action_space = self.game.action_space
# Health tracking
self.agent_hp = 176
self.enemy_hp = 176
def preprocess(self, observation):
"""
Preprocess observation: convert to grayscale and resize to 84x84
"""
gray = cv2.cvtColor(observation, cv2.COLOR_BGR2GRAY)
resize = cv2.resize(gray, (84, 84), interpolation=cv2.INTER_CUBIC)
state = np.reshape(resize, (84, 84, 1))
return state
def step(self, action):
"""
Take a step in the environment
"""
obs, _, done, truncated, info = self.game.step(action)
obs = self.preprocess(obs)
# Get current health values from the game info
# Retro uses 'health' and 'enemy_health' as variable names
current_agent_hp = info.get("health", self.agent_hp)
current_enemy_hp = info.get("enemy_health", self.enemy_hp)
# Calculate health change (agent_hp_change - enemy_hp_change)
# we want to create health advantage compared your opponent
delta_hp_diff = (current_agent_hp - self.agent_hp) - (
current_enemy_hp - self.enemy_hp
)
# Normalize health reward by max health (176) and add small time penalty
# health double diff
reward = delta_hp_diff / 176.0 - 0.0001
# Update health tracking
self.agent_hp = current_agent_hp
self.enemy_hp = current_enemy_hp
# Check for round end
done = False
if self.agent_hp <= 0 or self.enemy_hp <= 0:
done = True
if self.enemy_hp <= 0 and self.agent_hp > 0:
reward += 1.0 # Large win bonus
info["agent_won"] = True
else:
reward -= 1.0 # Large loss penalty
info["agent_won"] = False
# Add HP values to info for logging/debugging
info["agent_hp"] = self.agent_hp
info["enemy_hp"] = self.enemy_hp
return obs, reward, done, truncated, info
def reset(self, **kwargs):
"""
Reset the environment
"""
result = self.game.reset(**kwargs)
# Handle tuple return from game.reset()
if isinstance(result, tuple):
obs, info = result
else:
obs = result
info = {}
obs = self.preprocess(obs)
# Initialize health values from the game info dictionary
# Retro uses 'health' and 'enemy_health' as variable names
self.agent_hp = info.get("health", 176)
self.enemy_hp = info.get("enemy_health", 176)
return obs, info
def render(self, mode="human"):
"""
Render the environment
"""
return self.game.render()
def close(self):
"""
Close the environment
"""
self.game.close()
def get_action_meaning(self, action):
"""
Get the meaning of an action (for debugging)
"""
return self.game.get_action_meaning(action)
def make_env():
"""
Create and return the Street Fighter environment
"""
return StreetFighter()
# Export the main components
__all__ = ["StreetFighter", "make_env"]
print("🥊 Street Fighter RL wrapper loaded successfully!")
|