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
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
26
|
|
|
|
27
|
|
|
import java.util.ArrayList; |
28
|
|
|
import java.util.Collection; |
29
|
|
|
import java.util.Collections; |
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
|
|
|
@SuppressWarnings("cast.unsafe") // @Nullable cast cause a compiler crash on java 8 |
68
|
|
|
private static <F extends FighterData> Collection<F> resolveFromEffectArea(F caster, BattlefieldCell target, SpellEffect effect) { |
69
|
|
|
// Use lazy instantiation and do not use stream API to optimise memory allocations |
70
|
1 |
|
F firstTarget = null; |
71
|
1 |
|
Collection<F> targets = null; |
72
|
|
|
|
73
|
1 |
|
for (BattlefieldCell cell : effect.area().resolve(target, caster.cell())) { |
74
|
1 |
|
final @Nullable F resolvedTarget = (/*@Nullable*/ F) cell.fighter(); |
75
|
|
|
|
76
|
1 |
|
if (resolvedTarget == null || !effect.target().test(caster, resolvedTarget)) { |
77
|
1 |
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Found the first target |
81
|
1 |
|
if (firstTarget == null) { |
82
|
1 |
|
firstTarget = resolvedTarget; |
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); |
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
|
|
|
|