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

;; name: kid.gc
;; name in dgo: kid
;; dgos: CTYKORA

;; DECOMP BEGINS

(define *kid-nav-enemy-info*
  (new 'static 'nav-enemy-info
    :use-die-falling #t
    :use-victory #f
    :use-jump-blocked #f
    :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 2
      (new 'static 'idle-control-frame :command (ic-cmd play) :anim #x3 :param0 #x1 :param1 #x1)
      (new 'static 'idle-control-frame)
      )
    :idle-anim 3
    :notice-anim 3
    :hostile-anim -1
    :hit-anim 3
    :knocked-anim 17
    :knocked-land-anim 18
    :die-anim 3
    :die-falling-anim 3
    :victory-anim -1
    :jump-wind-up-anim 3
    :jump-in-air-anim 3
    :jump-land-anim 3
    :neck-joint 6
    :look-at-joint 6
    :bullseye-joint 4
    :notice-distance (meters 30)
    :notice-distance-delta (meters 10)
    :proximity-notice-distance (meters 30)
    :default-hit-points 100
    :gnd-collide-with (collide-spec backgnd)
    :overlaps-others-collide-with-filter (collide-spec jak civilian enemy vehicle-sphere hit-by-others-list 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.7
    :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 2730.6667
    :knocked-soft-vxz-lo 40960.0
    :knocked-soft-vxz-hi 40960.0
    :knocked-soft-vy-lo 12288.0
    :knocked-soft-vy-hi 12288.0
    :knocked-medium-vxz-lo 40960.0
    :knocked-medium-vxz-hi 40960.0
    :knocked-medium-vy-lo 12288.0
    :knocked-medium-vy-hi 12288.0
    :knocked-hard-vxz-lo 40960.0
    :knocked-hard-vxz-hi 40960.0
    :knocked-hard-vy-lo 12288.0
    :knocked-hard-vy-hi 12288.0
    :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 40960.0
    :knocked-yellow-vy-lo 12288.0
    :knocked-yellow-vy-hi 12288.0
    :knocked-red-vxz-lo 40960.0
    :knocked-red-vxz-hi 40960.0
    :knocked-red-vy-lo 12288.0
    :knocked-red-vy-hi 12288.0
    :knocked-blue-vxz-lo 40960.0
    :knocked-blue-vxz-hi 40960.0
    :knocked-blue-vy-lo 12288.0
    :knocked-blue-vy-hi 12288.0
    :shadow-size (meters 2)
    :shadow-max-y (meters 1)
    :shadow-min-y (meters -1)
    :shadow-locus-dist (meters 150)
    :gem-joint -1
    :gem-offset (new 'static 'sphere :r 163840.0)
    :callback-info #f
    :use-momentum #t
    :use-frustration #t
    :use-stop-chase #f
    :use-circling #f
    :use-pacing #f
    :walk-anim 3
    :turn-anim -1
    :run-anim 3
    :taunt-anim -1
    :run-travel-speed (meters 5.5)
    :run-acceleration (meters 5)
    :run-turning-acceleration (meters 6)
    :walk-travel-speed (meters 1.5)
    :walk-acceleration (meters 5)
    :walk-turning-acceleration (meters 6)
    :maximum-rotation-rate (degrees 180)
    :notice-nav-radius (meters 0.85)
    :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! (-> *kid-nav-enemy-info* fact-defaults) *fact-info-enemy-defaults*)

(defmethod general-event-handler ((this kid) (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
    (('arrest)
     (let* ((s4-0 (handle->process (-> this arrestor-handle)))
            (v1-4 (if (type? s4-0 process-focusable)
                      s4-0
                      )
                  )
            )
       (when (or (not v1-4) (= v1-4 arg0) (and (!= v1-4 arg0) (time-elapsed? (-> this arrest-attempt-time) (seconds 0.1))))
         (set! (-> this arrestor-handle) (process->handle arg0))
         (set-time! (-> this arrest-attempt-time))
         0
         )
       )
     #t
     )
    (('instant-arrest)
     (logior! (-> this bot-flags) (bot-flags bf20))
     (let ((v1-18 (the-as object (-> arg3 param 0))))
       (set! (-> this arrestor-handle) (process->handle (the-as process v1-18)))
       (set-time! (-> this arrest-attempt-time))
       (let ((a1-8 (new 'stack-no-clear 'vector)))
         (vector-! a1-8 (-> (the-as process-drawable v1-18) root trans) (-> this root trans))
         (set! (-> a1-8 y) 0.0)
         (forward-up->quaternion (-> this root quat) a1-8 *up-vector*)
         )
       )
     (logclear! (-> this enemy-flags) (enemy-flag vulnerable vulnerable-backup))
     (logclear! (-> this mask) (process-mask collectable))
     (logclear! (-> this enemy-flags) (enemy-flag attackable-backup))
     (logclear! (-> this mask) (process-mask actor-pause))
     (logclear! (-> this enemy-flags) (enemy-flag actor-pause-backup))
     (send-event (handle->process (-> this master-handle)) 'notify 'arrest #f)
     (go (method-of-object this arrested))
     )
    (('attack)
     ((method-of-type bot general-event-handler) this arg0 arg1 arg2 arg3)
     #f
     )
    (else
      ((method-of-type bot general-event-handler) this arg0 arg1 arg2 arg3)
      )
    )
  )

(defmethod enemy-method-97 ((this kid))
  (let* ((s4-0 (handle->process (-> this attacker-handle)))
         (s5-0 (if (type? s4-0 process-focusable)
                   s4-0
                   )
               )
         (s3-0 (handle->process (-> this arrestor-handle)))
         (s4-1 (if (type? s3-0 process-focusable)
                   s3-0
                   )
               )
         )
    (when s5-0
      (cond
        ((= (-> s5-0 type) target)
         (when (or (not (logtest? (-> this bot-flags) (bot-flags attacked)))
                   (time-elapsed? (-> this attacker-time) (seconds 1.5))
                   )
           (if (logtest? (-> this bot-flags) (bot-flags attacked))
               (reset-attacker! this)
               )
           (set! s5-0 (the-as process #f))
           (set! (-> this attacker-handle) (the-as handle #f))
           )
         )
        (else
          (when (time-elapsed? (-> this attacker-time) (seconds 6))
            (set! s5-0 (the-as process #f))
            (set! (-> this attacker-handle) (the-as handle #f))
            )
          )
        )
      )
    (when s4-1
      (when (or (time-elapsed? (-> this arrest-attempt-time) (seconds 0.5))
                (focus-test? (the-as process-focusable s4-1) dead hit)
                )
        (set! s4-1 (the-as process #f))
        (set! (-> this arrestor-handle) (the-as handle #f))
        )
      )
    (let ((v1-34 (-> this focus-mode))
          (s3-1 (the-as process #f))
          )
      (cond
        ((zero? v1-34)
         (cond
           (s4-1
             (set! s3-1 s4-1)
             )
           (s5-0
             (set! s3-1 s5-0)
             )
           ((begin (set! s3-1 (select-focus! this)) s3-1)
            (empty)
            )
           (else
             (let ((s5-1 (handle->process (-> this poi-handle))))
               (set! s3-1 (if (type? s5-1 process-focusable)
                              s5-1
                              )
                     )
               )
             (if s3-1
                 (empty)
                 (set! s3-1 *target*)
                 )
             )
           )
         )
        ((= v1-34 1)
         (cond
           (s4-1
             (set! s3-1 s4-1)
             )
           (s5-0
             (set! s3-1 s5-0)
             )
           (else
             (let ((s5-2 (handle->process (-> this poi-handle))))
               (set! s3-1 (if (type? s5-2 process-focusable)
                              s5-2
                              )
                     )
               )
             (cond
               (s3-1
                 (empty)
                 )
               ((begin (set! s3-1 (select-focus! this)) s3-1)
                (empty)
                )
               (else
                 (set! s3-1 *target*)
                 )
               )
             )
           )
         )
        )
      (cond
        (s3-1
          (try-update-focus (-> this focus) (the-as process-focusable s3-1) this)
          (if (and (logtest? (-> this bot-flags) (bot-flags attacked)) (!= (-> s3-1 type) target))
              (logclear! (-> this bot-flags) (bot-flags attacked))
              )
          )
        (else
          (clear-focused (-> this focus))
          (logclear! (-> this bot-flags) (bot-flags attacked))
          )
        )
      s3-1
      )
    )
  )

(defmethod alive? ((this kid))
  (let ((t9-0 (method-of-type bot alive?)))
    (and (t9-0 this)
         (not (and (-> this next-state) (let ((v1-4 (-> this next-state name)))
                                          (or (= v1-4 'arrested) (= v1-4 'die) (= v1-4 'die-falling))
                                          )
                   )
              )
         (not (logtest? (bot-flags bf20) (-> this bot-flags)))
         )
    )
  )

(defmethod init-enemy-collision! ((this kid))
  "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 hit-by-others))))
    (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 3) 0)))
      (set! (-> s5-0 total-prims) (the-as uint 4))
      (set! (-> s4-0 prim-core collide-as) (collide-spec bot bot-targetable))
      (set! (-> s4-0 prim-core collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> s4-0 prim-core action) (collide-action solid can-ride no-standon))
      (set-vector! (-> s4-0 local-sphere) 0.0 3276.8 0.0 4505.6)
      (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 bot bot-targetable))
      (set! (-> v1-13 prim-core collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> v1-13 prim-core action) (collide-action solid can-ride))
      (set-vector! (-> v1-13 local-sphere) 0.0 2048.0 0.0 2048.0)
      )
    (let ((v1-15 (new 'process 'collide-shape-prim-sphere s5-0 (the-as uint 0))))
      (set! (-> v1-15 prim-core collide-as) (collide-spec bot bot-targetable))
      (set! (-> v1-15 prim-core collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> v1-15 prim-core action) (collide-action solid))
      (set-vector! (-> v1-15 local-sphere) 0.0 3584.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 bot bot-targetable))
      (set! (-> v1-17 prim-core collide-with)
            (collide-spec backgnd jak crate civilian enemy obstacle vehicle-sphere hit-by-others-list player-list pusher)
            )
      (set! (-> v1-17 prim-core action) (collide-action solid no-standon))
      (set-vector! (-> v1-17 local-sphere) 0.0 5120.0 0.0 2048.0)
      )
    (set! (-> s5-0 nav-radius) 3481.6)
    (let ((v1-19 (-> s5-0 root-prim)))
      (set! (-> s5-0 backup-collide-as) (-> v1-19 prim-core collide-as))
      (set! (-> s5-0 backup-collide-with) (-> v1-19 prim-core collide-with))
      )
    (set! (-> s5-0 max-iteration-count) (the-as uint 3))
    (set! (-> s5-0 event-priority) (the-as uint 3))
    (set! (-> this root) s5-0)
    )
  0
  (none)
  )

(defmethod init! ((this kid))
  "Set defaults for various fields."
  (let ((t9-0 (method-of-type bot init!)))
    (t9-0 this)
    )
  (set! (-> this min-speed) 6144.0)
  (set! (-> this max-speed) 22528.0)
  (set! (-> this channel) (gui-channel kid))
  (set! (-> this notice-enemy-dist) 122880.0)
  (set! (-> this travel-anim-interp) 0.0)
  (set! (-> this focus-info max-los-dist) 0.0)
  (set-vector! (-> this root scale) 1.2 1.2 1.2 1.0)
  (set! (-> this spot-color) (the-as uint #x60004f8f))
  (set! (-> this arrestor-handle) (the-as handle #f))
  0
  (none)
  )

(defmethod init-enemy! ((this kid))
  "Common method called to initialize the enemy, typically sets up default field values and calls ancillary helper methods"
  (init! this)
  (initialize-skeleton
    this
    (the-as skeleton-group (art-group-get-by-name *level* "skel-kid" (the-as (pointer uint32) #f)))
    (the-as pair 0)
    )
  (init-enemy-behaviour-and-stats! this *kid-nav-enemy-info*)
  (let ((v1-7 (-> this neck)))
    (set! (-> v1-7 up) (the-as uint 1))
    (set! (-> v1-7 nose) (the-as uint 2))
    (set! (-> v1-7 ear) (the-as uint 0))
    (set-vector! (-> v1-7 twist-max) 11832.889 11832.889 0.0 1.0)
    (set! (-> v1-7 ignore-angle) 30947.555)
    )
  (let ((t9-4 (method-of-type bot init-enemy!)))
    (t9-4 this)
    )
  (set! (-> this my-simple-focus) (process-spawn simple-focus :to this))
  0
  (none)
  )

(defmethod bot-method-214 ((this kid))
  #f
  )

;; WARN: Return type mismatch none vs object.
(defmethod go-hostile ((this kid))
  (kid-method-232 this)
  )

;; WARN: Return type mismatch none vs object.
(defmethod react-to-focus ((this kid))
  "@TODO - flesh out docs"
  (kid-method-232 this)
  )

;; WARN: Return type mismatch object vs none.
(defmethod kid-method-233 ((this kid))
  (go (method-of-object this waiting-turn))
  (none)
  )

;; WARN: Return type mismatch object vs none.
(defmethod kid-method-232 ((this kid))
  (let ((s5-0 (handle->process (-> this arrestor-handle))))
    (cond
      ((if (type? s5-0 process-focusable)
           s5-0
           )
       (go (method-of-object this arrested))
       )
      ((logtest? (bot-flags bf19) (-> this bot-flags))
       (go (method-of-object this scared-idle))
       )
      (else
        (go (method-of-object this waiting-idle))
        )
      )
    )
  (none)
  )

(defmethod kid-method-234 ((this kid))
  (with-pp
    (let ((f30-0 (-> this nav state speed)))
      (let ((f0-1 (lerp-scale 0.0 1.0 f30-0 6144.0 22528.0)))
        (seek! (-> this travel-anim-interp) f0-1 (* 4.0 (seconds-per-frame)))
        )
      (let ((f28-0 (-> this travel-anim-interp))
            (v1-7 (if (> (-> this skel active-channels) 0)
                      (-> this skel root-channel 0 frame-group)
                      )
                  )
            )
        (cond
          ((and v1-7 (= v1-7 (-> this draw art-group data 5)))
           (let ((v1-12 (-> this skel root-channel 1)))
             (set! (-> v1-12 frame-interp 1) f28-0)
             (set! (-> v1-12 frame-interp 0) f28-0)
             )
           (let* ((f28-1 (current-cycle-distance (-> this skel)))
                  (f0-5 (quaternion-y-angle (-> this root quat)))
                  (f1-2 (deg- f0-5 (-> this travel-prev-ry)))
                  (f0-8 (fmin 22528.0 (* 0.35342914 (-> pp clock frames-per-second) (fabs f1-2))))
                  (f0-11 (/ (* 8.0 (fmax f30-0 f0-8)) (* 15.0 f28-1)))
                  (a0-10 (-> this skel root-channel 0))
                  )
             (set! (-> a0-10 param 0) f0-11)
             (joint-control-channel-group-eval! a0-10 (the-as art-joint-anim #f) num-func-loop!)
             )
           (let ((a0-11 (-> this skel root-channel 1)))
             (set! (-> a0-11 param 0) 0.0)
             (joint-control-channel-group-eval! a0-11 (the-as art-joint-anim #f) num-func-chan)
             )
           )
          (else
            (ja-channel-push! 2 (seconds 0.15))
            (let ((a0-13 (-> this skel root-channel 0)))
              (set! (-> a0-13 dist) 3276.8)
              (set! (-> a0-13 frame-group) (the-as art-joint-anim (-> this draw art-group data 5)))
              (set! (-> a0-13 param 0) 1.0)
              (joint-control-channel-group! a0-13 (the-as art-joint-anim (-> this draw art-group data 5)) num-func-loop!)
              )
            (let ((a0-14 (-> this skel root-channel 1)))
              (set! (-> a0-14 frame-interp 1) f28-0)
              (set! (-> a0-14 frame-interp 0) f28-0)
              (set! (-> a0-14 dist) 8737.997)
              (set! (-> a0-14 frame-group) (the-as art-joint-anim (-> this draw art-group data 6)))
              (set! (-> a0-14 param 0) 0.0)
              (joint-control-channel-group! a0-14 (the-as art-joint-anim (-> this draw art-group data 6)) num-func-chan)
              )
            )
          )
        )
      )
    (none)
    )
  )

(defmethod damage-amount-from-attack ((this kid) (arg0 process) (arg1 event-message-block))
  "@returns the amount of damage taken from an attack.  This can come straight off the [[attack-info]] or via [[penetrate-using->damage]]"
  (if (= (-> this course course-id) 7)
      0
      ((method-of-type bot damage-amount-from-attack) this arg0 arg1)
      )
  )

(defmethod bot-method-190 ((this kid))
  #t
  )

(defmethod enemy-method-78 ((this kid) (arg0 enemy-knocked-info))
  (ja-channel-push! 1 (seconds 0.1))
  (let ((a1-2 (-> this draw art-group data (-> this enemy-info knocked-land-anim)))
        (a0-4 (-> this skel root-channel 0))
        )
    (set! (-> a0-4 frame-group) (the-as art-joint-anim a1-2))
    (set! (-> a0-4 param 0) (the float (+ (-> (the-as art-joint-anim a1-2) frames num-frames) -1)))
    (set! (-> a0-4 param 1) (-> arg0 anim-speed))
    (set! (-> a0-4 frame-num) 0.0)
    (joint-control-channel-group! a0-4 (the-as art-joint-anim a1-2) num-func-seek!)
    )
  #t
  )

;; WARN: Return type mismatch float vs meters.
(defmethod set-cam-height! ((this kid) (arg0 vector))
  (the-as
    meters
    (cond
      ((and (-> this next-state) (= (-> this next-state name) 'arrested))
       (set-vector! arg0 49152.0 12288.0 49152.0 1.0)
       (vector<-cspace+vector! arg0 (-> this node-list data 2) arg0)
       (if (focus-test? this under-water)
           (set! (-> arg0 y) (+ (get-water-height this) (-> *setting-control* cam-current target-height)))
           )
       )
      (else
        ((method-of-type bot set-cam-height!) this arg0)
        )
      )
    )
  )

(defmethod enemy-method-102 ((this kid))
  (if (logtest? (bot-flags bf20) (-> this bot-flags))
      #f
      ((method-of-type bot enemy-method-102) this)
      )
  )

;; WARN: Return type mismatch object vs none.
(defmethod go-idle ((this kid))
  (cond
    ((= (-> this course course-id) 7)
     (cond
       ((task-node-closed? (game-task-node city-help-kid-resolution))
        (cleanup-for-death this)
        (go (method-of-object this die-fast))
        )
       (else
         (go (method-of-object this waiting-with-kor))
         )
       )
     )
    (else
      (go (method-of-object this waiting-idle))
      )
    )
  (none)
  )
