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

(import "goal_src/jak1/import/sunkenfisha-ag.gc")

;; definition of type sunkenfisha
(deftype sunkenfisha (process-drawable)
  ((path-u                 float              :offset-assert 176)
   (path-speed             float              :offset-assert 180)
   (path-speed-seek-speed  float              :offset-assert 184)
   (targ-path-speed        float              :offset-assert 188)
   (path-normal-speed-lo   float              :offset-assert 192)
   (path-normal-speed-hi   float              :offset-assert 196)
   (path-dir               float              :offset-assert 200)
   (change-path-dir-time   time-frame         :offset-assert 208)
   (local-path-offset      vector     :inline :offset-assert 224)
   (targ-local-path-offset vector     :inline :offset-assert 240)
   (local-path-offset-dir  vector     :inline :offset-assert 256)
   (max-local-path-offset  vector     :inline :offset-assert 272)
   (facing-rot             vector     :inline :offset-assert 288)
   (path-trans-offset      vector     :inline :offset-assert 304)
   )
  :heap-base #xd0
  :method-count-assert 28
  :size-assert         #x140
  :flag-assert         #x1c00d00140
  (:methods
    (TODO-RENAME-20 (_type_) float 20)
    (TODO-RENAME-orbit?-21 (_type_ vector float vector) vector 21)
    (dummy-22 (_type_) none 22)
    (TODO-RENAME-23 (_type_) quaternion 23)
    (TODO-RENAME-24 (_type_) vector 24)
    (dummy-25 (_type_) none 25)
    (TODO-RENAME-26 (_type_) float 26)
    (TODO-RENAME-27 (_type_) float 27)
    )
  (:states
    sunkenfisha-idle
    )
  )

;; definition for method 3 of type sunkenfisha
(defmethod inspect sunkenfisha ((obj sunkenfisha))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tpath-u: ~f~%" (-> obj path-u))
  (format #t "~T~Tpath-speed: ~f~%" (-> obj path-speed))
  (format #t "~T~Tpath-speed-seek-speed: ~f~%" (-> obj path-speed-seek-speed))
  (format #t "~T~Ttarg-path-speed: ~f~%" (-> obj targ-path-speed))
  (format #t "~T~Tpath-normal-speed-lo: ~f~%" (-> obj path-normal-speed-lo))
  (format #t "~T~Tpath-normal-speed-hi: ~f~%" (-> obj path-normal-speed-hi))
  (format #t "~T~Tpath-dir: ~f~%" (-> obj path-dir))
  (format #t "~T~Tchange-path-dir-time: ~D~%" (-> obj change-path-dir-time))
  (format #t "~T~Tlocal-path-offset: #<vector @ #x~X>~%" (-> obj local-path-offset))
  (format #t "~T~Ttarg-local-path-offset: #<vector @ #x~X>~%" (-> obj targ-local-path-offset))
  (format #t "~T~Tlocal-path-offset-dir: #<vector @ #x~X>~%" (-> obj local-path-offset-dir))
  (format #t "~T~Tmax-local-path-offset: #<vector @ #x~X>~%" (-> obj max-local-path-offset))
  (format #t "~T~Tfacing-rot: #<vector @ #x~X>~%" (-> obj facing-rot))
  (format #t "~T~Tpath-trans-offset: #<vector @ #x~X>~%" (-> obj path-trans-offset))
  obj
  )

;; failed to figure out what this is:
(defskelgroup *sunkenfisha-red-yellow-sg* sunkenfisha sunkenfisha-red-yellow-lod0-jg -1
              ((sunkenfisha-red-yellow-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 1.5 0 2.5)
              )

;; failed to figure out what this is:
(defskelgroup *sunkenfisha-yellow-blue-sg* sunkenfisha sunkenfisha-yellow-blue-lod0-jg -1
              ((sunkenfisha-yellow-blue-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 1.5 0 2.5)
              )

;; failed to figure out what this is:
(defskelgroup *sunkenfisha-yellow-eye-sg* sunkenfisha sunkenfisha-yellow-eye-lod0-jg -1
              ((sunkenfisha-yellow-eye-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 1.5 0 2.5)
              )

;; definition for method 22 of type sunkenfisha
;; INFO: Return type mismatch int vs none.
(defmethod dummy-22 sunkenfisha ((obj sunkenfisha))
  0
  (none)
  )

;; definition for method 21 of type sunkenfisha
(defmethod TODO-RENAME-orbit?-21 sunkenfisha ((obj sunkenfisha) (arg0 vector) (arg1 float) (arg2 vector))
  (eval-path-curve! (-> obj path) arg0 arg1 'interp)
  (vector+! arg0 arg0 (-> obj path-trans-offset))
  (let ((s2-0 (new 'stack-no-clear 'vector)))
    (TODO-RENAME-14 (-> obj path) s2-0 arg1)
    (let ((f0-2 (atan (-> s2-0 x) (-> s2-0 z)))
          (s4-1 (new 'stack-no-clear 'vector))
          )
      (vector-rotate-around-y! s4-1 arg2 f0-2)
      (vector+! arg0 arg0 s4-1)
      )
    )
  )

;; definition for method 24 of type sunkenfisha
(defmethod TODO-RENAME-24 sunkenfisha ((obj sunkenfisha))
  (let ((s4-0 (new 'stack-no-clear 'vector))
        (s3-0 (new 'stack-no-clear 'matrix))
        (gp-0 (new 'stack-no-clear 'vector))
        )
    (vector-! s4-0 (-> obj targ-local-path-offset) (-> obj local-path-offset))
    (when (>= (* 4096.0 (-> *display* seconds-per-frame)) (vector-length s4-0))
      (until (< (* 16384.0 (-> *display* seconds-per-frame)) (vector-length s4-0))
        (set! (-> obj targ-local-path-offset x)
              (rand-vu-float-range (- (-> obj max-local-path-offset x)) (-> obj max-local-path-offset x))
              )
        (set! (-> obj targ-local-path-offset y)
              (rand-vu-float-range (- (-> obj max-local-path-offset y)) (-> obj max-local-path-offset y))
              )
        (vector-! s4-0 (-> obj targ-local-path-offset) (-> obj local-path-offset))
        )
      )
    (vector-normalize! s4-0 1.0)
    (matrix-from-two-vectors-max-angle-partial!
      s3-0
      (-> obj local-path-offset-dir)
      s4-0
      (* 16384.0 (-> *display* seconds-per-frame))
      0.1
      )
    (vector-matrix*! (-> obj local-path-offset-dir) (-> obj local-path-offset-dir) s3-0)
    (vector-normalize! (-> obj local-path-offset-dir) 1.0)
    (vector-float*! gp-0 (-> obj local-path-offset-dir) (* 4096.0 (-> *display* seconds-per-frame)))
    (vector+! (-> obj local-path-offset) (-> obj local-path-offset) gp-0)
    )
  )

;; definition for method 25 of type sunkenfisha
;; INFO: Return type mismatch float vs none.
(defmethod dummy-25 sunkenfisha ((obj sunkenfisha))
  (let* ((f0-0 (-> obj path-speed))
         (f1-1
           (seek f0-0 (-> obj targ-path-speed) (* (-> obj path-speed-seek-speed) (-> *display* seconds-per-frame)))
           )
         )
    (set! (-> obj path-speed) f1-1)
    (let ((f30-0 (+ (-> obj path-u) (* (-> obj path-dir) f1-1 (-> *display* seconds-per-frame)))))
      (cond
        ((< f30-0 0.0)
         (set! f30-0 (- f30-0))
         (TODO-RENAME-20 obj)
         )
        ((< 1.0 f30-0)
         (+! f30-0 (* 2.0 (- 1.0 f30-0)))
         (TODO-RENAME-20 obj)
         )
        )
      (set! (-> obj path-u) f30-0)
      )
    )
  (none)
  )

;; definition for method 23 of type sunkenfisha
(defmethod TODO-RENAME-23 sunkenfisha ((obj sunkenfisha))
  (let ((s5-0 (new 'stack-no-clear 'vector)))
    (set-vector! s5-0 (- (vector-x-angle (-> obj root transv))) (vector-y-angle (-> obj root transv)) 0.0 1.0)
    (set! (-> obj facing-rot x)
          (deg-seek-smooth (-> obj facing-rot x) (-> s5-0 x) (* 16384.0 (-> *display* seconds-per-frame)) 0.1)
          )
    (set! (-> obj facing-rot y)
          (deg-seek-smooth (-> obj facing-rot y) (-> s5-0 y) (* 32768.0 (-> *display* seconds-per-frame)) 0.1)
          )
    )
  (quaternion-zxy! (-> obj root quat) (-> obj facing-rot))
  )

;; definition for method 20 of type sunkenfisha
(defmethod TODO-RENAME-20 sunkenfisha ((obj sunkenfisha))
  (set! (-> obj path-dir) (- (-> obj path-dir)))
  (set! (-> obj path-speed) 0.0)
  (set! (-> obj targ-path-speed)
        (rand-vu-float-range (-> obj path-normal-speed-lo) (-> obj path-normal-speed-hi))
        )
  (set! (-> obj change-path-dir-time)
        (+ (-> *display* base-frame-counter) (rand-vu-int-range (seconds 5) (seconds 18)))
        )
  (set! (-> obj targ-local-path-offset y)
        (* 0.5 (+ (-> obj targ-local-path-offset y) (-> obj local-path-offset y)))
        )
  (let* ((f0-8 (-> obj local-path-offset x))
         (f1-3 (- (-> obj targ-local-path-offset x) f0-8))
         )
    (when (< (fabs f1-3) 12288.0)
      (cond
        ((>= f1-3 0.0)
         (let ((f0-10 (fmin (+ 12288.0 f0-8) (-> obj max-local-path-offset x))))
           (set! (-> obj targ-local-path-offset x) f0-10)
           f0-10
           )
         )
        (else
          (let ((f0-12 (fmax (+ -12288.0 f0-8) (- (-> obj max-local-path-offset x)))))
            (set! (-> obj targ-local-path-offset x) f0-12)
            f0-12
            )
          )
        )
      )
    )
  )

;; failed to figure out what this is:
(defstate sunkenfisha-idle (sunkenfisha)
  :trans (behavior ()
    (local-vars (at-0 int))
    (rlet ((vf0 :class vf)
           (vf1 :class vf)
           (vf2 :class vf)
           )
      (init-vf0-vector)
      (if (>= (-> *display* base-frame-counter) (-> self change-path-dir-time))
          (TODO-RENAME-20 self)
          )
      (TODO-RENAME-24 self)
      (dummy-25 self)
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (TODO-RENAME-orbit?-21 self gp-0 (-> self path-u) (-> self local-path-offset))
        (vector-! (-> self root transv) gp-0 (-> self root trans))
        (set! (-> self root trans quad) (-> gp-0 quad))
        )
      (let ((v1-16 (-> self root transv)))
        (.lvf vf1 (&-> (-> self root transv) quad))
        (let ((f0-1 (-> *display* frames-per-second)))
          (.mov at-0 f0-1)
          )
        (.mov vf2 at-0)
        (.mov.vf vf1 vf0 :mask #b1000)
        (.mul.x.vf vf1 vf1 vf2 :mask #b111)
        (.svf (&-> v1-16 quad) vf1)
        )
      (TODO-RENAME-23 self)
      (none)
      )
    )
  :code (behavior ()
    (loop
      (ja-no-eval :group! (-> self draw art-group data 6) :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    (none)
    )
  :post (the-as (function none :behavior sunkenfisha) ja-post)
  )

;; definition for method 26 of type sunkenfisha
(defmethod TODO-RENAME-26 sunkenfisha ((obj sunkenfisha))
  (set! (-> obj root) (new 'process 'trsqv))
  (process-drawable-from-entity! obj (-> obj entity))
  (set-vector! (-> obj root scale) 6.0 6.0 6.0 1.0)
  (let ((v1-3 (rand-vu-int-count 3)))
    (cond
      ((zero? v1-3)
       (initialize-skeleton obj *sunkenfisha-red-yellow-sg* '())
       )
      ((= v1-3 1)
       (initialize-skeleton obj *sunkenfisha-yellow-blue-sg* '())
       )
      ((= v1-3 2)
       (initialize-skeleton obj *sunkenfisha-yellow-eye-sg* '())
       )
      )
    )
  (ja-channel-set! 1)
  (let ((s5-0 (-> obj skel root-channel 0)))
    (joint-control-channel-group-eval!
      s5-0
      (the-as art-joint-anim (-> obj draw art-group data 6))
      num-func-identity
      )
    (let ((f0-4 0.0))
      (set! (-> s5-0 frame-num) f0-4)
      f0-4
      )
    )
  )

;; definition for method 27 of type sunkenfisha
;; INFO: Used lq/sq
(defmethod TODO-RENAME-27 sunkenfisha ((obj sunkenfisha))
  (local-vars (sv-16 res-tag) (sv-32 res-tag) (sv-48 res-tag))
  (vector-reset! (-> obj path-trans-offset))
  (set! (-> obj path-u) (rand-vu-float-range 0.0 1.0))
  (if (zero? (rand-vu-int-count 2))
      (set! (-> obj path-dir) 1.0)
      (set! (-> obj path-dir) -1.0)
      )
  (set-vector! (-> obj max-local-path-offset) 16384.0 28672.0 0.0 1.0)
  (set! sv-16 (new 'static 'res-tag))
  (let ((v1-5 (res-lump-data (-> obj entity) 'path-max-offset (pointer float) :tag-ptr (& sv-16))))
    (when v1-5
      (set! (-> obj max-local-path-offset x) (-> v1-5 0))
      (set! (-> obj max-local-path-offset y) (-> v1-5 1))
      )
    )
  (set-vector! (-> obj local-path-offset) 0.0 0.0 0.0 1.0)
  (set! (-> obj local-path-offset x)
        (rand-vu-float-range (- (-> obj max-local-path-offset x)) (-> obj max-local-path-offset x))
        )
  (set! (-> obj local-path-offset y)
        (rand-vu-float-range (- (-> obj max-local-path-offset y)) (-> obj max-local-path-offset y))
        )
  (set! (-> obj targ-local-path-offset quad) (-> obj local-path-offset quad))
  (set! (-> obj targ-local-path-offset x)
        (rand-vu-float-range (- (-> obj max-local-path-offset x)) (-> obj max-local-path-offset x))
        )
  (set! (-> obj targ-local-path-offset y)
        (rand-vu-float-range (- (-> obj max-local-path-offset y)) (-> obj max-local-path-offset y))
        )
  (vector-! (-> obj local-path-offset-dir) (-> obj targ-local-path-offset) (-> obj local-path-offset))
  (vector-normalize! (-> obj local-path-offset-dir) 1.0)
  (set! (-> obj change-path-dir-time)
        (+ (-> *display* base-frame-counter) (rand-vu-int-range (seconds 5) (seconds 18)))
        )
  (set! (-> obj path) (new 'process 'curve-control obj 'path -1000000000.0))
  (logior! (-> obj path flags) (path-control-flag display draw-line draw-point draw-text))
  (set! sv-32 (new 'static 'res-tag))
  (let ((v1-16 (res-lump-data (-> obj entity) 'path-trans-offset (pointer float) :tag-ptr (& sv-32))))
    (when v1-16
      (+! (-> obj path-trans-offset x) (-> v1-16 0))
      (+! (-> obj path-trans-offset y) (-> v1-16 1))
      (+! (-> obj path-trans-offset z) (-> v1-16 2))
      )
    )
  (if (< (-> obj path curve num-cverts) 2)
      (go process-drawable-art-error "bad path")
      )
  (let ((f28-0 8192.0)
        (f30-0 26624.0)
        )
    (set! sv-48 (new 'static 'res-tag))
    (let ((v1-23 (res-lump-data (-> obj entity) 'speed (pointer float) :tag-ptr (& sv-48))))
      (when v1-23
        (set! f28-0 (-> v1-23 0))
        (set! f30-0 (-> v1-23 1))
        )
      )
    (let ((f0-35 (path-distance (-> obj path))))
      (set! (-> obj path-normal-speed-lo) (/ f28-0 f0-35))
      (set! (-> obj path-normal-speed-hi) (/ f30-0 f0-35))
      )
    )
  (set! (-> obj path-speed-seek-speed) (* 2.0 (- (-> obj path-normal-speed-hi) (-> obj path-normal-speed-lo))))
  (set! (-> obj path-speed) (rand-vu-float-range (-> obj path-normal-speed-lo) (-> obj path-normal-speed-hi)))
  (set! (-> obj targ-path-speed) (-> obj path-speed))
  (let ((s4-0 (new 'stack-no-clear 'vector)))
    (TODO-RENAME-14 (-> obj path) s4-0 (-> obj path-u))
    (set-vector! (-> obj facing-rot) 0.0 (atan (-> s4-0 x) (-> s4-0 z)) 0.0 1.0)
    )
  (when (< (-> obj path-dir) 0.0)
    (let ((f0-52 (- (-> obj facing-rot y))))
      (set! (-> obj facing-rot y) f0-52)
      f0-52
      )
    )
  )

;; definition for function sunkenfisha-init-by-other
;; INFO: Return type mismatch object vs none.
(defbehavior sunkenfisha-init-by-other sunkenfisha ((arg0 entity-actor))
  (set! (-> self entity) arg0)
  (TODO-RENAME-26 self)
  (TODO-RENAME-27 self)
  (go sunkenfisha-idle)
  (none)
  )

;; definition for method 11 of type sunkenfisha
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! sunkenfisha ((obj sunkenfisha) (arg0 entity-actor))
  (TODO-RENAME-26 obj)
  (TODO-RENAME-27 obj)
  (let ((s5-0 (+ (res-lump-value (-> obj entity) 'count uint128 :default (the-as uint128 1)) -1)))
    (while (> (the-as int s5-0) 0)
      (process-spawn sunkenfisha (-> obj entity) :to obj)
      (+! s5-0 -1)
      )
    )
  (go sunkenfisha-idle)
  (none)
  )
