def __carDecision(self): if self.__arriveDest==1 or self.__ifCrash==1 or self.__ifDestReachable==0: return carmovement(self.getDeviceID(),[]) self.__velocity = self.__maxVelocity myMovement = carmovement(self.getDeviceID(),[]) for unitv in range(self.__velocity): startCordinateStr = self.__carPath[self.__curPosInPath+unitv] startPosOfComma = int(startCordinateStr.index(',')) startPosX = (int)(startCordinateStr[0:startPosOfComma]) startPosY = (int)(startCordinateStr[startPosOfComma+1:len(startCordinateStr)]) endCordinateStr = self.__carPath[self.__curPosInPath+unitv+1] endPosOfComma = int(endCordinateStr.index(',')) endPosX = (int)(endCordinateStr[0:endPosOfComma]) endPosY = (int)(endCordinateStr[endPosOfComma+1:len(endCordinateStr)]) if startPosY==endPosY and endPosX==startPosX-1: myMovement.addStep(0) elif startPosX==endPosX and endPosY==startPosY+1: myMovement.addStep(1) elif startPosY==endPosY and endPosX==startPosX+1: myMovement.addStep(2) elif startPosX==endPosX and endPosY==startPosY-1: myMovement.addStep(3) else: print ("Error in costructing steps") return myMovement
def Update(self,simulatorIns): candp = simulatorIns.checkCycleAndPosition(self.getDeviceID()) #print (candp) if candp is None: return carmovement(self.getDeviceID(),[]) self.__curCycle = candp[0] self.setPosX(candp[1]) self.setPosY(candp[2]) if self.__arriveDest==1 or self.__ifCrash==1: return carmovement(self.getDeviceID(),[]) if candp[1]==self.__destPosX and candp[2]==self.__destPosY: self.__arriveDest = 1 print ("Car {0} has arrived.".format(self.getDeviceID())) return carmovement(self.getDeviceID(),[]) #GPSListener work #self.__carGPSListener.GPSMsgQueue.clear() #newMsg = simulatorIns.feedGPSMessage(self.getDeviceID()) #self.__carGPSListener.receiveGPSMessageTest(newMsg,self.__curCycle) if len(self.__gpsReceiver.GPSMsgQueue)>0: #print("Car {} has a GPS message!".format(self.getDeviceID())) self.dealGPSMessage() if len(self.__carPath)==0: return carmovement(self.getDeviceID(),[]) pathElement = str(self.getPosX())+','+str(self.getPosY()) for i in range(len(self.__carPath)): if pathElement==self.__carPath[i]: self.__curPosInPath = i break for s in self.__statusList: carx = s.getStatusCarPosX() cary = s.getStatusCarPosY() self.__myMap[carx][cary].clearCarIDs() self.__statusList.clear() sList = simulatorIns.lookAround(self.getDeviceID()) self.__statusList = deepcopy(sList) for s in self.__statusList: carx = s.getStatusCarPosX() cary = s.getStatusCarPosY() self.__myMap[carx][cary].addCarID(s.getStatusCarDevID()) #print ("I am Car {0}".format(self.getDeviceID())) #print ("My path is:") #print (self.__carPath) myMovement = self.__carDecision() return myMovement
def Update(self,simulatorIns): candp = simulatorIns.checkCycleAndPosition(self.getDeviceID()) #print (candp) if candp is None: return carmovement(self.getDeviceID(),[]) self.__curCycle = candp[0] self.setPosX(candp[1]) self.setPosY(candp[2]) if self.__arriveDest==1 or self.__ifCrash==1: return carmovement(self.getDeviceID(),[]) if candp[1]==self.__destPosX and candp[2]==self.__destPosY: self.__arriveDest = 1 print ("Car {0} has arrived.".format(self.getDeviceID())) return carmovement(self.getDeviceID(),[]) pathElement = str(self.getPosX())+','+str(self.getPosY()) for i in range(len(self.__carPath)): if pathElement==self.__carPath[i]: self.__curPosInPath = i break for s in self.__statusList: carx = s.getStatusCarPosX() cary = s.getStatusCarPosY() self.__myMap[carx][cary].clearCarIDs() self.__statusList.clear() sList = simulatorIns.lookAround(self.getDeviceID()) self.__statusList = deepcopy(sList) for s in self.__statusList: carx = s.getStatusCarPosX() cary = s.getStatusCarPosY() self.__myMap[carx][cary].addCarID(s.getStatusCarDevID()) #print ("I am Car {0}".format(self.getDeviceID())) #print ("My path is:") #print (self.__carPath) myMovement = self.__carDecision() return myMovement
def __carDecision(self): if self.__arriveDest==1 or self.__ifCrash==1 or self.__ifDestReachable==0: return carmovement(self.getDeviceID(),[]) for i in range(1,self.__maxVelocity+1): if self.__curPosInPath+i>=len(self.__carPath): break cordinateStr = self.__carPath[self.__curPosInPath+i] posOfComma = int(cordinateStr.index(',')) aheadPosX = (int)(cordinateStr[0:posOfComma]) aheadPosY = (int)(cordinateStr[posOfComma+1:len(cordinateStr)]) if len(self.__myMap[aheadPosX][aheadPosY].getRoadDirecs())==0: print ("Device {0} old path fail, recalculate path".format(self.getDeviceID())) self.__recalculatePath() if len(self.__carPath)==0: return carmovement(self.getDeviceID(),[]) else: break self.__velocity = 0 posOfLightAhead = [] idOfLightAhead = [] posOfCarAhead = [] for i in range(1,self.__maxVelocity+1): if self.__curPosInPath+i>=len(self.__carPath): break cordinateStr = self.__carPath[self.__curPosInPath+i] posOfComma = int(cordinateStr.index(',')) aheadPosX = (int)(cordinateStr[0:posOfComma]) aheadPosY = (int)(cordinateStr[posOfComma+1:len(cordinateStr)]) if self.__myMap[aheadPosX][aheadPosY].getLightID()!=-1: posOfLightAhead.append(i) idOfLightAhead.append(self.__myMap[aheadPosX][aheadPosY].getLightID()) if len(self.__myMap[aheadPosX][aheadPosY].getCarIDs())>0: posOfCarAhead.append(i) if len(posOfLightAhead)==0: #no light ahead if len(posOfCarAhead)==0: if self.__curPosInPath+self.__maxVelocity>=len(self.__carPath): self.__velocity = len(self.__carPath)-1-self.__curPosInPath #print ("no light no car dest ahead, my velocity is",self.__velocity) else: self.__velocity = self.__maxVelocity #print ("no light no car, my velocity is",self.__velocity) else: self.__velocity = posOfCarAhead[0]-1 #print ("no light exist car, my velocity is",self.__velocity) else: #light ahead for p in range(1,self.__maxVelocity+1): if self.__curPosInPath+p>=len(self.__carPath): break aheadCordinateStr = self.__carPath[self.__curPosInPath+p] aheadPosOfComma = int(aheadCordinateStr.index(',')) aheadPosX = (int)(aheadCordinateStr[0:aheadPosOfComma]) aheadPosY = (int)(aheadCordinateStr[aheadPosOfComma+1:len(aheadCordinateStr)]) if self.__myMap[aheadPosX][aheadPosY].getLightID()!=-1: aheadLightSignalRule = self.__myMap[aheadPosX][aheadPosY].getPointLightRule() aheadLightSignal = self.readLightSignal(aheadLightSignalRule) if self.getPosX()==aheadPosX and aheadLightSignal==0: break elif self.getPosY()==aheadPosY and aheadLightSignal==1: break elif self.getPosX()!=aheadPosX and self.getPosY()!=aheadPosY: formerCordinateStr = self.__carPath[self.__curPosInPath+p-1] formerPosOfComma = int(formerCordinateStr.index(',')) formerPosX = (int)(formerCordinateStr[0:formerPosOfComma]) formerPosY = (int)(formerCordinateStr[formerPosOfComma+1:len(formerCordinateStr)]) if formerPosX==aheadPosX and aheadLightSignal==0: break elif formerPosY==aheadPosY and aheadLightSignal==1: break else: continue else: continue if len(self.__myMap[aheadPosX][aheadPosY].getCarIDs())!=0: break self.__velocity = p myMovement = carmovement(self.getDeviceID(),[]) for unitv in range(self.__velocity): startCordinateStr = self.__carPath[self.__curPosInPath+unitv] startPosOfComma = int(startCordinateStr.index(',')) startPosX = (int)(startCordinateStr[0:startPosOfComma]) startPosY = (int)(startCordinateStr[startPosOfComma+1:len(startCordinateStr)]) endCordinateStr = self.__carPath[self.__curPosInPath+unitv+1] endPosOfComma = int(endCordinateStr.index(',')) endPosX = (int)(endCordinateStr[0:endPosOfComma]) endPosY = (int)(endCordinateStr[endPosOfComma+1:len(endCordinateStr)]) if startPosY==endPosY and endPosX==startPosX-1: myMovement.addStep(0) elif startPosX==endPosX and endPosY==startPosY+1: myMovement.addStep(1) elif startPosY==endPosY and endPosX==startPosX+1: myMovement.addStep(2) elif startPosX==endPosX and endPosY==startPosY-1: myMovement.addStep(3) else: print ("Error in costructing steps") return myMovement