-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
172 lines (123 loc) · 6.05 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
# -*- coding: utf-8 -*-
# python imports
import random
# chillin imports
from chillin_client import RealtimeAI
# project imports
from ks.models import ECell, EDirection, Position
from ks.commands import ChangeDirection, ActivateWallBreaker
from scripts.Tron import Tron
from time import perf_counter
class AI(RealtimeAI):
def __init__(self, world):
super(AI, self).__init__(world)
def initialize(self):
self.tron = Tron()
self.tron.set_walls()
self.tron.show_banner()
agents = self.world.agents
names = [self.my_side, self.other_side]
scores = self.world.scores
board = self.world.board
curr_cycle = self.current_cycle
crash_score = self.world.constants.area_wall_crash_score
self.tron.set_requirements(agents, names, scores, board, curr_cycle, crash_score)
self.tron.init_file()
self.tron.identify_walls(True)
self.tron.find_walls_neighbors()
self.tron.find_closest_area_wall()
self.tron.find_target_handler()
def decide(self):
tt1 = perf_counter()
agents = self.world.agents
names = [self.my_side, self.other_side]
scores = self.world.scores
board = self.world.board
curr_cycle = self.current_cycle
crash_score = self.world.constants.area_wall_crash_score
self.tron.set_requirements(agents, names, scores, board, curr_cycle, crash_score)
self.tron.log_string += f'cycle: {self.current_cycle}\n'
print(f'cycle: {self.current_cycle}')
self.tron.log_string += f'current_position: {(agents[self.my_side].position.y, agents[self.my_side].position.x)}\n'
t1 = perf_counter()
self.tron.set_walls()
self.tron.identify_walls(False)
t2 = perf_counter()
print('identify walls: {:.5f}'.format(t2-t1))
t1 = perf_counter()
self.tron.update_state()
t2 = perf_counter()
print('update_state: {:.5f}'.format(t2-t1))
t1 = perf_counter()
if self.tron.is_target_reached():
self.tron.log_string += 'target reached!\n'
if self.tron.agent_state == 'attack':
self.tron.log_string += "target reached! state is attack\n"
if self.tron.agent_attack_state == 'onway':
self.tron.agent_attack_state = 'attacking'
self.target_passed_cycles = len(self.tron.attacking_reaching_path)
self.tron.target_pos = self.tron.attacking_reaching_path[-1]
self.tron.target_wall_type = self.tron.get_wall_type(self.tron.target_pos)
self.tron.try_again_cycles = 0
else:
self.tron.agent_attack_state = 'onway'
self.tron.reaching_path_index = 0
self.tron.find_target_handler()
else:
self.tron.log_string += "target reached! state isn't attack!\n"
self.tron.find_target_handler()
elif self.tron.is_target_cycles_exceeded() or self.tron.agent_state == 'brutal':
self.tron.log_string += 'target cycles_exceeded!\n'
self.tron.find_target_handler()
elif self.tron.is_target_wall_type_changed():
self.tron.log_string += 'wall_type_changedd!\n'
self.tron.find_target_handler()
if self.tron.agent_state == 'attack' and self.tron.agent_attack_state == 'onway':
if self.tron.is_attack_target_increased():
self.tron.log_string += 'increased!\n'
self.tron.find_target_handler()
elif self.tron.try_again_cycles == 0:
if self.tron.is_attack_target_got_unreachable():
self.tron.log_string += 'attack target got unreachable\n'
self.tron.find_target_handler()
t2 = perf_counter()
# print('find_target: {:.5f}'.format(t2-t1))
if self.tron.agent_state == 'attack':
self.tron.check_pattern_changing()
if self.tron.agent_attack_state == 'attacking':
if self.tron.reaching_path_index >= self.tron.attacking_route_opp_walls - 1:
self.tron.update_attacking_path()
if self.tron.agent_state != 'attack' or (self.tron.agent_state == 'attack' and self.tron.agent_attack_state == 'onway'):
t1 = perf_counter()
ignore_cost = False
if self.tron.try_again_cycles != 0:
ignore_cost = True
agent_pos = (self.tron.agent.position.y, self.tron.agent.position.x)
best_route = self.tron.find_best_route(agent_pos, self.tron.target_pos, ignore_cost)
self.tron.reaching_path = best_route
if self.tron.agent_state == 'attack' and self.tron.agent_attack_state == 'onway':
self.tron.best_attack_target_path = best_route
t2 = perf_counter()
print('find_next_route {:.5f}'.format(t2-t1))
t1 = perf_counter()
if self.tron.agent_state != 'suicide':
target_weight = self.tron.get_wall_weight(self.tron.target_pos)
else:
target_weight = 0
# self.tron.log_string += f'target: {self.tron.target_pos} weight: {target_weight}\n\n'
# self.tron.show_walls_info()
t2 = perf_counter()
# self.tron.log_string += 'showing things {:.5f}\n'.format(t2-t1)
if self.tron.is_wallbreaker_needed():
if agents[self.my_side].wall_breaker_cooldown == 0:
self.send_command(ActivateWallBreaker())
next_dir = self.tron.next_dir()
self.send_command(ChangeDirection(next_dir))
# t1 = perf_counter()
# self.tron.output_log()
# t2 = perf_counter()
# print('log time: {:.5f}'.format(t2-t1))
tt2 = perf_counter()
overall_time = tt2-tt1
# self.tron.log_string += 'overall_time: {:.5f}\n\n\n'.format(overall_time)
print('overall_time: {:.5f}\n\n\n'.format(overall_time))