Skip to content

Commit

Permalink
Merge pull request #5513 from SJuliez/ds-loading
Browse files Browse the repository at this point in the history
DS loading, issue 5495
  • Loading branch information
SJuliez authored May 28, 2024
2 parents d10e1c7 + 8516dfe commit 4db1bd6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
16 changes: 0 additions & 16 deletions megamek/src/megamek/server/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3687,14 +3687,6 @@ private void loadUnit(Entity loader, Entity unit, int bayNumber) {
// set deployment round of the loadee to equal that of the loader
unit.setDeployRound(loader.getDeployRound());

// Update the loading unit's passenger count, if it's a large craft
if ((loader instanceof SmallCraft) || (loader instanceof Jumpship)) {
// Don't add DropShip crew to a JumpShip or station's passenger list
if (!unit.isLargeCraft()) {
loader.setNPassenger(loader.getNPassenger() + unit.getCrew().getSize());
}
}

// Update the loaded unit.
entityUpdate(unit.getId());
entityUpdate(loader.getId());
Expand Down Expand Up @@ -3958,14 +3950,6 @@ private boolean unloadUnit(Entity unloader, Targetable unloaded,
unit.setDone(false);
}

//Update the transport unit's passenger count, if it's a large craft
if (unloader instanceof SmallCraft || unloader instanceof Jumpship) {
//Don't add dropship crew to a jumpship or station's passenger list
if (!unit.isLargeCraft()) {
unloader.setNPassenger(Math.max(0, unloader.getNPassenger() - unit.getCrew().getSize()));
}
}

// Update the unloaded unit.
entityUpdate(unit.getId());

Expand Down
60 changes: 60 additions & 0 deletions megamek/unittests/megamek/common/DropShipMekLoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package megamek.common;

import megamek.common.enums.GamePhase;
import megamek.common.net.enums.PacketCommand;
import megamek.common.net.packets.Packet;
import megamek.common.verifier.TestEntity;
import megamek.server.GameManager;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class DropShipMekLoadTest {

@Test
public void test() throws Exception {
MechSummaryCache instance = MechSummaryCache.getInstance(true);
Mech atlas = (Mech) instance.getMech("Atlas AS7-D").loadEntity();
atlas.setId(2);
Dropship leopard = (Dropship) instance.getMech("Leopard (2537)").loadEntity();
leopard.setId(1);

Game game = new Game();
game.setPhase(GamePhase.LOUNGE);
game.addPlayer(0, new Player(0, "TestPlayer"));
game.addEntity(atlas);
game.addEntity(leopard);

GameManager gm = mock(GameManager.class);
doNothing().when(gm).entityUpdate(anyInt());
when(gm.getGame()).thenReturn(game);
doCallRealMethod().when(gm).setGame(any(Game.class));
doCallRealMethod().when(gm).handlePacket(anyInt(), any(Packet.class));
gm.setGame(game);

Packet packet = new Packet(PacketCommand.ENTITY_LOAD, atlas.getId(), leopard.getId(), -1);
gm.handlePacket(0, packet);

doAssertions(leopard, atlas);

leopard.setAltitude(0);

doAssertions(leopard, atlas);
}

private void doAssertions(Entity leopard, Entity atlas) {
StringBuffer errors = new StringBuffer();
assertTrue(isValid(leopard, errors), "Leopard is not valid after loading Atlas, errors: " + errors);
assertTrue(isValid(atlas, errors), "Atlas is not valid after loading onto Leopard, errors: " + errors);
assertEquals(atlas.getTransportId(), leopard.getId(),
"Carrier ID " + atlas.getTransportId() + " wrong, should be " + leopard.getId());
assertEquals(1, leopard.getLoadedUnits().size(),
"Loaded unit list size" + leopard.getLoadedUnits().size() + " wrong, should be 1");
assertNull(atlas.getPosition(), "Loaded Atlas position is " + atlas.getPosition() + ", should be null");
}

private boolean isValid(Entity entity, StringBuffer errors) {
return TestEntity.getEntityVerifier(entity).correctEntity(errors);
}
}

0 comments on commit 4db1bd6

Please sign in to comment.