-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresentation.py
134 lines (110 loc) · 5.82 KB
/
presentation.py
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
import os
import random
import numpy as np
from absl import app
from pysc2.agents import base_agent
from pysc2.lib import actions, features, units
from pysc2.env import sc2_env, run_loop
class RawAgent(base_agent.BaseAgent):
def __init__(self):
super(RawAgent, self).__init__()
self.base_top_left = None
def get_my_units_by_type(self, obs, unit_type):
return [unit for unit in obs.observation.raw_units
if unit.unit_type == unit_type
and unit.alliance == features.PlayerRelative.SELF]
def get_my_completed_units_by_type(self, obs, unit_type):
return [unit for unit in obs.observation.raw_units
if unit.unit_type == unit_type
and unit.build_progress == 100
and unit.alliance == features.PlayerRelative.SELF]
def get_distances(self, obs, units, xy):
units_xy = [(unit.x, unit.y) for unit in units]
return np.linalg.norm(np.array(units_xy) - np.array(xy), axis=1)
def step(self, obs):
super(RawAgent, self).step(obs)
if obs.first():
nexus = self.get_my_units_by_type(obs, units.Protoss.Nexus)[0]
self.base_top_left = (nexus.x < 32)
nexi = self.get_my_units_by_type(obs, units.Protoss.Nexus)
probes = self.get_my_units_by_type(obs, units.Protoss.Probe)
pylons = self.get_my_units_by_type(obs, units.Protoss.Pylon)
completed_pylons = self.get_my_completed_units_by_type(obs,
units.Protoss.Pylon)
free_supply = (obs.observation.player.food_cap -
obs.observation.player.food_used)
if len(pylons) == 0 and obs.observation.player.minerals >= 100:
probes = self.get_my_units_by_type(obs, units.Protoss.Probe)
if len(probes) > 0:
pylon_xy = (21, 22) if self.base_top_left else (42, 44)
distances = self.get_distances(obs, probes, pylon_xy)
probe = probes[np.argmin(distances)]
return actions.RAW_FUNCTIONS.Build_Pylon_pt("now", probe.tag, pylon_xy)
idle_probes = [probe for probe in probes if probe.order_length == 0]
if len(idle_probes) > 0:
probe = random.choice(idle_probes)
mineral_patches = [unit for unit in obs.observation.raw_units
if unit.unit_type in [units.Neutral.BattleStationMineralField,
units.Neutral.BattleStationMineralField750,
units.Neutral.LabMineralField,
units.Neutral.LabMineralField750,
units.Neutral.MineralField,
units.Neutral.MineralField750,
units.Neutral.PurifierMineralField,
units.Neutral.PurifierMineralField750,
units.Neutral.PurifierRichMineralField,
units.Neutral.PurifierRichMineralField750,
units.Neutral.RichMineralField,
units.Neutral.RichMineralField750]]
distances = self.get_distances(obs, mineral_patches, (probe.x, probe.y))
mineral_patch = mineral_patches[np.argmin(distances)]
return actions.RAW_FUNCTIONS.Harvest_Gather_unit(
"now", probe.tag, mineral_patch.tag)
if free_supply > 1 and len(probes) < 18*len(nexi) and len(nexi) > 0:
nexus = random.choice(nexi)
if obs.observation.player.minerals >= 50 and nexus.order_length < 3:
return actions.RAW_FUNCTIONS.Train_Probe_quick("now", nexus.tag)
if obs.observation.player.minerals >= 100 and free_supply < 3:
probes = self.get_my_units_by_type(obs, units.Protoss.Probe)
if len(probes) > 0:
x = random.randint(0,23)
y = random.randint(0,23)
pylon_xy = (x, y) if self.base_top_left else (64-x, 64-y)
distances = self.get_distances(obs, probes, pylon_xy)
probe = probes[np.argmin(distances)]
return actions.RAW_FUNCTIONS.Build_Pylon_pt("now", probe.tag, np.array(pylon_xy)+np.array([probe.x//2,probe.y//2]))
if obs.observation.player.minerals >= 400 and len(probes) >= 18*len(nexi):
probes = self.get_my_units_by_type(obs, units.Protoss.Probe)
if len(probes) > 0:
x = random.randint(0,30)
y = random.randint(0,30)
if self.base_top_left:
nexus_xy = (x, y)
else:
nexus_xy = (64-x, 64-y)
distances = self.get_distances(obs, probes, nexus_xy)
probe = probes[np.argmin(distances)]
k = random.randint(0,10)
if k < 4:
return actions.RAW_FUNCTIONS.Build_Nexus_pt("now", probe.tag, np.array(nexus_xy)+np.array([probe.x,probe.y]))
return actions.RAW_FUNCTIONS.no_op()
def main(unused_argv):
agent = RawAgent()
try:
while True:
with sc2_env.SC2Env(
map_name="AbyssalReef",
players=[sc2_env.Agent(sc2_env.Race.protoss),
sc2_env.Bot(sc2_env.Race.protoss,
sc2_env.Difficulty.hard)],
agent_interface_format=features.AgentInterfaceFormat(
action_space=actions.ActionSpace.RAW,
use_raw_units=True,
raw_resolution=64,
),
) as env:
run_loop.run_loop([agent], env)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
app.run(main)