Search in sources :

Example 71 with GameData

use of games.strategy.engine.data.GameData in project triplea by triplea-game.

the class PbemSetupPanel method postStartGame.

@Override
public void postStartGame() {
    // // store the dice server
    final GameData data = gameSelectorModel.getGameData();
    data.getProperties().set(DICE_ROLLER, diceServerEditor.getBean());
    // store the Turn Summary Poster
    final IForumPoster poster = (IForumPoster) forumPosterEditor.getBean();
    if (poster != null) {
        // clone the poster, the remove sensitive info, and put the clone into the game data
        // this was the sensitive info is not stored in the save game, but the user cache still has the password
        final IForumPoster summaryPoster = poster.doClone();
        summaryPoster.clearSensitiveInfo();
        data.getProperties().set(PBEMMessagePoster.FORUM_POSTER_PROP_NAME, summaryPoster);
    }
    // store the email poster
    IEmailSender sender = (IEmailSender) emailSenderEditor.getBean();
    if (sender != null) {
        // create a clone, delete the sensitive information in the clone, and use it in the game
        // the locally cached version still has the password so the user doesn't have to enter it every time
        sender = sender.clone();
        sender.clearSensitiveInfo();
        data.getProperties().set(PBEMMessagePoster.EMAIL_SENDER_PROP_NAME, sender);
    }
    // store whether we are a pbem game or not, whether we are capable of posting a game save
    if (poster != null || sender != null) {
        data.getProperties().set(PBEMMessagePoster.PBEM_GAME_PROP_NAME, true);
    }
}
Also used : GameData(games.strategy.engine.data.GameData) IEmailSender(games.strategy.engine.pbem.IEmailSender) IForumPoster(games.strategy.engine.pbem.IForumPoster)

Example 72 with GameData

use of games.strategy.engine.data.GameData in project triplea by triplea-game.

the class MapPanel method paint.

@Override
public void paint(final Graphics g) {
    final Graphics2D g2d = (Graphics2D) g;
    super.paint(g2d);
    g2d.clip(new Rectangle2D.Double(0, 0, (getImageWidth() * scale), (getImageHeight() * scale)));
    int x = model.getX();
    int y = model.getY();
    final List<Tile> images = new ArrayList<>();
    final List<Tile> undrawnTiles = new ArrayList<>();
    final Stopwatch stopWatch = new Stopwatch(Logger.getLogger(MapPanel.class.getName()), Level.FINER, "Paint");
    // make sure we use the same data for the entire paint
    final GameData data = gameData;
    // if the map fits on screen, dont draw any overlap
    final boolean fitAxisX = !mapWidthFitsOnScreen() && uiContext.getMapData().scrollWrapX();
    final boolean fitAxisY = !mapHeightFitsOnScreen() && uiContext.getMapData().scrollWrapY();
    if (fitAxisX || fitAxisY) {
        if (fitAxisX && x + (int) getScaledWidth() > model.getMaxWidth()) {
            x -= model.getMaxWidth();
        }
        if (fitAxisY && y + (int) getScaledHeight() > model.getMaxHeight()) {
            y -= model.getMaxHeight();
        }
        // handle wrapping off the screen
        if (fitAxisX && x < 0) {
            if (fitAxisY && y < 0) {
                final Rectangle2D.Double leftUpperBounds = new Rectangle2D.Double(model.getMaxWidth() + x, model.getMaxHeight() + y, -x, -y);
                drawTiles(g2d, images, data, leftUpperBounds, undrawnTiles);
            }
            final Rectangle2D.Double leftBounds = new Rectangle2D.Double(model.getMaxWidth() + x, y, -x, getScaledHeight());
            drawTiles(g2d, images, data, leftBounds, undrawnTiles);
        }
        if (fitAxisY && y < 0) {
            final Rectangle2D.Double upperBounds = new Rectangle2D.Double(x, model.getMaxHeight() + y, getScaledWidth(), -y);
            drawTiles(g2d, images, data, upperBounds, undrawnTiles);
        }
    }
    // handle non overlap
    final Rectangle2D.Double mainBounds = new Rectangle2D.Double(x, y, getScaledWidth(), getScaledHeight());
    drawTiles(g2d, images, data, mainBounds, undrawnTiles);
    if (routeDescription != null && mouseShadowImage != null && routeDescription.getEnd() != null) {
        final AffineTransform t = new AffineTransform();
        t.translate(scale * normalizeX(routeDescription.getEnd().getX() - getXOffset()), scale * normalizeY(routeDescription.getEnd().getY() - getYOffset()));
        t.translate(mouseShadowImage.getWidth() / -2, mouseShadowImage.getHeight() / -2);
        t.scale(scale, scale);
        g2d.drawImage(mouseShadowImage, t, this);
    }
    if (routeDescription != null) {
        routeDrawer.drawRoute(g2d, routeDescription, movementLeftForCurrentUnits, movementFuelCost, uiContext.getResourceImageFactory());
    }
    // used to keep strong references to what is on the screen so it wont be garbage collected
    // other references to the images are weak references
    this.images.clear();
    this.images.addAll(images);
    if (highlightedUnits != null) {
        for (final Entry<Territory, List<Unit>> entry : highlightedUnits.entrySet()) {
            final Set<UnitCategory> categories = UnitSeperator.categorize(entry.getValue());
            for (final UnitCategory category : categories) {
                final List<Unit> territoryUnitsOfSameCategory = category.getUnits();
                if (territoryUnitsOfSameCategory.isEmpty()) {
                    continue;
                }
                final Rectangle r = tileManager.getUnitRect(territoryUnitsOfSameCategory, gameData);
                if (r == null) {
                    continue;
                }
                final Optional<Image> image = uiContext.getUnitImageFactory().getHighlightImage(category.getType(), category.getOwner(), category.hasDamageOrBombingUnitDamage(), category.getDisabled());
                if (image.isPresent()) {
                    final AffineTransform t = new AffineTransform();
                    t.translate(normalizeX(r.getX() - getXOffset()) * scale, normalizeY(r.getY() - getYOffset()) * scale);
                    t.scale(scale, scale);
                    g2d.drawImage(image.get(), t, this);
                }
            }
        }
    }
    // draw the tiles nearest us first
    // then draw farther away
    updateUndrawnTiles(undrawnTiles, 30, true);
    updateUndrawnTiles(undrawnTiles, 257, true);
    // when we are this far away, dont force the tiles to stay in memroy
    updateUndrawnTiles(undrawnTiles, 513, false);
    updateUndrawnTiles(undrawnTiles, 767, false);
    clearPendingDrawOperations();
    undrawnTiles.forEach(tile -> pendingDrawOperations.add(Tuple.of(tile, data)));
    stopWatch.done();
}
Also used : Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) Rectangle2D(java.awt.geom.Rectangle2D) ArrayList(java.util.ArrayList) Stopwatch(games.strategy.triplea.util.Stopwatch) Rectangle(java.awt.Rectangle) Tile(games.strategy.triplea.ui.screen.Tile) TripleAUnit(games.strategy.triplea.TripleAUnit) Unit(games.strategy.engine.data.Unit) TimeUnit(java.util.concurrent.TimeUnit) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) Graphics2D(java.awt.Graphics2D) AffineTransform(java.awt.geom.AffineTransform) List(java.util.List) ArrayList(java.util.ArrayList) UnitCategory(games.strategy.triplea.util.UnitCategory)

Example 73 with GameData

use of games.strategy.engine.data.GameData in project triplea by triplea-game.

the class MustFightBattleTest method testFightWithIsSuicideOnHit.

@Test
public void testFightWithIsSuicideOnHit() throws Exception {
    final GameData twwGameData = TestMapGameData.TWW.getGameData();
    // Create battle with 1 cruiser attacking 1 mine
    final PlayerID usa = GameDataTestUtil.usa(twwGameData);
    final PlayerID germany = GameDataTestUtil.germany(twwGameData);
    final Territory sz33 = territory("33 Sea Zone", twwGameData);
    addTo(sz33, GameDataTestUtil.americanCruiser(twwGameData).create(1, usa));
    final Territory sz40 = territory("40 Sea Zone", twwGameData);
    addTo(sz40, GameDataTestUtil.germanMine(twwGameData).create(1, germany));
    final ITestDelegateBridge bridge = GameDataTestUtil.getDelegateBridge(usa, twwGameData);
    bridge.setStepName("CombatMove");
    moveDelegate(twwGameData).setDelegateBridgeAndPlayer(bridge);
    moveDelegate(twwGameData).start();
    move(sz33.getUnits().getUnits(), new Route(sz33, sz40));
    moveDelegate(twwGameData).end();
    final MustFightBattle battle = (MustFightBattle) AbstractMoveDelegate.getBattleTracker(twwGameData).getPendingBattle(sz40, false, null);
    bridge.setRemote(dummyPlayer);
    // Set first roll to hit (mine AA) and check that both units are killed
    final ScriptedRandomSource randomSource = new ScriptedRandomSource(0, ScriptedRandomSource.ERROR);
    bridge.setRandomSource(randomSource);
    battle.fight(bridge);
    assertEquals(1, randomSource.getTotalRolled());
    assertEquals(0, sz40.getUnits().size());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) ITestDelegateBridge(games.strategy.engine.data.ITestDelegateBridge) ScriptedRandomSource(games.strategy.engine.random.ScriptedRandomSource) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 74 with GameData

use of games.strategy.engine.data.GameData in project triplea by triplea-game.

the class MoveValidatorTest method testValidateUnitsCanLoadInHostileSeaZones.

@Test
public void testValidateUnitsCanLoadInHostileSeaZones() throws Exception {
    final GameData twwGameData = TestMapGameData.TWW.getGameData();
    // Load german unit in sea zone with no enemy ships
    final PlayerID germans = GameDataTestUtil.germany(twwGameData);
    final Territory northernGermany = territory("Northern Germany", twwGameData);
    final Territory sz27 = territory("27 Sea Zone", twwGameData);
    final Route r = new Route(northernGermany, sz27);
    northernGermany.getUnits().clear();
    addTo(northernGermany, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
    final List<Unit> transport = sz27.getUnits().getMatches(Matches.unitIsTransport());
    MoveValidationResult results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add USA ship to transport sea zone
    final PlayerID usa = GameDataTestUtil.usa(twwGameData);
    addTo(sz27, GameDataTestUtil.americanCruiser(twwGameData).create(1, usa));
    results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
    assertFalse(results.isMoveValid());
    // Set 'Units Can Load In Hostile Sea Zones' to true
    twwGameData.getProperties().set(Constants.UNITS_CAN_LOAD_IN_HOSTILE_SEA_ZONES, true);
    results = MoveValidator.validateMove(northernGermany.getUnits(), r, germans, transport, new HashMap<>(), false, null, twwGameData);
    assertTrue(results.isMoveValid());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) HashMap(java.util.HashMap) Unit(games.strategy.engine.data.Unit) TripleAUnit(games.strategy.triplea.TripleAUnit) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Example 75 with GameData

use of games.strategy.engine.data.GameData in project triplea by triplea-game.

the class MoveValidatorTest method testValidateMoveForLandTransports.

@Test
public void testValidateMoveForLandTransports() throws Exception {
    final GameData twwGameData = TestMapGameData.TWW.getGameData();
    // Move truck 2 territories
    final PlayerID germans = GameDataTestUtil.germany(twwGameData);
    final Territory berlin = territory("Berlin", twwGameData);
    final Territory easternGermany = territory("Eastern Germany", twwGameData);
    final Territory poland = territory("Poland", twwGameData);
    final Route r = new Route(berlin, easternGermany, poland);
    berlin.getUnits().clear();
    GameDataTestUtil.truck(twwGameData).create(1, germans);
    addTo(berlin, GameDataTestUtil.truck(twwGameData).create(1, germans));
    MoveValidationResult results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add an infantry for truck to transport
    addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add an infantry and the truck can't transport both
    addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertFalse(results.isMoveValid());
    // Add a large truck (has capacity for 2 infantry) to transport second infantry
    addTo(berlin, GameDataTestUtil.largeTruck(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add an infantry that the large truck can also transport
    addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertTrue(results.isMoveValid());
    // Add an infantry that can't be transported
    addTo(berlin, GameDataTestUtil.germanInfantry(twwGameData).create(1, germans));
    results = MoveValidator.validateMove(berlin.getUnits(), r, germans, Collections.emptyList(), new HashMap<>(), true, null, twwGameData);
    assertFalse(results.isMoveValid());
}
Also used : PlayerID(games.strategy.engine.data.PlayerID) Territory(games.strategy.engine.data.Territory) GameData(games.strategy.engine.data.GameData) TestMapGameData(games.strategy.triplea.xml.TestMapGameData) HashMap(java.util.HashMap) MoveValidationResult(games.strategy.triplea.delegate.dataObjects.MoveValidationResult) Route(games.strategy.engine.data.Route) Test(org.junit.jupiter.api.Test)

Aggregations

GameData (games.strategy.engine.data.GameData)204 Unit (games.strategy.engine.data.Unit)100 PlayerID (games.strategy.engine.data.PlayerID)92 Territory (games.strategy.engine.data.Territory)92 ArrayList (java.util.ArrayList)83 TripleAUnit (games.strategy.triplea.TripleAUnit)64 HashSet (java.util.HashSet)50 CompositeChange (games.strategy.engine.data.CompositeChange)40 List (java.util.List)36 HashMap (java.util.HashMap)32 Set (java.util.Set)32 Route (games.strategy.engine.data.Route)31 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)30 Collection (java.util.Collection)29 UnitType (games.strategy.engine.data.UnitType)26 Change (games.strategy.engine.data.Change)24 Test (org.junit.jupiter.api.Test)23 Resource (games.strategy.engine.data.Resource)22 TestMapGameData (games.strategy.triplea.xml.TestMapGameData)22 Map (java.util.Map)21