check(Turn,Castable,BattlefieldCell)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
crap 2
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.validator;
21
22
import fr.arakne.utils.value.Interval;
23
import fr.quatrevieux.araknemu.data.constant.Characteristic;
24
import fr.quatrevieux.araknemu.game.fight.castable.Castable;
25
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
26
import fr.quatrevieux.araknemu.game.fight.turn.Turn;
27
import fr.quatrevieux.araknemu.network.game.out.info.Error;
28
import org.checkerframework.checker.nullness.qual.Nullable;
29
30
/**
31
 * Validate the cast range
32
 */
33 1
public final class RangeValidator implements CastConstraintValidator {
34
    @Override
35
    public boolean check(Turn turn, Castable castable, BattlefieldCell target) {
36 1
        final int distance = turn.fighter().cell().coordinate().distance(target);
37
38 1
        Interval range = castable.constraints().range();
39
40 1
        if (castable.modifiableRange()) {
41 1
            range = range.modify(turn.fighter().characteristics().get(Characteristic.SIGHT_BOOST));
42
        }
43
44 1
        return range.contains(distance);
45
    }
46
47
    @Override
48
    public @Nullable Error validate(Turn turn, Castable castable, BattlefieldCell target) {
49 1
        final int distance = turn.fighter().cell().coordinate().distance(target);
50
51 1
        Interval range = castable.constraints().range();
52
53 1
        if (castable.modifiableRange()) {
54 1
            range = range.modify(turn.fighter().characteristics().get(Characteristic.SIGHT_BOOST));
55
        }
56
57 1
        if (!range.contains(distance)) {
58 1
            return Error.cantCastBadRange(range, distance);
59
        }
60
61 1
        return null;
62
    }
63
}
64