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.monster; |
21
|
|
|
|
22
|
|
|
import fr.arakne.utils.value.Interval; |
23
|
|
|
import fr.arakne.utils.value.helper.RandomUtil; |
24
|
|
|
|
25
|
|
|
import java.util.List; |
26
|
|
|
import java.util.NoSuchElementException; |
27
|
|
|
import java.util.stream.Collectors; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set of grades for a monster template |
31
|
|
|
*/ |
32
|
|
|
public final class GradeSet { |
33
|
1 |
|
private static final RandomUtil RANDOM = RandomUtil.createShared(); |
34
|
|
|
|
35
|
|
|
private final List<Monster> grades; |
36
|
|
|
|
37
|
1 |
|
GradeSet(List<Monster> grades) { |
38
|
1 |
|
this.grades = grades; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get all available grades |
43
|
|
|
* The grades are sorted by level (lower to higher level) |
44
|
|
|
*/ |
45
|
|
|
public List<Monster> all() { |
46
|
1 |
|
return grades; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get one grade from the GradeSet |
51
|
|
|
* @param i The grade to get from 1 to 6 |
52
|
|
|
*/ |
53
|
|
|
public Monster get(int i) { |
54
|
1 |
|
return grades.get(i - 1); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get all grades that contained into the given level interval |
59
|
|
|
* The interval is inclusive (min <= grade level <= max) |
60
|
|
|
* |
61
|
|
|
* If the interval is larger than grade levels interval, all grades are returned |
62
|
|
|
* If the interval is disjoint with grade levels, an empty list is returned |
63
|
|
|
*/ |
64
|
|
|
public List<Monster> in(Interval levels) { |
65
|
|
|
// All levels are requested |
66
|
1 |
|
if (levels.min() == 1 && levels.max() == Integer.MAX_VALUE) { |
67
|
1 |
|
return grades; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return grades.stream() |
71
|
1 |
|
.filter(monster -> levels.contains(monster.level())) |
72
|
1 |
|
.collect(Collectors.toList()) |
73
|
|
|
; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get one random grade extracted from matching grades into levels interval (including) |
78
|
|
|
* |
79
|
|
|
* @see GradeSet#in(Interval) |
80
|
|
|
* |
81
|
|
|
* @throws NoSuchElementException When levels interval is disjoint with grades levels |
82
|
|
|
*/ |
83
|
|
|
public Monster random(Interval levels) { |
84
|
1 |
|
final List<Monster> matching = in(levels); |
85
|
|
|
|
86
|
1 |
|
switch (matching.size()) { |
87
|
|
|
case 0: |
88
|
1 |
|
throw new NoSuchElementException("Cannot found any valid grade for monster " + grades.get(0).id()); |
89
|
|
|
|
90
|
|
|
case 1: |
91
|
1 |
|
return matching.get(0); |
92
|
|
|
|
93
|
|
|
default: |
94
|
1 |
|
return RANDOM.of(matching); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|