;;-*-Lisp-*-
(in-package goal)

;; name: trail.gc
;; name in dgo: trail
;; dgos: CWI, WASALL

(declare-type trail-vis-work structure)

;; DECOMP BEGINS

;; WARN: Return type mismatch symbol vs none.
(defmethod debug-draw ((this trail-conn) (arg0 trail-graph) (arg1 int))
  (let ((a2-3 (-> arg0 node (-> this head-id)))
        (v1-2 (-> arg0 node (-> this tail-id)))
        (s4-0 (new 'stack-no-clear 'vector))
        (s3-0 (new 'stack-no-clear 'vector))
        (s5-0 (new 'stack-no-clear 'vector))
        )
    (set-vector!
      s4-0
      (* 4096.0 (the float (-> a2-3 x)))
      (* 4096.0 (the float (-> a2-3 y)))
      (* 4096.0 (the float (-> a2-3 z)))
      1.0
      )
    (set-vector!
      s3-0
      (* 4096.0 (the float (-> v1-2 x)))
      (* 4096.0 (the float (-> v1-2 y)))
      (* 4096.0 (the float (-> v1-2 z)))
      1.0
      )
    (vector-lerp! s5-0 s4-0 s3-0 0.5)
    (let* ((s2-0 (math-camera-pos))
           (f0-14 (vector-vector-distance-squared s4-0 s2-0))
           (f1-12 819200.0)
           )
      (when (or (< f0-14 (* f1-12 f1-12))
                (let ((f0-15 (vector-vector-distance-squared s3-0 s2-0))
                      (f1-15 819200.0)
                      )
                  (< f0-15 (* f1-15 f1-15))
                  )
                (let ((f0-16 (vector-vector-distance-squared s5-0 s2-0))
                      (f1-18 819200.0)
                      )
                  (< f0-16 (* f1-18 f1-18))
                  )
                )
        (add-debug-line #t (bucket-id debug) s4-0 s3-0 *color-orange* #f (the-as rgba -1))
        (let ((s4-1 add-debug-text-3d)
              (s3-1 #t)
              (s2-1 583)
              )
          (format (clear *temp-string*) "~D" arg1)
          (s4-1 s3-1 (the-as bucket-id s2-1) *temp-string* s5-0 (font-color yellow) (the-as vector2h #f))
          )
        )
      )
    )
  (none)
  )

(defmethod debug-draw ((this trail-node) (arg0 int))
  (let ((s5-0 (new 'stack-no-clear 'vector))
        (s4-0 (new 'stack-no-clear 'sphere))
        )
    (set-vector!
      s5-0
      (* 4096.0 (the float (-> this x)))
      (* 4096.0 (the float (-> this y)))
      (* 4096.0 (the float (-> this z)))
      1.0
      )
    (set! (-> s4-0 quad) (-> s5-0 quad))
    (set! (-> s4-0 r) 4096.0)
    (let ((f0-8 (vector-vector-distance-squared s5-0 (math-camera-pos)))
          (f1-6 819200.0)
          )
      (when (and (< f0-8 (* f1-6 f1-6)) (sphere-in-view-frustum? s4-0))
        (add-debug-x #t (bucket-id debug) s5-0 *color-red*)
        (let ((s4-1 add-debug-text-3d)
              (s3-1 #t)
              (s2-1 583)
              )
          (format (clear *temp-string*) "~D" arg0)
          (s4-1 s3-1 (the-as bucket-id s2-1) *temp-string* s5-0 (font-color cyan) (the-as vector2h #f))
          )
        )
      )
    )
  )

(defmethod trail-graph-method-15 ((this trail-graph) (arg0 int))
  (local-vars (sv-80 int) (sv-96 (function _varargs_ object)))
  (let* ((s5-0 (-> this conn-hash))
         (s4-0 (-> s5-0 cell arg0))
         (s3-0 (new 'stack-no-clear 'inline-array 'vector 4))
         )
    (set-vector!
      (-> s3-0 0)
      (+ (* (-> s5-0 cell-width) (the float (logand arg0 15))) (-> s5-0 origin x))
      53248.0
      (+ (* (-> s5-0 cell-width) (the float (/ arg0 16))) (-> s5-0 origin z))
      1.0
      )
    (vector-copy! (-> s3-0 1) (-> s3-0 0))
    (+! (-> s3-0 1 x) (-> s5-0 cell-width))
    (vector-copy! (-> s3-0 2) (-> s3-0 1))
    (+! (-> s3-0 2 z) (-> s5-0 cell-width))
    (vector-copy! (-> s3-0 3) (-> s3-0 2))
    (set! (-> s3-0 3 x) (- (-> s3-0 3 x) (-> s5-0 cell-width)))
    (add-debug-line #t (bucket-id debug-no-zbuf1) (-> s3-0 0) (-> s3-0 1) *color-white* #f (the-as rgba -1))
    (add-debug-line #t (bucket-id debug-no-zbuf1) (-> s3-0 1) (-> s3-0 2) *color-white* #f (the-as rgba -1))
    (add-debug-line #t (bucket-id debug-no-zbuf1) (-> s3-0 2) (-> s3-0 3) *color-white* #f (the-as rgba -1))
    (add-debug-line #t (bucket-id debug-no-zbuf1) (-> s3-0 3) (-> s3-0 0) *color-white* #f (the-as rgba -1))
    (set-vector!
      (-> s3-0 3)
      (* 0.5 (+ (-> s3-0 0 x) (-> s3-0 2 x)))
      (-> s3-0 0 y)
      (* 0.5 (+ (-> s3-0 0 z) (-> s3-0 2 z)))
      1.0
      )
    (let ((s1-0 add-debug-text-3d)
          (s0-0 #t)
          )
      (set! sv-80 583)
      (set! sv-96 format)
      (let ((a0-21 (clear *temp-string*))
            (a1-5 "cell ~D (~D,~D)")
            (a2-4 arg0)
            (a3-4 (logand arg0 15))
            (t0-4 (/ arg0 16))
            )
        (sv-96 a0-21 a1-5 a2-4 a3-4 t0-4)
        )
      (let ((a2-5 *temp-string*)
            (a3-5 (-> s3-0 3))
            (t0-5 1)
            (t1-4 #f)
            )
        (s1-0 s0-0 (the-as bucket-id sv-80) a2-5 a3-5 (the-as font-color t0-5) (the-as vector2h t1-4))
        )
      )
    (countdown (s2-1 (-> s4-0 conn-count))
      (let ((s1-1 (-> this conn (-> s5-0 conn-ids (+ s2-1 (-> s4-0 first-conn))))))
        (get-position (-> this node (-> s1-1 head-id)) (-> s3-0 0))
        (+! (-> s3-0 0 x) -2048.0)
        (+! (-> s3-0 0 z) -2048.0)
        (get-position (-> this node (-> s1-1 tail-id)) (-> s3-0 1))
        )
      (+! (-> s3-0 1 x) -2048.0)
      (+! (-> s3-0 1 z) -2048.0)
      (add-debug-line #t (bucket-id debug) (-> s3-0 0) (-> s3-0 1) *color-white* #f (the-as rgba -1))
      )
    )
  #f
  )

;; WARN: new jak 2 until loop case, check carefully
(defmethod trail-graph-method-16 ((this trail-graph) (arg0 int) (arg1 (pointer uint16)) (arg2 vector) (arg3 vector) (arg4 rgba) (arg5 float))
  (local-vars (sv-48 int))
  (let ((s0-0 (new 'stack-no-clear 'inline-array 'vector 2)))
    (set-vector! (-> s0-0 1) (+ (-> arg2 x) arg5) (+ (-> arg2 y) arg5) (+ (-> arg2 z) arg5) 1.0)
    (set! sv-48 0)
    (until #f
      (vector-copy! (-> s0-0 0) (-> s0-0 1))
      (cond
        ((< sv-48 arg0)
         (let ((a0-6 (-> this node (-> arg1 sv-48))))
           (set-vector!
             (-> s0-0 1)
             (+ (* 4096.0 (the float (-> a0-6 x))) arg5)
             (+ (* 4096.0 (the float (-> a0-6 y))) arg5)
             (+ (* 4096.0 (the float (-> a0-6 z))) arg5)
             1.0
             )
           )
         )
        ((= sv-48 arg0)
         (set-vector! (-> s0-0 1) (+ (-> arg3 x) arg5) (+ (-> arg3 y) arg5) (+ (-> arg3 z) arg5) 1.0)
         )
        (else
          (return #f)
          )
        )
      (add-debug-line #t (bucket-id debug) (-> s0-0 0) (-> s0-0 1) arg4 #f (the-as rgba -1))
      (set! sv-48 (+ sv-48 1))
      )
    )
  #f
  )

;; WARN: new jak 2 until loop case, check carefully
(defmethod debug-draw ((this trail-graph))
  (when (= (-> this mode) 3)
    (let ((s5-0 (new 'stack-no-clear 'inline-array 'vector 2)))
      (vector-copy! (-> s5-0 1) (-> this orig-goal-pos))
      (let ((v1-3 (-> this goal-node-id)))
        (until #f
          (vector-copy! (-> s5-0 0) (-> s5-0 1))
          (cond
            ((>= v1-3 0)
             (let ((s4-0 (-> this node v1-3)))
               (set-vector!
                 (-> s5-0 1)
                 (+ 2048.0 (* 4096.0 (the float (-> s4-0 x))))
                 (+ 2048.0 (* 4096.0 (the float (-> s4-0 y))))
                 (+ 2048.0 (* 4096.0 (the float (-> s4-0 z))))
                 1.0
                 )
               (add-debug-line #t (bucket-id debug) (-> s5-0 0) (-> s5-0 1) *color-green* #f (the-as rgba -1))
               (set! v1-3 (-> s4-0 parent-id))
               )
             )
            (else
              (vector-copy! (-> s5-0 1) (-> this orig-start-pos))
              (add-debug-line #t (bucket-id debug) (-> s5-0 0) (-> s5-0 1) *color-green* #f (the-as rgba -1))
              (goto cfg-7)
              )
            )
          )
        )
      )
    #f
    )
  (label cfg-7)
  (case (-> this mode)
    ((1 2 3)
     (let ((s5-1 (new 'stack-no-clear 'vector)))
       (add-debug-sphere
         #t
         (bucket-id debug-no-zbuf1)
         (-> this orig-start-pos)
         (meters 1)
         (new 'static 'rgba :r #xff :a #x80)
         )
       (vector-copy! s5-1 (-> this conn-start-pos))
       (add-debug-sphere #t (bucket-id debug-no-zbuf1) s5-1 (meters 0.25) (new 'static 'rgba :r #xff :a #x80))
       (add-debug-sphere
         #t
         (bucket-id debug-no-zbuf1)
         (-> this orig-goal-pos)
         (meters 1)
         (new 'static 'rgba :g #xff :a #x80)
         )
       (vector-copy! s5-1 (-> this conn-goal-pos))
       (add-debug-sphere #t (bucket-id debug-no-zbuf1) s5-1 (meters 0.25) (new 'static 'rgba :g #xff :a #x80))
       )
     )
    )
  (dotimes (s5-2 (the-as int (-> this conn-count)))
    (debug-draw (-> this conn s5-2) this s5-2)
    )
  (dotimes (s5-3 (the-as int (-> this node-count)))
    (debug-draw (-> this node s5-3) s5-3)
    )
  (let* ((a2-7 (target-pos 0))
         (a0-40 (-> this conn-hash))
         (v1-26 (max 0 (min 15 (the int (/ (- (-> a2-7 x) (-> a0-40 origin x)) (-> a0-40 cell-width))))))
         (a1-16 (max 0 (min 15 (the int (/ (- (-> a2-7 z) (-> a0-40 origin z)) (-> a0-40 cell-width))))))
         )
    (trail-graph-method-15 this (+ (* a1-16 16) v1-26))
    )
  )

(defmethod trail-graph-method-18 ((this trail-graph) (arg0 vector))
  (let ((a0-1 (-> this conn-hash)))
    (+ (max 0 (min 15 (the int (/ (- (-> arg0 x) (-> a0-1 origin x)) (-> a0-1 cell-width)))))
       (* (max 0 (min 15 (the int (/ (- (-> arg0 z) (-> a0-1 origin z)) (-> a0-1 cell-width))))) 16)
       )
    )
  )

(defmethod get-position ((this trail-node) (arg0 vector))
  (let ((v0-0 arg0))
    (set! (-> v0-0 x) (* 4096.0 (the float (-> this x))))
    (set! (-> v0-0 y) (* 4096.0 (the float (-> this y))))
    (set! (-> v0-0 z) (* 4096.0 (the float (-> this z))))
    (set! (-> v0-0 w) 1.0)
    v0-0
    )
  )

(defmethod trail-graph-method-20 ((this trail-graph) (arg0 uint) (arg1 vector))
  (get-position (-> this node (the-as int arg0)) arg1)
  )

(defmethod trail-graph-method-21 ((this trail-graph) (arg0 (pointer uint16)) (arg1 int) (arg2 (pointer int32)) (arg3 (pointer float)))
  (set! (-> arg3 0) 0.0)
  (set! (-> arg2 0) (-> this goal-node-id))
  (let ((v0-0 -1))
    (when (= (-> this mode) 3)
      (let ((v1-3 (-> this node))
            (a3-2 0)
            )
        (let ((t1-0 (-> this goal-node-id)))
          (while (>= t1-0 0)
            (+! a3-2 1)
            (set! t1-0 (-> v1-3 t1-0 parent-id))
            )
          )
        (let ((t1-4 (-> this goal-node-id)))
          (let ((t2-1 (- a3-2 arg1)))
            (cond
              ((> t2-1 0)
               (while (> t2-1 0)
                 (set! t1-4 (-> v1-3 t1-4 parent-id))
                 (+! t2-1 -1)
                 )
               (set! v0-0 arg1)
               )
              (else
                (set! v0-0 a3-2)
                )
              )
            )
          (countdown (a2-3 v0-0)
            (set! (-> arg0 a2-3) (the-as uint t1-4))
            (set! t1-4 (-> v1-3 t1-4 parent-id))
            )
          )
        (when (> a3-2 0)
          (let ((a0-3 (-> v1-3 (-> this goal-node-id)))
                (v1-4 (-> v1-3 (-> arg0 0)))
                )
            (set! (-> arg3 0)
                  (- (* 512.0 (the float (-> a0-3 cost-from-start))) (* 512.0 (the float (-> v1-4 cost-from-start))))
                  )
            )
          )
        )
      )
    v0-0
    )
  )

(defmethod trail-graph-method-23 ((this trail-graph))
  (let ((a3-0 (shr (+ (-> this node-count) 127) 7)))
    (when (< 6 (the-as int a3-0))
      (format 0 "ERROR: <SW> TRAIL_NODE_BIT_ARRAY_QUAD_COUNT is ~d, but should be ~d!  Please change it!~%" 6 a3-0)
      (return #f)
      )
    )
  (let ((a3-1 (shr (+ (-> this conn-count) 127) 7)))
    (when (< 7 (the-as int a3-1))
      (format 0 "ERROR: <SW> TRAIL_CONN_BIT_ARRAY_QUAD_COUNT is ~d, but should be ~d!  Please change it!~%" 7 a3-1)
      (return #f)
      )
    )
  (set! (-> this mode) (the-as uint 0))
  (set! (-> this goal-conn-id) -1)
  (set! (-> this goal-node-id) -1)
  (set! (-> this open-head-id) -1)
  (let ((gp-0 *minimap*))
    (when gp-0
      (countdown (s5-0 6)
        (reset (-> gp-0 trail s5-0))
        )
      )
    )
  #t
  )

(defmethod trail-graph-method-26 ((this trail-graph))
  (when (nonzero? (-> this mode))
    (set! (-> this goal-node-id) -1)
    (let ((a1-0 (-> this goal-conn-id)))
      (when (>= a1-0 0)
        (trail-graph-method-24 this (the-as uint a1-0) (-> this orig-goal-pos) 0 1)
        (set! (-> this goal-conn-id) -1)
        )
      )
    (set! (-> this pov-can-see-goal) (the-as uint 0))
    (set! (-> this open-head-id) -1)
    (let ((v1-7 (-> this open-quads)))
      (set! (-> v1-7 0 quad) (the-as uint128 0))
      (set! (-> v1-7 1 quad) (the-as uint128 0))
      (set! (-> v1-7 2 quad) (the-as uint128 0))
      (set! (-> v1-7 3 quad) (the-as uint128 0))
      (set! (-> v1-7 4 quad) (the-as uint128 0))
      (set! (-> v1-7 5 quad) (the-as uint128 0))
      )
    0
    (let ((v1-9 (-> this closed-quads)))
      (set! (-> v1-9 0 quad) (the-as uint128 0))
      (set! (-> v1-9 1 quad) (the-as uint128 0))
      (set! (-> v1-9 2 quad) (the-as uint128 0))
      (set! (-> v1-9 3 quad) (the-as uint128 0))
      (set! (-> v1-9 4 quad) (the-as uint128 0))
      (set! (-> v1-9 5 quad) (the-as uint128 0))
      )
    0
    (set! (-> this mode) (the-as uint 0))
    0
    )
  )

(defmethod trail-graph-method-24 ((this trail-graph) (arg0 uint) (arg1 vector) (arg2 int) (arg3 int))
  (let* ((s5-0 (lognot arg3))
         (a0-2 (-> this conn arg0))
         (a1-1 (-> a0-2 visgroup-id))
         (v1-1 (-> this node))
         (s4-0 #f)
         )
    (cond
      ((> a1-1 0)
       (let* ((a1-4 (the-as object (+ (the-as uint (-> this visgroup)) (* (+ a1-1 -1) 4))))
              (a0-7 (&-> (-> this visnode-ids) (-> (the-as (pointer uint16) a1-4))))
              )
         (countdown (a1-5 (-> (the-as trail-conn-hash-cell a1-4) conn-count))
           (let ((a3-4 (-> v1-1 (-> a0-7 0))))
             (if (logtest? (-> a3-4 flags) (trail-node-flag tnf2))
                 (set! s4-0 #t)
                 (set! (-> a3-4 flags) (logior (logand (-> a3-4 flags) s5-0) arg2))
                 )
             )
           (set! a0-7 (&-> a0-7 1))
           )
         )
       )
      (else
        (dotimes (a1-6 2)
          (let ((a3-7 (-> v1-1 (if (zero? a1-6)
                                   (-> a0-2 head-id)
                                   (-> a0-2 tail-id)
                                   )
                          )
                      )
                )
            (if (logtest? (-> a3-7 flags) (trail-node-flag tnf2))
                (set! s4-0 #t)
                (set! (-> a3-7 flags) (logior (logand (-> a3-7 flags) s5-0) arg2))
                )
            )
          )
        )
      )
    (when s4-0
      (let ((v1-11 (-> this cell-pov-bit-arrays (trail-graph-method-18 this arg1)))
            (a0-10 (-> this node 0))
            )
        (while (nonzero? v1-11)
          (if (logtest? v1-11 1)
              (set! (-> a0-10 flags) (logior (logand (-> a0-10 flags) s5-0) arg2))
              )
          (&+! a0-10 20)
          (set! v1-11 (shr v1-11 1))
          )
        )
      0
      )
    s4-0
    )
  )

(defmethod trail-graph-method-28 ((this trail-graph) (arg0 trail-conn-search) (arg1 int) (arg2 int))
  (let* ((v1-1 (+ (* arg2 16) arg1))
         (a0-1 (/ v1-1 8))
         (a1-2 (ash 1 (logand v1-1 7)))
         (a2-4 (-> arg0 cell-quads 0 byte a0-1))
         )
    (when (not (logtest? a2-4 a1-2))
      (set! (-> arg0 cell-quads 0 byte a0-1) (logior a2-4 a1-2))
      (let* ((v1-3 (-> this conn-hash cell v1-1))
             (s4-0 (&-> (-> this conn-hash conn-ids) (-> v1-3 first-conn)))
             )
        (countdown (s3-0 (-> v1-3 conn-count))
          (let* ((s2-0 (-> s4-0 0))
                 (v1-4 (shr s2-0 3))
                 (a1-8 (ash 1 (the-as int (logand s2-0 7))))
                 (a2-5 (-> arg0 conn-quads 0 byte v1-4))
                 )
            (when (not (logtest? a2-5 a1-8))
              (set! (-> arg0 conn-quads 0 byte v1-4) (logior a2-5 a1-8))
              (let* ((v1-7 (-> this conn s2-0))
                     (a0-14 (-> v1-7 flags))
                     )
                (when (= (logand (the-as conn-flag (-> this conn-mask)) a0-14) a0-14)
                  (let ((a3-2 (-> this node (-> v1-7 head-id)))
                        (v1-10 (-> this node (-> v1-7 tail-id)))
                        (a1-15 (new 'stack-no-clear 'vector))
                        (a2-7 (new 'stack-no-clear 'vector))
                        (s1-0 (new 'stack-no-clear 'vector))
                        )
                    (set-vector!
                      a1-15
                      (* 4096.0 (the float (-> a3-2 x)))
                      (* 4096.0 (the float (-> a3-2 y)))
                      (* 4096.0 (the float (-> a3-2 z)))
                      1.0
                      )
                    (set-vector!
                      a2-7
                      (* 4096.0 (the float (-> v1-10 x)))
                      (* 4096.0 (the float (-> v1-10 y)))
                      (* 4096.0 (the float (-> v1-10 z)))
                      1.0
                      )
                    (let ((f0-14 (vector-segment-distance-point! (-> arg0 src-pos) a1-15 a2-7 s1-0)))
                      (when (or (< (-> arg0 best-conn-id) 0) (< f0-14 (-> arg0 best-dist)))
                        (when (or (and (>= (-> arg0 src-pos y) (-> this over-under-thresh)) (>= (-> s1-0 y) (-> this over-under-above-too-low)))
                                  (and (< (-> arg0 src-pos y) (-> this over-under-thresh)) (>= (-> this over-under-below-too-high) (-> s1-0 y)))
                                  )
                          (set! (-> arg0 best-dist) f0-14)
                          (set! (-> arg0 best-conn-id) (the-as int s2-0))
                          (vector-copy! (-> arg0 conn-pos) s1-0)
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          (set! s4-0 (&-> s4-0 1))
          )
        )
      #f
      )
    )
  )

(defmethod trail-graph-method-17 ((this trail-graph) (arg0 vector) (arg1 vector))
  (let ((v1-0 (-> this conn-hash))
        (gp-0 (new 'stack-no-clear 'trail-conn-search))
        )
    (set! (-> gp-0 src-pos) arg0)
    (set! (-> gp-0 conn-pos) arg1)
    (set! (-> gp-0 best-conn-id) -1)
    (let ((a0-2 (-> gp-0 conn-quads)))
      (set! (-> a0-2 0 quad) (the-as uint128 0))
      (set! (-> a0-2 1 quad) (the-as uint128 0))
      (set! (-> a0-2 2 quad) (the-as uint128 0))
      (set! (-> a0-2 3 quad) (the-as uint128 0))
      (set! (-> a0-2 4 quad) (the-as uint128 0))
      (set! (-> a0-2 5 quad) (the-as uint128 0))
      (set! (-> a0-2 6 quad) (the-as uint128 0))
      )
    0
    (let ((a0-4 (-> gp-0 cell-quads)))
      (set! (-> a0-4 0 quad) (the-as uint128 0))
      (set! (-> a0-4 1 quad) (the-as uint128 0))
      )
    0
    (let* ((f0-0 (-> v1-0 cell-width))
           (f1-1 (/ (the float f0-0) 2))
           )
      (let ((f3-0 (- (-> arg0 x) f1-1))
            (f2-2 (- (-> arg0 z) f1-1))
            )
        (set! (-> gp-0 bounds min x) (max 0 (min 15 (the int (/ (- f3-0 (-> v1-0 origin x)) f0-0)))))
        (set! (-> gp-0 bounds min z) (max 0 (min 15 (the int (/ (- f2-2 (-> v1-0 origin z)) f0-0)))))
        )
      (let ((f2-7 (+ (-> arg0 x) f1-1))
            (f1-2 (+ (-> arg0 z) f1-1))
            )
        (set! (-> gp-0 bounds max x) (max 0 (min 15 (the int (/ (- f2-7 (-> v1-0 origin x)) f0-0)))))
        (set! (-> gp-0 bounds max z) (max 0 (min 15 (the int (/ (- f1-2 (-> v1-0 origin z)) f0-0)))))
        )
      )
    (let ((s4-0 (-> gp-0 bounds min z)))
      (until (< (-> gp-0 bounds max z) s4-0)
        (let ((s3-0 (-> gp-0 bounds min x)))
          (until (< (-> gp-0 bounds max x) s3-0)
            (trail-graph-method-28 this gp-0 s3-0 s4-0)
            (+! s3-0 1)
            )
          )
        (+! s4-0 1)
        )
      )
    (while (< (-> gp-0 best-conn-id) 0)
      (let ((v1-8 15))
        (set! (-> gp-0 bounds min x) (max 0 (+ (-> gp-0 bounds min x) -1)))
        (set! (-> gp-0 bounds min z) (max 0 (+ (-> gp-0 bounds min z) -1)))
        (set! (-> gp-0 bounds max x) (min (+ (-> gp-0 bounds max x) 1) v1-8))
        (set! (-> gp-0 bounds max z) (min (+ (-> gp-0 bounds max z) 1) v1-8))
        )
      (let ((s4-1 (-> gp-0 bounds min x)))
        (until (< (-> gp-0 bounds max x) s4-1)
          (trail-graph-method-28 this gp-0 s4-1 (-> gp-0 bounds min z))
          (trail-graph-method-28 this gp-0 s4-1 (-> gp-0 bounds max z))
          (+! s4-1 1)
          )
        )
      (let ((s4-2 (-> gp-0 bounds min z)))
        (until (< (-> gp-0 bounds max z) s4-2)
          (trail-graph-method-28 this gp-0 (-> gp-0 bounds min x) s4-2)
          (trail-graph-method-28 this gp-0 (-> gp-0 bounds max x) s4-2)
          (+! s4-2 1)
          )
        )
      )
    (-> gp-0 best-conn-id)
    )
  )

(defmethod trail-graph-method-9 ((this trail-graph) (arg0 int))
  (let ((s4-0 (-> this node arg0)))
    (set! (-> s4-0 cost-from-start) (get-dist-score s4-0 (-> this orig-start-pos)))
    (set! (-> s4-0 cost-to-goal) (get-dist-score s4-0 (-> this orig-goal-pos)))
    )
  (trail-graph-method-11 this arg0 -1)
  0
  )

(defmethod trail-graph-method-10 ((this trail-graph) (arg0 int))
  (let* ((s4-0 (-> this conn arg0))
         (v1-1 (-> s4-0 visgroup-id))
         (s5-0 #f)
         )
    (cond
      ((> v1-1 0)
       (let* ((v1-4 (the-as object (+ (the-as uint (-> this visgroup)) (* (+ v1-1 -1) 4))))
              (s4-1 (&-> (-> this visnode-ids) (-> (the-as (pointer uint16) v1-4))))
              (s3-0 (-> (the-as trail-conn-hash-cell v1-4) conn-count))
              )
         (-> this visnode-ids)
         (while (nonzero? s3-0)
           (+! s3-0 -1)
           (let ((a1-2 (-> s4-1 0)))
             (if (logtest? (-> this node a1-2 flags) (trail-node-flag tnf2))
                 (set! s5-0 #t)
                 (trail-graph-method-9 this (the-as int a1-2))
                 )
             )
           (set! s4-1 (&-> s4-1 1))
           )
         )
       )
      (else
        (dotimes (s3-1 2)
          (let ((a1-3 (if (zero? s3-1)
                          (-> s4-0 head-id)
                          (-> s4-0 tail-id)
                          )
                      )
                )
            (if (logtest? (-> this node a1-3 flags) (trail-node-flag tnf2))
                (set! s5-0 #t)
                (trail-graph-method-9 this (the-as int a1-3))
                )
            )
          )
        )
      )
    (when s5-0
      (trail-graph-method-32 this)
      (let ((s4-2 (-> this cached-start-pov pov-can-see-start))
            (s3-2 0)
            )
        (while (nonzero? s4-2)
          (if (logtest? s4-2 1)
              (trail-graph-method-9 this s3-2)
              )
          (+! s3-2 1)
          (set! s4-2 (shr s4-2 1))
          )
        )
      0
      )
    s5-0
    )
  )

;; WARN: new jak 2 until loop case, check carefully
(defmethod trail-graph-method-32 ((this trail-graph))
  (local-vars (s4-2 uint))
  (let* ((gp-0 (-> this cached-start-pov))
         (v1-1 (current-time))
         (a0-2 (- v1-1 (-> gp-0 last-updated)))
         )
    (cond
      ((< (seconds 1) a0-2)
       (set! (-> gp-0 last-updated) v1-1)
       (vector-copy! (-> gp-0 start-pos) (-> this orig-start-pos))
       (let* ((v1-7 (-> this cell-pov-bit-arrays (trail-graph-method-18 this (-> gp-0 start-pos))))
              (s3-0 (-> this node 0))
              (s2-0 1)
              (s4-1 (lognot v1-7))
              )
         (countdown (s1-0 (-> this pov-node-count))
           (if (and (logtest? s4-1 s2-0) (not (trail-graph-method-12 this s3-0 (-> gp-0 start-pos))))
               (set! s4-1 (logior s4-1 s2-0))
               )
           (set! s2-0 (* s2-0 2))
           (&+! s3-0 20)
           )
         (set! s4-2 (lognot s4-1))
         )
       (set! (-> gp-0 pov-can-see-start) s4-2)
       s4-2
       )
      ((> a0-2 0)
       (set! (-> gp-0 last-updated) v1-1)
       (vector-copy! (-> gp-0 start-pos) (-> this orig-start-pos))
       (let ((s3-1 (-> this cell-pov-bit-arrays (trail-graph-method-18 this (-> gp-0 start-pos)))))
         (set! s4-2 (logand (-> gp-0 pov-can-see-start) s3-1))
         (let ((s2-1 (-> gp-0 next-node-id)))
           (let ((s1-1 6))
             (until #f
               (let ((s0-0 (ash 1 s2-1)))
                 (when (logtest? s3-1 s0-0)
                   (if (trail-graph-method-12 this (-> this node s2-1) (-> gp-0 start-pos))
                       (set! s4-2 (logior s4-2 s0-0))
                       (set! s4-2 (logclear s4-2 s0-0))
                       )
                   (+! s1-1 -1)
                   (if (zero? s1-1)
                       (goto cfg-27)
                       )
                   )
                 )
               (set! s2-1 (mod (+ s2-1 1) (the-as int (-> this pov-node-count))))
               (if (= s2-1 (-> gp-0 next-node-id))
                   (goto cfg-27)
                   )
               )
             )
           #f
           (label cfg-27)
           (set! (-> gp-0 next-node-id) s2-1)
           )
         )
       (set! (-> gp-0 pov-can-see-start) s4-2)
       s4-2
       )
      )
    )
  )

(defmethod trail-graph-method-13 ((this trail-graph) (arg0 vector) (arg1 vector))
  (let ((gp-0 (new 'stack-no-clear 'inline-array 'vector 3)))
    (vector-copy! (-> gp-0 1) arg0)
    (set! (-> gp-0 1 y) 0.0)
    (set! (-> gp-0 1 w) 1.0)
    (vector-copy! (-> gp-0 2) arg1)
    (set! (-> gp-0 2 y) 0.0)
    (set! (-> gp-0 2 w) 1.0)
    (let ((s5-0 (-> this blocker 0)))
      (countdown (s4-0 (-> this blocker-count))
        (let ((f0-5 (vector4-dot (-> gp-0 1) (the-as vector (-> s5-0 plane))))
              (f1-1 (vector4-dot (-> gp-0 2) (the-as vector (-> s5-0 plane))))
              )
          (when (< (* f0-5 f1-1) 0.0)
            (vector-! (-> gp-0 0) (-> gp-0 2) (-> gp-0 1))
            (vector-float*! (-> gp-0 0) (-> gp-0 0) (/ f0-5 (- f0-5 f1-1)))
            (vector+! (-> gp-0 0) (-> gp-0 0) (-> gp-0 1))
            (if (>= (-> s5-0 center w) (vector-vector-xz-distance-squared (-> gp-0 0) (-> s5-0 center)))
                (return #f)
                )
            )
          )
        (&+! s5-0 32)
        )
      )
    )
  #t
  )

(defmethod trail-graph-method-12 ((this trail-graph) (arg0 trail-node) (arg1 vector))
  (let ((v1-0 (new 'stack-no-clear 'vector)))
    (set-vector! v1-0 (* 4096.0 (the float (-> arg0 x))) 0.0 (* 4096.0 (the float (-> arg0 z))) 1.0)
    (trail-graph-method-13 this v1-0 arg1)
    )
  )

;; WARN: new jak 2 until loop case, check carefully
(defmethod trail-graph-method-11 ((this trail-graph) (arg0 int) (arg1 int))
  (let ((v1-0 (/ arg0 8))
        (a3-1 (ash 1 (logand arg0 7)))
        )
    (logior! (-> this open-quads 0 byte v1-0) a3-1)
    )
  (let* ((v1-2 (-> this node))
         (v0-0 (-> v1-2 arg0))
         )
    (set! (-> v0-0 parent-id) arg1)
    (let ((a3-6 (+ (-> v0-0 cost-from-start) (-> v0-0 cost-to-goal)))
          (t0-4 (-> this open-head-id))
          (a2-2 -1)
          )
      (until #f
        (when (< t0-4 0)
          (set! (-> v0-0 next-id) -1)
          (set! (-> v0-0 prev-id) a2-2)
          (if (>= a2-2 0)
              (set! (-> v1-2 a2-2 next-id) arg0)
              (set! (-> this open-head-id) arg0)
              )
          (return v0-0)
          )
        (let ((t1-4 (-> v1-2 t0-4)))
          (when (>= (+ (-> t1-4 cost-from-start) (-> t1-4 cost-to-goal)) a3-6)
            (set! (-> v0-0 next-id) t0-4)
            (set! (-> v0-0 prev-id) a2-2)
            (set! (-> t1-4 prev-id) arg0)
            (if (>= a2-2 0)
                (set! (-> v1-2 a2-2 next-id) arg0)
                (set! (-> this open-head-id) arg0)
                )
            (return v0-0)
            )
          (set! a2-2 t0-4)
          (set! t0-4 (-> t1-4 next-id))
          )
        )
      )
    #f
    v0-0
    )
  )

(defmethod trail-graph-method-25 ((this trail-graph) (arg0 int))
  (let ((v1-0 (/ arg0 8))
        (a2-1 (ash 1 (logand arg0 7)))
        )
    (logior! (-> this open-quads 0 byte v1-0) a2-1)
    )
  (let* ((v1-2 (-> this node))
         (a2-4 (-> v1-2 arg0))
         (a1-2 (-> a2-4 prev-id))
         (a2-5 (-> a2-4 next-id))
         )
    (cond
      ((>= a1-2 0)
       (set! (-> v1-2 a1-2 next-id) a2-5)
       (when (>= a2-5 0)
         (set! (-> v1-2 a2-5 prev-id) a1-2)
         a1-2
         )
       )
      (else
        (set! (-> this open-head-id) a2-5)
        (when (>= a2-5 0)
          (let ((v0-0 -1))
            (set! (-> v1-2 a2-5 prev-id) v0-0)
            v0-0
            )
          )
        )
      )
    )
  )

(defmethod trail-graph-method-27 ((this trail-graph))
  (let ((v0-0 (-> this open-head-id)))
    (when (>= v0-0 0)
      (let* ((v1-1 (-> this node))
             (a2-0 (-> v1-1 v0-0 next-id))
             )
        (set! (-> this open-head-id) a2-0)
        (if (>= a2-0 0)
            (set! (-> v1-1 a2-0 prev-id) -1)
            )
        )
      (let ((v1-3 (/ v0-0 8))
            (a1-6 (ash 1 (logand v0-0 7)))
            )
        (logior! (-> this closed-quads 0 byte v1-3) a1-6)
        (logxor! (-> this open-quads 0 byte v1-3) (the-as uint a1-6))
        )
      )
    v0-0
    )
  )

;; WARN: Return type mismatch int vs uint.
(defmethod get-dist-score ((this trail-node) (arg0 vector))
  (let* ((f0-1 (- (-> arg0 x) (* 4096.0 (the float (-> this x)))))
         (f1-3 (- (-> arg0 z) (* 4096.0 (the float (-> this z)))))
         (f0-4 (sqrtf (+ (* f0-1 f0-1) (* f1-3 f1-3))))
         )
    (the uint (fmin 65535.0 (* 0.00024414062 (* 8.0 f0-4))))
    )
  )

(defmethod trail-graph-method-33 ((this trail-graph) (arg0 int))
  (let ((s5-0 (-> this node arg0)))
    (if (not (logtest? (-> s5-0 flags) (trail-node-flag tnf2)))
        (return #t)
        )
    (let ((s4-0 (ash 1 arg0)))
      (if (logtest? s4-0 (-> this pov-can-see-goal))
          (return #t)
          )
      (cond
        ((trail-graph-method-12 this s5-0 (-> this orig-goal-pos))
         (logior! (-> this pov-can-see-goal) s4-0)
         #t
         )
        (else
          (logclear! (-> s5-0 flags) (trail-node-flag tnf0))
          #f
          )
        )
      )
    )
  )

(defmethod trail-graph-method-30 ((this trail-graph))
  (let ((s5-0 (trail-graph-method-27 this)))
    (if (< s5-0 0)
        (return 2)
        )
    (let ((s4-0 (-> this node s5-0)))
      (when (and (logtest? (-> s4-0 flags) (trail-node-flag tnf0)) (trail-graph-method-33 this s5-0))
        (set! (-> this goal-node-id) s5-0)
        (return 3)
        )
      (let ((s3-0 (&-> (-> this conn-ids) (-> s4-0 first-conn))))
        (countdown (s2-0 (-> s4-0 conn-count))
          (let* ((a0-8 (-> this conn (-> s3-0 0)))
                 (v1-14 (-> a0-8 flags))
                 )
            (when (= (logand (the-as conn-flag (-> this conn-mask)) v1-14) v1-14)
              (let ((s1-0 (-> a0-8 tail-id)))
                (if (= s1-0 s5-0)
                    (set! s1-0 (cond
                                 ((logtest? v1-14 (conn-flag cf0))
                                  (goto cfg-27)
                                  s1-0
                                  )
                                 (else
                                   (-> a0-8 head-id)
                                   )
                                 )
                          )
                    )
                (let ((s0-0 (-> this node s1-0))
                      (v1-21 (min #xffff (the-as int (+ (-> a0-8 cost) (-> s4-0 cost-from-start)))))
                      (a0-11 (shr s1-0 3))
                      (a1-9 (ash 1 (the-as int (logand s1-0 7))))
                      )
                  (cond
                    ((logtest? (-> this open-quads 0 byte a0-11) a1-9)
                     (when (< (the-as uint v1-21) (-> s0-0 cost-from-start))
                       (set! (-> s0-0 cost-from-start) (the-as uint v1-21))
                       (trail-graph-method-25 this (the-as int s1-0))
                       (trail-graph-method-11 this (the-as int s1-0) s5-0)
                       )
                     )
                    ((not (logtest? (-> this closed-quads 0 byte a0-11) a1-9))
                     (set! (-> s0-0 cost-from-start) (the-as uint v1-21))
                     (set! (-> s0-0 cost-to-goal) (get-dist-score s0-0 (-> this orig-goal-pos)))
                     (trail-graph-method-11 this (the-as int s1-0) s5-0)
                     )
                    ((< (the-as uint v1-21) (-> s0-0 cost-from-start))
                     (set! (-> s0-0 cost-from-start) (the-as uint v1-21))
                     (trail-graph-method-11 this (the-as int s1-0) s5-0)
                     )
                    )
                  )
                )
              )
            )
          (label cfg-27)
          (set! s3-0 (&-> s3-0 1))
          )
        )
      )
    )
  1
  )

;; ERROR: Unsupported inline assembly instruction kind - [mfc0 v1, Count]
;; ERROR: Unsupported inline assembly instruction kind - [mfc0 v1, Count]
(defmethod trail-graph-method-31 ((this trail-graph) (arg0 int))
  (local-vars (v1-1 int))
  (let ((v0-0 (the-as int (-> this mode))))
    0
    (.mfc0 v1-1 Count)
    (while (and (= v0-0 1)
                ;; og:preserve-this
                ;; changed in PC port: they used to abort early if searching takes too long
                ;; it is fast enough on PC that we don't care.
                ;; (< (the-as uint v1-1) (the-as uint arg0)) HACK
      )
      (set! v0-0 (trail-graph-method-30 this))
      (.mfc0 v1-1 Count)
      )
    (set! (-> this mode) (the-as uint v0-0))
    )
  (none)
  )

(deftype trail-vis-work (structure)
  ((best-count     uint32)
   (best-dist      float)
   (start-conn-id  uint32)
   (p0             vector  :inline)
   (p1             vector  :inline)
   (best-node-id   uint16  64)
   )
  )


(defmethod trail-graph-method-22 ((this trail-graph) (arg0 int) (arg1 int))
  (local-vars (s4-1 symbol))
  (let* ((s4-0 (-> this node))
         (v1-2 (-> s4-0 arg1))
         (s5-0 (new 'stack-no-clear 'trail-vis-work))
         )
    (set! (-> s5-0 start-conn-id) (the-as uint arg0))
    (set-vector!
      (-> s5-0 p0)
      (* 4096.0 (the float (-> v1-2 x)))
      (* 4096.0 (the float (-> v1-2 y)))
      (* 4096.0 (the float (-> v1-2 z)))
      1.0
      )
    (vector-copy! (-> s5-0 p1) (-> s5-0 p0))
    (set! (-> s5-0 best-count) (the-as uint 0))
    (set! (-> s5-0 best-dist) -1.0)
    (let ((s2-0 (&-> (-> this conn-ids) (-> v1-2 first-conn))))
      (countdown (s1-0 (-> v1-2 conn-count))
        (let* ((v1-4 (-> this conn (-> s2-0 0)))
               (s0-0 (-> v1-4 tail-id))
               )
          (if (= s0-0 arg1)
              (set! s0-0 (-> v1-4 head-id))
              )
          (let ((v1-8 (-> s4-0 s0-0)))
            (set! (-> s5-0 p1 x) (* 4096.0 (the float (-> v1-8 x))))
            (set! (-> s5-0 p1 y) (* 4096.0 (the float (-> v1-8 y))))
            (set! (-> s5-0 p1 z) (* 4096.0 (the float (-> v1-8 z))))
            )
          (let ((f0-14 (vector-segment-distance-point! (-> this orig-goal-pos) (-> s5-0 p0) (-> s5-0 p1) (the-as vector #f)))
                (f1-12 (-> s5-0 best-dist))
                )
            (cond
              ((or (< f1-12 0.0) (< f0-14 f1-12))
               (set! (-> s5-0 best-dist) f0-14)
               (set! (-> s5-0 best-count) (the-as uint 1))
               (set! (-> s5-0 best-node-id 0) s0-0)
               )
              ((= f0-14 f1-12)
               (let ((v1-15 (-> s5-0 best-count)))
                 (when (< v1-15 (the-as uint 64))
                   (set! (-> s5-0 best-node-id v1-15) s0-0)
                   (set! (-> s5-0 best-count) (+ v1-15 1))
                   )
                 )
               )
              )
            )
          )
        (set! s2-0 (&-> s2-0 1))
        )
      )
    (trail-graph-method-24 this (-> s5-0 start-conn-id) (the-as vector #f) 2 0)
    (countdown (v1-20 (-> s5-0 best-count))
      (let ((a1-16 (-> s4-0 (-> s5-0 best-node-id v1-20))))
        (when (= (logand (-> a1-16 flags) (trail-node-flag tnf0 tnf1)) (trail-node-flag tnf0 tnf1))
          (set! s4-1 #t)
          (goto cfg-22)
          )
        )
      )
    (set! s4-1 #f)
    (label cfg-22)
    (trail-graph-method-24 this (-> s5-0 start-conn-id) (the-as vector #f) 0 2)
    )
  s4-1
  )

(defmethod trail-graph-method-29 ((this trail-graph) (arg0 vector) (arg1 vector) (arg2 trail-cached-search-info))
  (local-vars (v0-6 int))
  (trail-graph-method-26 this)
  (+! (-> this search-id) 1)
  (vector-copy! (-> this orig-start-pos) arg0)
  (let ((s5-1 0))
    (let ((a1-1 -1))
      (when arg2
        (let ((v1-6 (-> arg2 goal-conn-id)))
          (when (and (>= v1-6 0)
                     (= (-> arg2 orig-goal-pos x) (-> arg1 x))
                     (= (-> arg2 orig-goal-pos y) (-> arg1 y))
                     (= (-> arg2 orig-goal-pos z) (-> arg1 z))
                     )
            (set! a1-1 v1-6)
            (vector-copy! (-> this conn-goal-pos) (-> arg2 conn-goal-pos))
            )
          )
        )
      (vector-copy! (-> this orig-goal-pos) arg1)
      (when (< a1-1 0)
        (set! a1-1 (trail-graph-method-17 this (-> this orig-goal-pos) (-> this conn-goal-pos)))
        (when arg2
          (set! (-> arg2 goal-conn-id) a1-1)
          (vector-copy! (-> arg2 orig-goal-pos) (-> this orig-goal-pos))
          (vector-copy! (-> arg2 conn-goal-pos) (-> this conn-goal-pos))
          )
        )
      (set! (-> this goal-conn-id) a1-1)
      (if (trail-graph-method-24 this (the-as uint a1-1) (-> this orig-goal-pos) 1 0)
          (set! s5-1 2)
          )
      )
    (let ((v1-19 -1))
      (let ((a0-16 (-> *game-info* features)))
        (if (not (logtest? (game-feature feature31) a0-16))
            (set! v1-19 (logand -3 v1-19))
            )
        (if (not (logtest? (game-feature feature32) a0-16))
            (set! v1-19 (logand -5 v1-19))
            )
        (if (not (logtest? (game-feature feature33) a0-16))
            (set! v1-19 (logand -9 v1-19))
            )
        (if (not (logtest? (game-feature feature34) a0-16))
            (set! v1-19 (logand -17 v1-19))
            )
        (if (not (logtest? (game-feature feature35) a0-16))
            (set! v1-19 (logand -33 v1-19))
            )
        )
      (set! (-> this conn-mask) (the-as uint v1-19))
      )
    (let ((s4-1 (trail-graph-method-17 this (-> this orig-start-pos) (-> this conn-start-pos))))
      (if (trail-graph-method-10 this s4-1)
          (set! s5-1 (logior s5-1 1))
          )
      (set! (-> this mode) (the-as uint 1))
      (cond
        ((zero? s5-1)
         (let ((a2-5 (-> this open-head-id)))
           (when (and (logtest? (-> this node a2-5 flags) (trail-node-flag tnf0)) (trail-graph-method-22 this s4-1 a2-5))
             (set! v0-6 3)
             (set! (-> this mode) (the-as uint v0-6))
             v0-6
             )
           )
         )
        ((= s5-1 3)
         (when (trail-graph-method-13 this (-> this orig-start-pos) (-> this orig-goal-pos))
           (set! v0-6 3)
           (set! (-> this mode) (the-as uint v0-6))
           v0-6
           )
         )
        )
      )
    )
  )

(if (not (trail-graph-method-23 *trail-graph*))
    (set! *trail-graph* #f)
    )
