Skip to content

Commit

Permalink
fix: improved the documentation, reduced duplicated code, added bette…
Browse files Browse the repository at this point in the history
…r preset for auto resolve
  • Loading branch information
Scoppio committed Oct 31, 2024
1 parent 1976015 commit bc0163c
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 547 deletions.
54 changes: 54 additions & 0 deletions MekHQ/docs/help/en/AutoResolve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Auto Resolve</title>
</head>
<body>
<div>
<h1 id="auto-resolve-behavior-settings">Auto Resolve Behavior Settings</h1>
<h2 id="introduction">Introduction</h2>
<p>The Auto Resolve feature is a way to let the PrincessBot control your units so she can quickly play out the battles
for you, its pretty straight forward to setup and use if you want to focus on your company management or just dont
want to play every single lengthy battle.</p>
<h2 id="how-to-use">How to Use</h2>
<p>Auto Resolve is available as a new option in the Scenario panel, the button &quot;Auto Resolve&quot; will setup the game scenario
on MegaMek as normal, and then it adds a Princess bot with the name following the format <YourCompany>:AI, it is setup
as the player is setup, same starting position, color, camouflage and team, and then all the players units change owner
to it.</p>
<p>To make the game faster, you can change configuration to not show the reports between phases, and select the skip
action if no available unit, with those options selected the game will not request you to press Done between those
phases.</p>
<h2 id="configuring-the-auto-resolve-behavior">Configuring the Auto Resolve Behavior</h2>
<p>The configuration is accessible in the <strong>Manage Campaign</strong> menu, in the MekHQ toolbar, there is the entry
<strong>Auto Resolve Behavior Settings</strong>, it will open the <strong>Configure Princess Bot</strong> window, where you can define the
behavior for the auto resolve preset.</p>
<p>By default, every campaign now have an auto-resolve default preset, that is named <YourCompany>:AI, you can change the
configuration as you like and select whatever preset you want.</p>
<p>Whenever you hit the &quot;OK&quot; button the current configuration being shown overwrites the auto resolve preset
for your company, so if you want to experiment with different behaviors, I suggest that you create new presets and then
just select the one you want before hitting &quot;OK&quot; to close the configuration.</p>
<h2 id="qa">Q&amp;A</h2>
<p><strong>Q: Can I change the configuration during the game?</strong></p>
<p>Sure, its a Princess Bot, so all chat commands to change behavior apply</p>
<p><strong>Q: Can I change the configuration for a specific scenario?</strong></p>
<p>Also yes, you can create many presets, like for example one for a scape scenario, another for a defend scenario, etc.
Just remember to change the selected preset behavior before entering the game with auto resolve. And remember to change
it back later to your &quot;default&quot; preset.</p>
<p><strong>Q: Can I change the configuration for a specific unit?</strong></p>
<p>No, you can&#39;t, you can emulate that by manually creating many bots with different configurations and then assigning
individual units or lances to them, but that is outside the current scope of the Auto Resolve.</p>
<p><strong>Q: I deleted my company preset! Is everything lost?</strong></p>
<p>You lost the configuration, sure, but MekHQ won&#39;t make any fuss about it, if you delete it by mistake inside MegaMek,
once you open the <strong>Auto Resolve Behavior Settings</strong> again, the default preset will be recreated for you. And if it is
missing before you enter an auto resolve game, it will use the default behavior for the Princess Bot instead.</p>
<p><strong>Q: I want to share my preset with my friends, how can I do that?</strong></p>
<p>It&#39;s just a preset, so you can use the same way you used to share it with your friends before.</p>
<p><strong>Q: The preset is written to the campaign save?</strong></p>
<p>No, the preset is saved in the MekHQ configuration folder, so it&#39;s available for all your campaigns, not just the one
that created it. So... if you create two different campaigns with the same name they would use the same preset.
Also means that if you change the preset in one save file, the preset is the same in your other campaign. Remember, it
is a preset that you are telling MekHQ to use, not a campaign specific configuration.</p>
</div>
</body>
</html>
31 changes: 21 additions & 10 deletions MekHQ/src/mekhq/AtBGameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import megamek.client.generator.RandomCallsignGenerator;
import megamek.client.ui.swing.ClientGUI;
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.logging.MMLogger;
import mekhq.campaign.force.Force;
Expand Down Expand Up @@ -56,18 +57,29 @@ public class AtBGameThread extends GameThread {
private final AtBScenario scenario;
private final BehaviorSettings autoResolveBehaviorSettings;

/**
* Constructor for AtBGameThread
*
* <p>
* This constructor creates a new AtBGameThread with the given name, password, client, MekHQ application, list of
* units, scenario, and auto resolve behavior settings. The game thread is started by default.
* </p>
*
* @param name The name of the player
* @param password The password for the game
* @param c The client
* @param app The MekHQ application
* @param units The list of units to import into the game
* @param scenario The scenario to use for this game
* @param autoResolveBehaviorSettings The behavior settings for the auto resolve bot
*/
public AtBGameThread(String name, String password, Client c, MekHQ app, List<Unit> units,
AtBScenario scenario) {
this(name, password, c, app, units, scenario, null, true);
}

public AtBGameThread(String name, String password, Client c, MekHQ app, List<Unit> units,
AtBScenario scenario, BehaviorSettings autoResolveBehaviorSettings) {
AtBScenario scenario, @Nullable BehaviorSettings autoResolveBehaviorSettings) {
this(name, password, c, app, units, scenario, autoResolveBehaviorSettings, true);
}

public AtBGameThread(String name, String password, Client c, MekHQ app, List<Unit> units,
AtBScenario scenario, BehaviorSettings autoResolveBehaviorSettings, boolean started) {
AtBScenario scenario, @Nullable BehaviorSettings autoResolveBehaviorSettings, boolean started) {
super(name, password, c, app, units, scenario, started);
this.scenario = Objects.requireNonNull(scenario);
this.autoResolveBehaviorSettings = autoResolveBehaviorSettings;
Expand Down Expand Up @@ -498,12 +510,11 @@ private void setupPlayerBotForAutoResolve(Player player) throws InterruptedExcep
botClient.sendPlayerInfo();
Thread.sleep(MekHQ.getMHQOptions().getStartGameBotClientDelay());

var ent = client.getEntitiesVector().stream()
var playerEntities = client.getEntitiesVector().stream()
.filter(entity -> entity.getOwnerId() == player.getId())
.collect(Collectors.toList());
botClient.sendChangeOwner(ent, botClient.getLocalPlayer().getId());
botClient.sendChangeOwner(playerEntities, botClient.getLocalPlayer().getId());
Thread.sleep(MekHQ.getMHQOptions().getStartGameBotClientDelay());

}

private PlanetaryConditions getPlanetaryConditions() {
Expand Down
20 changes: 19 additions & 1 deletion MekHQ/src/mekhq/MekHQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import megamek.client.ui.swing.gameConnectionDialogs.ConnectDialog;
import megamek.client.ui.swing.gameConnectionDialogs.HostDialog;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.annotations.Nullable;
import megamek.common.event.*;
import megamek.common.net.marshalling.SanityInputFilter;
import megamek.logging.MMLogger;
Expand Down Expand Up @@ -374,11 +375,28 @@ public void joinGame(Scenario scenario, List<Unit> meks) {
gameThread.start();
}

/**
* Start hosting a game.
* This method is used to start hosting a game. It will create a new server and a client and connect to it.
*
* @param scenario The scenario to host
* @param loadSavegame Whether to load a savegame
* @param meks The units you want to use in the scenario
*/
public void startHost(Scenario scenario, boolean loadSavegame, List<Unit> meks) {
startHost(scenario, loadSavegame, meks, null);
}

public void startHost(Scenario scenario, boolean loadSavegame, List<Unit> meks, BehaviorSettings autoResolveBehaviorSettings)
/**
* Start hosting a game.
* This method is used to start hosting a game. It will create a new server and a client and connect to it.
*
* @param scenario The scenario to host
* @param loadSavegame Whether to load a savegame
* @param meks The units you want to use in the scenario
* @param autoResolveBehaviorSettings The auto resolve behavior settings to use if running an AtB scenario and auto resolve is wanted
*/
public void startHost(Scenario scenario, boolean loadSavegame, List<Unit> meks, @Nullable BehaviorSettings autoResolveBehaviorSettings)
{
HostDialog hostDialog = new HostDialog(campaignGUI.getFrame(), getCampaign().getName());
hostDialog.setVisible(true);
Expand Down
3 changes: 2 additions & 1 deletion MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
import java.util.*;
import java.util.Map.Entry;

import static mekhq.utilities.MoreObjects.firstNonNull;
import static org.apache.commons.lang3.ObjectUtils.firstNonNull;


public class CampaignXmlParser {
private final InputStream is;
Expand Down
Loading

0 comments on commit bc0163c

Please sign in to comment.