001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.process;
019
020import baritone.api.behavior.IPathingBehavior;
021import baritone.api.event.events.PathEvent;
022
023/**
024 * A process that can control the PathingBehavior.
025 * <p>
026 * Differences between a baritone process and a behavior:
027 * <ul>
028 * <li>Only one baritone process can be active at a time</li>
029 * <li>PathingBehavior can only be controlled by a process</li>
030 * </ul>
031 * <p>
032 * That's it actually
033 *
034 * @author leijurv
035 */
036public interface IBaritoneProcess {
037
038    /**
039     * Default priority. Most normal processes should have this value.
040     * <p>
041     * Some examples of processes that should have different values might include some kind of automated mob avoidance
042     * that would be temporary and would forcefully take control. Same for something that pauses pathing for auto eat, etc.
043     * <p>
044     * The value is -1 beacuse that's what Impact 4.5's beta auto walk returns and I want to tie with it.
045     */
046    double DEFAULT_PRIORITY = -1;
047
048    /**
049     * Would this process like to be in control?
050     *
051     * @return Whether or not this process would like to be in contorl.
052     */
053    boolean isActive();
054
055    /**
056     * Called when this process is in control of pathing; Returns what Baritone should do.
057     *
058     * @param calcFailed     {@code true} if this specific process was in control last tick,
059     *                       and there was a {@link PathEvent#CALC_FAILED} event last tick
060     * @param isSafeToCancel {@code true} if a {@link PathingCommandType#REQUEST_PAUSE} would happen this tick, and
061     *                       {@link IPathingBehavior} wouldn't actually tick. {@code false} if the PathExecutor reported
062     *                       pausing would be unsafe at the end of the last tick. Effectively "could request cancel or
063     *                       pause and have it happen right away"
064     * @return What the {@link IPathingBehavior} should do
065     */
066    PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel);
067
068    /**
069     * Returns whether or not this process should be treated as "temporary".
070     * <p>
071     * If a process is temporary, it doesn't call {@link #onLostControl} on the processes that aren't execute because of it.
072     * <p>
073     * For example, {@code CombatPauserProcess} and {@code PauseForAutoEatProcess} should return {@code true} always,
074     * and should return {@link #isActive} {@code true} only if there's something in range this tick, or if the player would like
075     * to start eating this tick. {@code PauseForAutoEatProcess} should only actually right click once onTick is called with
076     * {@code isSafeToCancel} true though.
077     *
078     * @return Whether or not if this control is temporary
079     */
080    boolean isTemporary();
081
082    /**
083     * Called if {@link #isActive} returned {@code true}, but another non-temporary
084     * process has control. Effectively the same as cancel. You want control but you
085     * don't get it.
086     */
087    void onLostControl();
088
089    /**
090     * Used to determine which Process gains control if multiple are reporting {@link #isActive()}. The one
091     * that returns the highest value will be given control.
092     *
093     * @return A double representing the priority
094     */
095    default double priority() {
096        return DEFAULT_PRIORITY;
097    }
098
099    /**
100     * Returns a user-friendly name for this process. Suitable for a HUD.
101     *
102     * @return A display name that's suitable for a HUD
103     */
104    default String displayName() {
105        if (!isActive()) {
106            // i love it when impcat's scuffed HUD calls displayName for inactive processes for 1 tick too long
107            // causing NPEs when the displayname relies on fields that become null when inactive
108            return "INACTIVE";
109        }
110        return displayName0();
111    }
112
113    String displayName0();
114}