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

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

(declare-type crimson-guard citizen)

;; DECOMP BEGINS

(defstate waiting-idle (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logclear (-> v1-2 enemy-flags) (enemy-flag enemy-flag36))))
      (set! (-> v1-2 nav callback-info) *nav-enemy-null-callback-info*)
      )
    0
    (nav-enemy-method-167 self)
    (let ((v1-7 self))
      (set! (-> v1-7 enemy-flags) (the-as enemy-flag (logclear (-> v1-7 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (vector-reset! (-> self root transv))
    (look-at-target! self (enemy-flag look-at-focus))
    (set! (-> self travel-anim-interp) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (b!
      (not (and (-> self focus-info fproc) (>= (fabs (-> self focus-info ry-diff)) 9102.223)))
      cfg-6
      :delay (empty-form)
      )
    (kid-method-233 self)
    (b! #t cfg-15 :delay (nop!))
    (label cfg-6)
    (let ((gp-0 (handle->process (-> self arrestor-handle))))
      (if (if (type? gp-0 process-focusable)
              gp-0
              )
          (go-virtual arrested)
          )
      )
    (label cfg-15)
    (when (not (focus-test? self grabbed))
      (if (not (outside-spot-radius? self (the-as bot-spot #f) (the-as vector #f) #f))
          (go-virtual traveling)
          )
      )
    )
  :code (behavior ()
    (let ((v1-2 (ja-group)))
      (cond
        ((and v1-2 (= v1-2 kid-idle0-ja))
         (ja-no-eval :num! (seek!))
         (while (not (ja-done? 0))
           (suspend)
           (ja-eval)
           )
         )
        (else
          (ja-channel-push! 1 (seconds 0.2))
          )
        )
      )
    (until #f
      (ja-no-eval :group! kid-idle0-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    #f
    )
  :post nav-enemy-simple-post
  )

(defstate waiting-turn (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logclear (-> v1-2 enemy-flags) (enemy-flag enemy-flag36))))
      (set! (-> v1-2 nav callback-info) *nav-enemy-null-callback-info*)
      )
    0
    (nav-enemy-method-167 self)
    (let ((v1-7 self))
      (set! (-> v1-7 enemy-flags) (the-as enemy-flag (logclear (-> v1-7 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (look-at-target! self (enemy-flag look-at-focus))
    (set! (-> self travel-anim-interp) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (when (not (focus-test? self grabbed))
      (if (and (not (outside-spot-radius? self (the-as bot-spot #f) (the-as vector #f) #f))
               (let ((gp-0 (handle->process (-> self arrestor-handle))))
                 (not (if (type? gp-0 process-focusable)
                          gp-0
                          )
                      )
                 )
               )
          (go-virtual traveling)
          )
      )
    )
  :code (behavior ()
    (local-vars (s4-1 art-element) (f0-9 float))
    (let ((gp-0 (new 'stack-no-clear 'bot-turn-info)))
      (let ((s5-0 (-> self focus-info fproc)))
        (if (not s5-0)
            (kid-method-232 self)
            )
        (turn-to-target self gp-0 s5-0 0.8)
        )
      (let ((s4-0 (>= (-> gp-0 predicted-ry-diff) 0.0)))
        (let ((s5-1 (-> self draw art-group data (if s4-0
                                                     7
                                                     11
                                                     )
                        )
                    )
              )
          (ja-channel-push! 1 (seconds 0.4))
          (ja-no-eval :group! s5-1 :num! (seek!) :frame-num 0.0)
          )
        (until (ja-done? 0)
          (suspend)
          (ja :num! (seek!))
          )
        (let ((a2-3 (-> self focus-info fproc)))
          (if a2-3
              (turn-to-target self gp-0 a2-3 0.533)
              )
          )
        (let ((s5-2 (new 'stack-no-clear 'quaternion))
              (f0-8 (-> gp-0 predicted-ry-diff))
              (f30-0 5.0)
              (f28-0 7.0)
              )
          (cond
            (s4-0
              (if (< f0-8 0.0)
                  (set! f0-8 (+ 65536.0 f0-8))
                  )
              (cond
                ((< f0-8 18204.445)
                 (set! f0-9 9102.223)
                 (set! s4-1 kid-turn-left50-end0-ja)
                 (set! f28-0 8.0)
                 )
                ((< f0-8 27306.666)
                 (set! f0-9 18204.445)
                 (set! s4-1 kid-turn-left100-end0-ja)
                 )
                (else
                  (set! f0-9 27306.666)
                  (set! s4-1 kid-turn-left150-end0-ja)
                  )
                )
              )
            (else
              (if (>= f0-8 0.0)
                  (set! f0-8 (+ -65536.0 f0-8))
                  )
              (cond
                ((< -18204.445 f0-8)
                 (set! f0-9 -9102.223)
                 (set! s4-1 kid-turn-right50-end0-ja)
                 (set! f28-0 8.0)
                 )
                ((< -27306.666 f0-8)
                 (set! f0-9 -18204.445)
                 (set! s4-1 kid-turn-right100-end0-ja)
                 )
                (else
                  (set! f0-9 -27306.666)
                  (set! s4-1 kid-turn-right150-end0-ja)
                  )
                )
              )
            )
          (quaternion-rotate-y! s5-2 (-> gp-0 src-quat) f0-9)
          (ja-no-eval :group! s4-1 :num! (seek!) :frame-num 0.0)
          (until (ja-done? 0)
            (let ((f0-14 (ja-aframe-num 0)))
              (cond
                ((< f0-14 f28-0)
                 (if (>= f0-14 f30-0)
                     (quaternion-slerp! (-> self root quat) (-> gp-0 src-quat) s5-2 (/ (- f0-14 f30-0) (- f28-0 f30-0)))
                     )
                 )
                (else
                  (quaternion-copy! (-> self root quat) s5-2)
                  )
                )
              )
            (suspend)
            (ja :num! (seek!))
            )
          )
        )
      )
    (kid-method-232 self)
    )
  :post nav-enemy-simple-post
  )

(defstate waiting-with-kor (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (-> (method-of-type kid waiting-idle) enter)
  :exit (-> (method-of-type kid waiting-idle) exit)
  :trans (behavior ()
    (bot-method-223 self #f)
    (when (not (focus-test? self grabbed))
      (if (not (outside-spot-radius? self (the-as bot-spot #f) (the-as vector #f) #f))
          (go-virtual traveling)
          )
      )
    )
  :code (-> (method-of-type kid waiting-idle) code)
  :post (-> (method-of-type kid waiting-idle) post)
  )

(defstate scared-idle (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logclear (-> v1-2 enemy-flags) (enemy-flag enemy-flag36))))
      (set! (-> v1-2 nav callback-info) *nav-enemy-null-callback-info*)
      )
    0
    (nav-enemy-method-167 self)
    (let ((v1-7 self))
      (set! (-> v1-7 enemy-flags) (the-as enemy-flag (logclear (-> v1-7 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (vector-reset! (-> self root transv))
    (look-at-target! self (enemy-flag look-at-focus))
    (set! (-> self travel-anim-interp) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (b!
      (not (and (-> self focus-info fproc) (>= (fabs (-> self focus-info ry-diff)) 9102.223)))
      cfg-6
      :delay (empty-form)
      )
    (kid-method-233 self)
    (b! #t cfg-15 :delay (nop!))
    (label cfg-6)
    (let ((gp-0 (handle->process (-> self arrestor-handle))))
      (if (if (type? gp-0 process-focusable)
              gp-0
              )
          (go-virtual arrested)
          )
      )
    (label cfg-15)
    (when (not (focus-test? self grabbed))
      (if (not (outside-spot-radius? self (the-as bot-spot #f) (the-as vector #f) #f))
          (go-virtual traveling)
          )
      )
    )
  :code (behavior ()
    (let ((v1-2 (ja-group)))
      (cond
        ((and v1-2 (= v1-2 kid-scared0-ja))
         (ja-no-eval :num! (seek!))
         (while (not (ja-done? 0))
           (suspend)
           (ja-eval)
           )
         )
        (else
          (ja-channel-push! 1 (seconds 0.2))
          )
        )
      )
    (until #f
      (ja-no-eval :group! kid-scared0-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    #f
    )
  :post nav-enemy-simple-post
  )

(defstate traveling (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (if (not (logtest? (enemy-flag enemy-flag36) (-> v1-2 enemy-flags)))
          (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag38) (-> v1-2 enemy-flags))))
          )
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag36) (-> v1-2 enemy-flags))))
      (set! (-> v1-2 nav callback-info) (-> v1-2 enemy-info callback-info))
      )
    0
    (let ((v1-5 self))
      (set! (-> v1-5 enemy-flags) (the-as enemy-flag (logior (enemy-flag enemy-flag37) (-> v1-5 enemy-flags))))
      )
    0
    (stop-looking-at-target! self)
    (nav-enemy-method-166 self)
    (set! (-> self player-blocking) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (let ((gp-0 (handle->process (-> self arrestor-handle))))
      (cond
        ((if (type? gp-0 process-focusable)
             gp-0
             )
         (go-virtual arrested)
         )
        ((outside-spot-radius? self (the-as bot-spot #f) (the-as vector #f) #t)
         (kid-method-232 self)
         )
        ((and (time-elapsed? (-> self state-time) (seconds 0.5)) (bot-method-208 self))
         (go-virtual traveling-blocked)
         )
        ((and (nav-enemy-method-163 self) (time-elapsed? (-> self state-time) (-> self reaction-time)))
         (go-stare2 self)
         )
        )
      )
    0
    )
  :code (behavior ()
    (until #f
      (kid-method-234 self)
      (suspend)
      )
    #f
    )
  :post (behavior ()
    (let ((a0-0 (-> self nav state))
          (v1-1 (-> self spot))
          )
      (logclear! (-> a0-0 flags) (nav-state-flag directional-mode))
      (logior! (-> a0-0 flags) (nav-state-flag target-poly-dirty))
      (set! (-> a0-0 target-post quad) (-> v1-1 center quad))
      )
    0
    (nav-enemy-travel-post)
    )
  )

(defstate traveling-blocked (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logclear (-> v1-2 enemy-flags) (enemy-flag enemy-flag36))))
      (set! (-> v1-2 nav callback-info) *nav-enemy-null-callback-info*)
      )
    0
    (nav-enemy-method-167 self)
    (let ((v1-7 self))
      (set! (-> v1-7 enemy-flags) (the-as enemy-flag (logclear (-> v1-7 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (vector-reset! (-> self root transv))
    (look-at-target! self (enemy-flag look-at-focus))
    (set! (-> self travel-anim-interp) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (let ((gp-0 (handle->process (-> self arrestor-handle))))
      (cond
        ((if (type? gp-0 process-focusable)
             gp-0
             )
         (go-virtual arrested)
         )
        ((and (time-elapsed? (-> self state-time) (seconds 1)) (not (bot-method-208 self)))
         (go-virtual traveling)
         )
        )
      )
    )
  :code (-> (method-of-type kid waiting-idle) code)
  :post nav-enemy-simple-post
  )

(defstate stare (kid)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type bot stare) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (let ((v1-4 self))
      (set! (-> v1-4 enemy-flags) (the-as enemy-flag (logclear (-> v1-4 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (set! (-> self travel-anim-interp) 0.0)
    )
  :trans (behavior ()
    (bot-method-223 self #f)
    (let ((gp-0 (handle->process (-> self arrestor-handle))))
      (if (if (type? gp-0 process-focusable)
              gp-0
              )
          (go-virtual arrested)
          )
      )
    (when (time-elapsed? (-> self state-time) (seconds 0.1))
      (if (not (nav-enemy-method-163 self))
          (go-virtual traveling)
          )
      )
    )
  :code (-> (method-of-type kid waiting-idle) code)
  )

(defstate hit (kid)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type bot hit) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (set! (-> self travel-anim-interp) 0.0)
    )
  :code (behavior ()
    (local-vars (v1-31 enemy-flag) (v1-39 enemy-flag))
    (ja-channel-push! 1 (seconds 0.1))
    (ja-no-eval :group! kid-idle0-ja :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (if (logtest? (-> self enemy-flags) (enemy-flag dangerous-backup))
        (logior! (-> self focus-status) (focus-status dangerous))
        (logclear! (-> self focus-status) (focus-status dangerous))
        )
    (let ((v1-30 (-> self enemy-flags)))
      (if (logtest? v1-30 (enemy-flag vulnerable-backup))
          (set! v1-31 (logior v1-30 (enemy-flag vulnerable)))
          (set! v1-31 (logclear v1-30 (enemy-flag vulnerable)))
          )
      )
    (set! (-> self enemy-flags) v1-31)
    (if (logtest? (-> self enemy-flags) (enemy-flag attackable-backup))
        (logior! (-> self mask) (process-mask collectable))
        (logclear! (-> self mask) (process-mask collectable))
        )
    (let ((v1-38 (-> self enemy-flags)))
      (if (logtest? (enemy-flag trackable-backup) v1-38)
          (set! v1-39 (logior (enemy-flag trackable) v1-38))
          (set! v1-39 (logclear v1-38 (enemy-flag trackable)))
          )
      )
    (set! (-> self enemy-flags) v1-39)
    (logclear! (-> self enemy-flags) (enemy-flag lock-focus))
    (logclear! (-> self focus-status) (focus-status hit))
    (react-to-focus self)
    )
  )

(defstate knocked (kid)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type bot knocked) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (set! (-> self travel-anim-interp) 0.0)
    )
  )

(defstate arrested (kid)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (set-time! (-> self state-time))
    (let ((v1-2 self))
      (set! (-> v1-2 enemy-flags) (the-as enemy-flag (logclear (-> v1-2 enemy-flags) (enemy-flag enemy-flag36))))
      (set! (-> v1-2 nav callback-info) *nav-enemy-null-callback-info*)
      )
    0
    (nav-enemy-method-167 self)
    (let ((v1-7 self))
      (set! (-> v1-7 enemy-flags) (the-as enemy-flag (logclear (-> v1-7 enemy-flags) (enemy-flag enemy-flag37))))
      )
    0
    (vector-reset! (-> self root transv))
    (stop-looking-at-target! self)
    (set! (-> self travel-anim-interp) 0.0)
    (if (logtest? (bot-flags bf20) (-> self bot-flags))
        (logclear! (-> self enemy-flags) (enemy-flag vulnerable vulnerable-backup))
        )
    )
  :trans (behavior ()
    (cond
      ((logtest? (-> self bot-flags) (bot-flags bf09))
       (fail-falling self)
       )
      (else
        (bot-method-223 self #f)
        (when (not (logtest? (bot-flags bf20) (-> self bot-flags)))
          (if (and (-> self focus-info fproc) (>= (fabs (-> self focus-info ry-diff)) 9102.223))
              (kid-method-233 self)
              )
          (when (not (focus-test? self grabbed))
            (let ((gp-0 (handle->process (-> self arrestor-handle))))
              (if (not (if (type? gp-0 process-focusable)
                           gp-0
                           )
                       )
                  (react-to-focus self)
                  )
              )
            )
          )
        )
      )
    )
  :code (behavior ()
    (when (not (logtest? (bot-flags bf20) (-> self bot-flags)))
      (ja-channel-push! 1 (seconds 0.1))
      (ja-no-eval :group! kid-arrest-start-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    (fail-mission! self)
    (let* ((gp-0 (handle->process (-> self arrestor-handle)))
           (v1-32 (if (type? gp-0 process-focusable)
                      gp-0
                      )
                  )
           )
      (if v1-32
          (logclear! (-> (the-as crimson-guard v1-32) enemy-flags) (enemy-flag vulnerable vulnerable-backup))
          )
      )
    (let ((v1-35 (ja-group)))
      (if (not (and v1-35 (= v1-35 kid-arrest-start-ja)))
          (ja-channel-push! 1 (seconds 0.1))
          )
      )
    (set-time! (-> self state-time))
    (until #f
      (ja-no-eval :group! kid-arrest-idle0-ja :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (if (and (logtest? (-> self bot-flags) (bot-flags failed))
                 (time-elapsed? (-> self state-time) (seconds 4))
                 (reset? *fail-mission-control*)
                 )
            (reset! *fail-mission-control*)
            )
        (suspend)
        (ja :num! (seek!))
        )
      )
    #f
    )
  :post nav-enemy-simple-post
  )

(defstate failed (kid)
  :virtual #t
  :enter (behavior ()
    (logior! (-> self bot-flags) (bot-flags bf20))
    ((-> (method-of-type kid arrested) enter))
    )
  :trans (-> (method-of-type kid arrested) trans)
  :code (-> (method-of-type kid arrested) code)
  :post (-> (method-of-type kid arrested) post)
  )

(defstate die-falling (kid)
  :virtual #t
  :enter (-> (method-of-type kid failed) enter)
  :trans (-> (method-of-type kid failed) trans)
  :code (-> (method-of-type kid failed) code)
  )
