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

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

;; DECOMP BEGINS

;; WARN: Return type mismatch float vs object.
(defmethod init! ((this rope-prim-system) (arg0 int))
  (call-parent-method this arg0)
  (set! (-> this strip1)
        (new 'process 'prim-strip (* arg0 4) (new 'static 'texture-id :index #x3 :page #x1) (the-as string #f))
        )
  (set! (-> this strip2)
        (new 'process 'prim-strip (* arg0 4) (new 'static 'texture-id :index #x3 :page #x1) (the-as string #f))
        )
  (set! (-> this strip1 num-verts) (the-as uint 0))
  (set! (-> this strip2 num-verts) (the-as uint 0))
  (set! (-> this alpha) 1.0)
  (set! (-> this uv-repeat-dist) 40960000.0)
  (set! (-> this width) 1228.8)
  )

(define *rope-prim-alpha-blend* (new 'static 'gs-alpha :b #x1 :d #x1))

(defmethod rope-system-method-19 ((this rope-prim-system))
  (local-vars
    (sv-112 vector)
    (sv-116 vector)
    (sv-120 matrix)
    (sv-124 float)
    (sv-128 float)
    (sv-132 float)
    (sv-136 float)
    (sv-240 vector)
    (sv-244 vector)
    (sv-248 vector)
    (sv-252 vector)
    (sv-256 object)
    (sv-260 rgbaf)
    )
  (let ((f30-0 0.0))
    0.0
    (set! (-> this strip1 num-verts) (the-as uint 0))
    (set! (-> this strip2 num-verts) (the-as uint 0))
    (set! (-> this strip1 alpha) *rope-prim-alpha-blend*)
    (set! (-> this strip2 alpha) *rope-prim-alpha-blend*)
    (set! (-> this strip1 adnops 0 cmds) (gs-reg64 hack))
    (set! (-> this strip1 data0) (new 'static 'gs-test))
    (set! (-> this strip2 adnops 0 cmds) (gs-reg64 hack))
    (set! (-> this strip2 data0) (new 'static 'gs-test))
    (logior! (-> this strip1 flags) (prim-flags fog-enable))
    (logior! (-> this strip2 flags) (prim-flags fog-enable))
    (dotimes (s5-0 (-> this knots length))
      (set! sv-112 (new 'stack-no-clear 'vector))
      (set! sv-116 (new 'stack-no-clear 'vector))
      (set! sv-120 (new 'stack-no-clear 'matrix))
      (set! sv-124 (-> this width))
      (set! sv-128 (-> this alpha))
      (set! sv-132 (the-as float 0.0))
      (set! sv-136 (the-as float 0.0))
      (vector-copy! sv-112 (-> this knots data s5-0 pos))
      (cond
        ((< s5-0 (+ (-> this knots length) -1))
         (vector-! sv-116 (-> this knots data (+ s5-0 1) pos) sv-112)
         (set! sv-136 (vector-vector-distance sv-112 (-> this knots data (+ s5-0 1) pos)))
         )
        (else
          (vector-! sv-116 sv-112 (-> this knots data (+ s5-0 -1) pos))
          )
        )
      (let ((f0-7 (/ f30-0 (-> this uv-repeat-dist))))
        (set! sv-132 (- f0-7 (* (the float (the int (/ f0-7 6.0))) 6.0)))
        )
      (vector-normalize! sv-116 1.0)
      (matrix-f-u-compose sv-120 sv-116 *up-vector*)
      (set! sv-240 (vector+float*! (new 'stack-no-clear 'vector) sv-112 (-> sv-120 rvec) (* 0.5 sv-124)))
      (set! sv-244 (vector+float*! (new 'stack-no-clear 'vector) sv-112 (-> sv-120 rvec) (* -0.5 sv-124)))
      (set! sv-248 (vector+float*! (new 'stack-no-clear 'vector) sv-112 (-> sv-120 uvec) (* 0.5 sv-124)))
      (set! sv-252 (vector+float*! (new 'stack-no-clear 'vector) sv-112 (-> sv-120 uvec) (* -0.5 sv-124)))
      (set! sv-256 (new 'stack-no-clear 'rgbaf))
      (set! sv-260 (new 'stack-no-clear 'rgbaf))
      (set! (-> sv-260 x) 1.0)
      (set! (-> sv-260 y) 1.0)
      (set! (-> sv-260 z) 1.0)
      (set! (-> sv-260 w) sv-128)
      (set! sv-256 (rgba<-rgbaf (the-as rgba sv-256) sv-260))
      ;; og:preserve-this
      (add-prim-vert this (-> this strip1) sv-240 (the-as rgba sv-256) 0.0 sv-132)
      (add-prim-vert this (-> this strip1) sv-244 (the-as rgba sv-256) 1.0 sv-132)
      (add-prim-vert this (-> this strip2) sv-248 (the-as rgba sv-256) 1.0 sv-132)
      (add-prim-vert this (-> this strip2) sv-252 (the-as rgba sv-256) 0.0 sv-132)
      (+! f30-0 sv-136)
      )
    )
  1
  (call-parent-method this)
  )

(defmethod add-prim-vert ((this rope-prim-system) (arg0 prim-strip) (arg1 vector) (arg2 rgba) (arg3 float) (arg4 float))
  (when (< (-> arg0 num-verts) (-> arg0 allocated-num-verts))
    (let ((v1-5 (-> arg0 data (-> arg0 num-verts))))
      (vector-copy! (-> v1-5 pos) arg1)
      (set! (-> v1-5 col) arg2)
      (set! (-> v1-5 stq x) arg3)
      (set! (-> v1-5 stq y) arg4)
      )
    (+! (-> arg0 num-verts) 1)
    )
  0
  (none)
  )

(defmethod relocate ((this rope-prim-system) (offset int))
  (if (nonzero? (-> this strip1))
      (&+! (-> this strip1) offset)
      )
  (if (nonzero? (-> this strip2))
      (&+! (-> this strip2) offset)
      )
  (call-parent-method this offset)
  )

(defmethod rope-system-method-21 ((this rope-prim-system))
  (set! (-> this strip1 num-verts) (the-as uint 0))
  (set! (-> this strip2 num-verts) (the-as uint 0))
  (call-parent-method this)
  )

(defmethod init ((this rope-joint-system) (arg0 rope-joint-system-info) (arg1 process-drawable))
  (set! (-> this info) arg0)
  (set! (-> this proc) (process->handle arg1))
  (let ((s4-0 (-> arg0 joint-info-array length)))
    (init! this s4-0)
    (set! (-> this joint-mods) (new 'process 'joint-mod-world-array s4-0))
    )
  (let ((a3-0 0)
        (s4-1 0)
        (s3-0 (+ (-> this joint-mods length) -1))
        )
    (while (>= s3-0 s4-1)
      (init
        (-> this joint-mods data s4-1)
        arg1
        (the-as uint (-> this info joint-info-array s4-1 joint-index))
        (the-as joint-mod-base-flags a3-0)
        )
      (set! a3-0 1)
      (+! s4-1 1)
      )
    )
  (dotimes (s4-2 (-> this knot-constraints length))
    (let ((s3-1 (vector<-cspace!
                  (new 'stack-no-clear 'vector)
                  (-> arg1 node-list data (-> this info joint-info-array s4-2 joint-index))
                  )
                )
          (a1-12 (vector<-cspace!
                   (new 'stack-no-clear 'vector)
                   (-> arg1 node-list data (-> this info joint-info-array (+ s4-2 1) joint-index))
                   )
                 )
          )
      (set! (-> this knot-constraints data s4-2 length) (vector-vector-distance s3-1 a1-12))
      )
    )
  (dotimes (s4-3 (-> this joint-mods length))
    (let ((v1-44
            (vector<-cspace!
              (new 'stack-no-clear 'vector)
              (-> arg1 node-list data (-> this info joint-info-array s4-3 joint-index))
              )
            )
          )
      (set! (-> this knots data s4-3 pos quad) (-> v1-44 quad))
      (set! (-> (the-as (pointer uint128) (+ (the-as uint (-> this knots data 0 prev-pos)) (* s4-3 64))))
            (-> v1-44 quad)
            )
      )
    (when #t
      (let ((s3-2 (-> this joint-mods data s4-3)))
        (matrix->transformq
          (-> s3-2 transform)
          (-> arg1 node-list data (-> this info joint-info-array s4-3 joint-index) bone transform)
          )
        (matrix->scale
          (-> arg1 node-list data (-> this info joint-info-array s4-3 joint-index) bone transform)
          (-> s3-2 transform scale)
          )
        )
      )
    )
  #f
  )

(defmethod relocate ((this rope-joint-system) (offset int))
  (if (nonzero? (-> this joint-mods))
      (&+! (-> this joint-mods) offset)
      )
  (call-parent-method this offset)
  )

(defmethod rope-system-method-10 ((this rope-joint-system))
  (call-parent-method this)
  (let ((v1-1 (handle->process (-> this proc))))
    (vector<-cspace!
      (-> this knots data 0 pos)
      (-> (the-as process-drawable v1-1) node-list data (-> this info joint-info-array 0 joint-index))
      )
    )
  (let ((v0-3 (-> this knots data 0 prev-pos)))
    (vector-copy! v0-3 (-> this knots data 0 pos))
    v0-3
    )
  )

(defmethod rope-system-method-19 ((this rope-joint-system))
  (local-vars (sv-288 vector))
  (call-parent-method this)
  (let* ((s5-0 (-> this joint-mods data))
         (s4-0 (handle->process (-> this proc)))
         (s3-0 (vector-normalize-copy!
                 (new 'stack-no-clear 'vector)
                 (-> (the-as process-drawable s4-0)
                     node-list
                     data
                     (-> this info joint-info-array 0 joint-index)
                     bone
                     transform
                     fvec
                     )
                 1.0
                 )
               )
         (s2-0 (vector-normalize-copy!
                 (new 'stack-no-clear 'vector)
                 (-> (the-as process-drawable s4-0)
                     node-list
                     data
                     (-> this info joint-info-array 0 joint-index)
                     bone
                     transform
                     rvec
                     )
                 1.0
                 )
               )
         )
    (vector-normalize-copy!
      (new 'stack-no-clear 'vector)
      (-> (the-as process-drawable s4-0)
          node-list
          data
          (-> this info joint-info-array 0 joint-index)
          bone
          transform
          uvec
          )
      1.0
      )
    (matrix->quaternion (-> s5-0 0 transform quat) (matrix-fr-compose (new 'stack-no-clear 'matrix) s3-0 s2-0))
    )
  (let ((s5-2 1)
        (s4-2 (+ (-> this joint-mods length) -1))
        )
    (while (>= s4-2 s5-2)
      (let ((v1-18 (-> this knots data (+ s5-2 -1)))
            (a0-15 (-> this knots data s5-2))
            (s2-1 (new 'stack-no-clear 'vector))
            )
        (vector-! s2-1 (-> a0-15 pos) (-> v1-18 pos))
        (vector-normalize! s2-1 1.0)
        (let ((s3-1 (-> this joint-mods data s5-2)))
          (let ((s1-0 matrix-u-f-compose)
                (s0-0 (new 'stack-no-clear 'matrix))
                )
            (set! sv-288 s2-1)
            (let* ((a2-4 (vector-z-quaternion! (new 'stack-no-clear 'vector) (-> s3-1 transform quat)))
                   (a1-28 (s1-0 s0-0 sv-288 a2-4))
                   )
              (matrix->quaternion (-> s3-1 transform quat) a1-28)
              )
            )
          (quaternion-normalize! (-> s3-1 transform quat))
          (let* ((v1-24 (-> this joint-mods data (+ s5-2 -1)))
                 (s1-1 (vector-z-quaternion! (new 'stack-no-clear 'vector) (-> v1-24 transform quat)))
                 (s0-1 (vector-z-quaternion! (new 'stack-no-clear 'vector) (-> s3-1 transform quat)))
                 )
            (vector-flatten! s1-1 s1-1 s2-1)
            (vector-normalize! s1-1 1.0)
            (let ((s2-3 (vector-cross! (new 'stack-no-clear 'vector) s0-1 s1-1))
                  (f30-0 (vector-dot s0-1 s1-1))
                  (s1-2 (new 'stack-no-clear 'quaternion))
                  )
              (vector-normalize! s2-3 1.0)
              (let ((f0-1 (acos f30-0)))
                (when #t
                  (quaternion-vector-angle! s1-2 s2-3 f0-1)
                  (quaternion*! (-> s3-1 transform quat) s1-2 (-> s3-1 transform quat))
                  (quaternion-normalize! (-> s3-1 transform quat))
                  )
                )
              )
            )
          )
        )
      (+! s5-2 1)
      )
    )
  (let ((v1-31 0)
        (a0-35 (+ (-> this joint-mods length) -1))
        )
    (while (>= a0-35 v1-31)
      (set! (-> this joint-mods data v1-31 transform trans quad) (-> this knots data v1-31 pos quad))
      (+! v1-31 1)
      )
    )
  #f
  )
