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 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); }
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 static double enchantScore(ECSAction action, ScoreParameters<Entity> params) { if (!action.getName().equals(CyborgChroniclesGame.ENCHANT_ACTION)) { return 0; } TargetSet targets = action.getTargetSets().get(0); targets.clearTargets(); List<Entity> possibleTargets = targets.findPossibleTargets(); if (possibleTargets.isEmpty()) { return -1; } Entity enchantment = action.getOwner(); int attackBonus = attack.getFor(enchantment); int healthBonus = health.getFor(enchantment); possibleTargets.sort(Comparator.comparingDouble(e -> enchantScore(e, attackBonus, healthBonus))); Entity chosenTarget = possibleTargets.get(possibleTargets.size() - 1); targets.addTarget(chosenTarget); return enchantScore(chosenTarget, attackBonus, healthBonus); }
protected void useActionWithFailedTarget(Entity source, String actionName, Entity target) { ECSAction action = getAction(source, actionName); Objects.requireNonNull(action, source + " does not have action " + actionName); assertTrue(action.isAllowed(phase.getCurrentEntity())); assertEquals(1, action.getTargetSets().size()); TargetSet targets = action.getTargetSets().get(0); assertFalse("Did not expect target to be allowed", targets.addTarget(target)); assertFalse("Did not expect action to be performed", action.perform(phase.getCurrentEntity())); }
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())); }
private void performMulligan(Entity player, int[] cardsToChange) { HandComponent zone = player.get(hand); ECSAction action = player.getComponent(ActionComponent.class).getAction("Mulligan"); assertNotNull("Action not found on " + player); TargetSet targets = action.getTargetSets().get(0); Stream<Entity> chosenTargets = Arrays.stream(cardsToChange).mapToObj(i -> zone.stream().filter(e -> standardRetr.getFor(e) == i).findAny().get()); chosenTargets.forEach(e -> targets.addTarget(e)); assertTrue("Mulligan was not performed", action.perform(player)); }
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++; }
targetAction.clearTargets(); for (int target : message.getTargets()) { targetAction.addTarget(game.getEntity(target));