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

;; name: bot-states.gc
;; name in dgo: bot-states
;; dgos: ATE, SEB, SWB, LKIDDOGE, UNB, CTYKORA, CTYASHA

;; DECOMP BEGINS

(defstate hit (bot)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy hit) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (stop-speech self (the-as uint 0) #t)
    (bot-method-191 self)
    (logclear! (-> self bot-flags) (bot-flags bf11))
    )
  :exit (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy hit) exit)))
      (if t9-0
          (t9-0)
          )
      )
    (logior! (-> self bot-flags) (bot-flags bf11))
    )
  :code (behavior ()
    (local-vars (v1-37 enemy-flag) (v1-45 enemy-flag))
    (ja-channel-push! 1 (seconds 0.2))
    (let ((f30-0 (rnd-float-range self 0.9 1.1)))
      (ja-no-eval :group! (-> self draw art-group data (-> self enemy-info hit-anim))
                  :num! (seek! max f30-0)
                  :frame-num 0.0
                  )
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek! max f30-0))
        )
      )
    (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-36 (-> self enemy-flags)))
      (if (logtest? v1-36 (enemy-flag vulnerable-backup))
          (set! v1-37 (logior v1-36 (enemy-flag vulnerable)))
          (set! v1-37 (logclear v1-36 (enemy-flag vulnerable)))
          )
      )
    (set! (-> self enemy-flags) v1-37)
    (if (logtest? (-> self enemy-flags) (enemy-flag attackable-backup))
        (logior! (-> self mask) (process-mask collectable))
        (logclear! (-> self mask) (process-mask collectable))
        )
    (let ((v1-44 (-> self enemy-flags)))
      (if (logtest? (enemy-flag trackable-backup) v1-44)
          (set! v1-45 (logior (enemy-flag trackable) v1-44))
          (set! v1-45 (logclear v1-44 (enemy-flag trackable)))
          )
      )
    (set! (-> self enemy-flags) v1-45)
    (logclear! (-> self enemy-flags) (enemy-flag lock-focus))
    (logclear! (-> self focus-status) (focus-status hit))
    (react-to-focus self)
    )
  )

(defstate knocked (bot)
  :virtual #t
  :enter (behavior ()
    (logclear! (-> self enemy-flags) (enemy-flag victory))
    (stop-speech self (the-as uint 0) #t)
    (if (and (logtest? (-> self bot-flags) (bot-flags attacked))
             (nonzero? (-> self hit-points))
             (zero? (-> self fated-time))
             )
        (logior! (-> self bot-flags) (bot-flags bf12))
        )
    (bot-method-191 self)
    (when (zero? (-> self hit-points))
      (set! (-> self hit-points) 1)
      (set-time! (-> self fated-time))
      )
    (logclear! (-> self bot-flags) (bot-flags bf11))
    (let ((t9-2 (-> (method-of-type nav-enemy knocked) enter)))
      (if t9-2
          (t9-2)
          )
      )
    )
  :exit (behavior ()
    (let ((gp-0 (-> self incoming attack-id)))
      (let ((t9-0 (-> (method-of-type nav-enemy knocked) exit)))
        (if t9-0
            (t9-0)
            )
        )
      (set! (-> self incoming attack-id) gp-0)
      )
    (logior! (-> self bot-flags) (bot-flags bf11))
    (logclear! (-> self bot-flags) (bot-flags bf12))
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy knocked) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (when (and (logtest? (-> self bot-flags) (bot-flags bf12)) (!= (-> self state-time) (current-time)))
      (logclear! (-> self bot-flags) (bot-flags bf12))
      (play-attacked-speech self)
      )
    )
  )

(defstate die-falling (bot)
  :virtual #t
  :enter (behavior ()
    (let ((t9-0 (-> (method-of-type nav-enemy die-falling) enter)))
      (if t9-0
          (t9-0)
          )
      )
    (kill-nearby-enemies (-> self root trans) 61440.0)
    (stop-speech self (the-as uint 0) #f)
    (logclear! (-> self bot-flags) (bot-flags bf06 bf11))
    (set! (-> self delay-too-far-check) -1)
    (logior! (-> self root nav-flags) (nav-flags has-root-sphere))
    (fail-mission! self)
    )
  :exit (behavior ()
    (logior! (-> self bot-flags) (bot-flags bf11))
    (logclear! (-> self bot-flags) (bot-flags bf13 bf14))
    (let ((t9-0 (-> (method-of-type nav-enemy die-falling) exit)))
      (if t9-0
          (t9-0)
          )
      )
    )
  :trans (behavior ()
    (fail-falling self)
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.1))
    (let ((gp-0 (-> self draw art-group data (if (enemy-method-133 self)
                                                 (-> self enemy-info die-falling-anim)
                                                 (-> self enemy-info die-anim)
                                                 )
                    )
                )
          (f30-0 (rnd-float-range self 0.8 1.2))
          )
      (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))
        )
      )
    (until (not (channel-active? self (gui-channel none)))
      (suspend)
      )
    (until #f
      (if (and (logtest? (-> self bot-flags) (bot-flags failed)) (reset? *fail-mission-control*))
          (reset! *fail-mission-control*)
          )
      (suspend)
      )
    #f
    )
  )

(defstate failed (bot)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    ((-> (method-of-type bot die-falling) enter))
    (logclear! (-> self bot-flags) (bot-flags bf11))
    (when (logtest? (-> self bot-flags) (bot-flags failed))
      (let ((a0-1 (-> self course too-far-fail-speeches)))
        (when (and (nonzero? a0-1) a0-1)
          (let ((a1-1 (bot-speech-list-method-9 a0-1 self (-> self course speeches) (speech-flags))))
            (if (>= a1-1 0)
                (play-speech self a1-1)
                )
            )
          )
        )
      )
    )
  :exit (behavior ()
    (logior! (-> self bot-flags) (bot-flags bf11))
    )
  :trans (behavior ()
    ((-> (method-of-type bot die-falling) trans))
    (if (channel-active? self (gui-channel none))
        (set-time! (-> self state-time))
        )
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.1))
    (let ((f30-0 (rnd-float-range self 0.9 1.1))
          (gp-0 (-> self draw art-group data (-> self enemy-info idle-anim)))
          )
      (until #f
        (ja-no-eval :group! gp-0 :num! (seek! max f30-0) :frame-num 0.0)
        (until (ja-done? 0)
          (when (and (logtest? (-> self bot-flags) (bot-flags failed))
                     (time-elapsed? (-> self state-time) (seconds 0.5))
                     (reset? *fail-mission-control*)
                     )
            (cleanup-for-death self)
            (process-entity-status! self (entity-perm-status no-kill) #f)
            (reset! *fail-mission-control*)
            )
          (suspend)
          (ja :num! (seek! max f30-0))
          )
        )
      )
    #f
    )
  :post nav-enemy-die-falling-post
  )

(defstate hidden (bot)
  :virtual #t
  :event enemy-event-handler
  :enter (behavior ()
    (logior! (-> self focus-status) (focus-status disable))
    (let ((v1-3 (-> self root root-prim)))
      (set! (-> v1-3 prim-core collide-as) (collide-spec))
      (set! (-> v1-3 prim-core collide-with) (collide-spec))
      )
    0
    (logior! (-> self draw status) (draw-control-status no-draw))
    (set! (-> self notice-enemy-dist) 0.0)
    (logclear! (-> self enemy-flags) (enemy-flag check-water))
    )
  :exit (behavior ()
    (local-vars (v0-0 enemy-flag))
    (logclear! (-> self draw status) (draw-control-status no-draw))
    (logclear! (-> self focus-status) (focus-status disable))
    (let ((v1-5 (-> self root root-prim)))
      (set! (-> v1-5 prim-core collide-as) (-> self root backup-collide-as))
      (set! (-> v1-5 prim-core collide-with) (-> self root backup-collide-with))
      )
    (let ((v1-6 (-> self enemy-flags)))
      (if (logtest? (enemy-flag check-water-backup) v1-6)
          (set! v0-0 (logior (enemy-flag check-water) v1-6))
          (set! v0-0 (logclear v1-6 (enemy-flag check-water)))
          )
      )
    (set! (-> self enemy-flags) v0-0)
    )
  :code sleep-code
  :post nav-enemy-simple-post
  )
