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

Move Field of Fire Sprites out of BoardView #5466

Merged
merged 17 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 23 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
24 changes: 12 additions & 12 deletions megamek/src/megamek/client/AbstractClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected void disconnected() {
}

if (!host.equals(MMConstants.LOCALHOST)) {
getIGame().fireGameEvent(new GamePlayerDisconnectedEvent(this, getLocalPlayer()));
getGame().fireGameEvent(new GamePlayerDisconnectedEvent(this, getLocalPlayer()));
}
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public void sendNextPlayer() {
* Sends the info associated with the local player.
*/
public void sendPlayerInfo() {
send(new Packet(PacketCommand.PLAYER_UPDATE, getIGame().getPlayer(localPlayerNumber)));
send(new Packet(PacketCommand.PLAYER_UPDATE, getGame().getPlayer(localPlayerNumber)));
}

/**
Expand Down Expand Up @@ -217,9 +217,9 @@ protected void receivePlayerInfo(Packet c) {
int pindex = c.getIntValue(0);
Player newPlayer = (Player) c.getObject(1);
if (!playerExists(newPlayer.getId())) {
getIGame().addPlayer(pindex, newPlayer);
getGame().addPlayer(pindex, newPlayer);
} else {
getIGame().setPlayer(pindex, newPlayer);
getGame().setPlayer(pindex, newPlayer);
}
}

Expand Down Expand Up @@ -301,11 +301,11 @@ protected synchronized void checkDuplicateNamesDuringAdd(Entity entity) {
protected void receiveUnitReplace(Packet packet) {
@SuppressWarnings(value = "unchecked")
List<Force> forces = (List<Force>) packet.getObject(1);
forces.forEach(force -> getIGame().getForces().replace(force.getId(), force));
forces.forEach(force -> getGame().getForces().replace(force.getId(), force));

@SuppressWarnings(value = "unchecked")
List<InGameObject> units = (List<InGameObject>) packet.getObject(0);
getIGame().replaceUnits(units);
getGame().replaceUnits(units);
}

/**
Expand Down Expand Up @@ -441,25 +441,25 @@ protected boolean handleGameIndependentPacket(Packet packet) {
Player player = getPlayer(packet.getIntValue(0));
if (player != null) {
player.setDone(packet.getBooleanValue(1));
getIGame().fireGameEvent(new GamePlayerChangeEvent(player, player));
getGame().fireGameEvent(new GamePlayerChangeEvent(player, player));
}
break;
case PLAYER_REMOVE:
bots.values().removeIf(bot -> bot.localPlayerNumber == packet.getIntValue(0));
getIGame().removePlayer(packet.getIntValue(0));
getGame().removePlayer(packet.getIntValue(0));
break;
case CHAT:
possiblyWriteToLog((String) packet.getObject(0));
getIGame().fireGameEvent(new GamePlayerChatEvent(this, null, (String) packet.getObject(0)));
getGame().fireGameEvent(new GamePlayerChatEvent(this, null, (String) packet.getObject(0)));
break;
case ENTITY_ADD:
receiveUnitReplace(packet);
break;
case SENDING_BOARD:
getIGame().receiveBoards((Map<Integer, Board>) packet.getObject(0));
getGame().receiveBoards((Map<Integer, Board>) packet.getObject(0));
break;
case ROUND_UPDATE:
getIGame().setCurrentRound(packet.getIntValue(0));
getGame().setCurrentRound(packet.getIntValue(0));
break;
case PHASE_CHANGE:
changePhase((GamePhase) packet.getObject(0));
Expand All @@ -485,7 +485,7 @@ protected void possiblyWriteToLog(String message) {
* Changes the game phase, and the displays that go along with it.
*/
public void changePhase(GamePhase phase) {
getIGame().receivePhase(phase);
getGame().receivePhase(phase);
switch (phase) {
case STARTING_SCENARIO:
case EXCHANGE:
Expand Down
23 changes: 8 additions & 15 deletions megamek/src/megamek/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import megamek.common.options.OptionsConstants;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.preference.PreferenceManager;
import megamek.common.Report;
import megamek.common.util.ImageUtil;
import megamek.common.util.SerializationHelper;
import megamek.common.util.StringUtil;
Expand Down Expand Up @@ -98,16 +99,10 @@
}
}

@Override
public IGame getIGame() {
return game;
}

public Game getGame() {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
IClient.getGame
; it is advisable to add an Override annotation.
return game;
}


/**
* Get hexes designated for automatic artillery hits.
*/
Expand Down Expand Up @@ -204,9 +199,7 @@
}
}

/**
* is it my turn?
*/
@Override
public boolean isMyTurn() {
if (getGame().getPhase().isSimultaneous(getGame())) {
return game.getTurnForPlayer(localPlayerNumber) != null;
Expand Down Expand Up @@ -701,7 +694,7 @@


// Should be private?
public String receiveReport(Vector<Report> v) {
public String receiveReport(List<Report> v) {
Sleet01 marked this conversation as resolved.
Show resolved Hide resolved
if (v == null) {
return "[null report vector]";
}
Expand Down Expand Up @@ -948,7 +941,7 @@
break;
case SENDING_REPORTS:
case SENDING_REPORTS_TACTICAL_GENIUS:
phaseReport = receiveReport((Vector<Report>) packet.getObject(0));
phaseReport = receiveReport((List<Report>) packet.getObject(0));
if (keepGameLog()) {
if ((log == null) && (game.getRoundCount() == 1)) {
initGameLog();
Expand All @@ -957,25 +950,25 @@
log.append(phaseReport);
}
}
game.addReports((Vector<Report>) packet.getObject(0));
game.addReports((List<Report>) packet.getObject(0));
roundReport = receiveReport(game.getReports(game.getRoundCount()));
if (packet.getCommand().isSendingReportsTacticalGenius()) {
game.processGameEvent(new GameReportEvent(this, roundReport));
}
break;
case SENDING_REPORTS_SPECIAL:
game.processGameEvent(new GameReportEvent(this,
receiveReport((Vector<Report>) packet.getObject(0))));
receiveReport((List<Report>) packet.getObject(0))));
break;
case SENDING_REPORTS_ALL:
Vector<Vector<Report>> allReports = (Vector<Vector<Report>>) packet.getObject(0);
var allReports = (List<List<Report>>) packet.getObject(0);
game.setAllReports(allReports);
if (keepGameLog()) {
// Re-write gamelog.txt from scratch
initGameLog();
if (log != null) {
for (int i = 0; i < allReports.size(); i++) {
log.append(receiveReport(allReports.elementAt(i)));
log.append(receiveReport(allReports.get(i)));
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions megamek/src/megamek/client/IClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,31 @@ public interface IClient {
*
* @return The game of this client
*/
IGame getIGame();
IGame getGame();

/** @return The in-game object associated with the given ID. */
default Optional<InGameObject> getInGameObject(int id) {
return getIGame().getInGameObject(id);
return getGame().getInGameObject(id);
}

/** @return A list of all in-game objects of the game. This list is copied and may be safely modified. */
default List<InGameObject> getInGameObjects() {
return getIGame().getInGameObjects();
return getGame().getInGameObjects();
}

/** @return The player with the given ID, or null if there is no such player. */
default Player getPlayer(int id) {
return getIGame().getPlayer(id);
return getGame().getPlayer(id);
}

/** @return The ID of the player playing at this Client. */
int getLocalPlayerNumber();

/**
* @return True when the currently active turn is a turn for the local player
*/
boolean isMyTurn();

/**
* Sets the ID of the player playing at this Client.
* // TODO : only used by AddBotUtil -> could be included in a bot's constructor and removed here
Expand Down
65 changes: 47 additions & 18 deletions megamek/src/megamek/client/SBFClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,14 @@
*/
package megamek.client;

import megamek.MMConstants;
import megamek.client.bot.princess.BehaviorSettings;
import megamek.client.bot.princess.Princess;
import megamek.common.*;
import megamek.common.actions.ArtilleryAttackAction;
import megamek.common.actions.WeaponAttackAction;
import megamek.common.enums.GamePhase;
import megamek.common.event.*;
import megamek.common.net.enums.PacketCommand;
import megamek.common.net.packets.Packet;
import megamek.common.options.GameOptions;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.strategicBattleSystems.SBFGame;
import megamek.server.SmokeCloud;
import org.apache.logging.log4j.LogManager;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

/**
* This is the game client for Strategic BattleForce games, as one would think.
Expand Down Expand Up @@ -66,12 +53,54 @@ public SBFClient(String name, String host, int port) {
}

@Override
public IGame getIGame() {
public SBFGame getGame() {
return game;
}

@Override
protected boolean handleGameSpecificPacket(Packet packet) throws Exception {
return false;
public boolean isMyTurn() {
return (game.getTurn() != null) && game.getTurn().isValid(localPlayerNumber, game);
}

@Override
@SuppressWarnings("unchecked")
protected boolean handleGameSpecificPacket(Packet packet) {
LogManager.getLogger().info("Received packet: {}", packet);
switch (packet.getCommand()) {
case SENDING_REPORTS_ALL:
var receivedReports = (Map<Integer, List<Report>>) packet.getObject(0);
game.replaceAllReports(receivedReports);
if (keepGameLog()) {
// Re-write the gamelog from scratch
initGameLog();
for (int round : receivedReports.keySet().stream().sorted().collect(Collectors.toList())) {
possiblyWriteToLog(assembleAndAddImages(receivedReports.get(round)));
}
}
roundReport = assembleAndAddImages(receivedReports.get(game.getCurrentRound()));
// We don't really have a copy of the phase report at this point, so I guess we'll just use the
// round report until the next phase actually completes.
phaseReport = roundReport;
break;
default:
return false;
}
return true;
}

private String assembleAndAddImages(List<Report> reports) {
if (reports == null) {
LogManager.getLogger().error("Received a null list of reports!");
return "";
}

StringBuilder assembledReport = new StringBuilder();
for (Report report : reports) {
if (report != null) {
assembledReport.append(report.getText());
}
}

return assembledReport.toString();
}
}
3 changes: 2 additions & 1 deletion megamek/src/megamek/client/bot/BotClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import megamek.common.options.OptionsConstants;
import megamek.common.pathfinder.BoardClusterTracker;
import megamek.common.preference.PreferenceManager;
import megamek.common.Report;
import megamek.common.util.BoardUtilities;
import megamek.common.util.StringUtil;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -1187,7 +1188,7 @@ protected void receiveBuildingCollapse(Packet packet) {
* Let's save ourselves a little processing time and not deal with any of it
*/
@Override
public String receiveReport(Vector<Report> v) {
public String receiveReport(List<Report> v) {
return "";
}

Expand Down
5 changes: 3 additions & 2 deletions megamek/src/megamek/client/bot/princess/Precognition.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import megamek.common.net.enums.PacketCommand;
import megamek.common.options.GameOptions;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.Report;
import megamek.server.SmokeCloud;
import org.apache.logging.log4j.LogManager;

Expand Down Expand Up @@ -202,10 +203,10 @@ void handlePacket(Packet c) {
break;
case SENDING_REPORTS:
case SENDING_REPORTS_TACTICAL_GENIUS:
getGame().addReports((Vector<Report>) c.getObject(0));
getGame().addReports((List<Report>) c.getObject(0));
break;
case SENDING_REPORTS_ALL:
Vector<Vector<Report>> allReports = (Vector<Vector<Report>>) c.getObject(0);
var allReports = (List<List<Report>>) c.getObject(0);
getGame().setAllReports(allReports);
break;
case ENTITY_ATTACK:
Expand Down
5 changes: 0 additions & 5 deletions megamek/src/megamek/client/bot/princess/Princess.java
Original file line number Diff line number Diff line change
Expand Up @@ -1610,11 +1610,6 @@ protected void initMovement() {
}
}

@Override
public Game getGame() {
return game;
}

@Override
public void initialize() {
try {
Expand Down
Loading
Loading