📄 platoonagent.java
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.// Edited By Omid Aghazadehpackage yab.agent;import java.net.*;import yab.agent.*;import yab.agent.object.*;import java.util.*;import MRL.Utilities.Partitioning.*;import MRL.Utilities.*;import MRL.Utilities.MessageManagement.AgentMessageManager;public abstract class PlatoonAgent extends HumanoidAgent implements MRL. Utilities.ConstantConditions { //Ashkan added public AgentMessageManager messageManager = null; RealObject lastPosition = null; RealObject currentPosition = null; //Ashkan added protected PlatoonAgent(int agentType, InetAddress address, int port) { super(agentType, address, port); } private Humanoid self() { return (Humanoid) world.self; } protected void rest() throws ActionCommandException { self().setAct(REST_ACT); super.rest(); //To change body of overridden methods use File | Settings | File Templates. } // ---------------------------------------------------------------- OMID Aghazadeh protected final Set visitedBuildingsIC = new HashSet(); protected final Collection shouldEmptyPoses = new HashSet(); // For Handling Police's MOVE Message. public static final int NEAR_FIRE_UB_SEARCH_RADIUS = 40000; public static final int NEAR_FIRE_LB_SEARCH_RADIUS = 10000; public static final int TIME_STARTING_TO_SEARCH_BY_AVAILABILITY = 100; public final static int MAX_HEARING_DISTANCE = 30000; public final static int MIN_FIRE_AVAILABLE_SEARCH = EXTINGUISHABLE_DISTANCE + 10000; public final int MAX_BLDGS_SEARCH_COUNT= 500 ; public final static int MAX_WAITING_TO_ROAM = 4; public static final int MIN_TIME_TO_CLEAR_TRAFFIC = 5; public static final int MAX_TIME_WAIT_TO_SET_TRAFFIC = 3; public static final int MAX_TIME_WAIT_TO_ROAM_FROM_TRAFFIC = 4; public static final int HUMANOID_SEARCH_RADIUS = 30000; public static final int MIN_BURNING_BLDGS_TO_START_SEARCH_AROUND_FIRE = 5; public static final int MAX_TIME_TO_SEARCH_FOR_HEARED_CIV = 10; public static final int MIN_TIME_AT_SEARCH_HEARED_CIVS = 30; public static final int MIN_TIME_AT_SEARCH_DEEP = 25 ; public static final int OBJS_TO_REQUEST_TO_CLEAR = 10; protected final int CIV_REPORT_PERIOD = 5; public static final int MAX_HP_TO_START_KANE = 5000; protected final Collection shouldUpdateCivBuildings = new HashSet(); protected Civilian prevCheckCivilian; protected final Condition SHOULD_UPDATE_CIV_C = new Condition(){ Civilian civ; Integer lc; int lastCheck; public boolean eval(Object obj){ if(obj instanceof Civilian){ civ = (Civilian) obj; if(! civ.needRescue()) return false; lc =(Integer) world.civCheckTime.get(civ); if(lc == null) return false; lastCheck =lc.intValue(); return (time() - lastCheck >= CIV_REPORT_PERIOD); } else return false; } }; protected void checkAndReportCivilians() throws ActionCommandException { checkAndStay(); updateShouldUpdateCivs();// if(onlyInPartition && searchingPartition() == null) return;// List toCheck = null;// int time = time();// if(onlyInPartition){// toCheck = searchingPartition().MO_OBJS_IN_PART_CND.extract(world.needRescueCivs);// }// else{// toCheck = new ArrayList(world.needRescueCivs);// }//// if(toCheck.isEmpty()) return;//// Building bldg;// if (prevCheckCivilian != null) {// if (prevCheckCivilian.needRescue() && ! prevCheckCivilian.isVisible() && self().motionlessPosition() != prevCheckCivilian.motionlessPosition() && NOT_IN_TRAFFIC_C.eval(prevCheckCivilian.motionlessPosition()))// if (prevCheckCivilian.position() instanceof Building && ! fullBuildings.contains(prevCheckCivilian.motionlessPosition())) {// if (SHOULD_UPDATE_CIV_C.eval(prevCheckCivilian)) {// bldg = (Building) prevCheckCivilian.motionlessPosition();// if (!bldg.isBurned() && !bldg.isBurning())// move(bldg);// }// }// }//// prevCheckCivilian = null;//// List needCheckCivs;// needCheckCivs = getNeedUpdateCivs(toCheck);//// if(needCheckCivs.isEmpty()) return;//// List needCheckPoses = POSITION_PRP.collect(needCheckCivs);//// int indx = random.nextInt(needCheckPoses.size());// bldg = (Building) needCheckPoses.get(indx);//// List civs = POSITION_PRP.eq(bldg).extract(needCheckCivs);// prevCheckCivilian = (Civilian) civs.iterator().next();// move(bldg); } public final Condition SHOULD_KANE_CIVS = new Condition (){ Humanoid hum; public boolean eval(Object obj){ if(obj instanceof Humanoid){ hum =(Humanoid)obj; return hum.hp()<MAX_HP_TO_START_KANE &&( hum.needRescue() || hum.needLoad()) ; }else{ return false; } } }; Building m_lastKANEBUILDING = null; protected void checkAndStay() throws ActionCommandException{ List civKanePoses = getNeedKaneCivPoses(); if(m_lastKANEBUILDING != null && ! (m_lastKANEBUILDING.isBurning() || m_lastKANEBUILDING.isBurned()) && REACHABLE_C.eval(m_lastKANEBUILDING)){ List visAgents = VISIBLE_C.and(POSITION_PRP.eq(m_lastKANEBUILDING)).extract(world.platoonAgents); world.sortByID(visAgents); if(!(Utils.getConditionCount(visAgents,AMBULANCE_TEAM_C) >0 || visAgents.indexOf(self()) > 0) && civKanePoses.contains(m_lastKANEBUILDING)){ if(self().motionlessPosition() == m_lastKANEBUILDING){ rest(); } else{// System.out.println(self() + " ----------- 7"); checkShouldContinueToBuildingAndMove(m_lastKANEBUILDING,false,false); } } } m_lastKANEBUILDING = null; int kanePosSize = civKanePoses.size(); List freeAgents = getFreeAgentsToUpdate(); int indx = freeAgents.indexOf(self()); if(indx == -1 ) return;// System.out.println(self() + " CivNeedKanePosesSize : " + kanePosSize + " FreeAgentsSize : " + freeAgents.size() + " In Time : " + time()); if(freeAgents.size() > kanePosSize){ if(indx >= kanePosSize){// System.out.println(self() + " ----------- 0"); return; } else{ m_lastKANEBUILDING =getKaneBuilding(civKanePoses,indx);// if(Utils.getConditionCount(world.ambulanceTeams,POSITION_PRP.equal(m_lastKANEBUILDING).and(VISIBLE_C))>0)// System.out.println(self() + " ----------- 1"); } } else if(freeAgents.size() == kanePosSize){ m_lastKANEBUILDING =getKaneBuilding(civKanePoses,indx);// System.out.println(self() + " ----------- 2"); } else{ // freeAgents < posSize if(kanePosSize > ((1f + MAX_KANE_PERCENT) * freeAgents.size()) ) {// System.out.println(self() + " ----------- 3"); return; } int maxKaneIndx = getMaxKaneIndex();// if(maxKaneIndx <= 0 )// System.err.println(self() + " <=0 MAX KANE INDEX."); if(indx > maxKaneIndx) {// System.out.println(self() + " ----------- 4"); return; } else{ m_lastKANEBUILDING =getKaneBuilding(civKanePoses,indx);// System.out.println(self() + " ----------- 5"); } } } protected Building getKaneBuilding(List civPoses , int selfIndx){ Building res; int cnt = selfIndx; do{ if(civPoses.size() <= cnt ) return null; res = (Building) civPoses.remove(cnt); cnt++; } while (Utils.getConditionCount(world.ambulanceTeams, VISIBLE_C.and(POSITION_PRP.equal(res)))>0); return res; } private Building m_lastBldgToUpdate = null; protected void updateShouldUpdateCivs() throws ActionCommandException{ correctShouldUpdateCivs(); if(m_lastBldgToUpdate != null){ if(shouldUpdateCivBuildings.contains(m_lastBldgToUpdate) && ! m_lastBldgToUpdate.isVisible()) checkShouldContinueToBuildingAndMove(m_lastBldgToUpdate,false,false); } m_lastBldgToUpdate = null; if(shouldUpdateCivBuildings.isEmpty()){ shouldUpdateCivBuildings.addAll(getNewShouldUpdateCivBuildings()); }// System.out.println(self() + " should Update Civ Buildings In Time : " + time() + " , Size : " + shouldUpdateCivBuildings.size()); Building bldg,res=null; int time , best = Integer.MAX_VALUE; for(Iterator it = shouldUpdateCivBuildings.iterator();it.hasNext();){ bldg = (Building) it.next(); time = bldg.time(); if(time < best && ! FULL_BUILDING_C.eval(bldg) && REACHABLE_C.eval(bldg)){ best = time; res = bldg; } } if(res != null){ m_lastBldgToUpdate = res; checkShouldContinueToBuildingAndMove(res,false,false); } } protected void correctShouldUpdateCivs(){ Building bldg; Collection civPoses = getCivPoses(); for(Iterator it = shouldUpdateCivBuildings.iterator();it.hasNext();){ bldg = (Building) it.next(); if(civPoses.contains(bldg)) continue; it.remove(); } if(m_lastBldgToUpdate != null && m_lastBldgToUpdate.isVisible()){ List visAgents = VISIBLE_C.and(POSITION_PRP.eq(m_lastBldgToUpdate)).extract(world.platoonAgents); world.sortByID(visAgents); if(visAgents.indexOf(self()) > 0 || Utils.getConditionCount(visAgents,AMBULANCE_TEAM_C)>0) shouldUpdateCivBuildings.remove(m_lastBldgToUpdate); } } protected Collection getNewShouldUpdateCivBuildings(){ Collection res = new HashSet(); List freeAgents = getFreeAgentsToUpdate(); if(! freeAgents.contains(self())){ return res; } List civPoses = getCivPoses(); int isKaneIngIndx = getMaxKaneIndex(); int sources = freeAgents.size(); int posSize = civPoses.size(); int posSources = posSize; if(isKaneIngIndx != -1 ){ sources -= (isKaneIngIndx + 1); posSources -= (isKaneIngIndx + 1); for(int i = 0 ; i <= isKaneIngIndx ; i ++ ) civPoses.remove(0); } if(sources <= 0) return res; if(searchingPartition()!= null){ res.addAll(searchingPartition().ML_OBJS_IN_PART_CND.extract(civPoses)); if(! res.isEmpty()) return res; } int count = (int) Math.floor(posSources / sources) + 1;// System.out.println(self() + " Free Agents : " + freeAgents.size() + " TotalKane : " + (isKaneIngIndx + 1) +" civPoses Size : " +civPoses.size() + " COUNT : " + count); Building bldg , tmp; int si = freeAgents.indexOf(self()); if(si == -1) System.err.println(self() + " EERRRRRRRRRRRRRRRRRRR Negative Index@!@###$"); int i = 0; do{ if(civPoses.isEmpty()) { return res; } bldg = (Building)civPoses.remove(0); if(i == si) res.add(bldg); int j = 1; while(j<count){ if(civPoses.isEmpty()) { return res; } tmp = (Building) bldg.distancePrp.min(civPoses); if(i == si) res.add(tmp); civPoses.remove(tmp); j++; } i++; } while (i <= si); return res; } protected int getMaxKaneIndex() { List freeAgents = getFreeAgentsToUpdate(); List civPoses = getNeedKaneCivPoses(); if(civPoses.size() > (1f+ MAX_KANE_PERCENT) * freeAgents.size()) return -1; else if (freeAgents.size() >= civPoses.size()) return Util.min(freeAgents.size() - 1 , civPoses.size() - 1); else return (int) (freeAgents.size() * MAX_KANE_PERCENT ); } protected List getFreeAgentsToUpdate(){ List freeAgents = new ArrayList(); freeAgents.addAll(world.policeForces); if(world.burningBuildings.isEmpty()){ freeAgents.addAll(world.fireBrigades); } world.sortByID(freeAgents); return freeAgents; } public static final float MAX_KANE_PERCENT = 0.5f; protected List getFreeNotKaneAgentsToUpdate(){ List freeAgents = getFreeAgentsToUpdate(); int maxKaneIndx = getMaxKaneIndex(); for(int i = 0 ; i < maxKaneIndx ; i++) freeAgents.remove(0); return freeAgents; } protected List getNeedKaneCivPoses(){ Civilian civ; Building bldg; List res = new ArrayList(); for (Iterator it = world.needRescueCivs.iterator(); it.hasNext(); ) { civ = (Civilian) it.next(); bldg = (Building) civ.motionlessPosition(); if(! SHOULD_KANE_CIVS.eval(civ)) continue; if(!res.contains(bldg)) res.add(bldg); } for (Iterator it = world.needLoadCivs.iterator(); it.hasNext(); ) { civ = (Civilian) it.next(); bldg = (Building) civ.motionlessPosition(); if(! SHOULD_KANE_CIVS.eval(civ)) continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -