Test Failed
Branch master (3c0c2d)
by Vincent
12:24
created

AIHelper(AI)   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 1
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-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.ai.util;
21
22
import fr.quatrevieux.araknemu.game.fight.ai.AI;
23
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
24
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
25
26
import java.util.function.Function;
27
28
/**
29
 * Utility class for perform common AI operations on the current fighter
30
 *
31
 * @see AI#helper()
32
 */
33
public final class AIHelper {
34
    private final AI ai;
35
    private final CellsHelper cells;
36
    private final SpellsHelper spells;
37
    private final FightersHelper enemies;
38
    private final FightersHelper allies;
39
40 1
    public AIHelper(AI ai) {
41 1
        this.ai = ai;
42
43 1
        this.cells = new CellsHelper(ai);
44 1
        this.spells = new SpellsHelper(this, ai);
45 1
        this.enemies = new FightersHelper(this, ai, fighter -> !fighter.team().equals(ai.fighter().team()));
46 1
        this.allies = new FightersHelper(this, ai, fighter -> !fighter.equals(ai.fighter()) && fighter.team().equals(ai.fighter().team()));
47 1
    }
48
49
    /**
50
     * @return The spells helper
51
     */
52
    public SpellsHelper spells() {
53 1
        return spells;
54
    }
55
56
    /**
57
     * @return The cells helper
58
     */
59
    public CellsHelper cells() {
60 1
        return cells;
61
    }
62
63
    /**
64
     * @return Get the fighters helper for enemies
65
     */
66
    public FightersHelper enemies() {
67 1
        return enemies;
68
    }
69
70
    /**
71
     * @return Get the fighters helper for allies
72
     */
73
    public FightersHelper allies() {
74 1
        return allies;
75
    }
76
77
    /**
78
     * @return The current number of movement points
79
     */
80
    public int movementPoints() {
81 1
        return ai.turn().points().movementPoints();
82
    }
83
84
    /**
85
     * @return The current number of action points
86
     */
87
    public int actionPoints() {
88 1
        return ai.turn().points().actionPoints();
89
    }
90
91
    /**
92
     * Does the fighter has at least 1 AP
93
     *
94
     * @return true if has at least 1 AP
95
     */
96
    public boolean hasActionPoints() {
97 1
        return ai.turn().points().actionPoints() >= 1;
98
    }
99
100
    /**
101
     * Does the fighter has at least 1 MP
102
     *
103
     * @return true if has at least 1 MP
104
     */
105
    public boolean hasMovementPoints() {
106 1
        return ai.turn().points().movementPoints() >= 1;
107
    }
108
109
    /**
110
     * Check if the current fighter has movement points, and he's not blocked by enemies on adjacent cells
111
     */
112
    public boolean canMove() {
113 1
        return hasMovementPoints() && cells().adjacent().anyMatch(FightCell::walkable);
114
    }
115
116
    /**
117
     * Check if the current fighter can cast a spell
118
     * Return true only if the fighter has at least one available spell
119
     */
120
    public boolean canCast() {
121 1
        return hasActionPoints() && spells().hasAvailable();
122
    }
123
124
    /**
125
     * Simulate a movement to a given cell by changing the current cell of the fighter
126
     *
127
     * <pre>{@code
128
     * ai.helper().simulateMove(newCell, fighter -> {
129
     *     // fighter is on "newCell"
130
     *     return performSimulation(fighter);
131
     * });
132
     * }</pre>
133
     *
134
     * @param cell The new cell
135
     * @param task Action to perform on the moved fighter. It takes as argument the fighter, and returns the action result
136
     *
137
     * @param <R> The action result type
138
     *
139
     * @return The action result
140
     */
141
    public <R> R simulateMove(FightCell cell, Function<ActiveFighter, R> task) {
142 1
        final ActiveFighter fighter = ai.fighter();
143 1
        final FightCell currentCell = fighter.cell();
144
145
        try {
146 1
            fighter.move(cell);
147
148 1
            return task.apply(fighter);
149
        } finally {
150 1
            fighter.move(currentCell);
151
        }
152
    }
153
}
154