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

;; definition of type babak-with-cannon
(deftype babak-with-cannon (babak)
  ((cannon-ent entity  :offset-assert 400)
   (distance   float   :offset-assert 404)
   )
  :heap-base #x130
  :method-count-assert 76
  :size-assert         #x198
  :flag-assert         #x4c01300198
  (:states
    babak-with-cannon-jump-off-cannon
    babak-with-cannon-jump-onto-cannon
    babak-with-cannon-shooting
    )
  )

;; definition for method 3 of type babak-with-cannon
(defmethod inspect babak-with-cannon ((obj babak-with-cannon))
  (let ((t9-0 (method-of-type babak inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tcannon-ent: ~A~%" (-> obj cannon-ent))
  (format #t "~T~Tdistance: ~f~%" (-> obj distance))
  obj
  )

;; failed to figure out what this is:
nav-enemy-default-event-handler

;; failed to figure out what this is:
(defstate nav-enemy-idle (babak-with-cannon)
  :virtual #t
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-default-event-handler
    )
  :trans (behavior ()
    (if (and (and *target* (>= (-> self enemy-info idle-distance)
                               (vector-vector-distance (-> self collide-info trans) (-> *target* control trans))
                               )
                  )
             (>= (- (-> *display* base-frame-counter) (-> self state-time)) (-> self state-timeout))
             )
        (go-virtual nav-enemy-patrol)
        )
    (none)
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (ja-no-eval :group! (-> self draw art-group data (-> self nav-info idle-anim)) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (ja-post)
      (suspend)
      (ja :num! (seek!))
      )
    (anim-loop)
    (none)
    )
  :post (the-as (function none :behavior babak-with-cannon) #f)
  )

;; failed to figure out what this is:
(defstate nav-enemy-patrol (babak-with-cannon)
  :virtual #t
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-default-event-handler
    )
  :trans (behavior ()
    (when (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 0.1))
      (let ((f30-0 (- (-> (target-pos 0) y) (-> self collide-info trans y))))
        (if (and (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 3))
                 (or (or (not *target*)
                         (< (-> self distance) (vector-vector-distance (-> self collide-info trans) (-> *target* control trans)))
                         )
                     (< f30-0 -40960.0)
                     )
                 )
            (go babak-run-to-cannon)
            )
        )
      (when (>= (- (-> *display* base-frame-counter) (-> self state-time)) (-> self state-timeout))
        (if (or (not *target*) (< (-> self enemy-info idle-distance)
                                  (vector-vector-distance (-> self collide-info trans) (-> *target* control trans))
                                  )
                )
            (go-virtual nav-enemy-idle)
            )
        )
      (if (nav-enemy-notice-player?)
          (go-virtual nav-enemy-notice)
          )
      )
    (none)
    )
  :code (-> (method-of-type babak nav-enemy-patrol) code)
  )

;; failed to figure out what this is:
(defstate babak-run-to-cannon (babak)
  :event (the-as
    (function process int symbol event-message-block object :behavior babak)
    nav-enemy-default-event-handler
    )
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self nav destination-pos quad) (-> self entity extra trans quad))
    (set! (-> self nav target-pos quad) (-> self entity extra trans quad))
    (none)
    )
  :trans (behavior ()
    (if (nav-enemy-notice-player?)
        (go-virtual nav-enemy-chase)
        )
    (if (logtest? (nav-control-flags navcf19) (-> self nav flags))
        (go babak-with-cannon-jump-onto-cannon)
        )
    (none)
    )
  :code (behavior ()
    (ja-channel-push! 1 (seconds 0.075))
    (loop
      (ja-no-eval :group! (-> self draw art-group data 8) :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (suspend)
        (ja :num! (seek!))
        )
      )
    (none)
    )
  :post (behavior ()
    (nav-enemy-travel-post)
    (none)
    )
  )

;; definition for function handle->process-safe
(defun handle->process-safe ((arg0 handle))
  (let ((v0-0 (the-as process #f)))
    (if (handle->process arg0)
        (set! v0-0 (-> arg0 process 0))
        )
    v0-0
    )
  )

;; definition for function babak-with-cannon-compute-cannon-dir
;; Used lq/sq
(defun babak-with-cannon-compute-cannon-dir ((arg0 mistycannon) (arg1 vector))
  (set! (-> arg1 quad) (-> arg0 node-list data 3 bone transform vector 2 quad))
  arg1
  )

;; definition for function babak-with-cannon-compute-ride-point
;; Used lq/sq
(defun babak-with-cannon-compute-ride-point ((arg0 mistycannon) (arg1 vector))
  (set! (-> arg1 quad) (-> arg0 root-override trans quad))
  (let ((a1-4 (new 'static 'vector :y 18149.377 :z -17289.217 :w 1.0))
        (a2-0 (-> arg0 node-list data 3 bone transform))
        )
    (vector-matrix*! arg1 a1-4 a2-0)
    )
  arg1
  )

;; definition for function babak-with-cannon-ride-cannon-post
(defbehavior babak-with-cannon-ride-cannon-post babak-with-cannon ()
  (let* ((v1-0 (-> self cannon-ent))
         (s5-0 (if v1-0
                   (-> v1-0 extra process)
                   )
               )
         (gp-0 (if (and (nonzero? s5-0) (type-type? (-> s5-0 type) process-drawable))
                   s5-0
                   )
               )
         )
    (when gp-0
      (let ((s5-1 (new 'stack-no-clear 'vector)))
        (babak-with-cannon-compute-cannon-dir (the-as mistycannon gp-0) s5-1)
        (forward-up->quaternion (-> self collide-info quat) s5-1 *y-vector*)
        )
      (babak-with-cannon-compute-ride-point (the-as mistycannon gp-0) (-> self collide-info trans))
      )
    )
  (nav-enemy-simple-post)
  (none)
  )

;; failed to figure out what this is:
(defstate babak-with-cannon-jump-onto-cannon (babak-with-cannon)
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-jump-event-handler
    )
  :exit (behavior ()
    (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-rotate enable-travel))
    (none)
    )
  :code (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self rotate-speed) (-> self nav-info run-rotate-speed))
    (set! (-> self turn-time) (-> self nav-info run-turn-time))
    (logclear! (-> self nav-enemy-flags) (nav-enemy-flags enable-rotate enable-travel))
    (nav-enemy-neck-control-inactive)
    (let* ((v1-7 (-> self cannon-ent))
           (gp-0 (if v1-7
                     (-> v1-7 extra process)
                     )
                 )
           (a0-2 (if (and (nonzero? gp-0) (type-type? (-> gp-0 type) process-drawable))
                     gp-0
                     )
                 )
           )
      (cond
        (a0-2
          (babak-with-cannon-compute-ride-point (the-as mistycannon a0-2) (-> self event-param-point))
          (nav-enemy-initialize-jump (-> self event-param-point))
          )
        (else
          (go-virtual nav-enemy-idle)
          )
        )
      )
    (when (not (nav-enemy-facing-point? (-> self jump-dest) 5461.3335))
      (ja-channel-push! 1 (seconds 0.2))
      (nav-enemy-turn-to-face-point (-> self jump-dest) 1820.4445)
      )
    (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel))
    (nav-enemy-execute-jump)
    (logclear! (-> self nav-enemy-flags) (nav-enemy-flags enable-rotate enable-travel))
    (let* ((v1-20 (-> self cannon-ent))
           (gp-1 (if v1-20
                     (-> v1-20 extra process)
                     )
                 )
           (a0-9 (if (and (nonzero? gp-1) (type-type? (-> gp-1 type) process-drawable))
                     gp-1
                     )
                 )
           )
      (when a0-9
        (let ((gp-2 (babak-with-cannon-compute-cannon-dir (the-as mistycannon a0-9) (new 'stack-no-clear 'vector))))
          (when (not (nav-enemy-facing-direction? gp-2 1820.4445))
            (ja-channel-push! 1 (seconds 0.2))
            (nav-enemy-turn-to-face-dir gp-2 182.04445)
            (forward-up->quaternion (-> self collide-info quat) gp-2 *y-vector*)
            )
          )
        )
      )
    (ja-channel-push! 1 (seconds 0.1))
    (ja-no-eval :group! (-> self draw art-group data 17) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek!))
      )
    (go babak-with-cannon-shooting)
    (none)
    )
  :post (the-as (function none :behavior babak-with-cannon) nav-enemy-jump-post)
  )

;; failed to figure out what this is:
(defstate babak-with-cannon-jump-off-cannon (babak-with-cannon)
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-jump-event-handler
    )
  :exit (behavior ()
    (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-rotate enable-travel))
    (none)
    )
  :code (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (nav-enemy-initialize-jump (-> self entity extra trans))
    (nav-enemy-neck-control-look-at)
    (logclear! (-> self nav-enemy-flags) (nav-enemy-flags enable-rotate enable-travel))
    (ja-no-eval :group! (-> self draw art-group data 17) :num! (seek! 0.0 2.0) :frame-num max)
    (until (ja-done? 0)
      (suspend)
      (ja :num! (seek! 0.0 2.0))
      )
    (when (not (nav-enemy-facing-point? (-> self jump-dest) 5461.3335))
      (ja-channel-push! 1 (seconds 0.2))
      (nav-enemy-turn-to-face-point (-> self jump-dest) 1820.4445)
      )
    (logior! (-> self nav-enemy-flags) (nav-enemy-flags enable-travel))
    (nav-enemy-execute-jump)
    (let ((a1-6 (dummy-16 (-> self nav) (-> self jump-dest))))
      (set-current-poly! (-> self nav) a1-6)
      )
    (go-virtual nav-enemy-jump-land)
    (none)
    )
  :post (the-as (function none :behavior babak-with-cannon) nav-enemy-jump-post)
  )

;; failed to figure out what this is:
(defstate babak-with-cannon-shooting (babak-with-cannon)
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-default-event-handler
    )
  :enter (behavior ()
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (let ((v1-2 (entity-actor-lookup (-> self entity) 'alt-actor 0)))
      (if v1-2
          (logior! (-> v1-2 extra perm status) (entity-perm-status complete))
          )
      )
    (none)
    )
  :exit (behavior ()
    (let ((v1-0 (entity-actor-lookup (-> self entity) 'alt-actor 0)))
      (if v1-0
          (logclear! (-> v1-0 extra perm status) (entity-perm-status complete))
          )
      )
    (none)
    )
  :trans (behavior ()
    (let ((f0-1 (- (-> (target-pos 0) y) (-> self collide-info trans y))))
      (if (and (< -40960.0 f0-1)
               (and (and *target*
                         (>= (-> self distance) (vector-vector-distance (-> self collide-info trans) (-> *target* control trans)))
                         )
                    (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 3))
                    )
               )
          (go babak-with-cannon-jump-off-cannon)
          )
      )
    (none)
    )
  :code (behavior ()
    (loop
      (suspend)
      )
    (none)
    )
  :post babak-with-cannon-ride-cannon-post
  )

;; failed to figure out what this is:
(defstate nav-enemy-die (babak-with-cannon)
  :virtual #t
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    nav-enemy-default-event-handler
    )
  :trans (behavior ()
    (if (and *target* (= (-> *target* current-level name) 'beach))
        (spool-push *art-control* "beachcam-cannon" 0 self -1.0)
        )
    (if (and *target* (= (-> *target* current-level name) 'misty))
        (spool-push *art-control* "mistycam-cannon" 0 self -1.0)
        )
    (let ((t9-3 (-> (the-as (state nav-enemy) (find-parent-method babak-with-cannon 23)) trans)))
      (if t9-3
          (t9-3)
          )
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate nav-enemy-fuel-cell (babak-with-cannon)
  :virtual #t
  :event (the-as
    (function process int symbol event-message-block object :behavior babak-with-cannon)
    process-drawable-fuel-cell-handler
    )
  :code (behavior ()
    (ja-channel-set! 0)
    (clear-collide-with-as (-> self collide-info))
    (ja-post)
    (when (not (task-complete? *game-info* (-> self entity extra perm task)))
      (when (not (and (-> self entity) (logtest? (-> self entity extra perm status) (entity-perm-status complete))))
        (process-entity-status! self (entity-perm-status complete) #t)
        (if (and *target* (= (-> *target* current-level name) 'beach))
            (beachcam-spawn)
            )
        (if (and *target* (= (-> *target* current-level name) 'misty))
            (mistycam-spawn)
            )
        )
      (if (not (-> self child))
          (birth-pickup-at-point
            (-> self collide-info trans)
            (pickup-type fuel-cell)
            (the float (-> self entity extra perm task))
            #f
            self
            (the-as fact-info #f)
            )
          )
      (while (-> self child)
        (suspend)
        )
      )
    (cleanup-for-death self)
    (none)
    )
  )

;; definition for method 39 of type babak-with-cannon
(defmethod common-post babak-with-cannon ((obj babak-with-cannon))
  (cond
    ((= (level-status *level* 'beach) 'active)
     (spool-push *art-control* "beachcam-cannon" 0 obj -99.0)
     )
    ((= (level-status *level* 'misty) 'active)
     (spool-push *art-control* "mistycam-cannon" 0 obj -99.0)
     )
    )
  ((method-of-type nav-enemy common-post) obj)
  (none)
  )

;; definition for method 11 of type babak-with-cannon
;; INFO: Return type mismatch object vs none.
(defmethod init-from-entity! babak-with-cannon ((obj babak-with-cannon) (arg0 entity-actor))
  (initialize-collision obj)
  (process-drawable-from-entity! obj arg0)
  (TODO-RENAME-48 obj)
  (set! (-> obj distance) (res-lump-float arg0 'distance :default 163840.0))
  (set! (-> obj cannon-ent) (entity-actor-lookup (-> obj entity) 'alt-actor 0))
  (logclear! (-> obj mask) (process-mask actor-pause))
  (if (or (not (and (-> obj entity) (logtest? (-> obj entity extra perm status) (entity-perm-status complete))))
          (zero? (logand (-> obj enemy-info options) (fact-options has-power-cell)))
          )
      (go (method-of-object obj nav-enemy-idle))
      )
  (go (method-of-object obj nav-enemy-fuel-cell))
  (none)
  )
