-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
208 lines (153 loc) · 6.03 KB
/
ai.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
from settings import *
from ai_math import *
import ai_math
import main, sound
from math import *
from random import seed, randint, uniform
seed(42)
def ai_grey(t, tick_time):
if not t.ai_has_target:
if abs(t.aim - t.aim_target) < 0.02:
t.aim_speed = uniform(0.005, 0.02)
t.aim_target = uniform(0, 1)
if not t.stop and t.bullets_shot < t.bullet_limit:
for _ in range(3):
r = t.aim + uniform(-0.3, 0.3)
try_res = try_bullet(t, r)
if try_res[0] and ((try_res[1] == 0 and randint(0, 20) == 0) or (try_res[1] >= 1 and randint(0, 200) == 0)):
t.ai_has_target = True
t.aim_target = r
t.aim_speed = 0.2
elif abs(t.aim - t.aim_target) < 0.005:
t.shoot()
t.ai_has_target = False
def ai_yellow(t, tick_time):
if has_sight(t):
t.ai_has_target = True
t.aim_speed = 0.2
t.aim_target = sight_angle(t)
if randint(0, 100) == 0 and abs(t.aim - t.aim_target) < 0.01:
t.shoot()
elif abs(t.aim - t.aim_target) < 0.1:
t.ai_has_target = False
t.aim_speed = 0.005
t.aim_target = uniform(0, 1)
t.ai_target_timeout -= 1
if t.ai_target_timeout <= 0 or len(t.ai_move_targets) == 0:
t.ai_target_timeout = randint(180, 420)
tar = (randint(0, FIELDWIDTH-1), randint(0, FIELDHEIGHT-1))
while main.map[tar[0]][tar[1]] != None or ai_math.tank_mapout[tar[0]][tar[1]] not in (0, main.tanks.index(t), None):
tar = (randint(0, FIELDWIDTH-1), randint(0, FIELDHEIGHT-1))
path_target(t, *tar)
else:
move_target(t)
def ai_blue(t, tick_time):
if not has_sight(t):
t.ai_has_target = False
t.aim_speed = 0.2
t.ai_shot_timer = -1
elif has_sight(t) and randint(0, 300) == 0:
t.ai_has_target = True
t.aim_speed = 0.5
t.ai_shot_timer = randint(0, 49)
t.aim_target = sight_angle(t) + uniform(-0.02, 0.02)
if t.ai_shot_timer % 10 == 0:
t.shoot()
t.aim_target = sight_angle(t) + uniform(-0.02, 0.02)
if t.ai_shot_timer >= 0:
t.ai_shot_timer -= 1
else:
t.aim_target = sight_angle(t)
t.ai_target_timeout -= 1
if t.ai_target_timeout <= 0 or (len(t.ai_move_targets) == 0 and not t.ai_stuck):
t.ai_target_timeout = randint(60, 180)
path_target(t, int(main.tanks[0].x + 0.5), int(main.tanks[0].y + 0.5))
else:
move_target(t)
def ai_green(t, tick_time):
t.ai_has_target = False
if abs(t.aim - t.aim_target) < 0.1:
t.aim_speed = 0.05
t.aim_target = uniform(0, 1)
t.ai_target_timeout -= 1
def find_tar():
tar = (randint(1, FIELDWIDTH-2), randint(1, FIELDHEIGHT-2))
while main.map[tar[0]][tar[1]] != None or ai_math.tank_mapout[tar[0]][tar[1]] not in (0, main.tanks.index(t), None):
tar = (randint(1, FIELDWIDTH-2), randint(1, FIELDHEIGHT-2))
for x in range(-2, 3):
for y in range(-2, 3):
if (main.map[max(min(tar[0]+x, FIELDWIDTH-1), 0)][max(min(tar[1]+y, FIELDHEIGHT-1), 0)] == None and
(main.map[max(min(tar[0]+x+1, FIELDWIDTH-1), 0)][max(min(tar[1]+y, FIELDHEIGHT-1), 0)] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x-1][tar[1]+y] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x][tar[1]+y+1] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x+1][tar[1]+y-1] in ["dwall1", "dwall2", "dwall3"])):
return (tar[0]+x, tar[1]+y)
return tar
if t.ai_stuck:
t.ai_target_timeout = randint(0, 5)
if t.ai_target_timeout <= 0 or len(t.ai_move_targets) == 0:
t.ai_target_timeout = randint(420, 540)
path_target(t, *find_tar())
else:
tar_len = len(t.ai_move_targets)
move_target(t)
if len(t.ai_move_targets) == 0 and randint(0, 2) == 0:
t.place_mine()
if (tick_time % 100 == 0 or len(t.ai_move_targets) == tar_len - 1) and len(t.ai_move_targets) > 1:
if ai_math.tank_mapout[t.ai_move_targets[-1][0]][t.ai_move_targets[-1][1]] == "MINE":
path_target(t, *find_tar())
else:
path_target(t, *t.ai_move_targets[-1])
def ai_red(t, tick_time):
if not t.ai_has_target:
if abs(t.aim - t.aim_target) < 0.02:
t.aim_speed = 0.5
t.aim_target = sight_angle(t)
if not t.stop and t.bullets_shot < t.bullet_limit:
for _ in range(5):
r = t.aim + uniform(-0.5, 0.5)
try_res = try_bullet(t, r)
if try_res[0] and (try_res[1] >= 3 and randint(0, 10) == 0):
t.ai_has_target = True
t.aim_target = r
t.aim_speed = 0.5
elif abs(t.aim - t.aim_target) < 0.002:
t.shoot()
t.ai_has_target = False
def ai_black(t, tick_time):
if not t.ai_has_target:
if abs(t.aim - t.aim_target) < 0.02:
t.aim_speed = 0.5
t.aim_target = sight_angle(t)
if not t.stop and t.bullets_shot < t.bullet_limit:
for _ in range(5):
r = t.aim + uniform(-0.5, 0.5)
try_res = try_bullet(t, r)
if try_res[0] and (try_res[1] >= 1 and randint(0, 10) == 0):
t.ai_has_target = True
t.aim_target = r
t.aim_speed = 0.5
elif abs(t.aim - t.aim_target) < 0.002:
t.shoot()
t.ai_has_target = False
t.ai_target_timeout -= 1
def find_tar():
tar = (randint(1, FIELDWIDTH-2), randint(1, FIELDHEIGHT-2))
while main.map[tar[0]][tar[1]] != None or ai_math.tank_mapout[tar[0]][tar[1]] not in (0, main.tanks.index(t), None):
tar = (randint(1, FIELDWIDTH-2), randint(1, FIELDHEIGHT-2))
for x in range(-2, 3):
for y in range(-2, 3):
if (main.map[max(min(tar[0]+x, FIELDWIDTH-1), 0)][max(min(tar[1]+y, FIELDHEIGHT-1), 0)] == None and
(main.map[max(min(tar[0]+x+1, FIELDWIDTH-1), 0)][max(min(tar[1]+y, FIELDHEIGHT-1), 0)] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x-1][tar[1]+y] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x][tar[1]+y+1] in ["dwall1", "dwall2", "dwall3"] or main.map[tar[0]+x+1][tar[1]+y-1] in ["dwall1", "dwall2", "dwall3"])):
return (tar[0]+x, tar[1]+y)
return tar
if t.ai_target_timeout <= 0 or len(t.ai_move_targets) == 0:
t.ai_target_timeout = randint(180, 420)
path_target(t, *find_tar())
else:
tar_len = len(t.ai_move_targets)
move_target(t)
if len(t.ai_move_targets) == 0 and randint(0, 2) == 0:
t.place_mine()
if len(t.ai_move_targets) == tar_len - 1 and len(t.ai_move_targets) > 1:
if ai_math.tank_mapout[t.ai_move_targets[-1][0]][t.ai_move_targets[-1][1]] == "MINE":
path_target(t, *find_tar())
else:
path_target(t, *t.ai_move_targets[-1])