woodsja

asdf

Nov 4th, 2018
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from pysc2.agents import base_agent
  2. from pysc2.env import sc2_env
  3. from pysc2.lib import actions, features, units
  4. from absl import app
  5. import time
  6. from datetime import datetime
  7.  
  8. # tensorforce imports for environment
  9. import numpy as np
  10. from tensorforce import TensorForceError
  11. from tensorforce.environments import Environment
  12.  
  13.  
  14.  
  15. SCREEN_SIZE = 84
  16. MINIMAP_SIZE = 64
  17.  
  18. class minigame_agent(base_agent.BaseAgent):
  19.   def __init__(self):
  20.     super(minigame_agent, self).__init__()
  21.  
  22.     self.attack_coordinates = None
  23.  
  24.   def unit_type_is_selected(self, obs, unit_type):
  25.     if (len(obs.observation.single_select) > 0 and
  26.             obs.observation.single_select[0].unit_type == unit_type):
  27.       return True
  28.  
  29.     if (len(obs.observation.multi_select) > 0 and
  30.             obs.observation.multi_select[0].unit_type == unit_type):
  31.       return True
  32.  
  33.     return False
  34.  
  35.   def get_units_by_type(self, obs, unit_type):
  36.     return [unit for unit in obs.observation.feature_units
  37.             if unit.unit_type == unit_type]
  38.  
  39.   def can_do(self, obs, action):
  40.     return action in obs.observation.available_actions
  41.  
  42.   def step(self, obs):
  43.     super(minigame_agent, self).step(obs)
  44.  
  45.     marines = self.get_units_by_type(obs, units.Terran.Marine)
  46.     roaches = self.get_units_by_type(obs, units.Zerg.Roach)
  47.  
  48.     env = marines + roaches
  49.  
  50.     time.sleep(.5)
  51.     return actions.FUNCTIONS.no_op()
  52.  
  53. def main(unused_argv):
  54.   agent = minigame_agent()
  55.   try:
  56.     while True:
  57.       try:
  58.         with sc2_env.SC2Env(
  59.           map_name="DefeatRoaches",
  60.           #players=[sc2_env.Agent(sc2_env.Race.Terran),
  61.           #         ],
  62.           agent_interface_format=features.AgentInterfaceFormat(
  63.             feature_dimensions=features.Dimensions(screen=SCREEN_SIZE,
  64.                                                    minimap=MINIMAP_SIZE),
  65.             use_feature_units=True),
  66.           step_mul=16,
  67.           game_steps_per_episode=0,
  68.           visualize=True,
  69.           ) as env:
  70.  
  71.           agent.setup(env.observation_spec(), env.action_spec())
  72.           timesteps = env.reset()
  73.           agent.reset()
  74.  
  75.           while True:
  76.             step_actions = [agent.step(timesteps[0])]
  77.             timesteps = env.step(step_actions)
  78.  
  79.       except KeyboardInterrupt:
  80.         raise
  81.       except Exception as e:
  82.         with open('log.txt', 'a') as myfile:
  83.           myfile.write('At %s recorded exception: %s.\n' %
  84.                        (datetime.now(), repr(e)))
  85.  
  86.   except KeyboardInterrupt:
  87.     pass
  88.  
  89. if __name__ == "__main__":
  90.   app.run(main)
Advertisement
Add Comment
Please, Sign In to add comment