📄 civilian.java
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.package yab.agent.object;import yab.agent.*;import yab.io.object.*;import java.util.*;import MRL.Utilities.Utils;public class Civilian extends Humanoid { public Civilian(int id, DisasterSpace world) { super(id, new BaseCivilian(id), world); } private BaseCivilian obj() {return (BaseCivilian) object; } // Omid Aghazadeh private final Random rnd = new Random(world.self.id); public static final String CIV_HEAR_OUCH = "Ouch!"; public static final String CIV_HEAR_HELP = "Help!"; public final List <MotionlessObject > messageHearedPositions = new ArrayList<MotionlessObject>(); public final List <MotionlessObject> selfHearedPositions = new ArrayList<MotionlessObject>(); private final List <int []> selfHearedXY = new ArrayList<int[]>() ; private final List <int []> messageHearedXY = new ArrayList<int[]>() ; private final List <int[]> messageComputedXY = new ArrayList<int[]>(); private final List<int []> selfComputedXY = new ArrayList<int[]>(); public final List <Integer> searchedTime = new ArrayList<Integer>(); public final List <Building> possibleBuildings = new ArrayList<Building>(); private final List <Building> guessedPoses = new ArrayList<Building>(); private final List <Integer> guessedPosesTime = new ArrayList<Integer>(); private Building guessedBldg = null; private Building mostProbableBldg = null; private static final int XYSEARCH_TOLERANCE = 1000; private final static int SELF_SEARCH_DIST = PlatoonAgent.MAX_HEARING_DISTANCE ; private final static int MESSAGE_SEARCH_DIST = PlatoonAgent.MAX_HEARING_DISTANCE + 2*XYSEARCH_TOLERANCE ; public void updatePossiblePoses(){ RealObject self = world.self; int time = world.time(); if(MRL.MRLConstants.CIV_DEBUG_MODE) System.out.println(self + " UpdatingPossiblePoses : " + this + " , GuessedPos : " + guessedPosition() + " , possBuilding Before : " + possibleBuildings ); if(possibleBuildings.isEmpty()){ if(selfComputedXY.size() != 0 || messageComputedXY.size() != 0){ // System.err.println(self + " , Exiting updatePossiblePoses() OF : " +this + " Because Of No Possible Poses , Time : " + time); guessedBldg = null; return; } } while(hasNewPosToCompute()){ ComputeNext(); updateGuessedPos(); } if(MRL.MRLConstants.CIV_DEBUG_MODE) System.out.println(self + " UpdatingPossiblePoses : " + this + " , GuessedPos : " + guessedPosition() + " , possBuilding After : " + possibleBuildings + " , Time : " + time); } public boolean hasNewPosToCompute(){ return hasNewSelfPosToCompute() || hasNewMessageHearedToCompute(); } public boolean hasNewSelfPosToCompute(){ return selfHearedXY.size() > selfComputedXY.size(); } public boolean hasNewMessageHearedToCompute(){ return messageHearedXY.size() > messageComputedXY.size(); } public Building guessedPosition() {// if(guessedBldg == null) System.err.println(world.self + " Error : guessedBldg Is Null , MayB Civ's Dead !?"); return guessedBldg; } private void ComputeNext(){ Collection tmp= null; boolean isFirst = (possibleBuildings.isEmpty() && selfComputedXY.isEmpty() && messageComputedXY.isEmpty()); List toCompute; if(hasNewSelfPosToCompute()){ toCompute = new ArrayList(selfHearedXY); toCompute.removeAll(selfComputedXY); int[] xy = (int[])toCompute.get(0); tmp = world.getPossibleHearedBuildings(xy[0],xy[1], SELF_SEARCH_DIST); selfComputedXY.add(xy); } else if(hasNewMessageHearedToCompute()){ toCompute = new ArrayList(messageHearedXY); toCompute.removeAll(messageComputedXY); int [] xy = (int[]) toCompute.get(0); int indx = messageHearedXY.lastIndexOf(xy); int searchDistExtra =0; MotionlessObject mo = (MotionlessObject) messageHearedPositions.get(indx); if(mo instanceof Road){// Road rd = (Road) mo;// double delt = 1000/rd.length();// double xdel = Math.pow(rd.head().x() - rd.tail().x(),2);// double ydel =Math.pow(rd.head().y() - rd.tail().y(),2);// int ex = (int) (delt * Math.sqrt(xdel+ydel));// System.out.println("--- Computed extra For Road : " + rd + " Is : " + ex);// searchDistExtra +=ex; searchDistExtra =2* XYSEARCH_TOLERANCE; } tmp = world.getPossibleHearedBuildings(xy[0],xy[1],SELF_SEARCH_DIST + searchDistExtra); messageComputedXY.add(xy); } else throw new Error(" WHA@"); if(isFirst) possibleBuildings.addAll(tmp); else possibleBuildings.retainAll(tmp); } private void updateGuessedPos(){ if(possibleBuildings.isEmpty()){ guessedBldg = null; mostProbableBldg = null; } else if(Utils.getConditionCount(possibleBuildings,REFUGE_C) > 0){ List refuges = REFUGE_C.extract(possibleBuildings); guessedBldg = (Building) refuges.iterator().next(); mostProbableBldg = guessedBldg; } else{ Building poss,mp = null; int x, y, cnt =0, xsum=0,ysum=0 ; for(Iterator it = possibleBuildings.iterator();it.hasNext();){ poss = (Building)it.next(); if(poss.isBurned()) continue; if(!world.unvisitedBuildings.contains(poss) ) continue; x= poss.x(); y= poss.y(); xsum += x; ysum +=y; cnt++; } int xavg = xsum/cnt , yavg = ysum/cnt; int dist , best = Integer.MAX_VALUE ; for(Iterator it = possibleBuildings.iterator();it.hasNext();){ poss = (Building)it.next(); if(poss.isBurned()) continue; if(!world.unvisitedBuildings.contains(poss)) continue; x= poss.x(); y= poss.y(); dist = Util.distance(xavg,yavg,x,y); if(dist < best){ mp = poss; best = dist; } } mostProbableBldg = mp; if(guessedBldg == null || ! world.unvisitedBuildings.contains(guessedBldg)){ int randIndx = rnd.nextInt(possibleBuildings.size()); Building res = (Building) possibleBuildings.get(randIndx); guessedBldg = res; } } if(guessedBldg != null){ guessedPoses.add(guessedBldg) ; guessedPosesTime.add(world.time()); } } public int getSearchStartTime(){ if(searchedTime.isEmpty()) return 0; return searchedTime.get(0); } public int getLastSearchTime(){ return searchedTime.get(searchedTime.size()-1); } public void setSearchTime(){ searchedTime.add(world.time()); } public int getCyclesSearched(){ return searchedTime.size(); } public Building getMostProbableBldg(){ return mostProbableBldg; } public void addHearedPosition(MotionlessObject mo , int x , int y , boolean selfHeared){ Collection hearedPoses , XYheared; if(selfHeared){ hearedPoses = selfHearedPositions; XYheared = selfHearedXY; } else{ hearedPoses = messageHearedPositions; XYheared = messageHearedXY; } int [] xy; for(Iterator it = XYheared.iterator();it.hasNext();){ xy = (int[]) it.next(); if(Util.distance(xy[0],xy[1],x,y) <= XYSEARCH_TOLERANCE) return; } hearedPoses.add(mo); xy = new int[2]; xy[0] =x; xy[1] = y; XYheared.add(xy); } private boolean m_reportedInRefuge = false; public boolean isReportedInRefuge(){ return m_reportedInRefuge; } public void setReportedInRefuge(){ m_reportedInRefuge= true; } private int lastHearedTime = -1; public int getLastHearedTime(){ return lastHearedTime; } public void setLastHearedTime(int val){ lastHearedTime = val; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -