# Path: oscar/env/envs/pysc2_general_env.py
# class Pysc2GeneralEnv(Pysc2Env):
#     """
#         Gym version of the pysc2 env using the OSCAR general/commander/agent structure
#     """
# 
#     # action_space = spaces.Discrete(1)
#     # observation_space = None  # set in init
# 
#     def __init__(self,
#                  path_to_configuration=DEFAULT_CONFIGURATION,
#                  enable_visualisation=True,
#                  game_step_per_update=8):
#         self.pysc2_env = SC2Env(  # map_name='CollectMineralsAndGas',
#             map_name='Simple64',
#             players=[Agent(Race.terran),
#                      Bot(Race.random, Difficulty.very_easy)],
#             agent_interface_format=[AgentInterfaceFormat(feature_dimensions=Dimensions(screen=(SCREEN_RESOLUTION,
#                                                                                                SCREEN_RESOLUTION),
#                                                                                        minimap=(MINIMAP_RESOLUTION,
#                                                                                                 MINIMAP_RESOLUTION)),
#                                                          camera_width_world_units=TILES_VISIBLE_ON_SCREEN_WIDTH)],
#             # git version give camera position in observation if asked
#             visualize=enable_visualisation,
#             step_mul=game_step_per_update,
#             game_steps_per_episode=None  # use map default
#         )
#         self.general = General(path_to_configuration)
#         action_spec = self.pysc2_env.action_spec()
#         observation_spec = self.pysc2_env.observation_spec()
#         self.general.setup(observation_spec, action_spec)
#         # self.observation_space = self.general.training_memory.observation_space
#         super().__init__()
# 
#     def step(self, action):
#         """
#         This beautiful environment as a good sens of hierarchy.
#         He has a general and his general is better than you, and so the action you give
#         is pointless and general will choose what is a good idea (when you only have one
#         choice its easier for you, no ?).
#         :param action: an int, if you want but None is fine too (dict, tuple and object
#          are accepted but not recommended)
#         :return: a tuple with the observation, reward, done and an empty dict
#         """
#         action = self.general.step(self.last_obs)
#         obs, reward, done, debug_dict = super().step([action])
#         # explore observation to decide if agent can still play
#         # first condition is to check if a command center exist
#         # second is to check if the agent has unit or minerals to create one
#         if obs.observation[PLAYER][FOOD_CAP] < 15 \
#                 or (obs.observation[PLAYER][FOOD_USED] == 0 and obs.observation[PLAYER][MINERALS] < 50):
#             warnings.warn("Environment decided that game is lost")
#             done = True
#         return obs, reward, done, debug_dict
# 
#     def reset(self):
#         self.general.reset()
#         return super().reset()
# 
#     def render(self, mode='human', close=False):
#         super().render(mode, close)
# 
#     def close(self):
#         super().close()
# 
#     def seed(self, seed=None):
#         super().seed(seed)
# 
#     def get_action_mask(self):
#         return None  # no action required here


import gym
import threading
import numpy as np
import pandas as pd
import time
import os
from gym import spaces
from oscar.env.envs.pysc2_general_env import Pysc2GeneralEnv
from oscar.constants import *
        self.action_space = self.shared_memory.action_space
        self.observation_space = self.shared_memory.observation_space
        # start thread
        self.env_thread.start()

    def step(self, action):
        info_dict = {}
        # set action into shared memory
        self.shared_memory.shared_action = action
        # let the thread run again
        self.semaphore_action_set.release()
        # increase the number of steps performed by the learning agent
        self.env_thread.learning_agent_step += 1
        # wait a call to the target agent
        self.semaphore_obs_ready.acquire(blocking=True, timeout=None)
        # get the obs of the current state as seen by the agent
        obs = self.shared_memory.shared_obs
        # get the reward of the current run and reset it to 0 for next step
        reward = self.env_thread.reward  # / self.env_thread.step_count
        self.env_thread.reward = 0
        # get done state from thread
        done = self.env_thread.was_done
        if done:
            self.env_thread.was_done = False  # reset was_done to false for next loop
            if self.publish_stats:
                info_dict["stats"] = self.env_thread.stats
            # log result to disk if asked
            if self.log_file is not None and self.env_thread.stats is not None:
                if os.path.isfile(self.log_file):
                    self.env_thread.stats.to_csv(self.log_file, sep=',', mode='a', header=False)

# Path: oscar/env/envs/pysc2_general_env.py
# class Pysc2GeneralEnv(Pysc2Env):
#     """
#         Gym version of the pysc2 env using the OSCAR general/commander/agent structure
#     """
# 
#     # action_space = spaces.Discrete(1)
#     # observation_space = None  # set in init
# 
#     def __init__(self,
#                  path_to_configuration=DEFAULT_CONFIGURATION,
#                  enable_visualisation=True,
#                  game_step_per_update=8):
#         self.pysc2_env = SC2Env(  # map_name='CollectMineralsAndGas',
#             map_name='Simple64',
#             players=[Agent(Race.terran),
#                      Bot(Race.random, Difficulty.very_easy)],
#             agent_interface_format=[AgentInterfaceFormat(feature_dimensions=Dimensions(screen=(SCREEN_RESOLUTION,
#                                                                                                SCREEN_RESOLUTION),
#                                                                                        minimap=(MINIMAP_RESOLUTION,
#                                                                                                 MINIMAP_RESOLUTION)),
#                                                          camera_width_world_units=TILES_VISIBLE_ON_SCREEN_WIDTH)],
#             # git version give camera position in observation if asked
#             visualize=enable_visualisation,
#             step_mul=game_step_per_update,
#             game_steps_per_episode=None  # use map default
#         )
#         self.general = General(path_to_configuration)
#         action_spec = self.pysc2_env.action_spec()
#         observation_spec = self.pysc2_env.observation_spec()
#         self.general.setup(observation_spec, action_spec)
#         # self.observation_space = self.general.training_memory.observation_space
#         super().__init__()
# 
#     def step(self, action):
#         """
#         This beautiful environment as a good sens of hierarchy.
#         He has a general and his general is better than you, and so the action you give
#         is pointless and general will choose what is a good idea (when you only have one
#         choice its easier for you, no ?).
#         :param action: an int, if you want but None is fine too (dict, tuple and object
#          are accepted but not recommended)
#         :return: a tuple with the observation, reward, done and an empty dict
#         """
#         action = self.general.step(self.last_obs)
#         obs, reward, done, debug_dict = super().step([action])
#         # explore observation to decide if agent can still play
#         # first condition is to check if a command center exist
#         # second is to check if the agent has unit or minerals to create one
#         if obs.observation[PLAYER][FOOD_CAP] < 15 \
#                 or (obs.observation[PLAYER][FOOD_USED] == 0 and obs.observation[PLAYER][MINERALS] < 50):
#             warnings.warn("Environment decided that game is lost")
#             done = True
#         return obs, reward, done, debug_dict
# 
#     def reset(self):
#         self.general.reset()
#         return super().reset()
# 
#     def render(self, mode='human', close=False):
#         super().render(mode, close)
# 
#     def close(self):
#         super().close()
# 
#     def seed(self, seed=None):
#         super().seed(seed)
# 
#     def get_action_mask(self):
#         return None  # no action required here


# Path: oscar/env/envs/general_learning_env.py
import gym
import threading
import numpy as np
import pandas as pd
import time
import os
from gym import spaces
from oscar.env.envs.pysc2_general_env import Pysc2GeneralEnv
from oscar.constants import *

        self.action_space = self.shared_memory.action_space
        self.observation_space = self.shared_memory.observation_space
        # start thread
        self.env_thread.start()

    def step(self, action):
        info_dict = {}
        # set action into shared memory
        self.shared_memory.shared_action = action
        # let the thread run again
        self.semaphore_action_set.release()
        # increase the number of steps performed by the learning agent
        self.env_thread.learning_agent_step += 1
        # wait a call to the target agent
        self.semaphore_obs_ready.acquire(blocking=True, timeout=None)
        # get the obs of the current state as seen by the agent
        obs = self.shared_memory.shared_obs
        # get the reward of the current run and reset it to 0 for next step
        reward = self.env_thread.reward  # / self.env_thread.step_count
        self.env_thread.reward = 0
        # get done state from thread
        done = self.env_thread.was_done
        if done:
            self.env_thread.was_done = False  # reset was_done to false for next loop
            if self.publish_stats:
                info_dict["stats"] = self.env_thread.stats
            # log result to disk if asked
            if self.log_file is not None and self.env_thread.stats is not None:
                if os.path.isfile(self.log_file):
                    self.env_thread.stats.to_csv(self.log_file, sep=',', mode='a', header=False)

                else: