def createCars(self): #def __init__(self,x,y,devid,maxv,destx,desty,trafficmap): ''' self.deviceList.append(Car(6,2,0,3,11,27,self.simulatorMap)) self.simulatorMap[6][2].addCarID(0) self.deviceStatusList.append(carstatus(0,6,2)) self.deviceList.append(Car(10,23,1,3,15,8,self.simulatorMap)) self.simulatorMap[10][23].addCarID(1) self.deviceStatusList.append(carstatus(1,10,23)) self.deviceList.append(Car(16,18,2,3,1,5,self.simulatorMap)) self.simulatorMap[16][18].addCarID(2) self.deviceStatusList.append(carstatus(2,16,18)) self.deviceList.append(Car(20,12,3,3,20,23,self.simulatorMap)) self.simulatorMap[20][12].addCarID(3) self.deviceStatusList.append(carstatus(3,20,12)) ''' self.deviceList.append(GPSCar(15, 2, 0, 3, 3, 8, self.simulatorMap, 0)) self.simulatorMap[15][2].addCarID(0) self.deviceStatusList.append(carstatus(0, 15, 2)) self.simuGPSServer.addCarToList(self.deviceList[-1].getDeviceID()) self.deviceList.append(Car(19, 5, 1, 3, 3, 8, self.simulatorMap)) self.simulatorMap[19][5].addCarID(1) self.deviceStatusList.append(carstatus(1, 19, 5)) self.deviceList.append(GPSCar(20, 5, 2, 3, 3, 8, self.simulatorMap, 1)) self.simulatorMap[20][5].addCarID(2) self.deviceStatusList.append(carstatus(2, 20, 5)) self.simuGPSServer.addCarToList(self.deviceList[-1].getDeviceID())
def createCarsTest(self): self.deviceList.append(Car(6, 2, 0, 3, 11, 27, self.simulatorMap)) self.simulatorMap[6][2].addCarID(0) self.deviceStatusList.append(carstatus(0, 6, 2)) self.deviceList.append(Badcar(4, 2, 1, 3, 11, 27, self.simulatorMap)) self.simulatorMap[4][2].addCarID(1) self.deviceStatusList.append(carstatus(1, 4, 2)) self.deviceList.append(Car(16, 18, 2, 3, 1, 5, self.simulatorMap)) self.simulatorMap[16][18].addCarID(2) self.deviceStatusList.append(carstatus(2, 16, 18))
def createCars(self): #def __init__(self,x,y,devid,maxv,destx,desty,trafficmap): self.carList.append(Car(6, 2, 0, 3, 11, 27, self.simulatorMap)) self.simulatorMap[6][2].addCarID(0) self.carStatusBuff.append(carstatus(0, 6, 2)) self.carList.append(Car(10, 23, 1, 3, 15, 8, self.simulatorMap)) self.simulatorMap[10][23].addCarID(1) self.carStatusBuff.append(carstatus(1, 10, 23)) self.carList.append(Car(16, 18, 2, 3, 1, 5, self.simulatorMap)) self.simulatorMap[16][18].addCarID(2) self.carStatusBuff.append(carstatus(2, 16, 18)) self.carList.append(Car(20, 12, 3, 3, 20, 23, self.simulatorMap)) self.simulatorMap[20][12].addCarID(3) self.carStatusBuff.append(carstatus(3, 20, 12))
def applyMovement(self, movement, device): if movement is None: return stepList = movement.getMovement() devID = movement.getMovementCarID() curX = -1 curY = -1 for s in self.deviceStatusList: if devID == s.getStatusCarDevID(): curX = s.getStatusCarPosX() curY = s.getStatusCarPosY() break if curX == -1 or curY == -1: #print ("Apply movement fail, no such device.") return moveX = curX moveY = curY for s in stepList: #print ("step is:",s) if s == 0: if len(self.simulatorMap[moveX - 1][moveY].getRoadDirecs()) == 0: #print ("Device {0} movement illegal.".format(devID)) break else: moveX = moveX - 1 if len(self.simulatorMap[moveX][moveY].getCarIDs()) > 0: self.simulatorMap[moveX][moveY].clearRoadDirecs() device.dealCrash() for ids in self.simulatorMap[moveX][moveY].getCarIDs(): print("in simulator, crash device ", ids) self.crashDevice(ids) break elif s == 1: if len(self.simulatorMap[moveX][moveY + 1].getRoadDirecs()) == 0: #print ("Device {0} movement illegal.".format(devID)) break else: moveY = moveY + 1 if len(self.simulatorMap[moveX][moveY].getCarIDs()) > 0: self.simulatorMap[moveX][moveY].clearRoadDirecs() device.dealCrash() for ids in self.simulatorMap[moveX][moveY].getCarIDs(): print("in simulator, crash device ", ids) self.crashDevice(ids) break elif s == 2: if len(self.simulatorMap[moveX + 1][moveY].getRoadDirecs()) == 0: #print ("Device {0} movement illegal.".format(devID)) break else: moveX = moveX + 1 if len(self.simulatorMap[moveX][moveY].getCarIDs()) > 0: self.simulatorMap[moveX][moveY].clearRoadDirecs() device.dealCrash() for ids in self.simulatorMap[moveX][moveY].getCarIDs(): print("in simulator, crash device ", ids) self.crashDevice(ids) break elif s == 3: if len(self.simulatorMap[moveX][moveY - 1].getRoadDirecs()) == 0: #print ("Device {0} movement illegal.".format(devID)) break else: moveY = moveY - 1 if len(self.simulatorMap[moveX][moveY].getCarIDs()) > 0: self.simulatorMap[moveX][moveY].clearRoadDirecs() device.dealCrash() for ids in self.simulatorMap[moveX][moveY].getCarIDs(): print("in simulator, crash device ", ids) self.crashDevice(ids) break else: #print ("Illegal movement") break if moveX == device.getDestPosX() and moveY == device.getDestPosY(): device.dealArrive() print("Device {0} has arrived.".format(devID)) print("") else: self.simulatorMap[moveX][moveY].addCarID(devID) self.tempStatusList.append(carstatus(devID, moveX, moveY))
def __carDecision(self): if self.__arriveDest==1 or self.__ifCrash==1 or self.__ifDestReachable==0: return carstatus(self.getDeviceID(),self.getPosX(),self.getPosY()) 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].getCarIDs())>0: self.__myMap[aheadPosX][aheadPosY].clearRoadDirecs() self.__recalculatePath() 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())==1: 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 self.__curPosInPath+=self.__velocity #print ("I am car {0}, I decide my velocity to be {1}, next PosInPath is {2}".format(self.getDeviceID(),self.__velocity,self.__curPosInPath)) newCordinateStr = self.__carPath[self.__curPosInPath] newPosOfComma = int(newCordinateStr.index(',')) newPosX = (int)(newCordinateStr[0:newPosOfComma]) newPosY = (int)(newCordinateStr[newPosOfComma+1:len(newCordinateStr)]) #print ("I am Car {0}, my velocity is {1}, I am goint to ({2},{3})".format(self.getDeviceID(),self.__velocity,newPosX,newPosY)) #print ("My path is:") #print (self.__carPath) myDecision = carstatus(self.getDeviceID(),newPosX,newPosY) if newPosX==self.__destPosX and newPosY==self.__destPosY: self.__arriveDest = 1 print ("Car {0} has arrived.".format(self.getDeviceID())) return myDecision