# Path: oscar/agent/custom_agent.py
# class CustomAgent(BaseAgent):
#     """A base agent to write custom scripted agents."""
# 
#     def __init__(self):
#         super().__init__()
#         self._shared = {}
# 
#     def add_shared(self, name, shared):
#         self._shared[name] = shared
# 
# Path: oscar/util/location.py
# class Location:
# 
#     def __init__(self, minimap_loc=None, screen_loc=None, camera_loc=None):
#         self.minimap = minimap_loc
#         self.screen = screen_loc
#         self.camera = camera_loc
# 
#     def compute_minimap_loc(self, obs, shared):
#         scale_x = shared['camera'].width(obs) / shared['screen'].width(obs)
#         scale_y = shared['camera'].height(obs) / shared['screen'].height(obs)
# 
#         self.minimap = shared['minimap'].bound(obs, Point(
#             int(round(self.camera.x - 0.5*shared['camera'].width(obs) + self.screen.x * scale_x)),
#             int(round(self.camera.y - 0.5*shared['camera'].height(obs) + self.screen.y * scale_y))))
# 
#     def squarred_distance(self, other):
#         if not self.minimap or not other.minimap:
#             raise Exception("Can't compute distance")
#         return self.minimap.distance(other.minimap)
# 
#     def distance(self, other):
#         return math.sqrt(self.squarred_distance(other))
# 
#     def equals(self, other):
#         def equals_minimap():
#             return (not self.minimap and not other.minimap) \
#                 or (self.minimap and other.minimap and self.minimap.equals(other.minimap))
# 
#         def equals_screen():
#             return (not self.screen and not other.screen) \
#                 or (self.screen and other.screen and self.screen.equals(other.screen))
# 
#         def equals_camera():
#             return (not self.camera and not other.camera) \
#                 or (self.camera and other.camera and self.camera.equals(other.camera))
# 
#         return equals_minimap() and equals_screen() and equals_camera()
# 
#     def __str__(self):
#         return "Location {\n" \
#                + "  minimap: " + str(self.minimap) + "\n" \
#                + "  screen: " + str(self.screen) + "\n" \
#                + "  camera: " + str(self.camera) + "\n" \
#                + "}"


import random
from oscar.agent.custom_agent import CustomAgent
from oscar.constants import *
from oscar.meta_action.build import *
from oscar.util.location import Location
                    play['actions'] = [actions.FunctionCall(HARVEST_GATHER_SCREEN, 
                        [NOT_QUEUED, selected_mineral.location.screen.get_flipped().to_array()])]
                self._state = EconomyManager._IDLE
                
        elif self._state == EconomyManager._BUILDING_SUPPLY_DEPOT:
            if not self._worker_selected:
                if obs.observation[PLAYER][IDLE_WORKER_COUNT] > 0:
                    play['actions'] = [actions.FunctionCall(SELECT_IDLE_WORKER, [NEW_SELECTION])]
                    self._worker_selected = True

                else: # if no idle scv, select one randomly
                    on_screen_scv = self._shared['screen'].scan_units(obs, self._shared, [TERRAN_SCV], PLAYER_SELF)
                    
                    if len(on_screen_scv) == 0:
                        if len(self._shared['economy'].command_centers) > 0:
                            selected_command_center = random.choice(self._shared['economy'].command_centers)
                            play['actions'] = [actions.FunctionCall(MOVE_CAMERA,
                                [selected_command_center.camera.to_array()])]
                    else:
                        selected_scv = random.choice(on_screen_scv)
                        play['actions'] = [actions.FunctionCall(SELECT_POINT, 
                            [NEW_SELECTION, selected_scv.location.screen.get_flipped().to_array()])]
                        self._worker_selected = True

            elif BUILD_SUPPLY_DEPOT in obs.observation["available_actions"]:
                building_center = get_random_building_point(obs, self._shared, 11)
                if building_center:
                    play['actions'] = [actions.FunctionCall(BUILD_SUPPLY_DEPOT,
                        [NOT_QUEUED, building_center.get_flipped().to_array()])]
                    self._shared['economy'].add_supply_depot(obs, self._shared,

# Path: oscar/agent/custom_agent.py
# class CustomAgent(BaseAgent):
#     """A base agent to write custom scripted agents."""
# 
#     def __init__(self):
#         super().__init__()
#         self._shared = {}
# 
#     def add_shared(self, name, shared):
#         self._shared[name] = shared
# 
# Path: oscar/util/location.py
# class Location:
# 
#     def __init__(self, minimap_loc=None, screen_loc=None, camera_loc=None):
#         self.minimap = minimap_loc
#         self.screen = screen_loc
#         self.camera = camera_loc
# 
#     def compute_minimap_loc(self, obs, shared):
#         scale_x = shared['camera'].width(obs) / shared['screen'].width(obs)
#         scale_y = shared['camera'].height(obs) / shared['screen'].height(obs)
# 
#         self.minimap = shared['minimap'].bound(obs, Point(
#             int(round(self.camera.x - 0.5*shared['camera'].width(obs) + self.screen.x * scale_x)),
#             int(round(self.camera.y - 0.5*shared['camera'].height(obs) + self.screen.y * scale_y))))
# 
#     def squarred_distance(self, other):
#         if not self.minimap or not other.minimap:
#             raise Exception("Can't compute distance")
#         return self.minimap.distance(other.minimap)
# 
#     def distance(self, other):
#         return math.sqrt(self.squarred_distance(other))
# 
#     def equals(self, other):
#         def equals_minimap():
#             return (not self.minimap and not other.minimap) \
#                 or (self.minimap and other.minimap and self.minimap.equals(other.minimap))
# 
#         def equals_screen():
#             return (not self.screen and not other.screen) \
#                 or (self.screen and other.screen and self.screen.equals(other.screen))
# 
#         def equals_camera():
#             return (not self.camera and not other.camera) \
#                 or (self.camera and other.camera and self.camera.equals(other.camera))
# 
#         return equals_minimap() and equals_screen() and equals_camera()
# 
#     def __str__(self):
#         return "Location {\n" \
#                + "  minimap: " + str(self.minimap) + "\n" \
#                + "  screen: " + str(self.screen) + "\n" \
#                + "  camera: " + str(self.camera) + "\n" \
#                + "}"


# Path: oscar/agent/scripted/economy_manager.py
import random
from oscar.agent.custom_agent import CustomAgent
from oscar.constants import *
from oscar.meta_action.build import *
from oscar.util.location import Location

                    play['actions'] = [actions.FunctionCall(HARVEST_GATHER_SCREEN, 
                        [NOT_QUEUED, selected_mineral.location.screen.get_flipped().to_array()])]
                self._state = EconomyManager._IDLE
                
        elif self._state == EconomyManager._BUILDING_SUPPLY_DEPOT:
            if not self._worker_selected:
                if obs.observation[PLAYER][IDLE_WORKER_COUNT] > 0:
                    play['actions'] = [actions.FunctionCall(SELECT_IDLE_WORKER, [NEW_SELECTION])]
                    self._worker_selected = True

                else: # if no idle scv, select one randomly
                    on_screen_scv = self._shared['screen'].scan_units(obs, self._shared, [TERRAN_SCV], PLAYER_SELF)
                    
                    if len(on_screen_scv) == 0:
                        if len(self._shared['economy'].command_centers) > 0:
                            selected_command_center = random.choice(self._shared['economy'].command_centers)
                            play['actions'] = [actions.FunctionCall(MOVE_CAMERA,
                                [selected_command_center.camera.to_array()])]
                    else:
                        selected_scv = random.choice(on_screen_scv)
                        play['actions'] = [actions.FunctionCall(SELECT_POINT, 
                            [NEW_SELECTION, selected_scv.location.screen.get_flipped().to_array()])]
                        self._worker_selected = True

            elif BUILD_SUPPLY_DEPOT in obs.observation["available_actions"]:
                building_center = get_random_building_point(obs, self._shared, 11)
                if building_center:
                    play['actions'] = [actions.FunctionCall(BUILD_SUPPLY_DEPOT,
                        [NOT_QUEUED, building_center.get_flipped().to_array()])]
                    self._shared['economy'].add_supply_depot(obs, self._shared,

                        Location(camera_loc=self._shared['camera'].location(obs, self._shared), screen_loc=building_center))