Search in sources :

Example 1 with Book

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;
    }
}
Also used : IntStream(java.util.stream.IntStream) WorldPoint(net.runelite.api.coords.WorldPoint) Arrays(java.util.Arrays) Getter(lombok.Getter) Book(net.runelite.client.plugins.kourendlibrary.Book) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Map(java.util.Map) Collections(java.util.Collections) Book(net.runelite.client.plugins.kourendlibrary.Book) WorldPoint(net.runelite.api.coords.WorldPoint)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 IntStream (java.util.stream.IntStream)1 Singleton (javax.inject.Singleton)1 Getter (lombok.Getter)1 Slf4j (lombok.extern.slf4j.Slf4j)1 WorldPoint (net.runelite.api.coords.WorldPoint)1 Book (net.runelite.client.plugins.kourendlibrary.Book)1