📄 policeforceagent.java
字号:
if(! ratherNear.isEmpty()) toCheckPaths = ratherNear; Path path; int indx = random.nextInt(toCheckPaths.size()); path = (Path) toCheckPaths.get(indx); m_lastTargetPath = path; res = getExploringRoute(path); res = makeRouteBigger(res); move(res); } protected void hearSay(RealObject sender, String msg) { super.hearSay(sender, msg); } protected void hearTellBin(RealObject sender, byte[] message) { if (sender.id != self().id){ if ((messageManager instanceof AgentMessageManager && sender instanceof PoliceOffice) || messageManager instanceof AgentNoStationMessageManager) messageManager.importMessage(message, time(), sender); } } private boolean wasBurried = false; protected void prepareForAct() { super.prepareForAct(); if(time() == 2 && world.policeOffices.size() == 0){ world.sortByID(world.policeForces); int indx = world.policeForces.indexOf(self()); if(indx < world.partitioner.getPartitions().size()){ Partition part = world.partitioner.getPartition(indx); if(part != null) world.setWorkingPartition(part); } } if(self().buriedness() > 0) wasBurried = true; else if (wasBurried){ self().freePolices.add(self()); world.sortByID(self().freePolices); wasBurried = false; } self().taskHandler.updateTasks(); if(! MRL.MRLConstants.IS_SIMPLE && workingPartition() != null) world.updatePathValues();// correctRoadsCleared(); reportClearedRoads(); if(self().motionlessPosition() instanceof Road){ Road rd = (Road) self().motionlessPosition(); if(NOT_PASSABLE_C.eval(rd)) messageManager.reportClear(rd); }// if (MRL.MRLConstants.POLICE_LEARN_MODE && time() == 300) { // || toReport// Utils.writeAlphasToFile(world.ai, world.policeForces.size());// toReport = false;// }// world.updateLockedBB(); self().blockData.updateLockedByBlockade();// prevPath = world.getPrevPath();// reportPrevCycleClearRoads();// checkAndReportPrevCycleClearedPaths();// if(workingPartition() != null){ //checkEntirePathClearedAndReport();// }// Path cPath = world.getCurrentPath(); //continuePrevRouteIfValid(); //updatePrevRouteShouldChecks();// checkStuckInPath(); if(time() == PARTITIONING_START_TIME + MESSAGE_DELAY ) { initFreePolices(); } //if(! world.lockedBBAgents.isEmpty()) System.out.println(world.lockedBBAgents);// checkMove(); /* world.buriedAgents.clear(); world.buriedAgents.addAll(MRL.Utilities.ConstantConditions.BURIED_C.extract( world.agents)); */ correctReportedBlockade(); chooseNextTask(); messageManager.sendMessages(); } private void initFreePolices(){ self().freePolices.addAll(world.policeForces); self().freePolices.removeAll(self().polPartMap.keySet()); Collection burried =BURIED_C.extract(self().freePolices); self().freePolices.removeAll(burried);// PoliceForce pf;// for(Iterator it = self().freePolices.iterator();it.hasNext();){// pf = (PoliceForce) it.next();// self().freePolTaskMap.put(pf,new Integer(0));// } if(MRL.MRLConstants.PF_DEBUG_MODE) System.out.println(self() + (self().freePolices.contains(self())? " IS " : " IS NOT " ) + " Free Police , Size Of Free Polices : " + self().freePolices.size() + " , Burried : " + burried.size()); world.sortByID(self().freePolices);// self().taskHandler = new TaskHandler(world); } protected void correctRoadsCleared() { if(roadsCleared.isEmpty()) return; Road road = (Road) roadsCleared.get(roadsCleared.size()-1); if (NOT_PASSABLE_C.eval(road)) roadsCleared.remove(road); } private int m_lastTimeTrafficInPath = -1; private Path m_lastTrafficPath; protected void checkMorePolices() throws ActionCommandException { MotionlessObject pos = self().motionlessPosition(); if (pos instanceof Refuge)return; Path path = world.getCurrentPath(); boolean isInPath = true; if (! submittedMoveAct || prevRoute == null|| prevRoute.end == pos || prevRoute.end instanceof Building)return; if(time() <= TIME_STARTING_ACTION + 1) return; if(path == null){ isInPath = false; Node node = (Node) pos; Road road; Path tpath; for(Iterator it = node.getConnectedRoads().iterator();it.hasNext();){ road = (Road) it.next(); tpath = world.getContainingPath(road); if(prevRoute.isContaining(road)){ path = tpath; break; } } } if(path == null){ System.err.println(self() + " , Invalid prevRoute In checkMorePolices()"); return; }// MotionlessObject mo; List pols = POLICE_FORCE_C.extract(path.getVisibleHumanoids()); PoliceForce pf; for(Iterator it = pols.iterator();it.hasNext();){ pf = (PoliceForce) it.next(); if(pf.position() instanceof AmbulanceTeam) it.remove(); } int lines = path.minRoadLines(); if (pols.size() < lines){ m_lastTrafficPath = path; m_lastTimeTrafficInPath = time(); return; }// if(acted ||(prevRoute != null && self().motionlessPosition()== prevRoute.end ))return;// world.sortByID(pols);// if (pols.indexOf(self())==0)return; // There's At Least One Police Except This One In The Path , Should Check If There Isn't A Road Blocked/Unseen Between Them , The One Who's Road Bao .... pols.remove(self()); if (pols.isEmpty()){ m_lastTrafficPath = path; m_lastTimeTrafficInPath = time(); return; } int lastTimeCleared = -1; int time= time(); for(int i = time -1 ; i > TIME_STARTING_ACTION ; i--){ if(self().getSubmittedAction(i)){ lastTimeCleared = i; break; } }// if(m_lastTrafficPath == path){ // Be Har Dalil Agar Tooye Path Moonde Bood Bishaz Yek Te'dade Cycle Va Kar Nakarde Traffice .// int timesWereInPath = self().getCyclesInPath(path) ;// if(timesWereInPath >= MAX_TIME_WAIT_TO_SET_TRAFFIC && time - MAX_TIME_WAIT_TO_SET_TRAFFIC >= lastTimeCleared){// setTraffic(path,time,isInPath);// return;// }// } Collection toCleanRoads = PASSABLE_C.not().extract(path); if (toCleanRoads.isEmpty()) { m_lastTrafficPath = path; m_lastTimeTrafficInPath = time();// setTraffic(path,time(),isInPath); //It's Handled In Platoon Agent .... return; } Road nearestRoad = (Road) distancePrp.min(toCleanRoads); int selfRoadDist = self().distance(nearestRoad), polDist = -1, polRoadDist; boolean traffic = false; for (Iterator it = pols.iterator(); it.hasNext(); ) { pf = (PoliceForce) it.next(); polRoadDist = pf.distance(nearestRoad);// if (pf.position() instanceof AmbulanceTeam)continue; polDist = self().distance(pf.position()); if (polDist > polRoadDist && polDist > selfRoadDist) { // From Two Sides continue; } else if (selfRoadDist <= polRoadDist)continue; traffic = true; } if (!traffic){ return; }// if(m_lastTrafficPath == path){// if (time() - m_lastTimeTrafficInPath > 0) {// Collection bldgs = new HashSet();// if (pos instanceof Node) {// bldgs = ((Node) pos).getConnectedBuildings();// } else if (pos instanceof Road) {// bldgs = ((Road) pos).getConnectedBuildings();// }// if (!bldgs.isEmpty())// move(bldgs);// else {// roam();// chooseNextTask();// m_reportedBlockades.removeAll(path);// reportedBlockades.remove(path);// reportedBlockadesIP.remove(path);// }// } // End Of Time >=2// } // End Of LastTrafficPath = Path// else { m_lastTrafficPath = path; m_lastTimeTrafficInPath = time(); setTraffic(path,time(),isInPath);// List AvailableRoads = getRoadsNotInTrafficFromPrevRoute();//// List availableBuildings = world.getConnectedBuildings(AvailableRoads);//// availableBuildings.retainAll(world.unvisitedBuildings);//// if(MRL.MRLConstants.PF_DEBUG_MODE)// System.out.println(self() +// " , AvRoads : " +// AvailableRoads + " , AvBldgs : " +// availableBuildings);// moveRandomBuilding(availableBuildings);// if(! availableBuildings.isEmpty())// move(availableBuildings);// roam();// } } protected TaskHandler taskHandler() { return self().taskHandler; } private void chooseNextTask() {// if(self().task() < TASK_SEARCH_CIVILIANS){// taskHandler().setNewDecidedTaskToMe(TASK_SEARCH_CIVILIANS);// if(true)return;// }// else return; int time = time(); if(time < TASK_ASSIGN_START_TIME + MESSAGE_DELAY) return; int task = self().task(); if(task == -1){ taskHandler().setNewDecidedTaskToMe(TASK_MOVE_REPORTED_BLOCKADES); System.out.println(self() + " Invalid Task , Correcting To 0 In Time : " + time()); return; } if(workingPartition() == null && (!taskHandler().isMyLastCenterAssignedTaskDone())){ if(isCurrentTaskDone()){// if(MRL.MRLConstants.POLICE_TASK_DEBUG_MODE) // System.out.println(self() + " , Task : " + self().task() + " Is Finished In : "+ time); taskHandler().setTaskAssignedByCenterFinishedForMe(); messageManager.reportTaskFinished(); } else return ; } int nTask = decideNewTask(); if(nTask != self().task()){ taskHandler().setNewDecidedTaskToMe(nTask); } else return; m_taskDone = -1; } protected int decideNewTask(){ int res = 0 ;// int time = time();// int remTasks = TASKS_COUNT - TASK_MOVE_NEAR_FIRE_BUILDINGS;// if(workingPartition() != null){// if(shouldSearchBlocks())// res =TASK_MOVE_REPORTED_BLOCKADES;// else// res = random.nextInt(remTasks) + TASK_MOVE_NEAR_FIRE_BUILDINGS;// }// else{// List withoutTask = taskHandler().getWithoutTaskFreePolices();// int withoutTaskIndx = withoutTask.indexOf(self());// if(withoutTaskIndx >= remTasks) withoutTaskIndx = random.nextInt(remTasks);// if(withoutTaskIndx == -1)// throw new Error("AHA");// if (time < TASK_ASSIGN_START_TIME + TASK_ASSIGNING_TIME) {// res = TASK_MOVE_REPORTED_BLOCKADES;// } else if (time < TASK_ASSIGN_START_TIME + 2 * TASK_ASSIGNING_TIME) {// if(shouldSearchBlocks())// res = TASK_MOVE_REPORTED_BLOCKADES;// else// res = TASK_MOVE_NEAR_FIRE_BUILDINGS + withoutTaskIndx;// } else { // To The End Of Simulation// if(shouldSearchBlocks())// res = TASK_MOVE_REPORTED_BLOCKADES;// else // random.nextInt(TASKS_COUNT - TASK_MOVE_NEAR_FIRE_BUILDINGS) +// res = TASK_MOVE_NEAR_FIRE_BUILDINGS + withoutTaskIndx ;// } for(int i = 0 ; i < TASKS_COUNT ; i++){ if(!isTaskDone(i)){ // || TIMEOUT res = i; break; } }// } return res; } protected void act() throws ActionCommandException { super.act(); if (self().damage()>4 ) {// System.out.println(" My ID " + self().id + " Has Damage " + self().damage()); restAtRefuge(); } if (time() < TIME_STARTING_ACTION)return; /* System.out.println("POLICE FORCE TIME OF AGENT : " + self().id + ", " + world.time() + (workingPartition() == null ? "" : " , Partition : " + workingPartition().getID())); */ clearHere();// if(time() > 20) gotoPossibleCivBuildings();// testRoute();//// acted = false;// doSomethingIfStuck();// continuePrevRouteIfValid();// rest(); checkMorePolices(); world.computeTraffic(); if (MRL.MRLConstants.POLICE_TASK_DEBUG_MODE) System.out.println(self() + " , Time : " + time() + " , Task : " + self().task()); if (!MRL.MRLConstants.IS_SIMPLE && workingPartition() != null ) { if(self().task() <= TASK_SEARCH_BLOCKADES) choosePath(); }// if(time() <= PARTITIONING_START_TIME) rest(); switch (self().task() ) { case TASK_MOVE_REPORTED_BLOCKADES: if (MRL.MRLConstants.POLICE_TASK_DEBUG_DEEP_MODE) System.out.println(self() + " , Starting Move Reported Blockades : " + " In Time : " + time());// clearBlockadeReportedInPath(); moveReportedBlockades(); moveBuriedAgentPath();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -