⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 movingobject.java

📁 2004年robotcup世界冠军源代码
💻 JAVA
字号:
// Copyright (C) 2002 Takeshi Morimoto <morimoto@takopen.cs.uec.ac.jp>// All rights reserved.// Edited By Omid Aghazadehpackage yab.agent.object;import java.util.*;import yab.agent.DisasterSpace;import yab.io.object.*;import MRL.Utilities.Partitioning.Path;/** * edited By Omid Aghazadeh . */public abstract class MovingObject extends RealObject implements MRL.Utilities.ConstantConditions , yab.agent.Constants{  public MovingObject(int id, BaseMovingObject object,                      DisasterSpace world) {    super(id, object, world);    Arrays.fill(subMittedAct, -1);    Arrays.fill(wereLockedByBlockade,false);  }  private final int SIM_TIME = yab.agent.                                    Constants.SIMULATING_TIME;  private int[][] positionHistory = new int[SIM_TIME+2][];  private BaseMovingObject obj() {    return (BaseMovingObject) object;  }  public int x() {    if (!(position() instanceof Edge))      return position().x();    Edge e = (Edge) position();    return e.head().x()        + (e.tail().x() - e.head().x()) * positionExtra() / e.length();  }  public int y() {    if (!(position() instanceof Edge))      return position().y();    Edge e = (Edge) position();    return e.head().y()        + (e.tail().y() - e.head().y()) * positionExtra() / e.length();  }  public MotionlessObject motionlessPosition() {    return position().motionlessPosition();  }  public RealObject position() {    return (RealObject) world.get(obj().position());  }  public int positionExtra() {    return obj().positionExtra();  }  /*     public boolean isMissed() {    return (time() != world.time()            && time() == motionlessPosition().time());     }     public boolean isLost() {    return (time() != world.time()            && time() < motionlessPosition().time());     }   */  public void updatePositionHistory() { // Should Be Run Each Cycle ONCE    if(time() > 300) return;    int [] ph = obj().positionHistory();    int time = time();    positionHistory[time] = ph;    moHistory[time] = motionlessPosition();    positionExtra[time] = positionExtra();    selfX[time] = x();    selfY[time] = y();    hasMovedInPrevCycle[time] = hasMovedInLastCycle();    stuckCycles[time] = calStuckCycles();    m_poshistObjs.clear();    m_poshistRoads.clear();    m_poshistNodes.clear();    if(ph[0]==0) {//        if(ph.length != 1) throw new Error("WTFFFFFFFFFFFFFF" + ph.length + " Ph[1] " + ph[1]);        return;    }    Node node;    for (int i = 0; i < ph.length; i++) {      node = (Node)world.get(ph[i]);//      if(node == null) throw new Error("WHA!");      m_poshistNodes.add(node);    }    m_poshistObjs.addAll(m_poshistNodes);    if(ph.length <2 ) return ;    List roads;    Node nextNode;    Road road;    int size = m_poshistNodes.size();    for(int i = 0 ; i < size -1 ;i++){      node = (Node) m_poshistNodes.get(i);      nextNode = (Node) m_poshistNodes.get(i+1);      roads = node.getConnectedRoads();      for (Iterator it = roads.iterator(); it.hasNext(); ) {        road = (Road) it.next();        if ((road.head() == node && road.tail() == nextNode) ||            (road.tail() == node && road.head() == nextNode)) {          m_poshistRoads.add(road);          break;        }      }    }    m_poshistObjs.addAll(m_poshistRoads);    /*    for (int i = 0; i < ph.length - 1; i++) {      node = (Node) world.get(ph[i]);      nextNode = (Node) world.get(ph[i + 1]);      roads = node.getConnectedRoads();      for (Iterator it = roads.iterator(); it.hasNext(); ) {        road = (Road) it.next();        if ((road.head() == node && road.tail() == nextNode) ||            (road.tail() == node && road.head() == nextNode)) {          m_poshistRoads.add(road);          break;        }      }    }   */  }  public int[] getPositionHistory(int time){    return positionHistory[time];  }  private final List m_poshistObjs = new ArrayList();  public List getPosHistObjs(){    return m_poshistObjs;  }  private final List m_poshistRoads = new ArrayList();  public List getPosHistRoads() { // Doesnt Include Roads That Aren't Completely Travelled    return m_poshistRoads;  }  private final List m_poshistNodes = new ArrayList();  public List getPosHistNodes() {    return m_poshistNodes;  }  private boolean hasMovedInLastCycle() {//    if(obj().positionHistory() == null) return false;    int m_posLen ;    int [] ph = obj().positionHistory();    if(ph[0] == 0) return false;    m_posLen =ph.length;    if(m_posLen > 1 ) return true;    else if(m_posLen == 0 ) return false;    else{ // m_posLen == 1        if(motionlessPosition() instanceof Road) return true;        else { // He's On A Node.            if (time() < TIME_STARTING_ACTION + 1)                           return false;            int [] prevPH = getPositionHistory(time() -1);            if(prevPH.length == 0 )return true; // He Were On A Road            else{                int nodeID = obj().positionHistory()[0];                int prevnodeID = prevPH[prevPH.length -1];                if(nodeID == prevnodeID) return false;                else return true;            }        }    }//      return HasMovedInCycle(time());  }  private boolean [] hasMovedInPrevCycle = new boolean[SIM_TIME + 2];  public boolean HasMovedInPrevCycle(int time){      return hasMovedInPrevCycle[time];  }  public boolean HasMovedInLastCycle(){      return hasMovedInPrevCycle[time()];  }//  private boolean getHasMovedInCycle(int time) {//    if(time > time()) return false;//    int [] cPH = getPositionHistory(time);//    int cposLen = cPH.length;//    if(cposLen > 1 ) return true;//    else if(cposLen == 0 ) return false;//    else{//        if(getMotionlessPosition(time) instanceof Road) return true;//        else {//            int [] prevPH = getPositionHistory(time-1);//            if(prevPH.length == 0 )return true;//            else{//                int nodeID = cPH[0];//                int prevnodeID = prevPH[prevPH.length -1];//                if(nodeID == prevnodeID) return false;//                else return true;//            }//        }//    }//  }  public boolean isSeen() {    return position() != null;  }  public void showPositionHistory() {    String res = Integer.toString(id);    for (int i = 0; i < positionHistory.length; i++) {//      res += " , : " + world.get(positionhistory[i]);    }  }    private MotionlessObject moHistory[] = new MotionlessObject[SIM_TIME + 2];    private int positionExtra [] = new int[SIM_TIME + 2];    public MotionlessObject getMotionlessPosition(int time){        return (MotionlessObject) moHistory[time];    }    public int getPositionExtra(int time){        return positionExtra[time];    }    public boolean getSubmittedMoveAct(int time){        return subMittedAct[time] == MOVE_ACT;    }    public boolean submittedMoveAct(){        return subMittedAct[time()-1]==MOVE_ACT;    }    public boolean rested(){        return subMittedAct[time() -1] == REST_ACT;    }    public boolean getRested(int time){        return subMittedAct[time] == REST_ACT;    }    private int [] subMittedAct = new int [SIM_TIME +2 ];    public void setAct(int act){        if(! isValidAct(act))            throw new Error("WHA");        subMittedAct[time()] = act;    }    public boolean submittedAction(){        return subMittedAct[time() - 1] > MOVE_ACT;    }    private boolean isValidAct(int act){        if(act < REST_ACT || act > CLEAR_ACT)            return false;        return true;    }    public boolean getSubmittedAction(int time){        return subMittedAct[time] > MOVE_ACT;    }    private int [] stuckCycles =new int [SIM_TIME + 2];    private int calStuckCycles(){//        int time = time();//        int checkLen = time - TIME_STARTING_ACTION;//        if(checkLen <=0) return 0;//        int res = 0;//        for(int i = 0 ; i < checkLen ; i++){//            if(!HasMovedInPrevCycle(time-i) && getSubmittedMoveAct(time-i-1))//                res++;//            else//                break;//        }//        return res;        int prevVal = stuckCycles[time() -1];        if(!submittedMoveAct()){            return 0;        }        else{            if(hasMovedInPrevCycle[time()]){                return 0;            }            else{                return prevVal +1;            }        }    }    public int getStuckCycles(int time){        return stuckCycles[time];    }    public int getStuckCycles(){        return stuckCycles[time()];    }    private boolean wereLockedByBlockade [] = new boolean [SIM_TIME+2];    public void setWereLockedByBlockade(){        wereLockedByBlockade[time()] = true;    }    public boolean getWereLockedByBlockade(int time){        return wereLockedByBlockade[time];    }    public boolean getWereLockedByBlockade(){        return wereLockedByBlockade[time()-1];    }    public int getCyclesHaveBeenInPath(Path path , boolean countActCycles){        int res = 0;        for(int i = time();i> TIME_STARTING_ACTION; i--){            if(path.IsContaining(moHistory[i])){                if(!countActCycles){                    if(getSubmittedAction(i))                        break;                }                res++;            }            else                break;        }        return res;    }    private final int [] selfX = new int [SIM_TIME+2];    private final int [] selfY = new int [SIM_TIME+2];    public int getSelfX(int time){        return selfX[time];    }    public int getSelfY(int time){        return selfY[time];    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -