Skip to content

Commit

Permalink
allow limiting Any deployment to a smaller square area
Browse files Browse the repository at this point in the history
  • Loading branch information
kuronekochomusuke committed Nov 24, 2023
1 parent c4cd96c commit cf4f166
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 6 deletions.
49 changes: 49 additions & 0 deletions megamek/src/megamek/client/ui/swing/CustomMechDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public class CustomMechDialog extends AbstractButtonDialog implements ActionList
private final JFormattedTextField txtDeploymentOffset = new JFormattedTextField(formatterFactory);
private final JFormattedTextField txtDeploymentWidth = new JFormattedTextField(formatterFactory);

private JSpinner spinStartingAnyNWx;
private JSpinner spinStartingAnyNWy;
private JSpinner spinStartingAnySEx;
private JSpinner spinStartingAnySEy;

private final JLabel labDeployShutdown = new JLabel(
Messages.getString("CustomMechDialog.labDeployShutdown"), SwingConstants.RIGHT);
private final JCheckBox chDeployShutdown = new JCheckBox();
Expand Down Expand Up @@ -473,6 +478,17 @@ private void refreshDeployment() {
txtDeploymentOffset.setText(Integer.toString(entity.getStartingOffset(false)));
txtDeploymentWidth.setText(Integer.toString(entity.getStartingWidth(false)));

int bh = clientgui.getClient().getMapSettings().getBoardHeight();
int bw = clientgui.getClient().getMapSettings().getBoardWidth();
int x = entity.getStartingAnyNWx() + 1 >= bw ? bw : entity.getStartingAnyNWx() + 1;
spinStartingAnyNWx.setValue(x);
int y = entity.getStartingAnyNWy() + 1 >= bh ? bh : entity.getStartingAnyNWy() + 1;
spinStartingAnyNWy.setValue(y);
x = entity.getStartingAnySEx() + 1 >= bw ? bw : entity.getStartingAnySEx() + 1;
spinStartingAnySEy.setValue(x);
y = entity.getStartingAnySEy() + 1 >= bh ? bh : entity.getStartingAnySEy() + 1;
spinStartingAnySEy.setValue(y);

boolean enableDeploymentZoneControls = choDeploymentZone.isEnabled() && (choDeploymentZone.getSelectedIndex() > 0);
txtDeploymentOffset.setEnabled(enableDeploymentZoneControls);
txtDeploymentWidth.setEnabled(enableDeploymentZoneControls);
Expand Down Expand Up @@ -881,6 +897,13 @@ protected void okAction() {
entity.setStartingOffset(Integer.parseInt(txtDeploymentOffset.getText()));
entity.setStartingWidth(Integer.parseInt(txtDeploymentWidth.getText()));

int x = (Integer) spinStartingAnyNWx.getValue() > (Integer) spinStartingAnySEx.getValue() ? (Integer) spinStartingAnySEx.getValue() : (Integer) spinStartingAnyNWx.getValue();
int y = (Integer) spinStartingAnyNWy.getValue() > (Integer) spinStartingAnySEy.getValue() ? (Integer) spinStartingAnySEy.getValue() : (Integer) spinStartingAnyNWy.getValue();
entity.setStartingAnyNWx(x - 1);
entity.setStartingAnyNWy(y - 1);
entity.setStartingAnySEx((Integer) spinStartingAnySEx.getValue() - 1);
entity.setStartingAnySEy((Integer) spinStartingAnySEy.getValue() - 1);

// Should the entity begin the game shutdown?
if (chDeployShutdown.isSelected() && gameOptions().booleanOption(OptionsConstants.RPG_BEGIN_SHUTDOWN)) {
entity.performManualShutdown();
Expand Down Expand Up @@ -1200,6 +1223,32 @@ protected Container createCenterPane() {
panDeploy.add(labDeploymentWidth, GBC.std());
panDeploy.add(txtDeploymentWidth, GBC.eol());

int bh = clientgui.getClient().getMapSettings().getBoardHeight();
int bw = clientgui.getClient().getMapSettings().getBoardWidth();

panDeploy.add(new JLabel("Deployment Any NW corner:"), GBC.std());
int x = entity.getStartingAnyNWx() + 1 >= bw ? bw : entity.getStartingAnyNWx() + 1;
SpinnerNumberModel mStartingAnyNWx = new SpinnerNumberModel(x, 0,bw, 1);
spinStartingAnyNWx = new JSpinner(mStartingAnyNWx);
spinStartingAnyNWx.setValue(x);
panDeploy.add(spinStartingAnyNWx, GBC.std());
int y = entity.getStartingAnyNWy() + 1 >= bh ? bh : entity.getStartingAnyNWy() + 1;
SpinnerNumberModel mStartingAnyNWy = new SpinnerNumberModel(y, 0, bh, 1);
spinStartingAnyNWy = new JSpinner(mStartingAnyNWy);
spinStartingAnyNWy.setValue(y);
panDeploy.add(spinStartingAnyNWy, GBC.eol());
panDeploy.add(new JLabel("Deployment Any SE corner:"), GBC.std());
x = entity.getStartingAnySEx() + 1 >= bw ? bw : entity.getStartingAnySEx() + 1;
SpinnerNumberModel mStartingAnySEx = new SpinnerNumberModel(x, 0, bw, 1);
spinStartingAnySEx = new JSpinner(mStartingAnySEx);
spinStartingAnySEx.setValue(x);
panDeploy.add(spinStartingAnySEx, GBC.std());
y = entity.getStartingAnySEy() + 1 >= bh ? bh : entity.getStartingAnySEy() + 1;
SpinnerNumberModel mStartingAnySEy = new SpinnerNumberModel(y, -0, bh, 1);
spinStartingAnySEy = new JSpinner(mStartingAnySEy);
spinStartingAnySEy.setValue(y);
panDeploy.add(spinStartingAnySEy, GBC.eol());

numFormatter.setMinimum(0);
numFormatter.setCommitsOnValidEdit(true);

Expand Down
16 changes: 16 additions & 0 deletions megamek/src/megamek/client/ui/swing/MapDimensionsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import megamek.client.ui.Messages;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.MapSettings;

import javax.swing.*;
Expand Down Expand Up @@ -281,6 +282,21 @@ private void apply() {
mapSettings.setBoardSize(boardWidth, boardHeight);
mapSettings.setMapSize(mapWidth, mapHeight);
clientGUI.getClient().sendMapDimensions(mapSettings);

for (Entity entity : clientGUI.getClient().getGame().getEntitiesVector()) {
if (entity.getStartingAnyNWx() >= boardWidth) {
entity.setStartingAnyNWx(boardWidth - 1);
}
if (entity.getStartingAnyNWy() >= boardHeight) {
entity.setStartingAnyNWy(boardHeight - 1);
}
if (entity.getStartingAnySEx() >= boardWidth) {
entity.setStartingAnySEx(boardWidth - 1);
}
if (entity.getStartingAnySEy() >= boardHeight) {
entity.setStartingAnySEy(boardHeight - 1);
}
}
}


Expand Down
4 changes: 4 additions & 0 deletions megamek/src/megamek/client/ui/swing/lobby/ChatLounge.java
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,10 @@ public void configPlayer() {
player.setStartingPos(startPos);
player.setStartOffset(psd.getStartOffset());
player.setStartWidth(psd.getStartWidth());
player.setStartingAnyNWx(psd.getStartingAnyNWx());
player.setStartingAnyNWy(psd.getStartingAnyNWy());
player.setStartingAnySEx(psd.getStartingAnySEx());
player.setStartingAnySEy(psd.getStartingAnySEy());
c.sendPlayerInfo();

// If the gameoption set_arty_player_homeedge is set, adjust the player's offboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ static String formatUnitFull(Entity entity, ChatLounge lobby, boolean forceView)
}
String msg_start = Messages.getString("ChatLounge.Start");
result.append(" " + msg_start + ":" + IStartingPositions.START_LOCATION_NAMES[sp]);
if (sp == 0) {
int NWx = entity.getStartingAnyNWx() + 1;
int NWy = entity.getStartingAnyNWy() + 1;
int SEx = entity.getStartingAnySEx() + 1;
int SEy = entity.getStartingAnySEy() + 1;
if ((NWx + NWy + SEx + SEy) > 0) {
result.append("(" + NWx + ", " + NWy + ")-(" + SEx + ", " + SEy + ")");
}
}
int so = entity.getStartingOffset(true);
int sw = entity.getStartingWidth(true);
if ((so != 0) || (sw != 3)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ public int getStartPos() {
return currentPlayerStartPos;
}

public int getStartingAnyNWx() {
return (Integer) spinStartingAnyNWx.getValue() - 1;
}

public int getStartingAnyNWy() {
return (Integer) spinStartingAnyNWy.getValue() - 1;
}

public int getStartingAnySEx() {
return (Integer) spinStartingAnySEx.getValue() - 1;
}

public int getStartingAnySEy() {
return (Integer) spinStartingAnySEy.getValue() - 1;
}

/**
* @return the current {@link SkillGenerationOptionsPanel}
*/
Expand Down Expand Up @@ -167,6 +183,10 @@ public String getEmail() {
private final DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory(numFormatter);
private final JFormattedTextField txtOffset = new JFormattedTextField(formatterFactory, 0);
private final JFormattedTextField txtWidth = new JFormattedTextField(formatterFactory, 3);
private JSpinner spinStartingAnyNWx;
private JSpinner spinStartingAnyNWy;
private JSpinner spinStartingAnySEx;
private JSpinner spinStartingAnySEy;

// Bot Settings Section
private final JButton butBotSettings = new JButton(Messages.getString("PlayerSettingsDialog.botSettings"));
Expand Down Expand Up @@ -248,7 +268,17 @@ private JPanel deploymentParametersPanel() {
result.add(txtOffset, GBC.eol());
result.add(lblWidth, GBC.std());
result.add(txtWidth, GBC.eol());


int bh = clientgui.getClient().getMapSettings().getBoardHeight();

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'int bh' is never read.
int bw = clientgui.getClient().getMapSettings().getBoardWidth();

Check notice

Code scanning / CodeQL

Unread local variable Note

Variable 'int bw' is never read.

result.add(new JLabel("Deployment Any NW corner:"), GBC.std());
result.add(spinStartingAnyNWx, GBC.std());
result.add(spinStartingAnyNWy, GBC.eol());
result.add(new JLabel("Deployment Any SE corner:"), GBC.std());
result.add(spinStartingAnySEx, GBC.std());
result.add(spinStartingAnySEy, GBC.eol());

return result;
}

Expand Down Expand Up @@ -309,7 +339,27 @@ private void setupValues() {
fldEmail.setText(player.getEmail());
txtWidth.setText(Integer.toString(player.getStartWidth()));
txtOffset.setText(Integer.toString(player.getStartOffset()));


int bh = clientgui.getClient().getMapSettings().getBoardHeight();
int bw = clientgui.getClient().getMapSettings().getBoardWidth();

SpinnerNumberModel mStartingAnyNWx = new SpinnerNumberModel(0, 0,bw, 1);
spinStartingAnyNWx = new JSpinner(mStartingAnyNWx);
SpinnerNumberModel mStartingAnyNWy = new SpinnerNumberModel(0, 0, bh, 1);
spinStartingAnyNWy = new JSpinner(mStartingAnyNWy);
SpinnerNumberModel mStartingAnySEx = new SpinnerNumberModel(0, 0, bw, 1);
spinStartingAnySEx = new JSpinner(mStartingAnySEx);
SpinnerNumberModel mStartingAnySEy = new SpinnerNumberModel(0, -0, bh, 1);
spinStartingAnySEy = new JSpinner(mStartingAnySEy);

int x = player.getStartingAnyNWx() + 1 >= bw ? bw : player.getStartingAnyNWx() + 1;
spinStartingAnyNWx.setValue(x);
int y = player.getStartingAnyNWy() + 1 >= bh ? bh : player.getStartingAnyNWy() + 1;
spinStartingAnyNWy.setValue(y);
x = player.getStartingAnySEx() + 1 >= bw ? bw : player.getStartingAnySEx() + 1;
spinStartingAnySEx.setValue(x);
y = player.getStartingAnySEy() + 1 >= bh ? bh : player.getStartingAnySEy() + 1;
spinStartingAnySEy.setValue(y);
}

private void setupStartGrid() {
Expand Down
9 changes: 9 additions & 0 deletions megamek/src/megamek/client/ui/swing/lobby/PlayerTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ public Component getTableCellRendererComponent(JTable table, Object value, boole
result.append(msg_start + ": " + Messages.getString("ChatLounge.Player0"));
} else if ((player.getStartingPos() >= 0) && (player.getStartingPos() <= IStartingPositions.START_LOCATION_NAMES.length)) {
result.append(msg_start + ": " + IStartingPositions.START_LOCATION_NAMES[player.getStartingPos()]);
if (player.getStartingPos() == 0) {
int NWx = player.getStartingAnyNWx() + 1;
int NWy = player.getStartingAnyNWy() + 1;
int SEx = player.getStartingAnySEx() + 1;
int SEy = player.getStartingAnySEy() + 1;
if ((NWx + NWy + SEx + SEy) > 0) {
result.append("(" + NWx + ", " + NWy + ")-(" + SEx + ", " + SEy + ")");
}
}
int so = player.getStartOffset();
int sw = player.getStartWidth();
if ((so != 0) || (sw != 3)) {
Expand Down
11 changes: 7 additions & 4 deletions megamek/src/megamek/common/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ public static boolean isValid(String board) {
* Can the given player deploy at these coordinates?
*/
public boolean isLegalDeployment(Coords c, Player p) {
return isLegalDeployment(c, p.getStartingPos(), p.getStartWidth(), p.getStartOffset());
return isLegalDeployment(c, p.getStartingPos(), p.getStartWidth(), p.getStartOffset(), p.getStartingAnyNWx(), p.getStartingAnyNWy(), p.getStartingAnySEx(), p.getStartingAnySEy());
}

/**
Expand All @@ -857,13 +857,13 @@ public boolean isLegalDeployment(Coords c, Entity e) {
return false;
}

return isLegalDeployment(c, e.getStartingPos(), e.getStartingWidth(), e.getStartingOffset());
return isLegalDeployment(c, e.getStartingPos(), e.getStartingWidth(), e.getStartingOffset(), e.getStartingAnyNWx(), e.getStartingAnyNWy(), e.getStartingAnySEx(), e.getStartingAnySEy());
}

/**
* Can an object be deployed at these coordinates, given a starting zone, width of starting zone and offset from edge of board?
*/
public boolean isLegalDeployment(Coords c, int zoneType, int startingWidth, int startingOffset) {
public boolean isLegalDeployment(Coords c, int zoneType, int startingWidth, int startingOffset, int startingAnyNWx, int startingAnyNWy, int startingAnySEx, int startingAnySEy) {
if ((c == null) || !contains(c)) {
return false;
}
Expand All @@ -876,7 +876,10 @@ public boolean isLegalDeployment(Coords c, int zoneType, int startingWidth, int

switch (zoneType) {
case START_ANY:
return true;
return (((startingAnyNWx == -1) || (c.getX() >= startingAnyNWx))
&& ((startingAnySEx == -1) || (c.getX() <= startingAnySEx))
&& ((startingAnyNWy == -1) || (c.getY() >= startingAnyNWy))
&& ((startingAnySEy == -1) || (c.getY() <= startingAnySEy)));
case START_NW:
return ((c.getX() < (minx + nLimit)) && (c.getX() >= minx) && (c.getY() >= miny) && (c.getY() < (height / 2)))
|| ((c.getY() < (miny + nLimit)) && (c.getY() >= miny) && (c.getX() >= minx) && (c.getX() < (width / 2)));
Expand Down
103 changes: 103 additions & 0 deletions megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ public abstract class Entity extends TurnOrdered implements Transporter, Targeta
private int startingOffset = 0;
private int startingWidth = 3;

private int startingAnyNWx = -1;
private int startingAnyNWy = -1;
private int startingAnySEx = -1;
private int startingAnySEy = -1;

/**
* The pilot of the entity. Even infantry has a 'pilot'.
*/
Expand Down Expand Up @@ -15334,10 +15339,108 @@ public void setStartingWidth(int startingWidth) {
this.startingWidth = startingWidth;
}

public int getStartingAnyNWx() {
return getStartingAnyNWx(true);
}

public int getStartingAnyNWx(boolean inheritFromOwner) {
// if we are given permission to use the owner's settings
// and have specified entity-specific settings, use the owner's settings
if (inheritFromOwner && (startingPos == Board.START_NONE)) {
final GameOptions gOpts = getGame().getOptions();
if (!getOwner().isBot() && gOpts.booleanOption(OptionsConstants.BASE_SET_PLAYER_DEPLOYMENT_TO_PLAYER0)) {
return game.getPlayer(0).getStartingAnyNWx();
}
else {
return getOwner().getStartingAnyNWx();
}
}

return startingAnyNWx;
}

public void setStartingAnyNWx(int i) {
this.startingAnyNWx = i;
}

public int getStartingAnyNWy() {
return getStartingAnyNWy(true);
}

public int getStartingAnyNWy(boolean inheritFromOwner) {
// if we are given permission to use the owner's settings
// and have specified entity-specific settings, use the owner's settings
if (inheritFromOwner && (startingPos == Board.START_NONE)) {
final GameOptions gOpts = getGame().getOptions();
if (!getOwner().isBot() && gOpts.booleanOption(OptionsConstants.BASE_SET_PLAYER_DEPLOYMENT_TO_PLAYER0)) {
return game.getPlayer(0).getStartingAnyNWy();
}
else {
return getOwner().getStartingAnyNWy();
}
}

return startingAnyNWy;
}

public void setStartingAnyNWy(int i) {
this.startingAnyNWy = i;
}

public int getStartingAnySEx() {
return getStartingAnySEx(true);
}

public int getStartingAnySEx(boolean inheritFromOwner) {
// if we are given permission to use the owner's settings
// and have specified entity-specific settings, use the owner's settings
if (inheritFromOwner && (startingPos == Board.START_NONE)) {
final GameOptions gOpts = getGame().getOptions();
if (!getOwner().isBot() && gOpts.booleanOption(OptionsConstants.BASE_SET_PLAYER_DEPLOYMENT_TO_PLAYER0)) {
return game.getPlayer(0).getStartingAnySEx();
}
else {
return getOwner().getStartingAnySEx();
}
}

return startingAnySEx;
}

public void setStartingAnySEx(int i) {
this.startingAnySEx = i;
}

public int getStartingAnySEy() {
return getStartingAnySEy(true);
}

public int getStartingAnySEy(boolean inheritFromOwner) {
// if we are given permission to use the owner's settings
// and have specified entity-specific settings, use the owner's settings
if (inheritFromOwner && (startingPos == Board.START_NONE)) {
final GameOptions gOpts = getGame().getOptions();
if (!getOwner().isBot() && gOpts.booleanOption(OptionsConstants.BASE_SET_PLAYER_DEPLOYMENT_TO_PLAYER0)) {
return game.getPlayer(0).getStartingAnySEy();
}
else {
return getOwner().getStartingAnySEy();
}
}

return startingAnySEy;
}

public void setStartingAnySEy(int i) {
this.startingAnySEy = i;
}

public int getBloodStalkerTarget() {
return bloodStalkerTarget;
}



/**
* Whether this entity can activate the "blood stalker" ability
*/
Expand Down
Loading

0 comments on commit cf4f166

Please sign in to comment.