fr.quatrevieux.araknemu.game.spell.effect.area.LineArea   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 54
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addCells(Set,C,Direction,int) 0 22 5
A resolve(C,C) 0 13 2
A LineArea(EffectArea) 0 2 1
A type() 0 3 1
A size() 0 3 1
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.spell.effect.area;
21
22
import fr.arakne.utils.maps.CoordinateCell;
23
import fr.arakne.utils.maps.DofusMap;
24
import fr.arakne.utils.maps.MapCell;
25
import fr.arakne.utils.maps.constant.Direction;
26
import fr.quatrevieux.araknemu.data.value.EffectArea;
27
28
import java.util.Collections;
29
import java.util.HashSet;
30
import java.util.Set;
31
32
/**
33
 * Resolve in line area
34
 */
35
public final class LineArea implements SpellEffectArea {
36
    private final EffectArea area;
37
38 1
    public LineArea(EffectArea area) {
39 1
        this.area = area;
40 1
    }
41
42
    @Override
43
    public <C extends MapCell<C>> Set<C> resolve(C target, C source) {
44 1
        if (area.size() == 0) {
45 1
            return Collections.singleton(target);
46
        }
47
48 1
        final Direction direction = source.coordinate().directionTo(target.coordinate());
49 1
        final Set<C> cells = new HashSet<>(area.size() + 1);
50
51 1
        cells.add(target);
52 1
        addCells(cells, target, direction, area.size());
53
54 1
        return cells;
55
    }
56
57
    @Override
58
    public EffectArea.Type type() {
59 1
        return EffectArea.Type.LINE;
60
    }
61
62
    @Override
63
    public int size() {
64 1
        return area.size();
65
    }
66
67
    static <C extends MapCell<C>> void addCells(Set<C> cells, C start, Direction direction, int size) {
68 1
        final DofusMap<C> map = start.map();
69 1
        final int inc = direction.nextCellIncrement(map.dimensions().width());
70
71 1
        CoordinateCell<C> last = start.coordinate();
72
73 1
        for (int i = 0; i < size; ++i) {
74 1
            final int cellId = last.id() + inc;
75
76 1
            if (cellId < 0 || cellId >= map.size()) {
77 1
                break;
78
            }
79
80 1
            final CoordinateCell<C> next = map.get(cellId).coordinate();
81
82
            // The next cell is out of the direction
83 1
            if (last.directionTo(next) != direction) {
84 1
                break;
85
            }
86
87 1
            cells.add(next.cell());
88 1
            last = next;
89
        }
90 1
    }
91
}
92