RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
MobList.java
1package com.osroyale.game.world.entity;
2
3import com.osroyale.game.world.entity.mob.Mob;
4import com.osroyale.util.RandomUtils;
5
6import java.util.*;
7import java.util.function.Consumer;
8import java.util.function.Predicate;
9import java.util.stream.Collectors;
10import java.util.stream.IntStream;
11import java.util.stream.Stream;
12import java.util.stream.StreamSupport;
13
14import static com.google.common.base.Preconditions.*;
15
26public final class MobList<E extends Mob> implements Iterable<E> {
27
36 public static final class EntityListIterator<E extends Mob> implements Iterator<E> {
37
39 private final MobList<E> list;
40
42 private int curr;
43
45 private int prev = -1;
46
53 EntityListIterator(MobList<E> list) {
54 this.list = list;
55 }
56
57 @Override
58 public boolean hasNext() {
59 return curr < list.capacity() && skipNullIndexes();
60 }
61
62 @Override
63 public E next() {
64 skipNullIndexes();
65 checkPositionIndex(curr, list.capacity(), "No elements left");
66 E entity = list.element(curr);
67 prev = curr++;
68 return entity;
69 }
70
71 @Override
72 public void remove() {
73 checkState(prev != -1, "remove() can only be called once after each call to next()");
74
75 list.remove(list.get(prev));
76 prev = -1;
77 }
78
86 private boolean skipNullIndexes() {
87 while (list.get(curr) == null) {
88 if (++curr >= list.capacity()) {
89 return false;
90 }
91 }
92 return true;
93 }
94 }
95
97 private final E[] entities;
98
100 private final TreeSet<Integer> indices;
101
103 private int size;
104
106 private int largestIndex;
107
113 @SuppressWarnings("unchecked")
114 public MobList(int capacity) {
115 entities = (E[]) new Mob[++capacity];
116 Stream<Integer> indexStream = IntStream.rangeClosed(1, capacity).boxed();
117 indices = new TreeSet<>(indexStream.collect(Collectors.toList()));
118 }
119
120 @Override
121 public Iterator<E> iterator() {
122 return new EntityListIterator<>(this);
123 }
124
126 public void shuffle() {
127 if (size < 2) {
128 return;
129 }
130
131 for (int index = largestIndex; index > 1; index--) {
132 int first = index - 1;
133 int second = RandomUtils.inclusive(1, index);
134 if (first == second) continue;
135
136 E tmp = entities[first];
137 entities[first] = entities[second];
138 entities[second] = tmp;
139
140 if (entities[first] != null) {
141 entities[first].setListIndex(first);
142 }
143
144 if (entities[second] != null) {
145 entities[second].setListIndex(second);
146 }
147
148 }
149 }
150
161 @Override
162 public void forEach(Consumer<? super E> action) {
163 int index = 0;
164 for (E e : entities) {
165 if (e == null) continue;
166 action.accept(e);
167 if (++index >= size) return;
168 }
169 }
170
178 public Optional<E> findFirst(Predicate<? super E> filter) {
179 for (E e : this) {
180 if (filter.test(e)) {
181 return Optional.of(e);
182 }
183 }
184 return Optional.empty();
185 }
186
194 public Optional<E> findLast(Predicate<? super E> filter) {
195 for (int index = capacity(); index > 1; index--) {
196 E entity = entities[index];
197 if (entity == null) {
198 continue;
199 }
200 if (filter.test(entity)) {
201 return Optional.of(entity);
202 }
203 }
204 return Optional.empty();
205 }
206
213 public List<E> findAll(Predicate<? super E> filter) {
214 List<E> list = new ArrayList<>();
215 for (E e : this) {
216 if (filter.test(e)) {
217 list.add(e);
218 }
219 }
220 return list;
221 }
222
229 @Override
230 public Spliterator<E> spliterator() {
231 return Spliterators.spliterator(entities, Spliterator.ORDERED | Spliterator.DISTINCT);
232 }
233
240 public boolean add(E entity) {
241 Integer index = indices.pollFirst();
242
243 if (index == null || index == capacity()) {
244 return false;
245 }
246
247 if (index > largestIndex) {
248 largestIndex = index;
249 }
250
251 entities[index] = entity;
252 entity.setIndex(index);
253 entity.setListIndex(index);
254 size++;
255 return true;
256 }
257
264 public boolean remove(E entity) {
265 checkArgument(entity.getListIndex() != -1, "index == -1");
266 indices.add(entity.getListIndex());
267 entities[entity.getListIndex()] = null;
268 size--;
269 return true;
270 }
271
279 public void remove(int index) {
280 remove(entities[index]);
281 }
282
289 public E get(int index) {
290 return entities[index];
291 }
292
301 public E element(int index) {
302 E entity = get(index);
303 checkArgument(entity != null, "index -> null EntityNode");
304 return entity;
305 }
306
314 public boolean contains(E entity) {
315 return entity != null && entity.equals(get(entity.getListIndex()));
316 }
317
321 public boolean isFull() {
322 return size() == capacity();
323 }
324
328 public boolean isEmpty() {
329 return size() == 0;
330 }
331
335 public int size() {
336 return size;
337 }
338
342 public int remaining() {
343 return capacity() - size();
344 }
345
349 public int capacity() {
350 return entities.length;
351 }
352
359 public E[] toArray() {
360 return Arrays.copyOf(entities, entities.length);
361 }
362
366 public void clear() {
367 forEach(this::remove);
368 }
369
374 public Stream<E> stream() {
375 return StreamSupport.stream(spliterator(), false).filter(Objects::nonNull);
376 }
377
382 public Stream<E> parallelStream() {
383 Spliterator<E> split = Spliterators.spliterator(entities, spliterator().characteristics() | Spliterator.IMMUTABLE);
384 return StreamSupport.stream(split, true).filter(Objects::nonNull);
385 }
386}
void forEach(Consumer<? super E > action)
Definition MobList.java:162
Optional< E > findFirst(Predicate<? super E > filter)
Definition MobList.java:178
Optional< E > findLast(Predicate<? super E > filter)
Definition MobList.java:194
List< E > findAll(Predicate<? super E > filter)
Definition MobList.java:213