Search in sources :

Example 1 with ResourceCollection

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

the class ChangeResourceChange method perform.

@Override
protected void perform(final GameData data) {
    final Resource resource = data.getResourceList().getResource(m_resource);
    final ResourceCollection resources = data.getPlayerList().getPlayerId(m_player).getResources();
    if (m_quantity > 0) {
        resources.addResource(resource, m_quantity);
    } else if (m_quantity < 0) {
        resources.removeResource(resource, -m_quantity);
    }
}
Also used : Resource(games.strategy.engine.data.Resource) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 2 with ResourceCollection

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

the class EndTurnDelegate method getResourceProduction.

/**
 * Since territory resource may contain any resource except PUs (PUs use "getProduction" instead),
 * we will now figure out the total production of non-PUs resources.
 */
public static IntegerMap<Resource> getResourceProduction(final Collection<Territory> territories, final GameData data) {
    final IntegerMap<Resource> resources = new IntegerMap<>();
    for (final Territory current : territories) {
        final TerritoryAttachment attachment = TerritoryAttachment.get(current);
        if (attachment == null) {
            throw new IllegalStateException("No attachment for owned territory:" + current.getName());
        }
        final ResourceCollection toAdd = attachment.getResources();
        if (toAdd == null) {
            continue;
        }
        // Match will check if territory is originally owned convoy center, or if contested
        if (Matches.territoryCanCollectIncomeFrom(current.getOwner(), data).test(current)) {
            resources.add(toAdd.getResourcesCopy());
        }
    }
    return resources;
}
Also used : IntegerMap(games.strategy.util.IntegerMap) Territory(games.strategy.engine.data.Territory) TerritoryAttachment(games.strategy.triplea.attachments.TerritoryAttachment) Resource(games.strategy.engine.data.Resource) ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 3 with ResourceCollection

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

the class ResourceCollectionUtils method exclude.

/**
 * Returns a copy of the specified resource collection filtered to exclude the blacklisted resources.
 *
 * @param unfiltered The resource collection to filter; must not be {@code null}.
 * @param resources The blacklisted resources to exclude; must not be {@code null}.
 *
 * @return The filtered resource collection; never {@code null}.
 *
 * @throws IllegalArgumentException If {@code resources} contains a {@code null} element.
 */
public static ResourceCollection exclude(final ResourceCollection unfiltered, final Resource... resources) {
    checkNotNull(unfiltered);
    checkNotNull(resources);
    checkArgument(Arrays.stream(resources).noneMatch(Objects::isNull), "resources must not contain null");
    final ResourceCollection filtered = new ResourceCollection(unfiltered);
    Arrays.stream(resources).forEach(filtered::removeAllOfResource);
    return filtered;
}
Also used : ResourceCollection(games.strategy.engine.data.ResourceCollection)

Example 4 with ResourceCollection

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

the class DoesNothingAi method endTurn.

@Override
protected void endTurn(final IAbstractForumPosterDelegate endTurnForumPosterDelegate, final GameData data, final PlayerID player) {
    // destroy whatever we have
    final ResourceCollection resourceCollection = player.getResources();
    final Change removeChange = ChangeFactory.removeResourceCollection(player, resourceCollection);
    // shameless cheating... (do NOT do this, normally you are never supposed to access the IDelegateBridge from outside
    // of a delegate)
    final IDelegateBridge bridge = endTurnForumPosterDelegate.getBridge();
    // resourceCollection is not yet a valid renderingObject
    bridge.getHistoryWriter().startEvent(player.getName() + " removes resources: " + resourceCollection, null);
    bridge.addChange(removeChange);
}
Also used : Change(games.strategy.engine.data.Change) ResourceCollection(games.strategy.engine.data.ResourceCollection) IDelegateBridge(games.strategy.engine.delegate.IDelegateBridge)

Example 5 with ResourceCollection

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

the class ResourceCollectionUtilsTest method testExcludeByNames_ShouldExcludeSpecifiedResources.

@Test
public void testExcludeByNames_ShouldExcludeSpecifiedResources() {
    givenGameResources(pus, techTokens, vps);
    final ResourceCollection unfiltered = createResourceCollection(pus, techTokens, vps);
    final ResourceCollection filtered = ResourceCollectionUtils.exclude(unfiltered, pus.getName(), vps.getName());
    assertThat(filtered.getQuantity(pus), is(0));
    assertThat(filtered.getQuantity(techTokens), is(unfiltered.getQuantity(techTokens)));
    assertThat(filtered.getQuantity(vps), is(0));
}
Also used : ResourceCollection(games.strategy.engine.data.ResourceCollection) Test(org.junit.jupiter.api.Test)

Aggregations

ResourceCollection (games.strategy.engine.data.ResourceCollection)23 Resource (games.strategy.engine.data.Resource)7 Test (org.junit.jupiter.api.Test)6 PlayerID (games.strategy.engine.data.PlayerID)4 ProductionRule (games.strategy.engine.data.ProductionRule)4 IntegerMap (games.strategy.util.IntegerMap)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Territory (games.strategy.engine.data.Territory)3 Unit (games.strategy.engine.data.Unit)3 UnitType (games.strategy.engine.data.UnitType)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Map (java.util.Map)3 GameData (games.strategy.engine.data.GameData)2 NamedAttachable (games.strategy.engine.data.NamedAttachable)2 ProductionFrontier (games.strategy.engine.data.ProductionFrontier)2 RepairRule (games.strategy.engine.data.RepairRule)2 TripleAUnit (games.strategy.triplea.TripleAUnit)2 UnitAttachment (games.strategy.triplea.attachments.UnitAttachment)2