Esempio n. 1
0
  public static void writePosition(NetOutput out, Position pos) throws IOException {
    long x = pos.getX() & POSITION_WRITE_SHIFT;
    long y = pos.getY() & POSITION_Y_SHIFT;
    long z = pos.getZ() & POSITION_WRITE_SHIFT;

    out.writeLong(x << POSITION_X_SIZE | y << POSITION_Y_SIZE | z);
  }
  protected static boolean isTileVisible(
      DrawContext dc, Tile tile, double minDistanceSquared, double maxDistanceSquared) {
    if (!tile.getSector().intersects(dc.getVisibleSector())) return false;

    View view = dc.getView();
    Position eyePos = view.getEyePosition();
    if (eyePos == null) return false;

    Angle lat =
        clampAngle(
            eyePos.getLatitude(),
            tile.getSector().getMinLatitude(),
            tile.getSector().getMaxLatitude());
    Angle lon =
        clampAngle(
            eyePos.getLongitude(),
            tile.getSector().getMinLongitude(),
            tile.getSector().getMaxLongitude());
    Vec4 p = dc.getGlobe().computePointFromPosition(lat, lon, 0d);
    double distSquared = dc.getView().getEyePoint().distanceToSquared3(p);
    //noinspection RedundantIfStatement
    if (minDistanceSquared > distSquared || maxDistanceSquared < distSquared) return false;

    return true;
  }
Esempio n. 3
0
 public static Location getEvacuation(Player player) {
   World world = player.getWorld();
   String[] portalnames;
   if (Permission.has(player, "world.spawn") || Permission.has(player, "tpp")) {
     for (Position pos : getSpawnPoints()) {
       Location loc = pos.toLocation();
       if (loc.getWorld() == null) continue;
       if (Permission.canEnter(player, loc.getWorld())) {
         return loc;
       }
     }
     portalnames = Portal.getPortals();
   } else {
     portalnames = Portal.getPortals(world);
   }
   for (String name : portalnames) {
     if (Permission.canEnterPortal(player, name)) {
       Location loc = Portal.getPortalLocation(name, player.getWorld().getName(), true);
       if (loc == null) continue;
       if (loc.getWorld() == null) continue;
       if (Permission.canEnter(player, loc.getWorld())) {
         return loc;
       }
     }
   }
   return null;
 }
Esempio n. 4
0
 public static Position[] getSpawnPoints(String onWorld) {
   ArrayList<Position> pos = new ArrayList<Position>();
   for (Position p : getSpawnPoints()) {
     if (p.getWorldName().equalsIgnoreCase(onWorld)) {
       pos.add(p);
     }
   }
   return pos.toArray(new Position[0]);
 }
Esempio n. 5
0
 public static Location getRespawnLocation(String ofWorld) {
   Position pos = getRespawn(ofWorld);
   if (pos != null) {
     Location loc = pos.toLocation();
     if (loc.getWorld() != null) {
       return loc;
     }
   }
   return null;
 }
  public ArrayList<Position> getMoves(Position p) {
    String e = ask("moves:" + p.getX() + ":" + p.getY() + ";");
    ArrayList<Position> m = new ArrayList<Position>();

    String[] o = e.split(":");
    for (int i = 0; i < o.length; i = i + 2) {
      m.add(new Position(Integer.parseInt(o[i]), Integer.parseInt(o[i + 1])));
    }

    return m;
  }
  protected static boolean isNameVisible(
      DrawContext dc, PlaceNameService service, Position namePosition) {
    double elevation = dc.getVerticalExaggeration() * namePosition.getElevation();
    Vec4 namePoint =
        dc.getGlobe()
            .computePointFromPosition(
                namePosition.getLatitude(), namePosition.getLongitude(), elevation);
    Vec4 eyeVec = dc.getView().getEyePoint();

    double dist = eyeVec.distanceTo3(namePoint);
    return dist >= service.getMinDisplayDistance() && dist <= service.getMaxDisplayDistance();
  }
  protected static void writeReferencePositions(String filePath, ArrayList<Position> positions)
      throws FileNotFoundException {
    PrintStream os = new PrintStream(new File(filePath));

    for (Position pos : positions) {
      os.format(
          "%.5f %.5f %.4f\n",
          pos.getLatitude().degrees, pos.getLongitude().degrees, pos.getElevation());
    }

    os.flush();
  }
  private boolean atMaxLevel(DrawContext dc) {
    Position vpc = dc.getViewportCenterPosition();
    if (dc.getView() == null || this.getLevels() == null || vpc == null) return false;

    if (!this.getLevels().getSector().contains(vpc.getLatitude(), vpc.getLongitude())) return true;

    Level nextToLast = this.getLevels().getNextToLastLevel();
    if (nextToLast == null) return true;

    Sector centerSector =
        nextToLast.computeSectorForPosition(
            vpc.getLatitude(), vpc.getLongitude(), this.getLevels().getTileOrigin());
    return this.needToSplit(dc, centerSector);
  }
Esempio n. 10
0
  public void gotoPosition(ImmutablePosition pos, boolean silent) {
    if (m_position.equals(pos)) return; // =====>

    int curNode = getCurNode();
    gotoStart(true);
    do {
      if (m_position.equals(pos)) {
        int posNode = getCurNode();
        gotoNode(curNode, true);
        gotoNode(posNode, silent);
        return; // =====>
      }
    } while (goForward(true));
  }
Esempio n. 11
0
 public void move(Piece p, Position pos) {
   String s =
       ask(
           "move:"
               + p.getPos().getX()
               + ":"
               + p.getPos().getY()
               + ":"
               + pos.getX()
               + ":"
               + pos.getY()
               + ";");
   System.out.println(s);
 }
Esempio n. 12
0
  /**
   * Compute the lat/lon position of the view center
   *
   * @param dc the current DrawContext
   * @param view the current View
   * @return the ground position of the view center or null
   */
  protected Position computeGroundPosition(DrawContext dc, View view) {
    if (view == null) return null;

    Position groundPos =
        view.computePositionFromScreenPoint(
            view.getViewport().getWidth() / 2, view.getViewport().getHeight() / 2);
    if (groundPos == null) return null;

    double elevation =
        dc.getGlobe().getElevation(groundPos.getLatitude(), groundPos.getLongitude());
    return new Position(
        groundPos.getLatitude(),
        groundPos.getLongitude(),
        elevation * dc.getVerticalExaggeration());
  }
  private Vec4 computeReferencePoint(DrawContext dc) {
    if (dc.getViewportCenterPosition() != null)
      return dc.getGlobe().computePointFromPosition(dc.getViewportCenterPosition());

    java.awt.geom.Rectangle2D viewport = dc.getView().getViewport();
    int x = (int) viewport.getWidth() / 2;
    for (int y = (int) (0.5 * viewport.getHeight()); y >= 0; y--) {
      Position pos = dc.getView().computePositionFromScreenPoint(x, y);
      if (pos == null) continue;

      return dc.getGlobe().computePointFromPosition(pos.getLatitude(), pos.getLongitude(), 0d);
    }

    return null;
  }
Esempio n. 14
0
 public Move getNextMove(int whichLine) {
   short shortMove = m_moves.getMove(m_moves.goForward(m_cur, whichLine));
   if (shortMove == GameMoveModel.NO_MOVE) return null; // =====>
   try {
     m_position.setNotifyListeners(false);
     m_position.doMove(shortMove);
     //            ChMove move = m_position.getLastMove(shortMove);
     Move move = m_position.getLastMove();
     m_position.undoMove();
     m_position.setNotifyListeners(true);
     return move;
   } catch (IllegalMoveException ex) {
     ex.printStackTrace();
     return null;
   }
 }
Esempio n. 15
0
  private boolean goBackInLine(boolean silent) {
    if (DEBUG) System.out.println("goBackInLine");

    int index = m_moves.goBack(m_cur, false);
    if (index != -1) {
      m_cur =
          index; // needs to be set before undoing the move to allow listeners to check for curNode
      m_ignoreNotifications = true;
      if (silent) m_position.setNotifyListeners(false);
      m_position.undoMove();
      if (silent) m_position.setNotifyListeners(true);
      m_ignoreNotifications = false;
      return true;
    } else {
      return false;
    }
  }
Esempio n. 16
0
  public Move[] getMainLine() {
    int num = 0;
    int index = m_cur;
    while (m_moves.hasNextMove(index)) {
      index = m_moves.goForward(index);
      num++;
    }

    Move[] moves = new Move[num];
    for (int i = 0; i < num; i++) {
      moves[i] = goForwardAndGetMove(true);
    }

    m_position.setNotifyListeners(false);
    for (int i = 0; i < moves.length; i++) m_position.undoMove();
    m_position.setNotifyListeners(true);

    return moves;
  }
Esempio n. 17
0
 public Move[] getNextMoves() {
   m_position.setNotifyListeners(false);
   Move[] moves = new Move[m_moves.getNumOfNextMoves(m_cur)];
   for (int i = 0; i < moves.length; i++) {
     short move = m_moves.getMove(m_moves.goForward(m_cur, i));
     try {
       m_position.doMove(move);
       //                moves[i] = m_position.getLastMove(move);
       moves[i] = m_position.getLastMove();
       m_position.undoMove();
     } catch (IllegalMoveException ex) {
       m_moves.write(System.out);
       System.out.println("cur = " + m_cur + " move=" + GameMoveModel.valueToString(move));
       ex.printStackTrace();
     }
   }
   m_position.setNotifyListeners(true);
   return moves;
 }
Esempio n. 18
0
  public static void writeEntityMetadata(NetOutput out, EntityMetadata[] metadata)
      throws IOException {
    for (EntityMetadata meta : metadata) {
      int id = MagicValues.value(Integer.class, meta.getType()) << 5 | meta.getId() & 0x1F;
      out.writeByte(id);
      switch (meta.getType()) {
        case BYTE:
          out.writeByte((Byte) meta.getValue());
          break;
        case SHORT:
          out.writeShort((Short) meta.getValue());
          break;
        case INT:
          out.writeInt((Integer) meta.getValue());
          break;
        case FLOAT:
          out.writeFloat((Float) meta.getValue());
          break;
        case STRING:
          out.writeString((String) meta.getValue());
          break;
        case ITEM:
          writeItem(out, (ItemStack) meta.getValue());
          break;
        case POSITION:
          Position pos = (Position) meta.getValue();
          out.writeInt(pos.getX());
          out.writeInt(pos.getY());
          out.writeInt(pos.getZ());
          break;
        case ROTATION:
          Rotation rot = (Rotation) meta.getValue();
          out.writeFloat(rot.getPitch());
          out.writeFloat(rot.getYaw());
          out.writeFloat(rot.getRoll());
          break;
        default:
          throw new IOException("Unmapped metadata type: " + meta.getType());
      }
    }

    out.writeByte(127);
  }
Esempio n. 19
0
 /**
  * Compute the view range footprint on the globe.
  *
  * @param dc the current <code>DrawContext</code>
  * @param steps the number of steps.
  * @return an array list of <code>LatLon</code> forming a closed shape.
  */
 protected ArrayList<LatLon> computeViewFootPrint(DrawContext dc, int steps) {
   ArrayList<LatLon> positions = new ArrayList<LatLon>();
   Position eyePos = dc.getView().getEyePosition();
   Angle distance =
       Angle.fromRadians(
           Math.asin(
               dc.getView().getFarClipDistance()
                   / (dc.getGlobe().getRadius() + eyePos.getElevation())));
   if (distance.degrees > 10) {
     double headStep = 360d / steps;
     Angle heading = Angle.ZERO;
     for (int i = 0; i <= steps; i++) {
       LatLon p = LatLon.greatCircleEndPosition(eyePos, heading, distance);
       positions.add(p);
       heading = heading.addDegrees(headStep);
     }
     return positions;
   } else return null;
 }
Esempio n. 20
0
  private boolean goBack(boolean silent) {
    if (DEBUG) System.out.println("goBack");

    int index = m_moves.goBack(m_cur, true);
    if (index != -1) {
      //        if (m_position.canUndoMove()) {  // do not rely on position since in silent mode it
      // is not updated
      //            m_cur = m_moves.goBack(m_cur, true);
      m_cur = index;
      m_ignoreNotifications = true;
      if (silent) m_position.setNotifyListeners(false);
      m_position.undoMove();
      if (silent) m_position.setNotifyListeners(true);
      m_ignoreNotifications = false;
      return true;
    } else {
      return false;
    }
  }
Esempio n. 21
0
  private void setModel(GameModel gameModel) {
    m_model = gameModel;
    m_header = gameModel.getHeaderModel();
    m_moves = gameModel.getMoveModel();

    String fen = m_header.getTag(PGN.TAG_FEN);
    if (fen != null) {
      setPosition(new Position(fen, false));
    } else {
      setPosition(Position.createInitialPosition());
    }
  }
Esempio n. 22
0
  @Test
  public void testMoveStep() {
    robot = new Robot(new Energy(2400, unitOfPower.Ws), Orientation.RIGHT);
    robot.setPosition(Position.newPosition(3, 4, board));

    Program moveProgram = new Program(move, robot);
    robot.setProgram(moveProgram);

    assertEquals(1, Position.newPosition(3, 4, board).getElements().size());
    assertEquals(0, Position.newPosition(4, 4, board).getElements().size());
    assertEquals(0, Position.newPosition(5, 4, board).getElements().size());
    assertEquals(Orientation.RIGHT, robot.getOrientation());
    assertEquals(2400, robot.getAmountOfEnergy(), epsilon);

    moveProgram.step();

    assertEquals(0, Position.newPosition(3, 4, board).getElements().size());
    assertEquals(1, Position.newPosition(4, 4, board).getElements().size());
    assertEquals(0, Position.newPosition(5, 4, board).getElements().size());
    assertEquals(Orientation.RIGHT, robot.getOrientation());
    assertEquals(1900, robot.getAmountOfEnergy(), epsilon);

    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();
    moveProgram.step();

    assertEquals(0, Position.newPosition(3, 4, board).getElements().size());
    assertEquals(1, Position.newPosition(4, 4, board).getElements().size());
    assertEquals(0, Position.newPosition(5, 4, board).getElements().size());
    assertEquals(Orientation.RIGHT, robot.getOrientation());
    assertEquals(1900, robot.getAmountOfEnergy(), epsilon);
  }
  protected static ArrayList<Position> readReferencePositions(String filePath)
      throws FileNotFoundException {
    ArrayList<Position> positions = new ArrayList<Position>();
    Scanner scanner = new Scanner(new File(filePath));

    while (scanner.hasNextDouble()) {
      double lat = scanner.nextDouble();
      double lon = scanner.nextDouble();
      double elevation = scanner.nextDouble();
      positions.add(Position.fromDegrees(lat, lon, elevation));
    }

    return positions;
  }
Esempio n. 24
0
  // board stuff
  public Piece getNet(Position p) {
    Piece tmp = null;

    String b = ask("get:" + p.getX() + ":" + p.getY() + ";");
    String[] cms = b.split(":");

    if (cms[0].equals("n")) {
      return tmp;
    }
    char[] chr = cms[0].toCharArray();
    char c = chr[0];
    Player s = new Player(cms[2].equals("b"), null);
    int l = Integer.parseInt(cms[1]);

    if (c == 'P') tmp = new Pawn(p, l, s);
    else if (c == 'R') tmp = new Rook(p, l, s);
    else if (c == 'K') tmp = new Knight(p, l, s);
    else if (c == 'B') tmp = new Bishop(p, l, s);
    else if (c == 'Q') tmp = new Queen(p, l, s);
    else if (c == '!') tmp = new King(p, l, s);

    return tmp;
  }
Esempio n. 25
0
  private boolean goForward(int whichLine, boolean silent) {
    if (DEBUG) System.out.println("goForward " + whichLine);

    int index = m_moves.goForward(m_cur, whichLine);
    short shortMove = m_moves.getMove(index);
    if (DEBUG) System.out.println("  move = " + Move.getString(shortMove));
    if (shortMove != GameMoveModel.NO_MOVE) {
      try {
        m_cur = index;
        m_ignoreNotifications = true;
        if (silent) m_position.setNotifyListeners(false);
        m_position.doMove(shortMove);
        if (silent) m_position.setNotifyListeners(true);
        m_ignoreNotifications = false;
        return true;
      } catch (IllegalMoveException ex) {
        ex.printStackTrace();
      }
    } else {
      //            new Exception("Forward at end of line").printStackTrace();
    }
    return false;
  }
    protected boolean isNavSectorVisible(
        DrawContext dc, double minDistanceSquared, double maxDistanceSquared) {
      if (!navSector.intersects(dc.getVisibleSector())) return false;

      View view = dc.getView();
      Position eyePos = view.getEyePosition();
      if (eyePos == null) return false;

      // check for eyePos over globe
      if (Double.isNaN(eyePos.getLatitude().getDegrees())
          || Double.isNaN(eyePos.getLongitude().getDegrees())) return false;

      Angle lat =
          clampAngle(eyePos.getLatitude(), navSector.getMinLatitude(), navSector.getMaxLatitude());
      Angle lon =
          clampAngle(
              eyePos.getLongitude(), navSector.getMinLongitude(), navSector.getMaxLongitude());
      Vec4 p = dc.getGlobe().computePointFromPosition(lat, lon, 0d);
      double distSquared = dc.getView().getEyePoint().distanceToSquared3(p);
      //noinspection RedundantIfStatement
      if (minDistanceSquared > distSquared || maxDistanceSquared < distSquared) return false;

      return true;
    }
Esempio n. 27
0
 /**
  * Returns whether the given position occurs in the main line of this game.
  *
  * @param position the position to look for, must not be null
  * @return whether the given position occurs in the main line of this game
  */
 public boolean containsPosition(ImmutablePosition position) {
   boolean res = false;
   int index = getCurNode();
   gotoStart(true);
   for (; ; ) {
     if (m_position.getHashCode() == position.getHashCode()) {
       res = true;
       break;
     }
     if (!hasNextMove()) break;
     goForward(true);
   }
   gotoNode(index, true);
   return res;
 }
  protected static ArrayList<Position> generateReferenceLocations(
      Sector sector, int numLats, int numLons) {
    ArrayList<Position> locations = new ArrayList<Position>();
    double dLat =
        (sector.getMaxLatitude().degrees - sector.getMinLatitude().degrees) / (numLats - 1);
    double dLon =
        (sector.getMaxLongitude().degrees - sector.getMinLongitude().degrees) / (numLons - 1);
    for (int j = 0; j < numLats; j++) {
      double lat = sector.getMinLatitude().degrees + j * dLat;

      for (int i = 0; i < numLons; i++) {
        double lon = sector.getMinLongitude().degrees + i * dLon;

        // Specify angles to five decimal places.
        locations.add(
            Position.fromDegrees(
                Math.round(lat * 100000.0) / 100000.0, Math.round(lon * 100000.0) / 100000.0, 0));
      }
    }

    return locations;
  }
Esempio n. 29
0
  public void update(java.util.Observable observable, java.lang.Object obj) {
    Navigation navigation_ = null;
    try {
      navigation_ = (Navigation) obj;
    } catch (Exception excp) {
      System.out.println(excp);
      System.out.println("Visualization Component received wrong data type!");
    }
    if (navigation_ != null) {
      // make sure all the values are in the proper range.
      navigation_.roll = (navigation_.roll > 180 || navigation_.roll < -180) ? 0 : navigation_.roll;
      navigation_.pitch =
          (navigation_.pitch > 90 || navigation_.pitch < -90) ? 0 : navigation_.pitch;

      // update the artificial horizon
      alt_hor_.update_display(navigation_.roll, navigation_.pitch);

      navigation_.pitch =
          (navigation_.position_latitude > 90 || navigation_.position_latitude < -90)
              ? 0
              : navigation_.position_latitude;
      navigation_.pitch =
          (navigation_.position_longitude > 100 || navigation_.position_longitude < 00)
              ? 0
              : navigation_.position_longitude;
      navigation_.pitch =
          (navigation_.altitude > 90 || navigation_.altitude < -90) ? 0 : navigation_.altitude;
      navigation_.pitch =
          (navigation_.heading > 180 || navigation_.heading < -180) ? 0 : navigation_.heading;

      // update the position display
      pos_.update_display(
          navigation_.position_latitude,
          navigation_.position_longitude,
          navigation_.altitude,
          navigation_.heading);
    }
  }
Esempio n. 30
0
 public String stringfor(Position p) {
   return "" + p.element();
 }