diff --git a/Car.py b/Car.py index e9091d9..9f05feb 100644 --- a/Car.py +++ b/Car.py @@ -1,5 +1,5 @@ import sumolib -from math import dist, ceil, sqrt +from math import dist, ceil, sqrt, log from random import randint from PySide6.QtGui import QPainter from PySide6.QtCore import QPointF, Signal, QObject @@ -99,6 +99,7 @@ class Car(): def getLeader(self, maxDist): shapeInd = self.laneInd edgeInd = self.index + prevInd = edgeInd laneShape = self.laneShape laneId = self.laneId l = 0 @@ -126,6 +127,12 @@ class Car(): if(shapeInd>=len(laneShape)-1): shapeInd = 0 edgeInd+=1 + carComing = self.getLeaderAtIntersection(prevInd,edgeInd) + if(carComing is not None): + self.leaderDist = l + return carComing + if(not self.route[edgeInd].isSpecial()): + prevInd = edgeInd carsHere = self.controller.getCarsOnLane(self.route[edgeInd].getID(), laneId) carsHere = list(filter(lambda c: c.id != self.id, carsHere)) # me demande pas pourquoi mais si on le convertit pas en liste ici le filter original est modifié if(edgeInd>=len(self.route)-1): @@ -145,6 +152,35 @@ class Car(): return return + def getLeaderAtIntersection(self, prevInd, edgeInd): + if(self.route[edgeInd].isSpecial()): + return None + inter = self.route[edgeInd-1].getFromNode() + connection = self.route[prevInd].getConnections(self.route[edgeInd])[0] + linkInd = inter.getLinkIndex(connection) + if(linkInd == -1): # Ca devrait pas arriver, mais de toute évidence ça arrive + return; + resp = inter._prohibits[linkInd] # Si je me souvient bien les variables précédées d'un _ doivent pas être touchées? + connRaw = inter.getConnections() + conn = [0] * len(resp) + for c in connRaw: + ind = inter.getLinkIndex(c) + if(ind == -1): + continue + conn[ind] = c + cars = None + for i,f in enumerate(reversed(resp)): + if(f == '0'): + continue + edge = conn[i].getFrom() + cars = self.controller.getCarsOnLane(edge.getID(), conn[i].getFromLane().getIndex()) + if(cars is None): + return + cars = list(cars) + if(len(cars) == 0): + return None + return cars[0] # TODO: récuperer la voiture la plus proche au lieu d'un au hasard + def draw(self,painter): pt = QPointF(*self.pos) painter.drawEllipse(pt,self.size,self.size) diff --git a/Map.py b/Map.py index 60f5ab7..f793961 100644 --- a/Map.py +++ b/Map.py @@ -50,7 +50,7 @@ class Map: @safeCall def getLane(self,laneID): return self.net.getLane(laneID) - + @safeCall def getBounds(self): return self.net.getBBoxXY() diff --git a/mainLoop.py b/mainLoop.py index 17e49d7..6afe3a5 100644 --- a/mainLoop.py +++ b/mainLoop.py @@ -64,7 +64,7 @@ class mainLoop(QObject): self.controller.update() except: (type, value, traceback) = sys.exc_info() - print(type, value, traceback) + print(type, value, traceback.print_tb()) @threadSafe def openNetwork(self, filename): diff --git a/test12.rou.xml b/test12.rou.xml index 1a84b6f..b17576c 100644 --- a/test12.rou.xml +++ b/test12.rou.xml @@ -1,13 +1,13 @@ - + + + - - -