/**
  * Legions are sorted in descending order of total point value, with the titan legion always
  * coming first.
  *
  * <p>TODO This is inconsistent with equals() which means the Comparable contract is not
  * fulfilled. Probably better of in a Comparator in any case.
  */
 public int compareTo(LegionServerSide other) {
   if (hasTitan()) {
     return Integer.MIN_VALUE;
   } else if (other.hasTitan()) {
     return Integer.MAX_VALUE;
   } else {
     return (other.getPointValue() - this.getPointValue());
   }
 }
  /**
   * Split off creatures into a new legion using legion marker markerId. (Or the first available
   * marker, if markerId is null.) Return the new legion, or null if there's an error.
   */
  LegionServerSide split(List<CreatureType> creatures, String newMarkerId) {
    assert newMarkerId != null : "We need a marker to split";
    PlayerServerSide player = getPlayer();

    player.selectMarkerId(newMarkerId);
    LegionServerSide newLegion =
        new LegionServerSide(
            newMarkerId, this, getCurrentHex(), getCurrentHex(), getPlayer(), game);

    Iterator<CreatureType> it = creatures.iterator();
    while (it.hasNext()) {
      CreatureType creature = it.next();
      creature = removeCreature(creature, false, false);
      if (creature == null) {
        // Abort the split.
        LOGGER.warning("Split aborted since removeCreature(..) returned null");
        newLegion.recombine(this, true);
        return null;
      }
      newLegion.addCreature(creature, false);
    }

    player.addLegion(newLegion);

    game.getServer().allUpdatePlayerInfo("Split");
    LOGGER.log(
        Level.INFO,
        newLegion.getHeight()
            + " creatures are split off from legion "
            + this
            + " into new legion "
            + newLegion);

    sortCritters();
    newLegion.sortCritters();

    // game.getServer().allTellLegionLocation(newMarkerId);

    return newLegion;
  }
  /**
   * Recombine this legion into another legion. Only remove this legion from the Player if remove is
   * true. If it's false, the caller is responsible for removing this legion, which can avoid
   * concurrent access problems. Someone needs to call MasterBoard.alignLegions() on the remaining
   * legion's hexLabel after the recombined legion is actually removed.
   */
  void recombine(Legion legion, boolean remove) {
    // Sanity check
    if (legion == this) {
      LOGGER.log(Level.WARNING, "Tried to recombine a legion with itself!");
      return;
    }
    for (Creature critter : getCreatures()) {
      ((LegionServerSide) legion).addCreature(critter.getType(), false);
    }

    if (remove) {
      remove(false, false);
    } else {
      prepareToRemove(false, false);
    }

    LOGGER.log(Level.INFO, "Legion " + this + " recombined into legion " + legion);

    sortCritters();

    // Let the clients know that the legions have recombined.
    game.getServer().undidSplit(this, legion, true, game.getTurnNumber());
  }