import API
import time
import math
import heapq
import re
import os

# =============================================================================
# SL_Lumberjack.py  -  Shattered Legacy: chop every tree around you
# Stand in a forest and press Start. It chops each tree in reach until the
# server says there's not enough wood, then walks to the next stand of trees.
# Your axe has to be in your hands (the server requires it); the script equips
# a hatchet or axe from your pack when needed.
#
# Wood lives in 4x3 tile "groves" (one tree's wood runs out for every tree in
# that little block). Groves never change wood on Shattered Legacy, so it can
# steer you toward the rare woods your Lumberjacking can actually pull.
#
# Control window: Start / Pause / Stop, Set Storage (a chest or crate to unload
# into), Set Satchel, Make boards, Survey, Go to grove, and live status. Logs go
# straight into your Foresters' Lumber Satchel on this shard. When you get heavy
# it can turn logs into boards (lighter) and walk to your storage to unload.
#
# Forester's Logbook: read at Start, so it knows which woods and groves you have
# logged. A rare wood (Ironwood and up) you have never found is worth Foresters'
# Standing and Timber Tokens once reported, so it is steered toward first.
#
# Walks through teleporters on long trips (it shares the teleporters SL_Mining
# has learned). Writes SL_Lumberjack.log next to the script.
#
# Set DIAGNOSE = True for one run to list every axe it can see.
# =============================================================================

# ---- CONFIG -----------------------------------------------------------------
RADIUS          = 2         # chop trees this far from where you stand (the server allows 2)
SWING_DELAY     = 4.0       # longest to wait for a chop's result (~1.6 s + lag)
MAX_SWINGS_TREE = 25        # safety cap per tree (misses included)
WEIGHT_STOP     = 90        # % of max weight -> stop
LOOP_FOREVER    = False     # True = wait for regrowth and start over when nothing is left
RESPAWN_WAIT    = 1500      # seconds to wait for regrowth (groves regrow in 20-30 minutes)
DIAGNOSE        = False

# moving to the next trees
WANDER          = True      # walk to more trees when these are done (window checkbox)
WANDER_RANGE    = 20        # never go further than this from where you started
MAX_MOVES       = 40        # stop after this many moves (0 = no limit)
MIN_FRESH       = 1         # a spot needs this many unchopped trees in reach
RETURN_TO_START = True      # with LOOP_FOREVER, walk back to start before waiting
HUE_DEST        = 88        # blue: where it is walking to
MOVE_DEBUG      = True      # print what the mover is doing in the journal

# how it picks the next spot (higher weight = matters more)
LOOKAHEAD       = 6         # also count trees this far past a spot
W_FRESH         = 2.0       # per unchopped tree in reach of the spot
W_AHEAD         = 0.25      # per tree in the area beyond the spot
W_DIST          = 0.5       # penalty per tile walked
W_HEADING       = 2.0       # bonus for keeping the same direction as last move
BANK_MEMORY_MIN = 25        # remember chopped-out groves this long, even across runs

# tree highlighting (TazUO MarkTile). Hues are standard UO hue numbers.
SHOW_MARKS      = True
HUE_ACTIVE      = 53        # yellow: tree being chopped right now
HUE_WOOD        = 68        # green (ordinary wood); known colored groves use the wood's color
HUE_DRY         = 33        # red: chopped out
SHOW_PROGRESS   = True      # "Tree 3/7" over your head after each tree
MARK_SECONDS    = 30        # green/red marks fade after this many seconds (0 = keep)
END_LINGER      = 5         # seconds marks stay up after the script finishes

# unloading
GUI             = True      # show the control window (False = start-and-go mode)
MAKE_BOARDS     = False     # turn logs into boards when heavy (window checkbox). Boards weigh
                            # half as much, but Fletching on this shard uses logs.
AUTO_UNLOAD     = True      # walk to your storage container when heavy (needs Set Storage)
UNLOAD_AT       = 75        # % of max weight -> stash / make boards / unload
STORAGE_STAND   = 1         # how close to stand to the storage container
TRIP_MAX_DIST   = 150       # refuse a storage container farther than this
EMPTY_SATCHEL   = True      # at storage, also empty the lumber satchel
STORE_BONUS     = True      # at storage, also drop bark fragments, amber, fungi, switches...
HOP             = 12        # long walks are split into hops this long
CRUMB_STEP      = 5         # remember a walked tile every this many tiles
MAX_CRUMBS      = 600       # walked tiles remembered per character
POWER_THROUGH   = False     # keep chopping when heavy; only unload when the game says you're
                            # full (window checkbox). Walking overweight costs stamina.
LOG_GRAPHICS    = [0x1BDD, 0x1BDE, 0x1BDF, 0x1BE0, 0x1BE1, 0x1BE2]
BOARD_GRAPHICS  = [0x1BD7, 0x1BD8, 0x1BD9, 0x1BDA, 0x1BDB, 0x1BDC]
# bark fragment, parasitic plant, luminescent fungi, switch, brilliant amber
BONUS_GRAPHICS  = [0x318F, 0x3190, 0x3191, 0x2F5F, 0x3199]
STASH_EXTRA     = []        # other graphics to put in the satchel / storage

# axes, and making new hatchets (Tinkering 30+, 4 iron ingots each)
MAKE_TOOLS      = True      # craft hatchets when you have no axe left
MAKE_COUNT      = 2         # how many to make each time
KEEP_TINKER_SPARE = True    # also make a spare set of tinker's tools before the last one breaks
TOOL_REST       = 120       # seconds to set aside an axe that stops working (doubles if it fails again)
INGOT_GRAPHICS  = [0x1BEF, 0x1BF0, 0x1BF1, 0x1BF2, 0x1BF3, 0x1BF4]

# log file: everything the script says and does, for troubleshooting
LOG_TO_FILE     = True      # writes SL_Lumberjack.log next to the script
LOG_MAX_KB      = 2048      # then starts over (the old one is kept as SL_Lumberjack.log.old)

# survey: grove map + Forester's Logbook
SEEK_GROVES     = True      # steer toward colored woods you can chop (window checkbox)
W_GROVE         = 1.5       # bonus per tree in a colored grove you can chop
W_NEWGROVE      = 1.0       # extra for a grove your logbook doesn't have yet
W_NEWWOOD       = 6.0       # extra for a rare wood you have never found (a discovery to report)
NEW_GROVE_DIST  = 15        # the logbook counts a grove as new this far from a logged one
SURVEY_RANGE    = 40        # how far Survey looks
SURVEY_SHOW     = 6         # groves Survey lists
SURVEY_MARKS    = 60        # seconds Survey's colored marks stay up
SHOW_GROVE_MAP  = False     # tint trees in groves you know, in the wood's color (window checkbox)
GROVE_MAP_RANGE = 16        # tiles around you to tint
GROVE_MAP_BATCH = 25        # new trees checked per step
AUTO_RELOCATE   = 4         # when the area is chopped out, walk to this many surveyed groves (0 = off)
LOGBOOK_GRAPHIC = 0x1C11
SATCHEL_GRAPHIC = 0xA272    # Foresters' Lumber Satchel (same art as the ore satchel): found by name

AXE_GRAPHICS    = [0x0F43, 0x0F44, 0x0F49, 0x0F4A, 0x0F47, 0x0F48, 0x0F4B, 0x0F4C,
                   0x0F45, 0x0F46, 0x13FB, 0x13FA, 0x1443, 0x1442, 0x13B0, 0x13AF,
                   0x2D28, 0x2D34, 0x48B2, 0x48B3, 0x48B0, 0x48B1]
HATCHET_GRAPHICS = [0x0F43, 0x0F44]
HAND_LAYERS     = ["TwoHanded", "OneHanded"]
# -----------------------------------------------------------------------------

EMPTY_MSGS = ["not enough wood here", "Someone has gotten to the"]
MISS_MSGS  = ["You hack at the tree for a while"]       # a miss, but the tree still has wood
BAD_MSGS   = ["You can't use an axe on that", "You can't use a bladed item",
              "That is too far away", "Target cannot be seen", "You have no line of sight",
              "You have moved too far away", "You can't reach"]
FAR_MSGS   = ["That is too far away", "Target cannot be seen", "You have no line of sight",
              "You have moved too far away", "You can't reach"]
FULL_MSGS  = ["You can't place any wood", "You are overloaded", "cannot hold more weight",
              "Your backpack is full", "You can't carry"]
GOT_MSGS   = ["You chop some", "You put some logs", "unusual timber", "The bark crumbles"]
BUSY_MSGS  = ["You must wait to perform another action", "You are already"]
BROKE_MSG  = ["You broke your axe", "worn out"]
EQUIP_MSG  = "must be equipped"

# Every tree the shard's Lumberjacking accepts (ModernUO Lumberjacking.cs m_TreeTiles).
# The first group are trunks; the second includes the leaves on top of them.
TRUNK_TILES = set([
    0x0CCA, 0x0CCB, 0x0CCC, 0x0CCD, 0x0CD0, 0x0CD3, 0x0CD6, 0x0CD8,
    0x0CDA, 0x0CDD, 0x0CE0, 0x0CE3, 0x0CE6, 0x0CF8, 0x0CFB, 0x0CFE,
    0x0D01, 0x0D41, 0x0D42, 0x0D43, 0x0D44, 0x0D57, 0x0D58, 0x0D59,
    0x0D5A, 0x0D5B, 0x0D6E, 0x0D6F, 0x0D70, 0x0D71, 0x0D72, 0x0D84,
    0x0D85, 0x0D86, 0x12B5, 0x12B6, 0x12B7, 0x12B8, 0x12B9, 0x12BA,
    0x12BB, 0x12BC, 0x12BD])
TREE_TILES = TRUNK_TILES | set([
    0x0CCE, 0x0CCF, 0x0CD1, 0x0CD2, 0x0CD4, 0x0CD5, 0x0CD7, 0x0CD9,
    0x0CDB, 0x0CDC, 0x0CDE, 0x0CDF, 0x0CE1, 0x0CE2, 0x0CE4, 0x0CE5,
    0x0CE7, 0x0CE8, 0x0CF9, 0x0CFA, 0x0CFC, 0x0CFD, 0x0CFF, 0x0D00,
    0x0D02, 0x0D03, 0x0D45, 0x0D46, 0x0D47, 0x0D48, 0x0D49, 0x0D4A,
    0x0D4B, 0x0D4C, 0x0D4D, 0x0D4E, 0x0D4F, 0x0D50, 0x0D51, 0x0D52,
    0x0D53, 0x0D5C, 0x0D5D, 0x0D5E, 0x0D5F, 0x0D60, 0x0D61, 0x0D62,
    0x0D63, 0x0D64, 0x0D65, 0x0D66, 0x0D67, 0x0D68, 0x0D69, 0x0D73,
    0x0D74, 0x0D75, 0x0D76, 0x0D77, 0x0D78, 0x0D79, 0x0D7A, 0x0D7B,
    0x0D7C, 0x0D7D, 0x0D7E, 0x0D7F, 0x0D87, 0x0D88, 0x0D89, 0x0D8A,
    0x0D8B, 0x0D8C, 0x0D8D, 0x0D8E, 0x0D8F, 0x0D90, 0x0D95, 0x0D96,
    0x0D97, 0x0D99, 0x0D9A, 0x0D9B, 0x0D9D, 0x0D9E, 0x0D9F, 0x0DA1,
    0x0DA2, 0x0DA3, 0x0DA5, 0x0DA6, 0x0DA7, 0x0DA9, 0x0DAA, 0x0DAB,
    0x12BE, 0x12BF, 0x12C0, 0x12C1, 0x12C2, 0x12C3, 0x12C4, 0x12C5,
    0x12C6, 0x12C7])


# ---- tree highlighting ------------------------------------------------------------
MARKED = {}                 # (x, y) -> time the mark expires (None = while active)
MARK_VAR = "SL_Lumber_Marks"


def _save_marks():
    # remember what is on the ground so the next run can clean it up
    try:
        API.SetSharedVar(MARK_VAR, ";".join("{},{}".format(x, y) for x, y in
                                            list(MARKED) + list(GROVE_MAP["tiles"])))
    except Exception:
        pass


def _unmark(x, y):
    MARKED.pop((x, y), None)
    hue = GROVE_MAP["tiles"].get((x, y)) if GROVE_MAP["on"] else None
    try:
        if hue:
            API.MarkTile(x, y, hue)     # back to the grove-map tint
        else:
            API.RemoveMarkedTile(x, y)
    except Exception:
        pass


def mark(dx, dy, hue, lasting=True):
    if not opt("marks"):
        return
    x = API.Player.X + dx
    y = API.Player.Y + dy
    try:
        API.MarkTile(x, y, hue)
    except Exception:
        return
    MARKED[(x, y)] = (time.time() + MARK_SECONDS) if (lasting and MARK_SECONDS > 0) else None
    _save_marks()


def mark_at(x, y, hue, secs):
    if not opt("marks"):
        return
    try:
        API.MarkTile(x, y, hue)
    except Exception:
        return
    MARKED[(x, y)] = time.time() + secs
    _save_marks()


def expire_marks():
    now = time.time()
    if CTRL.get("arrow_until") and now > CTRL["arrow_until"]:
        CTRL["arrow_until"] = 0
        try:
            API.TrackingArrow(-1, -1)
        except Exception:
            pass
    gone = [k for k, t in MARKED.items() if t is not None and t <= now]
    for x, y in gone:
        _unmark(x, y)
    if gone:
        _save_marks()


def clear_marks():
    for x, y in list(MARKED):
        _unmark(x, y)
    clear_grove_map()
    _save_marks()


def clear_leftover_marks():
    # marks from a previous run that stopped before it could clean up
    try:
        saved = API.GetSharedVar(MARK_VAR) or ""
    except Exception:
        saved = ""
    for pair in str(saved).split(";"):
        if "," in pair:
            try:
                x, y = pair.split(",")
                API.RemoveMarkedTile(int(x), int(y))
            except Exception:
                pass
    try:
        API.SetSharedVar(MARK_VAR, "")
    except Exception:
        pass


# ---- log file -------------------------------------------------------------------
LOG = {"path": None, "failed": False}


def _log_path():
    cands = []
    try:
        cands.append(os.path.dirname(os.path.abspath(__file__)))
    except Exception:
        pass
    try:
        cwd = os.getcwd()
        cands += [os.path.join(cwd, "LegionScripts"), cwd]
    except Exception:
        pass
    for d in cands:
        if d and os.path.isdir(d) and os.path.exists(os.path.join(d, "SL_Lumberjack.py")):
            return os.path.join(d, "SL_Lumberjack.log")
    for d in cands:
        if d and os.path.isdir(d):
            return os.path.join(d, "SL_Lumberjack.log")
    return "SL_Lumberjack.log"


def log(msg):
    if not LOG_TO_FILE or LOG["failed"]:
        return
    try:
        if LOG["path"] is None:
            LOG["path"] = _log_path()
            try:
                if os.path.getsize(LOG["path"]) > LOG_MAX_KB * 1024:
                    old = LOG["path"] + ".old"
                    if os.path.exists(old):
                        os.remove(old)
                    os.rename(LOG["path"], old)
            except Exception:
                pass
        try:
            where = "{} {},{}".format(API.GetMap(), API.Player.X, API.Player.Y)
        except Exception:
            where = "?"
        with open(LOG["path"], "a") as f:
            f.write("{} [{}] {}\n".format(time.strftime("%Y-%m-%d %H:%M:%S"), where, msg))
    except Exception as e:
        LOG["failed"] = True
        try:
            API.SysMsg("[Lumber] Can't write the log file: {}".format(e), 33)
        except Exception:
            pass


def sysmsg(msg, hue=946):
    log(msg)
    API.SysMsg(msg, hue)


# ---- run control (driven by the window) --------------------------------------
class Halt(Exception):
    pass


CTRL = {"running": False, "paused": False, "quit": False, "want": [],
        "busy": False, "state": "Idle", "last_ui": 0.0, "session": False,
        "stash_refused": False}


def set_state(text):
    if CTRL.get("state") != text:
        log("state: " + text)
    CTRL["state"] = text
    gui_tick(force=True)


def checkpoint():
    """Called between every action: keeps the window alive, handles buttons,
    holds while paused, and bails out when Stop is pressed."""
    API.ProcessCallbacks()
    track_position()
    expire_marks()
    grove_map_tick()
    handle_wants()
    gui_tick()
    while CTRL["paused"] and CTRL["running"] and not CTRL["quit"] and not API.StopRequested:
        API.ProcessCallbacks()
        handle_wants()
        gui_tick()
        API.Pause(0.2)
    if API.StopRequested or CTRL["quit"]:
        raise Halt()
    if CTRL["session"] and not CTRL["running"]:
        raise Halt()


def on_stop():
    clear_marks()
    try:
        API.TrackingArrow(-1, -1)
    except Exception:
        pass
    sysmsg("SL_Lumberjack stopped.", 33)

API.OnStop(on_stop)


def item_name(item):
    name = item.Name or ""
    if not name:
        try:
            data = item.GetItemData()
            if data and data.Name:
                name = data.Name
        except Exception:
            pass
    return name.lower()


NOTES = []                  # events shown in the window


def note(msg, hue=43):
    sysmsg("[Lumber] " + msg, hue)
    API.HeadMsg(msg, API.Player, hue)
    NOTES.append(msg)
    del NOTES[:-3]


def pack_all():
    items = API.ItemsInContainer(API.Backpack, True) or []
    if not items:
        # client only knows contents of opened containers
        API.UseObject(API.Backpack)
        API.Pause(1.0)
        items = API.ItemsInContainer(API.Backpack, True) or []
    return items


def pack_items(graphics):
    out = []
    for it in API.ItemsInContainer(API.Backpack, True) or []:
        if it.Graphic in graphics:
            out.append(it)
    return out


TOOL_TEXT = {}              # serial -> tooltip text (cached)
REST = {}                   # serial -> [until_time, seconds]
STRIKES = {}                # serial -> swings with no recognizable result
CUR_TOOL = [0]


def tooltip(item, refresh=False):
    s_ = item.Serial
    if refresh or not TOOL_TEXT.get(s_):
        try:
            if refresh:
                API.RequestOPLData([s_])
                API.Pause(0.4)
            first = s_ not in TOOL_TEXT
            TOOL_TEXT[s_] = (API.ItemNameAndProps(s_, first and not refresh, 1) or "").lower()
        except Exception:
            TOOL_TEXT[s_] = TOOL_TEXT.get(s_, "")
    return TOOL_TEXT[s_]


def rest_tool(serial, why):
    """Set a tool aside; each repeat doubles how long."""
    now = time.time()
    prev = REST.get(serial)
    secs = min(prev[1] * 2, 1200) if prev else TOOL_REST
    REST[serial] = [now + secs, secs]
    STRIKES.pop(serial, None)
    log("resting tool {} for {}s ({})".format(serial, int(secs), why))
    return secs


def resting(serial):
    r = REST.get(serial)
    return bool(r) and time.time() < r[0]


def uses_left(item):
    """Uses remaining from the item's tooltip, or -1 if unknown."""
    m = re.search(r"uses remaining:?\s*(\d+)", tooltip(item), re.I)
    return int(m.group(1)) if m else -1


def is_axe(item):
    if item is None:
        return False
    if item.Graphic in AXE_GRAPHICS:
        return True
    n = item_name(item)
    if "pick" in n:             # pickaxe / pick axe are mining tools
        return False
    return "hatchet" in n or n.endswith("axe") or " axe" in n


OPENED_BAGS = [0.0]


def open_bags():
    """The client only knows what is in bags that were opened: open the ones in the pack."""
    if time.time() - OPENED_BAGS[0] < 60:
        return False
    OPENED_BAGS[0] = time.time()
    opened = 0
    for it in pack_all():
        try:
            is_bag = bool(it.IsContainer)
        except Exception:
            is_bag = False
        if is_bag and it.Serial != API.Backpack:
            API.UseObject(it.Serial)
            API.Pause(0.6)
            opened += 1
    if opened:
        dbg("Opened {} bag(s) in the pack to look for axes".format(opened))
    return opened > 0


def held_axe():
    for layer in HAND_LAYERS:
        try:
            held = API.FindLayer(layer)
        except Exception:
            held = None
        if is_axe(held):
            return held
    return None


def all_axes():
    """[held axe first, then the ones in the pack], skipping axes set aside."""
    found = []
    h = held_axe()
    if h is not None and not resting(h.Serial):
        found.append(h)
    pack = [it for it in pack_all() if is_axe(it) and not resting(it.Serial)
            and (h is None or it.Serial != h.Serial)]
    # hatchets first (light, cheap), then whatever has the fewest uses left
    pack.sort(key=lambda it: (it.Graphic not in HATCHET_GRAPHICS, uses_left(it) if uses_left(it) >= 0 else 9999))
    return found + pack


def equip_axe(axe):
    """Lumberjacking only works with the axe in your hands."""
    for layer in HAND_LAYERS:
        try:
            held = API.FindLayer(layer)
        except Exception:
            held = None
        if held is not None and held.Serial == axe.Serial:
            return True
    # free both hands (a shield or a sword would block a two-handed axe)
    for clear in ("ClearRightHand", "ClearLeftHand"):
        try:
            getattr(API, clear)()
            API.Pause(0.6)
        except Exception:
            pass
    try:
        API.EquipItem(axe.Serial)
        API.Pause(0.8)
    except Exception:
        pass
    h = held_axe()
    if h is not None and h.Serial == axe.Serial:
        log("equipped {} ({})".format(item_name(axe), axe.Serial))
        return True
    CTRL["equip_failed"] = CTRL.get("equip_failed", 0) + 1
    if CTRL["equip_failed"] in (1, 5):
        note("Could not put the {} in your hands.".format(item_name(axe) or "axe"), 33)
    return False


def find_tool():
    axes = all_axes()
    if not axes and open_bags():
        axes = all_axes()
    for axe in axes:
        if equip_axe(axe):
            return held_axe() or axe
        rest_tool(axe.Serial, "would not equip")
    return None


# ---- making hatchets with Tinkering ---------------------------------------------
# ModernUO craft gump: button id = 1 + type + index * 7 (CraftGump.GetButtonID).
# Tinkering groups: Wooden Items 0, Tools 1 ...  Tools items: tinker's tools 3,
# hatchet 4. The script reads the real buttons from the gump layout first and
# only falls back to these numbers.
CL_TINKER_MENU = 1044007
CL_TOOLS_GROUP = 1044046
CRAFT_ITEMS = {"hatchet": (1023907, 4), "tinker": (1044164, 3)}
TINKER_GRAPHICS = [0x1EB8, 0x1EB9, 0x1EBC]
CRAFT_GRAPHICS = {"hatchet": HATCHET_GRAPHICS, "tinker": TINKER_GRAPHICS}
CL_NO_METAL = ["1044037", "1044253"]     # not enough metal / material
CL_NO_SKILL = ["1044153", "1044263"]     # missing skill / tool not on you


def tinker_tools():
    return [it for it in pack_all() if it.Graphic in TINKER_GRAPHICS]


def iron_loose():
    return sum((it.Amount or 1) for it in pack_items(INGOT_GRAPHICS)
               if (it.Hue or 0) == 0 and it.Container == API.Backpack)


def pull_iron(need):
    """Take iron ingots out of bags (an ore satchel, say) if the pack is short."""
    have = iron_loose()
    if have >= need:
        return True
    for it in pack_items(INGOT_GRAPHICS):
        if (it.Hue or 0) == 0 and it.Container != API.Backpack:
            take = min(it.Amount or 1, need - have + 4)
            API.MoveItem(it.Serial, API.Backpack, take)
            API.Pause(0.8)
            have = iron_loose()
            if have >= need:
                break
    return have >= need

def _layout_button(layout, cliloc, button_x):
    """Find the button on the same row (and page) as a localized label in raw gump layout."""
    buttons = {}
    labels = []
    page = 0
    for line in layout.split("\n"):
        p = line.split()
        if not p:
            continue
        try:
            if p[0] == "page" and len(p) > 1:
                page = int(p[1])
            elif p[0] == "button" and len(p) >= 8 and int(p[1]) == button_x:
                buttons[(page, int(p[2]))] = int(p[7])
            elif p[0].startswith("xmfhtml") and len(p) >= 6 and p[5] == str(cliloc):
                labels.append((page, int(p[2])))
        except ValueError:
            continue
    for lp, ly in labels:
        for (bp, by), bid in buttons.items():
            if bp == lp and abs((ly - 3) - by) <= 2:
                return bid
    return None


def _craft_gump(timeout=5.0):
    """Wait for the Tinkering menu and return (gump_id, layout)."""
    t = 0.0
    while t < timeout:
        gid = API.HasGump()
        if gid:
            g = API.GetGump(gid)
            layout = ""
            try:
                layout = g.PacketGumpText or ""
            except Exception:
                pass
            if str(CL_TINKER_MENU) in layout or API.GumpContains("TINKERING", gid):
                return gid, layout
        API.Pause(0.25)
        t += 0.25
    return 0, ""



def _how_many(what):
    return sum(1 for it in pack_all() if it.Graphic in CRAFT_GRAPHICS[what])


def craft(what, count):
    """Make `count` hatchets / tinker's tools. Returns how many were made."""
    cl, idx = CRAFT_ITEMS[what]
    made = 0
    for _ in range(count * 3):          # allow for failures
        if made >= count or API.StopRequested:
            break
        tools = tinker_tools()
        if not tools:
            note("No tinker's tools left, can't make hatchets.", 33)
            break
        need = 2 if what == "tinker" else 4
        if not pull_iron(need):
            note("Not enough iron ingots to make a {}.".format(what), 33)
            break
        before = _how_many(what)
        API.ClearJournal()
        API.UseObject(tools[0].Serial)
        gid, layout = _craft_gump()
        if not gid:
            note("The Tinkering menu did not open.", 33)
            break
        grp = _layout_button(layout, CL_TOOLS_GROUP, 15) or (1 + 0 + 1 * 7)
        API.ReplyGump(grp, gid)
        gid, layout = _craft_gump()
        if not gid:
            break
        btn = _layout_button(layout, cl, 220) or (1 + 1 + idx * 7)
        API.ReplyGump(btn, gid)
        gid, layout = _craft_gump(8.0)
        API.Pause(0.4)
        if _how_many(what) > before:
            made += 1
        elif any(c in layout for c in CL_NO_METAL):
            note("Not enough metal to make a {}.".format(what), 33)
            break
        elif any(c in layout for c in CL_NO_SKILL) or API.InJournal("required skill"):
            note("Missing the skill to make a {}.".format(what), 33)
            break
    try:
        gid = API.HasGump()
        if gid and API.GumpContains("TINKERING", gid):
            API.CloseGump(gid)
    except Exception:
        pass
    return made


def tinkering_skill():
    try:
        return float(API.GetSkill("Tinkering").Value)
    except Exception:
        return 0.0


def ensure_tools(force=False):
    """Make hatchets when every axe is gone. Returns False if there is nothing to chop with."""
    have = bool(all_axes())
    if MAKE_TOOLS and (force or not have) and not CTRL["busy"]:
        skill = tinkering_skill()
        if skill < 30.0:
            if not CTRL.get("skill_noted"):
                note("Tinkering {:.1f}: need 30 to make hatchets.".format(skill), 43)
                CTRL["skill_noted"] = True
        else:
            CTRL["busy"] = True
            prev = CTRL["state"]
            try:
                if KEEP_TINKER_SPARE and len(tinker_tools()) == 1:
                    set_state("Making tinker's tools")
                    craft("tinker", 1)
                if MAKE_COUNT:
                    set_state("Making hatchets")
                    n = craft("hatchet", MAKE_COUNT)
                    if n:
                        note("Made {} hatchet{}.".format(n, "s" if n > 1 else ""), 68)
            finally:
                CTRL["busy"] = False
                set_state(prev)
            have = bool(all_axes())
    return have


def tools_summary():
    axes = all_axes()
    h = held_axe()
    parts = ["Axes: {}".format(len(axes))]
    if h is not None:
        u = uses_left(h)
        parts.append("in hand: {}{}".format(item_name(h) or "axe", " ({} uses)".format(u) if u >= 0 else ""))
    else:
        parts.append("none in hand")
    parts.append("Tinker {}".format(len(tinker_tools())))
    return "   ".join(parts)


def diagnose():
    sysmsg("=== SL_Lumberjack axe check ===", 68)
    for layer in HAND_LAYERS:
        held = API.FindLayer(layer)
        if held:
            sysmsg("{}: 0x{:04X} hue {} {}".format(layer, held.Graphic, held.Hue, item_name(held)), 90)
    for item in API.ItemsInContainer(API.Backpack, True) or []:
        n = item_name(item)
        if is_axe(item) or "axe" in n or "hatchet" in n:
            sysmsg("pack: 0x{:04X} hue {} {}{}".format(item.Graphic, item.Hue, n,
                                                     "" if is_axe(item) else "  (not counted)"), 90)
    axes = all_axes()
    sysmsg("Axe picked: " + (item_name(axes[0]) if axes else "NONE"), 68 if axes else 33)


def weight_pct():
    if API.Player.WeightMax <= 0:
        return 0
    return (API.Player.Weight * 100) / API.Player.WeightMax


# ---- what we have learned this run ------------------------------------------
BANK_W, BANK_H = 4, 3       # a grove (Lumberjacking BankWidth / BankHeight)
DEPLETED = set()            # (bx, by) groves that said "not enough wood"
TRIED = {}                  # (x, y) tree -> "wood" / "empty" / "bad"
VISITED = set()             # standing spots already chopped from
TILE_CACHE = {}
HEADING = [0.0, 0.0]        # direction of the last successful move
START = [0, 0, 0, 0]        # wander anchor x, y, z, map
BAD_GRAPHIC = {}            # tree graphic -> times "can't use an axe on that"
MEM_VAR = "SL_Lumber_Depleted"


def bank(x, y):
    return (x // BANK_W, y // BANK_H)


def tile_info(x, y):
    """(land graphic, land impassable, [static graphics], any static impassable, cave static)"""
    key = (x, y)
    if key in TILE_CACHE:
        return TILE_CACHE[key]
    lg, limp, sg, simp, cave = -1, False, [], False, False
    try:
        t = API.GetTile(x, y)
        if t:
            lg = t.Graphic
            limp = bool(t.Impassible)
    except Exception:
        pass
    for g, imp, cv, _z in _statics_at(x, y):
        sg.append(g)
        simp = simp or imp
        cave = cave or cv
    info = (lg, limp, sg, simp, cave)
    if lg >= 0:
        TILE_CACHE[key] = info      # far tiles the client hasn't loaded yet: ask again later
    else:
        STATIC_CHUNKS.pop((x >> 3, y >> 3), None)
    return info


# Every API call that reads the world waits for the game's next frame (~20 ms),
# so statics are read a whole 8x8 block at a time instead of tile by tile.
STATIC_CHUNKS = {}


def _statics_at(x, y):
    b = (x >> 3, y >> 3)
    ch = STATIC_CHUNKS.get(b)
    if ch is None:
        ch = {}
        try:
            for st in API.GetStaticsInArea(b[0] * 8, b[1] * 8, b[0] * 8 + 7, b[1] * 8 + 7) or []:
                ch.setdefault((st.X, st.Y), []).append(
                    (st.Graphic, bool(st.IsImpassible), bool(st.IsCave), st.Z))
        except Exception:
            return []
        STATIC_CHUNKS[b] = ch
    return ch.get((x, y), [])


def tree_at(x, y):
    """(graphic, z) of the tree the server accepts on this tile (trunk preferred), or None."""
    best = None
    for g, _imp, _cv, z in _statics_at(x, y):
        if g in TREE_TILES and BAD_GRAPHIC.get(g, 0) < 3:
            if best is None or (g in TRUNK_TILES and best[0] not in TRUNK_TILES):
                best = (g, z)
    return best


def usable(x, y):
    return (x, y) not in TRIED and bank(x, y) not in DEPLETED and tree_at(x, y) is not None


def learn_bad(g):
    BAD_GRAPHIC[g] = BAD_GRAPHIC.get(g, 0) + 1
    if BAD_GRAPHIC[g] == 3:
        dbg("Tree graphic 0x{:04X} keeps refusing; skipping that kind".format(g))


# ---- chopped-out grove memory that survives restarting the script ------------
def _now():
    return int(time.time())


def load_bank_memory():
    try:
        raw = API.GetPersistentVar(MEM_VAR, "", API.PersistentVar.Char) or ""
    except Exception:
        return
    m = API.GetMap()
    cutoff = _now() - BANK_MEMORY_MIN * 60
    n = 0
    for e in str(raw).split(";"):
        parts = e.split(",")
        if len(parts) != 4:
            continue
        try:
            mp, bx, by, t = [int(v) for v in parts]
        except ValueError:
            continue
        if mp == m and t >= cutoff:
            DEPLETED.add((bx, by))
            n += 1
    if n:
        dbg("Remembered {} chopped-out groves from earlier".format(n))


def save_bank(b):
    try:
        raw = API.GetPersistentVar(MEM_VAR, "", API.PersistentVar.Char) or ""
        cutoff = _now() - BANK_MEMORY_MIN * 60
        keep = []
        for e in str(raw).split(";"):
            parts = e.split(",")
            if len(parts) == 4 and parts[3].isdigit() and int(parts[3]) >= cutoff:
                keep.append(e)
        keep.append("{},{},{},{}".format(API.GetMap(), b[0], b[1], _now()))
        API.SavePersistentVar(MEM_VAR, ";".join(keep[-600:]), API.PersistentVar.Char)
    except Exception:
        pass


def deplete(x, y):
    b = bank(x, y)
    if b not in DEPLETED:
        DEPLETED.add(b)
        save_bank(b)
        if GROVE_MAP["on"]:
            grove_map_tick(force=True)  # an emptied grove turns red

# ---- survey: the grove map and your Forester's Logbook -----------------------------
# Groves on Shattered Legacy never re-roll (RandomizeVeins = false), so the wood in
# each 4x3 grove comes from a fixed formula (ModernUO HarvestDefinition.GetVeinAt
# with StableRandom). The script works it out here, checks itself against the
# groves in your logbook and the wood you chop, and turns itself off if they disagree.
_M64 = (1 << 64) - 1


def _rotl(x, k):
    return ((x << k) | (x >> (64 - k))) & _M64


def _stable_first(seed, maxv):
    """StableRandom.First(seed, max): splitmix64 seeds xoshiro256**, Lemire bounded draw."""
    s = [0, 0, 0, 0]
    x = seed & _M64
    for i in range(4):
        x = (x + 0x9E3779B97F4A7C15) & _M64
        z = x
        z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & _M64
        z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & _M64
        s[i] = z ^ (z >> 31)

    def nxt():
        r = (_rotl((s[1] * 5) & _M64, 7) * 9) & _M64
        t = (s[1] << 17) & _M64
        s[2] ^= s[0]
        s[3] ^= s[1]
        s[1] ^= s[2]
        s[0] ^= s[3]
        s[2] ^= t
        s[3] = _rotl(s[3], 45)
        return r

    prod = maxv * nxt()
    if (prod & _M64) < maxv:
        rem = ((1 << 64) - maxv) % maxv
        while (prod & _M64) < rem:
            prod = maxv * nxt()
    return prod >> 64


# (name as the logbook writes it, vein weight, Lumberjacking needed to pull it)
# Stock ModernUO ML lumber veins plus the shard's ClusterFLumberjackingExtension
# (Ironwood 8 ... Starwood 1), total 1036. Colored groves give ordinary logs half
# the time, and always when your skill is below the wood's requirement.
WOODS = [("Regular", 490, 0), ("Oak", 300, 65), ("Ash", 100, 80), ("Yew", 50, 95),
         ("Heartwood", 30, 100), ("Bloodwood", 20, 100), ("Frostwood", 10, 100),
         ("Ironwood", 8, 100), ("Ghostwood", 7, 100), ("Emberbark", 6, 100), ("Frostbark", 5, 100),
         ("Shadowbark", 4, 100), ("Runewood", 3, 100), ("Voidwood", 2, 100), ("Starwood", 1, 100)]
WOOD_TOTAL = sum(w for _, w, _ in WOODS)
WOOD_REQ = dict((n, r) for n, _, r in WOODS)
WOOD_RANK = dict((n, i) for i, (n, _, _) in enumerate(WOODS))
EXTENDED = set(n for n, _, _ in WOODS[7:])     # ordinary logs until reported to the Foresters
WOOD_NAMES = sorted([n for n, _, _ in WOODS[1:]], key=len, reverse=True)
WOOD_HUE = {"Oak": 0x7DA, "Ash": 0x4A7, "Yew": 0x4A8, "Heartwood": 0x4A9, "Bloodwood": 0x4AA,
            "Frostwood": 0x47F, "Ironwood": 0x966, "Ghostwood": 0x47E, "Emberbark": 0x058,
            "Frostbark": 0x76E, "Shadowbark": 0x544, "Runewood": 0x060, "Voidwood": 0x22E,
            "Starwood": 0x4EC}
FACETS = ["Felucca", "Trammel", "Ilshenar", "Malas", "Tokuno", "TerMur"]
GROVE_VAR = "SL_Lumber_Groves"

WOOD_CACHE = {}             # (map, bx, by) -> wood name
LOGGED = {}                 # wood name -> [(x, y)] groves in the logbook on this map
WORTH = {}                  # bank -> (grove bonus, new-grove bonus)
SURVEY = {"map_ok": True, "hits": 0, "misses": 0, "book_hits": 0, "book_total": 0,
          "book": None, "read_t": 0.0, "reported": set(), "found": set(), "skill": -1.0,
          "skill_t": 0.0, "map": -1, "list": [], "list_t": 0.0, "list_at": (0, 0),
          "went": set(), "seen_lines": []}


# Reading the map index (like any world read) waits a game frame, and the
# scoring loops ask for it per tile, so it is cached and refreshed each step.
MAPC = {"map": None, "t": 0.0}


def cur_map(fresh=False):
    now = time.time()
    if fresh or MAPC["map"] is None or now - MAPC["t"] > 1.0:
        try:
            MAPC["map"] = int(API.GetMap())
        except Exception:
            MAPC["map"] = MAPC["map"] or 0
        MAPC["t"] = now
    return MAPC["map"]


def wood_at_bank(b, mp=None):
    """The wood a grove holds: HarvestDefinition.GetVeinAt with the grove's own
    coordinates (x / 4, y / 3), StableRandom, and the shard's "<" vein boundary."""
    if mp is None:
        mp = cur_map()
    k = (mp, b[0], b[1])
    v = WOOD_CACHE.get(k)
    if v is None:
        r = _stable_first((b[0] * 17 + b[1] * 11 + mp * 3) & _M64, WOOD_TOTAL)
        v = WOODS[-1][0]
        for name, w, _ in WOODS:
            if r < w:
                v = name
                break
            r -= w
        WOOD_CACHE[k] = v
    return v


def wood_at(x, y):
    return wood_at_bank(bank(x, y))


def lumber_skill():
    now = time.time()
    if now - SURVEY["skill_t"] > 30:
        SURVEY["skill_t"] = now
        try:
            v = float(API.GetSkill("Lumberjacking").Value)
        except Exception:
            v = 0.0
        if v != SURVEY["skill"]:
            SURVEY["skill"] = v
            WORTH.clear()
    return SURVEY["skill"]


def wood_ok(name):
    """Is your Lumberjacking high enough to get this wood from its grove?"""
    return name != "Regular" and lumber_skill() >= WOOD_REQ.get(name, 999)


def logged_near(name, x, y, r=None):
    r = NEW_GROVE_DIST if r is None else r
    for lx, ly in LOGGED.get(name, ()):
        if (lx - x) * (lx - x) + (ly - y) * (ly - y) <= r * r:
            return True
    return False


def survey_on():
    return opt("seek") and SURVEY["map_ok"]


def _bank_center(b):
    return b[0] * BANK_W + BANK_W // 2, b[1] * BANK_H + BANK_H // 2


def bank_worth(b):
    """(bonus for wood you'll actually get, bonus for something new to log)"""
    if SURVEY["map"] != cur_map():
        SURVEY["map"] = cur_map()
        WORTH.clear()
        load_grove_log()
    lumber_skill()
    w = WORTH.get(b)
    if w is None:
        name = wood_at_bank(b)
        v = n = 0.0
        if wood_ok(name):
            cx, cy = _bank_center(b)
            if name in EXTENDED and name not in SURVEY["found"]:
                n = W_NEWWOOD           # first find: a discovery to report
            elif name not in EXTENDED or name in SURVEY["reported"]:
                v = W_GROVE
                if not logged_near(name, cx, cy):
                    n = W_NEWGROVE
        w = WORTH[b] = (v, n)
    return w


def tree_value(x, y):
    if not survey_on() or not usable(x, y):
        return 0.0
    v, n = bank_worth(bank(x, y))
    return v + n


# Groves whose wood you actually know: colored wood you chopped there, or a grove
# in your logbook. Only these get colored, so the script never shows a grove
# before you (or your logbook) found it.
KNOWN = {}                  # bank -> wood name
KNOWN_VAR = "SL_Lumber_Known"


def logged_bank(name, x, y):
    """The grove a logbook entry is in (the log holds where you stood, up to 2 tiles off)."""
    for dx in (0, -2, 2):
        for dy in (0, -2, 2):
            b = bank(x + dx, y + dy)
            if wood_at_bank(b) == name:
                return b
    return bank(x, y)


def know(b, name):
    if KNOWN.get(b) == name:
        return
    KNOWN[b] = name
    try:
        raw = API.GetPersistentVar(KNOWN_VAR, "", API.PersistentVar.Char) or ""
        keep = [e for e in str(raw).split(";") if e]
        keep.append("{},{},{},{}".format(cur_map(), b[0], b[1], name))
        API.SavePersistentVar(KNOWN_VAR, ";".join(keep[-1500:]), API.PersistentVar.Char)
    except Exception:
        pass
    if GROVE_MAP["on"]:
        grove_map_tick(force=True)


def load_known():
    mp = cur_map()
    try:
        raw = API.GetPersistentVar(KNOWN_VAR, "", API.PersistentVar.Char) or ""
    except Exception:
        return
    for e in str(raw).split(";"):
        p = e.split(",")
        if len(p) == 4 and p[3] in WOOD_REQ:
            try:
                if int(p[0]) == mp:
                    KNOWN[(int(p[1]), int(p[2]))] = p[3]
            except ValueError:
                pass


def _add_logged(name, x, y, save=True):
    pts = LOGGED.setdefault(name, [])
    KNOWN.setdefault(logged_bank(name, x, y), name)
    if (x, y) in pts:
        return False
    pts.append((x, y))
    WORTH.clear()
    if save:
        save_grove_log()
    return True


def load_grove_log():
    """Groves this character saw logged, kept in case the logbook isn't in the pack."""
    LOGGED.clear()
    KNOWN.clear()
    load_known()
    try:
        raw = API.GetPersistentVar(GROVE_VAR, "", API.PersistentVar.Char) or ""
    except Exception:
        return
    mp = cur_map()
    for e in str(raw).split(";"):
        p = e.split(",")
        if len(p) == 4 and p[1] in WOOD_REQ:
            try:
                if int(p[0]) == mp:
                    _add_logged(p[1], int(p[2]), int(p[3]), save=False)
            except ValueError:
                pass
        elif len(p) == 2 and p[0] in ("found", "reported") and p[1] in WOOD_REQ:
            SURVEY[p[0]].add(p[1])


def save_grove_log():
    try:
        raw = API.GetPersistentVar(GROVE_VAR, "", API.PersistentVar.Char) or ""
        mp = cur_map()
        keep = [e for e in str(raw).split(";") if e and not e.startswith(str(mp) + ",")
                and not e.startswith("found,") and not e.startswith("reported,")]
        for name, pts in LOGGED.items():
            for x, y in pts:
                keep.append("{},{},{},{}".format(mp, name, x, y))
        keep = keep[-800:]
        for k in ("found", "reported"):
            for name in sorted(SURVEY[k]):
                keep.append("{},{}".format(k, name))
        API.SavePersistentVar(GROVE_VAR, ";".join(keep), API.PersistentVar.Char)
    except Exception:
        pass


# ---- reading the Forester's Logbook ---------------------------------------------
# Rows look like  "Oak - first cut 2026-10-01, 2 groves, 120 logs chopped"
#                 "Ironwood - first found 2026-10-02, 1 grove, 10 logs chopped [Reported]"
#                 "  * 2026-10-01 | Britain Woods, Trammel | (1234, 567) | 60 logs"
_HEAD_RE = re.compile(r"(" + "|".join(WOOD_NAMES) + r")\s*-\s*first (?:cut|found)", re.I)
_ROW_RE = re.compile(r"\|\s*([^|]*),\s*([^|,]+?)\s*\|\s*\(\s*(\d+)\s*,\s*(\d+)\s*\)")


def find_logbook():
    for it in pack_all():
        n = item_name(it)
        if "forester" in n and "logbook" in n:
            return it
        if it.Graphic == LOGBOOK_GRAPHIC and "forester" in tooltip(it):
            return it
    return None


def _strip_html(text):
    text = re.sub(r"(?i)<br\s*/?>", "\n", text)
    text = re.sub(r"<[^>]*>", " ", text)
    return text.replace("&nbsp;", " ")


def parse_logbook(text):
    """{wood: {"reported": bool, "groves": [(facet, x, y)]}} from the logbook gump text."""
    text = _strip_html(text)
    heads = list(_HEAD_RE.finditer(text))
    out = {}
    for i, h in enumerate(heads):
        name = next((n for n in WOOD_NAMES if n.lower() == h.group(1).lower()), h.group(1))
        seg = text[h.start():heads[i + 1].start() if i + 1 < len(heads) else len(text)]
        bar = seg.find("|")
        head = (seg[:bar] if bar >= 0 else seg).lower()
        entry = out.setdefault(name, {"reported": False, "groves": []})
        # common woods are reported the moment you cut them; rare ones say so
        if name not in EXTENDED or ("[reported]" in head and "pending" not in head):
            entry["reported"] = True
        for r in _ROW_RE.finditer(seg):
            entry["groves"].append((r.group(2).strip(), int(r.group(3)), int(r.group(4))))
    return out


def _logbook_gump(timeout=4.0):
    t = 0.0
    while t < timeout:
        gid = API.HasGump()
        if gid and (API.GumpContains("Forester", gid) or API.GumpContains("Timber types", gid)):
            API.Pause(0.3)
            return gid
        API.Pause(0.25)
        t += 0.25
    return 0


def _gump_text(gid):
    parts = []
    try:
        parts.append(API.GetGumpContents(gid) or "")
    except Exception:
        pass
    try:
        g = API.GetGump(gid)
        if g is not None:
            parts.append(g.PacketGumpText or "")
    except Exception:
        pass
    return "\n".join(parts)


def read_logbook(quiet=False):
    """Open the logbook, note every logged grove on this facet, close it."""
    SURVEY["read_t"] = time.time()
    if SURVEY["map"] != cur_map():
        SURVEY["map"] = cur_map()
        load_grove_log()
    book = find_logbook()
    if book is None:
        SURVEY["book"] = None
        if not quiet:
            note("No Forester's Logbook in your pack; tracking groves myself.", 43)
        return False
    API.UseObject(book.Serial)
    gid = _logbook_gump()
    if not gid:
        if not quiet:
            note("The logbook didn't open.", 43)
        return False
    text = _gump_text(gid)
    try:
        API.CloseGump(gid)
    except Exception:
        pass
    data = parse_logbook(text)
    mp = cur_map()
    facet = FACETS[mp].lower() if 0 <= mp < len(FACETS) else ""
    n = 0
    for name, e in data.items():
        SURVEY["found"].add(name)
        if e["reported"]:
            SURVEY["reported"].add(name)
        else:
            SURVEY["reported"].discard(name)
        for fac, x, y in e["groves"]:
            if fac.lower().replace(" ", "") == facet:
                _add_logged(name, x, y, save=False)
                n += 1
    save_grove_log()
    WORTH.clear()
    SURVEY["book"] = sum(len(e["groves"]) for e in data.values())
    check_grove_map()
    pending = sorted(k for k in data if k in EXTENDED and not data[k]["reported"])
    pending = [k for k in pending if k not in SURVEY.setdefault("pending_noted", set())]
    if pending:
        SURVEY["pending_noted"].update(pending)
        note("Unreported rare wood: {}. Report it in your logbook to start getting it.".format(
            ", ".join(pending)), 43)
    dbg("Logbook: {} woods, {} groves on this facet".format(len(data), n))
    return True


def check_grove_map():
    """How many logged groves sit where the grove map says that wood is."""
    hits = total = 0
    for name, pts in LOGGED.items():
        for x, y in pts:
            total += 1
            # the logbook stores where you stood; trees are up to 2 tiles out
            if any(wood_at(x + dx, y + dy) == name for dx in (-2, 0, 2) for dy in (-2, 0, 2)):
                hits += 1
    SURVEY["book_hits"], SURVEY["book_total"] = hits, total
    judge_grove_map()


def judge_grove_map():
    hits = SURVEY["book_hits"] + SURVEY["hits"]
    total = SURVEY["book_total"] + SURVEY["hits"] + SURVEY["misses"]
    ok = total < 5 or hits >= 0.6 * total
    if SURVEY["map_ok"] and not ok:
        note("Grove map doesn't match this shard ({}/{}); not steering by it.".format(hits, total), 33)
    SURVEY["map_ok"] = ok


_CHOP_RE = re.compile(r"You chop some (\w+) logs", re.I)
_FIND_RE = re.compile(r"unusual timber\W+(" + "|".join(WOOD_NAMES) + r")", re.I)
CHOP_NAMES = {"oak": "Oak", "ash": "Ash", "yew": "Yew", "heartwood": "Heartwood",
              "bloodwood": "Bloodwood", "frostwood": "Frostwood"}


def survey_after_wood(x, y):
    """After a good chop: check the wood against the map, catch new discoveries."""
    try:
        entries = API.GetJournalEntries(3) or []
    except Exception:
        entries = []
    px, py = API.Player.X, API.Player.Y
    for e in entries:
        text = getattr(e, "Text", "") or ""
        key = (text, str(getattr(e, "Time", "")))
        if key in SURVEY["seen_lines"]:
            continue
        SURVEY["seen_lines"].append(key)
        del SURVEY["seen_lines"][:-30]
        m = _FIND_RE.search(text)
        if m:
            name = next((n for n in WOOD_NAMES if n.lower() == m.group(1).lower()), m.group(1))
            SURVEY["found"].add(name)
            know(bank(x, y), name)
            _add_logged(name, px, py)
            WORTH.clear()
            note("New wood: {}! Report it from your Forester's Logbook.".format(name), 68)
            continue
        m = _CHOP_RE.search(text)
        if not m:
            continue
        name = CHOP_NAMES.get(m.group(1).lower())
        if name is None:
            continue                # ordinary logs: every colored grove gives them sometimes
        pred = wood_at(x, y)
        if name == "Frostwood" and pred in EXTENDED:
            name = pred             # the server reuses the frostwood message for rare woods
        know(bank(x, y), name)
        if not logged_near(name, px, py):
            _add_logged(name, px, py)   # the logbook starts a new grove here too
        if pred == name:
            SURVEY["hits"] += 1
        else:
            SURVEY["misses"] += 1
            dbg("Chopped {} where the map says {}".format(name, pred))
        judge_grove_map()


# ---- Survey button: list and mark the best groves nearby --------------------------
def _bank_tiles(b):
    return [(b[0] * BANK_W + i, b[1] * BANK_H + j) for i in range(BANK_W) for j in range(BANK_H)]


def _bank_trees(b):
    """Unchopped trees in a grove, or None if the client hasn't loaded that far."""
    n = known = 0
    for x, y in _bank_tiles(b):
        if tile_info(x, y)[0] >= 0:
            known += 1
        if usable(x, y):
            n += 1
    return n if known >= 4 else None


def survey(show=True):
    """Rank colored groves within SURVEY_RANGE. Returns the list, best first."""
    if time.time() - SURVEY["read_t"] > 300:
        read_logbook(quiet=True)
    px, py = API.Player.X, API.Player.Y
    pb = bank(px, py)
    rx, ry = SURVEY_RANGE // BANK_W + 1, SURVEY_RANGE // BANK_H + 1
    cands, low = [], {}
    for bx in range(pb[0] - rx, pb[0] + rx + 1):
        for by in range(pb[1] - ry, pb[1] + ry + 1):
            b = (bx, by)
            name = wood_at_bank(b)
            if name == "Regular" or b in DEPLETED:
                continue
            cx, cy = _bank_center(b)
            d = max(abs(cx - px), abs(cy - py))
            if d > SURVEY_RANGE:
                continue
            if not wood_ok(name):
                low[name] = low.get(name, 0) + 1
                continue
            v, n = bank_worth(b)
            if v + n <= 0:
                continue            # e.g. a rare wood you found but haven't reported yet
            pre = WOOD_RANK[name] + n - d / 10.0
            cands.append([pre, name, b, d, n > 0])
    cands.sort(reverse=True)
    out = []
    for pre, name, b, d, new in cands[:25]:
        checkpoint()
        trees = _bank_trees(b)
        if trees is not None and trees < 1:
            continue                # no trees in that grove (most of the map isn't forest)
        out.append({"score": pre + min(trees if trees is not None else 1, 4) / 2.0, "name": name,
                    "bank": b, "dist": d, "new": new, "trees": trees,
                    "first": name in EXTENDED and name not in SURVEY["found"]})
    out.sort(key=lambda v: -v["score"])
    SURVEY["list"], SURVEY["list_t"], SURVEY["list_at"] = out, time.time(), (px, py)
    if show:
        show_survey(out, low)
    return out


def _a(name):
    return ("an " if name[0] in "AEIOU" else "a ") + name


def show_survey(out, low):
    here = wood_at(API.Player.X, API.Player.Y)
    sysmsg("[Survey] Lumberjacking {:.1f}. You're in {}.".format(
        lumber_skill(), "an ordinary grove" if here == "Regular" else _a(here) + " grove"), 68)
    if not SURVEY["map_ok"]:
        sysmsg("[Survey] Grove map is off for this shard; the list may be wrong.", 33)
    if not out:
        sysmsg("[Survey] No colored groves you can chop within {} tiles.".format(SURVEY_RANGE), 43)
    for v in out[:SURVEY_SHOW]:
        cx, cy = _bank_center(v["bank"])
        t = v["trees"]
        sysmsg("[Survey] {}{}  {} tiles {}  ({} tree{})".format(
            v["name"], "  FIRST FIND" if v["first"] else ("  NEW" if v["new"] else ""),
            v["dist"], compass(cx - API.Player.X, cy - API.Player.Y),
            t if t is not None else "?", "" if t == 1 else "s"), 88 if v["new"] else 90)
    if low:
        sysmsg("[Survey] Also nearby, need more skill: " + ", ".join(
            "{} {} ({})".format(n, k, WOOD_REQ[k]) for k, n in
            sorted(low.items(), key=lambda kv: WOOD_RANK[kv[0]])), 90)
    for v in out[:3]:
        hue = WOOD_HUE.get(v["name"], 0x481)
        for x, y in _bank_tiles(v["bank"]):
            if usable(x, y):
                mark_at(x, y, hue, SURVEY_MARKS)
    if out:
        cx, cy = _bank_center(out[0]["bank"])
        try:
            API.TrackingArrow(cx, cy)
            CTRL["arrow_until"] = time.time() + SURVEY_MARKS
        except Exception:
            pass


def grove_stand_spot(b):
    """A walkable tile within reach of the most unchopped trees of a grove."""
    best = None
    px, py = API.Player.X, API.Player.Y
    trees = [t for t in _bank_tiles(b) if usable(t[0], t[1])]
    if not trees:
        return None
    for x in range(b[0] * BANK_W - RADIUS, b[0] * BANK_W + BANK_W + RADIUS):
        for y in range(b[1] * BANK_H - RADIUS, b[1] * BANK_H + BANK_H + RADIUS):
            if tile_info(x, y)[0] < 0 or not standable(x, y):
                continue
            n = sum(1 for tx, ty in trees if _cheb(tx, ty, x, y) <= RADIUS)
            d = max(abs(x - px), abs(y - py))
            if n >= 1 and (best is None or (n, -d) > best[0]):
                best = ((n, -d), x, y)
    return (best[1], best[2]) if best else None


def go_to_grove(auto=False):
    """Walk to the best surveyed grove. True if we got there."""
    lst = SURVEY["list"]
    if not lst or time.time() - SURVEY["list_t"] > 300 or \
            dist2((API.Player.X, API.Player.Y), SURVEY["list_at"]) > 16:
        lst = survey(show=not auto)
    here = bank(API.Player.X, API.Player.Y)
    lst = [v for v in lst if v["bank"] not in SURVEY["went"] and v["bank"] not in DEPLETED
           and v["bank"] != here]
    if not lst:
        if not auto:
            note("No surveyed grove to go to.", 43)
        return False
    v = lst[0]
    b = v["bank"]
    SURVEY["went"].add(b)
    spot = grove_stand_spot(b)
    tx, ty = spot if spot else _bank_center(b)
    note("Going to {} grove{}, {} tiles.".format(_a(v["name"]), " (first find!)" if v["first"]
                                                 else (" (new)" if v["new"] else ""), v["dist"]), 88)
    prev = CTRL["state"]
    set_state("Walking to {} grove".format(v["name"]))
    ok = go_to(tx, ty, 0 if spot else 3, v["name"] + " grove")
    if ok and not spot:
        spot = grove_stand_spot(b)      # close enough now to see the trees
        if spot:
            ok = go_to(spot[0], spot[1], 0, v["name"] + " grove")
    set_state(prev)
    if ok:
        START[:] = [API.Player.X, API.Player.Y, API.Player.Z, cur_map()]
        HEADING[0] = HEADING[1] = 0.0
        CTRL["moved"] = True
    else:
        note("Couldn't reach the {} grove.".format(v["name"]), 43)
    return ok


# ---- grove map: tint the trees of every grove you know -----------------------------
GROVE_MAP = {"tiles": {}, "center": None, "on": False, "queue": []}     # (x, y) -> hue


def wood_hue(x, y):
    """Highlight color for a tree: the wood you found in its grove, else plain green."""
    name = KNOWN.get(bank(x, y))
    return WOOD_HUE.get(name, 0x481) if name else HUE_WOOD


def grove_map_tick(force=False):
    """Tint the trees of every known grove around you. Reading tiles waits a game
    frame, so new trees are checked a few at a time."""
    on = opt("grovemap")
    if not on:
        if GROVE_MAP["on"]:
            clear_grove_map()
        return
    c = (cur_map(), API.Player.X // 8, API.Player.Y // 8)
    if force or not GROVE_MAP["on"] or GROVE_MAP["center"] != c:
        GROVE_MAP["on"] = True
        GROVE_MAP["center"] = c
        want = {}
        px, py = API.Player.X, API.Player.Y
        b0 = bank(px - GROVE_MAP_RANGE, py - GROVE_MAP_RANGE)
        b1 = bank(px + GROVE_MAP_RANGE, py + GROVE_MAP_RANGE)
        for bx in range(b0[0], b1[0] + 1):
            for by in range(b0[1], b1[1] + 1):
                b = (bx, by)
                name = KNOWN.get(b)
                if name is None:
                    continue            # not found yet: stays plain
                hue = HUE_DRY if b in DEPLETED else WOOD_HUE.get(name, 0x481)
                for t in _bank_tiles(b):
                    want[t] = hue
        for t in list(GROVE_MAP["tiles"]):
            if want.get(t) != GROVE_MAP["tiles"][t]:
                if t not in MARKED:
                    try:
                        API.RemoveMarkedTile(t[0], t[1])
                    except Exception:
                        pass
                del GROVE_MAP["tiles"][t]
        GROVE_MAP["queue"] = sorted([(t, h) for t, h in want.items() if t not in GROVE_MAP["tiles"]],
                                    key=lambda th: -_cheb(th[0][0], th[0][1], px, py))
    q = GROVE_MAP.get("queue") or []
    slow = 0
    while q and slow < GROVE_MAP_BATCH:
        t, hue = q.pop()            # closest first
        if (t[0] >> 3, t[1] >> 3) not in STATIC_CHUNKS:
            slow += 1
        if tree_at(t[0], t[1]) is None:
            continue
        GROVE_MAP["tiles"][t] = hue
        if t not in MARKED:
            try:
                API.MarkTile(t[0], t[1], hue)
            except Exception:
                pass


def clear_grove_map():
    for t in GROVE_MAP["tiles"]:
        if t not in MARKED:
            try:
                API.RemoveMarkedTile(t[0], t[1])
            except Exception:
                pass
    GROVE_MAP.update({"tiles": {}, "center": None, "on": False, "queue": []})


def survey_line():
    if not opt("seek"):
        return "Survey: off"
    here = KNOWN.get(bank(API.Player.X, API.Player.Y), "Regular")
    tot = SURVEY["book_total"] + SURVEY["hits"] + SURVEY["misses"]
    hits = SURVEY["book_hits"] + SURVEY["hits"]
    check = "map {}/{}".format(hits, tot) if tot else "map unchecked"
    if not SURVEY["map_ok"]:
        check = "map OFF ({}/{})".format(hits, tot)
    tag = ""
    if here == "Regular":
        here = "not found yet"
    else:
        tag = " new" if not logged_near(here, API.Player.X, API.Player.Y) else ""
        if not wood_ok(here):
            tag = " (skill {})".format(WOOD_REQ[here])
    book = "no logbook" if SURVEY["book"] is None else "log {}".format(sum(len(p) for p in LOGGED.values()))
    return "Here: {}{}   {}   {}".format(here, tag, book, check)


# ---- chopping ------------------------------------------------------------------
def trees_in_reach():
    """Unchopped trees within RADIUS of where you stand, best first."""
    px, py = API.Player.X, API.Player.Y
    out = []
    for dx in range(-RADIUS, RADIUS + 1):
        for dy in range(-RADIUS, RADIUS + 1):
            x, y = px + dx, py + dy
            if usable(x, y) and (x, y) not in CTRL.get("far", set()):
                out.append((-tree_value(x, y), max(abs(dx), abs(dy)), x, y))
    out.sort()
    return [(x, y) for _, _, x, y in out]


def last_journal_line():
    try:
        entries = API.GetJournalEntries(SWING_DELAY + 2) or []
        if entries:
            return entries[-1].Text
    except Exception:
        pass
    return ""


def strike(tool):
    """A chop with no target cursor at all. Three in a row and the axe is set aside."""
    n = STRIKES.get(tool.Serial, 0) + 1
    STRIKES[tool.Serial] = n
    if n < 3:
        return "none"
    heard = last_journal_line()
    secs = rest_tool(tool.Serial, "no response")
    note("The {} stopped working; setting it aside {}s.".format(item_name(tool) or "axe", int(secs)), 43)
    if heard:
        dbg("Last message was: " + heard)
    return "retry"


def swing(x, y, g, z):
    """One chop at a tree. Returns wood/miss/empty/bad/far/full/broke/notool/retry/none."""
    tool = find_tool()
    if tool is None:
        return "notool"
    CUR_TOOL[0] = tool.Serial
    # a cursor left open by an earlier chop would eat this one; clear it first
    if API.HasTarget():
        API.CancelTarget()
        API.Pause(0.3)
    API.ClearJournal()
    API.UseObject(tool.Serial)
    if not API.WaitForTarget("any", 3):
        API.Pause(0.3)
        if API.InJournal(EQUIP_MSG):
            CTRL["heard"] = "axe not equipped"
            return "retry"
        if API.InJournalAny(BUSY_MSGS):
            API.Pause(1.0)
            return "retry"
        return strike(tool)
    STRIKES.pop(tool.Serial, None)
    API.Target(x, y, z, g)
    API.Pause(0.15)
    if API.HasTarget():
        API.CancelTarget()
        CTRL["heard"] = "target did not go out"
        return "bad"

    # wait for the result instead of a fixed pause (a chop takes ~1.6 s plus lag)
    known = FULL_MSGS + BROKE_MSG + GOT_MSGS + MISS_MSGS + EMPTY_MSGS + BAD_MSGS + BUSY_MSGS
    waited = 0.0
    while waited < SWING_DELAY and not API.InJournalAny(known):
        API.Pause(0.2)
        waited += 0.2
    API.Pause(0.25)                 # let the rest of the lines arrive

    if API.InJournalAny(BUSY_MSGS):
        API.Pause(1.0)
        return "retry"
    if API.InJournalAny(FULL_MSGS):
        return "full"
    if API.InJournalAny(GOT_MSGS):
        survey_after_wood(x, y)
        if API.InJournalAny(BROKE_MSG):
            note("An axe broke; grabbing the next one.", 43)
        return "wood"
    if API.InJournalAny(BROKE_MSG):
        return "broke"
    if API.InJournalAny(MISS_MSGS):
        return "miss"
    if API.InJournalAny(EMPTY_MSGS):
        return "empty"
    if API.InJournalAny(FAR_MSGS):
        CTRL["heard"] = next((m for m in FAR_MSGS if API.InJournal(m)), "")
        return "far"
    if API.InJournalAny(BAD_MSGS):
        CTRL["heard"] = next((m for m in BAD_MSGS if API.InJournal(m)), "")
        return "bad"
    heard = last_journal_line()
    if heard:
        dbg("No result recognized; last message: " + heard)
    return "none"


def _chop_tree(x, y):
    tr = tree_at(x, y)
    if tr is None:
        return "skip"
    g, z = tr
    swings = blanks = retries = 0
    got = False
    here = (API.Player.X, API.Player.Y)
    while swings < MAX_SWINGS_TREE:
        checkpoint()
        if (API.Player.X, API.Player.Y) != here:
            return "moved"
        u = unload_check()
        if u != "ok":
            return u                # "trip" or "stop"
        if not ensure_tools():
            sysmsg("[Lumber] Out of axes and can't make more.", 33)
            return "stop"
        r = swing(x, y, g, z)
        log("chop {},{} (0x{:04X}) -> {}{}".format(
            x, y, g, r, ("  heard: " + (CTRL.pop("heard", "") or last_journal_line()))
            if r in ("none", "bad", "far", "retry") else ""))
        swings += 1
        if r == "notool":
            if ensure_tools(force=True):
                continue
            sysmsg("[Lumber] No hatchet or axe, and can't make one.", 33)
            return "stop"
        if r == "full":
            u = unload_check(force=True)
            if u == "ok":
                continue
            if u == "stop":
                sysmsg("[Lumber] Overloaded. Go unload.", 33)
            return u
        if r == "broke":
            continue                # grab the next axe on the next chop
        if r == "retry":
            swings -= 1
            retries += 1
            if retries > 6:
                return "skip"       # axes keep failing: leave this tree for later
            continue
        if r == "wood":
            got = True
            blanks = 0
            continue
        if r == "miss":
            blanks = 0
            continue                # the tree still has wood
        if r == "empty":
            return "wood_done" if got else "empty"
        if r == "far":
            return "far"
        if r == "bad":
            if not got:
                learn_bad(g)
            return "wood" if got else "bad"
        blanks += 1                 # no message; lag or unknown text
        if blanks >= 3:
            break
    return "wood" if got else "bad"


def chop_tree(x, y):
    """Chop one tree until its grove is out of wood. Returns stop/trip/moved or a result."""
    if (x, y) in TRIED or bank(x, y) in DEPLETED:
        return "skip"
    mark_at(x, y, HUE_ACTIVE, 3600)
    result = _chop_tree(x, y)
    if result == "moved":
        _unmark(x, y)
        return "moved"
    if result in ("skip", "far"):
        _unmark(x, y)
        if result == "far":
            CTRL.setdefault("far", set()).add((x, y))   # try it from another spot
        return "skip"
    if result in ("stop", "trip"):
        _unmark(x, y)
        return result
    if result == "wood_done":       # got wood, then the grove ran out
        deplete(x, y)
        result = "wood"
    TRIED[(x, y)] = result
    if result == "empty":
        deplete(x, y)
    mark_at(x, y, wood_hue(x, y) if result == "wood" else HUE_DRY, MARK_SECONDS or 3600)
    return result


def chop_here():
    """Chop every tree in reach of the current spot. Returns done / trip / stop / moved."""
    VISITED.add((API.Player.X, API.Player.Y))
    CTRL["moved"] = False
    CTRL["far"] = set()
    set_state("Chopping")
    px, py = API.Player.X, API.Player.Y
    v = KNOWN.get(bank(px, py))
    if survey_on() and v and wood_ok(v):
        API.HeadMsg("{} grove{}".format(v, "" if logged_near(v, px, py) else " (new!)"), API.Player, 68)
    done = 0
    for _ in range(40):
        checkpoint()
        if CTRL.get("moved"):
            return "moved"
        trees = trees_in_reach()
        if not trees:
            break
        x, y = trees[0]
        r = chop_tree(x, y)
        if r in ("moved", "stop", "trip"):
            return r
        if r != "skip":
            done += 1
            if SHOW_PROGRESS:
                API.HeadMsg("Tree {}/{}".format(done, done + len(trees) - 1), API.Player, 53)
    return "done"

def standable(x, y):
    if LINK_SRC and (cur_map(), x, y) in LINK_SRC:
        return False            # a teleporter: stepping there sends you away
    lg, limp, sg, simp, _ = tile_info(x, y)
    if lg < 0:
        return True             # unknown: let the pathfinder decide
    return not limp and not simp


def dbg(msg):
    if MOVE_DEBUG:
        sysmsg("[Lumber] " + msg, 90)
    else:
        log("[debug] " + msg)


class Grid:
    """Counts usable trees in any box fast (summed-area table)."""
    def __init__(self, x0, y0, x1, y1, fn):
        self.x0, self.y0 = x0, y0
        w, h = x1 - x0 + 1, y1 - y0 + 1
        self.w, self.h = w, h
        self.sat = [[0] * (w + 1) for _ in range(h + 1)]
        self.total = 0
        for j in range(h):
            row = 0
            for i in range(w):
                v = fn(x0 + i, y0 + j)
                v = (1 if v is True else v) or 0
                self.total += v
                row += v
                self.sat[j + 1][i + 1] = self.sat[j][i + 1] + row

    def count(self, xa, ya, xb, yb):
        i0 = max(0, xa - self.x0)
        j0 = max(0, ya - self.y0)
        i1 = min(self.w - 1, xb - self.x0)
        j1 = min(self.h - 1, yb - self.y0)
        if i1 < i0 or j1 < j0:
            return 0
        S = self.sat
        return S[j1 + 1][i1 + 1] - S[j0][i1 + 1] - S[j1 + 1][i0] + S[j0][i0]


def compass(dx, dy):
    names = ["E", "SE", "S", "SW", "W", "NW", "N", "NE"]   # UO: +y is south
    ang = math.atan2(dy, dx)
    return names[int(round(ang / (math.pi / 4))) % 8]


def find_spots(start):
    """Score every standable spot in range; best first as [(x, y, fresh, ahead)]."""
    px, py = API.Player.X, API.Player.Y
    pad = RADIUS + LOOKAHEAD
    x0, y0 = start[0] - WANDER_RANGE - pad, start[1] - WANDER_RANGE - pad
    x1, y1 = start[0] + WANDER_RANGE + pad, start[1] + WANDER_RANGE + pad
    grid = Grid(x0, y0, x1, y1, usable)
    vgrid = Grid(x0, y0, x1, y1, tree_value) if survey_on() else None
    dbg("{} unchopped trees in range".format(grid.total))

    hx, hy = HEADING
    hlen = math.hypot(hx, hy)
    cands = []
    for x in range(start[0] - WANDER_RANGE, start[0] + WANDER_RANGE + 1):
        for y in range(start[1] - WANDER_RANGE, start[1] + WANDER_RANGE + 1):
            dx, dy = x - px, y - py
            d = max(abs(dx), abs(dy))
            if d < 1 or (x, y) in VISITED:
                continue
            fresh = grid.count(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS)
            if fresh < MIN_FRESH or not standable(x, y):
                continue
            ahead = grid.count(x - pad, y - pad, x + pad, y + pad) - fresh
            # a spot right next to the trees beats one at the edge of reach
            near = grid.count(x - 1, y - 1, x + 1, y + 1)
            score = W_FRESH * fresh + 0.5 * near + W_AHEAD * ahead - W_DIST * d
            if vgrid is not None:
                vin = vgrid.count(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS)
                score += vin + W_AHEAD * (vgrid.count(x - pad, y - pad, x + pad, y + pad) - vin)
            if hlen > 0 and d > 0:
                cosang = (dx * hx + dy * hy) / (math.hypot(dx, dy) * hlen)
                score += W_HEADING * cosang
            cands.append((score, x, y, fresh, ahead))
    cands.sort(reverse=True)
    return [(x, y, f, a) for _, x, y, f, a in cands], grid

# TazUO's Pathfind z default is int.MinValue ("use the ground height there").
# The API.py stub shows 1337, but passing 1337 makes the pathfinder aim at
# height 1337, which it can never reach. Always pass this instead.
ANY_Z = -2147483648


def settle_after_jump(secs=2.5):
    """Right after a teleport the client is still loading the new area and its
    pathfinder fails; wait for it."""
    while time.time() - POS.get("jump_t", 0) < secs:
        API.ProcessCallbacks()
        API.Pause(0.25)


def _sgn(v):
    return 1 if v > 0 else (-1 if v < 0 else 0)


def _teleporter_in_the_way(x, y):
    if not LINK_SRC:
        return False
    mp = cur_map()
    px, py = API.Player.X, API.Player.Y
    x0, x1 = min(px, x) - 2, max(px, x) + 2
    y0, y1 = min(py, y) - 2, max(py, y) + 2
    for m, tx, ty in LINK_SRC:
        if m == mp and x0 <= tx <= x1 and y0 <= ty <= y1 and (tx, ty) != (x, y):
            return True
    return False


def step_along(path):
    """Walk a planned path one step at a time (the game's pathfinder would happily
    cut across a teleporter). True if we reached the end."""
    names = {(0, -1): "north", (1, -1): "northeast", (1, 0): "east", (1, 1): "southeast",
             (0, 1): "south", (-1, 1): "southwest", (-1, 0): "west", (-1, -1): "northwest"}
    for nx, ny in path[1:]:
        checkpoint()
        px, py = API.Player.X, API.Player.Y
        # plain ints: in TazUO's IronPython, bool math on the client's numbers made
        # (-1, -1) keys that didn't match this table (KeyError, 2026-10-05)
        d = (_sgn(int(nx) - int(px)), _sgn(int(ny) - int(py)))
        if d == (0, 0):
            continue
        if max(abs(nx - px), abs(ny - py)) > 1:
            return False
        name = names.get(d)
        if name is None:
            return False
        API.Walk(name)
        t = 0.0
        while t < 1.2:
            API.Pause(0.15)
            t += 0.15
            if (API.Player.X, API.Player.Y) == (nx, ny):
                break
        if (API.Player.X, API.Player.Y) != (nx, ny):
            # turning to face a new direction eats the first step: try once more
            API.Walk(name)
            API.Pause(0.5)
            if (API.Player.X, API.Player.Y) != (nx, ny):
                return False
    return True


def walk_to(x, y, near=1, arrow=True, timeout=15):
    """Pathfind to (x, y). True if we ended up within `near` tiles of it."""
    settle_after_jump()
    if near > 0 and _teleporter_in_the_way(x, y):
        path = _plan((API.Player.X, API.Player.Y), (x, y), near, 6)
        if path:
            dbg("Walking around a teleporter")
            step_along(path)
            track_position()
            return max(abs(API.Player.X - x), abs(API.Player.Y - y)) <= near
    ox, oy = API.Player.X, API.Player.Y
    if opt("marks"):
        try:
            API.MarkTile(x, y, HUE_DEST)
        except Exception:
            pass
    if arrow:
        try:
            API.TrackingArrow(x, y)
        except Exception:
            pass
    try:
        API.Pathfind(x, y, ANY_Z, near, False, timeout)
    except Exception as e:
        dbg("Pathfind error: " + str(e))
    waited = 0.0
    try:
        while API.Pathfinding() and waited < timeout:
            checkpoint()
            API.Pause(0.25)
            waited += 0.25
    except Halt:
        API.CancelPathfinding()
        raise
    if opt("marks"):
        try:
            API.RemoveMarkedTile(x, y)
        except Exception:
            pass
    try:
        API.TrackingArrow(-1, -1)
    except Exception:
        pass
    track_position()            # notice a teleport right away
    moved = (API.Player.X, API.Player.Y) != (ox, oy)
    if moved:
        HEADING[0] = float(API.Player.X - ox)
        HEADING[1] = float(API.Player.Y - oy)
    close = max(abs(API.Player.X - x), abs(API.Player.Y - y)) <= near
    return close and (moved or near > 0)


# ---- long-distance walking ------------------------------------------------------
# Every tile you stand on (while the window is open) is remembered as a proven
# stepping stone. Long walks hop between stepping stones when it can, and
# between straight-line points with detours when it can't.
CRUMBS = []                 # [(x, y)] walked tiles on this map
CRUMB_VAR = "SL_Lumber_Crumbs"


def dist2(a, b):
    return max(abs(a[0] - b[0]), abs(a[1] - b[1]))


def track_position():
    now = time.time()
    q = (cur_map(fresh=True), API.Player.X, API.Player.Y)
    last = POS["last"]
    dt = now - POS.get("t", now)
    POS["t"] = now
    if last is not None and q != last:
        # running mounted covers ~10 tiles a second, so allow for time between looks
        if q[0] != last[0] or _cheb(q[1], q[2], last[1], last[2]) > max(6, 12 * dt + 2):
            POS["jumps"] += 1
            POS["jump_t"] = now
            log("teleported: map {} {},{} -> map {} {},{}".format(last[0], last[1], last[2], q[0], q[1], q[2]))
            # walked into it (not recall / gate travel, which start standing still)
            if LEARN_TELEPORTS and now - POS["moved_t"] < 2.5:
                learn_link(last, q, POS["prev"])
            if q[0] != last[0]:
                on_map_change()
            POS["prev"] = None
        else:
            POS["prev"] = last
            POS["moved_t"] = now
    POS["last"] = q
    p = (API.Player.X, API.Player.Y)
    if not CRUMBS or min(dist2(p, c) for c in CRUMBS[-40:]) >= CRUMB_STEP:
        CRUMBS.append(p)
        if len(CRUMBS) > MAX_CRUMBS:
            del CRUMBS[:len(CRUMBS) - MAX_CRUMBS]


def load_crumbs():
    try:
        raw = API.GetPersistentVar(CRUMB_VAR, "", API.PersistentVar.Char) or ""
        mp, _, pts = str(raw).partition("|")
        if pts and int(mp) == API.GetMap():
            for e in pts.split(";"):
                x, y = e.split(",")
                CRUMBS.append((int(x), int(y)))
    except Exception:
        pass


def save_crumbs():
    try:
        API.SavePersistentVar(CRUMB_VAR, "{}|{}".format(
            API.GetMap(), ";".join("{},{}".format(x, y) for x, y in CRUMBS[-MAX_CRUMBS:])),
            API.PersistentVar.Char)
    except Exception:
        pass


BLOCKED = set()             # tiles a hop could not reach (per trip)


def walkable(x, y):
    return (x, y) not in BLOCKED and standable(x, y)


def plan_path(start, goal, near):
    """A* over the client's tile data; searches wider if the first try finds nothing."""
    for pad in (20, 45):
        path = _plan(start, goal, near, pad)
        if path:
            return path
    return None


def _plan(start, goal, near, pad):
    x0 = min(start[0], goal[0]) - pad
    x1 = max(start[0], goal[0]) + pad
    y0 = min(start[1], goal[1]) - pad
    y1 = max(start[1], goal[1]) + pad
    known = set(CRUMBS)

    def h(p):
        return max(0, dist2(p, goal) - near)

    openq = [(h(start), 0.0, start)]
    came = {start: None}
    cost = {start: 0.0}
    expanded = 0
    t_end = time.time() + 15        # every unknown tile read costs a frame: don't plan forever
    mp = cur_map()
    while openq and expanded < 4000 and time.time() < t_end:
        _, g, cur = heapq.heappop(openq)
        if g > cost.get(cur, 1e9):
            continue
        if dist2(cur, goal) <= near:
            path = []
            while cur is not None:
                path.append(cur)
                cur = came[cur]
            path.reverse()
            return path
        expanded += 1
        cx, cy = cur
        for dx in (-1, 0, 1):
            for dy in (-1, 0, 1):
                if not dx and not dy:
                    continue
                nx, ny = cx + dx, cy + dy
                if nx < x0 or nx > x1 or ny < y0 or ny > y1:
                    continue
                # right around the start, trust the game over our tile reading
                near_start = dist2((nx, ny), start) <= 2
                if (mp, nx, ny) in LINK_SRC and (nx, ny) != goal:
                    continue            # a teleporter would send us away
                if not near_start and not walkable(nx, ny):
                    continue
                if not near_start and dx and dy and not (walkable(cx + dx, cy) and walkable(cx, cy + dy)):
                    continue            # no cutting corners
                step = 0.7 if (nx, ny) in known else 1.0
                ng = g + step
                if ng < cost.get((nx, ny), 1e9):
                    cost[(nx, ny)] = ng
                    came[(nx, ny)] = cur
                    heapq.heappush(openq, (ng + h((nx, ny)), ng, (nx, ny)))
    return None


def _go_to(tx, ty, near=1, label=""):
    """Walk anywhere on this map: plan a route, walk it in hops, re-plan around
    anything the game refuses. True if we got within `near`. Stops if a
    teleporter moves us (go_to re-plans)."""
    target = (tx, ty)
    j0 = POS["jumps"]
    try:
        API.TrackingArrow(tx, ty)
    except Exception:
        pass
    try:
        # the game's own pathfinder handles elevation, houses and creatures:
        # give it the whole trip first, then fall back to planned hops
        if walk_to(tx, ty, near, arrow=False, timeout=30):
            return True
        if POS["jumps"] != j0:
            return False
        if time.time() - POS.get("jump_t", 0) < 10:
            dbg("Just teleported; trying the walk again")
            API.Pause(1.5)
            if walk_to(tx, ty, near, arrow=False, timeout=30):
                return True
        dbg("Direct path failed, planning hops")
        replans = 0
        path = None
        while replans <= 8:
            checkpoint()
            if POS["jumps"] != j0:
                return False
            me = (API.Player.X, API.Player.Y)
            if dist2(me, target) > TRIP_MAX_DIST:
                dbg("Too far to plan a walk ({} tiles)".format(dist2(me, target)))
                return False
            if dist2(me, target) <= near:
                return True
            if path is None:
                path = plan_path(me, target, near)
                if path is None:
                    dbg("No route found to {},{}".format(tx, ty))
                    # last resort: let the game's own pathfinder try
                    return dist2(me, target) <= HOP * 2 and walk_to(tx, ty, near, arrow=False)
                dbg("Route planned: {} steps".format(len(path)))
            # the farthest point on the route within a hop
            idx = 0
            for k, p in enumerate(path):
                if dist2(me, p) <= 1:
                    idx = k
            wp_i = min(len(path) - 1, idx + HOP)
            wp = path[wp_i]
            if wp == me:
                return dist2(me, target) <= near
            last = wp_i >= len(path) - 1
            ok = walk_to(tx, ty, near, arrow=False) if last else walk_to(wp[0], wp[1], 1, arrow=False)
            if POS["jumps"] != j0:
                return False
            if ok and (API.Player.X, API.Player.Y) == me:
                ok = False              # "arrived" without moving: treat as stuck
            if ok:
                if label:
                    API.HeadMsg("{} ({} tiles)".format(label, dist2((API.Player.X, API.Player.Y), target)),
                                API.Player, 88)
                continue
            # the game would not take us there: block it and re-plan
            dbg("Hop to {},{} failed, re-planning".format(wp[0], wp[1]))
            BLOCKED.add(wp)
            if wp_i > idx + 1:
                BLOCKED.add(path[(idx + wp_i) // 2])
            path = None
            replans += 1
        return False
    finally:
        try:
            API.TrackingArrow(-1, -1)
        except Exception:
            pass
        BLOCKED.clear()
        save_crumbs()


# ---- teleporters (dungeon and mine entrances) ---------------------------------------
# Most cave and dungeon entrances are invisible server teleporters the client can't
# see. The script starts from ModernUO's standard list (Distribution/Data/
# teleporters.json: map,x,y -> map,x,y, back) and learns any other teleporter you
# walk through with the window open. Long walks (storage trips, going back to the
# trees) use them when that is much shorter, e.g. the New Haven mine:
# 3508,2777 outside <-> 5912,355 inside.
TELEPORTER_DATA = "0,311,786,0,314,784,0;0,311,786,0,5750,350,0;0,313,786,0,5748,361,0;0,314,786,0,5749,361,0;0,424,3283,0,5267,2757,0;0,512,1559,0,5394,127,1;0,513,1559,0,5395,127,1;0,514,1559,0,5396,127,1;0,588,1637,0,6472,868,1;0,766,1645,0,6174,21,0;0,766,1646,0,6174,22,0;0,766,1647,0,6174,23,0;0,1013,1433,0,5138,2016,0;0,1013,1434,0,5138,2016,0;0,1142,3621,0,1414,3828,0;0,1163,2204,0,1171,2202,0;0,1163,2208,0,1165,2214,0;0,1165,2204,0,1163,2211,0;0,1166,2237,0,1163,2205,0;0,1406,3996,0,1414,3828,0;0,1409,3824,0,1124,3623,0;0,1419,3832,0,1466,4015,0;0,1491,1640,0,6032,1499,0;0,1491,1642,0,6032,1501,0;0,1593,2488,0,1600,2489,0;0,1594,2488,0,1600,2489,0;0,1595,2488,0,1600,2489,0;0,1629,3320,0,5899,1411,0;0,1653,2963,0,1677,2987,0;0,1677,2987,0,1675,2987,0;0,1714,2996,0,6308,892,0;0,1714,2996,0,6309,891,0;0,1714,2997,0,6308,892,0;0,1714,2997,0,6309,892,0;0,1987,2062,0,5826,464,0;0,1988,2062,0,5827,464,0;0,1989,2062,0,5828,464,0;0,1997,81,0,5881,242,0;0,2384,836,0,5615,1996,1;0,2384,837,0,5615,1997,1;0,2384,838,0,5615,1998,1;0,2399,198,0,5753,436,0;0,2400,198,0,5754,436,0;0,2420,883,0,5392,1959,1;0,2421,883,0,5393,1959,1;0,2422,883,0,5394,1959,1;0,2455,858,0,5388,2027,1;0,2456,858,0,5389,2027,1;0,2457,858,0,5390,2027,1;0,2498,916,0,5455,1864,1;0,2499,916,0,5456,1864,1;0,2500,916,0,5457,1864,1;0,2544,850,0,5578,1927,1;0,2545,850,0,5579,1927,1;0,2546,850,0,5580,1927,1;0,2603,2120,0,2605,2130,0;0,2603,2121,0,2605,2130,0;0,2618,977,0,2727,2133,1;0,2669,2071,0,2666,2099,0;0,2669,2072,0,2666,2099,0;0,2669,2073,0,2666,2099,0;0,2676,2241,0,2691,2234,0;0,2676,2242,0,2691,2234,0;0,2685,2063,0,2685,2063,0;0,2758,2092,0,2756,2097,0;0,2759,2092,0,2756,2097,0;0,2776,895,0,5685,387,0;0,2923,3405,0,5687,1423,0;0,2985,2890,0,5974,2697,0;0,4110,430,0,5187,639,0;0,4111,430,0,5188,639,0;0,4112,430,0,5189,639,0;0,4300,968,0,4442,1122,0;0,4436,1107,0,4300,992,0;0,4443,1137,0,4487,1475,0;0,4449,1107,0,4539,890,0;0,4449,1115,0,4671,1135,0;0,4496,1475,0,4442,1122,0;0,4540,898,0,4442,1122,0;0,4619,1203,0,4622,1204,0;0,4627,1207,0,4633,1202,0;0,4663,1134,0,4442,1122,0;0,4721,3813,0,5904,16,1;0,4722,3813,0,5905,16,1;0,4723,3813,0,5906,16,1;0,5126,3143,0,5849,432,0;0,5129,909,0,5142,798,0;0,5130,909,0,5143,798,0;0,5131,909,0,5144,798,0;0,5132,909,0,5145,798,0;0,5132,1946,0,5332,1379,0;0,5133,984,0,5152,808,0;0,5133,985,0,5152,809,0;0,5133,986,0,5152,810,0;0,5133,987,0,5152,811,0;0,5133,1946,0,5332,1379,0;0,5134,1946,0,5332,1379,0;0,5135,1946,0,5332,1379,0;0,5137,3664,0,6025,1344,0;0,5137,3665,0,6025,1345,0;0,5139,2015,0,1014,1434,0;0,5139,2016,0,1014,1434,0;0,5139,2017,0,1014,1434,0;0,5140,973,0,5331,707,0;0,5140,1773,0,5171,1586,0;0,5142,797,0,5129,908,0;0,5143,797,0,5130,908,0;0,5144,797,0,5131,908,0;0,5145,797,0,5132,908,0;0,5145,973,0,5201,1564,0;0,5150,4062,0,6005,1378,0;0,5150,4063,0,6005,1380,0;0,5153,808,0,5134,984,0;0,5153,809,0,5134,985,0;0,5153,810,0,5134,986,0;0,5153,811,0,5134,987,0;0,5172,1589,0,5143,1774,0;0,5186,639,0,4110,430,0;0,5187,639,0,4110,430,0;0,5188,639,0,4111,430,0;0,5189,639,0,4112,430,0;0,5191,152,0,1367,891,0;0,5200,71,0,5211,22,0;0,5203,2327,0,5876,147,0;0,5207,2322,0,5877,147,0;0,5207,2323,0,5876,147,0;0,5216,586,0,5304,533,0;0,5217,18,0,5204,74,0;0,5217,586,0,5305,533,0;0,5218,586,0,5306,533,0;0,5218,761,0,5305,651,0;0,5219,761,0,5306,651,0;0,5242,1007,0,1175,2635,1;0,5243,1007,0,1176,2635,1;0,5244,1007,0,1177,2635,1;0,5265,669,0,5876,1378,0;0,5265,683,0,5606,802,0;0,5267,2757,0,424,3283,0;0,5272,2043,0,5363,1290,0;0,5273,2043,0,5363,1290,0;0,5304,532,0,5216,585,0;0,5304,676,0,7109,1097,0;0,5305,532,0,5217,585,0;0,5305,650,0,5218,760,0;0,5306,532,0,5218,585,0;0,5306,650,0,5219,760,0;0,5329,1381,0,5134,1947,0;0,5330,1381,0,5134,1947,0;0,5331,1381,0,5134,1947,0;0,5332,1381,0,5134,1947,0;0,5333,1381,0,5134,1947,0;0,5334,1381,0,5134,1947,0;0,5340,1599,0,5426,3122,0;0,5341,1599,0,5426,3122,0;0,5346,578,0,5137,649,1;0,5356,1540,0,5331,707,0;0,5360,1540,0,5876,1378,0;0,5363,1289,0,5272,2041,0;0,5364,1289,0,5272,2041,0;0,5386,755,0,5408,858,0;0,5386,756,0,5408,859,0;0,5386,757,0,5408,860,0;0,5400,1288,0,5879,3817,0;0,5400,1289,0,5879,3817,0;0,5400,1290,0,5879,3818,0;0,5409,858,0,5387,755,0;0,5409,859,0,5387,756,0;0,5409,860,0,5387,757,0;0,5426,3123,0,5341,1602,0;0,5443,2325,0,5814,3548,0;0,5451,1360,0,5435,1341,0;0,5453,1360,0,5436,1341,0;0,5466,1804,0,5593,1840,0;0,5466,1805,0,5593,1841,0;0,5466,1806,0,5593,1842,0;0,5490,19,0,5514,10,1;0,5503,3971,0,5972,2418,0;0,5504,569,0,5574,628,0;0,5504,570,0,5574,629,0;0,5504,571,0,5574,630,0;0,5506,814,0,5164,1009,0;0,5506,817,0,5201,1564,0;0,5513,176,0,5540,187,0;0,5522,672,0,5573,632,0;0,5522,673,0,5573,633,0;0,5522,674,0,5573,634,0;0,5527,1340,0,5412,1396,0;0,5538,170,0,5517,176,0;0,5543,1355,0,5572,1307,0;0,5551,1805,0,5556,1825,0;0,5552,1805,0,5557,1825,0;0,5553,1805,0,5557,1825,0;0,5556,1826,0,5551,1806,0;0,5557,1317,0,5396,1297,0;0,5557,1826,0,5552,1806,0;0,5566,1337,0,5620,1341,0;0,5571,1299,0,6012,3786,0;0,5572,632,0,5521,672,0;0,5572,633,0,5521,673,0;0,5572,634,0,5521,674,0;0,5572,1299,0,6013,3786,0;0,5573,628,0,5503,569,0;0,5573,629,0,5503,570,0;0,5573,630,0,5503,571,0;0,5588,630,0,1296,1080,1;0,5588,631,0,1296,1081,1;0,5588,632,0,1296,1082,1;0,5594,1840,0,5467,1804,0;0,5594,1841,0,5467,1805,0;0,5604,102,0,5514,147,1;0,5658,423,0,5697,3659,0;0,5670,2391,0,5753,325,0;0,5682,1437,0,5164,1009,0;0,5682,1440,0,5606,802,0;0,5686,385,0,2777,894,0;0,5686,386,0,2777,894,0;0,5686,387,0,2777,895,0;0,5687,1424,0,2923,3406,0;0,5690,569,0,5829,595,0;0,5691,1348,0,5690,1352,1;0,5697,3660,0,5658,424,0;0,5698,662,0,5793,527,0;0,5698,662,0,5793,527,0;0,5701,1320,0,5786,1336,0;0,5702,1320,0,5787,1336,0;0,5703,1320,0,5788,1336,0;0,5705,146,0,5705,305,0;0,5705,147,0,5705,306,0;0,5705,625,0,5703,639,0;0,5706,305,0,5705,146,0;0,5706,306,0,5705,147,0;0,5731,445,0,6087,3676,0;0,5731,3420,0,5490,2348,0;0,5733,554,0,5829,595,0;0,5748,362,0,313,786,0;0,5749,362,0,313,786,0;0,5750,362,0,314,786,0;0,5753,324,0,5670,2391,0;0,5753,437,0,2400,199,0;0,5757,2907,0,5791,1415,0;0,5757,2908,0,5791,1416,0;0,5757,2909,0,5791,1417,0;0,5786,1335,0,5701,1319,0;0,5787,1335,0,5702,1319,0;0,5788,1335,0,5703,1319,0;0,5792,1415,0,5758,2907,0;0,5792,1416,0,5758,2908,0;0,5792,1417,0,5758,2909,0;0,5824,631,0,2041,215,1;0,5825,631,0,2042,215,1;0,5825,631,0,2043,215,1;0,5826,465,0,1987,2063,0;0,5827,465,0,1988,2063,0;0,5827,593,0,5690,569,0;0,5828,465,0,1989,2063,0;0,5829,593,0,5690,569,0;0,5831,323,0,5849,238,0;0,5832,323,0,5850,238,0;0,5833,323,0,5851,238,0;0,5834,323,0,5852,238,0;0,5835,323,0,5853,238,0;0,5849,239,0,5831,324,0;0,5850,239,0,5832,324,0;0,5850,431,0,5127,3143,0;0,5850,432,0,5127,3143,0;0,5850,433,0,5127,3143,0;0,5850,434,0,5127,3143,0;0,5851,239,0,5833,324,0;0,5852,239,0,5834,324,0;0,5853,239,0,5835,324,0;0,5867,528,0,5703,639,0;0,5867,529,0,5703,639,0;0,5868,528,0,5703,639,0;0,5868,529,0,5703,639,0;0,5870,530,0,5690,569,0;0,5870,531,0,5690,569,0;0,5871,530,0,5690,569,0;0,5871,531,0,5690,569,0;0,5874,146,0,5208,2323,0;0,5875,19,0,5507,162,1;0,5875,146,0,5208,2322,0;0,5876,146,0,5208,2322,0;0,5877,146,0,5208,2322,0;0,5878,3817,0,5399,1289,0;0,5878,3818,0,5399,1290,0;0,5882,241,0,1998,81,0;0,5882,242,0,1998,81,0;0,5882,243,0,1998,81,0;0,5899,1411,0,1630,3320,0;0,5900,1411,0,1630,3320,0;0,5919,168,0,6083,144,0;0,5919,169,0,6083,145,0;0,5919,170,0,6083,146,0;0,5918,1410,0,5961,1408,0;0,5918,1411,0,5961,1408,0;0,5918,1412,0,5961,1409,0;0,5920,168,0,6083,144,0;0,5920,169,0,6083,145,0;0,5920,170,0,6083,146,0;0,5926,145,0,5970,147,0;0,5926,146,0,5970,147,0;0,5926,147,0,5970,147,0;0,5926,148,0,5970,147,0;0,5926,149,0,5970,147,0;0,5933,145,0,5970,147,0;0,5933,146,0,5970,147,0;0,5933,147,0,5970,147,0;0,5933,149,0,5970,147,0;0,5944,144,0,5970,147,0;0,5944,145,0,5970,147,0;0,5944,146,0,5970,147,0;0,5944,147,0,5970,147,0;0,5944,148,0,5970,147,0;0,5955,144,0,5970,147,0;0,5955,145,0,5970,147,0;0,5955,146,0,5970,147,0;0,5955,148,0,5970,147,0;0,5955,149,0,5970,147,0;0,5955,150,0,5970,147,0;0,5961,1408,0,5918,1411,0;0,5961,1409,0,5918,1412,0;0,5963,144,0,5970,147,0;0,5963,145,0,5970,147,0;0,5963,146,0,5970,147,0;0,5963,147,0,5970,147,0;0,5963,148,0,5970,147,0;0,5972,168,0,5905,100,0;0,5974,2697,0,2985,2890,0;0,5996,2367,0,5522,3921,0;0,6005,1378,0,5151,4062,0;0,6005,1379,0,5151,4062,0;0,6005,1380,0,5151,4063,0;0,6011,3787,0,5570,1300,0;0,6012,3787,0,5571,1300,0;0,6013,3787,0,5572,1300,0;0,6014,3787,0,5573,1300,0;0,6025,1344,0,5137,3664,0;0,6025,1345,0,5137,3664,0;0,6025,1346,0,5137,3665,0;0,6026,196,0,6987,805,0;0,6026,197,0,6987,805,0;0,6031,1499,0,1491,1640,0;0,6031,1501,0,1491,1642,0;0,6083,144,0,5918,168,0;0,6083,145,0,5918,169,0;0,6083,146,0,5918,170,0;0,6040,194,0,6059,90,1;0,6044,226,0,6041,204,0;0,6053,2407,0,5958,2345,0;0,6075,3332,0,6126,1410,0;0,6080,2340,0,6089,2400,0;0,6083,144,0,5920,168,0;0,6083,145,0,5920,169,0;0,6083,146,0,5920,170,0;0,6086,3676,0,5731,446,0;0,6107,179,0,6124,155,0;0,6115,176,0,6107,176,0;0,6125,1411,0,6075,3332,0;0,6126,1411,0,6075,3332,0;0,6127,1411,0,6075,3332,0;0,6137,1409,0,6140,1432,0;0,6138,1409,0,6140,1432,0;0,6140,1431,0,6137,1408,0;0,6141,1431,0,6138,1408,0;0,6159,130,0,6317,63,0;0,6161,163,0,6321,106,0;0,6164,73,0,6320,22,0;0,6172,21,0,765,1645,0;0,6172,22,0,765,1646,0;0,6172,23,0,765,1647,0;0,6173,176,0,6233,15,0;0,6174,176,0,6234,15,0;0,6175,176,0,6235,15,0;0,6176,176,0,6236,15,0;0,6177,176,0,6237,15,0;0,6178,176,0,6238,15,0;0,6179,176,0,6239,15,0;0,6211,106,0,6355,34,0;0,6233,14,0,6172,174,0;0,6234,14,0,6173,174,0;0,6235,14,0,6174,174,0;0,6236,14,0,6175,174,0;0,6237,14,0,6176,174,0;0,6238,14,0,6177,174,0;0,6239,14,0,6177,174,0;0,6240,14,0,6178,174,0;0,6256,97,0,6257,95,0;0,6260,97,0,6260,95,0;0,6262,95,0,6262,99,0;0,6263,97,0,6262,95,0;0,6269,97,0,6269,95,0;0,6273,97,0,6272,95,0;0,6276,40,0,6374,124,0;0,6310,890,0,1715,2996,0;0,6310,890,0,1716,2997,0;0,6310,891,0,1715,2996,0;0,6310,891,0,1716,2997,0;0,6310,892,0,1715,2997,0;0,6310,892,0,1716,2997,0;0,6310,893,0,1715,2997,0;0,6310,893,0,1716,2997,0;0,6316,62,0,6160,131,0;0,6319,19,0,6165,74,0;0,6356,32,0,6210,105,0;0,6373,49,0,801,1682,0;0,6374,49,0,801,1682,0;0,6374,125,0,6277,41,0;0,6375,49,0,801,1682,0;0,6376,49,0,801,1682,0;0,6377,49,0,801,1682,0;0,6469,97,0,6503,88,0;0,6540,116,0,6537,138,0;0,6540,117,0,6537,139,0;0,6554,104,0,6555,116,0;0,6568,77,0,6576,73,0;0,6477,860,0,6574,889,0;0,6577,159,0,6577,177,0;0,6578,159,0,6577,177,0;0,6579,159,0,6577,177,0;0,6582,109,0,6553,142,0;0,6588,867,0,6488,849,0;0,6589,178,0,6519,116,0;0,6941,987,0,5524,3353,0;0,6972,802,0,5905,21,0;0,6992,1367,5,511,585,0;0,6993,1367,5,512,585,0;0,6994,1367,5,513,585,0;0,7045,1090,0,5405,2640,0;0,7049,709,0,2923,3406,0;0,7149,756,5,974,419,1;0,7150,756,5,975,419,1;0,7151,756,5,976,419,1;0,7152,756,5,977,419,1;1,311,786,1,314,784,0;1,311,786,1,5750,350,0;1,313,786,1,5748,361,0;1,314,786,1,5749,361,0;1,424,3283,1,5267,2757,0;1,512,1559,1,5394,127,1;1,513,1559,1,5395,127,1;1,514,1559,1,5396,127,1;1,588,1637,1,6472,868,1;1,6477,860,1,6574,889,0;1,766,1645,1,6174,21,0;1,766,1646,1,6174,22,0;1,766,1647,1,6174,23,0;1,1013,1433,1,5138,2016,0;1,1013,1434,1,5138,2016,0;1,1142,3621,1,1414,3828,0;1,1163,2204,1,1171,2202,0;1,1163,2208,1,1165,2214,0;1,1165,2204,1,1163,2211,0;1,1166,2237,1,1163,2205,0;1,1406,3996,1,1414,3828,0;1,1409,3824,1,1124,3623,0;1,1419,3832,1,1466,4015,0;1,1477,1471,1,6432,2677,0;1,1477,1472,1,6432,2678,0;1,1477,1473,1,6432,2679,0;1,1477,1474,1,6432,2680,0;1,1477,1475,1,6432,2681,0;1,1491,1640,1,6032,1499,0;1,1491,1642,1,6032,1501,0;1,1593,2488,1,1600,2489,0;1,1594,2488,1,1600,2489,0;1,1595,2488,1,1600,2489,0;1,1629,3320,1,5899,1411,0;1,1653,2963,1,1677,2987,0;1,1677,2987,1,1675,2987,0;1,1714,2996,1,6308,892,0;1,1714,2996,1,6309,891,0;1,1714,2997,1,6308,892,0;1,1714,2997,1,6309,892,0;1,1987,2062,1,5826,464,0;1,1988,2062,1,5827,464,0;1,1989,2062,1,5828,464,0;1,1997,81,1,5881,242,0;1,2384,836,1,5615,1996,1;1,2384,837,1,5615,1997,1;1,2384,838,1,5615,1998,1;1,2399,198,1,5753,436,0;1,2400,198,1,5754,436,0;1,2420,883,1,5392,1959,1;1,2421,883,1,5393,1959,1;1,2422,883,1,5394,1959,1;1,2455,858,1,5388,2027,1;1,2456,858,1,5389,2027,1;1,2457,858,1,5390,2027,1;1,2498,916,1,5455,1864,1;1,2499,916,1,5456,1864,1;1,2500,916,1,5457,1864,1;1,2544,850,1,5578,1927,1;1,2545,850,1,5579,1927,1;1,2546,850,1,5580,1927,1;1,2603,2120,1,2605,2130,0;1,2603,2121,1,2605,2130,0;1,2618,977,1,2727,2133,1;1,2669,2071,1,2666,2099,0;1,2669,2072,1,2666,2099,0;1,2669,2073,1,2666,2099,0;1,2676,2241,1,2691,2234,0;1,2676,2242,1,2691,2234,0;1,2685,2063,1,2685,2063,0;1,2758,2092,1,2756,2097,0;1,2759,2092,1,2756,2097,0;1,2776,895,1,5685,387,0;1,2923,3405,1,5687,1423,0;1,2985,2890,1,5974,2697,0;1,3508,2777,1,5912,355,1;1,4110,430,1,5187,639,0;1,4111,430,1,5188,639,0;1,4112,430,1,5189,639,0;1,4194,3260,5,1127,1215,0;1,4195,3260,5,1129,1215,0;1,4300,968,1,4442,1122,0;1,4436,1107,1,4300,992,0;1,4443,1137,1,4487,1475,0;1,4449,1107,1,4539,890,0;1,4449,1115,1,4671,1135,0;1,4496,1475,1,4442,1122,0;1,4540,898,1,4442,1122,0;1,4619,1203,1,4622,1204,0;1,4627,1207,1,4633,1202,0;1,4663,1134,1,4442,1122,0;1,4721,3813,1,5904,16,1;1,4722,3813,1,5905,16,1;1,4723,3813,1,5906,16,1;1,5126,3143,1,5849,432,0;1,5129,909,1,5142,798,0;1,5130,909,1,5143,798,0;1,5131,909,1,5144,798,0;1,5132,909,1,5145,798,0;1,5132,1946,1,5332,1379,0;1,5133,984,1,5152,808,0;1,5133,985,1,5152,809,0;1,5133,986,1,5152,810,0;1,5133,987,1,5152,811,0;1,5133,1946,1,5332,1379,0;1,5134,1946,1,5332,1379,0;1,5135,1946,1,5332,1379,0;1,5137,3664,1,6025,1344,0;1,5137,3665,1,6025,1345,0;1,5139,2015,1,1014,1434,0;1,5139,2016,1,1014,1434,0;1,5139,2017,1,1014,1434,0;1,5142,797,1,5129,908,0;1,5143,797,1,5130,908,0;1,5144,797,1,5131,908,0;1,5145,797,1,5132,908,0;1,5150,4062,1,6005,1378,0;1,5150,4063,1,6005,1380,0;1,5153,808,1,5134,984,0;1,5153,809,1,5134,985,0;1,5153,810,1,5134,986,0;1,5153,811,1,5134,987,0;1,5186,639,1,4110,430,0;1,5187,639,1,4110,430,0;1,5188,639,1,4111,430,0;1,5189,639,1,4112,430,0;1,5191,152,1,1367,891,0;1,5200,71,1,5211,22,0;1,5203,2327,1,5876,147,0;1,5207,2322,1,5877,147,0;1,5207,2323,1,5876,147,0;1,5216,586,1,5304,533,0;1,5217,18,1,5204,74,0;1,5217,586,1,5305,533,0;1,5218,586,1,5306,533,0;1,5218,761,1,5305,651,0;1,5219,761,1,5306,651,0;1,5242,1007,1,1175,2635,1;1,5243,1007,1,1176,2635,1;1,5244,1007,1,1177,2635,1;1,5260,1841,1,3613,2592,0;1,5267,2757,1,424,3283,0;1,5272,2043,1,5363,1290,0;1,5273,2043,1,5363,1290,0;1,5304,532,1,5216,585,0;1,5305,532,1,5217,585,0;1,5305,650,1,5218,760,0;1,5306,532,1,5218,585,0;1,5306,650,1,5219,760,0;1,5329,1381,1,5134,1947,0;1,5330,1381,1,5134,1947,0;1,5331,1381,1,5134,1947,0;1,5332,1381,1,5134,1947,0;1,5333,1381,1,5134,1947,0;1,5334,1381,1,5134,1947,0;1,5340,1599,1,5426,3122,0;1,5341,1599,1,5426,3122,0;1,5346,578,1,5137,649,1;1,5363,1289,1,5272,2041,0;1,5364,1289,1,5272,2041,0;1,5386,755,1,5408,858,0;1,5386,756,1,5408,859,0;1,5386,757,1,5408,860,0;1,5400,1288,1,5879,3817,0;1,5400,1289,1,5879,3817,0;1,5400,1290,1,5879,3818,0;1,5426,3123,1,5341,1602,0;1,5451,1360,1,5435,1341,0;1,5453,1360,1,5436,1341,0;1,5466,1804,1,5593,1840,0;1,5466,1805,1,5593,1841,0;1,5466,1806,1,5593,1842,0;1,5490,19,1,5514,10,1;1,5504,569,1,5574,628,0;1,5504,570,1,5574,629,0;1,5504,571,1,5574,630,0;1,5513,176,1,5540,187,0;1,5522,672,1,5573,632,0;1,5522,673,1,5573,633,0;1,5522,674,1,5573,634,0;1,5527,1340,1,5412,1396,0;1,5538,170,1,5517,176,0;1,5543,1355,1,5572,1307,0;1,5551,1805,1,5556,1825,0;1,5552,1805,1,5557,1825,0;1,5553,1805,1,5557,1825,0;1,5556,1826,1,5551,1806,0;1,5557,1317,1,5396,1297,0;1,5557,1826,1,5552,1806,0;1,5566,1337,1,5620,1341,0;1,5571,1299,1,6012,3786,0;1,5572,632,1,5521,672,0;1,5572,633,1,5521,673,0;1,5572,634,1,5521,674,0;1,5572,1299,1,6013,3786,0;1,5573,628,1,5503,569,0;1,5573,629,1,5503,570,0;1,5573,630,1,5503,571,0;1,5588,630,1,1296,1080,1;1,5588,631,1,1296,1081,1;1,5588,632,1,1296,1082,1;1,5594,1840,1,5467,1804,0;1,5594,1841,1,5467,1805,0;1,5604,102,1,5514,147,1;1,5658,423,1,5697,3659,0;1,5670,2391,1,5753,325,0;1,5686,385,1,2777,894,0;1,5686,386,1,2777,894,0;1,5686,387,1,2777,895,0;1,5687,1424,1,2923,3406,0;1,5690,569,1,5829,595,0;1,5691,1348,1,5690,1352,1;1,5697,3660,1,5658,424,0;1,5698,662,1,5793,527,0;1,5698,662,1,5793,527,0;1,5701,1320,1,5786,1336,0;1,5702,1320,1,5787,1336,0;1,5703,1320,1,5788,1336,0;1,5705,146,1,5705,305,0;1,5705,147,1,5705,306,0;1,5705,625,1,5703,639,0;1,5706,305,1,5705,146,0;1,5706,306,1,5705,147,0;1,5731,445,1,6087,3676,0;1,5733,554,1,5829,595,0;1,5748,362,1,313,786,0;1,5749,362,1,313,786,0;1,5750,362,1,314,786,0;1,5753,324,1,5670,2391,0;1,5753,437,1,2400,199,0;1,5757,2907,1,5791,1415,0;1,5757,2908,1,5791,1416,0;1,5757,2909,1,5791,1417,0;1,5786,1335,1,5701,1319,0;1,5787,1335,1,5702,1319,0;1,5788,1335,1,5703,1319,0;1,5792,1415,1,5758,2907,0;1,5792,1416,1,5758,2908,0;1,5792,1417,1,5758,2909,0;1,5824,631,1,2041,215,1;1,5825,631,1,2042,215,1;1,5825,631,1,2043,215,1;1,5826,465,1,1987,2063,0;1,5827,465,1,1988,2063,0;1,5827,593,1,5690,569,0;1,5828,465,1,1989,2063,0;1,5829,593,1,5690,569,0;1,5831,323,1,5849,238,0;1,5832,323,1,5850,238,0;1,5833,323,1,5851,238,0;1,5834,323,1,5852,238,0;1,5835,323,1,5853,238,0;1,5849,239,1,5831,324,0;1,5850,239,1,5832,324,0;1,5850,431,1,5127,3143,0;1,5850,432,1,5127,3143,0;1,5850,433,1,5127,3143,0;1,5850,434,1,5127,3143,0;1,5851,239,1,5833,324,0;1,5852,239,1,5834,324,0;1,5853,239,1,5835,324,0;1,5867,528,1,5703,639,0;1,5867,529,1,5703,639,0;1,5868,528,1,5703,639,0;1,5868,529,1,5703,639,0;1,5870,530,1,5690,569,0;1,5870,531,1,5690,569,0;1,5871,530,1,5690,569,0;1,5871,531,1,5690,569,0;1,5874,146,1,5208,2323,0;1,5875,19,1,5507,162,1;1,5875,146,1,5208,2322,0;1,5876,146,1,5208,2322,0;1,5877,146,1,5208,2322,0;1,5878,3817,1,5399,1289,0;1,5878,3818,1,5399,1290,0;1,5882,241,1,1998,81,0;1,5882,242,1,1998,81,0;1,5919,168,1,6083,144,0;1,5919,169,1,6083,145,0;1,5919,170,1,6083,146,0;1,5906,96,1,5977,169,0;1,5906,4069,1,2494,3576,1;1,5918,1410,1,5961,1408,0;1,5918,1411,1,5961,1408,0;1,5918,1412,1,5961,1409,0;1,5920,168,1,6083,144,0;1,5920,169,1,6083,145,0;1,5920,170,1,6083,146,0;1,5926,145,1,5970,147,0;1,5926,146,1,5970,147,0;1,5926,147,1,5970,147,0;1,5926,148,1,5970,147,0;1,5926,149,1,5970,147,0;1,5933,145,1,5970,147,0;1,5933,146,1,5970,147,0;1,5933,147,1,5970,147,0;1,5933,149,1,5970,147,0;1,5944,144,1,5970,147,0;1,5944,145,1,5970,147,0;1,5944,146,1,5970,147,0;1,5944,147,1,5970,147,0;1,5944,148,1,5970,147,0;1,5955,144,1,5970,147,0;1,5955,145,1,5970,147,0;1,5955,146,1,5970,147,0;1,5955,148,1,5970,147,0;1,5955,149,1,5970,147,0;1,5955,150,1,5970,147,0;1,5961,1408,1,5918,1411,0;1,5961,1409,1,5918,1412,0;1,5963,144,1,5970,147,0;1,5963,145,1,5970,147,0;1,5963,146,1,5970,147,0;1,5963,147,1,5970,147,0;1,5963,148,1,5970,147,0;1,5972,168,1,5905,100,0;1,5974,2697,1,2985,2890,0;1,6005,1378,1,5151,4062,0;1,6005,1379,1,5151,4062,0;1,6005,1380,1,5151,4063,0;1,6011,3787,1,5570,1300,0;1,6012,3787,1,5571,1300,0;1,6013,3787,1,5572,1300,0;1,6014,3787,1,5573,1300,0;1,6025,1344,1,5137,3664,0;1,6025,1345,1,5137,3664,0;1,6025,1346,1,5137,3665,0;1,6083,144,1,5918,168,0;1,6083,145,1,5918,169,0;1,6083,146,1,5918,170,0;1,6040,192,1,6059,88,1;1,6040,193,1,6059,89,1;1,6040,194,1,6059,90,1;1,6044,226,1,6041,204,0;1,6075,3332,1,6126,1410,0;1,6083,144,1,5920,168,0;1,6083,145,1,5920,169,0;1,6083,146,1,5920,170,0;1,6086,3676,1,5731,446,0;1,6107,179,1,6124,155,0;1,6115,176,1,6107,176,0;1,6125,1411,1,6075,3332,0;1,6126,1411,1,6075,3332,0;1,6127,1411,1,6075,3332,0;1,6137,1409,1,6140,1432,0;1,6138,1409,1,6140,1432,0;1,6140,1431,1,6137,1408,0;1,6141,1431,1,6138,1408,0;1,6159,130,1,6317,63,0;1,6161,163,1,6321,106,0;1,6164,73,1,6320,22,0;1,6172,21,1,765,1645,0;1,6172,22,1,765,1646,0;1,6172,23,1,765,1647,0;1,6173,176,1,6233,15,0;1,6174,176,1,6234,15,0;1,6175,176,1,6235,15,0;1,6176,176,1,6236,15,0;1,6177,176,1,6237,15,0;1,6178,176,1,6238,15,0;1,6179,176,1,6239,15,0;1,6211,106,1,6355,34,0;1,6233,14,1,6172,174,0;1,6234,14,1,6173,174,0;1,6235,14,1,6174,174,0;1,6236,14,1,6175,174,0;1,6237,14,1,6176,174,0;1,6238,14,1,6177,174,0;1,6239,14,1,6177,174,0;1,6240,14,1,6178,174,0;1,6256,97,1,6257,95,0;1,6260,97,1,6260,95,0;1,6262,95,1,6262,99,0;1,6263,97,1,6262,95,0;1,6269,97,1,6269,95,0;1,6273,97,1,6272,95,0;1,6276,40,1,6374,124,0;1,6310,890,1,1715,2996,0;1,6310,890,1,1716,2997,0;1,6310,891,1,1715,2996,0;1,6310,891,1,1716,2997,0;1,6310,892,1,1715,2997,0;1,6310,892,1,1716,2997,0;1,6310,893,1,1715,2997,0;1,6310,893,1,1716,2997,0;1,6316,62,1,6160,131,0;1,6319,19,1,6165,74,0;1,6356,32,1,6210,105,0;1,6373,49,1,801,1682,0;1,6374,49,1,801,1682,0;1,6374,125,1,6277,41,0;1,6375,49,1,801,1682,0;1,6376,49,1,801,1682,0;1,6377,49,1,801,1682,0;1,6440,2677,1,1477,1471,0;1,6440,2678,1,1477,1472,0;1,6440,2679,1,1477,1473,0;1,6440,2680,1,1477,1474,0;1,6440,2681,1,1477,1475,0;1,6469,97,1,6503,88,0;1,6540,116,1,6537,138,0;1,6540,117,1,6537,139,0;1,6554,104,1,6555,116,0;1,6568,77,1,6576,73,0;1,6577,159,1,6577,177,0;1,6578,159,1,6577,177,0;1,6579,159,1,6577,177,0;1,6582,109,1,6553,142,0;1,6588,867,1,6488,849,0;1,6589,178,1,6519,116,0;2,10,872,2,10,1518,0;2,10,1519,2,10,873,0;2,11,872,2,11,1518,0;2,11,1519,2,11,873,0;2,12,872,2,12,1518,0;2,12,1519,2,12,873,0;2,78,1365,2,393,1586,0;2,79,1365,2,394,1586,0;2,80,1365,2,395,1586,0;2,81,1365,2,396,1586,0;2,131,129,2,284,68,0;2,132,129,2,285,68,0;2,133,129,2,286,68,0;2,134,129,2,287,68,0;2,154,1472,2,575,1155,0;2,155,88,2,357,40,0;2,155,89,2,357,41,0;2,155,90,2,357,42,0;2,155,1472,2,576,1155,0;2,156,1472,2,577,1155,0;2,164,746,2,636,815,0;2,242,25,2,372,29,0;2,242,26,2,372,30,0;2,242,27,2,372,31,0;2,265,130,2,284,72,0;2,265,1587,2,225,1334,1;2,266,130,2,285,72,0;2,266,1587,2,226,1334,1;2,267,130,2,286,72,0;2,267,1587,2,227,1334,1;2,268,130,2,287,72,0;2,272,141,2,555,427,0;2,273,141,2,556,427,0;2,274,141,2,557,427,0;2,284,67,2,131,128,0;2,284,73,2,265,131,0;2,285,67,2,132,128,0;2,285,73,2,266,131,0;2,286,67,2,133,128,0;2,286,73,2,267,131,0;2,287,67,2,134,128,0;2,287,73,2,268,131,0;2,313,1329,2,327,1593,1;2,314,1329,2,328,1593,1;2,315,1329,2,329,1593,1;2,330,1593,2,315,1329,0;2,348,1427,2,18,1198,1;2,349,1427,2,19,1198,1;2,350,1427,2,20,1198,1;2,351,1427,2,21,1198,1;2,358,40,2,156,88,0;2,358,41,2,156,89,0;2,358,42,2,156,90,0;2,371,29,2,241,25,0;2,371,30,2,241,26,0;2,371,31,2,241,27,0;2,393,1587,2,78,1366,0;2,394,1587,2,79,1366,0;2,395,1587,2,80,1366,0;2,396,1587,2,81,1366,0;2,429,113,2,548,455,0;2,531,1533,2,810,875,0;2,532,1533,2,811,875,0;2,533,1533,2,812,875,0;2,534,1533,2,813,875,0;2,546,455,2,426,113,1;2,547,455,2,427,113,1;2,548,455,2,428,113,1;2,555,426,2,272,140,0;2,556,426,2,273,140,0;2,557,426,2,274,140,0;2,575,1156,2,154,1473,0;2,576,1156,2,155,1473,0;2,577,1156,2,156,1473,0;2,626,1527,2,650,1298,0;2,627,1527,2,651,1298,0;2,628,1527,2,652,1298,0;2,629,1527,2,653,1298,0;2,636,813,2,164,743,0;2,650,1297,2,626,1526,0;2,651,1297,2,627,1526,0;2,652,1297,2,628,1526,0;2,653,1297,2,629,1526,0;2,668,928,2,3,1267,1;2,668,929,2,3,1268,1;2,668,930,2,3,1269,1;2,694,1490,2,719,1490,0;2,694,1491,2,719,1491,0;2,694,1492,2,719,1492,0;2,694,1493,2,719,1493,0;2,709,667,2,912,451,0;2,710,667,2,912,452,0;2,711,667,2,912,453,0;2,712,1490,2,686,1490,0;2,712,1491,2,686,1491,0;2,712,1492,2,686,1492,0;2,712,1493,2,686,1493,0;2,722,1505,2,833,1550,0;2,722,1506,2,833,1551,0;2,722,1507,2,833,1552,0;2,722,1508,2,833,1553,0;2,747,1539,2,785,1514,0;2,747,1555,2,785,1570,0;2,751,1473,2,763,1479,0;2,751,1479,2,763,1555,0;2,752,1549,2,751,1484,0;2,775,1467,2,658,1498,0;2,775,1492,2,827,1515,0;2,776,1546,2,798,1547,0;2,776,1549,2,798,1547,0;2,777,1547,2,798,1547,0;2,777,1548,2,798,1547,0;2,777,1553,2,798,1547,0;2,777,1555,2,798,1547,0;2,778,1541,2,798,1547,0;2,778,1554,2,787,1538,0;2,779,1540,2,798,1547,0;2,779,1542,2,798,1547,0;2,780,1541,2,785,1554,0;2,780,1546,2,798,1547,0;2,780,1550,2,798,1547,0;2,780,1554,2,798,1547,0;2,781,1545,2,789,1552,0;2,781,1547,2,798,1547,0;2,781,1549,2,798,1547,0;2,781,1551,2,798,1547,0;2,781,1553,2,798,1547,0;2,781,1555,2,789,1556,0;2,782,1538,2,798,1547,0;2,782,1542,2,785,1546,0;2,782,1546,2,798,1547,0;2,782,1550,2,783,1538,0;2,782,1554,2,798,1547,0;2,783,1537,2,798,1547,0;2,783,1539,2,787,1542,0;2,783,1541,2,798,1547,0;2,783,1543,2,798,1547,0;2,784,1538,2,798,1547,0;2,784,1542,2,798,1547,0;2,784,1546,2,776,1548,0;2,784,1550,2,777,1554,0;2,784,1554,2,798,1547,0;2,784,1580,2,798,1547,0;2,785,1524,2,798,1547,0;2,785,1545,2,798,1547,0;2,785,1547,2,798,1547,0;2,785,1549,2,798,1547,0;2,785,1551,2,798,1547,0;2,785,1553,2,798,1547,0;2,785,1555,2,783,1542,0;2,786,1538,2,798,1547,0;2,786,1542,2,798,1547,0;2,786,1546,2,798,1547,0;2,786,1550,2,798,1547,0;2,786,1554,2,798,1547,0;2,787,1537,2,781,1546,0;2,787,1539,2,798,1547,0;2,787,1541,2,798,1547,0;2,787,1543,2,781,1554,0;2,788,1538,2,798,1547,0;2,788,1542,2,798,1547,0;2,788,1546,2,798,1547,0;2,788,1552,2,798,1547,0;2,788,1556,2,798,1547,0;2,789,1545,2,779,1541,0;2,789,1547,2,798,1547,0;2,789,1551,2,798,1547,0;2,789,1553,2,789,1546,0;2,789,1555,2,798,1547,0;2,789,1557,2,785,1550,0;2,790,1546,2,798,1547,0;2,790,1552,2,798,1547,0;2,790,1556,2,798,1547,0;2,791,1545,2,798,1547,0;2,791,1546,2,798,1547,0;2,791,1547,2,798,1547,0;2,791,1548,2,781,1547,0;2,791,1549,2,798,1547,0;2,791,1550,2,798,1547,0;2,810,874,2,532,1532,0;2,811,874,2,533,1532,0;2,812,874,2,534,1532,0;2,812,1546,2,848,1434,0;2,812,1547,2,848,1435,0;2,812,1548,2,848,1436,0;2,812,1549,2,848,1437,0;2,827,777,2,1975,114,0;2,827,778,2,1975,114,0;2,827,779,2,1975,114,0;2,828,777,2,1975,114,0;2,828,778,2,1975,114,0;2,828,779,2,1975,114,0;2,829,777,2,1975,114,0;2,829,778,2,1975,114,0;2,829,779,2,1975,114,0;2,838,1550,2,728,1505,0;2,838,1551,2,728,1506,0;2,838,1552,2,728,1507,0;2,838,1553,2,728,1508,0;2,843,1434,2,807,1546,0;2,843,1435,2,807,1547,0;2,843,1436,2,807,1548,0;2,843,1437,2,807,1549,0;2,871,1433,2,897,1449,0;2,871,1434,2,897,1450,0;2,871,1435,2,897,1451,0;2,871,1436,2,897,1452,0;2,871,1437,2,897,1453,0;2,879,1490,2,960,1425,0;2,879,1491,2,960,1426,0;2,879,1492,2,960,1427,0;2,879,1493,2,960,1428,0;2,892,1449,2,866,1433,0;2,892,1450,2,866,1434,0;2,892,1451,2,866,1435,0;2,892,1452,2,866,1436,0;2,892,1453,2,866,1437,0;2,904,1360,2,1014,1506,1;2,904,1361,2,1014,1507,1;2,904,1362,2,1014,1508,1;2,904,1363,2,1014,1509,1;2,911,451,2,709,668,0;2,911,452,2,710,668,0;2,911,453,2,711,668,0;2,938,494,2,83,749,1;2,939,494,2,84,749,1;2,940,494,2,85,749,1;2,941,494,2,86,749,1;2,948,1464,2,951,1442,1;2,948,1465,2,951,1443,1;2,948,1466,2,951,1444,1;2,948,1467,2,951,1445,1;2,954,1425,2,874,1490,0;2,954,1426,2,874,1491,0;2,954,1427,2,874,1492,0;2,954,1428,2,874,1493,0;2,954,1429,2,874,1493,0;2,1029,1153,2,1349,1510,1;2,1029,1154,2,1349,1511,1;2,1029,1155,2,1349,1512,1;2,1037,579,2,1790,66,0;2,1037,580,2,1790,67,0;2,1037,581,2,1790,68,0;2,1038,578,2,1786,70,0;2,1038,579,2,1786,70,0;2,1038,581,2,1786,65,0;2,1038,582,2,1786,65,0;2,1039,578,2,1787,70,0;2,1039,582,2,1787,65,0;2,1040,578,2,1788,70,0;2,1040,579,2,1788,70,0;2,1040,581,2,1788,65,0;2,1040,582,2,1788,65,0;2,1041,579,2,1785,66,0;2,1041,580,2,1785,67,0;2,1041,581,2,1785,68,0;2,1139,592,2,1238,583,0;2,1140,592,2,1238,584,0;2,1141,592,2,1238,585,0;2,1142,592,2,1238,585,0;2,1237,583,2,1139,593,0;2,1237,584,2,1140,593,0;2,1237,585,2,1141,593,0;2,1250,1511,2,1268,1510,0;2,1250,1512,2,1268,1510,0;2,1268,1508,2,1250,1508,1;2,1268,1509,2,1250,1509,1;2,1268,1510,2,1250,1510,1;2,1349,1509,2,1030,1153,0;2,1362,1031,2,1981,1107,1;2,1363,1031,2,1982,1107,1;2,1364,1031,2,1983,1107,1;2,1419,909,2,1784,994,1;2,1420,909,2,1785,994,1;2,1421,909,2,1786,994,1;2,1456,1328,2,1479,1494,1;2,1456,1329,2,1479,1495,1;2,1456,1330,2,1479,1496,1;2,1479,1493,2,1456,1328,0;2,1479,1497,2,1456,1330,0;2,1516,879,2,1363,1105,1;2,1745,1236,2,2112,829,1;2,1746,1236,2,2113,829,1;2,1747,1236,2,2114,829,1;2,1748,1236,2,2115,829,1;2,1783,994,2,1419,909,0;2,1786,66,2,1038,583,0;2,1786,67,2,1042,579,0;2,1786,68,2,1042,580,0;2,1786,69,2,1042,581,0;2,1787,66,2,1039,583,0;2,1787,69,2,1038,577,0;2,1787,994,2,1421,909,0;2,1788,66,2,1040,583,0;2,1788,69,2,1039,577,0;2,1788,994,2,1421,909,0;2,1789,66,2,1041,583,0;2,1789,67,2,1036,579,0;2,1789,68,2,1036,578,0;2,1789,69,2,1036,581,0;2,1861,980,2,1490,877,1;2,1861,981,2,1490,878,1;2,1861,982,2,1490,879,1;2,1861,983,2,1490,880,1;2,1861,984,2,1490,880,0;2,1978,114,2,835,778,0;2,1978,115,2,835,778,0;2,1978,116,2,835,778,0;2,1978,117,2,835,778,0;2,1979,114,2,835,778,0;2,1979,115,2,835,778,0;2,1979,116,2,835,778,0;2,1979,117,2,835,778,0;2,1980,114,2,835,778,0;2,1980,115,2,835,778,0;2,1980,116,2,835,778,0;2,1980,117,2,835,778,0;2,1980,1107,2,1362,1031,0;2,1981,114,2,835,778,0;2,1981,115,2,835,778,0;2,1981,116,2,835,778,0;2,1981,117,2,835,778,0;2,1984,1107,2,1364,1031,0;2,2116,829,2,1748,1236,0;2,2186,294,2,2186,33,1;2,2187,294,2,2187,33,1;2,2187,320,2,1787,569,0;2,2188,294,2,2188,33,1;2,2188,320,2,1787,569,1;2,2189,294,2,2189,33,1;2,2189,320,2,1788,569,1;3,3,128,4,259,785,0;3,4,128,4,259,785,0;3,5,128,4,259,785,0;3,6,128,4,259,785,0;3,7,128,4,259,785,0;3,8,128,4,259,785,0;3,61,523,3,63,352,0;3,61,524,3,63,352,0;3,61,525,3,63,352,0;3,61,526,3,63,352,0;3,64,336,4,983,195,0;3,64,337,4,983,195,0;3,64,338,4,983,195,0;3,64,339,4,983,195,0;3,66,351,3,63,524,0;3,66,352,3,63,524,0;3,66,353,3,63,524,0;3,66,354,3,63,524,0;3,73,688,3,100,556,0;3,73,689,3,100,556,0;3,73,690,3,100,556,0;3,73,691,3,100,556,0;3,73,692,3,100,556,0;3,73,693,3,100,556,0;3,73,694,3,100,556,0;3,84,1673,3,156,1613,0;3,103,555,3,76,691,0;3,103,556,3,76,691,0;3,103,557,3,76,691,0;3,103,558,3,76,691,0;3,156,1609,3,87,1673,0;3,157,1609,3,87,1673,0;3,328,1972,3,1731,978,0;3,328,1973,3,1731,978,0;3,328,1974,3,1731,978,0;3,328,1975,3,1731,978,0;3,330,706,3,384,733,0;3,331,769,3,384,733,0;3,355,745,3,384,733,0;3,375,802,4,770,1209,0;3,384,810,3,403,1167,0;3,389,704,3,384,733,0;3,391,777,4,770,1209,0;3,403,1169,3,385,811,0;3,404,1169,3,385,808,0;3,405,1169,3,385,808,0;3,407,254,3,428,318,0;3,408,254,3,428,318,0;3,409,254,3,428,318,0;3,411,1117,3,422,806,0;3,412,1086,3,391,803,0;3,412,1123,3,417,806,0;3,421,1092,3,391,803,0;3,426,780,3,384,733,0;3,427,321,3,422,327,0;3,428,321,3,422,327,0;3,429,321,3,422,327,0;3,496,49,3,2350,1270,0;3,2315,1267,3,381,132,0;3,2315,1268,3,381,132,0;3,2315,1269,3,381,132,0;3,2316,1267,3,381,132,0;3,2316,1269,3,381,132,0;3,2317,1266,3,381,132,0;3,2317,1267,3,381,132,0;3,2317,1268,3,381,132,0;3,2317,1269,3,381,132,0;4,257,783,3,5,128,0;4,258,783,3,5,128,0;4,259,783,3,5,128,0;4,260,783,3,5,128,0;4,987,196,3,67,337,0;4,988,194,3,67,337,0;4,988,195,3,67,337,0;4,988,197,3,67,337,0;5,33,241,5,996,3842,0;5,34,241,5,997,3842,0;5,35,241,5,998,3842,0;5,36,241,5,999,3842,0;5,37,241,5,1000,3842,0;5,40,366,5,79,353,0;5,40,564,5,79,551,0;5,40,772,5,79,759,0;5,45,366,5,81,399,0;5,45,564,5,81,597,0;5,45,772,5,81,805,0;5,52,343,5,80,338,0;5,52,389,5,79,379,0;5,52,541,5,80,536,0;5,52,587,5,79,577,0;5,52,749,5,80,744,0;5,52,795,5,79,785,0;5,79,353,5,40,366,0;5,79,379,5,52,389,0;5,79,551,5,40,564,0;5,79,577,5,52,587,0;5,79,759,5,40,772,0;5,79,785,5,52,795,0;5,80,338,5,52,343,0;5,80,536,5,52,541,0;5,80,744,5,52,749,0;5,81,399,5,45,366,0;5,81,597,5,45,564,0;5,81,805,5,45,772,0;5,317,136,5,310,159,0;5,317,182,5,346,192,0;5,344,146,5,349,138,0;5,344,172,5,349,180,0;5,345,131,5,345,128,0;5,442,152,5,850,272,0;5,442,153,5,850,273,0;5,442,166,5,850,286,0;5,442,167,5,850,287,0;5,511,584,0,6992,1367,0;5,512,584,0,6992,1367,0;5,513,584,0,6993,1367,0;5,514,584,0,6993,1367,0;5,519,919,5,1125,1075,0;5,519,919,5,1125,1076,0;5,520,919,5,1125,1075,0;5,520,919,5,1126,1076,0;5,521,919,5,1126,1075,0;5,522,919,5,1126,1075,0;5,594,3846,5,644,3857,0;5,644,3857,5,594,3846,0;5,841,272,5,434,152,0;5,841,273,5,434,153,0;5,841,287,5,433,167,0;5,843,286,5,433,166,0;5,996,3841,5,33,240,0;5,997,3841,5,34,240,0;5,998,3841,5,35,240,0;5,999,3841,5,36,240,0;5,1000,3841,5,37,240,0;5,1006,1110,5,1012,1071,0;5,1006,1127,5,1010,1144,0;5,1010,1144,5,1006,1127,0;5,1012,1071,5,1006,1110,0;5,1042,1047,5,1238,1151,0;5,1079,882,5,1080,977,0;5,1079,883,5,1080,977,0;5,1079,884,5,1080,977,0;5,1079,885,5,1080,977,0;5,1080,974,5,1081,883,0;5,1080,975,5,1081,883,0;5,1081,974,5,1081,883,0;5,1081,975,5,1081,883,0;5,1125,1076,5,520,920,0;5,1125,1215,1,4194,3261,0;5,1125,1215,1,4194,3261,0;5,1126,1076,5,521,920,0;5,1126,1215,1,4194,3261,0;5,1126,1215,1,4194,3261,0;5,1127,1215,1,4194,3261,0;5,1127,1215,1,4194,3261,0;5,1128,1215,1,4194,3261,0;5,1128,1215,1,4194,3261,0;5,1129,1215,1,4194,3261,0;5,1129,1215,1,4195,3261,0;5,1130,1215,1,4194,3261,0;5,1130,1215,1,4195,3261,0;5,1131,1215,1,4194,3261,0;5,1131,1215,1,4195,3261,0;5,1162,1120,5,1147,1089,0;5,1180,885,5,1187,1127,0;5,1183,1180,5,1183,1182,0;5,1183,1182,5,1183,1180,0;5,1183,1187,5,1183,1189,0;5,1183,1189,5,1183,1187,0;5,1184,1177,5,1186,1177,0;5,1186,1177,5,1184,1177,0;5,1186,1183,5,1184,1183,0;5,1187,1126,5,1180,884,0;5,1187,1173,5,1187,1175,0;5,1187,1175,5,1187,1173,0;5,1190,1185,5,1190,1183,0;5,1192,1186,5,1190,1186,0;5,1197,1189,5,1195,1189,0;5,1198,1187,5,1196,1187,0;5,1200,1187,5,1200,1190,0;5,1201,1189,5,1201,1186,0;5,1202,1183,5,1202,1185,0;5,1203,1190,5,1201,1190,0;5,1206,1181,5,1204,1181,0;5,1238,1151,5,1140,951,0;5,4194,3260,5,1128,1213,0;5,4195,3260,5,1129,1213,0;5,7149,756,0,511,585,0;5,7149,756,5,511,585,0;5,7150,756,5,511,585,0;5,7151,756,5,511,585,0;5,7152,756,5,511,585,0;5,7153,756,5,511,585,0"
LEARN_TELEPORTS = True
LINK_VAR = "SL_Mining_Links"
LINKS = []                  # dicts: sm sx sy dm dx dy (dir vx vy) kind fails
LINK_SRC = set()            # (map, x, y) tiles that teleport you: never stand there
POS = {"last": None, "prev": None, "moved_t": 0.0, "jumps": 0}


def _add_link(sm, sx, sy, dm, dx, dy, kind, vx=0, vy=0):
    for L in LINKS:
        if L["sm"] == sm and L["dm"] == dm and abs(L["sx"] - sx) <= 2 and abs(L["sy"] - sy) <= 2 \
                and abs(L["dx"] - dx) <= 4 and abs(L["dy"] - dy) <= 4:
            if kind == "learned":
                L["fails"] = 0
                L["seen"] = True
            return None
    L = {"sm": sm, "sx": sx, "sy": sy, "dm": dm, "dx": dx, "dy": dy, "kind": kind,
         "vx": vx, "vy": vy, "fails": 0, "seen": kind == "learned"}
    LINKS.append(L)
    LINK_SRC.add((sm, sx, sy))
    return L


def load_links():
    LINKS[:] = []
    LINK_SRC.clear()
    data = TELEPORTER_DATA if not TELEPORTER_DATA.startswith("@@") else ""
    for e in data.split(";"):
        p = e.split(",")
        if len(p) != 7:
            continue
        try:
            sm, sx, sy, dm, dx, dy, back = [int(v) for v in p]
        except ValueError:
            continue
        _add_link(sm, sx, sy, dm, dx, dy, "data")
        if back:
            _add_link(dm, dx, dy, sm, sx, sy, "data")
    try:
        raw = API.GetPersistentVar(LINK_VAR, "", API.PersistentVar.Char) or ""
    except Exception:
        raw = ""
    for e in str(raw).split(";"):
        p = e.split(",")
        if len(p) == 8:
            try:
                v = [int(t) for t in p]
                _add_link(v[0], v[1], v[2], v[3], v[4], v[5], "learned", v[6], v[7])
            except ValueError:
                pass


def save_links():
    try:
        API.SavePersistentVar(LINK_VAR, ";".join(
            "{},{},{},{},{},{},{},{}".format(L["sm"], L["sx"], L["sy"], L["dm"], L["dx"], L["dy"],
                                             L["vx"], L["vy"])
            for L in LINKS if L["kind"] == "learned")[-4000:], API.PersistentVar.Char)
    except Exception:
        pass


def learn_link(src, dst, prev):
    vx = vy = 0
    if prev is not None and prev[0] == src[0]:
        vx = _sgn(int(src[1]) - int(prev[1]))
        vy = _sgn(int(src[2]) - int(prev[2]))
    L = _add_link(src[0], src[1], src[2], dst[0], dst[1], dst[2], "learned", vx, vy)
    if L is not None:
        save_links()
        note("Learned a teleporter: {},{} -> {},{}.".format(src[1], src[2], dst[1], dst[2]), 68)


def on_map_change():
    TILE_CACHE.clear()
    STATIC_CHUNKS.clear()
    TRIED.clear()
    VISITED.clear()
    DEPLETED.clear()
    CRUMBS[:] = []
    SURVEY["map"] = cur_map()
    WORTH.clear()
    load_grove_log()
    load_bank_memory()
    load_crumbs()


def _cheb(ax, ay, bx, by):
    return max(abs(ax - bx), abs(ay - by))


ROUTE_WALK = 400            # longest single walking leg a teleporter route may use


def plan_links(tm, tx, ty):
    """Teleporters to take to reach (tm, tx, ty): (cost, [links]). Direct walk = []."""
    cm, px, py = cur_map(), API.Player.X, API.Player.Y
    best = (_cheb(px, py, tx, ty) if cm == tm else 10 ** 9, [])
    ok = [L for L in LINKS if L["fails"] < 2]
    first = [L for L in ok if L["sm"] == cm and _cheb(px, py, L["sx"], L["sy"]) <= ROUTE_WALK]
    last = [L for L in ok if L["dm"] == tm and _cheb(L["dx"], L["dy"], tx, ty) <= ROUTE_WALK]
    for L in first:
        if L["dm"] == tm:
            c = _cheb(px, py, L["sx"], L["sy"]) + 3 + _cheb(L["dx"], L["dy"], tx, ty)
            if c < best[0]:
                best = (c, [L])
    first.sort(key=lambda L: _cheb(px, py, L["sx"], L["sy"]))
    last.sort(key=lambda L: _cheb(L["dx"], L["dy"], tx, ty))
    for A in first[:30]:
        a = _cheb(px, py, A["sx"], A["sy"]) + 3
        for B in last[:30]:
            if A is B or B["sm"] != A["dm"]:
                continue
            leg = _cheb(A["dx"], A["dy"], B["sx"], B["sy"])
            if leg > ROUTE_WALK:
                continue
            c = a + leg + 3 + _cheb(B["dx"], B["dy"], tx, ty)
            if c < best[0]:
                best = (c, [A, B])
    if best[1] and cm == tm and best[0] > _cheb(px, py, tx, ty) * 0.7 - 5:
        return (_cheb(px, py, tx, ty), [])      # walking is about as good
    return best


def _jumped_to(L, j0):
    return POS["jumps"] != j0 and cur_map() == L["dm"] and \
        _cheb(API.Player.X, API.Player.Y, L["dx"], L["dy"]) <= 4


def _step(vx, vy):
    for name, v in DIRS8.items():
        if v == (vx, vy):
            API.Walk(name)
            API.Pause(0.5)
            track_position()
            return


def take_link(L):
    """Walk onto a teleporter and make sure it took us across."""
    j0 = POS["jumps"]
    dbg("Taking teleporter {},{} -> {},{}".format(L["sx"], L["sy"], L["dx"], L["dy"]))
    if (cur_map(), API.Player.X, API.Player.Y) != (L["sm"], L["sx"], L["sy"]):
        _go_to(L["sx"], L["sy"], 0, "To teleporter")
    if _jumped_to(L, j0):
        return True
    if POS["jumps"] == j0 and _cheb(API.Player.X, API.Player.Y, L["sx"], L["sy"]) <= 1:
        if L["vx"] or L["vy"]:
            # learned while walking: the real trigger may be a step or two further on
            for _ in range(3):
                _step(L["vx"], L["vy"])
                if POS["jumps"] != j0:
                    break
        else:
            # standing on it doesn't fire it (you may have just arrived on it): step off and on
            for name, (vx, vy) in DIRS8.items():
                nx, ny = API.Player.X + vx, API.Player.Y + vy
                if standable(nx, ny):
                    API.Walk(name)
                    API.Pause(0.5)
                    _step(-vx, -vy)
                    break
        t = 0.0
        while POS["jumps"] == j0 and t < 2.0:
            API.Pause(0.25)
            track_position()
            t += 0.25
    if _jumped_to(L, j0):
        L["fails"] = 0
        return True
    L["fails"] += 1
    dbg("Teleporter at {},{} didn't work ({} fails)".format(L["sx"], L["sy"], L["fails"]))
    return False


def route_dist(tm, tx, ty):
    return plan_links(tm, tx, ty)[0]


def go_to(tx, ty, near=1, label="", tmap=None):
    """Walk to (tx, ty) on map tmap, using teleporters when that is much shorter."""
    tm = cur_map() if tmap is None else tmap
    for _ in range(4):
        checkpoint()
        cost, route = plan_links(tm, tx, ty)
        if route:
            note("Route via {} teleporter{} ({} tiles).".format(
                len(route), "s" if len(route) > 1 else "", cost), 88)
        took = True
        for L in route:
            if not take_link(L):
                took = False
                break
        if not took:
            continue                # re-plan from wherever we ended up
        if cur_map() != tm:
            continue
        j0 = POS["jumps"]
        if _go_to(tx, ty, near, label):
            return True
        if POS["jumps"] == j0:
            return False
        dbg("Teleported somewhere on the way; re-planning")
    return False


DIRS8 = {"north": (0, -1), "northeast": (1, -1), "east": (1, 0), "southeast": (1, 1),
         "south": (0, 1), "southwest": (-1, 1), "west": (-1, 0), "northwest": (-1, -1)}


def explore_step(start, grid):
    """Nothing worth walking to: head toward the most trees, else away from chopped groves."""
    dist = 6
    px, py = API.Player.X, API.Player.Y
    ranked = []
    for name, (vx, vy) in DIRS8.items():
        tx, ty = px + vx * dist, py + vy * dist
        if max(abs(tx - start[0]), abs(ty - start[1])) > WANDER_RANGE:
            continue
        cx, cy = px + vx * (dist + 3), py + vy * (dist + 3)
        trees = grid.count(cx - 5, cy - 5, cx + 5, cy + 5)
        ranked.append((trees, name, tx, ty))
    ranked.sort(reverse=True)
    for trees, name, tx, ty in ranked:
        if trees <= 0:
            break
        ox, oy = API.Player.X, API.Player.Y
        dbg("Exploring {} ({} trees that way)".format(name, trees))
        API.HeadMsg("Exploring " + name, API.Player, 88)
        if not walk_to(tx, ty):
            for _ in range(dist):
                API.Walk(name)
                API.Pause(0.45)
        if (API.Player.X, API.Player.Y) != (ox, oy):
            HEADING[0] = float(API.Player.X - ox)
            HEADING[1] = float(API.Player.Y - oy)
            return True
        dbg("Blocked going " + name)
    return False


def move_on(start):
    """Get to a new spot. Returns False if there is nowhere left to go."""
    spots, grid = find_spots(start)
    dbg("{} candidate spots in range".format(len(spots)))
    for x, y, fresh, ahead in spots[:8]:
        if API.StopRequested:
            return False
        d = compass(x - API.Player.X, y - API.Player.Y)
        API.HeadMsg("Heading {} ({} tree{} here, {} beyond)".format(
            d, fresh, "" if fresh == 1 else "s", ahead), API.Player, 88)
        j0 = POS["jumps"]
        ok = walk_to(x, y, 0) or max(abs(API.Player.X - x), abs(API.Player.Y - y)) <= 1
        VISITED.add((x, y))         # never pick this spot again, reached or not
        if POS["jumps"] != j0:
            return True             # a teleporter moved us: run_session walks back
        if ok and trees_in_reach():
            dbg("Arrived near {},{}".format(x, y))
            return True
        dbg("Could not reach {},{}".format(x, y))
    # nothing good nearby: walk to the best surveyed grove before wandering blind
    if survey_on() and CTRL.get("relocs", 0) < AUTO_RELOCATE:
        CTRL["relocs"] = CTRL.get("relocs", 0) + 1
        set_state("Surveying")
        if go_to_grove(auto=True):
            return True
    return explore_step(start, grid)


def reset_memory():
    DEPLETED.clear()
    TRIED.clear()
    VISITED.clear()
    HEADING[0] = HEADING[1] = 0.0
    SURVEY["went"].clear()
    try:
        API.SavePersistentVar(MEM_VAR, "", API.PersistentVar.Char)
    except Exception:
        pass


# ---- satchel, boards and storage ----------------------------------------------
STORE_VAR = "SL_Lumber_Storage"
SATCHEL_VAR = "SL_Lumber_Satchel"
STORAGE = {}                # map, x, y, z, serial
SATCHEL = [0]


def load_targets():
    try:
        raw = API.GetPersistentVar(STORE_VAR, "", API.PersistentVar.Char) or ""
        p = [int(v) for v in str(raw).split(",")] if raw else []
        if len(p) == 5:
            STORAGE.update(zip(["map", "x", "y", "z", "serial"], p))
    except Exception:
        pass
    try:
        SATCHEL[0] = int(API.GetPersistentVar(SATCHEL_VAR, "0", API.PersistentVar.Char) or 0)
    except Exception:
        SATCHEL[0] = 0


def set_storage():
    sysmsg("[Lumber] Target the chest or crate to unload into.", 68)
    obj = API.RequestAnyTarget(20)
    if obj is None:
        sysmsg("[Lumber] No storage set.", 33)
        return
    serial = 0
    try:
        serial = int(obj.Serial or 0)
    except Exception:
        serial = 0
    if not serial:
        sysmsg("[Lumber] That isn't a container. Target a chest or crate.", 33)
        return
    STORAGE.clear()
    STORAGE.update({"map": API.GetMap(), "x": obj.X, "y": obj.Y, "z": obj.Z, "serial": serial})
    API.SavePersistentVar(STORE_VAR, ",".join(str(STORAGE[k]) for k in
                          ["map", "x", "y", "z", "serial"]), API.PersistentVar.Char)
    CTRL["store_full"] = False
    sysmsg("[Lumber] Storage set at {},{}.".format(obj.X, obj.Y), 68)
    try:
        API.MarkTile(obj.X, obj.Y, HUE_DEST)
        API.Pause(1.5)
        API.RemoveMarkedTile(obj.X, obj.Y)
    except Exception:
        pass


def set_satchel():
    CTRL["stash_refused"] = False
    sysmsg("[Lumber] Target your lumber satchel.", 68)
    serial = API.RequestTarget(20)
    if not serial:
        sysmsg("[Lumber] No satchel set.", 33)
        return
    SATCHEL[0] = int(serial)
    API.SavePersistentVar(SATCHEL_VAR, str(SATCHEL[0]), API.PersistentVar.Char)
    sysmsg("[Lumber] Satchel set.", 68)


def is_lumber_satchel(it):
    n = item_name(it)
    if "lumber satchel" in n:
        return True
    return it.Graphic == SATCHEL_GRAPHIC and "lumber" in tooltip(it)


def auto_satchel(open_it=True):
    """Find the Foresters' Lumber Satchel and open it: chopped logs go straight into
    it on this shard, and the client only sees what is inside an opened bag."""
    if not (SATCHEL[0] and API.FindItem(SATCHEL[0]) is not None):
        for it in pack_all():
            if is_lumber_satchel(it):
                SATCHEL[0] = int(it.Serial)
                API.SavePersistentVar(SATCHEL_VAR, str(SATCHEL[0]), API.PersistentVar.Char)
                note("Found your lumber satchel.", 68)
                break
    if open_it and SATCHEL[0] and API.FindItem(SATCHEL[0]) is not None:
        API.UseObject(SATCHEL[0])
        API.Pause(0.6)


def count(graphics, outside_satchel=False):
    n = 0
    for it in pack_items(graphics):
        if outside_satchel and SATCHEL[0] and it.Container == SATCHEL[0]:
            continue
        n += it.Amount or 1
    return n


def stash(graphics):
    """Move matching items from the pack into the satchel. Returns how many moved."""
    if not SATCHEL[0]:
        return 0
    if API.FindItem(SATCHEL[0]) is None:
        sysmsg("[Lumber] Can't see the satchel. Is it in your pack?", 33)
        return 0
    moved = 0
    for it in pack_items(graphics):
        if it.Container == SATCHEL[0] or it.Serial == SATCHEL[0]:
            continue
        API.MoveItem(it.Serial, SATCHEL[0])
        API.Pause(0.8)
        after = API.FindItem(it.Serial)
        if after is not None and after.Container != SATCHEL[0]:
            note("The lumber satchel is full. Unload some wood.", 33)
            CTRL["stash_refused"] = True
            break
        moved += 1
    return moved


def satchel_mills():
    """Seasoned (tier 2) and Arborist's (tier 3) satchels turn logs into boards
    from their right-click menu."""
    it = API.FindItem(SATCHEL[0]) if SATCHEL[0] else None
    if it is None:
        return False
    t = item_name(it) + " " + tooltip(it)
    return "seasoned" in t or "arborist" in t


def make_boards(manual=False):
    """Turn every log you carry into boards (half the weight). Returns boards made
    (roughly), or 0."""
    if not manual and time.time() - CTRL.get("boards_t", 0) < 120:
        return 0                    # don't retry logs we can't work every swing
    CTRL["boards_t"] = time.time()
    if not count(LOG_GRAPHICS):
        if manual:
            note("No logs to turn into boards.", 43)
        return 0
    axe = find_tool()
    if axe is None:
        note("Need an axe in hand to make boards.", 33)
        return 0
    prev = CTRL["state"]
    set_state("Making boards")
    before = count(BOARD_GRAPHICS)
    try:
        if satchel_mills():
            stash(LOG_GRAPHICS)
            API.ClearJournal()
            ok = False
            try:
                ok = API.ContextMenu(SATCHEL[0], "Chops Logs", 4)
            except Exception as e:
                dbg("Satchel menu error: {}".format(e))
            if ok:
                t = 0.0
                msgs = ["You process", "no logs in the satchel", "must have a hatchet", "must be in your backpack"]
                while t < 4.0 and not API.InJournalAny(msgs):
                    API.Pause(0.2)
                    t += 0.2
                log("satchel boards: " + (last_journal_line() or "no reply"))
        # by hand: the axe on each stack of logs (log -> board, 1 for 1)
        skip = set()
        for _ in range(40):
            checkpoint()
            logs = [it for it in pack_items(LOG_GRAPHICS) if it.Serial not in skip]
            if not logs:
                break
            lg = logs[0]
            amt = lg.Amount or 1
            if API.HasTarget():
                API.CancelTarget()
            API.ClearJournal()
            API.UseObject(axe.Serial)
            if not API.WaitForTarget("any", 3):
                skip.add(lg.Serial)
                continue
            API.Target(lg.Serial)
            t = 0.0
            while t < 2.0:
                API.Pause(0.25)
                t += 0.25
                after = API.FindItem(lg.Serial)
                if after is None or (after.Amount or 1) != amt or API.InJournal("strange and unusual"):
                    break
            after = API.FindItem(lg.Serial)
            if after is not None and (after.Amount or 1) == amt:
                skip.add(lg.Serial)
                if API.InJournal("strange and unusual"):
                    dbg("Not enough skill to make boards from one kind of log")
        made = count(BOARD_GRAPHICS) - before
        if SATCHEL[0] and not CTRL.get("stash_refused"):
            stash(BOARD_GRAPHICS)
        if made > 0:
            note("Made {} boards.".format(made), 68)
        elif manual:
            note("Couldn't make boards from these logs.", 43)
        return made
    finally:
        set_state(prev)


def storage_ok():
    if not STORAGE:
        return False
    return STORAGE.get("map") == cur_map() or storage_dist() < 10 ** 8


STORE_ROUTE = {"key": None, "d": 9999, "via": 0}


def storage_dist():
    """Walking distance to the storage, through teleporters if that is shorter (cached)."""
    if not STORAGE:
        return 9999
    key = (cur_map(), API.Player.X // 4, API.Player.Y // 4, STORAGE["map"], STORAGE["x"], STORAGE["y"])
    if STORE_ROUTE["key"] != key:
        cost, route = plan_links(STORAGE["map"], STORAGE["x"], STORAGE["y"])
        STORE_ROUTE.update({"key": key, "d": cost, "via": len(route)})
    return STORE_ROUTE["d"]


def wood_graphics():
    return LOG_GRAPHICS + BOARD_GRAPHICS + STASH_EXTRA + (BONUS_GRAPHICS if STORE_BONUS else [])


def store_all():
    """Move the wood (and extras) into the storage container. Returns stacks moved."""
    serial = STORAGE["serial"]
    API.UseObject(serial)
    API.Pause(0.8)
    moved = 0
    for it in pack_items(wood_graphics()):
        checkpoint()
        if SATCHEL[0] and it.Container == SATCHEL[0] and not EMPTY_SATCHEL:
            continue
        API.MoveItem(it.Serial, serial)
        API.Pause(0.8)
        after = API.FindItem(it.Serial)
        if after is not None and after.Container != serial:
            # still on you: the container is full or too far
            note("The storage container won't take more. Empty it.", 33)
            CTRL["store_full"] = True
            break
        moved += 1
    return moved


def unload_check(force=False):
    """ok = keep chopping, trip = go to storage, stop = can't unload."""
    if not force and opt("power"):
        return "ok"                 # power through: only unload when the game says full
    if not force and weight_pct() < CTRL.get("trip_at", UNLOAD_AT):
        return "ok"
    loose = LOG_GRAPHICS + BOARD_GRAPHICS + STASH_EXTRA
    if SATCHEL[0] and not CTRL["stash_refused"] and count(loose, outside_satchel=True):
        stash(loose)
        if weight_pct() < CTRL.get("trip_at", UNLOAD_AT):
            return "ok"
    if opt("boards") and count(LOG_GRAPHICS):
        make_boards()
        if weight_pct() < CTRL.get("trip_at", UNLOAD_AT):
            return "ok"
    if opt("unload") and storage_ok() and storage_dist() <= TRIP_MAX_DIST and not CTRL.get("store_full"):
        return "trip"
    if weight_pct() >= WEIGHT_STOP or force:
        if opt("unload") and CTRL.get("store_full"):
            sysmsg("[Lumber] Heavy, and the storage is full.", 33)
        elif opt("unload") and not storage_ok():
            sysmsg("[Lumber] Heavy, and no storage set on this map. Press Set Storage.", 33)
        elif opt("unload"):
            sysmsg("[Lumber] Storage is too far away ({} tiles).".format(storage_dist()), 33)
        else:
            sysmsg("[Lumber] Weight limit reached. Go unload.", 33)
        return "stop"
    return "ok"


def storage_trip():
    """Walk to the storage, unload the wood, walk back. True if we made it back."""
    if CTRL["busy"]:
        return False
    CTRL["busy"] = True
    try:
        if not storage_ok():
            sysmsg("[Lumber] No storage set on this map.", 33)
            return False
        home = (API.Player.X, API.Player.Y)
        home_map = cur_map()
        heading = list(HEADING)
        set_state("Walking to storage")
        API.HeadMsg("Off to unload ({} tiles)".format(storage_dist()), API.Player, 88)
        if (cur_map() != STORAGE["map"] or storage_dist() > STORAGE_STAND) and \
                not go_to(STORAGE["x"], STORAGE["y"], STORAGE_STAND, "To storage", STORAGE["map"]):
            sysmsg("[Lumber] Could not reach the storage ({} tiles away). Walk there once "
                   "with the window open so it learns the way.".format(storage_dist()), 33)
            HEADING[:] = heading
            return False
        set_state("Unloading")
        auto_satchel()              # see the logs that went into the satchel
        n = store_all()
        log("stored {} stacks".format(n))
        if cur_map() != home_map or max(abs(API.Player.X - home[0]), abs(API.Player.Y - home[1])) > 0:
            set_state("Walking back")
            if not go_to(home[0], home[1], 1, "Back to the trees", home_map):
                sysmsg("[Lumber] Could not get back to the chopping spot.", 33)
                HEADING[:] = heading
                return False
        HEADING[:] = heading
        sysmsg("[Lumber] Unloaded. Weight {}%.".format(int(weight_pct())), 68)
        return True
    finally:
        CTRL["busy"] = False
        CTRL["legit_jumps"] = POS["jumps"]


# ---- control window ------------------------------------------------------------
UI = {}
OPTS = {"t": 0.0}


def opt(name):
    """Window checkbox state. Reading a checkbox waits a game frame, so the values
    are read once a second and cached (scoring asks per tile)."""
    now = time.time()
    if now - OPTS["t"] > 1.0:
        OPTS["t"] = now
        for key, default in (("wander", WANDER), ("seek", SEEK_GROVES), ("boards", MAKE_BOARDS),
                             ("unload", AUTO_UNLOAD), ("marks", SHOW_MARKS),
                             ("grovemap", SHOW_GROVE_MAP), ("power", POWER_THROUGH)):
            cb = UI.get("cb_" + key)
            if cb is None:
                OPTS[key] = default
                continue
            try:
                OPTS[key] = bool(cb.IsChecked)
            except Exception:
                OPTS[key] = default
    return OPTS.get(name, True)


def want(action):
    CTRL["want"].append(action)


def handle_wants():
    """Button actions that need targeting or walking run here, at a safe point."""
    while CTRL["want"] and not CTRL["busy"]:
        a = CTRL["want"].pop(0)
        if a == "storage":
            set_storage()
        elif a == "satchel":
            set_satchel()
        elif a == "unload":
            CTRL["store_full"] = False
            storage_trip() if storage_ok() else sysmsg("[Lumber] Set a storage container first.", 33)
        elif a == "tools":
            ensure_tools(force=True)
        elif a == "boards":
            CTRL["busy"] = True
            try:
                auto_satchel()
                make_boards(manual=True)
            finally:
                CTRL["busy"] = False
        elif a in ("survey", "grove"):
            CTRL["busy"] = True
            try:
                if a == "survey":
                    read_logbook()
                    survey()
                else:
                    go_to_grove()
            finally:
                CTRL["busy"] = False
        elif a == "stash":
            CTRL["stash_refused"] = False
            n = stash(LOG_GRAPHICS + BOARD_GRAPHICS + STASH_EXTRA)
            sysmsg("[Lumber] Stashed {} stacks.".format(n), 68)
        gui_tick(force=True)


def on_start():
    if not CTRL["running"]:
        CTRL["running"] = True
        CTRL["paused"] = False
    else:
        CTRL["paused"] = not CTRL["paused"]
    gui_tick(force=True)


def on_stop_btn():
    CTRL["running"] = False
    CTRL["paused"] = False


def on_close():
    CTRL["quit"] = True
    CTRL["running"] = False


def build_gui():
    W, H = 250, 470
    g = API.CreateGump(True, True, False)
    g.SetRect(120, 120, W, H)
    g.Add(API.CreateGumpColorBox(0.85, "#161616").SetRect(0, 0, W, H))
    g.Add(API.CreateGumpTTFLabel("SL Lumberjack", 16, "#8FD18A").SetPos(10, 6))

    UI["state"] = API.CreateGumpTTFLabel("Idle", 13, "#FFFFFF")
    UI["load"] = API.CreateGumpTTFLabel("", 12, "#BBBBBB")
    UI["store"] = API.CreateGumpTTFLabel("", 12, "#BBBBBB")
    UI["sat"] = API.CreateGumpTTFLabel("", 12, "#BBBBBB")
    UI["tools"] = API.CreateGumpTTFLabel("", 12, "#BBBBBB")
    UI["survey"] = API.CreateGumpTTFLabel("", 12, "#9FD3FF")
    UI["note"] = API.CreateGumpTTFLabel("", 12, "#E8C15A")
    y = 30
    for key in ("state", "load", "store", "sat", "tools", "survey", "note"):
        g.Add(UI[key].SetPos(10, y))
        y += 20 if key == "state" else 18

    def button(text, x, y, fn, w=72):
        b = API.CreateSimpleButton(text, w, 22)
        g.Add(b.SetPos(x, y))
        API.AddControlOnClick(b, fn)
        return b

    y = 166
    UI["start"] = button("Start", 10, y, on_start)
    button("Stop", 88, y, on_stop_btn)
    button("Unload now", 166, y, lambda: want("unload"))
    button("Set Storage", 10, y + 28, lambda: want("storage"), 110)
    button("Set Satchel", 128, y + 28, lambda: want("satchel"), 110)
    button("Stash now", 10, y + 56, lambda: want("stash"))
    button("Make boards", 88, y + 56, lambda: want("boards"))
    button("Make axes", 166, y + 56, lambda: want("tools"))
    button("Survey", 10, y + 84, lambda: want("survey"), 110)
    button("Go to grove", 128, y + 84, lambda: want("grove"), 110)

    y += 116
    for key, label, val in [("wander", "Wander to more trees", WANDER),
                            ("seek", "Seek colored groves", SEEK_GROVES),
                            ("boards", "Make boards when heavy", MAKE_BOARDS),
                            ("unload", "Go unload when heavy", AUTO_UNLOAD),
                            ("marks", "Highlight trees", SHOW_MARKS),
                            ("grovemap", "Show grove map (tint trees)", SHOW_GROVE_MAP),
                            ("power", "Power through when heavy", POWER_THROUGH)]:
        cb = API.CreateGumpCheckbox(label, 1153, val)
        g.Add(cb.SetPos(10, y))
        UI["cb_" + key] = cb
        y += 20

    API.AddControlOnDisposed(g, on_close)
    API.AddGump(g)
    UI["gump"] = g
    gui_tick(force=True)


def _set(key, text):
    try:
        UI[key].SetText(text)
    except Exception:
        pass


def gui_tick(force=False):
    if not UI:
        return
    now = time.time()
    if not force and now - CTRL["last_ui"] < 1.0:
        return
    CTRL["last_ui"] = now
    st = CTRL["state"]
    if CTRL["running"] and CTRL["paused"]:
        st = "Paused (" + st + ")"
    elif not CTRL["running"]:
        st = "Idle"
    _set("state", st)
    try:
        UI["start"].SetText("Pause" if CTRL["running"] and not CTRL["paused"]
                            else ("Resume" if CTRL["running"] else "Start"))
    except Exception:
        pass
    _set("note", NOTES[-1] if NOTES else "")
    # the rest reads the pack and the world (a game frame per read): every 5 s
    if now - CTRL.get("last_heavy", 0) < 5.0:
        return
    CTRL["last_heavy"] = now
    _set("load", "Weight {}%   Logs {}   Boards {}".format(
        int(weight_pct()), count(LOG_GRAPHICS), count(BOARD_GRAPHICS)))
    if storage_ok():
        d = storage_dist()
        _set("store", "Storage: {},{}  ({} tiles{}){}".format(
            STORAGE["x"], STORAGE["y"], d if d < 10 ** 8 else "?",
            ", via teleporter" if STORE_ROUTE["via"] else "",
            "  FULL" if CTRL.get("store_full") else ""))
    elif STORAGE:
        _set("store", "Storage: set on another map")
    else:
        _set("store", "Storage: not set")
    if SATCHEL[0] and API.FindItem(SATCHEL[0]) is not None:
        inside = (count(LOG_GRAPHICS) - count(LOG_GRAPHICS, outside_satchel=True),
                  count(BOARD_GRAPHICS) - count(BOARD_GRAPHICS, outside_satchel=True))
        _set("sat", "Satchel: {} logs, {} boards inside".format(inside[0], inside[1]))
    else:
        _set("sat", "Satchel: not set" if not SATCHEL[0] else "Satchel: not found")
    _set("tools", tools_summary())
    _set("survey", survey_line())


# ---- the lumberjacking session ----------------------------------------------------
def run_session():
    tool = find_tool()
    if tool is None and ensure_tools(force=True):
        tool = find_tool()
    if tool is None:
        sysmsg("[Lumber] No hatchet or axe found. Run with DIAGNOSE = True.", 33)
        return
    START[:] = [API.Player.X, API.Player.Y, API.Player.Z, cur_map()]
    start = START
    load_bank_memory()
    auto_satchel()
    if opt("seek"):
        set_state("Reading logbook")
        read_logbook(quiet=True)
        tot = SURVEY["book_total"]
        if SURVEY["book"] is not None:
            sysmsg("[Survey] Logbook: {} groves here; grove map matches {}/{}.".format(
                tot, SURVEY["book_hits"], tot), 68 if SURVEY["map_ok"] else 33)
    sysmsg("[Lumber] Using {}. Lumberjacking {:.1f}{}".format(
        item_name(tool) or "axe", lumber_skill(),
        ", wandering up to {} tiles.".format(WANDER_RANGE) if opt("wander") else "."), 68)

    moves = 0
    CTRL["relocs"] = 0
    while True:
        checkpoint()
        if cur_map(fresh=True) != START[3] or \
                _cheb(API.Player.X, API.Player.Y, START[0], START[1]) > WANDER_RANGE + 12:
            # a teleporter (or something) took us away from the area we were working
            note("Ended up away from the trees; heading back.", 43)
            if not go_to(START[0], START[1], 2, "Back to the trees", START[3]):
                note("Couldn't get back. Chopping here instead.", 43)
                START[:] = [API.Player.X, API.Player.Y, API.Player.Z, cur_map()]
            moves = 0
            continue
        r = chop_here()
        if r == "moved":
            moves = 0
            continue
        if r == "trip":
            if not storage_trip():
                return
            w = weight_pct()
            if w >= WEIGHT_STOP - 5 and not opt("power"):
                note("Still {}% heavy after unloading. Drop something.".format(int(w)), 33)
                return
            if w > UNLOAD_AT - 20:
                CTRL["trip_at"] = min(WEIGHT_STOP - 2, max(UNLOAD_AT, w + 20))
                if not CTRL.get("heavy_noted"):
                    CTRL["heavy_noted"] = True
                    note("Still {}% after unloading; next trip at {}%.".format(
                        int(w), int(CTRL["trip_at"])), 43)
            else:
                CTRL["trip_at"] = UNLOAD_AT
            continue                # back at the spot: finish its trees
        if r == "stop":
            return

        if opt("wander") and (MAX_MOVES == 0 or moves < MAX_MOVES):
            set_state("Looking for trees")
            if move_on(start):
                moves += 1
                continue
            dbg("Nowhere left to go within " + str(WANDER_RANGE) + " tiles")

        if not LOOP_FOREVER:
            sysmsg("[Lumber] Nothing left in range. Move to a new forest.", 43)
            API.HeadMsg("ALL CHOPPED - MOVE", API.Player, 33)
            return

        if opt("wander") and RETURN_TO_START:
            go_to(start[0], start[1], 1, "Back to start", start[3])
        set_state("Waiting for regrowth")
        waited = 0
        while waited < RESPAWN_WAIT:
            checkpoint()
            API.Pause(1)
            waited += 1
        reset_memory()
        moves = 0


def run_logged():
    """run_session, but a script error is written to the log instead of vanishing."""
    try:
        run_session()
    except Halt:
        log("stopped")
        raise
    except Exception as e:
        if "OperationCanceled" in type(e).__name__ or "OperationCanceled" in str(e):
            log("stopped (the client cancelled the script)")
            return
        try:
            import traceback
            log("SCRIPT ERROR:\n" + traceback.format_exc())
        except Exception:
            log("SCRIPT ERROR: {!r}".format(e))
        sysmsg("[Lumber] Script error: {} (details in SL_Lumberjack.log)".format(e), 33)


def main():
    log("===== SL_Lumberjack started =====")
    if DIAGNOSE:
        diagnose()
        return

    clear_leftover_marks()
    load_targets()
    load_crumbs()
    load_links()
    SURVEY["map"] = cur_map()
    load_grove_log()            # groves logged before + groves you've chopped
    track_position()

    if not GUI:
        CTRL["running"] = True
        CTRL["session"] = True
        try:
            run_logged()
        except Halt:
            pass
        API.Pause(END_LINGER)
        return

    build_gui()
    sysmsg("[Lumber] Window open. Set your storage (optional), then press Start.", 68)
    while not CTRL["quit"] and not API.StopRequested:
        API.ProcessCallbacks()
        track_position()        # learn the way while you walk around
        grove_map_tick()
        try:
            handle_wants()      # Set Storage / Unload now while idle
        except Halt:
            pass
        gui_tick()
        if CTRL["running"]:
            CTRL["session"] = True
            try:
                run_logged()
            except Halt:
                pass
            CTRL["session"] = False
            CTRL["running"] = False
            CTRL["paused"] = False
            set_state("Idle")
            try:
                API.TrackingArrow(-1, -1)
            except Exception:
                pass
        API.Pause(0.2)
    save_crumbs()
    try:
        if not UI["gump"].IsDisposed:
            UI["gump"].Dispose()
    except Exception:
        pass


main()
clear_marks()               # every normal exit
