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 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++; }
if (!action.getTargetSets().isEmpty()) { TargetSet targetAction = action.getTargetSets().get(0); targetAction.clearTargets(); for (int target : message.getTargets()) { targetAction.addTarget(game.getEntity(target));