numEven += Math.round (evenTracker[0] * (teamEvenTurns % min) / min) - evenTracker[1];
numEven += Math.ceil (evenTracker[0] * (teamEvenTurns % min) / min - 0.5) - evenTracker[1];
    private void determineTurnOrder(int phase) {        // Determine whether infantry and/or Protomechs move        // and/or deploy even according to game options.        boolean infMoveEven =            ( game.getOptions().booleanOption("inf_move_even") &&              (game.getPhase() == Game.PHASE_INITIATIVE ||               game.getPhase() == Game.PHASE_MOVEMENT) ) ||            ( game.getOptions().booleanOption("inf_deploy_even") &&              game.getPhase() == Game.PHASE_DEPLOYMENT );        boolean infMoveMulti =            game.getOptions().booleanOption("inf_move_multi");        boolean protosMoveEven =            ( game.getOptions().booleanOption("protos_move_even") &&              (game.getPhase() == Game.PHASE_INITIATIVE ||               game.getPhase() == Game.PHASE_MOVEMENT) ) ||            ( game.getOptions().booleanOption("protos_deploy_even") &&              game.getPhase() == Game.PHASE_DEPLOYMENT );        boolean protosMoveMulti =            game.getOptions().booleanOption("protos_move_multi");        boolean protosFireMulti = !protosMoveMulti &&            game.getPhase() == Game.PHASE_FIRING;        int evenMask = 0;        if ( infMoveEven ) evenMask += GameTurn.CLASS_INFANTRY;        if ( protosMoveEven ) evenMask += GameTurn.CLASS_PROTOMECH;        // Reset all of the Players' turn category counts        for (Enumeration loop = game.getPlayers(); loop.hasMoreElements();) {            final Player player = (Player) loop.nextElement();            player.resetEvenTurns();            player.resetMultiTurns();            player.resetOtherTurns();            // Add turns for protomechs weapons declaration.            if ( protosFireMulti ) {                // How many Protomechs does the player have?                int numPlayerProtos = game.getSelectedEntityCount                    ( new EntitySelector() {                            private final int ownerId = player.getId();                            public boolean accept( Entity entity ) {                                if ( entity instanceof Protomech &&                                     ownerId == entity.getOwnerId() )                                    return true;                                return false;                            }                        } );                int numProtoUnits =                    (int) Math.ceil( ((double) numPlayerProtos) / 5.0 );                for ( int unit = 0; unit < numProtoUnits; unit++ ) {                    if ( protosMoveEven ) player.incrementEvenTurns();                    else player.incrementOtherTurns();                }            } // End handle-proto-firing-turns        } // Handle the next player        // Go through all entities, and update the turn categories of the        // entity's player.  The teams get their totals from their players.        // N.B. protomechs declare weapons fire based on their point.        for (Enumeration loop = game.getEntities(); loop.hasMoreElements();) {            final Entity entity = (Entity)loop.nextElement();            if (entity.isSelectableThisTurn(game)) {                final Player player = entity.getOwner();                final Team team = game.getTeamForPlayer( player );                if ( entity instanceof Infantry ) {                    if ( infMoveEven ) player.incrementEvenTurns();                    else if ( infMoveMulti ) player.incrementMultiTurns();                    else player.incrementOtherTurns();                }                else if ( entity instanceof Protomech ) {                    if ( !protosFireMulti ) {                        if ( protosMoveEven ) player.incrementEvenTurns();                        else if ( protosMoveMulti ) player.incrementMultiTurns();                        else player.incrementOtherTurns();                    }                }                else                    player.incrementOtherTurns();            }        }        // Generate the turn order for the Players *within*        // each Team.  Map the teams to their turn orders.        // Count the number of teams moving this turn.        Hashtable allTeamTurns = new Hashtable( game.getTeamsVector().size() );        Hashtable evenTrackers = new Hashtable( game.getTeamsVector().size() );        int numTeamsMoving = 0;        for (Enumeration loop = game.getTeams(); loop.hasMoreElements(); ) {            final Team team = (Team) loop.nextElement();            allTeamTurns.put( team, team.determineTeamOrder() );            // Track both the number of times we've checked the team for            // "leftover" turns, and the number of "leftover" turns placed.            int[] evenTracker = new int[2];            evenTracker[0] = 0;            evenTracker[1] = 0;            evenTrackers.put (team, evenTracker);            // Count this team if it has any "normal" moves.            if (team.getNormalTurns() > 0)                numTeamsMoving++;        }        // Now, generate the global order of all teams' turns.        TurnVectors team_order = TurnOrdered.generateTurnOrder            ( game.getTeamsVector() );        // See if there are any loaded units stranded on immobile transports.        Enumeration strandedUnits = game.getSelectedEntities            ( new EntitySelector() {                    public boolean accept( Entity entity ) {                        if ( Server.this.game.isEntityStranded(entity) )                            return true;                        return false;                    }                } );        // Now, we collect everything into a single vector.        Vector turns;        if ( strandedUnits.hasMoreElements() &&             game.getPhase() == Game.PHASE_MOVEMENT ) {            // Add a game turn to unload stranded units, if this            //  is the movement phase.            turns = new Vector( team_order.getNormalTurns() +                                team_order.getEvenTurns() + 1);            turns.addElement( new GameTurn.UnloadStrandedTurn(strandedUnits) );        } else {            // No stranded units.            turns = new Vector( team_order.getNormalTurns() +                                team_order.getEvenTurns() );        }        // Walk through the global order, assigning turns        // for individual players to the single vector.        // Keep track of how many turns we've added to the vector.        Team prevTeam = null;        int min = team_order.getMin();        for ( int numTurn = 0; team_order.hasMoreElements(); numTurn++ ) {            Team team = (Team) team_order.nextElement();            TurnVectors withinTeamTurns = (TurnVectors) allTeamTurns.get(team);            int[] evenTracker = (int[]) evenTrackers.get (team);            float teamEvenTurns = (float) team.getEvenTurns();            // Calculate the number of "even" turns to add for this team.            int numEven = 0;            if (1 == numTeamsMoving) {                // The only team moving should move all "even" units.                numEven += teamEvenTurns;            }            else if (prevTeam == null) {                // Increment the number of times we've checked for "leftovers".                evenTracker[0]++;                // The first team to move just adds the "baseline" turns.                numEven += teamEvenTurns / min;            }            else if (!team.equals(prevTeam)) {                // Increment the number of times we've checked for "leftovers".                evenTracker[0]++;                // This wierd equation attempts to spread the "leftover"                // turns accross the turn's moves in a "fair" manner.                // It's based on the number of times we've checked for                // "leftovers" the number of "leftovers" we started with,                // the number of times we've added a turn for a "leftover",                // and the total number of times we're going to check.                numEven += Math.round (evenTracker[0] * (teamEvenTurns % min)                                       / min) - evenTracker[1];                // Update the number of turns actually added for "leftovers".                evenTracker[1] += numEven;                // Add the "baseline" number of turns.                numEven += teamEvenTurns / min;            }            // Record this team for the next move.            prevTeam = team;            // This may be a "placeholder" for a team without "normal" turns.            if (withinTeamTurns.hasMoreElements()) {                // Not a placeholder... get the player who moves next.                Player player = (Player) withinTeamTurns.nextElement();                // If we've added all "normal" turns, allocate turns                // for the infantry and/or protomechs moving even.                GameTurn turn = null;                if ( numTurn >= team_order.getNormalTurns() ) {                    turn = new GameTurn.EntityClassTurn                        (player.getId(), evenMask);                }                // If either Infantry or Protomechs move even, only allow                // the other classes to move during the "normal" turn.                else if ( infMoveEven || protosMoveEven ) {                    turn = new GameTurn.EntityClassTurn                        (player.getId(), ~evenMask);                }                // Otherwise, let *anybody* move.                else {                    turn = new GameTurn( player.getId() );                }                turns.addElement(turn);            } // End team-has-"normal"-turns            // Add the calculated number of "even" turns.            // Allow the player at least one "normal" turn before the            // "even" turns to help with loading infantry in deployment.            while (numEven > 0 && withinTeamTurns.hasMoreEvenElements()) {                Player evenPlayer = (Player) withinTeamTurns.nextEvenElement();                turns.addElement                    (new GameTurn.EntityClassTurn (evenPlayer.getId(),                                                   evenMask));                numEven--;            }        }        System.out.println ("All Done!"); //killme        // set fields in game        game.setTurnVector(turns);        game.resetTurnIndex();        // send turns to all players        send(createTurnVectorPacket());    }