/
Sneaky.py
145 lines (119 loc) · 4.77 KB
/
Sneaky.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
#from pandas.hashtable import na_sentinel
import hlt
from hlt import NORTH, EAST, SOUTH, WEST, STILL, Move, Square
import random
import logging
from collections import namedtuple
myID, game_map, targetCoord = hlt.get_init()
hlt.send_init("SneakyBevster")
logging.basicConfig(filename=str(myID) + '.log', level=logging.DEBUG)
def find_nearest_enemy_direction(square):
direction = NORTH
max_distance = min(game_map.width, game_map.height) / 2
for d in (NORTH, EAST, SOUTH, WEST):
distance = 0
current = square
while (current.owner == myID or current.owner==0) and distance < max_distance:
distance += 1
current = game_map.get_target(current, d)
if distance < max_distance:
direction = d
max_distance = distance
return direction
def heuristic(square):
if square.owner == 0 and square.strength > 0:
return square.production / square.strength
else:
# return total potential damage caused by overkill when attacking this square
return sum(neighbor.strength for neighbor in game_map.neighbors(square) if neighbor.owner not in (0, myID))
def get_move(square):
target, direction = max(((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
if neighbor.owner != myID),
default=(None, None),
key=lambda t: heuristic(t[0]))
if target is not None and target.strength < square.strength:
return Move(square, direction)
elif square.strength < square.production * 5: #and square.strength<100
return Move(square, STILL)
border = any(neighbor.owner != myID for neighbor in game_map.neighbors(square))
if not border:
#if square.strength < square.production * 2:
# return Move(square, STILL)
#else:
return Move(square, find_nearest_enemy_direction(square))
else:
# wait until we are strong enough to attack
return Move(square, STILL)
def get_xydistance(self, sq1, sq2):
"Returns Manhattan distance between two squares."
dx = min(abs(sq1.x - sq2.x), sq1.x + self.width - sq2.x, sq2.x + self.width - sq1.x)
dy = min(abs(sq1.y - sq2.y), sq1.y + self.height - sq2.y, sq2.y + self.height - sq1.y)
return dx,dy
def find_nearest_dense(square,targetCoord):
direc = NORTH
lrDist = targetCoord.x- square.x
lrFlip = False
if abs(lrDist)>game_map.width/2:
lrFlip = True
if lrDist !=0:
if lrFlip:
if (lrDist) > 0:
neighbour = game_map.get_target(square, WEST)
direc = WEST
else:
neighbour = game_map.get_target(square, EAST)
direc = EAST
else:
if (lrDist)>0:
neighbour = game_map.get_target(square, EAST)
direc = EAST
else:
neighbour = game_map.get_target(square, WEST)
direc = WEST
maxStrength = neighbour.strength
else:
maxStrength = 10000
udDist = targetCoord.y - square.y
udFlip = False
if abs(udDist)>game_map.height/2:
udFlip = True
if udDist != 0:
if udFlip:
if (udDist) < 0:
neighbour = game_map.get_target(square, SOUTH)
if neighbour.strength<maxStrength:
direc = SOUTH
else:
neighbour = game_map.get_target(square, NORTH)
if neighbour.strength<maxStrength:
direc = NORTH
else:
if (udDist) < 0:
neighbour = game_map.get_target(square, NORTH)
if neighbour.strength<maxStrength:
direc = NORTH
else:
neighbour = game_map.get_target(square, SOUTH)
if neighbour.strength<maxStrength:
direc = SOUTH
#logging.debug(str(direc) + " " + str(square) + " " + str(targetCoord))
neighbour = game_map.get_target(square, direc)
if neighbour.strength < square.strength:
return Move(square, direc)
else:# square.strength < square.production * 5: #and square.strength<100
return Move(square, STILL)
target = False
while True:
game_map.get_frame()
if target==False:
#logging.debug(ma)
#first = False
moves = [find_nearest_dense(square,targetCoord) for square in game_map if square.owner == myID]
for square in game_map:
if square.x == targetCoord.x and square.y == targetCoord.y and square.owner == myID:
target = True
logging.debug(target)
hlt.send_frame(moves)
else:
moves = [get_move(square) for square in game_map if square.owner == myID]
hlt.send_frame(moves)