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

;; name: metalmonk.gc
;; name in dgo: metalmonk
;; dgos: DG1, MTN, ATE, D3A

;; DECOMP BEGINS

(defskelgroup skel-metalmonk metalmonk metalmonk-lod0-jg -1
              ((metalmonk-lod0-mg (meters 20)) (metalmonk-lod1-mg (meters 40)) (metalmonk-lod2-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5)
              :shadow metalmonk-shadow-mg
              :origin-joint-index 14
              )

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


(deftype metalmonk-global-info (basic)
  ((prev-yellow-hit  int8)
   (prev-blue-hit    int8)
   (idle-anim        int32  3)
   (patrol-anim      int32  2)
   (notice-anim      int32  2)
   (charge-anim      int32  2)
   (attack-anim      int32  2)
   (knocked-anim     int32  2)
   (celebrate-anim   int32  2)
   (yellow-hit-anim  int32  4)
   (blue-hit-anim    int32  3)
   )
  :pack-me
  )


(deftype metalmonk (nav-enemy)
  ((new-facing  vector      :inline :offset 624)
   (old-facing  vector      :inline :offset 640)
   (high-time   time-frame          :offset 656)
   (intro-path  path-control)
   )
  (:state-methods
    attack
    )
  (:methods
    (metalmonk-method-179 (_type_ vector) none)
    (metalmonk-method-180 (_type_ symbol) none)
    (metalmonk-method-181 (_type_ float float) none)
    (metalmonk-method-182 (_type_ float float) none)
    )
  )


(define *metalmonk-global-info* (new 'static 'metalmonk-global-info
                                  :idle-anim (new 'static 'array int32 3 5 6 7)
                                  :patrol-anim (new 'static 'array int32 2 8 9)
                                  :notice-anim (new 'static 'array int32 2 10 11)
                                  :charge-anim (new 'static 'array int32 2 12 13)
                                  :attack-anim (new 'static 'array int32 2 14 15)
                                  :knocked-anim (new 'static 'array int32 2 16 18)
                                  :celebrate-anim (new 'static 'array int32 2 22 23)
                                  :yellow-hit-anim (new 'static 'array int32 4 28 29 30 31)
                                  :blue-hit-anim (new 'static 'array int32 3 24 25 26)
                                  )
        )

(define *metalmonk-nav-enemy-info*
  (new 'static 'nav-enemy-info
    :use-die-falling #t
    :use-victory #t
    :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 12
    :hit-anim -1
    :knocked-anim 16
    :knocked-land-anim 17
    :die-anim 32
    :die-falling-anim 33
    :victory-anim 22
    :jump-wind-up-anim 12
    :jump-in-air-anim 12
    :jump-land-anim 12
    :neck-joint 7
    :look-at-joint 7
    :bullseye-joint 4
    :sound-hit (static-sound-name "metalmonk-hit")
    :sound-die (static-sound-name "metalmonk-die")
    :notice-distance (meters 40)
    :notice-distance-delta (meters 10)
    :proximity-notice-distance (meters 40)
    :default-hit-points 4
    :gnd-collide-with (collide-spec backgnd)
    :overlaps-others-collide-with-filter (collide-spec jak bot player-list)
    :penetrate-knocked (penetrate
      touch
      generic-attack
      lunge
      flop
      punch
      spin
      roll
      uppercut
      bonk
      tube
      vehicle
      flut-attack
      board
      mech
      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
      eco-red
      eco-blue
      eco-green
      knocked
      penetrate-33
      penetrate-34
      penetrate-35
      penetrate-36
      penetrate-37
      penetrate-38
      penetrate-39
      penetrate-40
      penetrate-41
      penetrate-42
      penetrate-43
      penetrate-44
      penetrate-45
      penetrate-46
      penetrate-47
      penetrate-48
      penetrate-49
      penetrate-50
      penetrate-51
      penetrate-52
      penetrate-53
      penetrate-54
      penetrate-55
      penetrate-56
      penetrate-57
      penetrate-58
      penetrate-59
      penetrate-60
      penetrate-61
      penetrate-62
      penetrate-63
      )
    :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 6371.5557
    :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 #t
    :use-frustration #t
    :use-stop-chase #f
    :use-circling #t
    :use-pacing #t
    :walk-anim 8
    :turn-anim -1
    :run-anim 12
    :taunt-anim 22
    :run-travel-speed (meters 7)
    :run-acceleration (meters 7)
    :run-turning-acceleration (meters 20)
    :walk-travel-speed (meters 2)
    :walk-acceleration (meters 3)
    :walk-turning-acceleration (meters 3)
    :maximum-rotation-rate (degrees 360)
    :notice-nav-radius (meters 1)
    :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! (-> *metalmonk-nav-enemy-info* fact-defaults) *fact-info-enemy-defaults*)

;; WARN: Return type mismatch vector vs none.
(defmethod metalmonk-method-179 ((this metalmonk) (arg0 vector))
  (set! (-> arg0 quad)
        (-> (quaternion->matrix (new 'stack-no-clear 'matrix) (-> this root quat)) vector 2 quad)
        )
  (none)
  )

(defmethod metalmonk-method-181 ((this metalmonk) (arg0 float) (arg1 float))
  (let ((f28-0 (vector-dot (-> this new-facing) (-> this old-facing)))
        (f30-1 (/ (ja-frame-num 0) (the float (ja-num-frames 0))))
        )
    (when (and (< f28-0 (cos (* 182.04445 arg0))) (let ((v1-7 (if (> (-> this skel active-channels) 0)
                                                                  (-> this skel root-channel 0 frame-group)
                                                                  )
                                                              )
                                                        )
                                                    (not (and v1-7 (= v1-7 (-> this draw art-group data 13))))
                                                    )
               )
      (ja-channel-push! 1 (seconds 0.16))
      (let ((v1-13 (-> this skel root-channel 0)))
        (set! (-> v1-13 frame-group) (the-as art-joint-anim (-> this draw art-group data 13)))
        )
      (let ((s4-1 (-> this skel root-channel 0)))
        (set! (-> s4-1 num-func) num-func-identity)
        (set! (-> s4-1 frame-num) (* f30-1 (the float (ja-num-frames 0))))
        )
      (set-time! (-> this high-time))
      )
    (let ((v1-22 (if (> (-> this skel active-channels) 0)
                     (-> this skel root-channel 0 frame-group)
                     )
                 )
          )
      (when (or (not (and v1-22 (or (= v1-22 (-> this draw art-group data 12)) (= v1-22 (-> this draw art-group data 13)))))
                (let ((v1-28 (if (> (-> this skel active-channels) 0)
                                 (-> this skel root-channel 0 frame-group)
                                 )
                             )
                      )
                  (and (and v1-28 (= v1-28 (-> this draw art-group data 13)))
                       (< (ja-frame-num 0) 3.0)
                       (and (< (cos (* 182.04445 arg1)) f28-0) (time-elapsed? (-> this high-time) (seconds 1)))
                       )
                  )
                )
        (ja-channel-push! 1 (seconds 0.16))
        (let ((v1-41 (-> this skel root-channel 0)))
          (set! (-> v1-41 frame-group) (the-as art-joint-anim (-> this draw art-group data 12)))
          )
        (let ((gp-1 (-> this skel root-channel 0)))
          (set! (-> gp-1 num-func) num-func-identity)
          (set! (-> gp-1 frame-num) (* f30-1 (the float (ja-num-frames 0))))
          )
        )
      )
    )
  0
  (none)
  )

(defmethod metalmonk-method-182 ((this metalmonk) (arg0 float) (arg1 float))
  (let ((f28-0 (vector-dot (-> this new-facing) (-> this old-facing)))
        (f30-1 (/ (ja-frame-num 0) (the float (ja-num-frames 0))))
        )
    (when (and (< f28-0 (cos (* 182.04445 arg0))) (let ((v1-7 (if (> (-> this skel active-channels) 0)
                                                                  (-> this skel root-channel 0 frame-group)
                                                                  )
                                                              )
                                                        )
                                                    (not (and v1-7 (= v1-7 (-> this draw art-group data 9))))
                                                    )
               )
      (ja-channel-push! 1 (seconds 0.16))
      (let ((v1-13 (-> this skel root-channel 0)))
        (set! (-> v1-13 frame-group) (the-as art-joint-anim (-> this draw art-group data 9)))
        )
      (let ((s4-1 (-> this skel root-channel 0)))
        (set! (-> s4-1 num-func) num-func-identity)
        (set! (-> s4-1 frame-num) (* f30-1 (the float (ja-num-frames 0))))
        )
      (set-time! (-> this high-time))
      )
    (let ((v1-22 (if (> (-> this skel active-channels) 0)
                     (-> this skel root-channel 0 frame-group)
                     )
                 )
          )
      (when (or (not (and v1-22 (or (= v1-22 (-> this draw art-group data 8)) (= v1-22 (-> this draw art-group data 9)))))
                (let ((v1-28 (if (> (-> this skel active-channels) 0)
                                 (-> this skel root-channel 0 frame-group)
                                 )
                             )
                      )
                  (and (and v1-28 (= v1-28 (-> this draw art-group data 9)))
                       (and (< (cos (* 182.04445 arg1)) f28-0) (time-elapsed? (-> this high-time) (seconds 1)))
                       )
                  )
                )
        (ja-channel-push! 1 (seconds 0.5))
        (let ((v1-39 (-> this skel root-channel 0)))
          (set! (-> v1-39 frame-group) (the-as art-joint-anim (-> this draw art-group data 8)))
          )
        (let ((gp-1 (-> this skel root-channel 0)))
          (set! (-> gp-1 num-func) num-func-identity)
          (set! (-> gp-1 frame-num) (* f30-1 (the float (ja-num-frames 0))))
          )
        )
      )
    )
  0
  (none)
  )

(defmethod common-post ((this metalmonk))
  "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"
  (let ((t9-0 (method-of-type nav-enemy common-post)))
    (t9-0 this)
    )
  (set! (-> this old-facing quad) (-> this new-facing quad))
  (metalmonk-method-179 this (-> this new-facing))
  0
  (none)
  )

(defmethod general-event-handler ((this metalmonk) (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"
  (case arg2
    (('hit 'hit-knocked 'hit-flinch)
     (logclear! (-> this mask) (process-mask actor-pause))
     (logclear! (-> this focus-status) (focus-status dangerous))
     (logclear! (-> this enemy-flags) (enemy-flag use-notice-distance))
     (logior! (-> this enemy-flags) (enemy-flag alert))
     (logior! (-> this focus-status) (focus-status hit))
     (if (zero? (-> this hit-points))
         (logior! (-> this focus-status) (focus-status dead))
         )
     (logclear! (-> this enemy-flags) (enemy-flag lock-focus))
     (enemy-method-62 this)
     (logior! (-> this enemy-flags) (enemy-flag lock-focus))
     (process-contact-action arg0)
     (send-event arg0 'get-attack-count 1)
     (cond
       ((zero? (-> this hit-points))
        (let ((s5-1 (-> this incoming knocked-type)))
          (cond
            ((and (= s5-1 (knocked-type knocked-type-4))
                  (not (and (-> this next-state) (let ((v1-33 (-> this next-state name)))
                                                   (or (= v1-33 'knocked) (= v1-33 'jump) (= v1-33 'jump-land))
                                                   )
                            )
                       )
                  (zero? (rnd-int-count this 3))
                  (let ((f0-0 (vector-vector-distance-squared (-> this root trans) (target-pos 0)))
                        (f1-0 32768.0)
                        )
                    (>= f0-0 (* f1-0 f1-0))
                    )
                  )
             (kill-prefer-falling this)
             )
            ((or (= s5-1 (knocked-type knocked-type-4)) (= s5-1 (knocked-type knocked-type-6)))
             (set! (-> this incoming knocked-type) (knocked-type knocked-type-0))
             (go (method-of-object this knocked))
             )
            (else
              (go (method-of-object this knocked))
              )
            )
          )
        )
       (else
         (go (method-of-object this knocked))
         )
       )
     #t
     )
    (else
      ((method-of-type nav-enemy general-event-handler) this arg0 arg1 arg2 arg3)
      )
    )
  )

(defmethod enemy-method-104 ((this metalmonk) (arg0 process) (arg1 touching-shapes-entry) (arg2 uint))
  (let* ((s3-0 arg0)
         (a0-2 (if (type? s3-0 process-focusable)
                   s3-0
                   )
               )
         (s3-1 (new 'stack-no-clear 'vector))
         )
    (let ((s1-0 (new 'stack-no-clear 'vector)))
      (vector-! s3-1 (get-trans (the-as process-focusable a0-2) 0) (-> this root trans))
      (set! (-> s3-1 y) 0.0)
      (vector-normalize! s3-1 1.0)
      (set-vector! s1-0 0.0 1.0 0.0 1.0)
      (vector-cross! s1-0 s1-0 (-> this old-facing))
      (vector-normalize! s1-0 1.0)
      (let ((f0-6 (vector-dot s1-0 s3-1)))
        (cond
          ((< 0.5 (fabs f0-6))
           )
          ((< f0-6 0.0)
           (vector--float*! s3-1 s3-1 s1-0 0.5)
           )
          (else
            (vector+float*! s3-1 s3-1 s1-0 0.5)
            )
          )
        )
      )
    (vector-normalize! s3-1 (* 1.5 (-> this enemy-info attack-shove-back)))
    (set! (-> s3-1 y) (-> this enemy-info attack-shove-up))
    (when (send-event
            arg0
            'attack
            arg1
            (static-attack-info ((id arg2) (angle 'front) (vector s3-1) (mode (-> this enemy-info attack-mode))))
            )
      (enemy-method-105 this arg0)
      #t
      )
    )
  )

(defmethod enemy-method-84 ((this metalmonk) (arg0 enemy-jump-info))
  (let* ((f0-0 (vector-vector-xz-distance (-> arg0 start-pos) (-> arg0 dest-pos)))
         (f1-1 (fmax (-> this enemy-info jump-height-min) (* (-> this enemy-info jump-height-factor) f0-0)))
         (f0-3 (fmax (-> arg0 start-pos y) (-> arg0 dest-pos y)))
         (f0-4 (- (fmax (+ (fmin (-> arg0 start-pos y) (-> arg0 dest-pos y)) f1-1) (+ 10240.0 f0-3)) f0-3))
         )
    (setup-from-to-height! (-> arg0 traj) (-> arg0 start-pos) (-> arg0 dest-pos) f0-4 -4.551111)
    )
  (none)
  )

(defmethod enemy-method-89 ((this metalmonk) (arg0 enemy-jump-info))
  #f
  )

(defmethod enemy-method-87 ((this metalmonk) (arg0 enemy-jump-info))
  (let ((s5-0 (-> this draw art-group data (-> this enemy-info run-anim)))
        (v1-6 (if (> (-> this skel active-channels) 0)
                  (-> this skel root-channel 0 frame-group)
                  )
              )
        )
    (when (not (and v1-6 (= v1-6 s5-0)))
      (ja-channel-push! 1 (seconds 0.1))
      (let ((a0-5 (-> this skel root-channel 0)))
        (set! (-> a0-5 frame-group) (the-as art-joint-anim s5-0))
        (set! (-> a0-5 param 0) (the float (+ (-> (the-as art-joint-anim s5-0) frames num-frames) -1)))
        (set! (-> a0-5 param 1) (-> arg0 anim-speed))
        (set! (-> a0-5 frame-num) 0.0)
        (joint-control-channel-group! a0-5 (the-as art-joint-anim s5-0) num-func-seek!)
        )
      )
    )
  #t
  )

(defmethod enemy-method-88 ((this metalmonk) (arg0 enemy-jump-info))
  #f
  )

(defmethod enemy-method-90 ((this metalmonk) (arg0 int) (arg1 enemy-jump-info))
  (case arg0
    ((3)
     (let ((a0-1 (-> this skel root-channel 0)))
       (set! (-> a0-1 param 0) 1.0)
       (joint-control-channel-group-eval! a0-1 (the-as art-joint-anim #f) num-func-loop!)
       )
     #f
     )
    (else
      ((method-of-type nav-enemy enemy-method-90) this arg0 arg1)
      )
    )
  )

(defstate notice (metalmonk)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy notice) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (let ((v1-4 (-> self nav)))
      (set! (-> v1-4 max-rotation-rate) (* 2.0 (-> self enemy-info maximum-rotation-rate)))
      )
    0
    (let ((v1-6 (-> self nav)))
      (set! (-> v1-6 turning-acceleration) (* 2.0 (-> self enemy-info run-turning-acceleration)))
      )
    0
    )
  :exit (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy notice) exit)))
      (if t9-0
          (t9-0)
          )
      )
    ;; og:preserve-this fix potential use-after-free bug
    (if (-> self nav)
      (let ((v1-4 (-> self nav)))
        (set! (-> v1-4 max-rotation-rate) (-> self enemy-info maximum-rotation-rate))
        ))
    0
    ;; og:preserve-this fix potential use-after-free bug
    (if (-> self nav)
      (let ((v1-6 (-> self nav)))
        (set! (-> v1-6 turning-acceleration) (-> self enemy-info run-turning-acceleration))
        ))
    0
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (let ((f30-0 (rnd-float-range self 0.8 1.2))
          (gp-0 (new 'stack-no-clear 'vector))
          )
      (ja :group! (-> self draw art-group data (-> *metalmonk-global-info* notice-anim (rnd-int-count self 2))))
      (ja-no-eval :group! (ja-group) :num! (seek! max f30-0) :frame-num 0.0)
      (until (ja-done? 0)
        (let ((a1-4 (-> self nav state)))
          (set! (-> gp-0 quad) (-> a1-4 travel quad))
          )
        (seek-toward-heading-vec! (-> self root) gp-0 131072.0 (seconds 0.01))
        (suspend)
        (ja :num! (seek! max f30-0))
        )
      )
    (react-to-focus self)
    )
  )

(defstate hostile (metalmonk)
  :virtual #t
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy hostile) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (let ((gp-0 (handle->process (-> self focus handle))))
      (if (and (time-elapsed? (-> self state-time) (-> self reaction-time))
               (and gp-0
                    (or (and (< (vector-vector-xz-distance (get-trans (the-as process-focusable gp-0) 0) (-> self root trans)) 20480.0)
                             (let ((v1-19 (ja-group)))
                               (and (and v1-19 (= v1-19 metalmonk-charge0-ja)) (< (ja-frame-num 0) 3.0))
                               )
                             )
                        (and (< (vector-vector-xz-distance (get-trans (the-as process-focusable gp-0) 0) (-> self root trans)) 16384.0)
                             (let ((v1-29 (ja-group)))
                               (and v1-29 (= v1-29 metalmonk-charge1-ja))
                               )
                             (< (ja-frame-num 0) 8.0)
                             )
                        )
                    )
               )
          (go-virtual attack)
          )
      )
    )
  :code (behavior ()
    (talker-spawn-func (-> *talker-speech* 95) *entity-pool* (target-pos 0) (the-as region #f))
    (let ((f30-0 (rnd-float-range self 0.9 1.1)))
      (suspend)
      (until #f
        (metalmonk-method-181 self 1.2 0.1)
        (suspend)
        (ja :num! (loop! f30-0))
        )
      )
    #f
    )
  )

(defstate active (metalmonk)
  :virtual #t
  :code (behavior ()
    (let ((v1-2 (ja-group)))
      (when (or (and v1-2 (or (= v1-2 metalmonk-charge0-ja) (= v1-2 metalmonk-charge1-ja)))
                (let ((v1-8 (ja-group)))
                  (and v1-8 (or (= v1-8 metalmonk-patrol0-ja) (= v1-8 metalmonk-patrol1-ja)))
                  )
                )
        (ja-no-eval :num! (seek!))
        (while (not (ja-done? 0))
          (suspend)
          (ja-eval)
          )
        )
      )
    (let ((f30-0 (rnd-float-range self 0.9 1.1)))
      (until #f
        (let ((gp-0 (-> self draw art-group data (-> (&-> *metalmonk-global-info* patrol-anim (rnd-int-count self 2)) 0)))
              (s5-0 (rnd-int-range self 1 8))
              )
          (let ((v1-34 (ja-group)))
            (if (not (and v1-34 (= v1-34 gp-0)))
                (ja-channel-push! 1 (seconds 0.2))
                )
            )
          (dotimes (s4-0 s5-0)
            (ja-no-eval :group! gp-0 :num! (seek! max f30-0) :frame-num 0.0)
            (until (ja-done? 0)
              (suspend)
              (ja :num! (seek! max f30-0))
              )
            )
          )
        (when (zero? (rnd-int-count self 2))
          (let ((v1-58 self))
            (set! (-> v1-58 enemy-flags) (the-as enemy-flag (logclear (-> v1-58 enemy-flags) (enemy-flag enemy-flag36))))
            (set! (-> v1-58 nav callback-info) *nav-enemy-null-callback-info*)
            )
          0
          (ja-channel-push! 1 (seconds 0.3))
          (let ((gp-1 0))
            (until (not (rnd-go-idle? self 0.4))
              (let* ((v1-62 (rnd-bit self 3 gp-1))
                     (a1-16 (-> self draw art-group data (-> *metalmonk-global-info* idle-anim v1-62)))
                     )
                (set! gp-1 (ash 1 v1-62))
                (ja-no-eval :group! a1-16 :num! (seek! max f30-0) :frame-num 0.0)
                )
              (ja-no-eval :group! (ja-group) :num! (seek! max f30-0) :frame-num 0.0)
              (until (ja-done? 0)
                (suspend)
                (ja :num! (seek! max f30-0))
                )
              )
            )
          (let ((v1-102 self))
            (if (not (logtest? (enemy-flag enemy-flag36) (-> v1-102 enemy-flags)))
                (set! (-> v1-102 enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag38) (-> v1-102 enemy-flags))))
                )
            (set! (-> v1-102 enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag36) (-> v1-102 enemy-flags))))
            (set! (-> v1-102 nav callback-info) (-> v1-102 enemy-info callback-info))
            )
          0
          )
        )
      )
    #f
    )
  )

(defstate attack (metalmonk)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (logior! (-> self enemy-flags) (enemy-flag lock-focus))
    (metalmonk-method-180 self #t)
    (nav-enemy-method-169 self 6144.0 #f)
    (let ((v1-6 (-> self nav)))
      (set! (-> v1-6 target-speed) (+ 8192.0 (-> self enemy-info run-travel-speed)))
      )
    0
    (let ((v1-8 (-> self nav)))
      (set! (-> v1-8 turning-acceleration) (* 4.0 (-> self enemy-info run-turning-acceleration)))
      )
    0
    (set! (-> self root penetrate-using) (penetrate generic-attack lunge))
    (enemy-method-49 self)
    (logior! (-> self focus-status) (focus-status dangerous))
    (let* ((v1-15 *game-info*)
           (v0-3 (+ (-> v1-15 attack-id) 1))
           )
      (set! (-> v1-15 attack-id) v0-3)
      (set! (-> self attack-id) v0-3)
      )
    )
  :exit (behavior ()
    (logclear! (-> self enemy-flags) (enemy-flag lock-focus))
    (metalmonk-method-180 self #f)
    (nav-enemy-method-168 self)
    ;; og:preserve-this fix potential use-after-free bug
    (if (-> self nav)
      (let ((v1-6 (-> self nav)))
        (set! (-> v1-6 target-speed) (-> self enemy-info run-travel-speed))
        ))
    0
    (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 ()
    (enemy-method-49 self)
    )
  :code (behavior ()
    (let ((gp-0 #f))
      (let* ((v1-2 (ja-group))
             (s5-0 (if (and v1-2 (= v1-2 metalmonk-charge0-ja))
                       (-> self draw art-group data (-> *metalmonk-global-info* attack-anim 0))
                       (-> self draw art-group data (-> *metalmonk-global-info* attack-anim 1))
                       )
                   )
             )
        (ja-channel-push! 1 (seconds 0.1))
        (ja-no-eval :group! s5-0 :num! (seek!) :frame-num 0.0)
        )
      (until (ja-done? 0)
        (if (logtest? (-> self enemy-flags) (enemy-flag victory))
            (set! gp-0 #t)
            )
        (suspend)
        (ja :num! (seek!))
        )
      (if gp-0
          (go-virtual victory)
          (go-hostile self)
          )
      )
    )
  :post nav-enemy-chase-post
  )

(defstate victory (metalmonk)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy victory) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (set-time! (-> self state-time))
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy victory) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (let ((a0-1 (handle->process (-> self focus handle))))
      (if (and a0-1
               (and (time-elapsed? (-> self state-time) (seconds 0.05))
                    (let ((f0-0 (vector-vector-xz-distance-squared (get-trans (the-as process-focusable a0-1) 0) (-> self root trans)))
                          (f1-0 14336.0)
                          )
                      (< f0-0 (* f1-0 f1-0))
                      )
                    )
               )
          (react-to-focus self)
          )
      )
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (ja :group! (-> self draw art-group data (-> *metalmonk-global-info* celebrate-anim (rnd-int-count self 2))))
    (ja :num-func num-func-identity :frame-num 0.0)
    (let ((f30-0 (rnd-float-range self 0.8 1.2)))
      (ja-no-eval :group! (ja-group) :num! (seek! max f30-0) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek! max f30-0))
        )
      )
    (react-to-focus self)
    )
  )

(defstate circling (metalmonk)
  :virtual #t
  :code (behavior ()
    (nav-enemy-method-166 self)
    (let ((f30-0 (rnd-float-range self 0.9 1.1)))
      (suspend)
      (until #f
        (metalmonk-method-181 self 1.6 1.5)
        (suspend)
        (ja :num! (loop! f30-0))
        )
      )
    #f
    )
  )

(defstate pacing (metalmonk)
  :virtual #t
  :code (behavior ()
    (let ((v1-2 (ja-group)))
      (when (and v1-2 (or (= v1-2 metalmonk-charge0-ja) (= v1-2 metalmonk-charge1-ja)))
        (let ((v1-6 (-> self nav)))
          (set! (-> v1-6 target-speed) (-> self enemy-info run-travel-speed))
          )
        0
        (ja-no-eval :num! (seek!))
        (while (not (ja-done? 0))
          (suspend)
          (ja-eval)
          )
        )
      )
    (nav-enemy-method-165 self)
    (let ((f30-0 (rnd-float-range self 0.9 1.1)))
      (-> self draw art-group data (-> *metalmonk-global-info* patrol-anim (rnd-int-count self 2)))
      (until #f
        (metalmonk-method-182 self 1.2 0.5)
        (suspend)
        (ja :num! (loop! f30-0))
        )
      )
    #f
    )
  )

(defstate ambush (metalmonk)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy ambush) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (when (not (-> self intro-path))
      (format 0 "ERROR: ~A has no intro path, skipping ambush~%" (-> self name))
      (go-virtual notice)
      )
    (get-point-at-percent-along-path! (-> self intro-path) (-> self root trans) 0.0 'interp)
    )
  :code (behavior ()
    (until #f
      (let ((gp-0 (new 'stack-no-clear 'vector)))
        (get-point-at-percent-along-path! (-> self intro-path) gp-0 1.0 'interp)
        (set! (-> self enemy-flags)
              (the-as enemy-flag (logclear (-> self enemy-flags) (enemy-flag jump-check-blocked)))
              )
        (let ((a1-1 (new 'stack-no-clear 'event-message-block)))
          (let ((v1-4 (process->ppointer self)))
            (set! (-> a1-1 from) v1-4)
            )
          (set! (-> a1-1 num-params) 2)
          (set! (-> a1-1 message) 'jump)
          (set! (-> a1-1 param 0) (the-as uint 2))
          (set! (-> a1-1 param 1) (the-as uint gp-0))
          (send-event-function self a1-1)
          )
        )
      (suspend)
      )
    #f
    )
  )

(defmethod enemy-method-77 ((this metalmonk) (arg0 enemy-knocked-info))
  (case (-> this incoming knocked-type)
    (((knocked-type knocked-type-4))
     (ja-channel-push! 1 0)
     (let* ((a2-0 (ash 1 (-> *metalmonk-global-info* prev-yellow-hit)))
            (v1-3 (rnd-bit this 4 a2-0))
            (a1-6 (-> this draw art-group data (-> *metalmonk-global-info* yellow-hit-anim v1-3)))
            )
       (set! (-> *metalmonk-global-info* prev-yellow-hit) v1-3)
       (let ((a0-13 (-> this skel root-channel 0)))
         (set! (-> a0-13 frame-group) (the-as art-joint-anim a1-6))
         (set! (-> a0-13 param 0) (the float (+ (-> (the-as art-joint-anim a1-6) frames num-frames) -1)))
         (set! (-> a0-13 param 1) (-> arg0 anim-speed))
         (set! (-> a0-13 frame-num) 0.0)
         (joint-control-channel-group! a0-13 (the-as art-joint-anim a1-6) num-func-seek!)
         )
       )
     )
    (((knocked-type knocked-type-6))
     (let* ((a2-2 (ash 1 (-> *metalmonk-global-info* prev-blue-hit)))
            (v1-12 (rnd-bit this 3 a2-2))
            (s5-1 (-> this draw art-group data (-> *metalmonk-global-info* blue-hit-anim v1-12)))
            )
       (set! (-> *metalmonk-global-info* prev-blue-hit) v1-12)
       (let ((v1-15 (if (> (-> this skel active-channels) 0)
                        (-> this skel root-channel 0 frame-group)
                        )
                    )
             )
         (if (and v1-15 (= v1-15 (-> this draw art-group data 27)))
             (ja-channel-push! 1 (seconds 0.17))
             (ja-channel-push! 1 (seconds 0.02))
             )
         )
       (let ((a0-31 (-> this skel root-channel 0)))
         (set! (-> a0-31 frame-group) (the-as art-joint-anim s5-1))
         (set! (-> a0-31 param 0) (the float (+ (-> (the-as art-joint-anim s5-1) frames num-frames) -1)))
         (set! (-> a0-31 param 1) 1.0)
         (set! (-> a0-31 frame-num) 0.0)
         (joint-control-channel-group! a0-31 (the-as art-joint-anim s5-1) num-func-seek!)
         )
       )
     )
    (else
      (let ((s4-1
              (if (< (vector-dot (the-as vector (metalmonk-method-179 this (new 'stack-no-clear 'vector))) (-> this root transv))
                     0.0
                     )
                  (-> this draw art-group data (-> *metalmonk-global-info* knocked-anim (rnd-int-count this 2)))
                  (-> this draw art-group data 20)
                  )
              )
            )
        (ja-channel-push! 1 (seconds 0.17))
        (let ((a0-38 (-> this skel root-channel 0)))
          (set! (-> a0-38 frame-group) (the-as art-joint-anim s4-1))
          (set! (-> a0-38 param 0) (the float (+ (-> (the-as art-joint-anim s4-1) frames num-frames) -1)))
          (set! (-> a0-38 param 1) (-> arg0 anim-speed))
          (set! (-> a0-38 frame-num) 0.0)
          (joint-control-channel-group! a0-38 (the-as art-joint-anim s4-1) num-func-seek!)
          )
        )
      )
    )
  #t
  )

(defmethod enemy-method-78 ((this metalmonk) (arg0 enemy-knocked-info))
  (cond
    ((= (-> this incoming knocked-type) (knocked-type knocked-type-6))
     (when (>= (-> this incoming blue-juggle-count) (the-as uint 2))
       (let ((s4-0 (-> this draw art-group data 27)))
         (ja-channel-push! 1 (seconds 0.17))
         (let ((a0-3 (-> this skel root-channel 0)))
           (set! (-> a0-3 frame-group) (the-as art-joint-anim s4-0))
           (set! (-> a0-3 param 0) (the float (+ (-> (the-as art-joint-anim s4-0) frames num-frames) -1)))
           (set! (-> a0-3 param 1) (-> arg0 anim-speed))
           (set! (-> a0-3 frame-num) 0.0)
           (joint-control-channel-group! a0-3 (the-as art-joint-anim s4-0) num-func-seek!)
           )
         )
       #t
       )
     )
    ((!= (-> this incoming knocked-type) (knocked-type knocked-type-4))
     (let* ((v1-14 (if (> (-> this skel active-channels) 0)
                       (-> this skel root-channel 0 frame-group)
                       )
                   )
            (s4-1 (cond
                    ((and v1-14 (= v1-14 (-> this draw art-group data 16)))
                     (-> this draw art-group data 17)
                     )
                    (else
                      (let ((v1-21 (if (> (-> this skel active-channels) 0)
                                       (-> this skel root-channel 0 frame-group)
                                       )
                                   )
                            )
                        (if (and v1-21 (= v1-21 (-> this draw art-group data 18)))
                            (-> this draw art-group data 19)
                            (-> this draw art-group data 21)
                            )
                        )
                      )
                    )
                  )
            )
       (ja-channel-push! 1 (seconds 0.17))
       (let ((a0-14 (-> this skel root-channel 0)))
         (set! (-> a0-14 frame-group) (the-as art-joint-anim s4-1))
         (set! (-> a0-14 param 0) (the float (+ (-> (the-as art-joint-anim s4-1) frames num-frames) -1)))
         (set! (-> a0-14 param 1) (-> arg0 anim-speed))
         (set! (-> a0-14 frame-num) 0.0)
         (joint-control-channel-group! a0-14 (the-as art-joint-anim s4-1) num-func-seek!)
         )
       )
     #t
     )
    )
  )

(defmethod metalmonk-method-180 ((this metalmonk) (arg0 symbol))
  (let ((v1-1 (-> this root root-prim)))
    (dotimes (a0-1 1)
      (let ((a2-1 (-> (the-as collide-shape-prim-group v1-1) child a0-1)))
        (if arg0
            (set! (-> a2-1 prim-core action) (collide-action solid deadly))
            (set! (-> a2-1 prim-core action) (collide-action solid))
            )
        )
      )
    )
  0
  (none)
  )

(defmethod init-enemy-collision! ((this metalmonk))
  "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 9) 0)))
      (set! (-> s5-0 total-prims) (the-as uint 10))
      (set! (-> s4-0 prim-core collide-as) (collide-spec enemy))
      (set! (-> s4-0 prim-core collide-with)
            (collide-spec backgnd jak bot crate obstacle hit-by-others-list player-list)
            )
      (set! (-> s4-0 prim-core action) (collide-action solid deadly no-standon))
      (set-vector! (-> s4-0 local-sphere) 0.0 8192.0 0.0 16384.0)
      (set! (-> s5-0 root-prim) s4-0)
      )
    (let ((v1-13 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-13 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-13 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-13 prim-core action) (collide-action solid deadly))
      (set! (-> v1-13 transform-index) 7)
      (set-vector! (-> v1-13 local-sphere) 0.0 0.0 0.0 2662.4)
      )
    (let ((v1-15 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-15 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-15 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-15 prim-core action) (collide-action solid))
      (set! (-> v1-15 transform-index) 38)
      (set-vector! (-> v1-15 local-sphere) 0.0 0.0 0.0 2048.0)
      )
    (let ((v1-17 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-17 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-17 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-17 prim-core action) (collide-action solid))
      (set! (-> v1-17 transform-index) 41)
      (set-vector! (-> v1-17 local-sphere) 0.0 0.0 0.0 2048.0)
      )
    (let ((v1-19 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-19 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-19 prim-core collide-with) (collide-spec backgnd crate obstacle hit-by-others-list))
      (set! (-> v1-19 prim-core action) (collide-action solid))
      (set-vector! (-> v1-19 local-sphere) 0.0 6144.0 0.0 6144.0)
      )
    (let ((v1-21 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-21 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-21 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-21 prim-core action) (collide-action solid no-standon))
      (set-vector! (-> v1-21 local-sphere) 0.0 5120.0 0.0 5120.0)
      )
    (let ((v1-23 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-23 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-23 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-23 prim-core action) (collide-action solid))
      (set! (-> v1-23 transform-index) 4)
      (set-vector! (-> v1-23 local-sphere) 0.0 0.0 0.0 3072.0)
      )
    (let ((v1-25 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-25 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-25 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-25 prim-core action) (collide-action solid))
      (set! (-> v1-25 transform-index) 10)
      (set-vector! (-> v1-25 local-sphere) 0.0 0.0 0.0 2048.0)
      )
    (let ((v1-27 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-27 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-27 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-27 prim-core action) (collide-action solid))
      (set! (-> v1-27 transform-index) 13)
      (set-vector! (-> v1-27 local-sphere) 0.0 0.0 0.0 2048.0)
      )
    (let ((v1-29 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-29 prim-core collide-as) (collide-spec enemy))
      (set! (-> v1-29 prim-core collide-with) (collide-spec jak bot player-list))
      (set! (-> v1-29 prim-core action) (collide-action solid))
      (set! (-> v1-29 transform-index) 22)
      (set-vector! (-> v1-29 local-sphere) 0.0 0.0 0.0 2662.4)
      )
    (set! (-> s5-0 nav-radius) 8192.0)
    (let ((v1-31 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-31 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-31 prim-core collide-with))
      )
    (set! (-> s5-0 max-iteration-count) (the-as uint 3))
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod coin-flip? ((this metalmonk))
  "@returns The result of a 50/50 RNG roll"
  #f
  )

(defmethod relocate ((this metalmonk) (arg0 int))
  (if (nonzero? (-> this intro-path))
      (&+! (-> this intro-path) arg0)
      )
  (call-parent-method this arg0)
  )

(defmethod init-enemy! ((this metalmonk))
  "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-metalmonk" (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 *metalmonk-nav-enemy-info*)
  (let ((v1-8 (-> this neck)))
    (set! (-> v1-8 up) (the-as uint 1))
    (set! (-> v1-8 nose) (the-as uint 2))
    (set! (-> v1-8 ear) (the-as uint 0))
    (set-vector! (-> v1-8 twist-max) 10922.667 12743.111 0.0 1.0)
    (set! (-> v1-8 ignore-angle) 18204.445)
    )
  (let ((v1-10 (-> this nav)))
    (set! (-> v1-10 speed-scale) 1.0)
    )
  0
  (logior! (-> this nav flags) (nav-control-flag momentum-ignore-heading))
  (set-gravity-length (-> this root dynam) 573440.0)
  (set! (-> this intro-path) (new 'process 'path-control this 'intro 0.0 (-> this entity) #f))
  (metalmonk-method-180 this #f)
  (set! (-> this high-time) 0)
  (add-connection
    *part-engine*
    this
    7
    this
    4986
    (new 'static 'vector :x 1064.96 :y -450.56 :z 1146.88 :w 163840.0)
    )
  (add-connection
    *part-engine*
    this
    7
    this
    4987
    (new 'static 'vector :x -1064.96 :y -450.56 :z 1146.88 :w 163840.0)
    )
  (add-connection *part-engine* this 7 this 4988 (new 'static 'vector :y 1556.48 :z 368.64 :w 163840.0))
  0
  (none)
  )
