Example #1
0
 public final void setPlayers(PlayerSlot[] players) {
   int num_humans = countHumans(players);
   int[] player_slots = new int[num_humans];
   int[] player_ratings = new int[num_humans];
   int[] player_teams = new int[num_humans];
   int human_index = 0;
   updating = true;
   SortedSet new_human_names = new TreeSet();
   for (int i = 0; i < players.length; i++) {
     PlayerSlot player = players[i];
     PulldownButton slot_button = slot_buttons[i];
     PulldownButton race_button = race_buttons[i];
     PulldownButton team_button = team_buttons[i];
     Diode ready_mark = ready_marks[i];
     ready_mark.setLit(player.isReady());
     race_button.getMenu().chooseItem(player.getInfo() != null ? player.getInfo().getRace() : 0);
     team_button.getMenu().chooseItem(player.getInfo() != null ? player.getInfo().getTeam() : 0);
     if (player.getType() != PlayerSlot.CLOSED) {
       slot_button
           .getMenu()
           .getItem(OPEN_INDEX)
           .setLabelString(Utils.getBundleString(bundle, "open"));
       slot_button.getMenu().chooseItem(OPEN_INDEX);
     } else {
       slot_button.getMenu().chooseItem(CLOSED_INDEX);
     }
     race_button.setDisabled(true);
     team_button.setDisabled(true);
     if (player.getInfo() != null) {
       PlayerInfo player_info = player.getInfo();
       switch (player.getType()) {
         case PlayerSlot.AI:
           assert !rated;
           slot_button.getMenu().chooseItem(player.getAIDifficulty() + 1);
           race_button.setDisabled(!canControlSlot(i));
           team_button.setDisabled(!canControlSlot(i));
           break;
         case PlayerSlot.HUMAN:
           String player_name = player_info.getName();
           new_human_names.add(player_name);
           slot_button.getMenu().getItem(OPEN_INDEX).setLabelString(player_name);
           slot_button.getMenu().chooseItem(OPEN_INDEX);
           race_button.setDisabled(i != local_player_slot);
           team_button.setDisabled(i != local_player_slot);
           player_slots[human_index] = i;
           player_ratings[human_index] = player.getRating();
           player_teams[human_index] = player_info.getTeam();
           human_index++;
           break;
         default:
           throw new RuntimeException("Unknown Player type: " + player.getType());
       }
     }
   }
   if (rated)
     updateRatedLabels(
         player_slots,
         player_ratings,
         GameSession.calculateMatchPoints(player_ratings, player_teams));
   Iterator it = new_human_names.iterator();
   while (it.hasNext()) {
     String name = (String) it.next();
     if (human_names.contains(name)) human_names.remove(name);
     else playerJoined(name);
   }
   it = human_names.iterator();
   while (it.hasNext()) {
     String name = (String) it.next();
     playerLeft(name);
   }
   human_names = new_human_names;
   setReady(players[local_player_slot].isReady());
   setStartEnable(players);
   updating = false;
 }
Example #2
0
 private final void adjustPlayerSlot(int player_slot) {
   if (updating || game_network.getClient() == null) return;
   PlayerSlot player = game_network.getClient().getPlayers()[player_slot];
   int index = slot_buttons[player_slot].getMenu().getChosenItemIndex();
   if (index == COMPUTER_HARD_INDEX && !Renderer.isRegistered()) {
     slot_buttons[player_slot].getMenu().chooseItem(COMPUTER_NORMAL_INDEX);
     index = COMPUTER_NORMAL_INDEX;
     ResourceBundle db = ResourceBundle.getBundle(DemoForm.class.getName());
     Form demo_form =
         new DemoForm(
             gui_root,
             Utils.getBundleString(db, "hard_ai_unavailable_header"),
             new GUIImage(512, 256, 0f, 0f, 1f, 1f, "/textures/gui/demo_hardai"),
             Utils.getBundleString(db, "hard_ai_unavailable"));
     gui_root.addModalForm(demo_form);
   }
   int race_index = race_buttons[player_slot].getMenu().getChosenItemIndex();
   int team_index = team_buttons[player_slot].getMenu().getChosenItemIndex();
   int difficulty_index = slot_buttons[player_slot].getMenu().getChosenItemIndex() - 1;
   boolean race_changed = player.getInfo() == null || race_index != player.getInfo().getRace();
   boolean team_changed = player.getInfo() == null || team_index != player.getInfo().getTeam();
   boolean ready_changed = ready != player.isReady();
   boolean difficulty_changed =
       player.getInfo() == null || player.getAIDifficulty() != difficulty_index;
   PulldownButton slot_button = slot_buttons[player_slot];
   switch (index) {
     case OPEN_INDEX:
       if ((player.getType() != PlayerSlot.OPEN && player.getType() != PlayerSlot.HUMAN)
           || race_changed
           || team_changed
           || ready_changed) {
         if (player_slot == local_player_slot) {
           int new_type = PlayerSlot.HUMAN;
           game_network
               .getClient()
               .getServerInterface()
               .setPlayerSlot(
                   player_slot, new_type, race_index, team_index, ready, PlayerSlot.AI_NONE);
         } else {
           game_network.getClient().getServerInterface().resetSlotState(player_slot, true);
         }
       }
       break;
     case CLOSED_INDEX:
       if (player.getType() != PlayerSlot.CLOSED || race_changed || team_changed) {
         slot_button
             .getMenu()
             .getItem(OPEN_INDEX)
             .setLabelString(Utils.getBundleString(bundle, "open"));
         game_network.getClient().getServerInterface().resetSlotState(player_slot, false);
       }
       break;
     case COMPUTER_EASY_INDEX:
     case COMPUTER_NORMAL_INDEX:
     case COMPUTER_HARD_INDEX:
       assert !rated;
       boolean new_ai = player.getType() != PlayerSlot.AI;
       if (new_ai || race_changed || team_changed || difficulty_changed) {
         slot_button
             .getMenu()
             .getItem(OPEN_INDEX)
             .setLabelString(Utils.getBundleString(bundle, "open"));
         if (new_ai) {
           team_index = player_slot;
           race_index =
               new Random(LocalEventQueue.getQueue().getHighPrecisionManager().getTick())
                   .nextInt(RacesResources.getNumRaces());
         }
         game_network
             .getClient()
             .getServerInterface()
             .setPlayerSlot(
                 player_slot, PlayerSlot.AI, race_index, team_index, true, difficulty_index);
       }
       break;
     default:
       throw new RuntimeException("Invalid item index");
   }
 }