Passed
Pull Request — master (#281)
by Vincent
16:38
created

resolveFromEffectArea(F,BattlefieldCell,SpellEffect)   B

Complexity

Conditions 7

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 40
ccs 19
cts 19
cp 1
rs 8
c 0
b 0
f 0
cc 7
crap 7
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2022 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable;
21
22
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
23
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
24
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
25
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.Collections;
29
import java.util.Optional;
30
31
/**
32
 * Perform target resolution for a casted effect
33
 */
34
public final class CastTargetResolver {
35
    /**
36
     * Utility class : disable constructor
37
     */
38
    private CastTargetResolver() {}
39
40
    /**
41
     * Resolve targets of an effect
42
     *
43
     * @param caster The action caster
44
     * @param target The target cell
45
     * @param action Action to perform
46
     * @param effect The effect to resolve
47
     *
48
     * @return List of fighters
49
     *
50
     * @see fr.quatrevieux.araknemu.game.spell.effect.target.EffectTarget
51
     */
52
    public static <F extends FighterData> Collection<F> resolveFromEffect(F caster, BattlefieldCell target, Castable action, SpellEffect effect) {
53 1
        if (effect.target().onlyCaster()) {
54 1
            return Collections.singleton(caster);
55
        }
56
57 1
        if (action.constraints().freeCell()) {
58 1
            return Collections.emptyList();
59
        }
60
61 1
        return resolveFromEffectArea(caster, target, effect);
62
    }
63
64
    /**
65
     * Perform resolution from effect target and effect area
66
     */
67
    private static <F extends FighterData> Collection<F> resolveFromEffectArea(F caster, BattlefieldCell target, SpellEffect effect) {
68
        // Use lazy instantiation and do not use stream API to optimise memory allocations
69 1
        F firstTarget = null;
70 1
        Collection<F> targets = null;
71
72 1
        for (BattlefieldCell cell : effect.area().resolve(target, caster.cell())) {
73
            // @todo change fight cell
0 ignored issues
show
introduced by
Comment matches to-do format '(TODO:)|(@todo )'.
Loading history...
74 1
            final Optional<F> resolvedTarget = (Optional<F>) cell.fighter().filter(fighter -> effect.target().test(caster, fighter));
75
76 1
            if (!resolvedTarget.isPresent()) {
77 1
                continue;
78
            }
79
80
            // Found the first target
81 1
            if (firstTarget == null) {
82 1
                firstTarget = resolvedTarget.get();
83 1
                continue;
84
            }
85
86
            // Multiple targets are found : instantiate the collection
87 1
            if (targets == null) {
88 1
                targets = new ArrayList<>();
89 1
                targets.add(firstTarget);
90
            }
91
92 1
            targets.add(resolvedTarget.get());
93 1
        }
94
95
        // There is multiple targets
96 1
        if (targets != null) {
97 1
            return targets;
98
        }
99
100
        // There is only one target : create a singleton
101 1
        if (firstTarget != null) {
102 1
            return Collections.singleton(firstTarget);
103
        }
104
105
        // No targets are resolved
106 1
        return Collections.emptyList();
107
    }
108
}
109