Search in sources :

Example 1 with ITraceEntry

use of fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry in project NoCheatPlus by NoCheatPlus.

the class FightListener method locationTraceChecks.

/**
 * Quick split-off: Checks using a location trace.
 * @param player
 * @param loc
 * @param data
 * @param cc
 * @param damaged
 * @param damagedPlayer
 * @param damagedLoc
 * @param damagedTrace
 * @param tick
 * @param reachEnabled
 * @param directionEnabled
 * @return If to cancel (true) or not (false).
 */
private boolean locationTraceChecks(final Player player, final Location loc, final FightData data, final FightConfig cc, final IPlayerData pData, final Entity damaged, final boolean damagedIsFake, final Location damagedLoc, LocationTrace damagedTrace, final long tick, final long now, final boolean debug, final boolean reachEnabled, final boolean directionEnabled) {
    // TODO: Order / splitting off generic stuff.
    /*
         * TODO: Abstract: interface with common setup/loop/post routine, only
         * pass the ACTIVATED checks on to here (e.g. IFightLoopCheck...
         * loopChecks). Support an arbitrary number of loop checks, special
         * behavior -> interface and/or order within loopChecks.
         */
    boolean cancelled = false;
    // (Might pass generic context to factories, for shared + heavy properties.)
    final ReachContext reachContext = reachEnabled ? reach.getContext(player, loc, damaged, damagedLoc, data, cc) : null;
    final DirectionContext directionContext = directionEnabled ? direction.getContext(player, loc, damaged, damagedIsFake, damagedLoc, data, cc) : null;
    // TODO: Set by latency-window.
    final long traceOldest = tick - cc.loopMaxLatencyTicks;
    // TODO: Iterating direction, which, static/dynamic choice.
    final Iterator<ITraceEntry> traceIt = damagedTrace.maxAgeIterator(traceOldest);
    // No tick with all checks passed.
    boolean violation = true;
    // Passed individually for some tick.
    boolean reachPassed = !reachEnabled;
    // Passed individually for some tick.
    boolean directionPassed = !directionEnabled;
    // TODO: Maintain a latency estimate + max diff and invalidate completely (i.e. iterate from latest NEXT time)], or just max latency.
    // TODO: Consider a max-distance to "now", for fast invalidation.
    long latencyEstimate = -1;
    ITraceEntry successEntry = null;
    while (traceIt.hasNext()) {
        final ITraceEntry entry = traceIt.next();
        // Simplistic just check both until end or hit.
        // TODO: Other default distances/tolerances.
        boolean thisPassed = true;
        if (reachEnabled) {
            if (reach.loopCheck(player, loc, damaged, entry, reachContext, data, cc)) {
                thisPassed = false;
            } else {
                reachPassed = true;
            }
        }
        // TODO: Efficiency: don't check at all, if strict and !thisPassed.
        if (directionEnabled && (reachPassed || !directionPassed)) {
            if (direction.loopCheck(player, loc, damaged, entry, directionContext, data, cc)) {
                thisPassed = false;
            } else {
                directionPassed = true;
            }
        }
        if (thisPassed) {
            // TODO: Log/set estimated latency.
            violation = false;
            latencyEstimate = now - entry.getTime();
            successEntry = entry;
            break;
        }
    }
    // TODO: violation vs. reachPassed + directionPassed (current: fail one = fail all).
    if (reachEnabled) {
        // TODO: Might ignore if already cancelled by mixed/silent cancel.
        if (reach.loopFinish(player, loc, damaged, reachContext, successEntry, violation, data, cc, pData)) {
            cancelled = true;
        }
    }
    if (directionEnabled) {
        // TODO: Might ignore if already cancelled.
        if (direction.loopFinish(player, loc, damaged, directionContext, violation, data, cc)) {
            cancelled = true;
        }
    }
    // TODO: Log exact state, probably record min/max latency (individually).
    if (debug && latencyEstimate >= 0) {
        // FCFS rather, at present.
        debug(player, "Latency estimate: " + latencyEstimate + " ms.");
    }
    return cancelled;
}
Also used : ITraceEntry(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry)

Example 2 with ITraceEntry

use of fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry in project NoCheatPlus by NoCheatPlus.

the class TestLocationTrace method testIteratorSizeAndOrder.

private void testIteratorSizeAndOrder(LocationTrace trace, int expectedSize) {
    int size = trace.size();
    if (size != expectedSize) {
        fail("LocationTrace size differs from expected. Expect " + expectedSize + ", got instead: " + size);
    }
    TraceIterator[] iterators = new TraceIterator[] { trace.oldestIterator(), trace.latestIterator(), // Asserts entries to start at time >= 0.
    trace.maxAgeIterator(0) };
    String[] iteratorNames = new String[] { "oldest", "latest", "maxAge" };
    int[] increments = new int[] { 1, -1, 1 };
    for (int i = 0; i < iterators.length; i++) {
        int n = 0;
        TraceIterator it = iterators[i];
        ITraceEntry last = null;
        ITraceEntry current = null;
        while (it.hasNext()) {
            current = it.next();
            n++;
            if (n > size) {
                fail("Iterator (" + iteratorNames[i] + ") iterates too long.");
            }
            if (last != null) {
                if (current.getTime() - last.getTime() != increments[i]) {
                    fail("Bad time increment (" + iteratorNames[i] + "). Expected " + increments[i] + ", got instead: " + (current.getTime() - last.getTime()));
                }
            }
            last = current;
        }
        if (n != size) {
            fail("Iterator (" + iteratorNames[i] + ") should have " + size + " elements, found instead: " + n);
        }
    }
}
Also used : ITraceEntry(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry) TraceIterator(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.TraceIterator)

Example 3 with ITraceEntry

use of fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry in project NoCheatPlus by NoCheatPlus.

the class TestLocationTrace method testMaxAgeIterator.

@Test
public void testMaxAgeIterator() {
    int size = 80;
    LocationTrace trace = new LocationTrace(size, size, pool);
    // Adding up to size elements.
    for (int i = 0; i < size; i++) {
        trace.addEntry(i, i, i, i, 0, 0);
    }
    for (int i = 0; i < size; i++) {
        Iterator<ITraceEntry> it = trace.maxAgeIterator(i);
        long got = it.next().getTime();
        if (got != i) {
            fail("Bad entry point for iterator (maxAge), expected " + i + ", got instead: " + got);
        }
        int n = 1;
        while (it.hasNext()) {
            it.next();
            n++;
        }
        if (n != size - i) {
            fail("Bad number of elements for iterator (maxAge), expected " + (size - i) + ", got instead: " + n);
        }
    }
}
Also used : ITraceEntry(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry) LocationTrace(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace) Test(org.junit.Test)

Example 4 with ITraceEntry

use of fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry in project NoCheatPlus by NoCheatPlus.

the class TestLocationTrace method testMaxAgeFirstElementAnyway.

@Test
public void testMaxAgeFirstElementAnyway() {
    int size = 80;
    LocationTrace trace = new LocationTrace(size, size, pool);
    trace.addEntry(0, 0, 0, 0, 0, 0);
    if (!trace.maxAgeIterator(1000).hasNext()) {
        fail("Expect iterator (maxAge) to always contain the latest element.");
    }
    trace.addEntry(1, 0, 0, 0, 0, 0);
    final Iterator<ITraceEntry> it = trace.maxAgeIterator(2);
    if (!it.hasNext()) {
        fail("Expect iterator (maxAge) to always contain the latest element.");
    }
    it.next();
    if (it.hasNext()) {
        fail("Expect iterator (maxAge) to have only the latest element for all out of range entries.");
    }
}
Also used : ITraceEntry(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry) LocationTrace(fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace) Test(org.junit.Test)

Aggregations

ITraceEntry (fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.ITraceEntry)4 LocationTrace (fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace)2 Test (org.junit.Test)2 TraceIterator (fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.TraceIterator)1