public void displayTrain(int trainNumber) {
    this.trainNumber = trainNumber;

    trainViewJPanel1.display(trainNumber);
    String s;
    String destination = "";
    int priority = TrainModel.PRIORITY_NORMAL;
    int state = TrainModel.STATE_STOPPED;
    if (trainNumber >= 0) {
      TrainModel train =
          (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());

      trainCargoBundleKey = train.getCargoBundle();
      CargoBundle cb = (CargoBundle) w.get(train.getCargoBundle());
      s = "Train #" + trainNumber + ": ";
      ScheduleIterator si = train.getScheduleIterator();
      TrainOrdersModel tom = si.getCurrentOrder(w);
      if (tom != null) {
        ObjectKey2 ok = tom.getStation();
        StationModel station = (StationModel) w.get(ok);
        destination = station.getStationName();
      }
      priority = train.getPriority();
      state = train.getState();
      setStatusLabel(train);
    } else {
      s = "No trains to display";
      setStatusLabel(null);
    }
    setStatusButton(priority, state);
    nameJLabel.setText(s);
    destinationJLabel.setText(destination);
  }
示例#2
0
 public static TrackTile createTrackTile(ReadOnlyWorld w, byte trackLayout, int trackType) {
   TrackRule tr = (TrackRule) w.get(KEY.TRACK_RULES, trackType, Player.AUTHORITATIVE);
   if (tr.isDoubleTrack()) {
     return new DoubleTrackTile(trackLayout, trackType);
   }
   return new SingleTrackTile(trackLayout, trackType);
 }
        /**
         * Cycle through train status in the following order: Priority express, standard, slow,
         * stopped
         */
        public void actionPerformed(ActionEvent e) {
          if (trainNumber == -1) return;

          TrainModel tm =
              (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());

          int priority = tm.getPriority();
          int state = tm.getState();

          if (state == TrainModel.STATE_STOPPED) {
            state = TrainModel.STATE_RUNNABLE;
            priority = TrainModel.PRIORITY_EXPRESS;
          } else {
            switch (priority) {
              case TrainModel.PRIORITY_EXPRESS:
                priority = TrainModel.PRIORITY_NORMAL;
                break;
              case TrainModel.PRIORITY_NORMAL:
                priority = TrainModel.PRIORITY_SLOW;
                break;
              case TrainModel.PRIORITY_SLOW:
                state = TrainModel.STATE_STOPPED;
                break;
              default:
                throw new IllegalStateException();
            }
          }
          Move m;
          GameTime now = (GameTime) w.get(ITEM.TIME, Player.AUTHORITATIVE);
          if (tm.getState() != state) {
            m =
                ChangeTrainMove.generateMove(
                    trainNumber, modelRoot.getPlayerPrincipal(), tm, state, now);
            modelRoot.getReceiver().processMove(m);
            tm = (TrainModel) w.get(KEY.TRAINS, trainNumber, modelRoot.getPlayerPrincipal());
          }
          if (tm.getPriority() != priority) {
            m =
                ChangeTrainMove.generatePriorityMove(
                    trainNumber, modelRoot.getPlayerPrincipal(), tm, priority);
            modelRoot.getReceiver().processMove(m);
          }
        }
  /**
   * @return the ObjectKey2 in the STATIONS table of the station owned by player p within the city
   *     radius, or -1 if there is no station
   */
  public ObjectKey2 hasStation(FreerailsPrincipal p) {
    int xmin = cityModel.getCityX() - cityModel.getCityRadius();
    int xmax = cityModel.getCityX() + cityModel.getCityRadius();
    int ymin = cityModel.getCityY() - cityModel.getCityRadius();
    int ymax = cityModel.getCityY() + cityModel.getCityRadius();

    xmin = xmin < 0 ? 0 : xmin;
    ymin = ymin < 0 ? 0 : ymin;
    xmax = xmax >= world.getMapWidth() ? world.getMapWidth() - 1 : xmax;
    ymax = ymax >= world.getMapHeight() ? world.getMapHeight() - 1 : ymax;

    Iterator i = world.getIterator(KEY.STATIONS, p);
    while (i.hasNext()) {
      StationModel sm = (StationModel) i.next();
      int x = sm.getStationX();
      int y = sm.getStationY();
      if (x >= xmin && x <= xmax && y >= ymin && y <= ymax) {
        return new ObjectKey2(KEY.STATIONS, p, sm.getUUID());
      }
    }
    return null;
  }