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; }
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 ECSAction addTargetSet(int min, int max) { // TODO: Consider using an ECSAction builder and put `addTargetSet` there this.targetSets.add(new TargetSet(this, min, max)); return this; }
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); }
/** * Sends a list of target entities based on the action supplied in the message * * @param message The RequestTargetsMessage object * @param client The client requesting the targets */ public void informAboutTargets(RequestTargetsMessage message, ClientIO client) { ECSAction action = findAction(message.getId(), message.getAction()); TargetSet targetAction = action.getTargetSets().get(0); List<Entity> targets = targetAction.findPossibleTargets(); int[] targetIds = targets.stream().mapToInt(e -> e.getId()).toArray(); client.sendToClient(new AvailableTargetsMessage(message.getId(), message.getAction(), targetIds, targetAction.getMin(), targetAction.getMax())); }
if (!action.getTargetSets().isEmpty()) { TargetSet targetAction = action.getTargetSets().get(0); targetAction.clearTargets(); for (int target : message.getTargets()) { targetAction.addTarget(game.getEntity(target));
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())); }
public Stream<Entity> getAllTargets() { return targetSets.stream().flatMap(e -> e.getChosenTargets().stream()); }
public boolean perform(Entity performer) { synchronized (performer.getGame()) { if (!this.isAllowed(performer)) { return false; } if (!this.targetSets.stream().allMatch(targets -> targets.hasEnoughTargets())) { return false; } try { this.owner.getGame().executeEvent(new ActionPerformEvent(owner, this, performer), () -> this.perform.accept(this)); } catch (RuntimeException ex) { throw new RuntimeException("Error performing action " + this, ex); } this.targetSets.forEach(TargetSet::clearTargets); return true; } }
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(); }
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); }
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++; }
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 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 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)); }
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(); } }
@Override protected void onPerform(ActionPerformEvent event) { Entity target = event.getAction().getTargetSets().get(0).getChosenTargets().get(0); Entity enchantment = event.getEntity(); for (ECSResource resource : resources) { ResourceRetriever res = ResourceRetriever.forResource(resource); int enchantmentValue = res.getFor(enchantment); res.resFor(target).change(enchantmentValue); } enchantment.destroy(); }
@Override protected void onPerform(ActionPerformEvent event) { Entity source = event.getEntity(); Entity target = event.getAction().getTargetSets().get(0).getChosenTargets().get(0); ECSGame game = event.getEntity().getGame(); int attackDamage = attack.getFor(source); int defenseDamage = attack.getFor(target); if (target.hasComponent(PlayerComponent.class)) { damage(event, attackDamage, target, source, game); } else { DamageEvent damageEvent = new DamageEvent(event, target, source, attackDamage); game.getEvents().executeEvent(damageEvent, e -> { }); destroyOrNothing(damageEvent.getDamage(), target); } if (allowCounterAttack.test(source, target)) { destroyOrNothing(defenseDamage, source); } }
@Override protected void onPerform(ActionPerformEvent event) { Entity source = event.getEntity(); Entity target = event.getAction().getTargetSets().get(0).getChosenTargets().get(0); AttackEvent attackEvent = new AttackEvent(source, target); source.getGame().executeCancellableEvent(attackEvent, () -> { int attackDamage = attack.getFor(source); int defenseDamage = attack.getFor(target); int attackHealth = health.getFor(source); int defenseHealth = health.getFor(target); ECSGame game = source.getGame(); attackDamage = damage(attackEvent, attackDamage, target, source, game); boolean counterAttack = allowCounterAttack.test(source, target); if (counterAttack) { defenseDamage = damage(attackEvent, defenseDamage, source, target, game); } checkKill(target, attackDamage, defenseHealth); if (counterAttack) { checkKill(source, defenseDamage, attackHealth); } }); }