Passed
Pull Request — master (#281)
by Vincent
15:23
created

check(Turn,Castable,BattlefieldCell)   B

Complexity

Conditions 8

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
ccs 13
cts 13
cp 1
rs 7.3333
c 0
b 0
f 0
cc 8
crap 8
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.maps.constant.Direction;
23
import fr.quatrevieux.araknemu.game.fight.castable.Castable;
24
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
25
import fr.quatrevieux.araknemu.game.fight.turn.Turn;
26
import fr.quatrevieux.araknemu.network.game.out.info.Error;
27
import org.checkerframework.checker.nullness.qual.Nullable;
28
29
/**
30
 * Check if spell should be cast in line launch
31
 */
32 1
public final class LineLaunchValidator implements CastConstraintValidator {
33
    @Override
34
    public @Nullable Error validate(Turn turn, Castable castable, BattlefieldCell target) {
35 1
        if (check(turn, castable, target)) {
36 1
            return null;
37
        }
38
39 1
        return Error.cantCastLineLaunch();
40
    }
41
42
    @Override
43
    public boolean check(Turn turn, Castable castable, BattlefieldCell target) {
44 1
        if (!castable.constraints().lineLaunch()) {
45 1
            return true;
46
        }
47
48 1
        if (castable.constraints().range().max() < 2 || target.equals(turn.fighter().cell())) {
49 1
            return true;
50
        }
51
52 1
        final int mapWidth = target.map().dimensions().width();
53
54 1
        for (Direction direction : Direction.values()) {
55 1
            if (!direction.restricted()) {
56 1
                continue;
57
            }
58
59 1
            final int diff = Math.abs(turn.fighter().cell().id() - target.id());
60 1
            final int inc  = direction.nextCellIncrement(mapWidth);
61
62
            // cell ids diff is multiple of inc => cells are in same line
63 1
            if (diff / inc < (mapWidth * 2 + 1) && diff % inc == 0) {
64 1
                return true;
65
            }
66
        }
67
68 1
        return false;
69
    }
70
}
71