protected void useActionWithTarget(Entity entity, String actionName, Entity target) { ECSAction action = getAction(entity, actionName); assertTrue(action.isAllowed(phase.getCurrentEntity())); assertEquals("Unexpected numbet of TargetSets.", 1, action.getTargetSets().size()); TargetSet targets = action.getTargetSets().get(0); assertTrue("Target could not be added: " + target + " to action " + action, targets.addTarget(target)); assertTrue(action + " could not be performed", action.perform(phase.getCurrentEntity())); }
public ECSAction copy(Entity copyTo) { ECSAction action = new ECSAction(copyTo, this.name, this.allowed, this.perform); for (TargetSet set : targetSets) { action.addTargetSet(set.getMin(), set.getMax()); TargetSet lastSet = action.getTargetSets().get(action.getTargetSets().size() - 1); set.getChosenTargets().forEach(target -> lastSet.addTarget(target)); } return action; }
public ECSAction copy() { return copy(owner); }
private ECSAction createMulliganAction(Entity player) { return new ECSAction(player, ACTION_NAME, act -> true, this::performAction).addTargetSet(0, Integer.MAX_VALUE); }
protected List<Entity> findPossibleTargets(Entity entity, String actionName) { ECSAction action = getAction(entity, actionName); assertTrue(action.isAllowed(phase.getCurrentEntity())); assertEquals(1, action.getTargetSets().size()); TargetSet targets = action.getTargetSets().get(0); return targets.findPossibleTargets(); }
@Test public void deniedActionWithSystem() { String name = "Use"; game.addSystem(new SpecificActionSystem(name) { @Override protected void isAllowed(ActionAllowedCheckEvent event) { event.setAllowed(false); } @Override protected void onPerform(ActionPerformEvent event) { } }); game.startGame(); ActionComponent actions = retriever.get(entity); assertEquals(Collections.<String>emptySet(), actions.getActions()); @SuppressWarnings("unchecked") Consumer<ECSAction> perform = (Consumer<ECSAction>) mock(Consumer.class); actions.addAction(new ECSAction(entity, name, action -> true, perform)); actions.getAction(name).copy().perform(entity); verifyNoMoreInteractions(perform); assertFalse(actions.getAction(name).isAllowed(entity)); }
public void nextStep() { ReplayAction step = replayData.getActionInformation().get(currentActionIndex); Entity entity = game.getEntity(step.getEntity()); ECSAction action = Actions.getAction(entity, step.getActionName()); if (action.getTargetSets().size() != step.getTargets().size()) { throw new ReplayException("Number of targetsets does not match for action " + action + " at action index " + currentActionIndex); } for (int i = 0; i < action.getTargetSets().size(); i++) { TargetSet targetSet = action.getTargetSets().get(i); List<Integer> targets = step.getTargets().get(i); targetSet.clearTargets(); for (int target : targets) { Entity targetEntity = game.getEntity(target); if (targetEntity == null) { throw new ReplayException("Target " + target + " not found when performing " + action + " at action index " + currentActionIndex); } targetSet.addTarget(targetEntity); } } boolean performSuccess = action.perform(game.getEntity(step.getPerformer())); if (!performSuccess) { throw new ReplayException("Replay action not correctly performed " + action + " at action index " + currentActionIndex); } currentActionIndex++; }
/** * Looks at all Clients, resets their actions, then gets all actions for each and sends them. */ private void sendAvailableActions() { for (ClientIO io : this.getPlayers()) { io.sendToClient(new ResetAvailableActionsMessage()); if (game.isGameOver()) { continue; } Entity player = playerFor(io); getAllActions(game).filter(action -> action.isAllowed(player)) .forEach(action -> io.sendToClient(new UsableActionMessage(action.getOwner().getId(), action.getName(), !action.getTargetSets().isEmpty()))); } }
@Test public void testPerformAllowedAction() { game.startGame(); ActionComponent actions = retriever.get(entity); assertEquals(Collections.<String>emptySet(), actions.getActions()); @SuppressWarnings("unchecked") Consumer<ECSAction> perform = (Consumer<ECSAction>) mock(Consumer.class); String name = "Use"; actions.addAction(new ECSAction(entity, name, action -> true, perform)); actions.getAction(name).copy().perform(entity); verify(perform).accept(Mockito.any(ECSAction.class)); }
public static double attackScore(ECSAction action, ScoreParameters<Entity> params) { if (!action.getName().equals(CyborgChroniclesGame.ATTACK_ACTION)) { return 0; } TargetSet targets = action.getTargetSets().get(0); targets.clearTargets(); List<Entity> possibleTargets = targets.findPossibleTargets(); Optional<Entity> player = possibleTargets.stream().filter(e -> e.hasComponent(PlayerComponent.class)).findAny(); if (player.isPresent()) { // attacking a player is really the best option there is targets.addTarget(player.get()); return 100; } if (possibleTargets.isEmpty()) { throw new RuntimeException("Attack action has no targets: " + action); } possibleTargets.sort(Comparator.comparingInt(e -> attackVS(action.getOwner(), e))); Entity chosenTarget = possibleTargets.get(possibleTargets.size() - 1); targets.addTarget(chosenTarget); return attackVS(action.getOwner(), chosenTarget); }
private void perform(Entity entity, ECSAction action) { try { logger.info(entity + " performs " + action); if (ai.has(entity)) { ai.get(entity).future = null; } boolean performed = action.perform(entity); if (!performed) { logger.error(entity + " AI cannot perform action " + action); aiPerform(entity.getGame()); } } catch (Throwable ex) { // We're on a different thread, so we need information in case anything goes wrong logger.error(entity + " Error performing " + action, ex); } }
@Override public boolean canScoreField(ScoreParameters<Entity> params, ECSAction action) { return action.isAllowed(params.getParameters()); }
@Test public void removeSystemTest() { ECSGame game = new ECSGame(); ActionComponent act = new ActionComponent(); Entity entity = game.newEntity().addComponent(act); act.addAction(new ECSAction(entity, "Test", e -> true, e -> {})); system = new SpecificActionSystem("Test") { @Override protected void onPerform(ActionPerformEvent event) { value.getAndIncrement(); event.getEntity().getGame().removeSystem(system); } }; game.addSystem(system); game.startGame(); assertEquals(0, value.get()); act.getAction("Test").perform(entity); assertEquals(1, value.get()); act.getAction("Test").perform(entity); assertEquals(1, value.get()); game.addSystem(system); act.getAction("Test").perform(entity); assertEquals(2, value.get()); }
private boolean setTargetIfPossible(ECSAction action) { for (TargetSet targetset : action.getTargetSets()) { targetset.clearTargets(); while (!targetset.hasEnoughTargets()) { List<Entity> targets = targetset.findPossibleTargets(); if (targets.isEmpty()) { return false; } targetset.addTarget(targets.get(random.nextInt(targets.size()))); } return true; } return true; }
public static double scrapNeeded(ECSAction action, ScoreParameters<Entity> params) { if (!action.getName().equals(CyborgChroniclesGame.SCRAP_ACTION)) { return 0; } Entity entity = action.getOwner(); ComponentRetriever<CardComponent> card = Retrievers.component(CardComponent.class); Entity owner = card.get(entity).getOwner(); HandComponent hand = owner.getComponent(HandComponent.class); return hand.stream().mapToInt(e -> scrapCost.getOrDefault(e, 0)).sum(); }
private void onCardPlayed(ActionPerformEvent event) { if (event.getAction().getName().equals(actionName)) { return; } this.cardsPlayedThisTurn++; if (this.cardsPlayedThisTurn >= limit) { if (actionName == null) { PhaseController phases = Retrievers.singleton(event.getEntity().getGame(), PhaseController.class); phases.nextPhase(); } else { ECSAction action = Actions.getAction(event.getPerformer(), actionName); action.perform(event.getPerformer()); } cardsPlayedThisTurn = 0; } }
private ECSAction moveAction(String name, Entity entity, Class<? extends ZoneComponent> zone, boolean switchPlayer) { return new ECSAction(entity, name, act -> true, act -> { CardComponent card = act.getOwner().getComponent(CardComponent.class); Entity player = Players.findOwnerFor(act.getOwner()); final Entity origPlayer = player; if (switchPlayer) { Set<Entity> players = entity.getGame().getEntitiesWithComponent(PlayerComponent.class); player = players.stream().filter(pl -> pl != origPlayer).findAny().get(); } card.moveToBottom(player.getComponent(zone)); }); }
private ReplayAction(ActionPerformEvent event) { this.entity = event.getEntity().getId(); this.performer = event.getPerformer().getId(); this.actionName = event.getAction().getName(); this.targets = event.getAction().getTargetSets().stream() .map(targetSet -> targetSet.getChosenTargets().stream() .map(e -> e.getId()).collect(Collectors.toList())) .collect(Collectors.toList()); }
private void performAction(ECSAction mulliganAction) { List<Entity> chosenTargets = mulliganAction.getTargetSets().get(0).getChosenTargets(); chosenTargets.forEach(this::switchCard); Entity owner = mulliganAction.getOwner(); owner.getComponent(ActionComponent.class).removeAction(ACTION_NAME); owner.getComponent(DeckComponent.class).shuffle(); if (remainingPerforms.decrementAndGet() == 0) { phases.nextPhase(); } }