-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworld.py
More file actions
123 lines (107 loc) · 4.34 KB
/
world.py
File metadata and controls
123 lines (107 loc) · 4.34 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
"""
World:
Holds the map.
"""
from car import Car
# import scipy.ndimage as misc
from random import randint
import pickle
import numpy as np
class World_Map():
def __init__(self, size):
self.world_map = np.zeros(size)
self.world_map_size = size
# Quick wall for testing
for i in range(size[0]):
self.world_map[i][i] = 1
print("Made world_map")
class World():
def __init__(self, size=(1000, 1000), map_name='NONE'):
"""
Initializes the numpy matrix for the road and creates a car.
"""
self.started = False
# self.road = World_Map(size)
self.size = size
self.road = np.zeros(size)
self.track_points = []
# self.road = misc.imread('assets/track.png', mode='L')
self.car_start_pos = (500, 500)
self.car_start_angle = 0
map_selected, self.autopilot_style = pickle.load(open("map_name.p", "rb"))
if map_name != "None":
self.draw_new = False
else:
self.draw_new = True
self.reward_matrix = np.zeros(size)
try:
# print('Updated Road and reward files')
if map_name != 'None':
file_add = map_name + '/'
else:
file_add = ''
self.road = pickle.load(open(file_add + "road.p", "rb"))
self.car_start_position, self.car_start_angle = pickle.load(open(file_add + "pos_ang.p", "rb"))
self.started = True
self.reward_matrix = pickle.load(open(file_add + "reward.p", "rb"))
self.draw_new = False
except:
# print(file_add + "pos_ang.p")
# print('Pickle Files Not Found.')
pass
self.car = Car(self.road, size, [200, 500], [0, 1], [0.1, 0],
car_color=(randint(0, 255),
randint(0, 255),
randint(0, 255)))
self.checkpoints = []
def detect_crash(self):
"""
Returns True if the car has crashed, False otherwise
"""
# print('test', np.all(self.reward_matrix == 0), self.reward_matrix)
# print('started:', self.started)
if self.started is False:
return False
else:
try:
hit_points = [self.road[pos] == 0 for pos in self.car.points]
return any(hit_points)
except:
return True
def reset_car(self):
"""
Resets the position, angle, and velocity of the car.
"""
self.car.position = self.car_start_pos # Set the car at the starting point
self.car.angle = [self.car_start_angle, 0] # Set the car at the starting heading
self.car.velocity = [0, 0] # Stop the car
self.car.visible = True
def update_checkpoints(self, num_checkpoints=100):
"""
Builds a list of checkpoints based on a list of mouse movement points.
"""
track_points_len = len(self.track_points)
checkpoint_indices = [int(track_points_len/(num_checkpoints+1)*val) for val in range(1, num_checkpoints+1)]
self.checkpoints = [self.track_points[idx] for idx in checkpoint_indices]
combined_save = [self.car_start_pos, self.car_start_angle]
pickle.dump(self.road, open('road.p', 'wb'))
pickle.dump(combined_save, open('pos_ang.p', 'wb'))
self.started = True
self.draw_new = False
# print(self.autopilot_style)
if self.autopilot_style != '3':
self.update_reward_matrix()
pickle.dump(self.reward_matrix, open('reward.p', 'wb'))
def update_reward_matrix(self):
checks = len(self.checkpoints)
for value, checkpoint in enumerate(self.checkpoints):
c_value = (value + .1) / 10
radius = 50
print('Rendering Reward Matrix:', (int)((value/checks)*10000) / 100, 'Percent Complete', end='\r')
for y, row in enumerate(self.reward_matrix):
for x, rand in enumerate(row):
x_dist = abs(x - checkpoint[0])
y_dist = abs(y - checkpoint[1])
if x_dist < radius and y_dist < radius and rand == 0:
self.reward_matrix[y][x] = c_value
print(' ', end='\r')