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

;; definition for method 25 of type pov-camera
(defmethod check-for-abort pov-camera ((obj pov-camera))
  (when (or (and (>= (- (-> *display* base-frame-counter) (-> obj debounce-start-time)) (seconds 0.2))
                 (cpad-pressed? 0 triangle)
                 )
            (logtest? (-> obj flags) (pov-camera-flag allow-abort))
            )
    (logclear! (-> *cpad-list* cpads 0 button0-abs 0) (pad-buttons triangle))
    (logclear! (-> *cpad-list* cpads 0 button0-rel 0) (pad-buttons triangle))
    (when (logtest? (-> obj flags) (pov-camera-flag notify-of-abort))
      (send-event (handle->process (-> obj notify-handle)) 'notify 'abort-request)
      #t
      )
    )
  )

;; definition for method 26 of type pov-camera
(defmethod target-grabbed? pov-camera ((obj pov-camera))
  (or (not *target*) (process-grab? *target*))
  )

;; definition for method 28 of type pov-camera
(defmethod target-released? pov-camera ()
  (or (not *target*) (process-release? *target*))
  )

;; failed to figure out what this is:
(defstate pov-camera-startup (pov-camera)
  :virtual #t
  :code (behavior ()
    (go-virtual pov-camera-start-playing)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate pov-camera-start-playing (pov-camera)
  :virtual #t
  :code (behavior ()
    (logclear! (-> self mask) (process-mask actor-pause))
    (while (not (target-grabbed? self))
      (suspend)
      )
    (let ((gp-0 0))
      (let ((v1-7 (lookup-art (-> self draw jgeo) "camera" (the-as type #f))))
        (if v1-7
            (set! gp-0 (+ (-> v1-7 number) 1))
            )
        )
      (let ((v1-10 (process-spawn othercam self gp-0 #t #t :to self)))
        (send-event (ppointer->process v1-10) 'mask (-> self mask-to-clear))
        )
      )
    (go-virtual pov-camera-playing)
    (none)
    )
  )

;; definition for function pov-camera-play-and-reposition
;; INFO: Return type mismatch int vs none.
(defbehavior pov-camera-play-and-reposition pov-camera ((arg0 art-joint-anim) (arg1 vector) (arg2 float))
  (let ((s4-0 #f))
    (ja-no-eval :group! arg0 :num! (seek! max arg2) :frame-num 0.0)
    (until (ja-done? 0)
      (let ((v1-4 (and (not s4-0) (< (the float (+ (-> (ja-group) data 0 length) -4)) (ja-frame-num 0)))))
        (when v1-4
          (set! s4-0 #t)
          (send-event *camera* 'teleport-to-vector-start-string arg1)
          )
        )
      (suspend)
      (ja :num! (seek! max arg2))
      )
    )
  0
  (none)
  )

;; failed to figure out what this is:
(defstate pov-camera-playing (pov-camera)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('abort)
       (when (logtest? (-> self flags) (pov-camera-flag notify-of-abort))
         (logior! (-> self flags) (pov-camera-flag allow-abort))
         (if (= (-> self anim-name type) string)
             (go-virtual pov-camera-abort)
             )
         )
       )
      )
    )
  :enter (behavior ()
    (set! (-> self debounce-start-time) (-> *display* base-frame-counter))
    (if (= (-> self anim-name type) string)
        (backup-load-state-and-set-cmds *load-state* (-> self command-list))
        )
    (none)
    )
  :exit (behavior ()
    (if (= (-> self anim-name type) string)
        (restore-load-state-and-cleanup *load-state*)
        )
    (remove-setting! 'music-volume)
    (remove-setting! 'sfx-volume)
    (none)
    )
  :code (behavior ()
    (add-setting! 'music-volume 'rel (-> self music-volume-movie) 0)
    (add-setting! 'sfx-volume 'rel (-> self sfx-volume-movie) 0)
    (cond
      ((= (-> self anim-name type) string)
       (ja-no-eval :group! (ja-group) :num! (seek!) :frame-num 0.0)
       (until (ja-done? 0)
         (check-for-abort self)
         (suspend)
         (ja :num! (seek!))
         )
       )
      ((= (-> self anim-name type) spool-anim)
       (ja-play-spooled-anim
         (the-as spool-anim (-> self anim-name))
         (the-as art-joint-anim #f)
         (the-as art-joint-anim #f)
         (method-of-object self check-for-abort)
         )
       )
      )
    (go-virtual pov-camera-done-playing)
    (none)
    )
  :post (behavior ()
    (if (= (-> self anim-name type) string)
        (execute-commands-up-to *load-state* (ja-aframe-num 0))
        )
    (ja-post)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate pov-camera-abort (pov-camera)
  :virtual #t
  :enter (behavior ()
    (logior! (-> self flags) (pov-camera-flag allow-abort))
    (none)
    )
  :code (behavior ()
    (set-blackout-frames (seconds 0.035))
    (suspend)
    (suspend)
    (go-virtual pov-camera-done-playing)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate pov-camera-done-playing (pov-camera)
  :virtual #t
  :code (behavior ()
    (while (begin self (not ((method-of-object self target-released?))))
      (suspend)
      )
    (send-event (handle->process (-> self notify-handle)) 'notify 'die)
    (suspend)
    (suspend)
    (cleanup-for-death self)
    (deactivate self)
    (none)
    )
  )

;; definition for method 27 of type pov-camera
;; INFO: Return type mismatch int vs none.
(defmethod pre-startup-callback pov-camera ((obj pov-camera))
  0
  (none)
  )

;; definition for method 29 of type pov-camera
;; INFO: Return type mismatch symbol vs none.
(defmethod set-stack-size! pov-camera ((obj pov-camera))
  (none)
  )

;; definition for function pov-camera-init-by-other
;; INFO: Return type mismatch object vs none.
;; Used lq/sq
(defbehavior pov-camera-init-by-other pov-camera ((arg0 vector) (arg1 skeleton-group) (arg2 string) (arg3 pov-camera-flag) (arg4 process-drawable) (arg5 pair))
  (set-stack-size! self)
  (set! (-> *game-info* pov-camera-handle) (process->handle self))
  (set! (-> self flags) arg3)
  (set! (-> self command-list) arg5)
  (set! (-> self music-volume-movie) 100.0)
  (set! (-> self sfx-volume-movie) 100.0)
  (if arg4
      (set! (-> self notify-handle) (process->handle arg4))
      (set! (-> self notify-handle) (the-as handle #f))
      )
  (set! (-> self debounce-start-time) (-> *display* base-frame-counter))
  (logclear! (-> self mask) (process-mask actor-pause movie enemy platform projectile))
  (set! (-> self root) (new 'process 'trsqv))
  (set! (-> self root trans quad) (-> arg0 quad))
  (when (logtest? (-> self flags) (pov-camera-flag inherit-orientation))
    (let ((v1-20 (if (and (nonzero? arg4) (type-type? (-> arg4 type) process-drawable))
                     arg4
                     )
                 )
          )
      (quaternion-copy! (-> self root quat) (-> v1-20 root quat))
      )
    )
  (initialize-skeleton self arg1 '())
  (logior! (-> self draw status) (draw-status skip-bones))
  (logior! (-> self skel status) (janim-status inited))
  (set! (-> self anim-name) arg2)
  (cond
    ((= (-> arg2 type) string)
     (logior! (-> self skel status) (janim-status spool))
     (let ((s5-1 (lookup-art (-> self draw art-group) arg2 art-joint-anim)))
       (if (not s5-1)
           (go process-drawable-art-error arg2)
           )
       (ja-channel-set! 1)
       (set! (-> self skel root-channel 0 frame-group) (the-as art-joint-anim s5-1))
       )
     )
    ((= (-> arg2 type) spool-anim)
     )
    )
  (set! (-> self mask-to-clear) (process-mask movie enemy platform projectile))
  (set! (-> self event-hook) (lambda :behavior pov-camera
                               ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
                               (let ((v1-0 arg2))
                                 (the-as object (cond
                                                  ((= v1-0 'mask)
                                                   (let ((v0-0 (the-as number (-> arg3 param 0))))
                                                     (set! (-> self mask-to-clear) (the-as process-mask v0-0))
                                                     v0-0
                                                     )
                                                   )
                                                  ((= v1-0 'music-movie-volume)
                                                   (let ((f0-0 (the-as float (-> arg3 param 0))))
                                                     (set! (-> self music-volume-movie) f0-0)
                                                     f0-0
                                                     )
                                                   )
                                                  ((= v1-0 'sfx-movie-volume)
                                                   (let ((f0-1 (the-as float (-> arg3 param 0))))
                                                     (set! (-> self sfx-volume-movie) f0-1)
                                                     f0-1
                                                     )
                                                   )
                                                  )
                                         )
                                 )
                               )
        )
  (pre-startup-callback self)
  (go-virtual pov-camera-startup)
  (none)
  )
