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

;; name: rope-system.gc
;; name in dgo: rope-system
;; dgos: DESRESC

;; DECOMP BEGINS

;; WARN: Return type mismatch symbol vs object.
(defmethod init! ((this rope-system) (arg0 int))
  (set! (-> this accum-force quad) (the-as uint128 0))
  (logior! (-> this flags) (rope-system-flag rs6))
  (vector-float*! (-> this gravity-dir) *up-vector* -1.0)
  (set! (-> this gravity) 196608.0)
  (set! (-> this drag) 0.3)
  (set! (-> this knots) (new 'process 'rope-knot-array arg0))
  (set! (-> this knot-constraints) (new 'process 'rope-knot-constraint-array (+ arg0 -1)))
  (set! (-> this knots length) (-> this knots allocated-length))
  (set! (-> this knot-constraints length) (-> this knot-constraints allocated-length))
  (dotimes (v1-13 (-> this knots length))
    (set! (-> this knots data v1-13 pos quad) (the-as uint128 0))
    (set! (-> (the-as vector (+ (the-as uint (-> this knots data 0 prev-pos)) (* v1-13 64))) quad)
          (the-as uint128 0)
          )
    (set! (-> this knots data v1-13 mass) 1.0)
    (set! (-> (the-as vector (+ (the-as uint (-> this knots data 0 force)) (* v1-13 64))) quad)
          (the-as uint128 0)
          )
    )
  (dotimes (v1-16 (-> this knot-constraints length))
    (set! (-> this knot-constraints data v1-16 max-angle) 3640.889)
    (set! (-> this knot-constraints data v1-16 rest-seek-rate) (* 1820.4445 (seconds-per-frame)))
    )
  #f
  )

(defmethod rope-system-method-20 ((this rope-system))
  (dotimes (v1-0 (-> this knots length))
    (set! (-> (the-as vector (+ (the-as uint (-> this knots data 0 prev-pos)) (* v1-0 64))) quad)
          (-> this knots data v1-0 pos quad)
          )
    (set! (-> (the-as vector (+ (the-as uint (-> this knots data 0 force)) (* v1-0 64))) quad) (the-as uint128 0))
    )
  #f
  )

;; WARN: Return type mismatch rope-knot-constraint-array vs rope-system.
(defmethod relocate ((this rope-system) (offset int))
  (if (nonzero? (-> this knots))
      (&+! (-> this knots) offset)
      )
  (the-as rope-system (when (nonzero? (-> this knot-constraints))
                        (let ((v0-0 (&+ (-> this knot-constraints) offset)))
                          (set! (-> this knot-constraints) v0-0)
                          v0-0
                          )
                        )
          )
  )

(defmethod rope-system-method-11 ((this rope-system))
  (if (logtest? (-> this flags) (rope-system-flag rs1))
      (vector-float*! (-> this accum-force) (-> this gravity-dir) (-> this gravity))
      )
  )

(defmethod rope-system-method-12 ((this rope-system) (arg0 float))
  (let ((f0-1 (* arg0 arg0))
        (f1-1 (-> this drag))
        )
    (dotimes (v1-0 (-> this knots length))
      (let ((a2-1 (-> this knots data v1-0))
            (a1-3 (new 'stack-no-clear 'vector))
            )
        (let ((a3-0 (new 'stack-no-clear 'vector)))
          (vector-copy! a3-0 (-> this accum-force))
          (vector+! a3-0 a3-0 (-> a2-1 force))
          (vector-float*! a1-3 (-> a2-1 pos) (- 2.0 f1-1))
          (vector-! a1-3 a1-3 (vector-float*! (new 'stack-no-clear 'vector) (-> a2-1 prev-pos) (- 1.0 f1-1)))
          (vector+float*! a1-3 a1-3 a3-0 f0-1)
          )
        (vector-copy! (-> a2-1 prev-pos) (-> a2-1 pos))
        (vector-copy! (-> a2-1 pos) a1-3)
        )
      )
    )
  0
  (none)
  )

(defmethod rope-system-method-17 ((this rope-system))
  (local-vars (f28-0 float))
  (dotimes (s5-0 (+ (-> this knots length) -1))
    (let* ((s1-0 (-> this knot-constraints data s5-0))
           (s4-0 (-> this knots data s5-0))
           (s3-0 (-> this knots data (+ s5-0 1)))
           (s2-1 (vector-! (new 'stack-no-clear 'vector) (-> s3-0 pos) (-> s4-0 pos)))
           )
      0.0
      (vector-normalize-ret-len! s2-1 1.0)
      (let ((f30-0 (-> this knot-constraints data s5-0 length)))
        (vector-normalize! (-> s1-0 dir) 1.0)
        (let ((f0-3 (vector-dot s2-1 (-> s1-0 dir))))
          (when (< f0-3 0.999)
            (let ((s0-0 (new 'stack-no-clear 'vector)))
              0.0
              (let ((f0-4 (acos f0-3)))
                (set! f28-0 (cond
                              ((< (-> this knot-constraints data s5-0 max-angle) (fabs f0-4))
                               (let ((f1-3 (-> this knot-constraints data s5-0 max-angle)))
                                 (if (< f0-4 0.0)
                                     (set! f1-3 (* -1.0 f1-3))
                                     )
                                 (set! f28-0 (- f0-4 f1-3))
                                 )
                               (set-time! (-> this knot-constraints data s5-0 constrained-angle-time))
                               f28-0
                               )
                              (else
                                (seek 0.0 f0-4 (* (-> this knot-constraints data s5-0 rest-seek-rate) (seconds-per-frame)))
                                )
                              )
                      )
                )
              (vector-cross! s0-0 s2-1 (-> s1-0 dir))
              (vector-normalize! s0-0 1.0)
              (vector-rotate-around-axis! s2-1 s2-1 f28-0 s0-0)
              )
            (vector-normalize! s2-1 1.0)
            (if (< s5-0 (+ (-> this knot-constraints length) -1))
                (set! (-> this knot-constraints data (+ s5-0 1) dir quad) (-> s2-1 quad))
                )
            (vector+float*! (-> s3-0 pos) (-> s4-0 pos) s2-1 f30-0)
            )
          )
        )
      )
    )
  #f
  )

(defmethod rope-system-method-15 ((this rope-system))
  (local-vars
    (f24-0 float)
    (sv-144 symbol)
    (sv-160 int)
    (sv-176 (function _varargs_ object))
    (sv-192 symbol)
    (sv-208 int)
    (sv-224 (function _varargs_ object))
    (sv-240 vector)
    (sv-256 vector)
    (sv-272 symbol)
    (sv-288 int)
    (sv-304 (function _varargs_ object))
    (sv-320 symbol)
    (sv-336 int)
    (sv-352 (function _varargs_ object))
    (sv-368 vector)
    (sv-384 int)
    (sv-400 (function _varargs_ object))
    (sv-416 int)
    (sv-432 (function _varargs_ object))
    )
  (rlet ((vf0 :class vf)
         (vf1 :class vf)
         (vf2 :class vf)
         )
    (init-vf0-vector)
    (dotimes (s5-0 (+ (-> this knots length) -1))
      (let* ((s1-0 (-> this knot-constraints data s5-0))
             (s3-0 (-> this knots data s5-0))
             (s2-0 (-> this knots data (+ s5-0 1)))
             (s4-1 (vector-! (new 'stack-no-clear 'vector) (-> s2-0 pos) (-> s3-0 pos)))
             )
        0.0
        (vector-normalize-ret-len! s4-1 1.0)
        (let ((f30-0 (-> this knot-constraints data s5-0 length)))
          (vector-normalize! (-> s1-0 dir) 1.0)
          (let ((f0-3 (vector-dot s4-1 (-> s1-0 dir))))
            (when (< f0-3 0.999)
              (let ((s0-0 (new 'stack-no-clear 'vector)))
                0.0
                (let ((f0-4 (acos f0-3)))
                  (set! f24-0 (cond
                                ((< (-> this knot-constraints data s5-0 max-angle) (fabs f0-4))
                                 (let ((f1-3 (-> this knot-constraints data s5-0 max-angle)))
                                   (if (< f0-4 0.0)
                                       (set! f1-3 (* -1.0 f1-3))
                                       )
                                   (set! f24-0 (- f0-4 f1-3))
                                   )
                                 (set-time! (-> this knot-constraints data s5-0 constrained-angle-time))
                                 f24-0
                                 )
                                (else
                                  (seek 0.0 f0-4 (* (-> this knot-constraints data s5-0 rest-seek-rate) (seconds-per-frame)))
                                  )
                                )
                        )
                  )
                (vector-cross! s0-0 s4-1 (-> s1-0 dir))
                (vector-normalize! s0-0 1.0)
                (let* ((f0-7 (-> this knots data s5-0 mass))
                       (f2-3 (-> this knots data (+ s5-0 1) mass))
                       (f1-5 (+ f0-7 f2-3))
                       (f28-0 (/ f2-3 f1-5))
                       (f26-0 (/ f0-7 f1-5))
                       )
                  (let ((s1-1 (new 'stack-no-clear 'vector)))
                    (vector-copy! s1-1 (-> s3-0 pos))
                    (set! sv-368 (new 'stack-no-clear 'vector))
                    (let ((v1-35 (-> s2-0 pos quad)))
                      (set! (-> sv-368 quad) v1-35)
                      )
                    (set! sv-240 (new 'stack-no-clear 'vector))
                    (let ((v1-38 (-> s4-1 quad)))
                      (set! (-> sv-240 quad) v1-38)
                      )
                    (set! sv-256 (new 'stack-no-clear 'vector))
                    (let ((v1-40 s4-1)
                          (f0-8 -1.0)
                          )
                      (.lvf vf1 (&-> v1-40 quad))
                      (let ((v1-41 f0-8))
                        (.mov vf2 v1-41)
                        )
                      )
                    (.add.x.vf.w vf1 vf0 vf0)
                    (.mul.x.vf.xyz vf1 vf1 vf2)
                    (.svf (&-> sv-256 quad) vf1)
                    (add-debug-line #f (bucket-id debug) s1-1 sv-368 *color-red* #f (the-as rgba -1))
                    (vector-rotate-around-axis! sv-240 sv-240 f24-0 s0-0)
                    (vector-rotate-around-axis! sv-256 sv-256 f24-0 s0-0)
                    (vector-normalize! sv-240 1.0)
                    (vector-normalize! sv-256 1.0)
                    (add-debug-line
                      #f
                      (bucket-id debug)
                      s1-1
                      (vector+float*! (new 'stack-no-clear 'vector) s1-1 sv-240 f30-0)
                      *color-yellow*
                      #f
                      (the-as rgba -1)
                      )
                    (add-debug-line
                      #f
                      (bucket-id debug)
                      sv-368
                      (vector+float*! (new 'stack-no-clear 'vector) sv-368 sv-256 f30-0)
                      *color-white*
                      #f
                      (the-as rgba -1)
                      )
                    (let ((s0-1 add-debug-text-3d))
                      (set! sv-144 (the-as symbol #f))
                      (set! sv-160 583)
                      (set! sv-176 format)
                      (let ((a0-37 (clear *temp-string*))
                            (a1-13 "p0")
                            )
                        (sv-176 a0-37 a1-13)
                        )
                      (let ((a2-8 *temp-string*)
                            (a3-7 s1-1)
                            (t0-5 3)
                            (t1-3 #f)
                            )
                        (s0-1 sv-144 (the-as bucket-id sv-160) a2-8 a3-7 (the-as font-color t0-5) (the-as vector2h t1-3))
                        )
                      )
                    (let ((s0-2 add-debug-text-3d))
                      (set! sv-192 (the-as symbol #f))
                      (set! sv-208 583)
                      (set! sv-224 format)
                      (let ((a0-40 (clear *temp-string*))
                            (a1-15 "p1")
                            )
                        (sv-224 a0-40 a1-15)
                        )
                      (let ((a2-9 *temp-string*)
                            (a3-8 sv-368)
                            (t0-6 3)
                            (t1-4 #f)
                            )
                        (s0-2 sv-192 (the-as bucket-id sv-208) a2-9 a3-8 (the-as font-color t0-6) (the-as vector2h t1-4))
                        )
                      )
                    (let ((v1-49 sv-368)
                          (a0-42 s3-0)
                          (f0-11 f30-0)
                          )
                      (vector+float*! v1-49 (-> a0-42 pos) sv-240 f0-11)
                      )
                    (vector+float*! s1-1 (-> s2-0 pos) sv-256 f30-0)
                    (let ((s0-3 add-debug-text-3d))
                      (set! sv-272 (the-as symbol #f))
                      (set! sv-288 583)
                      (set! sv-304 format)
                      (let ((a0-45 (clear *temp-string*))
                            (a1-19 "p0new")
                            )
                        (sv-304 a0-45 a1-19)
                        )
                      (let ((a2-10 *temp-string*)
                            (a3-9 s1-1)
                            (t0-7 7)
                            (t1-5 #f)
                            )
                        (s0-3 sv-272 (the-as bucket-id sv-288) a2-10 a3-9 (the-as font-color t0-7) (the-as vector2h t1-5))
                        )
                      )
                    (let ((s0-4 add-debug-text-3d))
                      (set! sv-320 (the-as symbol #f))
                      (set! sv-336 583)
                      (set! sv-352 format)
                      (let ((a0-48 (clear *temp-string*))
                            (a1-21 "p1new")
                            )
                        (sv-352 a0-48 a1-21)
                        )
                      (let ((a2-11 *temp-string*)
                            (a3-10 sv-368)
                            (t0-8 7)
                            (t1-6 #f)
                            )
                        (s0-4 sv-320 (the-as bucket-id sv-336) a2-11 a3-10 (the-as font-color t0-8) (the-as vector2h t1-6))
                        )
                      )
                    (vector-lerp! (-> s3-0 pos) (-> s3-0 pos) s1-1 f28-0)
                    )
                  (let ((t9-25 vector-lerp!)
                        (a0-51 s2-0)
                        (a1-24 s2-0)
                        (a3-12 f26-0)
                        )
                    (t9-25 (-> a0-51 pos) (-> a1-24 pos) sv-368 a3-12)
                    )
                  )
                )
              (let ((s1-2 add-debug-text-3d)
                    (s0-5 #f)
                    )
                (set! sv-384 583)
                (set! sv-400 format)
                (let ((a0-53 (clear *temp-string*))
                      (a1-25 "p0f")
                      )
                  (sv-400 a0-53 a1-25)
                  )
                (let ((a2-14 *temp-string*)
                      (a3-13 s3-0)
                      (t0-9 6)
                      (t1-7 #f)
                      )
                  (s1-2 s0-5 (the-as bucket-id sv-384) a2-14 (-> a3-13 pos) (the-as font-color t0-9) (the-as vector2h t1-7))
                  )
                )
              (let ((s1-3 add-debug-text-3d)
                    (s0-6 #f)
                    )
                (set! sv-416 583)
                (set! sv-432 format)
                (let ((a0-56 (clear *temp-string*))
                      (a1-27 "p1f")
                      )
                  (sv-432 a0-56 a1-27)
                  )
                (let ((a2-15 *temp-string*)
                      (a3-14 s2-0)
                      (t0-10 6)
                      (t1-8 #f)
                      )
                  (s1-3 s0-6 (the-as bucket-id sv-416) a2-15 (-> a3-14 pos) (the-as font-color t0-10) (the-as vector2h t1-8))
                  )
                )
              (vector-! s4-1 (-> s2-0 pos) (-> s3-0 pos))
              (vector-normalize! s4-1 1.0)
              (rope-system-method-16 this (-> s3-0 pos) (-> s2-0 pos) s5-0)
              (when (< s5-0 (+ (-> this knot-constraints length) -1))
                (cond
                  ((logtest? (-> this flags) (rope-system-flag rs4))
                   (vector-lerp!
                     (-> this knot-constraints data (+ s5-0 1) dir)
                     (-> this knot-constraints data (+ s5-0 1) dir)
                     s4-1
                     (* (-> this angular-mass-seek-rate) (seconds-per-frame))
                     )
                   (vector-normalize! (-> this knot-constraints data (+ s5-0 1) dir) 1.0)
                   )
                  (else
                    (let ((v1-78 (-> this knot-constraints data (+ s5-0 1))))
                      (set! (-> v1-78 dir quad) (-> s4-1 quad))
                      v1-78
                      )
                    )
                  )
                )
              )
            )
          )
        )
      )
    #f
    )
  )

(defmethod rope-system-method-13 ((this rope-system))
  (dotimes (s5-0 (+ (-> this knots length) -1))
    (let* ((s4-0 (-> this knots data s5-0))
           (s3-0 (-> this knots data (+ s5-0 1)))
           (s2-1 (vector-! (new 'stack-no-clear 'vector) (-> s3-0 pos) (-> s4-0 pos)))
           )
      0.0
      (vector-normalize-ret-len! s2-1 1.0)
      (vector+float*! (-> s3-0 pos) (-> s4-0 pos) s2-1 (-> this knot-constraints data s5-0 length))
      )
    )
  #f
  )

(defmethod rope-system-method-16 ((this rope-system) (arg0 vector) (arg1 vector) (arg2 int))
  (let ((s5-1 (vector-! (new 'stack-no-clear 'vector) arg1 arg0)))
    0.0
    0.0
    0.0
    (let* ((f30-0 (-> this knot-constraints data arg2 length))
           (f2-0 (vector-normalize-ret-len! s5-1 1.0))
           (f0-3 (- f30-0 f2-0))
           (f3-1 (+ (-> this knots data arg2 mass) (-> this knots data (+ arg2 1) mass)))
           (f1-2 (/ (-> this knots data arg2 mass) f3-1))
           (f3-2 (/ (-> this knots data (+ arg2 1) mass) f3-1))
           )
      (when (< 0.4 (/ (fabs f0-3) (fabs f2-0)))
        (when (< f1-2 0.01)
          (set! f1-2 0.01)
          (set! f3-2 0.99)
          )
        (when (< f3-2 0.01)
          (set! f3-2 0.01)
          (set! f1-2 0.99)
          )
        )
      (vector+float*! arg1 arg1 s5-1 (* f0-3 f1-2))
      (vector+float*! arg0 arg0 s5-1 (* -1.0 f3-2 f0-3))
      )
    )
  )

(defmethod rope-system-method-14 ((this rope-system))
  (dotimes (s5-0 (+ (-> this knots length) -1))
    (let ((a1-0 (-> this knots data s5-0))
          (a2-0 (-> this knots data (+ s5-0 1)))
          )
      (rope-system-method-16 this (-> a1-0 pos) (-> a2-0 pos) s5-0)
      )
    )
  #f
  )

;; WARN: Return type mismatch symbol vs vector.
(defmethod rope-system-method-10 ((this rope-system))
  (when (logtest? (-> this flags) (rope-system-flag rs0))
    (if (logtest? (-> this flags) (rope-system-flag rs3))
        (rope-system-method-15 this)
        (rope-system-method-17 this)
        )
    )
  (the-as vector (rope-system-method-14 this))
  )

(defmethod rope-system-method-18 ((this rope-system))
  (with-pp
    (if (not (logtest? (-> this flags) (rope-system-flag rs6)))
        (return 0)
        )
    (when (not (logtest? (-> this flags) (rope-system-flag rs2)))
      (rope-system-method-11 this)
      (rope-system-method-12 this (* 0.0033333334 (the float (- (current-time) (-> pp clock old-frame-counter)))))
      )
    (rope-system-method-10 this)
    (rope-system-method-19 this)
    )
  )

;; WARN: Return type mismatch int vs symbol.
(defmethod rope-system-method-19 ((this rope-system))
  (the-as symbol 0)
  )

(defmethod rope-system-method-22 ((this rope-system))
  (rope-system-method-20 this)
  (let ((v0-1 (logior (-> this flags) (rope-system-flag rs6))))
    (set! (-> this flags) v0-1)
    v0-1
    )
  )

(defmethod rope-system-method-21 ((this rope-system))
  (rope-system-method-20 this)
  (let ((v0-1 (logclear (-> this flags) (rope-system-flag rs6))))
    (set! (-> this flags) v0-1)
    v0-1
    )
  )

(defmethod rope-system-method-23 ((this rope-system))
  (dotimes (s5-0 (-> this knot-constraints length))
    (let ((s4-0 (-> this knot-constraints data s5-0)))
      (vector-! (-> s4-0 dir) (-> this knots data (+ s5-0 1) pos) (-> this knots data s5-0 pos))
      (set! (-> s4-0 length) (vector-normalize-ret-len! (-> s4-0 dir) 1.0))
      )
    )
  #f
  )
