-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
206 lines (143 loc) · 5.82 KB
/
map.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
from itertools import *
from random import *
import math
from vmath import *
from config import global_config as config
from car import Car
from PySide.QtGui import *
from PySide.QtCore import *
MAP_WIDTH = 100
MAP_HEIGHT = 100
LANE_WIDTH = 1
LINE_WIDTH = 0.125
# Representation of a single road
class RoadGraphic(QGraphicsItem):
LINE_BYTES = '\xff\xff\xff\xff\x00\x00\x00\x00'
LINE_PATTERN = QImage(LINE_BYTES, 1, 2, QImage.Format_ARGB32_Premultiplied)
def __init__(self, start, end, lanes):
super(RoadGraphic, self).__init__()
self.lanes = lanes
self.length = dist(start, end)
self.setPos(start.x, start.y)
self.setRotation(math.degrees(angle(end-start, vec(0,1))))
def boundingRect(self):
width = Road.LANE*sum(self.lanes) + RoadGraphic.LINE
return QRectF(-width/2, 0, width, self.length)
def paint(self, painter, option, widget):
width = Road.LANE*sum(self.lanes) + RoadGraphic.LINE
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.black)
painter.drawRect(QRectF(-width/2, 0, width, self.length))
painter.setBrush(Qt.gray)
painter.drawRect(QRectF(-width/2+RoadGraphic.LINE, 0, width-2*RoadGraphic.LINE, self.length))
painter.setBrush(Qt.yellow)
painter.drawRect(QRectF(-RoadGraphic.LINE/2, 0, RoadGraphic.LINE, self.length))
painter.setBrush(Qt.white)
painter.setBrush(RoadGraphic.LINE_PATTERN)
for i in xrange(1, self.lanes[0]):
painter.drawRect(QRectF(-Road.LANE*i-RoadGraphic.LINE/2, 0, RoadGraphic.LINE, self.length))
for i in xrange(1, self.lanes[1]):
painter.drawRect(QRectF(Road.LANE*i-RoadGraphic.LINE/2, 0, RoadGraphic.LINE, self.length))
config.use('LINE_WIDTH', 0.125, RoadGraphic, 'LINE', float)
class Road:
def __init__(self, start, end, lanes):
self.start = start
self.end = end
self.lanes = lanes
self.graphic = RoadGraphic(start, end, lanes)
def step(self, dt):
pass
def target(self, side):
start = self.end if side else self.start
end = self.start if side else self.end
head = (end - start).norm()
def target(car):
if head * (car.pos - end) > 0:
car.target = self.nt[side]
return car.target(car)
lookahead = car.lookahead_time*abs(car.vel*head)
lookahead = max(lookahead, car.lookahead_min) * head
offset = car.lane*Road.LANE + Road.LANE/2.0
lstart = start + (offset * ~head)
target = projectunit(car.pos - lstart, head) + lstart
return target + lookahead
target.lanes = self.lanes[side]
target.width = Road.LANE
target.head = head
return target
config.use('LANE_WIDTH', 1.0, Road, 'LANE', float)
# Car entry into system
class Spawner:
def __init__(self, pos, head, lanes):
self.pos = pos
self.head = head
self.lanes = lanes
config.use('SPAWN_CHANCE', 10.0, self, 'spawn_chance')
def step(self, dt):
lane = randint(0, self.lanes[0]-1)
offset = lane*Road.LANE + Road.LANE/2.0
offset = (offset * ~self.head) + self.pos
if random() < dt*self.spawn_chance:
c = Car(self.nt[0], lane, offset, self.head)
self.map.add(c)
if c.collisions():
self.map.remove(c)
def target(self):
return lambda car: None
# Representation of all static world elements
class MapGraphic(QGraphicsItem):
def boundingRect(self):
return QRectF(0, 0, Map.WIDTH, Map.HEIGHT)
def paint(self, painter, option, widget):
painter.setPen(Qt.NoPen)
painter.setBrush(Qt.black)
painter.drawRect(-Map.WIDTH, -Map.HEIGHT, 3*Map.WIDTH, 3*Map.HEIGHT)
painter.setBrush(Qt.darkGreen)
painter.drawRect(0, 0, Map.WIDTH, Map.HEIGHT)
class Map:
def __init__(self):
self.graphic = MapGraphic()
self.time = 0
self.collisions = 0
self.entities = []
self.vehicles = []
#r = Road(vec(0, 0), vec(Map.WIDTH, Map.HEIGHT), (5,5))
#s0 = Spawner(vec(0, 0), vec(1,1).norm(), (5,5))
#s1 = Spawner(vec(100, 100), vec(-1,-1).norm(), (5,5))
r = Road(vec(50, 0), vec(50, 100), (4,4))
s0 = Spawner(vec(50, 0), vec(0,1).norm(), (4,4))
s1 = Spawner(vec(50, 100), vec(0,-1).norm(), (4,4))
r.nt = s0.target(), s1.target()
s0.nt = r.target(0),
s1.nt = r.target(1),
self.add(r)
self.add(s0)
self.add(s1)
def reset(self):
for c in list(self.vehicles):
self.remove(c)
self.time = 0
self.collisions = 0
def add(self, entry):
entry.map = self
if isinstance(entry, Car):
self.vehicles.append(entry)
else:
self.entities.append(entry)
if hasattr(entry, 'graphic'):
entry.graphic.setParentItem(self.graphic)
def remove(self, entry):
try:
self.vehicles.remove(entry)
except:
self.entities.remove(entry)
if hasattr(entry, 'graphic'):
entry.graphic.setParentItem(None)
def step(self, dt):
for i in self.vehicles:
i.step(dt)
for i in self.entities:
i.step(dt)
self.time += dt
config.use('MAP_WIDTH', 100, Map, 'WIDTH', int)
config.use('MAP_HEIGHT', 100, Map, 'HEIGHT', int)