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

;; name: find-nearest.gc
;; name in dgo: find-nearest
;; dgos: GAME

;; DECOMP BEGINS

(deftype search-info (structure)
  ((point         vector  :inline)
   (best-point    vector  :inline)
   (match-handle  handle)
   (match         process-focusable)
   (best          float)
   (radius        float)
   (rating        search-info-flag)
   (require       search-info-flag)
   (mask          search-info-flag)
   (rot-base      vector  :inline)
   (back-point    vector  :inline)
   (rot-range     float)
   )
  )


(define *search-info* (new 'global 'search-info))

;; ERROR: Stack slot load at 720 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 784 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 720 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 784 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 720 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 784 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 720 mismatch: defined as size 4, got size 16
;; ERROR: Stack slot load at 784 mismatch: defined as size 4, got size 16
;; ERROR: Unsupported inline assembly instruction kind - [mula.s f0, f3]
;; ERROR: Unsupported inline assembly instruction kind - [madda.s f1, f4]
;; ERROR: Unsupported inline assembly instruction kind - [madd.s f0, f2, f5]
(defun find-nearest-focusable ((arg0 (array collide-shape))
                      (arg1 vector)
                      (arg2 float)
                      (arg3 search-info-flag)
                      (arg4 search-info-flag)
                      (arg5 vector)
                      (arg6 vector)
                      (arg7 float)
                      )
  (local-vars
    (f0-12 float)
    (sv-704 (function float float float))
    (sv-720 float)
    (sv-736 vector)
    (sv-752 vector)
    (sv-768 (function float float float))
    (sv-784 float)
    )
  (with-pp
    (rlet ((acc :class vf)
           (vf0 :class vf)
           (vf1 :class vf)
           (vf2 :class vf)
           (vf3 :class vf)
           (vf4 :class vf)
           )
      (init-vf0-vector)
      (let ((gp-0 *search-info*))
        (set! (-> gp-0 match) #f)
        (vector-copy! (-> gp-0 point) arg1)
        (set! (-> gp-0 radius) arg2)
        (set! (-> gp-0 best) 1000000000000000000000.0)
        (set! (-> gp-0 rating) (search-info-flag))
        (set! (-> gp-0 require) arg4)
        (set! (-> gp-0 mask) arg3)
        (vector-copy! (-> gp-0 rot-base) arg5)
        (if arg6
            (vector-copy! (-> gp-0 back-point) arg6)
            (vector-copy! (-> gp-0 back-point) (-> gp-0 rot-base))
            )
        (set! arg7 (cond
                     ((= arg7 65536.0)
                      -2.0
                      )
                     (else
                       (empty)
                       arg7
                       )
                     )
              )
        (set! (-> gp-0 rot-range) arg7)
        (let ((s4-0 (-> arg0 length)))
          (while (begin (label cfg-104) (nonzero? s4-0))
            (+! s4-0 -1)
            (let* ((s0-0 (-> arg0 s4-0))
                   (s3-0 (as-type (-> s0-0 process) process-focusable))
                   )
              (when s3-0
                (let ((s1-0 (get-search-info-flag (the-as process-focusable s3-0))))
                  (when (and (and s3-0 (not (logtest? (-> (the-as process-focusable s3-0) focus-status) (focus-status disable dead))))
                             (logtest? s1-0 (search-info-flag crate guard attackable enemy attackable-priority high-priority))
                             )
                    (let* ((s2-1 (get-trans (the-as process-focusable s3-0) 3))
                           (f30-0 (- (vector-vector-distance (-> gp-0 point) s2-1) (-> s2-1 w)))
                           )
                      (when (#if PC_PORT  (and (nonzero? (-> s0-0 root-prim prim-core collide-as)) (or (not (-> *pc-settings* fix-projectile-focus)) (logtest? (collide-action solid) (-> s0-0 root-prim prim-core action))))
                                          (nonzero? (-> s0-0 root-prim prim-core collide-as)))
                        (let ((s0-1 (search-info-flag)))
                          0.0
                          (set! sv-736 (vector-normalize! (vector-! (new 'stack-no-clear 'vector) s2-1 (-> gp-0 point)) 1.0))
                          (let ((f28-0 (cond
                                         ((logtest? (-> gp-0 mask) (search-info-flag prefer-xz))
                                          (set! sv-704 deg-diff)
                                          (set! sv-720 (vector-y-angle (-> gp-0 rot-base)))
                                          (let ((a1-6 (vector-y-angle sv-736)))
                                            (fabs (sv-704 sv-720 a1-6))
                                            )
                                          )
                                         ((logtest? (-> gp-0 mask) (search-info-flag prefer-angle))
                                          (- -9999.0 (vector-dot sv-736 (-> gp-0 rot-base)))
                                          )
                                         ((logtest? (search-info-flag prefer-center) (-> gp-0 mask))
                                          (* f30-0 (fmax 0.01 (- 1.0 (vector-dot sv-736 (-> gp-0 rot-base)))))
                                          )
                                         (else
                                           f30-0
                                           )
                                         )
                                       )
                                )
                            (cond
                              ((logtest? (-> gp-0 mask) (search-info-flag cull-angle-simple))
                               (let* ((v1-41 (-> gp-0 rot-base))
                                      ; (f0-11 (-> sv-736 x))
                                      ; (f1-6 (-> sv-736 y))
                                      ; (f2-2 (-> sv-736 z))
                                      ; (f3-0 (-> v1-41 x))
                                      ; (f4-0 (-> v1-41 y))
                                      ; (f5-0 (-> v1-41 z))
                                      )
                                 ;; og:preserve-this
                                 ; (.mula.s f0-11 f3-0)
                                 ; (.madda.s f1-6 f4-0)
                                 ; (.madd.s f0-12 f2-2 f5-0)
                                 (set! f0-12 (vector-dot sv-736 v1-41))
                                 )
                               (if (< f0-12 (cos (-> gp-0 rot-range)))
                                   (return (the-as process-focusable #f))
                                   )
                               )
                              ((logtest? (-> gp-0 mask) (search-info-flag cull-angle))
                               (cond
                                 ((< (-> gp-0 rot-range) 14563.556)
                                  (let ((f26-1 (vector-dot (vector-! (new 'stack-no-clear 'vector) s2-1 (-> gp-0 point)) (-> gp-0 rot-base))))
                                    (set! sv-752 (new 'stack-no-clear 'vector))
                                    (let ((v1-53 (-> gp-0 point))
                                          (a0-27 (-> gp-0 rot-base))
                                          (f0-17 f26-1)
                                          )
                                      (.lvf vf2 (&-> a0-27 quad))
                                      (.lvf vf1 (&-> v1-53 quad))
                                      (let ((v1-54 f0-17))
                                        (.mov vf3 v1-54)
                                        )
                                      )
                                    (.add.x.vf.w vf4 vf0 vf0)
                                    (.mul.x.vf acc vf2 vf3)
                                    (.add.mul.w.vf.xyz vf4 vf1 vf0 acc)
                                    (.svf (&-> sv-752 quad) vf4)
                                    (let ((f26-2 (* f26-1 (tan (-> gp-0 rot-range))))
                                          (t9-10 vector-vector-distance)
                                          (a1-8 s2-1)
                                          )
                                      (if (< f26-2 (- (t9-10 sv-752 a1-8) (-> s2-1 w)))
                                          (goto cfg-104)
                                          )
                                      )
                                    )
                                  )
                                 (else
                                   (if (< (vector-dot sv-736 (-> gp-0 rot-base)) (cos (-> gp-0 rot-range)))
                                       (return (the-as process-focusable #f))
                                       )
                                   )
                                 )
                               )
                              ((logtest? (-> gp-0 mask) (search-info-flag cull-xz))
                               (let ((f26-4 (-> gp-0 rot-range)))
                                 (set! sv-768 deg-diff)
                                 (set! sv-784 (vector-y-angle (-> gp-0 rot-base)))
                                 (let ((a1-9 (vector-y-angle sv-736)))
                                   (if (< f26-4 (fabs (sv-768 sv-784 a1-9)))
                                       (goto cfg-104)
                                       )
                                   )
                                 )
                               )
                              )
                            (when (logtest? (-> gp-0 mask) (search-info-flag back-point))
                              (if (< (vector-dot
                                       (vector-normalize! (vector-! (new 'stack-no-clear 'vector) s2-1 (-> gp-0 back-point)) 1.0)
                                       (-> gp-0 rot-base)
                                       )
                                     0.0
                                     )
                                  (goto cfg-104)
                                  )
                              )
                            (when (logtest? (-> gp-0 mask) (search-info-flag combo))
                              (if (not (send-event s3-0 'combo))
                                  (goto cfg-104)
                                  )
                              )
                            (if (logtest? s1-0 (search-info-flag high-priority))
                                (set! s0-1 (the-as search-info-flag (logior (the-as int s0-1) (search-info-flag high-priority))))
                                )
                            (if (logtest? s1-0 (search-info-flag guard enemy attackable-priority high-priority))
                                (set! s0-1 (the-as search-info-flag (logior (the-as int s0-1) (search-info-flag enemy))))
                                )
                            (if (logtest? s1-0 (search-info-flag attackable))
                                (set! s0-1 (the-as search-info-flag (logior (the-as int s0-1) (search-info-flag attackable))))
                                )
                            (if (logtest? s1-0 (search-info-flag crate))
                                (set! s0-1 (the-as search-info-flag (logior (the-as int s0-1) (search-info-flag crate))))
                                )
                            (if (and (nonzero? (-> s3-0 draw)) (logtest? (-> s3-0 draw status) (draw-control-status on-screen)))
                                (set! s0-1 (the-as search-info-flag (logior (the-as int s0-1) (search-info-flag on-screen))))
                                )
                            (let ((s1-1 (the-as search-info-flag (logand (the-as int s0-1) (-> gp-0 mask)))))
                              (when (and (>= (the-as int s1-1) (the-as int (-> gp-0 rating)))
                                         (or (zero? (-> gp-0 require)) (logtest? (the-as int s1-1) (-> gp-0 require)))
                                         (or (and (logtest? (the-as int s1-1) (search-info-flag high-priority))
                                                  (not (logtest? (-> gp-0 rating) (search-info-flag high-priority)))
                                                  )
                                             (and (logtest? (the-as int s1-1) (search-info-flag high-priority))
                                                  (logtest? (-> gp-0 rating) (search-info-flag high-priority))
                                                  (< f28-0 (-> gp-0 best))
                                                  )
                                             (< f28-0 (-> gp-0 best))
                                             )
                                         (< f30-0 (-> gp-0 radius))
                                         )
                                (when (logtest? (search-info-flag probe) (-> gp-0 mask))
                                  (let ((a1-13 (new 'stack-no-clear 'collide-query)))
                                    (vector-copy! (-> a1-13 start-pos) (-> gp-0 back-point))
                                    (vector-! (-> a1-13 move-dist) s2-1 (-> gp-0 back-point))
                                    (let ((v1-124 a1-13))
                                      (set! (-> v1-124 radius) 1228.8)
                                      (set! (-> v1-124 collide-with) (collide-spec backgnd))
                                      (set! (-> v1-124 ignore-process0) pp)
                                      (set! (-> v1-124 ignore-process1) #f)
                                      (set! (-> v1-124 ignore-pat)
                                            (new 'static 'pat-surface :noentity #x1 :nojak #x1 :probe #x1 :noendlessfall #x1 :board #x1)
                                            )
                                      (set! (-> v1-124 action-mask) (collide-action solid))
                                      )
                                    (let ((f0-34 (fill-and-probe-using-line-sphere *collide-cache* a1-13)))
                                      (if (and (>= f0-34 0.0) (< f0-34 1.0))
                                          (goto cfg-104)
                                          )
                                      )
                                    )
                                  )
                                (set! (-> gp-0 match) (the-as process-focusable s3-0))
                                (set! (-> gp-0 best) f28-0)
                                (set! (-> gp-0 rating) (the-as search-info-flag s1-1))
                                )
                              )
                            )
                          )
                        )
                      )
                    )
                  )
                )
              )
            )
          )
        (-> gp-0 match)
        )
      )
    )
  )
