This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomputercarrepresentation.py
291 lines (237 loc) · 9.54 KB
/
computercarrepresentation.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from logicalcarrepresentation import LogicalCarRepresentation
import math
from destinationlane import DestinationLane
import random
from qualifyingsessionstatus import QualifyingSessionStatus
from racesessionstatus import RaceSessionStatus
from sessiontype import SessionType
class ComputerCarRepresentation(LogicalCarRepresentation):
def __init__(self,trackSession):
super(ComputerCarRepresentation, self).__init__(trackSession)
self.hasArrivedAtDestinationLane=True
self.topSpeed=0.675+random.random()/5
self.targetCornerSpeed=self.topSpeed
self.targetCarInteractionSpeed=self.topSpeed
self.lastMoveWasWrap=False
def getLastMoveWasWrap(self):
return self.lastMoveWasWrap
def setLastMoveWasWrap(self,lastMoveWasWrap):
self.lastMoveWasWrap=lastMoveWasWrap
def setCurrentLane(self,currentLane):
self.destinationLane=currentLane
self.hasArrivedAtDestinationLane=True
def setDestinationLane(self,destinationLane):
self.destinationLane=destinationLane
self.hasArrivedAtDestinationLane=False
def getDestinationLane(self):
return self.destinationLane
def approachDestinationLane(self):
if not self.hasArrivedAtDestinationLane:
sidewaysPosition=self.getSidewaysPosition()
if self.destinationLane==DestinationLane.left:
sidewaysPosition=sidewaysPosition-0.0075
self.setSidewaysMovement(-0.0075)
if sidewaysPosition<DestinationLane.leftPosition:
sidewaysPosition=DestinationLane.leftPosition
self.hasArrivedAtDestinationLane=True
else:
sidewaysPosition=sidewaysPosition+0.0075
self.setSidewaysMovement(0.0075)
if sidewaysPosition>DestinationLane.rightPosition:
sidewaysPosition=DestinationLane.rightPosition
self.hasArrivedAtDestinationLane=True
self.setSidewaysPosition(sidewaysPosition)
def avoidCollisions(self):
logicalCars=self.trackSession.getLogicalCarRepresentations()
threatInSameLane=False
threatInOtherLane=False
overtakeRequired=False
overtakingWouldBeStupid=False
speedOfClosestSameLaneThreat=1000
distanceOfClosestSameLaneThreat=1000
if not self.hasArrivedAtDestinationLane:
overtakingWouldBeStupid=True
for otherCar in logicalCars:
if not otherCar is self:
segmentPositionDifference=otherCar.getSegmentPosition()-self.getSegmentPosition()
speedDifference=self.getSpeed()-otherCar.getSpeed()
if segmentPositionDifference<15.0 and segmentPositionDifference>0.0:
if otherCar.getDestinationLane()==self.getDestinationLane():
threatInSameLane=True
if segmentPositionDifference<distanceOfClosestSameLaneThreat:
distanceOfClosestSameLaneThreat=segmentPositionDifference
speedOfClosestSameLaneThreat=otherCar.getSpeed()
else:
threatInOtherLane=True
if segmentPositionDifference<15.0 and segmentPositionDifference>-5.0 and not otherCar.hasArrivedAtDestinationLane:
overtakingWouldBeStupid=True
if threatInSameLane and threatInOtherLane:
if distanceOfClosestSameLaneThreat<5.0 and speedDifference>0.1:
self.setTargetCarInteractionSpeed(speedOfClosestSameLaneThreat-0.025)
else:
self.setTargetCarInteractionSpeed(speedOfClosestSameLaneThreat)
else:
if threatInSameLane and overtakingWouldBeStupid:
self.setTargetCarInteractionSpeed(speedOfClosestSameLaneThreat)
else:
self.setTargetCarInteractionSpeed(self.topSpeed)
if threatInSameLane and self.getSpeed()>speedOfClosestSameLaneThreat and not overtakingWouldBeStupid:
currentLane=self.getDestinationLane()
if currentLane==DestinationLane.left:
destinationLane=DestinationLane.right
else:
destinationLane=DestinationLane.left
self.setDestinationLane(destinationLane)
def setCornerSpeed(self):
currentSegment=int(math.floor(self.getSegmentPosition()))
sharpness=math.fabs(self.trackSession.getCornerSharpness(currentSegment+10))
self.setTargetCornerSpeed(0.85-sharpness*4)
pass
def getSpeed(self):
return self.speed
def setTargetCornerSpeed(self,targetSpeed):
if targetSpeed<0:
targetSpeed=0
if targetSpeed>self.topSpeed:
targetSpeed=self.topSpeed
self.targetCornerSpeed=targetSpeed
def setTargetCarInteractionSpeed(self,targetSpeed):
if targetSpeed<0:
targetSpeed=0
if targetSpeed>self.topSpeed:
targetSpeed=self.topSpeed
self.targetCarInteractionSpeed=targetSpeed
def getTargetSpeed(self):
speeds=[self.targetCornerSpeed,self.targetCarInteractionSpeed,self.trackSession.getSpeedLimit()]
return min(speeds)
#if self.targetCornerSpeed<self.targetCarInteractionSpeed:
# return self.targetCornerSpeed
#else:
# return self.targetCarInteractionSpeed
def updateSpeed(self):
if self.getExploding():
self.setTargetCornerSpeed(0)
self.setTargetCarInteractionSpeed(0)
speed=self.getSpeed()
speed=speed-0.0075
if speed<0:
speed=0
self.setSpeed(speed)
else:
targetSpeed=self.getTargetSpeed()
if self.getHandbrake()==True:
targetSpeed=0
speed=self.getSpeed()
if speed>targetSpeed:
speed=speed-0.01
if speed<targetSpeed:
speed=targetSpeed
else:
speed=speed+self.topSpeed/700
if speed>targetSpeed:
speed=targetSpeed
self.setSpeed(speed)
def checkPlayerRelativeBoundary(self):
self.setLastMoveWasWrap(False)
playerCar=self.trackSession.getPlayerCar()
if not playerCar:
playerCar=self.trackSession.getCameraCar()
playerSegmentPosition=playerCar.getSegmentPosition()
selfSegmentPosition=self.getSegmentPosition()
selfSpeed=self.getSpeed()
forwardZoneSize=120.0
backwardZoneSize=20.0
insideForwardBoundary=False
forwardBoundaryWrapped=False
forwardBoundary=playerSegmentPosition+forwardZoneSize
if forwardBoundary>len(self.trackSession.getSegments()):
forwardBoundary=forwardBoundary-len(self.trackSession.getSegments())
forwardBoundaryWrapped=True
if forwardBoundaryWrapped:
if selfSegmentPosition>=playerSegmentPosition or selfSegmentPosition<=forwardBoundary:
insideForwardBoundary=True
else:
if selfSegmentPosition>=playerSegmentPosition and selfSegmentPosition<=forwardBoundary:
insideForwardBoundary=True
insideBackwardBoundary=False
backwardBoundaryWrapped=False
backwardBoundary=playerSegmentPosition-backwardZoneSize
if backwardBoundary<0:
backwardBoundary=backwardBoundary+len(self.trackSession.getSegments())
backwardBoundaryWrapped=True
if backwardBoundaryWrapped:
if selfSegmentPosition<=playerSegmentPosition or selfSegmentPosition>=backwardBoundary:
insideBackwardBoundary=True
else:
if selfSegmentPosition>=backwardBoundary and selfSegmentPosition<=playerSegmentPosition:
insideBackwardBoundary=True
if not insideForwardBoundary and not insideBackwardBoundary:
selfAheadOfPlayer=False
halfTrackBoundaryWrapped=False
halfTrackBoundary=playerSegmentPosition+len(self.trackSession.getSegments())/2
if halfTrackBoundary>len(self.trackSession.getSegments()):
halfTrackBoundary=forwardBoundary-len(self.trackSession.getSegments())
halfTrackBoundaryWrapped=True
if halfTrackBoundaryWrapped:
if selfSegmentPosition>playerSegmentPosition or selfSegmentPosition<halfTrackBoundary:
selfAheadOfPlayer=True
else:
if selfSegmentPosition>playerSegmentPosition and selfSegmentPosition<halfTrackBoundary:
selfAheadOfPlayer=True
if selfAheadOfPlayer:
self.setLastMoveWasWrap(True)
selfSpeed=0.01
selfSegmentPosition=playerSegmentPosition-backwardZoneSize
if selfSegmentPosition<0:
selfSegmentPosition=selfSegmentPosition+len(self.trackSession.getSegments())
else:
self.setLastMoveWasWrap(True)
selfSegmentPosition=playerSegmentPosition+forwardZoneSize
selfSpeed=playerCar.getSpeed()-0.05
if self.speed>self.topSpeed:
self.speed=self.topSpeed
if selfSegmentPosition>len(self.trackSession.getSegments()):
selfSegmentPosition=selfSegmentPosition-len(self.trackSession.getSegments())
self.setSpeed(selfSpeed)
self.setSegmentPosition(selfSegmentPosition)
def updatePlayerDistance(self):
playerCar=self.trackSession.getPlayerCar()
if not playerCar:
playerCar=self.trackSession.getCameraCar()
playerSegmentPosition=playerCar.getSegmentPosition()
selfSegmentPosition=self.getSegmentPosition()
if selfSegmentPosition<playerSegmentPosition and selfSegmentPosition>playerSegmentPosition-10.0:
distance=playerSegmentPosition-selfSegmentPosition
elif selfSegmentPosition>playerSegmentPosition and selfSegmentPosition<playerSegmentPosition+10.0:
distance=selfSegmentPosition-playerSegmentPosition
else:
distance=10.0
self.playerDistance=distance
def getPlayerDistance(self):
return self.playerDistance
def move(self):
moveCar=True
sessionType=self.trackSession.getSessionType()
if sessionType==SessionType.qualifying or sessionType==SessionType.race:
sessionStatus=self.trackSession.getSessionStatus()
if sessionStatus==QualifyingSessionStatus.qualifyingSuccessStatus or sessionStatus==RaceSessionStatus.raceSuccessStatus:
if self.segmentPosition>len(self.trackSession.getSegments())-10:
moveCar=False
if moveCar:
super(ComputerCarRepresentation, self).move()
self.avoidCollisions()
self.setCornerSpeed()
self.updateSpeed()
self.applyForwardMovement()
self.checkPlayerRelativeBoundary()
self.updatePlayerDistance()
self.approachDestinationLane()
self.incrementLapTimeTicks()
self.updateExplosion()
def onExplosionEnd(self):
super(ComputerCarRepresentation, self).onExplosionEnd()
segmentPosition=self.getSegmentPosition()
segmentPosition=segmentPosition+float(len(self.trackSegments)/2)
if segmentPosition>float(len(self.trackSegments)):
segmentPosition=segmentPosition-float(len(self.trackSegments))
self.setSegmentPosition(segmentPosition)