Passed
Pull Request — master (#229)
by Vincent
11:03
created

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