-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbot.py
More file actions
152 lines (123 loc) · 5.24 KB
/
bot.py
File metadata and controls
152 lines (123 loc) · 5.24 KB
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
import random
from battlecode25.stubs import *
# This is an example bot written by the developers!
# Use this to help write your own code, or run it against your bot to see how well you can do!
# Globals
turn_count = 0
directions = [
Direction.NORTH,
Direction.NORTHEAST,
Direction.EAST,
Direction.SOUTHEAST,
Direction.SOUTH,
Direction.SOUTHWEST,
Direction.WEST,
Direction.NORTHWEST,
]
def turn():
"""
MUST be defined for robot to run
This function will be called at the beginning of every turn and should contain the bulk of your robot commands
"""
global turn_count
turn_count += 1
if get_type() == UnitType.SOLDIER:
run_soldier()
elif get_type() == UnitType.MOPPER:
run_mopper()
elif get_type() == UnitType.SPLASHER:
pass # TODO
elif get_type().is_tower_type():
run_tower()
else:
pass # Other robot types?
def run_tower():
# Pick a direction to build in.
dir = directions[random.randint(0, len(directions) - 1)]
next_loc = get_location().add(dir)
# Pick a random robot type to build.
robot_type = random.randint(0, 2)
if robot_type == 0 and can_build_robot(UnitType.SOLDIER, next_loc):
build_robot(UnitType.SOLDIER, next_loc)
log("BUILT A SOLDIER")
if robot_type == 1 and can_build_robot(UnitType.MOPPER, next_loc):
build_robot(UnitType.MOPPER, next_loc)
log("BUILT A MOPPER")
if robot_type == 2 and can_build_robot(UnitType.SPLASHER, next_loc):
set_indicator_string("SPLASHER NOT IMPLEMENTED YET");
#build_robot(RobotType.SPLASHER, next_loc)
#log("BUILT A SPLASHER")
# Read incoming messages
messages = read_messages()
for m in messages:
log(f"Tower received message: '#{m.get_sender_id()}: {m.get_bytes()}'")
# TODO: can we attack other bots?
def run_soldier():
# Sense information about all visible nearby tiles.
nearby_tiles = sense_nearby_map_infos()
# Search for a nearby ruin to complete.
cur_ruin = None
for tile in nearby_tiles:
if tile.has_ruin():
cur_ruin = tile
if cur_ruin is not None:
target_loc = cur_ruin.get_map_location()
dir = get_location().direction_to(target_loc)
if can_move(dir):
move(dir)
# Mark the pattern we need to draw to build a tower here if we haven't already.
should_mark = cur_ruin.get_map_location().subtract(dir)
if sense_map_info(should_mark).get_mark() == PaintType.EMPTY and can_mark_tower_pattern(UnitType.LEVEL_ONE_PAINT_TOWER, target_loc):
mark_tower_pattern(UnitType.LEVEL_ONE_PAINT_TOWER, target_loc)
log("Trying to build a tower at " + str(target_loc))
# Fill in any spots in the pattern with the appropriate paint.
for pattern_tile in sense_nearby_map_infos(target_loc, 8):
if pattern_tile.get_mark() != pattern_tile.get_paint() and pattern_tile.get_mark() != PaintType.EMPTY:
use_secondary = pattern_tile.get_mark() == PaintType.ALLY_SECONDARY
if can_attack(pattern_tile.get_map_location()):
attack(pattern_tile.get_map_location(), use_secondary)
# Complete the ruin if we can.
if can_complete_tower_pattern(UnitType.LEVEL_ONE_PAINT_TOWER, target_loc):
complete_tower_pattern(UnitType.LEVEL_ONE_PAINT_TOWER, target_loc)
set_timeline_marker("Tower built", 0, 255, 0)
log("Built a tower at " + str(target_loc) + "!")
# Move and attack randomly if no objective.
dir = directions[random.randint(0, len(directions) - 1)]
next_loc = get_location().add(dir)
if can_move(dir):
move(dir)
# Try to paint beneath us as we walk to avoid paint penalties.
# Avoiding wasting paint by re-painting our own tiles.
current_tile = sense_map_info(get_location())
if not current_tile.get_paint().is_ally() and can_attack(get_location()):
attack(get_location())
def run_mopper():
# Move and attack randomly.
dir = directions[random.randint(0, len(directions) - 1)]
next_loc = get_location().add(dir)
if can_move(dir):
move(dir)
if can_mop_swing(dir):
mop_swing(dir)
log("Mop Swing! Booyah!");
elif can_attack(next_loc):
attack(next_loc)
# We can also move our code into different methods or classes to better organize it!
update_enemy_robots()
def update_enemy_robots():
# Sensing methods can be passed in a radius of -1 to automatically
# use the largest possible value.
enemy_robots = sense_nearby_robots(team=get_team().opponent())
if len(enemy_robots) == 0:
return
set_indicator_string("There are nearby enemy robots! Scary!");
# Save an array of locations with enemy robots in them for possible future use.
enemy_locations = [None] * len(enemy_robots)
for i in range(len(enemy_robots)):
enemy_locations[i] = enemy_robots[i].get_location()
# Occasionally try to tell nearby allies how many enemy robots we see.
ally_robots = sense_nearby_robots(team=get_team())
if get_round_num() % 20 == 0:
for ally in ally_robots:
if can_send_message(ally.location):
send_message(ally.location, len(enemy_robots))