Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IGame in TurnOrdered #5170

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions megamek/src/megamek/common/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,6 @@ public void setOptions(final @Nullable GameOptions options) {
}
}

/**
* @return a player's team, which may be null if they do not have a team
*/
public @Nullable Team getTeamForPlayer(Player p) {
for (Team team : teams) {
for (Player player : team.players()) {
if (player.equals(p)) {
return team;
}
}
}
return null;
}

/**
* Set up the teams vector. Each player on a team (Team 1 .. Team X) is
* placed in the appropriate vector. Any player on 'No Team', is placed in
Expand Down
9 changes: 9 additions & 0 deletions megamek/src/megamek/common/IGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public interface IGame {

void setupTeams();

@Nullable default Team getTeamForPlayer(Player player) {
for (Team team : getTeams()) {
if (team.hasPlayer(player)) {
return team;
}
}
return null;
}

// UNITS //////////////

/** @return The next free id for InGameObjects (units and others). */
Expand Down
4 changes: 2 additions & 2 deletions megamek/src/megamek/common/ITurnOrdered.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public interface ITurnOrdered extends Serializable {
* @return the <code>int</code> number of "normal" turns this item should
* take in a phase.
*/
int getNormalTurns(Game game);
int getNormalTurns(IGame game);

int getOtherTurns();

int getEvenTurns();

int getMultiTurns(Game game);
int getMultiTurns(IGame game);

int getSpaceStationTurns();

Expand Down
8 changes: 6 additions & 2 deletions megamek/src/megamek/common/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public boolean isEmpty() {
return players.isEmpty();
}

public boolean hasPlayer(Player player) {
return players.contains(player);
}

public List<Player> players() {
return new ArrayList<>(players);
}
Expand Down Expand Up @@ -133,7 +137,7 @@ public int getId() {
* take in a phase.
*/
@Override
public int getNormalTurns(Game game) {
public int getNormalTurns(IGame game) {
int normalTurns = getMultiTurns(game) + getOtherTurns();
return (normalTurns == 0) ? getEvenTurns() : normalTurns;
}
Expand All @@ -149,7 +153,7 @@ public int getOtherTurns() {
}

@Override
public int getMultiTurns(Game game) {
public int getMultiTurns(IGame game) {
return players.stream().mapToInt(p -> p.getMultiTurns(game)).sum();
}

Expand Down
17 changes: 7 additions & 10 deletions megamek/src/megamek/common/TurnOrdered.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class TurnOrdered implements ITurnOrdered {
* take in a phase.
*/
@Override
public int getNormalTurns(Game game) {
public int getNormalTurns(IGame game) {
return getMultiTurns(game) + getOtherTurns();
}

Expand All @@ -63,7 +63,7 @@ public int getEvenTurns() {
}

@Override
public int getMultiTurns(Game game) {
public int getMultiTurns(IGame game) {
int turns = 0;
// turns_multi is transient, so it could be null
if (turns_multi == null) {
Expand Down Expand Up @@ -312,13 +312,10 @@ public static void resetInitiativeCompensation(List<? extends ITurnOrdered> v,
for (ITurnOrdered item : v) {
if (!item.equals(winningElement)) {
int newBonus = 0;
boolean observer = false;
boolean observer = ((item instanceof Player) && ((Player) item).isObserver())
|| ((item instanceof Team) && ((Team) item).isObserverTeam());
// Observers don't have initiative, and they don't get initiative compensation
if (((item instanceof Player) && ((Player) item).isObserver())
|| ((item instanceof Team) && ((Team) item).isObserverTeam())) {
observer = true;
}


if (!item.equals(lastRoundInitWinner) && !observer) {
newBonus = item.getInitCompensationBonus() + 1;
}
Expand All @@ -340,7 +337,7 @@ public static void resetInitiativeCompensation(List<? extends ITurnOrdered> v,
*
* @param v
* A vector of items that need to have turns.
* @param rerollRequests
* @param rerollRequests null when there should be no re-rolls
* @param bInitCompBonus
* A flag that determines whether initiative compensation bonus
* should be used: used to prevent one side getting long init win
Expand Down Expand Up @@ -411,7 +408,7 @@ public static void rollInitAndResolveTies(List<? extends ITurnOrdered> v,
/**
* This takes a Vector of TurnOrdered and generates a TurnVector.
*/
public static TurnVectors generateTurnOrder(List<? extends ITurnOrdered> v, Game game) {
public static TurnVectors generateTurnOrder(List<? extends ITurnOrdered> v, IGame game) {
int[] num_even_turns = new int[v.size()];
int[] num_normal_turns = new int[v.size()];
int[] num_space_station_turns = new int[v.size()];
Expand Down
Loading