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

;; name: ginsu.gc
;; name in dgo: ginsu
;; dgos: DMI

;; DECOMP BEGINS

(defpartgroup group-ginsu-sparks
  :id 475
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 2105) (sp-item 2106 :fade-after (meters 90)))
  )

(defpart 2105
  :init-specs ((:texture (motion-blur-part effects))
    (:num 2.0 3.0)
    (:scale-x (meters 2.5))
    (:rot-x 4)
    (:scale-y (meters 0.033))
    (:r 255.0)
    (:g 255.0)
    (:b 255.0)
    (:a 128.0)
    (:omega (degrees 0.0225))
    (:vel-y (meters 0.06666667) (meters 0.06666667))
    (:fade-g -0.85 -1.7)
    (:fade-b -8.0)
    (:fade-a -0.21333334 -0.21333334)
    (:accel-y (meters -0.0016666667) (meters -0.00066666666))
    (:friction 0.93)
    (:timer (seconds 0.1) (seconds 0.197))
    (:flags (sp-cpuinfo-flag-3))
    (:func 'sparticle-motion-blur)
    (:conerot-x (degrees 0) (degrees 3600))
    (:conerot-y (degrees 0) (degrees 3600))
    )
  )

(defpart 2106
  :init-specs ((:texture (glow effects))
    (:num 1.0)
    (:x (meters 0.5) (meters 0.5))
    (:scale-x (meters 1) (meters 0.5))
    (:rot-x (degrees 2.25))
    (:scale-y :copy scale-x)
    (:r 255.0)
    (:g 255.0)
    (:b 255.0)
    (:a 96.0)
    (:omega (degrees 2036.25))
    (:fade-a -4.8)
    (:timer (seconds 0.067))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 glow))
    (:userdata 409.6)
    )
  )

(defpartgroup group-ginsu-blade
  :id 476
  :duration (seconds 0.017)
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 4)
  :parts ((sp-item 2107 :flags (is-3d bit6 bit7))
    (sp-item 2108 :fade-after (meters 60) :flags (bit7) :period (seconds 1) :length (seconds 0.017))
    (sp-item 2108 :fade-after (meters 60) :flags (bit7) :period (seconds 2.985) :length (seconds 0.017))
    (sp-item 2108 :fade-after (meters 60) :flags (bit7) :period (seconds 4.967) :length (seconds 0.017))
    )
  )

(defpart 2107
  :init-specs ((:texture (drill-enemy-blade-blur drillmid-sprite))
    (:num 1.0)
    (:y (meters 0.09))
    (:scale-x (meters 3) (meters 0.5))
    (:rot-x (degrees 0))
    (:rot-y (degrees 0))
    (:rot-z (degrees 0))
    (:scale-y :copy scale-x)
    (:r 64.0 128.0)
    (:g :copy r)
    (:b :copy g)
    (:a 64.0 2 64.0)
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 sp-cpuinfo-flag-12))
    (:rotate-y (degrees 0))
    )
  )

(defpart 2108
  :init-specs ((:texture (glow effects))
    (:num 1.0)
    (:x (meters 0.5) (meters 0.5))
    (:scale-x (meters 1) (meters 0.5))
    (:rot-x (degrees 1.125))
    (:scale-y :copy scale-x)
    (:r 255.0)
    (:g 255.0)
    (:b 255.0)
    (:a 64.0)
    (:omega (degrees 1361.25))
    (:timer (seconds 0.017))
    (:flags (sp-cpuinfo-flag-2 sp-cpuinfo-flag-3 sp-cpuinfo-flag-12 glow))
    (:userdata 409.6)
    (:rotate-y (degrees 0) (degrees 3600))
    )
  )

(defskelgroup skel-ginsu ginsu ginsu-lod0-jg ginsu-idle-ja
              ((ginsu-lod0-mg (meters 20)) (ginsu-lod1-mg (meters 40)) (ginsu-lod2-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 2.5)
              :shadow ginsu-shadow-mg
              :origin-joint-index 8
              )

(deftype ginsu-anim-info (structure)
  ((anim-index  int32)
   )
  )


(deftype ginsu-global-info (basic)
  ((prev-blue-hit  int8)
   (blue-hit-anim  int32  3)
   )
  )


(deftype ginsu (nav-enemy)
  ((blade-jm             joint-mod)
   (blade-speed          cam-float-seeker  :inline)
   (blade-angle          float)
   (desired-distance     float)
   (spiral-time          time-frame)
   (blade-part           sparticle-launch-control)
   (ambush-path          path-control)
   (path-pos             float)
   (ambush-started       symbol)
   (blade-sound          sound-id)
   (blade-sound-playing  symbol)
   (grind-sound          sound-id)
   (grind-sound-playing  symbol)
   (grind-timer          time-frame)
   )
  (:state-methods
    anticipate-attack
    attack
    )
  (:methods
    (ginsu-method-180 (_type_) none)
    (ginsu-method-181 (_type_ vector) vector)
    (ginsu-method-182 (_type_) none)
    (ginsu-method-183 (_type_ symbol) none)
    )
  )


(define *ginsu-global-info*
  (new 'static 'ginsu-global-info :blue-hit-anim (new 'static 'array int32 3 5 5 5))
  )

(define *ginsu-nav-enemy-info*
  (new 'static 'nav-enemy-info
    :use-die-falling #t
    :use-victory #f
    :use-jump-blocked #t
    :debug-draw-neck #f
    :jump-debug-draw #f
    :move-to-ground #t
    :hover-if-no-ground #f
    :idle-anim-script (new 'static 'array idle-control-frame 4
      (new 'static 'idle-control-frame :command (ic-cmd play) :anim #x5 :param0 #x1 :param1 #x1)
      (new 'static 'idle-control-frame)
      (new 'static 'idle-control-frame)
      (new 'static 'idle-control-frame)
      )
    :idle-anim 5
    :notice-anim 10
    :hostile-anim 5
    :hit-anim 5
    :knocked-anim 11
    :knocked-land-anim 12
    :die-anim 11
    :die-falling-anim 5
    :victory-anim 5
    :jump-wind-up-anim 5
    :jump-in-air-anim 5
    :jump-land-anim 5
    :neck-joint 7
    :look-at-joint 7
    :bullseye-joint 4
    :sound-die (static-sound-name "ginsu-die")
    :notice-distance (meters 40)
    :notice-distance-delta (meters 10)
    :proximity-notice-distance (meters 40)
    :default-hit-points 1
    :gnd-collide-with (collide-spec backgnd)
    :overlaps-others-collide-with-filter (collide-spec jak bot enemy hit-by-others-list player-list)
    :penetrate-knocked (penetrate
      generic-attack
      flop
      punch
      spin
      roll
      uppercut
      bonk
      tube
      vehicle
      flut-attack
      board
      mech-punch
      mech-bonk
      dark-skin
      dark-punch
      dark-bomb
      dark-giant
      shield
      explode
      jak-yellow-shot
      jak-red-shot
      jak-blue-shot
      jak-dark-shot
      enemy-yellow-shot
      enemy-dark-shot
      eco-yellow
      knocked
      )
    :movement-gravity (meters -100)
    :friction 0.8
    :attack-shove-back (meters 3)
    :attack-shove-up (meters 2)
    :attack-mode 'generic
    :attack-damage 2
    :recover-gnd-collide-with (collide-spec backgnd crate obstacle hit-by-others-list pusher)
    :jump-height-min (meters 3)
    :jump-height-factor 0.5
    :knocked-seek-ry-clamp 4551.1113
    :knocked-soft-vxz-lo 72089.6
    :knocked-soft-vxz-hi 108134.4
    :knocked-soft-vy-lo 81920.0
    :knocked-soft-vy-hi 122880.0
    :knocked-medium-vxz-lo 147456.0
    :knocked-medium-vxz-hi 196608.0
    :knocked-medium-vy-lo 135168.0
    :knocked-medium-vy-hi 151552.0
    :knocked-hard-vxz-lo 78643.2
    :knocked-hard-vxz-hi 117964.8
    :knocked-hard-vy-lo 183500.8
    :knocked-hard-vy-hi 209715.2
    :knocked-huge-vxz-lo 164659.2
    :knocked-huge-vxz-hi 249036.8
    :knocked-huge-vy-lo 183500.8
    :knocked-huge-vy-hi 217907.2
    :knocked-yellow-vxz-lo 40960.0
    :knocked-yellow-vxz-hi 49152.0
    :knocked-yellow-vy-lo 57344.0
    :knocked-yellow-vy-hi 81920.0
    :knocked-red-vxz-lo 24576.0
    :knocked-red-vxz-hi 196608.0
    :knocked-red-vy-lo 94208.0
    :knocked-red-vy-hi 151552.0
    :knocked-blue-vxz-lo 40960.0
    :knocked-blue-vxz-hi 49152.0
    :knocked-blue-vy-lo 24576.0
    :knocked-blue-vy-hi 81920.0
    :shadow-size (meters 2)
    :shadow-max-y (meters 1)
    :shadow-min-y (meters -1)
    :shadow-locus-dist (meters 150)
    :gem-joint 7
    :gem-offset (new 'static 'sphere :r 163840.0)
    :callback-info #f
    :use-momentum #f
    :use-frustration #t
    :use-stop-chase #f
    :use-circling #t
    :use-pacing #t
    :walk-anim 5
    :turn-anim 5
    :run-anim 5
    :taunt-anim -1
    :run-travel-speed (meters 16)
    :run-acceleration (meters 1)
    :run-turning-acceleration (meters 100)
    :walk-travel-speed (meters 16)
    :walk-acceleration (meters 1)
    :walk-turning-acceleration (meters 3)
    :maximum-rotation-rate (degrees 360)
    :notice-nav-radius (meters 1.5)
    :frustration-distance (meters 8)
    :frustration-time (seconds 4)
    :blocked-time (seconds 0.3)
    :circle-dist-lo 20480.0
    :circle-dist-hi 61440.0
    :nav-mesh #f
    )
  )

(set! (-> *ginsu-nav-enemy-info* fact-defaults) *fact-info-enemy-defaults*)

(defmethod ginsu-method-180 ((this ginsu))
  (let ((v1-2 (if (> (-> this skel active-channels) 0)
                  (-> this skel root-channel 0 frame-group)
                  )
              )
        )
    (when (not (and v1-2 (or (= v1-2 (-> this draw art-group data 11)) (= v1-2 (-> this draw art-group data 12)))))
      (update! (-> this blade-speed) 0.0)
      (let ((f0-1 (+ (-> this blade-angle) (* 970903.75 (-> this blade-speed value)))))
        (set! (-> this blade-angle) (- f0-1 (* (the float (the int (/ f0-1 65536.0))) 65536.0)))
        )
      (quaternion-axis-angle! (-> this blade-jm quat) 0.0 1.0 0.0 (-> this blade-angle))
      (let ((s5-0 (new 'stack-no-clear 'matrix)))
        (let* ((a2-1 (-> this node-list data 8 bone transform))
               (v1-14 (-> a2-1 quad 0))
               (a0-10 (-> a2-1 quad 1))
               (a1-3 (-> a2-1 quad 2))
               (a2-2 (-> a2-1 trans quad))
               )
          (set! (-> s5-0 quad 0) v1-14)
          (set! (-> s5-0 quad 1) a0-10)
          (set! (-> s5-0 quad 2) a1-3)
          (set! (-> s5-0 trans quad) a2-2)
          )
        (when (logtest? (enemy-flag drawn-mirrored) (-> this enemy-flags))
          (vector-negate! (-> s5-0 vector 0) (-> s5-0 vector 0))
          (set! (-> s5-0 vector 0 w) 0.0)
          )
        (spawn-with-matrix (-> this blade-part) s5-0)
        )
      )
    )
  0
  (none)
  )

(defmethod ginsu-method-181 ((this ginsu) (arg0 vector))
  (vector-reset! arg0)
  (let ((a0-2 (handle->process (-> this focus handle))))
    (if a0-2
        (vector-! arg0 (get-trans (the-as process-focusable a0-2) 0) (-> this root trans))
        )
    )
  arg0
  )

;; WARN: Return type mismatch symbol vs none.
(defmethod common-post ((this ginsu))
  "Does a lot of various things relating to interacting with the target
   - tracks when the enemy was last drawn
   - looks at the target and handles attacking
   @TODO Not extremely well understood yet"
  (ginsu-method-180 this)
  (let ((t9-1 (method-of-type nav-enemy common-post)))
    (t9-1 this)
    )
  (cond
    ((not (and (-> this next-state) (let ((v1-6 (-> this next-state name)))
                                      (or (= v1-6 'idle)
                                          (= v1-6 'active)
                                          (= v1-6 'dormant)
                                          (= v1-6 'ambush)
                                          (= v1-6 'knocked)
                                          (= v1-6 'idle)
                                          (= v1-6 'dormant)
                                          (= v1-6 'die)
                                          (= v1-6 'die-falling)
                                          )
                                      )
               )
          )
     (sound-play "ginsu-loop" :id (-> this blade-sound) :position (-> this root trans))
     (set! (-> this blade-sound-playing) #t)
     )
    ((-> this blade-sound-playing)
     (sound-stop (-> this blade-sound))
     (set! (-> this blade-sound-playing) #f)
     )
    )
  (cond
    ((and (not (and (-> this next-state) (let ((v1-17 (-> this next-state name)))
                                           (or (= v1-17 'idle)
                                               (= v1-17 'active)
                                               (= v1-17 'dormant)
                                               (= v1-17 'ambush)
                                               (= v1-17 'knocked)
                                               (= v1-17 'idle)
                                               (= v1-17 'dormant)
                                               (= v1-17 'die)
                                               (= v1-17 'die-falling)
                                               )
                                           )
                    )
               )
          (< (- (current-time) (-> this grind-timer)) 0)
          )
     (sound-play "ginsu-grind" :id (-> this grind-sound) :position (-> this root trans))
     (set! (-> this grind-sound-playing) #t)
     )
    ((-> this grind-sound-playing)
     (sound-stop (-> this grind-sound))
     (set! (-> this grind-sound-playing) #f)
     )
    )
  (none)
  )

(defmethod ginsu-method-182 ((this ginsu))
  (local-vars (s5-1 art-element))
  (let ((s5-0 (new 'stack-no-clear 'vector))
        (s4-0 (new 'stack-no-clear 'matrix))
        )
    (if (> (-> this skel active-channels) 0)
        (-> this skel root-channel 0 frame-group)
        )
    (let* ((v1-8 (if (> (-> this skel active-channels) 0)
                     (-> this skel root-channel 0 frame-group)
                     )
                 )
           (f30-0 (if (and v1-8 (= v1-8 (-> this draw art-group data 7)))
                      (cos 9102.223)
                      (cos 7281.778)
                      )
                  )
           )
      (let ((a1-0 (-> this nav state)))
        (set! (-> s5-0 quad) (-> a1-0 velocity quad))
        )
      (quaternion->matrix s4-0 (-> this root quat))
      (let ((f0-1 (vector-dot s5-0 (-> s4-0 vector 0))))
        (if (logtest? (enemy-flag drawn-mirrored) (-> this enemy-flags))
            (set! f0-1 (- f0-1))
            )
        (cond
          ((< (- f30-0) f0-1)
           (set! s5-1 (-> this draw art-group data 9))
           )
          ((< f0-1 f30-0)
           (set! s5-1 (-> this draw art-group data 8))
           )
          (else
            (set! s5-1 (-> this draw art-group data 7))
            )
          )
        )
      )
    )
  (let ((v1-33 (if (> (-> this skel active-channels) 0)
                   (-> this skel root-channel 0 frame-group)
                   )
               )
        )
    (cond
      ((and v1-33 (= v1-33 s5-1))
       (let ((a0-11 (-> this skel root-channel 0)))
         (set! (-> a0-11 param 0) 1.0)
         (joint-control-channel-group-eval! a0-11 (the-as art-joint-anim #f) num-func-loop!)
         )
       )
      (else
        (let ((f30-2 (/ (ja-frame-num 0) (the float (ja-num-frames 0)))))
          (ja-channel-push! 1 (seconds 0.5))
          (set! (-> this skel root-channel 0 frame-group) (the-as art-joint-anim s5-1))
          (let ((gp-1 (-> this skel root-channel 0)))
            (set! (-> gp-1 num-func) num-func-identity)
            (set! (-> gp-1 frame-num) (* f30-2 (the float (ja-num-frames 0))))
            )
          )
        )
      )
    )
  0
  (none)
  )

(defmethod nav-enemy-method-142 ((this ginsu) (arg0 nav-control))
  (let ((s3-0 (new 'stack-no-clear 'vector)))
    (let ((a0-2 (handle->process (-> this focus handle))))
      (cond
        (a0-2
          (vector-! s3-0 (get-trans (the-as process-focusable a0-2) 0) (-> this root trans))
          )
        (else
          (let ((a0-7 (-> arg0 state)))
            (set! (-> s3-0 quad) (-> a0-7 heading quad))
            )
          )
        )
      )
    (set! (-> s3-0 y) 0.0)
    (vector-normalize! s3-0 1.0)
    (let ((s5-3 (new 'stack-no-clear 'quaternion))
          (s4-0 (-> this root quat))
          )
      (quaternion-set! s5-3 0.0 (sqrtf (* 0.5 (- 1.0 (-> s3-0 z)))) 0.0 (sqrtf (* 0.5 (+ 1.0 (-> s3-0 z)))))
      (if (< (-> s3-0 x) 0.0)
          (set! (-> s5-3 y) (- (-> s5-3 y)))
          )
      (quaternion-pseudo-seek
        s4-0
        s4-0
        s5-3
        (* (fmax 0.5 (/ (-> this nav state speed) METER_LENGTH)) (seconds-per-frame))
        )
      )
    )
  0
  (none)
  )

;; WARN: disable def twice: 11. This may happen when a cond (no else) is nested inside of another conditional, but it should be rare.
(defmethod general-event-handler ((this ginsu) (arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
  "Handles various events for the enemy
   @TODO - unsure if there is a pattern for the events and this should have a more specific name"
  (local-vars (v0-1 object))
  (case arg2
    (('touched)
     (when (the-as process (as-type arg0 ginsu))
       (let ((s5-1 (vector<-cspace! (new 'stack-no-clear 'vector) (-> this node-list data 8))))
         (let ((a0-5 (vector<-cspace! (new 'stack-no-clear 'vector) (-> this node-list data 8))))
           (vector+! s5-1 s5-1 a0-5)
           )
         (vector-float*! s5-1 s5-1 0.5)
         (spawn (-> this part) s5-1)
         )
       (set! v0-1 (+ (current-time) (seconds 0.125)))
       (set! (-> this grind-timer) (the-as time-frame v0-1))
       v0-1
       )
     )
    (('touch 'bonk 'attack)
     (cond
       ((or (!= arg0 *target*) (focus-test? *target* dark))
        ((method-of-type nav-enemy general-event-handler) this arg0 arg1 arg2 arg3)
        )
       (else
         (send-event arg0 'attack #f (static-attack-info ((id (new-attack-id)))))
         #f
         )
       )
     )
    (('trigger)
     (set! v0-1 #t)
     (set! (-> this ambush-started) (the-as symbol v0-1))
     v0-1
     )
    (else
      ((method-of-type nav-enemy general-event-handler) this arg0 arg1 arg2 arg3)
      )
    )
  )

(defstate circling (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy circling) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (if (< 0.5 (rand-vu))
        (set! (-> self enemy-flags) (the-as enemy-flag (logxor (shl 256 32) (the-as int (-> self enemy-flags)))))
        )
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy circling) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (ginsu-method-182 self)
    )
  :code sleep-code
  )

(defstate hostile (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy hostile) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (if (< 0.5 (rand-vu))
        (set! (-> self enemy-flags) (the-as enemy-flag (logxor (shl 256 32) (the-as int (-> self enemy-flags)))))
        )
    (set! (-> self enemy-flags) (the-as enemy-flag (logclear (-> self enemy-flags) (enemy-flag enemy-flag41))))
    (set-time! (-> self starting-time))
    (set-time! (-> self spiral-time))
    (set! (-> self desired-distance) (vector-length (ginsu-method-181 self (new 'stack-no-clear 'vector))))
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy hostile) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (if (and (< (vector-length (ginsu-method-181 self (new 'stack-no-clear 'vector))) 16384.0)
             (logtest? (-> self draw status) (draw-control-status on-screen))
             )
        (go-virtual anticipate-attack)
        )
    (if (nav-enemy-method-163 self)
        (go-stare2 self)
        )
    (if (logtest? (enemy-flag enemy-flag41) (-> self enemy-flags))
        (set! (-> self enemy-flags) (the-as enemy-flag (logxor (shl 256 32) (the-as int (-> self enemy-flags)))))
        )
    (ginsu-method-182 self)
    )
  :code sleep-code
  :post (behavior ()
    (let ((s5-0 (ginsu-method-181 self (new 'stack-no-clear 'vector)))
          (gp-0 (new-stack-vector0))
          )
      0.0
      0.0
      (set! (-> s5-0 y) 0.0)
      (let* ((f0-3 (vector-normalize-ret-len! s5-0 1.0))
             (f1-1 (- 1.0 (* 0.0016666667 (the float (- (current-time) (-> self spiral-time))))))
             (f2-3 (fmax 12288.0 (* 102400.0 f1-1)))
             (f0-4 (- f0-3 (fmin (fmax 12288.0 (* 122880.0 f1-1)) (fmax f2-3 (-> self desired-distance)))))
             (f30-0 (fmin 8192.0 (fmax -8192.0 f0-4)))
             )
        (set! (-> gp-0 x) (- (-> s5-0 z)))
        (set! (-> gp-0 z) (-> s5-0 x))
        (if (logtest? (enemy-flag enemy-flag40) (-> self enemy-flags))
            (vector-negate! gp-0 gp-0)
            )
        (vector+float*! gp-0 (-> self root trans) gp-0 20480.0)
        (vector+float*! gp-0 gp-0 s5-0 f30-0)
        )
      (cloest-point-on-mesh (-> self nav) gp-0 gp-0 (the-as nav-poly #f))
      (if (< (vector-vector-xz-distance gp-0 (-> self root trans)) 409.6)
          (set! (-> self enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag41) (-> self enemy-flags))))
          (set! (-> self enemy-flags) (the-as enemy-flag (logclear (-> self enemy-flags) (enemy-flag enemy-flag41))))
          )
      (let ((v1-32 (-> self nav state)))
        (logclear! (-> v1-32 flags) (nav-state-flag directional-mode))
        (logior! (-> v1-32 flags) (nav-state-flag target-poly-dirty))
        (set! (-> v1-32 target-post quad) (-> gp-0 quad))
        )
      )
    0
    (nav-enemy-method-176 self)
    )
  )

(defstate anticipate-attack (ginsu)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (logior! (-> self enemy-flags) (enemy-flag lock-focus))
    (logior! (-> self focus-status) (focus-status dangerous))
    (let ((v1-4 (-> self nav)))
      (set! (-> v1-4 target-speed) 8192.0)
      )
    0
    (set-time! (-> self state-time))
    (ja-channel-push! 1 (seconds 0.2))
    (ja :group! ginsu-idle-ja :num! min)
    )
  :exit (behavior ()
    (logclear! (-> self enemy-flags) (enemy-flag lock-focus))
    (if (logtest? (-> self enemy-flags) (enemy-flag dangerous-backup))
        (logior! (-> self focus-status) (focus-status dangerous))
        (logclear! (-> self focus-status) (focus-status dangerous))
        )
    )
  :trans (behavior ()
    (if (not (and (handle->process (-> self focus handle))
                  (not (logtest? (-> (the-as process-focusable (handle->process (-> self focus handle))) focus-status)
                                 (focus-status disable dead ignore grabbed)
                                 )
                       )
                  )
             )
        (go-virtual circling)
        )
    (if (logtest? (-> self enemy-flags) (enemy-flag victory))
        (go-virtual circling)
        )
    (if (time-elapsed? (-> self state-time) (seconds 0.75))
        (go-virtual attack)
        )
    (ja :num! (loop!))
    )
  :code sleep-code
  :post (behavior ()
    (let ((gp-0 (ginsu-method-181 self (new 'stack-no-clear 'vector))))
      (set! (-> gp-0 y) 0.0)
      (vector-normalize! gp-0 -819.2)
      (vector+! gp-0 gp-0 (-> self root trans))
      (cloest-point-on-mesh (-> self nav) gp-0 gp-0 (the-as nav-poly #f))
      (if (< (vector-vector-xz-distance gp-0 (-> self root trans)) 409.6)
          (set! (-> self enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag41) (-> self enemy-flags))))
          (set! (-> self enemy-flags) (the-as enemy-flag (logclear (-> self enemy-flags) (enemy-flag enemy-flag41))))
          )
      (let ((v1-12 (-> self nav state)))
        (logclear! (-> v1-12 flags) (nav-state-flag directional-mode))
        (logior! (-> v1-12 flags) (nav-state-flag target-poly-dirty))
        (set! (-> v1-12 target-post quad) (-> gp-0 quad))
        )
      )
    0
    (nav-enemy-method-176 self)
    )
  )

(defstate attack (ginsu)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (logior! (-> self enemy-flags) (enemy-flag lock-focus))
    (ginsu-method-183 self #t)
    (set! (-> self root penetrate-using) (penetrate generic-attack lunge))
    (enemy-method-49 self)
    (logior! (-> self focus-status) (focus-status dangerous))
    (let* ((v1-9 *game-info*)
           (a0-5 (+ (-> v1-9 attack-id) 1))
           )
      (set! (-> v1-9 attack-id) a0-5)
      (set! (-> self attack-id) a0-5)
      )
    (let ((v1-10 (-> self nav)))
      (set! (-> v1-10 target-speed) 49152.0)
      )
    0
    (set-time! (-> self state-time))
    (ja :group! ginsu-attack-ja :num! min)
    )
  :exit (behavior ()
    (logclear! (-> self enemy-flags) (enemy-flag lock-focus))
    (if (logtest? (-> self enemy-flags) (enemy-flag dangerous-backup))
        (logior! (-> self focus-status) (focus-status dangerous))
        (logclear! (-> self focus-status) (focus-status dangerous))
        )
    (ginsu-method-183 self #f)
    )
  :trans (behavior ()
    (enemy-method-49 self)
    (if (not (and (handle->process (-> self focus handle))
                  (not (logtest? (-> (the-as process-focusable (handle->process (-> self focus handle))) focus-status)
                                 (focus-status disable dead ignore grabbed)
                                 )
                       )
                  )
             )
        (go-virtual circling)
        )
    (if (logtest? (-> self enemy-flags) (enemy-flag victory))
        (go-virtual circling)
        )
    (ja :num! (seek!))
    (if (ja-done? 0)
        (go-virtual circling)
        )
    )
  :code sleep-code
  :post nav-enemy-chase-post
  )

(defstate ambush (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type enemy ambush) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (logclear! (-> self mask) (process-mask actor-pause))
    (logior! (-> self enemy-flags) (enemy-flag cam-attack-mode))
    (ja-channel-set! 0)
    (set! (-> self root scale x) 0.01)
    (set! (-> self root scale y) 0.01)
    (set! (-> self root scale z) 0.01)
    )
  :exit (behavior ()
    (logior! (-> self mask) (process-mask actor-pause))
    (logior! (-> self enemy-flags) (enemy-flag actor-pause-backup))
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type enemy ambush) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (when (-> self ambush-started)
      (when (zero? (-> self skel active-channels))
        (ja-channel-push! 1 0)
        (ja :group! ginsu-idle-ja :num! min)
        )
      (vector-seek! (-> self root scale) (new 'static 'vector :x 1.0 :y 1.0 :z 1.0) 0.02)
      ;; og:preserve-this high fps fix
      (+! (-> self path-pos) (/ (* DISPLAY_FPS_RATIO 2048.0) (total-distance (-> self ambush-path))))
      (let ((f30-1 (-> self path-pos)))
        (when (>= f30-1 1.0)
          (set! (-> self path-pos) 1.0)
          (go-hostile self)
          )
        (get-point-at-percent-along-path! (-> self ambush-path) (-> self root trans) f30-1 'interp)
        )
      (ja :num! (loop!))
      )
    )
  :code sleep-code
  :post enemy-simple-post
  )

(defbehavior ginsu-sounds-off ginsu ()
  (if (-> self blade-sound-playing)
      (sound-stop (-> self blade-sound))
      )
  (when (-> self grind-sound-playing)
    (sound-stop (-> self grind-sound))
    (set! (-> self grind-sound-playing) #f)
    (set! (-> self grind-timer) 0)
    0
    )
  (none)
  )

(defstate idle (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy idle) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (ginsu-sounds-off)
    )
  )

(defstate dormant (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy dormant) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (ginsu-sounds-off)
    )
  )

(defstate die (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy die) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (ginsu-sounds-off)
    )
  )

(defstate die-falling (ginsu)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy die-falling) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (ginsu-sounds-off)
    )
  )

(defmethod go-hostile ((this ginsu))
  (cond
    ((or (and (-> this enemy-info use-frustration) (logtest? (enemy-flag not-frustrated) (-> this enemy-flags)))
         (nav-enemy-method-163 this)
         )
     (go-stare2 this)
     )
    (else
      (let* ((v1-8 (/ (the-as int (rand-uint31-gen *random-generator*)) 256))
             (v1-9 (the-as number (logior (the-as int #x3f800000) v1-8)))
             )
        (if (and (< (+ -1.0 (the-as float v1-9)) 0.1) (logtest? (-> this draw status) (draw-control-status on-screen)))
            (go (method-of-object this anticipate-attack))
            (go (method-of-object this hostile))
            )
        )
      )
    )
  )

;; WARN: Return type mismatch object vs none.
(defmethod go-idle ((this ginsu))
  (if (not (logtest? (-> this ambush-path flags) (path-control-flag not-found)))
      (go (method-of-object this ambush))
      (go (method-of-object this idle))
      )
  (none)
  )

(defmethod deactivate ((this ginsu))
  "Make a process dead, clean it up, remove it from the active pool, and return to dead pool."
  (if (nonzero? (-> this blade-part))
      (kill-and-free-particles (-> this blade-part))
      )
  (if (-> this blade-sound-playing)
      (sound-stop (-> this blade-sound))
      )
  (if (-> this grind-sound-playing)
      (sound-stop (-> this grind-sound))
      )
  ((method-of-type nav-enemy deactivate) this)
  (none)
  )

;; WARN: Return type mismatch nav-enemy vs ginsu.
(defmethod relocate ((this ginsu) (offset int))
  (if (nonzero? (-> this blade-jm))
      (&+! (-> this blade-jm) offset)
      )
  (if (nonzero? (-> this blade-part))
      (&+! (-> this blade-part) offset)
      )
  (if (nonzero? (-> this ambush-path))
      (&+! (-> this ambush-path) offset)
      )
  (the-as ginsu ((method-of-type nav-enemy relocate) this offset))
  )

(defmethod ginsu-method-183 ((this ginsu) (arg0 symbol))
  (let ((v1-3 (-> (the-as collide-shape-prim-group (-> this root root-prim)) child 1)))
    (cond
      (arg0
        (set! (-> v1-3 prim-core collide-as) (collide-spec enemy))
        (set! (-> v1-3 prim-core collide-with) (collide-spec jak bot))
        )
      (else
        (set! (-> v1-3 prim-core collide-as) (collide-spec))
        (set! (-> v1-3 prim-core collide-with) (collide-spec))
        0
        )
      )
    )
  (none)
  )

(defmethod enemy-method-46 ((this ginsu) (arg0 int))
  "@abstract"
  (case arg0
    ((1)
     (let ((v1-2 (-> this root root-prim)))
       (logior! (-> v1-2 prim-core action) (collide-action solid))
       (let ((v1-4 (-> (the-as collide-shape-prim-group v1-2) child 0)))
         (logior! (-> v1-4 prim-core action) (collide-action solid))
         )
       )
     )
    )
  0
  (none)
  )

(defmethod init-enemy-collision! ((this ginsu))
  "Initializes the [[collide-shape-moving]] and any ancillary tasks to make the enemy collide properly"
  (let ((s5-0 (new 'process 'collide-shape-moving this (collide-list-enum usually-hit-by-player))))
    (set! (-> s5-0 dynam) (copy *standard-dynamics* 'process))
    (set! (-> s5-0 reaction) cshape-reaction-default)
    (set! (-> s5-0 no-reaction)
          (the-as (function collide-shape-moving collide-query vector vector object) nothing)
          )
    (set! (-> s5-0 penetrated-by) (penetrate
                                    generic-attack
                                    lunge
                                    flop
                                    punch
                                    spin
                                    roll
                                    uppercut
                                    bonk
                                    tube
                                    vehicle
                                    flut-attack
                                    board
                                    mech-punch
                                    dark-punch
                                    dark-giant
                                    )
          )
    (let ((s4-0 (new 'process 'collide-shape-prim-group s5-0 (the-as uint 2) 0)))
      (set! (-> s5-0 total-prims) (the-as uint 3))
      (set! (-> s4-0 prim-core collide-as) (collide-spec enemy))
      (set! (-> s4-0 prim-core collide-with)
            (collide-spec backgnd jak bot crate enemy obstacle hit-by-others-list player-list)
            )
      (set! (-> s4-0 prim-core action) (collide-action deadly no-standon))
      (set! (-> s4-0 transform-index) 8)
      (set-vector! (-> s4-0 local-sphere) 0.0 0.0 0.0 9216.0)
      (set! (-> s5-0 root-prim) s4-0)
      )
    (let ((v1-14 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-14 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-14 prim-core collide-with)
            (collide-spec backgnd jak bot crate enemy obstacle hit-by-others-list player-list)
            )
      (set! (-> v1-14 prim-core action) (collide-action no-standon))
      (set! (-> v1-14 transform-index) 8)
      (set-vector! (-> v1-14 local-sphere) 0.0 0.0 0.0 4915.2)
      )
    (let ((v1-16 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-16 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-16 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-16 prim-core action) (collide-action deadly))
      (set! (-> v1-16 transform-index) 7)
      (set-vector! (-> v1-16 local-sphere) 0.0 0.0 0.0 2662.4)
      )
    (set! (-> s5-0 nav-radius) 4505.6)
    (let ((v1-18 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-18 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-18 prim-core collide-with))
      )
    (set! (-> s5-0 max-iteration-count) (the-as uint 3))
    (set! (-> s5-0 event-self) 'touched)
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod init-enemy! ((this ginsu))
  "Common method called to initialize the enemy, typically sets up default field values and calls ancillary helper methods"
  (stack-size-set! (-> this main-thread) 256)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-ginsu" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (set! (-> this skel generate-frame-function) create-interpolated2-joint-animation-frame)
  (init-enemy-behaviour-and-stats! this *ginsu-nav-enemy-info*)
  (logclear! (-> this nav flags) (nav-control-flag update-heading-from-facing))
  (set! (-> this enemy-flags) (the-as enemy-flag (logclear (-> this enemy-flags) (enemy-flag enemy-flag43))))
  (logclear! (-> this nav flags) (nav-control-flag limit-rotation-rate))
  (let ((v1-14 (-> this neck)))
    (set! (-> v1-14 up) (the-as uint 1))
    (set! (-> v1-14 nose) (the-as uint 2))
    (set! (-> v1-14 ear) (the-as uint 0))
    (set-vector! (-> v1-14 twist-max) 10922.667 7281.778 0.0 1.0)
    (set! (-> v1-14 ignore-angle) 18204.445)
    )
  (let ((v1-16 (-> this nav)))
    (set! (-> v1-16 speed-scale) 1.0)
    )
  0
  (set-gravity-length (-> this root dynam) 573440.0)
  (ginsu-method-183 this #f)
  (set! (-> this blade-jm) (new 'process 'joint-mod (joint-mod-mode joint-set*) this 8))
  (init (-> this blade-speed) 1.0 0.01 0.1 0.9)
  (set! (-> this part) (create-launch-control group-ginsu-sparks this))
  (set! (-> this blade-part) (create-launch-control group-ginsu-blade this))
  (set! (-> this ambush-path) (new 'process 'curve-control this 'intro -1000000000.0))
  (set! (-> this ambush-started) #f)
  (set! (-> this path-pos) 0.0)
  (if (not (logtest? (-> this ambush-path flags) (path-control-flag not-found)))
      (logior! (-> this ambush-path flags) (path-control-flag display draw-line draw-point draw-text))
      )
  (set! (-> this blade-sound) (new-sound-id))
  (set! (-> this blade-sound-playing) #f)
  (set! (-> this grind-sound) (new-sound-id))
  (set! (-> this grind-sound-playing) #f)
  (set! (-> this grind-timer) 0)
  0
  (none)
  )
