Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. from pysc2.agents import base_agent
  2. from pysc2.lib import actions
  3. from pysc2.lib import features
  4.  
  5. import time
  6.  
  7. # Functions
  8. _BUILD_REFINERY = actions.FUNCTIONS.Build_Refinery_screen.id
  9. _BUILD_SUPPLYDEPOT = actions.FUNCTIONS.Build_SupplyDepot_screen.id
  10. _NOOP = actions.FUNCTIONS.no_op.id
  11. _SELECT_POINT = actions.FUNCTIONS.select_point.id
  12. # Features
  13. _PLAYER_RELATIVE = features.SCREEN_FEATURES.player_relative.index
  14. _UNIT_TYPE = features.SCREEN_FEATURES.unit_type.index
  15.  
  16. # Unit IDs
  17. _TERRAN_COMMANDCENTER = 18
  18. _VESPENE_GAS = 342
  19. _TERRAN_SUPPLYDEPOT = 19
  20. _TERRAN_SCV = 45
  21.  
  22. # Parameters
  23. _PLAYER_SELF = 1
  24. _NOT_QUEUED = [0]
  25. _QUEUED = [1]
  26.  
  27. ##x=-1
  28. ##y=-1
  29.  
  30. class SimpleAgent(base_agent.BaseAgent):
  31. base_top_left = None
  32. supply_depot_built = False
  33. scv_selected = False
  34. refinery_built = False
  35.  
  36. def transformLocation(self, x, x_distance, y, y_distance):
  37. if not self.base_top_left:
  38. return [x - x_distance, y - y_distance]
  39.  
  40. return [x + x_distance, y + y_distance]
  41.  
  42. def step(self, obs):
  43. super(SimpleAgent, self).step(obs)
  44.  
  45. time.sleep(0.5)
  46.  
  47. if self.base_top_left is None:
  48. player_y, player_x = (obs.observation["minimap"][_PLAYER_RELATIVE] == _PLAYER_SELF).nonzero()
  49. self.base_top_left = player_y.mean() <= 31
  50.  
  51. if not self.supply_depot_built:
  52. if not self.scv_selected:
  53. unit_type = obs.observation["screen"][_UNIT_TYPE]
  54. unit_y, unit_x = (unit_type == _TERRAN_SCV).nonzero()
  55. target = [unit_x[0], unit_y[0]]
  56. self.scv_selected = True
  57. return actions.FunctionCall(_SELECT_POINT, [_NOT_QUEUED, target])
  58. elif _BUILD_SUPPLYDEPOT in obs.observation["available_actions"]:
  59. unit_type = obs.observation["screen"][_UNIT_TYPE]
  60. unit_y, unit_x = (unit_type == _TERRAN_COMMANDCENTER).nonzero()
  61. target = self.transformLocation(int(unit_x.mean()), 20, int(unit_y.mean()), 0)
  62. self.supply_depot_built = True
  63. return actions.FunctionCall(_BUILD_SUPPLYDEPOT, [_NOT_QUEUED, target])
  64. elif not self.refinery_built:
  65. if not self.scv_selected:
  66. unit_type = obs.observation["screen"][_UNIT_TYPE]
  67. unit_y, unit_x = (unit_type == _TERRAN_SCV).nonzero()
  68. target = [unit_x[0], unit_y[0]]
  69. self.scv_selected = True
  70. return actions.FunctionCall(_SELECT_POINT, [_NOT_QUEUED, target])
  71. if _BUILD_REFINERY in obs.observation["available_actions"]:
  72. unit_type = obs.observation["screen"][_UNIT_TYPE]
  73. unit_y, unit_x = (unit_type == _VESPENE_GAS).nonzero()
  74. target = [unit_x.mean(), unit_y.mean()]
  75. self.refinery_built = True
  76. return actions.FunctionCall(_BUILD_REFINERY, [_QUEUED, target])
  77.  
  78. return actions.FunctionCall(_NOOP, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement