Skip to content

Commit

Permalink
Issue 3650: Moved Potential Transports in ATB thread to new class
Browse files Browse the repository at this point in the history
  • Loading branch information
psikomonkie committed Jan 13, 2025
1 parent 9c464cd commit ecf29e4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 27 deletions.
43 changes: 16 additions & 27 deletions MekHQ/src/mekhq/AtBGameThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
import megamek.common.annotations.Nullable;
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.logging.MMLogger;
import mekhq.campaign.enums.CampaignTransportType;
import mekhq.campaign.force.Force;
import mekhq.campaign.mission.*;
import mekhq.campaign.personnel.Person;
import mekhq.campaign.unit.ITransportAssignment;
import mekhq.campaign.unit.Unit;
import mekhq.utilities.MHQInternationalization;
import mekhq.utilities.PotentialTransportsMap;

import javax.swing.*;
import java.io.File;
Expand Down Expand Up @@ -239,9 +241,7 @@ public void run() {
}
}

Map<Class, Map<UUID, List<UUID>>> potentialTransports = new HashMap<>();
potentialTransports.put(SHIP_TRANSPORT.getTransportAssignmentType(), new HashMap<>());
potentialTransports.put(TACTICAL_TRANSPORT.getTransportAssignmentType(), new HashMap<>());
PotentialTransportsMap potentialTransports = new PotentialTransportsMap(CampaignTransportType.values());

var entities = new ArrayList<Entity>();
for (Unit unit : units) {
Expand All @@ -253,10 +253,10 @@ public void run() {
entity.setOwner(client.getLocalPlayer());
if (unit.hasShipTransportedUnits()) {
// Store this unit as a potential transport to load
potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).put(unit.getId(), new ArrayList<>());
potentialTransports.putNewTransport(SHIP_TRANSPORT, unit.getId());
}
if (unit.hasTacticalTransportedUnits()) {
potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).put(unit.getId(), new ArrayList<>());
potentialTransports.putNewTransport(TACTICAL_TRANSPORT, unit.getId());
}
// If this unit is a spacecraft, set the crew size and marine size values
if (entity.isLargeCraft() || (entity.getUnitType() == UnitType.SMALL_CRAFT)) {
Expand Down Expand Up @@ -314,34 +314,23 @@ public void run() {
if (unit.hasTransportShipAssignment()) {
Unit transportShip = unit.getTransportShipAssignment().getTransportShip();

if (potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).containsKey(transportShip.getId())) {
potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).get(transportShip.getId()).add(unit.getId());
if (potentialTransports.containsTransportKey(SHIP_TRANSPORT, transportShip.getId())) {
potentialTransports.addTransportedUnit(SHIP_TRANSPORT, transportShip.getId(), unit.getId());
isTransported = true;
}
}
if (!(isTransported) && unit.hasTacticalTransportAssignment()) {
Unit transport = unit.getTacticalTransportAssignment().getTransport();

if (potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).containsKey(transport.getId())) {
potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).get(transport.getId()).add(unit.getId());
if (potentialTransports.containsTransportKey(TACTICAL_TRANSPORT, transport.getId())) {
potentialTransports.addTransportedUnit(TACTICAL_TRANSPORT, transport.getId(), unit.getId());
isTransported = true;
}
}
}
// Now, clean the list of any transports that don't have deployed units in the
// game

for(Map<UUID, List<UUID>> campaignTransportTypes : potentialTransports.values() ) {
Set<UUID> emptyTransports = new HashSet<>();
for (UUID id : campaignTransportTypes.keySet()) {
if (campaignTransportTypes.get(id).isEmpty()) {
emptyTransports.add(id);
}
}
for (UUID id : emptyTransports) {
campaignTransportTypes.remove(id);
}
}
potentialTransports.removeEmptyTransports();

/* Add player-controlled ally units */
entities.clear();
Expand Down Expand Up @@ -414,8 +403,8 @@ public void run() {

// All player and bot units have been added to the lobby
// Prompt the player to autoload units into transport ships
if (!potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).isEmpty()) {
for (UUID transportId : potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).keySet()) {
if (!potentialTransports.getTransports(SHIP_TRANSPORT).isEmpty()) {
for (UUID transportId : potentialTransports.getTransports(SHIP_TRANSPORT)) {
boolean loadDropShips = false;
boolean loadSmallCraft = false;
boolean loadFighters = false;
Expand Down Expand Up @@ -456,7 +445,7 @@ public void run() {
// List of technicians assigned to transported units. Several units can share a
// tech.
Set<Person> cargoTechs = new HashSet<>();
for (UUID cargoId : potentialTransports.get(SHIP_TRANSPORT.getTransportAssignmentType()).get(transportId)) {
for (UUID cargoId : potentialTransports.getTransportedUnits(SHIP_TRANSPORT, transportId)) {
Unit transportedUnit = campaign.getUnit(cargoId);
if (transportedUnit != null) {
// Convert the list of Unit UUIDs to MM EntityIds
Expand All @@ -482,8 +471,8 @@ public void run() {
}

// Prompt the player to autoload units into tactical transports (lower priority)
if (!potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).isEmpty()) {
for (UUID transportId : potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).keySet()) {
if (!potentialTransports.getTransports(TACTICAL_TRANSPORT).isEmpty()) {
for (UUID transportId : potentialTransports.getTransports(TACTICAL_TRANSPORT)) {
boolean loadTactical = false;
Unit transport = campaign.getUnit(transportId);
Map<Integer, ITransportAssignment> toLoad = new HashMap<>();
Expand All @@ -499,7 +488,7 @@ public void run() {
// List of technicians assigned to transported units. Several units can share a
// tech.
Set<Person> cargoTechs = new HashSet<>();
for (UUID cargoId : potentialTransports.get(TACTICAL_TRANSPORT.getTransportAssignmentType()).get(transportId)) {
for (UUID cargoId : potentialTransports.getTransportedUnits(TACTICAL_TRANSPORT, transportId)) {
Unit transportedUnit = campaign.getUnit(cargoId);
if (transportedUnit != null && transport.getEntity().canLoad(transportedUnit.getEntity())) { //transportedUnit.getTransportAssignment().getTransporterType()) {
// Convert the list of Unit UUIDs to MM EntityIds
Expand Down
108 changes: 108 additions & 0 deletions MekHQ/src/mekhq/utilities/PotentialTransportsMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) 2025-2025 The MegaMek Team. All Rights Reserved.
*
* This file is part of MekHQ.
*
* MekHQ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MekHQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/

package mekhq.utilities;

import mekhq.campaign.enums.CampaignTransportType;

import java.util.*;

/**
* Map to help hold what transports are transporting what units for a given campaign transport type
* @see CampaignTransportType
*/
public class PotentialTransportsMap {

private final HashMap<CampaignTransportType, Map<UUID, List<UUID>>> hashMap = new HashMap<CampaignTransportType, Map<UUID, List<UUID>>>();

public PotentialTransportsMap(CampaignTransportType[] campaignTransportTypes) {
for (CampaignTransportType campaignTransportType : campaignTransportTypes) {
hashMap.put(campaignTransportType, new HashMap<>());
}
}

/**
* For the provided campaign transport type, get the transports
* @param campaignTransportType type (enum) of campaign transport
* @return transports for the given campaign transport type
*/
public Set<UUID> getTransports(CampaignTransportType campaignTransportType) {
return hashMap.get(campaignTransportType).keySet();
}

/**
* For the provided campaign transport type and transport id, get the transported units
* @param campaignTransportType type (enum) of campaign transport
* @param uuid transport id
* @return list of uuids of units on that transport
*/
public List<UUID> getTransportedUnits(CampaignTransportType campaignTransportType, UUID uuid) {
return hashMap.get(campaignTransportType).get(uuid);
}

/**
* For the provided campaign transport type, does the provided transport exist in the map?
* @param campaignTransportType type (enum) of campaign transport
* @param key transport id
* @return true if that transport exists, false if not
*/
public boolean containsTransportKey(CampaignTransportType campaignTransportType, UUID key) {
return hashMap.containsKey(campaignTransportType) && hashMap.get(campaignTransportType).containsKey(key);
}

/**
* For the provided campaign transport type, add a transport
* @param campaignTransportType type (enum) of campaign transport
* @param key transport id
*/
public void putNewTransport(CampaignTransportType campaignTransportType, UUID key) {
hashMap.get(campaignTransportType).put(key, new ArrayList<>());
}

/**
* For the provided campaign transport type and transport, add a transported unit
* @param campaignTransportType type (enum) of campaign transport
* @param key transport unit id
* @param value transported unit id
*/
public void addTransportedUnit(CampaignTransportType campaignTransportType, UUID key, UUID value) {
hashMap.get(campaignTransportType).get(key).add(value);
}

/**
* Removes any transports that are empty
*/
public void removeEmptyTransports() {
if (hashMap.isEmpty()) {
return;
}
for (CampaignTransportType campaignTransportType : hashMap.keySet()) {
Set<UUID> emptyTransports = new HashSet<>();
if (!(hashMap.get(campaignTransportType).isEmpty())) {
for (UUID transport : hashMap.get(campaignTransportType).keySet()) {
if (hashMap.get(campaignTransportType).get(transport).isEmpty())
emptyTransports.add(transport);
}
}
for (UUID emptyTransport : emptyTransports) {
hashMap.get(campaignTransportType).remove(emptyTransport);
}
}
}
}

0 comments on commit ecf29e4

Please sign in to comment.