forked from Dentosal/python-sc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotest_bot.py
330 lines (262 loc) · 12.8 KB
/
autotest_bot.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import random
import sc2
from sc2 import Race, Difficulty
from sc2.constants import *
from sc2.player import Bot, Computer
from sc2.data import Alliance
from sc2.position import Pointlike, Point2, Point3
from sc2.units import Units
from sc2.unit import Unit
from sc2.ids.unit_typeid import UnitTypeId
from sc2.ids.ability_id import AbilityId
from sc2.ids.buff_id import BuffId
from sc2.ids.upgrade_id import UpgradeId
from sc2.ids.effect_id import EffectId
class TestBot(sc2.BotAI):
def __init__(self):
# Tests related
self.game_time_timeout_limit = 2*60
self.tests_target = 8
self.tests_done_by_name = set()
async def on_step(self, iteration):
if iteration == 0:
await self.chat_send("(glhf)")
if iteration == 1:
# Test if chat message was sent correctly
assert len(self.state.chat) >= 1, self.state.chat
# Tests at start:
if iteration == 5:
# No need to use try except as the travis test script checks for "Traceback" in STDOUT
await self.test_botai_properties()
await self.test_gamestate_static_variables()
await self.test_game_info_static_variables()
await self.test_positions()
await self.test_unit()
await self.test_units()
# Test actions:
if "test_botai_actions1_successful" not in self.tests_done_by_name:
if iteration >= 6:
# Test actions
await self.test_botai_actions1()
# Test if actions were successful
await self.test_botai_actions1_successful()
elif "test_botai_actions2_successful" not in self.tests_done_by_name:
await self.test_botai_actions2()
await self.test_botai_actions2_successful()
elif "test_botai_actions3_successful" not in self.tests_done_by_name:
await self.test_botai_actions3()
await self.test_botai_actions3_successful()
elif "test_botai_actions4_successful" not in self.tests_done_by_name:
await self.test_botai_actions4()
await self.test_botai_actions4_successful()
elif "test_botai_actions5_successful" not in self.tests_done_by_name:
await self.test_botai_actions5()
await self.test_botai_actions5_successful()
# End when all tests successful
if len(self.tests_done_by_name) >= self.tests_target:
print("{}/{} Tests completed after {} seconds: {}".format(len(self.tests_done_by_name), self.tests_target, round(self.time, 1), self.tests_done_by_name))
exit(0)
# End time reached, cancel testing and report error: took too long
if self.time >= self.game_time_timeout_limit:
print("{}/{} Tests completed: {}".format(len(self.tests_done_by_name), self.tests_target, self.tests_done_by_name))
print("Not all tests were successful. Timeout reached. Testing was aborted")
exit(1000)
"""
TODO BotAI tests:
distribute workers, owned expansions, can cast, select build worker, can place, find placement, self build
"""
# Test BotAI properties
async def test_botai_properties(self):
assert 1 <= self.player_id <= 2, self.player_id
assert self.race == Race.Terran, self.race
assert 0 <= self.time <= 180, self.time
assert self.start_location == self.townhalls.random.position, (self.start_location, self.townhalls.random.position)
for loc in self.enemy_start_locations:
assert isinstance(loc, Point2), loc
assert loc.distance_to(self.start_location) > 20, (loc, self.start_location)
assert self.known_enemy_units.amount == 0, self.known_enemy_units
assert self.known_enemy_structures.amount == 0, self.known_enemy_structures
assert self.known_enemy_structures.amount <= self.known_enemy_units.amount
assert self.main_base_ramp.top_center.distance_to(self.start_location) < 30, self.main_base_ramp.top_center
assert len(await self.get_available_abilities(self.workers)) == self.workers.amount
assert self.can_afford(UnitTypeId.SCV)
assert self.minerals >= 50, self.minerals
assert self.vespene == 0, self.vespene
assert self.supply_used == 12, self.supply_used
assert self.supply_cap == 15, self.supply_cap
assert self.supply_left == 3, self.supply_left
self.tests_done_by_name.add("test_botai_properties")
# Test BotAI action: train SCV
async def test_botai_actions1(self):
if self.can_afford(UnitTypeId.SCV):
await self.do(self.townhalls.random.train(UnitTypeId.SCV))
# Test BotAI action: move all SCVs to center of map
async def test_botai_actions2(self):
combined_actions = []
center = self._game_info.map_center
for scv in self.units(UnitTypeId.SCV):
combined_actions.append(scv.move(center))
await self.do_actions(combined_actions)
# Test BotAI action: move some scvs to the center, some to minerals
async def test_botai_actions3(self):
combined_actions = []
center = self._game_info.map_center
scvs = self.workers
scvs1 = scvs[:6]
scvs2 = scvs[6:]
for scv in scvs1:
combined_actions.append(scv.move(center))
mf = self.state.mineral_field.closest_to(self.townhalls.random)
for scv in scvs2:
combined_actions.append(scv.gather(mf))
await self.do_actions(combined_actions)
# Test BotAI action: move all SCVs to mine minerals near townhall
async def test_botai_actions4(self):
combined_actions = []
mf = self.state.mineral_field.closest_to(self.townhalls.random)
for scv in self.units(UnitTypeId.SCV):
combined_actions.append(scv.gather(mf))
await self.do_actions(combined_actions)
# Test BotAI action: self.expand_now()
async def test_botai_actions5(self):
if self.can_afford(UnitTypeId.COMMANDCENTER) and not self.already_pending(UnitTypeId.COMMANDCENTER, all_units=True):
await self.expand_now()
# Test BotAI action results
async def test_botai_actions1_successful(self):
if self.already_pending(UnitTypeId.SCV, all_units=True) > 0:
self.tests_done_by_name.add("test_botai_actions1_successful")
async def test_botai_actions2_successful(self):
if self.units.filter(lambda x: x.is_moving).amount >= 12:
self.tests_done_by_name.add("test_botai_actions2_successful")
async def test_botai_actions3_successful(self):
if self.units.filter(lambda x: x.is_moving).amount >= 6:
if self.units.gathering.amount >= 6:
self.tests_done_by_name.add("test_botai_actions3_successful")
async def test_botai_actions4_successful(self):
if self.units.gathering.amount >= 12:
self.tests_done_by_name.add("test_botai_actions4_successful")
async def test_botai_actions5_successful(self):
if self.units(UnitTypeId.COMMANDCENTER).amount >= 2:
self.tests_done_by_name.add("test_botai_actions5_successful")
# Test self.state variables
async def test_gamestate_static_variables(self):
assert len(self.state.actions) == 0, self.state.actions
assert len(self.state.action_errors) == 0, self.state.action_errors
assert len(self.state.chat) == 0, self.state.chat
assert self.state.game_loop > 0, self.state.game_loop
assert self.state.score.collection_rate_minerals >= 0, self.state.score.collection_rate_minerals
assert len(self.state.destructables) > 0, self.state.destructables
assert self.state.units.amount > 0, self.state.units
assert len(self.state.blips) == 0, self.state.blips
assert len(self.state.upgrades) == 0, self.state.upgrades
# TODO: actions, actionerrors, observation, chat, common, psionic matrix as protoss, score, abilities, blips, pixelmaps, dead units, effects, upgrades
self.tests_done_by_name.add("test_gamestate_static_variables")
# Test self._game_info variables
async def test_game_info_static_variables(self):
assert len(self._game_info.players) == 2, self._game_info.players
assert len(self._game_info.map_ramps) >= 2, self._game_info.map_ramps
assert len(self._game_info.player_races) == 2, self._game_info.player_races
# TODO: map size, pixel maps, playable area, player races, start locations, map center
self.tests_done_by_name.add("test_game_info_static_variables")
# Test positions.py
async def test_positions(self):
p1 = Pointlike((2.3, 2.7))
p2 = Point2((-5.3, -7.9))
p3 = Point3((-2.7, 5.4, 133.2))
# Testing Pointlike
assert p1 == Pointlike((2.3, 2.7))
assert p1.rounded == Pointlike((2, 3))
assert p1.position == p1
assert p1.distance_to(Pointlike((-0.7, 6.7))) == 5
assert p1.closest([
Pointlike((2, 2)),
Pointlike((-2, -2))
]) == Pointlike((2, 2))
assert p1.furthest([
Pointlike((2, 2)),
Pointlike((-2, -2))
]) == Pointlike((-2, -2))
assert p1.offset(Pointlike((-1, -1))) == Pointlike((1.3, 1.7))
assert p1.offset(Pointlike((-1, 1))) == Pointlike((1.3, 3.7))
assert p1.towards(Pointlike((2.3, 50)), 5) == Pointlike((2.3, 7.7))
# testing backwards aswell
assert p1.towards(Pointlike((2.3, 50)), -5) == Pointlike((2.3, -2.3))
# Testing Point2
assert p2.x == -5.3
assert p2.y == -7.9
assert p2.to2 == p2
assert p2.to3 == Point3((-5.3, -7.9, 0))
assert (p2.neighbors4 ==
{
Point2((-5.3, -6.9)),
Point2((-5.3, -8.9)),
Point2((-4.3, -7.9)),
Point2((-6.3, -7.9)),
})
assert p2.neighbors8 == (p2.neighbors4 |
{
Point2((-4.3, -6.9)),
Point2((-4.3, -8.9)),
Point2((-6.3, -6.9)),
Point2((-6.3, -8.9)),
})
# Testing Point3
assert p3.z == 133.2
assert p3.to3 == p3
# Test unit.py
async def test_unit(self):
scv1, scv2, scv3 = self.workers[:3]
assert scv1.type_id == UnitTypeId.SCV
assert scv1._type_data == self._game_data.units[UnitTypeId.SCV.value]
assert scv1.alliance == Alliance.Self.value
assert scv1.is_mine == True
assert isinstance(scv1.position, Point2)
assert isinstance(scv1.position3d, Point3)
assert scv1.health == 45
assert scv1.health_max == 45
assert scv1.health_percentage == 45/45
assert scv1.energy == 0
assert scv1.energy_max == 0
assert scv1.energy_percentage == 0
assert not scv1.target_in_range(self.workers.tags_not_in({scv1.tag}).furthest_to(scv1.position))
assert scv1.target_in_range(scv1)
# Test units.py
async def test_units(self):
scv1, scv2, scv3 = self.workers[:3]
scv_group = self.workers.tags_in({scv1.tag, scv2.tag, scv3.tag})
empty_group = self.workers & []
assert scv_group.amount == 3
assert empty_group.amount == 0
assert not scv_group.empty
assert empty_group.empty
assert scv_group.exists
assert not empty_group.exists
assert scv_group.find_by_tag(scv1.tag) == scv1
assert scv_group.find_by_tag(1337) is None
assert scv_group.random in [scv1, scv2, scv3]
# Test distances and closest/furthest filters
test_point = scv1.position.offset(Point2((0.01, 0.01)))
assert abs(scv_group.closest_distance_to(test_point) - (0.01**2 + 0.01**2)**0.5) < 0.01
assert scv_group.closest_to(test_point) == scv1
assert scv_group.furthest_to(test_point) != scv1
assert scv_group.furthest_to(test_point) in [scv2, scv3]
assert scv_group.closer_than(0.02, test_point) == self.workers.tags_in({scv1.tag})
assert scv_group.closer_than(30, test_point) == scv_group
assert scv_group.further_than(0.02, test_point) == self.workers.tags_in({scv2.tag, scv3.tag})
# Test chained filters
assert scv_group.closer_than(50, test_point).further_than(0.00001, test_point).of_type(UnitTypeId.SCV) == scv_group
assert scv_group.of_type({UnitTypeId.SCV}) == scv_group
assert scv_group.of_type({UnitTypeId.MARINE}) == empty_group
assert scv_group.tags == {u.tag for u in scv_group}
assert scv_group.owned == scv_group
assert scv_group.enemy == empty_group
# TODO:
# Test ramp building placement position
# Test client.py debug functions
def main():
sc2.run_game(sc2.maps.get("(2)CatalystLE"), [
Bot(Race.Terran, TestBot()),
Computer(Race.Zerg, Difficulty.Easy)
], realtime=False)
if __name__ == '__main__':
main()