diff --git a/Car.py b/Car.py index f72689e..c111b04 100644 --- a/Car.py +++ b/Car.py @@ -11,16 +11,11 @@ class updateSignals(QObject): class Car(): def getShape(self, edgeInd): startEdge = self.route[edgeInd] - nextEdge = self.route[edgeInd+1] - inverted = not (startEdge.getToNode()==nextEdge.getFromNode() or startEdge.getToNode()==nextEdge.getToNode()) - laneId = 0 if not inverted else 1 + laneId = 0 lane = startEdge.getLane(laneId) vmax = lane.getSpeed() laneShape = lane.getShape() - if(inverted): - laneShape = laneShape.reverse() - return (laneShape, vmax, laneId) def initPath(self): @@ -129,8 +124,8 @@ class Car(): edgeInd+=1 carComing = self.getLeaderAtIntersection(prevInd,edgeInd) if(carComing is not None): - self.leaderDist = l - return carComing + self.leaderDist = l+carComing[0] + return carComing[1] if(not self.route[edgeInd].isSpecial()): prevInd = edgeInd carsHere = self.controller.getCarsOnLane(self.route[edgeInd].getID(), laneId) @@ -151,6 +146,22 @@ class Car(): else: return return + + def getCurrentEdge(self): + return self.route[self.index] + + def getCarDist(self, car, edge, laneInd, startFromEnd = True): + lanes = edge.getLane(laneInd).getShape().copy() + if startFromEnd: + lanes.reverse() + cDist = 0 + for i,l in enumerate(lanes[:-1]): + if car.laneInd != (i if not startFromEnd else len(lanes)-i-2): + cDist += dist(l, lanes[i+1]) + else: + cDist += dist(l, car.pos) + return cDist + return cDist def getLeaderAtIntersection(self, prevInd, edgeInd): if(self.route[edgeInd].isSpecial()): @@ -173,13 +184,25 @@ class Car(): if(f == '0'): continue edge = conn[i].getFrom() - cars += list(self.controller.getCarsOnLane(edge.getID(), conn[i].getFromLane().getIndex())) # doit y avoir moyen de le faire en gardant les filters, flemme + laneInd = conn[i].getFromLane().getIndex() intLane = self.map.getLane(conn[i].getViaLaneID()) + intLaneLgt = intLane.getLength() + carsInEdge = list(self.controller.getCarsOnLane(edge.getID(), laneInd)) # doit y avoir moyen de le faire en gardant les filters, flemme + if len(carsInEdge) != 0: + carsInEdge = zip([self.getCarDist(c, edge, laneInd)+intLaneLgt for c in carsInEdge], carsInEdge) + closest = min(carsInEdge, key=lambda c: c[0]) + cars.append(closest) intEdge = intLane.getEdge() - cars += list(self.controller.getCarsOnLane(intEdge.getID(), intLane.getIndex())) + carsInEdge = list(self.controller.getCarsOnLane(intEdge.getID(), intLane.getIndex())) + if len(carsInEdge) != 0: + carsInEdge = zip([self.getCarDist(c, intEdge, intLane.getIndex()) for c in carsInEdge], carsInEdge) + closest = min(carsInEdge, key=lambda c: c[0]) + cars.append(closest) + if(len(cars) == 0): return None - return cars[0] # TODO: récuperer la voiture la plus proche au lieu d'un au hasard + cDist,closest = min(cars, key=lambda c: c[0]) + return (cDist,closest) def draw(self,painter): pt = QPointF(*self.pos)