;;-*-Lisp-*-
(in-package goal)
(bundles "GAME.CGO")
(require "engine/common-obs/nav-enemy.gc")

;; DECOMP BEGINS

(deftype babak (nav-enemy) ()
  (:states
   babak-run-to-cannon))

(defskelgroup *babak-sg*
  babak
  babak-lod0-jg
  -1
  ((babak-lod0-mg (meters 20)) (babak-lod1-mg (meters 40)) (babak-lod2-mg (meters 999999)))
  :bounds (static-spherem 0 2 0 3)
  :longest-edge (meters 1)
  :shadow babak-shadow-mg)

(defstate nav-enemy-patrol (babak)
  :virtual #t
  :code
    (behavior ()
      (cond
        ((ja-group? babak-give-up-hop-ja)
         (ja-channel-push! 1 (seconds 0.15))
         (ja-no-eval :group! babak-walk-ja :num! (seek!) :frame-num (ja-aframe 12.0 0))
         (until (ja-done? 0)
           (suspend)
           (ja :num! (seek!))))
        (else (ja-channel-push! 1 (seconds 0.2))))
      ((the-as (function none) (-> (method-of-type nav-enemy nav-enemy-patrol) code)))))

(defstate nav-enemy-chase (babak)
  :virtual #t
  :code
    (behavior ()
      (let ((f30-0 (nav-enemy-rnd-float-range 0.9 1.1)))
        (cond
          ((ja-group? babak-jump-land-ja)
           (ja-no-eval :num! (seek!))
           (ja-channel-push! 1 (seconds 0.17))
           (ja-no-eval :group! (-> self draw art-group data (-> self nav-info run-anim)) :num! (seek! max f30-0) :frame-num 0.0)
           (until (ja-done? 0)
             (ja-blend-eval)
             (suspend)
             (ja :num! (seek! max f30-0))))
          (else
           (ja-channel-push! 1 (seconds 0.2))
           (ja :group! (-> self draw art-group data (-> self nav-info run-anim)))
           (ja :num-func num-func-identity :frame-num 0.0)))
        (loop
          (suspend)
          (ja :num! (loop! f30-0))))))

(defstate nav-enemy-stare (babak)
  :virtual #t
  :code
    (behavior ()
      (set! (-> self turn-time) (seconds 0.2))
      (let ((f30-0 (nav-enemy-rnd-float-range 0.8 1.2)))
        (when (or (logtest? (-> self nav-enemy-flags) (nav-enemy-flags navenmf8))
                  (and (nav-enemy-player-vulnerable?) (nav-enemy-rnd-percent? 0.5)))
          (ja-channel-push! 1 (seconds 0.1))
          (ja-no-eval :group! babak-win-ja :num! (seek! (ja-aframe 68.0 0) f30-0) :frame-num 0.0)
          (until (ja-done? 0)
            (suspend)
            (ja :num! (seek! (ja-aframe 68.0 0) f30-0))))
        (loop
          (when (not (nav-enemy-facing-player? 2730.6667))
            (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel))
            (ja-no-eval :num! (loop!))
            (ja-channel-push! 1 (seconds 0.2))
            (ja :group! babak-turn-ja)
            (ja :num-func num-func-identity :frame-num 0.0)
            (until (nav-enemy-facing-player? 1820.4445)
              (ja-blend-eval)
              (suspend)
              (ja :num! (loop! 0.75)))
            (logclear! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel)))
          (if (not (ja-group? babak-idle-ja)) (ja-channel-push! 1 (seconds 0.2)))
          (ja-no-eval :group! babak-idle-ja :num! (seek! max f30-0) :frame-num 0.0)
          (until (ja-done? 0)
            (suspend)
            (ja :num! (seek! max f30-0)))
          (when (nav-enemy-rnd-percent? 0.3)
            (ja-channel-push! 1 (seconds 0.1))
            (ja-no-eval :group! babak-win-ja :num! (seek! (ja-aframe 68.0 0) f30-0) :frame-num 0.0)
            (until (ja-done? 0)
              (suspend)
              (ja :num! (seek! (ja-aframe 68.0 0) f30-0))))))))

(defstate nav-enemy-give-up (babak)
  :virtual #t
  :code
    (behavior ()
      (set! (-> self rotate-speed) 218453.33)
      (set! (-> self turn-time) (seconds 0.5))
      (ja-channel-push! 1 (seconds 0.15))
      (let ((s4-0 (-> self collide-info))
            (s5-0 (target-pos 0)))
        (when (< (fabs (deg-diff (y-angle s4-0) (vector-y-angle (vector-! (new 'stack-no-clear 'vector) s5-0 (-> s4-0 trans)))))
                 12743.111)
          (ja-no-eval :group! babak-give-up-ja :num! (seek!) :frame-num 0.0)
          (until (ja-done? 0)
            (suspend)
            (ja :num! (seek!)))))
      (logclear! (-> self nav flags) (nav-control-flags navcf17 navcf19))
      (nav-enemy-get-new-patrol-point)
      (ja-no-eval :group! babak-give-up-hop-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (seek-to-point-toward-point! (-> self collide-info)
                                     (-> self nav destination-pos)
                                     (-> self rotate-speed)
                                     (-> self turn-time))
        (suspend)
        (ja :num! (seek!)))
      (go-virtual nav-enemy-patrol)))

(defstate nav-enemy-jump-land (babak)
  :virtual #t
  :code
    (behavior ()
      (ja-no-eval :num! (seek!))
      (ja-channel-push! 1 (seconds 0.075))
      (ja-no-eval :group!
                  (-> self draw art-group data (-> self nav-info jump-land-anim))
                  :num!
                  (seek! (ja-aframe 32.0 0) 0.5)
                  :frame-num 0.0)
      (until (ja-done? 0)
        (ja-blend-eval)
        (suspend)
        (ja :num! (seek! (ja-aframe 32.0 0) 0.5)))
      (go-virtual nav-enemy-chase)))

(define *babak-nav-enemy-info*
  (new 'static
       'nav-enemy-info
       :idle-anim 5
       :walk-anim 6
       :turn-anim 16
       :notice-anim 7
       :run-anim 8
       :jump-anim 13
       :jump-land-anim 14
       :victory-anim 11
       :taunt-anim 11
       :die-anim 12
       :neck-joint 5
       :player-look-at-joint 5
       :run-travel-speed (meters 6)
       :run-rotate-speed (degrees 2880)
       :run-acceleration (meters 1)
       :run-turn-time (seconds 0.1)
       :walk-travel-speed (meters 3)
       :walk-rotate-speed (degrees 720)
       :walk-acceleration (meters 1)
       :walk-turn-time (seconds 0.5)
       :attack-shove-back (meters 3)
       :attack-shove-up (meters 2)
       :shadow-size (meters 2)
       :notice-nav-radius (meters 2)
       :nav-nearest-y-threshold (meters 10)
       :notice-distance (meters 30)
       :proximity-notice-distance (meters 20)
       :stop-chase-distance (meters 40)
       :frustration-distance (meters 8)
       :frustration-time (seconds 4)
       :die-anim-hold-frame 24.0
       :jump-anim-start-frame 10.0
       :jump-land-anim-end-frame 10000000000.0
       :jump-height-min (meters 3)
       :jump-height-factor 0.5
       :jump-start-anim-speed 1.0
       :shadow-max-y (meters 1)
       :shadow-min-y (meters -1)
       :shadow-locus-dist (meters 150)
       :use-align #t
       :draw-shadow #t
       :move-to-ground #t
       :hover-if-no-ground #f
       :use-momentum #f
       :use-flee #t
       :use-proximity-notice #t
       :use-jump-blocked #t
       :use-jump-patrol #f
       :gnd-collide-with (collide-kind background)
       :debug-draw-neck #f
       :debug-draw-jump #f))

(defmethod initialize-collision ((this babak))
  (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) default-collision-reaction)
    (set! (-> s5-0 no-reaction) (the-as (function collide-shape-moving collide-shape-intersect vector vector none) nothing))
    (let ((s4-0 (new 'process 'collide-shape-prim-group s5-0 (the-as uint 3) 0)))
      (set! (-> s4-0 prim-core collide-as) (collide-kind enemy))
      (set! (-> s4-0 collide-with) (collide-kind target))
      (set! (-> s4-0 prim-core action) (collide-action solid))
      (set-vector! (-> s4-0 local-sphere) 0.0 8192.0 0.0 12288.0)
      (set-root-prim! s5-0 s4-0)
      (let ((s3-0 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 3))))
        (set! (-> s3-0 prim-core collide-as) (collide-kind enemy))
        (set! (-> s3-0 collide-with) (collide-kind target))
        (set! (-> s3-0 prim-core action) (collide-action solid))
        (set! (-> s3-0 prim-core offense) (collide-offense touch))
        (set-vector! (-> s3-0 local-sphere) 0.0 4096.0 0.0 3072.0)
        (append-prim s4-0 s3-0))
      (let ((s3-1 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 3))))
        (set! (-> s3-1 prim-core collide-as) (collide-kind enemy))
        (set! (-> s3-1 collide-with) (collide-kind target))
        (set! (-> s3-1 prim-core action) (collide-action solid))
        (set! (-> s3-1 prim-core offense) (collide-offense touch))
        (set-vector! (-> s3-1 local-sphere) 0.0 9830.4 0.0 3072.0)
        (append-prim s4-0 s3-1))
      (let ((s3-2 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 1))))
        (set! (-> s3-2 prim-core collide-as) (collide-kind enemy))
        (set! (-> s3-2 collide-with) (collide-kind target))
        (set! (-> s3-2 prim-core offense) (collide-offense normal-attack))
        (set! (-> s3-2 transform-index) 6)
        (set-vector! (-> s3-2 local-sphere) 0.0 0.0 0.0 2048.0)
        (append-prim s4-0 s3-2)))
    (set! (-> s5-0 nav-radius) 6144.0)
    (backup-collide-with-as s5-0)
    (set! (-> s5-0 max-iteration-count) (the-as uint 2))
    (set! (-> this collide-info) s5-0))
  0
  (none))

(defmethod nav-enemy-method-48 ((this babak))
  (initialize-skeleton this *babak-sg* '())
  (init-defaults! this *babak-nav-enemy-info*)
  (set! (-> this neck up) (the-as uint 0))
  (set! (-> this neck nose) (the-as uint 1))
  (set! (-> this neck ear) (the-as uint 2))
  0
  (none))

(defmethod nav-enemy-method-59 ((this babak))
  (cond
    ((and (and (-> this entity) (logtest? (-> this entity extra perm status) (entity-perm-status complete)))
          (logtest? (-> this enemy-info options) (fact-options has-power-cell)))
     (go (method-of-object this nav-enemy-fuel-cell)))
    ((logtest? (-> this enemy-info options) (fact-options fop5)) (go (method-of-object this nav-enemy-wait-for-cue)))
    (else (go (method-of-object this nav-enemy-idle))))
  0
  (none))
