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

coordinate()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
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-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.ai.proxy;
21
22
import fr.arakne.utils.maps.CoordinateCell;
23
import fr.arakne.utils.value.Dimensions;
24
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
25
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldMap;
26
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
27
28
import java.util.Arrays;
29
import java.util.Iterator;
30
import java.util.Optional;
31
import java.util.function.Consumer;
32
33
/**
34
 * Proxy class for redefine fighters and objects positions
35
 *
36
 * Note: this class is immutable
37
 */
38
public final class ProxyBattlefield implements BattlefieldMap {
39
    private final BattlefieldMap map;
40
    private final ProxyCell[] cells;
41
42 1
    public ProxyBattlefield(BattlefieldMap map) {
43 1
        this.map = map;
44 1
        this.cells = null;
45 1
    }
46
47 1
    private ProxyBattlefield(ProxyBattlefield other) {
48 1
        this.map = other.map;
49 1
        this.cells = new ProxyCell[other.map.size()];
50
51 1
        for (int i = 0; i < map.size(); ++i) {
52 1
            this.cells[i] = other.cells == null
53 1
                ? new ProxyCell(map.get(i))
54
                : new ProxyCell(other.cells[i])
55
            ;
56
        }
57 1
    }
58
59
    @Override
60
    public int size() {
61 1
        return map.size();
62
    }
63
64
    @Override
65
    public FightCell get(int id) {
66 1
        if (cells == null) {
67 1
            return map.get(id);
68
        }
69
70 1
        return cells[id];
71
    }
72
73
    @Override
74
    public Dimensions dimensions() {
75 1
        return map.dimensions();
76
    }
77
78
    @Override
79
    public Iterator<FightCell> iterator() {
80 1
        if (cells == null) {
81 1
            return map.iterator();
82
        }
83
84 1
        return Arrays.<FightCell>asList(cells).iterator();
85
    }
86
87
    /**
88
     * Modify the map objects
89
     *
90
     * Usage:
91
     * <pre>{@code
92
     * map.modify(modifier -> {
93
     *     modifier.free(123).setFighter(125, fighter); // Move "fighter" to cell 125
94
     * });
95
     * }</pre>
96
     *
97
     * @param configurator Configuration callback
98
     *
99
     * @return The new map instance
100
     */
101
    public ProxyBattlefield modify(Consumer<Modifier> configurator) {
102 1
        final Modifier modifier = new Modifier(this);
103
104 1
        configurator.accept(modifier);
105
106 1
        return modifier.map;
107
    }
108
109
    private final class ProxyCell implements FightCell {
110
        private final FightCell cell;
111 1
        private boolean free = false;
112 1
        private PassiveFighter fighter = null;
113 1
        private CoordinateCell<FightCell> coordinates = null;
114
115 1
        private ProxyCell(FightCell cell) {
116 1
            this.cell = cell;
117 1
        }
118
119 1
        private ProxyCell(ProxyCell other) {
120 1
            this.cell = other.cell;
121 1
            this.free = other.free;
122 1
            this.fighter = other.fighter;
123 1
        }
124
125
        @Override
126
        public BattlefieldMap map() {
127 1
            return ProxyBattlefield.this;
128
        }
129
130
        @Override
131
        public boolean walkableIgnoreFighter() {
132 1
            return cell.walkableIgnoreFighter();
133
        }
134
135
        @Override
136
        public Optional<PassiveFighter> fighter() {
137 1
            if (free) {
138 1
                return Optional.empty();
139
            }
140
141 1
            if (fighter != null) {
142 1
                return Optional.of(fighter);
143
            }
144
145 1
            return cell.fighter();
146
        }
147
148
        @Override
149
        public void set(PassiveFighter fighter) {
150 1
            throw new UnsupportedOperationException("This is a proxy cell");
151
        }
152
153
        @Override
154
        public void removeFighter() {
155 1
            throw new UnsupportedOperationException("This is a proxy cell");
156
        }
157
158
        @Override
159
        public boolean sightBlocking() {
160 1
            if (free && cell.walkableIgnoreFighter()) {
161 1
                return false;
162
            }
163
164 1
            if (fighter != null) {
165 1
                return true;
166
            }
167
168 1
            return cell.sightBlocking();
169
        }
170
171
        @Override
172
        public int id() {
173 1
            return cell.id();
174
        }
175
176
        @Override
177
        public boolean walkable() {
178 1
            if (free) {
179 1
                return cell.walkableIgnoreFighter();
180
            }
181
182 1
            if (fighter != null) {
183 1
                return false;
184
            }
185
186 1
            return cell.walkable();
187
        }
188
189
        @Override
190
        public CoordinateCell<FightCell> coordinate() {
191 1
            if (coordinates == null) {
192 1
                coordinates = new CoordinateCell<>(this);
193
            }
194
195 1
            return coordinates;
196
        }
197
    }
198
199
    /**
200
     * Builder class for modifying a proxy map
201
     */
202
    public static class Modifier {
203
        private final ProxyBattlefield map;
204
205 1
        public Modifier(ProxyBattlefield map) {
206 1
            this.map = new ProxyBattlefield(map);
207 1
        }
208
209
        /**
210
         * Free a cell
211
         * Calling this method will remove the fighter onb this cell
212
         *
213
         * @param cellId Cell to modify
214
         *
215
         * @return this instance
216
         */
217
        public Modifier free(int cellId) {
218 1
            map.cells[cellId].free = true;
219
220 1
            return this;
221
        }
222
223
        /**
224
         * Get a cell instance
225
         *
226
         * @param cellId The cell to get
227
         */
228
        public FightCell get(int cellId) {
229 1
            return map.cells[cellId];
230
        }
231
232
        /**
233
         * Set a fighter to the given cell
234
         *
235
         * @param cellId Cell to modify
236
         * @param fighter The fighter to cell
237
         *
238
         * @return this instance
239
         */
240
        public Modifier setFighter(int cellId, PassiveFighter fighter) {
241 1
            map.cells[cellId].free = false;
242 1
            map.cells[cellId].fighter = fighter;
243
244 1
            return this;
245
        }
246
    }
247
}
248