📄 platoonagent.java
字号:
if(!res.contains(bldg)) res.add(bldg); } world.sortByID(res); return res; } protected List getCivPoses(){ Civilian civ; Building bldg; List civPoses = new ArrayList(); for(Iterator it = world.needRescueCivs.iterator();it.hasNext();){ civ = (Civilian) it.next(); if(civ.position() instanceof Building){ bldg = (Building) civ.motionlessPosition(); if(bldg.isBurned() || bldg.isBurning()) continue; if(!civPoses.contains(bldg)) civPoses.add(bldg); } } for(Iterator it = world.needLoadCivs.iterator();it.hasNext();){ civ = (Civilian) it.next(); if(civ.position() instanceof Building){ bldg = (Building) civ.motionlessPosition(); if(bldg.isBurned() || bldg.isBurning()) continue; if(!civPoses.contains(bldg)) civPoses.add(bldg); } } Collections.sort(civPoses,Util.idComparator); return civPoses; } private final HearData hearData = world.hearData; protected void hearSay(RealObject sender, String msg) { super.hearSay(sender, msg); if (sender instanceof Civilian) { MotionlessObject mo = self().motionlessPosition(); Civilian civ = (Civilian) sender; if(civ.position() == null){// System.out.println(self() + " Heared : " + civ + " In : " + self().motionlessPosition() + " , X= " +self().x()// + " , Y=" + self().y() + " In Time : " + time()); if(!civ.selfHearedPositions.contains(mo) && ! civ.messageHearedPositions.contains(mo)) { //world.messagepool.ReportHearedCivilian(civ,mo); messageManager.reportHeardCivilian(civ,mo,self().positionExtra()); } hearData.addHearedCivilian(civ,mo,self().positionExtra(),time(),true); civ.updatePossiblePoses(); } } else { tryMove(sender, msg); hasOrder = true; } } protected void hear(RealObject sender, String msg) { //hear(msg); } protected int hearingLimit() { return HEARING_LIMIT_OF_HUMANOID; } /** * todo Aghazadeh : Complete This . MayB It's Better To Move To Farer Positions ? * Or MayBe It Should Be Handled In Each Agent !? */ protected Route orderedMove; protected void tryMove(RealObject sender, String msg) { if (self() instanceof PoliceForce)return; if(MRL.MRLConstants.ROUTING_DEBUG_MODE) System.out.println(self() + " , Heared Clearing Request : " + msg);// if(self().motionlessPosition().distance(sender.motionlessPosition()) > 100000) return; String[] strPosesIndxs = msg.split(","); for (int i = 0; i < strPosesIndxs.length; i++) {// shouldEmptyPoses.add(world.motionlessObjects.get(Integer.parseInt(// strPosesIndxs[i]))); shouldEmptyPoses.add(world.get(Integer.parseInt( strPosesIndxs[i]))); } MotionlessObject pos = self().motionlessPosition(); if (!shouldEmptyPoses.contains(pos)) { if (MRL.MRLConstants.ROUTING_DEBUG_MODE) System.out.println(self() + " Doesnt Need To Clear Position "); return; } if (pos instanceof Node) { shouldEmptyPoses.addAll(ROAD_C.extract(pos.neighborhood())); } else if (pos instanceof Road) { shouldEmptyPoses.addAll(NODE_C.extract(pos.neighborhood())); shouldEmptyPoses.addAll(((Road)pos).getConnectedBuildings()); } Collection possibleNearPoses = new HashSet(); Collection visited = new HashSet(); List tempPoses = new ArrayList(); tempPoses.add(pos); MotionlessObject tPos, ttPos; boolean hasBuilding = false; do { tPos = (MotionlessObject) tempPoses.remove(0); visited.add(tPos); for (Iterator it = tPos.neighborhood().iterator(); it.hasNext(); ) { ttPos = (MotionlessObject) it.next(); if (!shouldEmptyPoses.contains(ttPos)) { if (ttPos instanceof Building) { Building bldg = (Building) ttPos; if (!bldg.isBurning() && !fullBuildings.contains(bldg)){ possibleNearPoses.add(ttPos); hasBuilding = true; } } else possibleNearPoses.add(ttPos); } if (!tempPoses.contains(ttPos) && !visited.contains(ttPos)) tempPoses. add(ttPos); } } while (! hasBuilding && ! tempPoses.isEmpty()); if (possibleNearPoses.isEmpty()) { if (MRL.MRLConstants.ROUTING_DEBUG_MODE) System.out.println(self() + " Could'nt Find Any Possible Positions To Go"); return; }// orderedMove = getRouteFromCurrentPos(possibleNearPoses,CLEARING_COST_FUNCTION); Collection possibleBldgs = BUILDING_C.extract(possibleNearPoses); if(! possibleBldgs.isEmpty()){ orderedMove = getRouteFromCurrentPos(possibleBldgs,MOVING_COST_FUNCTION); } else orderedMove = getRouteFromCurrentPos(possibleNearPoses,MOVING_COST_FUNCTION); orderedMove.validateAndSubmit();// if (MRL.MRLConstants.ROUTING_DEBUG_MODE) // System.out.println(self() + // "Ordered Move : " + orderedMove); } protected boolean shouldSearch = false; protected boolean foundCiv = false; protected void act() throws ActionCommandException { shouldEmptyPoses.clear();// prevRoute = null; restIfBurried(); if(time() <= TIME_STARTING_ACTION) return; if(self().getWereLockedByBlockade(time()) ){ moveToAvailableBldgsIfStuck();// System.out.println(self() + " -------------- TRYN "); if((self() instanceof FireBrigade)) { List nearFires = distancePrp.lte(EXTINGUISHABLE_DISTANCE).extract(world.burningBuildings); if(nearFires.isEmpty()) tryToGetOutOfBlockades(); } else if ( (self() instanceof AmbulanceTeam)&& ((time() % 8)<2 ) ) { tryToGetOutOfBlockades(); } } if (!(self() instanceof PoliceForce)) { if (orderedMove != null) { if (MRL.MRLConstants.ROUTING_DEBUG_MODE) System.out.println(self() + " Try'n To Move OrderedMove : " + orderedMove); move(orderedMove); } } if (decidedRoute != null) { if (MRL.MRLConstants.ROUTING_DEBUG_DEEP_MODE) System.out.println(self() + " Try'n To Move deciDedRoute : " + decidedRoute);// if (self() instanceof FireBrigade )// System.out.println("decide route null nist " + self().id ); move(decidedRoute); } exitIfIsBurning();// checkCurPostHearedCivs(); if(shouldRoam) {// System.out.println(self() + " Roaming In Platoon.Act In : " + time());// roam(); }// checkStuck(); restIfIsDamaging(); /** * EVERYONE : shouldEmptyPoses Is A Collection Which We Should Clear Every Position In It And It'll Be Cleared In PlatoonAgent.act() So Use It Before Calling Super.Act() In Your Agent's ACT. */ } protected void moveToAvailableBldgsIfStuck() throws ActionCommandException{ Path cpath = world.getCurrentPath(); if(cpath == null) return; List bldgs = cpath.getConnectedBuildings(); bldgs.retainAll(world.unvisitedBuildings); if(bldgs.isEmpty()) return; Route rt = getRouteFromCurrentPos(bldgs,RELIABILITY_COST_FUNCTION); if(rt.cost< IN_BLOCKS) move(rt); } protected void ATCivSearch() throws ActionCommandException{ if(!world.unvisitedBuildings.isEmpty()){ List toCheck = correctListToCheck(world.unvisitedBuildings); if(!toCheck.isEmpty()) move(toCheck); } } protected void checkCurPostHearedCivs() throws ActionCommandException { if(time() < 100 || !hearedCivilianICC ) return; if (!(self() instanceof PoliceForce) || (self() instanceof FireBrigade && world.burningBuildings.size() > 2))return; if(self() instanceof PoliceForce ){ MotionlessObject mo = self().motionlessPosition(); if(mo instanceof Road && NOT_PASSABLE_C.eval((Road) mo)) // return; } if (!world.m_HearedCivPossPosMap.isEmpty()) { Collection res = world.m_HearedCivPossPosMap.values(); gotoPossibleCivBuildings(res); } } protected void restIfBurried() throws ActionCommandException { if (self().buriedness() > 0) rest(); } protected boolean hasOrder = false; protected Route decidedRoute; protected void prepareForAct() { super.prepareForAct(); if (currentPosition != null) lastPosition = currentPosition; else lastPosition = self().position(); currentPosition = self().position(); if (time() < TIME_STARTING_ACTION || time() > SIMULATING_TIME)return; submittedMoveAct = self().submittedMoveAct(); submittedAction = self().submittedAction(); decidedRoute = null; shouldRoam = false; shouldSearch = false; if (!hasOrder) orderedMove = null; hasOrder = false; checkFullBuildings(); hearData.updateHumStats(); Humanoid self = self(); updateNeedCheckBuildings(); updatePrevRoads(); world.accessibleData.updateAccessiblity(); updateHums(); report(); if (!(self instanceof PoliceForce)) { reportLockedByBlockade(); if (isLockedInBlockades(self)) { shouldSearch = true;// tryToGetOutOfBlockades(); self.setWereLockedByBlockade(); } } /** * todo Aghazadeh : Can Implement A Method Here Which Checks If Agent's Locked By Any Means For More Than 3 Cycles And Thus He Should Roam ? */ correctTMPs(); doSomethingIfWereStuckByHumanoids(); if(! (self instanceof PoliceForce)) world.computeTraffic();// updateLockedCivInfo();// reportLockedCivs();// reportHearedHumansInRefuge();// if(isLockedBySomeone()) Can Be Implemented Here// TalkBufferedMessages(utteranceLimit());// if(! (self instanceof AmbulanceTeam)) TalkBufferedMessages(utteranceLimit());// else TalkBufferedMessages(utteranceLimit() - 1); } protected void updateNeedCheckBuildings(){// MotionlessObject pos = self().motionlessPosition(); if(time() <= TIME_STARTING_ACTION +1) return; visitedBuildingsIC.clear(); visitedBuildingsIC.addAll(IN_VISIBLE_DIST.extract(world.updatedBuildings));// Set civs = posHearedCivilianMap.get(pos);// if(civs.isEmpty()){// Collection noCiv = world.getPossibleHearedBuildings(pos,true);// visitedBuildingsIC.addAll(noCiv);// }// if(time() > TIME_STARTING_ACTION + 3) { // MessageDelay + HearingDelay// Collection prev = hearData.updateNeedChecks();// if(!prev.isEmpty()){// System.out.println(self() + " , Deleting : PREV : " + prev);// visitedBuildingsIC.addAll(prev);// }// }// Building test = (Building) world.get(1970);// if(visitedBuildingsIC.contains(test)){// System.err.println(self() + " , Tries To Delete : 1970 , Dist : " + self().distance(test) + " Time : " + time());// if(world.get(2473) == null)// throw new Error("RIDI");// } world.unvisitedBuildings.removeAll(visitedBuildingsIC); } protected final Map <Building , Integer> fullBuildingsTimeMap = new HashMap(); protected final Collection fullBuildings = fullBuildingsTimeMap.keySet(); public static final int MIN_TIME_TO_CLEAR_FULL_BUILDINGS = 3; protected void checkFullBuildings() { Building bldg; int fullTime, time = time(); for(Iterator it = fullBuildings.iterator();it.hasNext();){ bldg = (Building) it.next(); fullTime = fullBuildingsTimeMap.get(bldg); if(time - fullTime > MIN_TIME_TO_CLEAR_FULL_BUILDINGS) it.remove();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -