use of net.runelite.client.plugins.kourendlibrary.Book in project runelite by runelite.
the class Library method mark.
public synchronized void mark(WorldPoint loc, Book book) {
Bookcase bookcase = byPoint.get(loc);
if (bookcase == null) {
log.debug("Requested non-existent bookcase at {}", loc);
return;
}
if (bookcase.isBookSet()) {
// Check for a mismatch, unless it is now null and had a dark manuscript
if (book != bookcase.getBook() && !(book == null && bookcase.getBook().isDarkManuscript())) {
reset();
}
} else if (state != SolvedState.NO_DATA) {
// We know all of the possible things in this shelf.
if (book != null) {
// Check to see if our guess is wrong
if (!bookcase.getPossibleBooks().contains(book)) {
reset();
}
}
}
// Everything is known, nothing to do
if (state == SolvedState.COMPLETE) {
return;
}
log.info("Setting bookcase {} to {}", bookcase.getIndex(), book);
for (; ; ) {
bookcase.setBook(book);
// Basing the sequences on null is not supported, though possible
if (book == null) {
return;
}
// This is one of the 6 bookcases with 2 ids. Not fully supported.
if (bookcase.getIndex().size() != 1) {
return;
}
int bookcaseIndex = bookcase.getIndex().get(0);
state = SolvedState.INCOMPLETE;
// Map each sequence to the number of bookcases that match the sequence
// return 0 if it is a mismatch.
// Keep in mind that Bookcases with dark manuscripts may be set to null.
int[] certainty = sequences.stream().mapToInt(sequence -> {
int zero = getBookcaseZeroIndexForSequenceWithBook(sequence, bookcaseIndex, book);
int found = 0;
for (int i = 0; i < byIndex.size(); i++) {
int ai = (i + zero) % byIndex.size();
Bookcase iBookcase = byIndex.get(ai);
if (i % step == 0) {
int seqI = i / step;
if (iBookcase.isBookSet() && seqI < sequence.size()) {
Book seqBook = sequence.get(seqI);
boolean isSeqManuscript = seqBook == null || seqBook.isDarkManuscript();
if (!((isSeqManuscript && iBookcase.getBook() == null) || (iBookcase.getBook() == seqBook))) {
log.debug("Bailing @ i={} ai={} {}; {} != {}", i, ai, iBookcase.getIndex(), iBookcase.getBook(), seqBook);
found = 0;
break;
}
found++;
}
} else {
// Only bail if this isn't a double bookcase
if (iBookcase.isBookSet() && iBookcase.getBook() != null && iBookcase.getIndex().size() == 1) {
log.debug("Bailing @ i={} ai={} {}; {} is set", i, ai, iBookcase.getIndex(), iBookcase.getBook());
found = 0;
break;
}
}
}
return found;
}).toArray();
log.info("Certainty is now {}", certainty);
for (Bookcase b : byIndex) {
b.getPossibleBooks().clear();
}
// Write the most likely sequences onto the bookcases
int max = IntStream.of(certainty).max().getAsInt();
// We have books set, but 0 sequences match, Something is wrong, reset.
if (max == 0) {
reset();
continue;
}
IntStream.range(0, sequences.size()).filter(i -> certainty[i] == max).forEach(isequence -> {
List<Book> sequence = sequences.get(isequence);
int zero = getBookcaseZeroIndexForSequenceWithBook(sequence, bookcaseIndex, book);
for (int i = 0; i < byIndex.size(); i++) {
int ai = (i + zero) % byIndex.size();
Bookcase iBookcase = byIndex.get(ai);
if (iBookcase.getBook() == null) {
int iseq = i / step;
if (i % step == 0 && iseq < sequence.size()) {
Book seqBook = sequence.get(iseq);
iBookcase.getPossibleBooks().add(seqBook);
}
}
}
});
if (IntStream.range(0, certainty.length).filter(i -> certainty[i] == max).count() == 1) {
state = SolvedState.COMPLETE;
}
return;
}
}
Aggregations