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

;; name: collide-reaction-target.gc
;; name in dgo: collide-reaction-target
;; dgos: GAME

(define-extern collide-shape-moving-angle-set! (function collide-shape-moving vector vector none))

;; DECOMP BEGINS

(defun poly-find-nearest-edge ((arg0 nav-poly) (arg1 (inline-array vector)) (arg2 vector) (arg3 vector))
  (local-vars (sv-32 vector) (sv-36 number) (sv-40 int) (sv-48 number) (sv-80 vector))
  (set! sv-32 (new 'stack-no-clear 'vector))
  (set! sv-36 4095996000.0)
  (set! sv-40 -1)
  (set! sv-48 0.0)
  (dotimes (s2-0 3)
    (let ((s0-0 (-> arg1 s2-0)))
      (set! sv-80 (-> arg1 (mod (+ s2-0 1) 3)))
      (let ((f30-0 (vector-line-distance-point! arg2 s0-0 sv-80 sv-32))
            (s1-1 (vector-! (new 'stack-no-clear 'vector) sv-80 s0-0))
            )
        0.0
        (vector-normalize! s1-1 1.0)
        (let ((f0-5 (fabs (vector-dot arg3 s1-1))))
          (when (or (< f30-0 (the-as float sv-36)) (and (= f30-0 (the-as float sv-36)) (< (the-as float sv-48) f0-5)))
            (set! sv-36 f30-0)
            (set! sv-40 s2-0)
            (set! sv-48 f0-5)
            )
          )
        )
      )
    )
  (set! (-> arg0 vertex 0 quad) (-> arg1 sv-40 quad))
  (vector-copy! (-> arg0 vertex1) (-> arg1 (mod (+ sv-40 1) 3)))
  arg0
  )

(defun target-collision-low-coverage ((arg0 control-info)
                             (arg1 collide-query)
                             (arg2 vector)
                             (arg3 (pointer cshape-reaction-flags))
                             (arg4 (pointer collide-status))
                             (arg5 (pointer symbol))
                             )
  (local-vars
    (contact-normal vector)
    (sv-20 (pointer cshape-reaction-flags))
    (sv-24 (pointer collide-status))
    (sv-28 (pointer symbol))
    (sv-32 cshape-reaction-flags)
    (sv-40 collide-status)
    (sv-48 symbol)
    (tangent vector)
    (overhang-nrm vector)
    )
  (set! contact-normal arg2)
  (set! sv-20 arg3)
  (set! sv-24 arg4)
  (set! sv-28 arg5)
  (set! sv-32 (-> arg3 0))
  (set! sv-40 (-> arg4 0))
  (set! sv-48 (-> arg5 0))
  (set! tangent (-> arg0 low-coverage-tangent))
  (set! overhang-nrm
        (vector-cross! (-> arg0 low-coverage-overhang-plane-normal) (-> arg0 poly-normal) contact-normal)
        )
  (vector-normalize! overhang-nrm 1.0)
  (if (>= (fabs (vector-dot (-> arg0 dynam gravity-normal) overhang-nrm)) 0.866)
      (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf09)))
      )
  (vector-cross! tangent contact-normal overhang-nrm)
  (if (< 0.0 (vector-dot (-> arg0 dynam gravity-normal) tangent))
      (vector-negate! tangent tangent)
      )
  (vector-normalize! tangent 1.0)
  (let ((s4-0 (new 'stack-no-clear 'collide-query)))
    (let ((s3-0 (vector-flatten! (-> arg0 low-coverage-tangent-xz) tangent (-> arg0 dynam gravity-normal))))
      (let ((v1-19 s4-0))
        (set! (-> v1-19 radius) 409.6)
        (set! (-> v1-19 collide-with) (-> arg1 best-my-prim prim-core collide-with))
        (set! (-> v1-19 ignore-process0) #f)
        (set! (-> v1-19 ignore-process1) #f)
        (set! (-> v1-19 ignore-pat) (-> arg0 pat-ignore-mask))
        (set! (-> v1-19 action-mask) (collide-action solid))
        )
      (vector-normalize! s3-0 1.0)
      (if (< (vector-dot s3-0 contact-normal) 0.0)
          (vector-negate! s3-0 s3-0)
          )
      (set! (-> arg0 low-coverage-slope-to-next1) 4095996000.0)
      (set! (-> arg0 low-coverage-dist-to-next2) 4095996000.0)
      (vector+float*! (-> s4-0 start-pos) (-> arg1 best-other-tri intersect) s3-0 2867.2)
      (vector-normalize-copy! (-> s4-0 move-dist) (-> arg0 dynam gravity-normal) -20480.0)
      (vector+float*! (-> s4-0 move-dist) (-> s4-0 move-dist) s3-0 4096.0)
      (when (>= (probe-using-line-sphere *collide-cache* s4-0) 0.0)
        (set! (-> arg0 low-coverage-slope-to-next1)
              (vector-dot
                (-> arg0 dynam gravity-normal)
                (vector-! (new 'stack-no-clear 'vector) (-> arg1 best-other-tri intersect) (-> s4-0 best-other-tri intersect))
                )
              )
        (set! (-> arg0 low-coverage-pat-next1) (-> s4-0 best-other-tri pat))
        (vector-copy! (-> arg0 low-coverage-norm-of-next1) (-> s4-0 best-other-tri normal))
        0
        )
      (vector+float*! (-> s4-0 start-pos) (-> arg1 best-other-tri intersect) (-> arg0 dynam gravity-normal) 819.2)
      (vector-normalize-copy! (-> s4-0 move-dist) (-> arg0 dynam gravity-normal) -20480.0)
      (vector+float*! (-> s4-0 start-pos) (-> s4-0 start-pos) s3-0 -819.2)
      (vector+float*! (-> s4-0 move-dist) (-> s4-0 move-dist) s3-0 -4096.0)
      )
    (when (>= (probe-using-line-sphere *collide-cache* s4-0) 0.0)
      (set! (-> arg0 low-coverage-dist-to-next2)
            (vector-vector-distance (-> s4-0 start-pos) (-> s4-0 best-other-tri intersect))
            )
      (set! (-> arg0 low-coverage-pat-next2) (-> s4-0 best-other-tri pat))
      (vector-copy! (-> arg0 low-coverage-norm-of-next2) (-> s4-0 best-other-tri normal))
      0
      )
    )
  (when (and (not (logtest? sv-32 (cshape-reaction-flags csrf09)))
             (and (or (< (* 1.25 (-> arg1 best-my-prim local-sphere w)) (-> arg0 low-coverage-slope-to-next1))
                      (= (-> arg0 low-coverage-pat-next1 mode) (pat-mode wall))
                      )
                  (and (< (-> arg0 low-coverage-dist-to-next2) (* 2.0 (-> arg1 best-my-prim local-sphere w)))
                       (or (= (-> arg0 low-coverage-pat-next2 mode) (pat-mode ground))
                           (= (-> arg0 low-coverage-pat-next2 mode) (pat-mode halfpipe))
                           )
                       )
                  )
             )
    (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf07)))
    (set-time! (-> arg0 time-of-last-lc-touch-edge))
    (set! sv-40 (logior sv-40 (collide-status touch-edge)))
    (set-time! (-> arg0 time-of-last-lc))
    (let ((f30-0 (vector-dot tangent (-> arg0 turn-to-target)))
          (f0-27 (if (logtest? sv-32 (cshape-reaction-flags csrf01))
                     (cos (- 16384.0 (acos (-> arg0 coverage))))
                     (-> arg0 coverage)
                     )
                 )
          (f1-11 (vector-dot
                   (-> arg0 dynam gravity-normal)
                   (vector-! (new 'stack-no-clear 'vector) (-> arg0 trans) (-> arg1 best-other-tri intersect))
                   )
                 )
          )
      (if (or (not (logtest? sv-32 (cshape-reaction-flags csrf05))) (< 0.5 f0-27))
          (set! sv-48 (the-as symbol #f))
          )
      (when (and (or (and (< f0-27 0.95) (>= f30-0 0.0))
                     (and (logtest? sv-32 (cshape-reaction-flags csrf05)) (< f0-27 0.3))
                     (< f1-11 (/ (-> arg1 best-my-prim local-sphere w) -4))
                     )
                 (>= (vector-dot tangent contact-normal) -0.000001)
                 )
        (set! (-> arg0 surf) *edge-surface*)
        (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf10)))
        (set! sv-48 (the-as symbol #f))
        (when (logtest? sv-32 (cshape-reaction-flags csrf05))
          (set! sv-48 #t)
          (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf12)))
          )
        )
      )
    )
  (if (< (-> arg0 surface-angle) 0.0)
      (set! sv-48 #t)
      )
  (when sv-48
    (cond
      ((and (or (or (= (-> arg0 poly-pat mode) (pat-mode ground)) (= (-> arg0 poly-pat mode) (pat-mode halfpipe)))
                (and (logtest? sv-32 (cshape-reaction-flags csrf03))
                     (>= (* 1.25 (-> arg1 best-my-prim local-sphere w)) (-> arg0 low-coverage-slope-to-next1))
                     (or (= (-> arg0 low-coverage-pat-next1 mode) (pat-mode ground))
                         (= (-> arg0 low-coverage-pat-next1 mode) (pat-mode halfpipe))
                         )
                     (< 0.3 (fabs (-> arg0 surface-angle)))
                     )
                )
            (not (logtest? sv-32 (cshape-reaction-flags csrf07)))
            )
       (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf06)))
       (set! sv-40 (logior sv-40 (collide-status touch-edge)))
       (set-time! (-> arg0 time-of-last-lc))
       (set! sv-48 (the-as symbol #f))
       )
      (#t
        (set! sv-32 (logior sv-32 (cshape-reaction-flags csrf06)))
        (set! sv-40 (logior sv-40 (collide-status touch-edge)))
        (set-time! (-> arg0 time-of-last-lc))
        )
      )
    )
  (set! (-> sv-24 0) sv-40)
  (set! (-> sv-20 0) sv-32)
  (set! (-> sv-28 0) sv-48)
  0
  (none)
  )

(defbehavior target-collision-reaction target ((arg0 control-info) (arg1 collide-query) (arg2 vector) (arg3 vector))
  (local-vars
    (sv-80 vector)
    (sv-84 vector)
    (sv-88 matrix)
    (sv-96 collide-status)
    (sv-104 cshape-reaction-flags)
    (sv-240 symbol)
    )
  (set! sv-80 (new-stack-vector0))
  (set! sv-84 (new-stack-vector0))
  (let ((v1-2 (new 'stack-no-clear 'matrix)))
    (dotimes (a0-1 2)
      (set! (-> v1-2 quad a0-1) (the-as uint128 0))
      )
    (set! sv-88 v1-2)
    )
  (set! sv-96 (collide-status))
  (set! sv-104 (cshape-reaction-flags))
  (vector-copy! (-> sv-88 rvec) arg3)
  (vector-copy! (-> sv-88 uvec) arg3)
  (let ((a1-3 (new 'stack-no-clear 'vector)))
    (vector-float*! a1-3 (-> arg1 move-dist) (-> arg1 best-dist))
    (move-by-vector! arg0 a1-3)
    )
  (set! sv-104 (logior sv-104 (react-to-pat! arg0 (-> arg1 best-other-tri pat))))
  (if (= (-> arg0 poly-pat mode) (pat-mode wall))
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf00)))
      )
  (if (logtest? (-> arg0 mod-surface flags) (surface-flag air))
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf05)))
      )
  (let ((v1-24 (new 'stack-no-clear 'vector)))
    (vector-copy! v1-24 (-> arg1 best-my-prim prim-core world-sphere))
    (vector-! sv-80 v1-24 (-> arg1 best-other-tri intersect))
    )
  (vector-normalize! sv-80 1.0)
  (set! (-> arg0 coverage) (vector-dot sv-80 (-> arg1 best-other-tri normal)))
  (when (< (-> arg0 coverage) 0.0)
    (set! (-> arg0 coverage) 0.0)
    (vector-flatten! sv-80 sv-80 (-> arg1 best-other-tri normal))
    (vector-normalize! sv-80 1.0)
    )
  (if (< (-> arg0 coverage) 0.9999)
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf03 csrf04)))
      )
  (let ((v1-35 (-> sv-80 quad)))
    (set! (-> sv-84 quad) v1-35)
    )
  (if (= (-> arg1 best-dist) 0.0)
      (move-by-vector! arg0 (vector-normalize-copy! (new-stack-vector0) sv-84 3.0))
      )
  (vector-copy! (-> arg0 poly-normal) (-> arg1 best-other-tri normal))
  (collide-shape-moving-angle-set! arg0 sv-84 (-> sv-88 rvec))
  (if (< (-> arg0 poly-angle) -0.2)
      (set! sv-96 (logior sv-96 (collide-status touch-ceiling)))
      )
  (if (< (-> arg0 poly-angle) 0.0)
      (set! sv-96 (logior sv-96 (collide-status touch-ceiling-sticky)))
      )
  (if (!= (-> arg1 best-my-prim prim-id) 6)
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf15)))
      )
  (if (and (= (-> arg0 cur-pat mode) (pat-mode halfpipe)) (< (-> arg0 poly-angle) 0.75))
      (send-event (-> arg0 process) 'slide)
      )
  (set! sv-240 (< (fabs (-> arg0 surface-angle)) (-> *pat-mode-info* (-> arg0 cur-pat mode) wall-angle)))
  (if (and sv-240 (and (= (-> arg0 mod-surface mode) 'dive) (< 0.4 (fabs (-> arg0 surface-angle)))))
      (set! sv-240 (the-as symbol #f))
      )
  (if (and (logtest? sv-104 (cshape-reaction-flags csrf15))
           (logtest? sv-104 (cshape-reaction-flags csrf05))
           (>= (-> arg0 poly-angle) 0.0)
           (not sv-240)
           )
      (set! sv-240 #t)
      )
  (if sv-240
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf01)))
      )
  (if (logtest? sv-104 (cshape-reaction-flags csrf03))
      (target-collision-low-coverage arg0 arg1 sv-84 (& sv-104) (& sv-96) (& sv-240))
      )
  (when (not (logtest? (-> arg0 prev-status) (collide-status on-surface)))
    (vector-copy! (-> arg0 transv-on-last-impact) (-> arg0 transv))
    (set! (-> arg0 ground-impact-vel) (- (vector-dot (-> arg0 transv) (-> arg0 dynam gravity-normal))))
    (set! (-> arg0 normal-impact-vel) (- (vector-dot (-> arg0 transv) sv-84)))
    (set! sv-96 (logior sv-96 (collide-status impact-surface)))
    (if (nonzero? (-> arg0 current-surface impact-hook))
        ((-> arg0 current-surface impact-hook) arg0 (&-> sv-88 rvec x) sv-84)
        )
    (when (not sv-240)
      (let ((f0-21 (- 1.0 (-> arg0 current-surface impact-fric))))
        (when (< f0-21 1.0)
          (let ((v1-113 (new-stack-vector0))
                (f1-13 (vector-dot (-> arg0 dynam gravity-normal) (-> sv-88 rvec)))
                )
            0.0
            (vector-! v1-113 (-> sv-88 rvec) (vector-float*! v1-113 (-> arg0 dynam gravity-normal) f1-13))
            (let* ((f2-2 (vector-length v1-113))
                   (f3-0 f2-2)
                   )
              (if (< f1-13 0.0)
                  (set! f1-13 (* f1-13 f0-21))
                  )
              (vector+!
                (-> sv-88 rvec)
                (vector-float*! (-> sv-88 rvec) (-> arg0 dynam gravity-normal) f1-13)
                (vector-float*! v1-113 v1-113 (/ f2-2 f3-0))
                )
              )
            )
          )
        )
      )
    )
  (set! sv-96 (logior sv-96 (collide-status touch-surface)))
  (cond
    ((-> arg1 best-other-prim)
     (set! sv-96 (logior sv-96 (collide-status touch-actor)))
     (set! (-> arg0 actor-contact-pt quad) (-> arg1 best-other-tri intersect quad))
     (vector-copy! (-> arg0 actor-contact-normal) (-> arg0 poly-normal))
     (set! (-> arg0 actor-contact-handle) (process->handle (-> arg1 best-other-prim cshape process)))
     )
    ((= (-> arg0 poly-pat material) (pat-material waterbottom))
     )
    (else
      (set! sv-96 (logior sv-96 (collide-status touch-background)))
      )
    )
  (cond
    (sv-240
      (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf02)))
      (set! sv-96 (logior sv-96 (collide-status touch-wall)))
      (set! (-> arg0 cur-pat mode) 1)
      (set! (-> arg0 wall-contact-pt quad) (-> arg1 best-other-tri intersect quad))
      (vector-copy! (-> arg0 wall-contact-poly-normal) (-> arg0 poly-normal))
      (vector-copy! (-> arg0 wall-contact-normal) sv-84)
      (set! (-> arg0 wall-contact-pat) (-> arg1 best-other-tri pat))
      (cond
        ((and (focus-test? (the-as process-focusable (-> arg0 process)) mech)
              (logtest? sv-104 (cshape-reaction-flags csrf15))
              (logtest? sv-104 (cshape-reaction-flags csrf05))
              (< 0.0 (vector-dot sv-84 (-> arg0 dynam gravity-normal)))
              (< 0.0 (vector-dot (-> sv-88 rvec) (-> arg0 dynam gravity-normal)))
              )
         (set! (-> arg0 surf) *edge-surface*)
         (set! sv-104 (logior (cshape-reaction-flags csrf16) sv-104))
         (vector-flatten! arg2 (vector-negate! (new 'stack-no-clear 'vector) (-> arg0 dynam gravity-normal)) sv-84)
         (vector-normalize! arg2 (vector-dot arg2 (-> sv-88 rvec)))
         )
        (else
          (vector-reflect-flat! arg2 (-> sv-88 rvec) sv-84)
          )
        )
      (cond
        ((and (or (and (>= (-> arg1 best-my-prim local-sphere w)
                           (vector-dot
                             (-> arg0 ground-poly-normal)
                             (vector-! (new 'stack-no-clear 'vector) (-> arg1 best-other-tri intersect) (-> arg0 ground-touch-point))
                             )
                           )
                       (not (time-elapsed? (-> arg0 list-time-on-ground) (seconds 0.3)))
                       )
                  (logtest? sv-104 (cshape-reaction-flags csrf15))
                  )
              (>= (vector-dot
                    (-> arg0 dynam gravity-normal)
                    (vector-! (new 'stack-no-clear 'vector) (-> arg1 best-other-tri intersect) (-> arg0 ground-touch-point))
                    )
                  0.0
                  )
              (and (< 0.0 (vector-dot (-> arg0 ground-poly-normal) arg2))
                   (not (logtest? sv-104 (cshape-reaction-flags csrf05)))
                   (not (logtest? (state-flags hang) (-> arg0 process state-flags)))
                   )
              )
         (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf08)))
         (set! sv-104 (logclear sv-104 (cshape-reaction-flags csrf06)))
         (let ((s3-4 (vector-cross! (new 'stack-no-clear 'vector) (-> arg0 poly-normal) (-> arg0 ground-poly-normal))))
           (vector-normalize! s3-4 1.0)
           (vector-float*! arg2 s3-4 (vector-dot (-> sv-88 rvec) s3-4))
           )
         (vector+! arg2 arg2 (-> arg0 poly-normal))
         )
        (else
          )
        )
      )
    (else
      (set! sv-96 (logior sv-96 (collide-status on-surface)))
      (set! (-> arg0 cur-pat mode) 0)
      (if (= (-> arg1 best-my-prim prim-id) 6)
          (vector-copy! (-> arg0 local-normal) sv-84)
          )
      (cond
        ((and (focus-test? (the-as process-focusable (-> arg0 process)) board)
              (not (focus-test? (the-as process-focusable (-> arg0 process)) halfpipe))
              (!= (-> arg0 cur-pat mode) 3)
              (!= (-> arg0 ground-pat mode) 3)
              )
         (vector-reflect-flat-gravity! arg2 (-> sv-88 rvec) sv-84 (-> arg0 dynam gravity-normal))
         (vector+! arg2 arg2 sv-84)
         )
        ((and (or (>= (fabs (-> arg0 trans x)) 16777216.0) (>= (fabs (-> arg0 trans z)) 16777216.0))
              (and (not (focus-test? (the-as process-focusable (-> arg0 process)) board)) (< 0.7 (-> arg0 surface-angle)))
              )
         (vector-reflect-flat-above! arg2 (-> sv-88 rvec) sv-84)
         )
        (else
          (vector-reflect-flat! arg2 (-> sv-88 rvec) sv-84)
          (vector+! arg2 arg2 sv-84)
          )
        )
      (let ((f0-37 (-> arg0 current-surface slope-change-preserve)))
        (when (< 0.0 f0-37)
          (let ((v1-234 (new-stack-vector0)))
            (let ((f1-28 (vector-dot (-> arg0 pre-collide-local-normal) (-> sv-88 rvec))))
              0.0
              (vector-! v1-234 (-> sv-88 rvec) (vector-float*! v1-234 (-> arg0 pre-collide-local-normal) f1-28))
              )
            (let ((f1-29 (vector-length v1-234)))
              f1-29
              (let ((f1-30 f1-29)
                    (f2-5 (vector-length arg2))
                    )
                (if (and (< 409.6 (fabs (- f2-5 f1-30))) (< f2-5 f1-30))
                    (vector-normalize! arg2 (lerp-scale f2-5 f1-30 f0-37 0.0 1.0))
                    )
                )
              )
            )
          )
        )
      (set! (-> arg0 ground-touch-point w) 0.0)
      (when (not (logtest? sv-104 (cshape-reaction-flags csrf00 csrf01 csrf02 csrf03 csrf14)))
        (set! sv-96 (logior sv-96 (collide-status on-ground)))
        (vector-copy! (-> arg0 ground-poly-normal) (-> arg0 poly-normal))
        (vector-copy! (-> arg0 ground-contact-normal) sv-84)
        (set! (-> arg0 ground-local-norm-dot-grav) (vector-dot sv-84 (-> arg0 dynam gravity-normal)))
        (set-time! (-> arg0 list-time-on-ground))
        (set! (-> arg0 ground-pat) (-> arg0 poly-pat))
        (set! (-> arg0 ground-touch-point quad) (-> arg1 best-other-tri intersect quad))
        (set! (-> arg0 ground-contact-sphere-center quad) (-> arg1 best-my-prim prim-core world-sphere quad))
        (set! sv-104 (logior sv-104 (cshape-reaction-flags csrf11)))
        (if (= (-> arg0 poly-pat material) (pat-material waterbottom))
            (set! sv-96 (logior sv-96 (collide-status on-water)))
            )
        )
      )
    )
  (logior! (-> arg0 status) sv-96)
  (set! (-> arg0 reaction-flag) sv-104)
  (when *debug-segment*
    (+! (-> arg0 history-idx) 1)
    (let ((v1-271 (clear-record-tags! *history* (history-channel surface-normal) (-> arg0 history-idx) (the-as uint 2)))
          )
      (vector-copy! (-> v1-271 origin) (-> arg1 best-other-tri intersect))
      (vector-copy! (-> v1-271 vector) (-> arg0 surface-normal))
      )
    (let ((v1-274 (clear-record-tags! *history* (history-channel local-normal) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-274 origin) (-> arg1 best-other-tri intersect))
      (vector-copy! (-> v1-274 vector) (-> arg0 local-normal))
      )
    (vector-copy!
      (-> (clear-record-tags! *history* (history-channel intersect) (-> arg0 history-idx) (the-as uint 2)) vector)
      (-> arg1 best-other-tri intersect)
      )
    (let ((v1-280 (clear-record-tags! *history* (history-channel transv-out) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-280 origin) (-> arg1 best-other-tri intersect))
      (vector-copy! (-> v1-280 vector) arg2)
      )
    (let ((v1-283 (clear-record-tags! *history* (history-channel transv-in) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-283 origin) (-> arg1 best-other-tri intersect))
      (vector-copy! (-> v1-283 vector) (-> sv-88 uvec))
      )
    (let ((v1-286 (clear-record-tags! *history* (history-channel transv) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-286 origin) (-> arg0 trans))
      (vector-copy! (-> v1-286 vector) (-> arg0 transv))
      )
    (vector-copy!
      (-> (clear-record-tags! *history* (history-channel trans) (-> arg0 history-idx) (the-as uint 2)) vector)
      (-> arg0 trans)
      )
    (let ((v1-292 (clear-record-tags! *history* (history-channel pat) (-> arg0 history-idx) (the-as uint 2))))
      (set! (-> v1-292 vector x) (the-as float (-> arg0 cur-pat)))
      )
    (let ((v1-294 (clear-record-tags! *history* (history-channel collide-status) (-> arg0 history-idx) (the-as uint 2)))
          )
      (set! (-> v1-294 collide-status) (-> arg0 status))
      (set! (-> v1-294 vector z) (the-as float (-> arg0 reaction-flag)))
      )
    )
  sv-96
  )

(defun target-collision-no-reaction ((arg0 control-info) (arg1 collide-query) (arg2 vector) (arg3 vector))
  (if (= (-> arg0 mod-surface mode) 'air)
      (logior! (-> arg0 reaction-flag) (cshape-reaction-flags csrf05))
      )
  (when *debug-segment*
    (+! (-> arg0 history-idx) 1)
    (let ((v1-9 (clear-record-tags! *history* (history-channel transv-out) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-9 origin) (-> arg0 trans))
      (vector-copy! (-> v1-9 vector) arg2)
      )
    (let ((v1-12 (clear-record-tags! *history* (history-channel transv-in) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-12 origin) (-> arg0 trans))
      (vector-copy! (-> v1-12 vector) arg3)
      )
    (let ((v1-15 (clear-record-tags! *history* (history-channel transv) (-> arg0 history-idx) (the-as uint 2))))
      (vector-copy! (-> v1-15 origin) (-> arg0 trans))
      (vector-copy! (-> v1-15 vector) (-> arg0 transv))
      )
    (vector-copy!
      (-> (clear-record-tags! *history* (history-channel trans) (-> arg0 history-idx) (the-as uint 2)) vector)
      (-> arg0 trans)
      )
    (let ((v1-21 (clear-record-tags! *history* (history-channel collide-status) (-> arg0 history-idx) (the-as uint 2)))
          )
      (set! (-> v1-21 collide-status) (logior (-> arg0 status) (collide-status no-touch)))
      (set! (-> v1-21 vector z) (the-as float (-> arg0 reaction-flag)))
      )
    )
  0
  (none)
  )

(define *collide-edge-board-spec* (new 'static 'collide-edge-spec
                                    :split-dists (new 'static 'array float 2 1024.0 1433.6)
                                    :outward-offset (new 'static 'vector :x 708.608 :y 13312.0 :w 1.0)
                                    :flags (collide-edge-spec-flags find-adjacent-edge send-event)
                                    :ignore-pat (new 'static 'pat-surface :noentity #x1 :nogrind #x1 :probe #x1)
                                    :max-dist-sqrd-to-outward-pt 67108864.0
                                    :max-dir-cosa-delta -1.0
                                    :max-dir-cosa-player -2.0
                                    :touching-segment #f
                                    :local-cache-fill-box (new 'static 'bounding-box
                                      :min (new 'static 'vector :x -12288.0 :y -11059.2 :z -12288.0 :w 1.0)
                                      :max (new 'static 'vector :x 12288.0 :y 16384.0 :z 12288.0 :w 1.0)
                                      )
                                    :local-within-reach-box (new 'static 'bounding-box
                                      :min (new 'static 'vector :x -11878.4 :y -2867.2 :z -11878.4 :w 1.0)
                                      :max (new 'static 'vector :x 11878.4 :y 2867.2 :z 11878.4 :w 1.0)
                                      )
                                    :local-player-spheres (new 'static 'inline-array sphere 12
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x 2539.52 :y -819.2 :r 1433.6)
                                      (new 'static 'sphere :x -737.28 :y 4096.0 :r 3072.0)
                                      (new 'static 'sphere :x -737.28 :y 6553.6 :r 3072.0)
                                      (new 'static 'sphere :x -737.28 :y 9420.8 :r 3072.0)
                                      (new 'static 'sphere :x 1720.32 :y 3686.4 :r 2949.12)
                                      (new 'static 'sphere :x 1720.32 :y 5734.4 :r 2949.12)
                                      (new 'static 'sphere :x 1720.32 :y 8601.6 :r 2949.12)
                                      )
                                    )
        )

(defbehavior rail-surface-touch target ()
  (when (time-elapsed? (-> self control time-of-last-surface-change) (seconds 0.2))
    (logclear! (-> *collide-edge-board-spec* flags) (collide-edge-spec-flags one send-event))
    (set! (-> *collide-edge-board-spec* touching-segment) #f)
    (target-method-28 *target* *collide-cache* *collide-edge-board-spec*)
    (logior! (-> *collide-edge-board-spec* flags) (collide-edge-spec-flags send-event))
    (let ((gp-0 *edge-grab-info*))
      (when (-> gp-0 found-edge?)
        (vector-normalize!
          (vector-! (-> self control edge-grab-edge-dir) (-> gp-0 world-vertex 0) (-> gp-0 world-vertex 1))
          1.0
          )
        (vector-copy! (-> self control edge-grab-across-edge-dir) (-> gp-0 hanging-matrix rvec))
        (send-event
          self
          'push-transv
          (vector-float*!
            (new 'stack-no-clear 'vector)
            (-> self control edge-grab-across-edge-dir)
            (seek (vector-length (-> self control additional-decaying-velocity)) 40960.0 (* 81920.0 (seconds-per-frame)))
            )
          (seconds 0.3)
          )
        )
      )
    )
  0
  (none)
  )

(defbehavior gravel-surface-touch target ()
  0
  (none)
  )

;; WARN: Return type mismatch float vs none.
(defbehavior mushroom-surface-touch target ()
  (lerp-scale 0.0 1.0 (-> self control surface-angle) 0.8 0.0)
  (if (< (-> self control surface-angle) 0.8)
      (seek! (-> *mushroom-surface* slip-factor) 0.0 (* 0.5 (seconds-per-frame)))
      (seek! (-> *mushroom-surface* slip-factor) 1.0 (* 2.5 (seconds-per-frame)))
      )
  (none)
  )
