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

Small Troopspace refactor #6629

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 0 additions & 5 deletions megamek/src/megamek/common/BattleArmorHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ public final List<Entity> getExternalUnits() {
return getLoadedUnits();
}

@Override
public int getCargoMpReduction(Entity carrier) {
return 0;
}

@Override
public String toString() {
return "BattleArmorHandles - troopers:" + carriedUnit;
Expand Down
10 changes: 0 additions & 10 deletions megamek/src/megamek/common/Bay.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ public String getDefaultSlotDescription() {
return "";
}

@Override
public boolean isWeaponBlockedAt(int loc, boolean isRear) {
return false;
}

@Override
public Entity getExteriorUnitAt(int loc, boolean isRear) {
return null;
Expand All @@ -305,11 +300,6 @@ public final List<Entity> getExternalUnits() {
return new ArrayList<>(1);
}

@Override
public int getCargoMpReduction(Entity carrier) {
return 0;
}

public String getType() {
return "Unknown";
}
Expand Down
10 changes: 0 additions & 10 deletions megamek/src/megamek/common/DockingCollar.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ public double getUnused() {
return dockedUnits.isEmpty() ? 1 : 0;
}

@Override
public boolean isWeaponBlockedAt(int loc, boolean isRear) {
return false;
}

@Override
public @Nullable Entity getExteriorUnitAt(int loc, boolean isRear) {
return null;
Expand All @@ -148,11 +143,6 @@ public final List<Entity> getExternalUnits() {
return new ArrayList<>();
}

@Override
public int getCargoMpReduction(Entity carrier) {
return 0;
}

public boolean isDamaged() {
return damaged;
}
Expand Down
222 changes: 48 additions & 174 deletions megamek/src/megamek/common/InfantryCompartment.java
Original file line number Diff line number Diff line change
@@ -1,177 +1,89 @@
/*
* Copyright (c) 2003-2004 Ben Mazur ([email protected])
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This program 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 2 of the License, or (at your option) any later
* version.
* This file is part of MegaMek.
*
* This program 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.
* MegaMek 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.
*
* MegaMek 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 MegaMek. If not, see <http://www.gnu.org/licenses/>.
*/
package megamek.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import megamek.logging.MMLogger;

import java.io.Serial;
import java.util.*;

/**
* Represents a volume of space set aside for carrying troops and their equipment under battle
* conditions. Typically, a component of an APC.
* Represents a volume of space set aside for carrying Conventional Infantry and BattleArmor and their equipment under battle conditions.
* TM p.239; see e.g. JI2A1 Attack APC
*/
public final class InfantryCompartment implements Transporter {
@Serial
private static final long serialVersionUID = 7837499891552862932L;
private static final MMLogger LOGGER = MMLogger.create(InfantryCompartment.class);

/**
* The troops being carried.
*/
Map<Integer, Double> troops = new HashMap<>();

/**
* The total amount of space available for troops.
*/
double totalSpace;
/** The carried troops, mapping unit ID to carried weight of the unit. */
private final Map<Integer, Double> carriedTroops = new HashMap<>();
final double totalSpace;
private double currentSpace;

/**
* The current amount of space available for troops.
*/
double currentSpace;

transient Game game;

/**
* The default constructor is only for serialization.
*/
private InfantryCompartment() {
totalSpace = 0;
currentSpace = 0;
}
private transient Game game;

/**
* Create a space for the given tonnage of troops. For this class, only the
* weight of the troops (and their equipment) are considered; if you'd like
* to think that they are stacked like lumber, be my guest.
* Creates an InfantryCompartment for the given tonnage of troops.
*
* @param space The weight of troops (in tons) this space can carry.
* @param tonnage The weight of troops (in tons) this space can carry
*/
public InfantryCompartment(double space) {
totalSpace = space;
currentSpace = space;
public InfantryCompartment(double tonnage) {
totalSpace = tonnage;
currentSpace = tonnage;
}

/**
* Determines if this object can accept the given unit. The unit may not be
* of the appropriate type or there may be no room for the unit.
*
* @param unit the <code>Entity</code> to be loaded.
* @return <code>true</code> if the unit can be loaded, <code>false</code> otherwise.
*/
@Override
public boolean canLoad(Entity unit) {
// Assume that we can carry the unit.
boolean result = true;

// Only Infantry and BattleArmor can be carried in TroopSpace.
if (!(unit instanceof Infantry)) {
result = false;
}

// We must have enough space for the new troops.
// POSSIBLE BUG: we may have to take the Math.ceil() of the weight.
else if (currentSpace < unit.getWeight()) {
result = false;
}

// Return our result.
return result;
return unit.isInfantry() && (currentSpace >= unit.getWeight());
}

/**
* Load the given unit.
*
* @param unit the <code>Entity</code> to be loaded.
* @throws IllegalArgumentException If the unit can't be loaded
*/
@Override
public void load(Entity unit) throws IllegalArgumentException {
// If we can't load the unit, throw an exception.
if (!canLoad(unit)) {
throw new IllegalArgumentException("Can not load " + unit.getShortName() + " into this troop space.");
throw new IllegalArgumentException("Can not load " + unit + " into this troop space.");
}

// Decrement the available space.
// POSSIBLE BUG: we may have to take the Math.ceil() of the weight.
currentSpace -= unit.getWeight();

// Add the unit to our list of troops.
troops.put(unit.getId(), unit.getWeight());

carriedTroops.put(unit.getId(), unit.getWeight());
}

/**
* Get a <code>List</code> of the units currently loaded into this payload.
*
* @return A <code>List</code> of loaded <code>Entity</code> units. This list will never be
* <code>null</code>, but it may be empty. The returned <code>List</code> is independent from
* the underlying data structure; modifying one does not affect the other.
*/
@Override
public Vector<Entity> getLoadedUnits() {
Vector<Entity> loaded = new Vector<>();
for (Map.Entry<Integer, Double> entry : troops.entrySet()) {
int key = entry.getKey();
Entity entity = game.getEntity(key);

if (entity != null) {
loaded.add(entity);
}
}

return loaded;
public List<Entity> getLoadedUnits() {
return carriedTroops.keySet().stream().map(game::getEntity).toList();
}

/**
* Unload the given unit.
*
* @param unit
* - the <code>Entity</code> to be unloaded.
* @return <code>true</code> if the unit was contained in this space,
* <code>false</code> otherwise.
*/
@Override
public boolean unload(Entity unit) {
// If this unit isn't loaded, nothing to do
if (!troops.containsKey(unit.getId())) {
if (unit == null) {
LOGGER.error("Trying to unload a null unit!");
return false;
} else if (carriedTroops.containsKey(unit.getId())) {
currentSpace += carriedTroops.get(unit.getId());
carriedTroops.remove(unit.getId());
return true;
} else {
return false;
}

// Remove the unit if we are carrying it.
boolean retval = false;
double unloadWeight = 0;

if (unit != null) {
unloadWeight = troops.get(unit.getId());
}

// If we removed it, restore our space.
if (troops.remove(unit.getId()) != null) {
retval = true;
currentSpace += unloadWeight;
}

// Return our status
return retval;
}

/**
* Return a string that identifies the unused capacity of this transporter.
*
* @return A <code>String</code> meant for a human.
*/
@Override
public String getUnusedString() {
return "Infantry Compartment - " + currentSpace + " tons";
Expand All @@ -182,57 +94,19 @@ public double getUnused() {
return currentSpace;
}

/**
* Determine if transported units prevent a weapon in the given location
* from firing.
*
* @param loc
* - the <code>int</code> location attempting to fire.
* @param isRear
* - a <code>boolean</code> value stating if the given location
* is rear facing; if <code>false</code>, the location is front
* facing.
* @return <code>true</code> if a transported unit is in the way,
* <code>false</code> if the weapon can fire.
*/
@Override
public boolean isWeaponBlockedAt(int loc, boolean isRear) {
return false;
}

/**
* If a unit is being transported on the outside of the transporter, it can
* suffer damage when the transporter is hit by an attack. Currently, no
* more than one unit can be at any single location; that same unit can be
* "spread" over multiple locations.
*
* @param loc
* - the <code>int</code> location hit by attack.
* @param isRear
* - a <code>boolean</code> value stating if the given location
* is rear facing; if <code>false</code>, the location is front
* facing.
* @return The <code>Entity</code> being transported on the outside at that
* location. This value will be <code>null</code> if no unit is
* transported on the outside at that location.
*/
@Override
public Entity getExteriorUnitAt(int loc, boolean isRear) {
return null;
}

@Override
public List<Entity> getExternalUnits() {
return new ArrayList<>(1);
}

@Override
public int getCargoMpReduction(Entity carrier) {
return 0;
return Collections.emptyList();
}

@Override
public String toString() {
// Beware - this is used for saving to blk files - which is a bad idea
return "troopspace:" + totalSpace;
}

Expand All @@ -243,7 +117,7 @@ public void setGame(Game game) {

@Override
public void resetTransporter() {
troops = new HashMap<>();
carriedTroops.clear();
currentSpace = totalSpace;
}
}
24 changes: 0 additions & 24 deletions megamek/src/megamek/common/TankTrailerHitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,6 @@ public void resetTransporter() {
towed = Entity.NONE;
}

/**
* Determine if transported units prevent a weapon in the given location
* from firing.
*
* @param loc
* - the <code>int</code> location attempting to fire.
* @param isRear
* - a <code>boolean</code> value stating if the given location
* is rear facing; if <code>false</code>, the location is front
* facing.
* @return <code>true</code> if a transported unit is in the way,
* <code>false</code> if the weapon can fire.
*/
@Override
public boolean isWeaponBlockedAt(int loc, boolean isRear) {
// Assume that the weapon is not blocked. See Entity.isWeaponBlockedByTowing() instead.
return false;
}

/**
* If a unit is being transported on the outside of the transporter, it can
* suffer damage when the transporter is hit by an attack. Currently, no
Expand Down Expand Up @@ -247,11 +228,6 @@ public final List<Entity> getExternalUnits() {
return Collections.unmodifiableList(rv);
}

@Override
public int getCargoMpReduction(Entity carrier) {
return 0;
}

@Override
public String toString() {
return "Trailer Hitch:" + getUnused();
Expand Down
Loading