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

(import "goal_src/jak1/import/logo-cam-ag.gc")
(import "goal_src/jak1/import/logo-black-ag.gc")
(import "goal_src/jak1/import/logo-ag.gc")
(import "goal_src/jak1/import/logo-volumes-ag.gc")
(import "goal_src/jak1/import/ndi-cam-ag.gc")
(import "goal_src/jak1/import/ndi-ag.gc")
(import "goal_src/jak1/import/ndi-volumes-ag.gc")

;; definition of type logo
(deftype logo (process-drawable)
  ((camera      handle      :offset-assert 176)
   (camera-anim handle      :offset-assert 184)
   (volumes     handle      :offset-assert 192)
   (black       handle      :offset-assert 200)
   (target      handle      :offset-assert 208)
   (sidekick    handle      :offset-assert 216)
   (main-joint  joint-mod   :offset-assert 224)
   (anim        spool-anim  :offset-assert 228)
   (next-anim   spool-anim  :offset-assert 232)
   (done?       symbol      :offset-assert 236)
   )
  :heap-base #x80
  :method-count-assert 24
  :size-assert         #xf0
  :flag-assert         #x18008000f0
  (:methods
    (idle () _type_ :state 20)
    (startup () _type_ :state 21)
    (hidden () _type_ :state 22)
    (ndi () _type_ :state 23)
    )
  )

;; definition for method 3 of type logo
(defmethod inspect logo ((obj logo))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tcamera: ~D~%" (-> obj camera))
  (format #t "~T~Tcamera-anim: ~D~%" (-> obj camera-anim))
  (format #t "~T~Tvolumes: ~D~%" (-> obj volumes))
  (format #t "~T~Tblack: ~D~%" (-> obj black))
  (format #t "~T~Ttarget: ~D~%" (-> obj target))
  (format #t "~T~Tsidekick: ~D~%" (-> obj sidekick))
  (format #t "~T~Tmain-joint: ~A~%" (-> obj main-joint))
  (format #t "~T~Tanim: ~A~%" (-> obj anim))
  (format #t "~T~Tnext-anim: ~A~%" (-> obj next-anim))
  (format #t "~T~Tdone?: ~A~%" (-> obj done?))
  obj
  )

;; definition for method 7 of type logo
;; INFO: Return type mismatch process-drawable vs logo.
(defmethod relocate logo ((obj logo) (arg0 int))
  (if (nonzero? (-> obj main-joint))
      (&+! (-> obj main-joint) arg0)
      )
  (the-as logo ((method-of-type process-drawable relocate) obj arg0))
  )

;; definition of type logo-slave
(deftype logo-slave (process-drawable)
  ((parent-process (pointer logo)  :offset         12)
   (main-joint     joint-mod       :offset-assert 176)
   )
  :heap-base #x50
  :method-count-assert 21
  :size-assert         #xb4
  :flag-assert         #x15005000b4
  (:methods
    (idle () _type_ :state 20)
    )
  )

;; definition for method 3 of type logo-slave
(defmethod inspect logo-slave ((obj logo-slave))
  (let ((t9-0 (method-of-type process-drawable inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tmain-joint: ~A~%" (-> obj main-joint))
  obj
  )

;; definition for method 7 of type logo-slave
;; INFO: Return type mismatch process-drawable vs logo-slave.
(defmethod relocate logo-slave ((obj logo-slave) (arg0 int))
  (if (nonzero? (-> obj main-joint))
      (&+! (-> obj main-joint) arg0)
      )
  (the-as logo-slave ((method-of-type process-drawable relocate) obj arg0))
  )

;; failed to figure out what this is:
(defskelgroup *logo-sg* logo logo-english-lod0-jg logo-idle-ja
              ((logo-english-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 10)
              )

;; failed to figure out what this is:
(defskelgroup *logo-japan-sg* logo logo-japan-lod0-jg logo-idle-ja
              ((logo-japan-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5)
              )

;; failed to figure out what this is:
(defskelgroup *logo-volumes-sg* logo-volumes logo-volumes-english-lod0-jg logo-volumes-idle-ja
              ((logo-volumes-english-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defskelgroup *logo-volumes-japan-sg* logo-volumes logo-volumes-japan-lod0-jg logo-volumes-idle-ja
              ((logo-volumes-japan-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defskelgroup *logo-black-sg* logo-black logo-black-lod0-jg logo-black-idle-ja
              ((logo-black-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defskelgroup *logo-cam-sg* logo-cam logo-cam-lod0-jg logo-cam-idle-ja
              ((logo-cam-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defskelgroup *ndi-sg* ndi ndi-lod0-jg ndi-idle-ja
              ((ndi-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 10 0 30)
              )

;; failed to figure out what this is:
(defskelgroup *ndi-volumes-sg* ndi-volumes ndi-volumes-lod0-jg ndi-volumes-idle-ja
              ((ndi-volumes-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defskelgroup *ndi-cam-sg* ndi-cam ndi-cam-lod0-jg ndi-cam-idle-ja
              ((ndi-cam-lod0-mg (meters 999999)))
              :bounds (static-spherem 0 0 0 5.5)
              )

;; failed to figure out what this is:
(defstate idle (logo-slave)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (local-vars (v0-0 uint))
    (let ((v1-0 arg2))
      (the-as object (cond
                       ((= v1-0 'blend-shape)
                        (cond
                          ((-> arg3 param 0)
                           (set! v0-0 (the-as uint (logior (-> self skel status) (janim-status blerc eye))))
                           (set! (-> self skel status) (the-as janim-status v0-0))
                           )
                          (else
                            (set! v0-0 (the-as uint (logclear (-> self skel status) (janim-status blerc eye))))
                            (set! (-> self skel status) (the-as janim-status v0-0))
                            )
                          )
                        v0-0
                        )
                       ((= v1-0 'origin-joint-index)
                        (set! v0-0 (-> arg3 param 0))
                        (set! (-> self draw origin-joint-index) v0-0)
                        v0-0
                        )
                       )
              )
      )
    )
  :code (behavior ()
    (ja-post)
    (loop
      (clone-anim-once
        (ppointer->handle (-> self parent-process))
        (the-as int (-> self draw origin-joint-index))
        #t
        ""
        )
      (if (nonzero? (-> self main-joint))
          (set-trs!
            (-> self main-joint)
            (-> self parent-process 0 main-joint trans)
            (the-as quaternion #f)
            (-> self parent-process 0 main-joint scale)
            )
          )
      (suspend)
      )
    (none)
    )
  )

;; definition for function logo-slave-init-by-other
;; INFO: Return type mismatch object vs none.
(defbehavior logo-slave-init-by-other logo-slave ((arg0 entity-actor) (arg1 skeleton-group))
  (set! (-> self entity) arg0)
  (set! (-> self root) (new 'process 'trsqv))
  (initialize-skeleton self arg1 '())
  (logior! (-> self skel status) (janim-status inited))
  (set! (-> self draw origin-joint-index) (the-as uint 3))
  (set! (-> self draw shadow-mask) (the-as uint 255))
  (set! (-> self draw global-effect) (draw-effect title))
  (when (or (string= (-> self draw art-group name) "logo-volumes")
            (string= (-> self draw art-group name) "logo-volumes-japan")
            (string= (-> self draw art-group name) "logo-black")
            )
    (set! (-> self main-joint) (new 'process 'joint-mod (joint-mod-handler-mode joint-set*) self 3))
    (set! (-> self main-joint max-dist) (the-as meters #t))
    )
  (set! (-> self event-hook) (-> (method-of-object self idle) event))
  (go-virtual idle)
  (none)
  )

;; failed to figure out what this is:
(defstate startup (logo)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (if (= v1-0 'update)
                         (remove-setting! 'process-mask)
                         )
              )
      )
    )
  :exit (behavior ()
    (let ((a0-0 (-> self anim)))
      (when (nonzero? a0-0)
        (ja-abort-spooled-anim a0-0 (the-as art-joint-anim #f) -1)
        (ja-channel-set! 0)
        (ja-post)
        (let ((a0-3 (handle->process (-> self camera))))
          (if a0-3
              (deactivate a0-3)
              )
          )
        )
      )
    (none)
    )
  :trans (behavior ()
    (if (not (none-reserved? *art-control*))
        (go-virtual hidden)
        )
    (if (nonzero? (-> self next-anim))
        (spool-push *art-control* (-> self next-anim name) 0 self -1.0)
        )
    (none)
    )
  :code (behavior ()
    (while (!= (file-status *art-control* (-> self next-anim name) 0) 'active)
      (set-blackout-frames (seconds 0.05))
      (suspend)
      )
    (set! (-> *setting-control* current bg-a) 1.0)
    (set-setting! 'bg-a 'abs 0.0 0)
    (set! (-> self camera-anim)
          (ppointer->handle (process-spawn logo-slave (-> self entity) *logo-cam-sg* :to self))
          )
    (set! (-> self camera)
          (ppointer->handle
            (process-spawn
              othercam
              (handle->process (-> self camera-anim))
              4
              #t
              'logo
              :to (handle->process (-> self camera-anim))
              )
            )
          )
    (send-event (handle->process (-> self camera)) 'mask 0)
    (case (scf-get-territory)
      ((2)
       (set! (-> self volumes)
             (ppointer->handle (process-spawn logo-slave (-> self entity) *logo-volumes-japan-sg* :to self))
             )
       )
      (else
        (set! (-> self volumes)
              (ppointer->handle (process-spawn logo-slave (-> self entity) *logo-volumes-sg* :to self))
              )
        )
      )
    (send-event (handle->process (-> self volumes)) 'origin-joint-index 3)
    (set! (-> self black) (ppointer->handle (process-spawn logo-slave (-> self entity) *logo-black-sg* :to self)))
    (send-event (handle->process (-> self black)) 'origin-joint-index 3)
    (set! (-> self anim) (-> self next-anim))
    (set! (-> self next-anim)
          (new 'static 'spool-anim
            :name "logo-intro-2"
            :index 7
            :parts 15
            :command-list '((260 want-force-inside village1 #t)
              (261 kill "sage-23")
              (261 kill "assistant-11")
              (261 kill "explorer-4")
              ((new 'static 'bfloat :data 261.5) kill "farmer-3")
              ((new 'static 'bfloat :data 261.5) kill "oracle-1")
              ((new 'static 'bfloat :data 261.5) kill "warp-gate-switch-3")
              (262 kill "sharkey-12")
              (262 kill "fishermans-boat-2")
              (262 kill "evilplant-2")
              ((new 'static 'bfloat :data 262.5) kill "revcycle-8")
              ((new 'static 'bfloat :data 262.5) kill "revcycleprop-2")
              ((new 'static 'bfloat :data 262.5) kill "water-vol-8")
              (263 kill "water-vol-9")
              (263 kill "water-vol-10")
              (263 kill "reflector-middle-2")
              (400 want-force-inside village1 #f)
              (400 display-level village1 display)
              (400 save)
              (500 set! *spawn-actors* #t)
              )
            )
          )
    (ja-play-spooled-anim
      (-> self anim)
      (the-as art-joint-anim #f)
      (the-as art-joint-anim #f)
      (the-as (function process-drawable symbol) false-func)
      )
    (set! *spawn-actors* #f)
    (add-setting! 'process-mask 'set 0.0 (process-mask progress))
    (apply-settings *setting-control*)
    (deactivate (handle->process (-> self black)))
    (deactivate (handle->process (-> self volumes)))
    (send-event (ppointer->process (-> self parent)) 'wait)
    (send-event self 'update)
    (ja-channel-set! 1)
    (ja-no-eval :group! (-> self draw art-group data 6) :num! (seek!) :frame-num 0.0)
    (until (ja-done? 0)
      (logior! (-> self skel status) (janim-status spool))
      (suspend)
      (ja :num! (seek!))
      )
    (set! (-> self anim) (-> self next-anim))
    (set! (-> self next-anim)
          (new 'static 'spool-anim
            :name "logo-loop"
            :index 9
            :parts 17
            :command-list '((61 kill "sage-23")
              (61 kill "assistant-11")
              (61 kill "explorer-4")
              ((new 'static 'bfloat :data 61.5) kill "farmer-3")
              ((new 'static 'bfloat :data 61.5) kill "oracle-1")
              ((new 'static 'bfloat :data 61.5) kill "warp-gate-switch-3")
              (62 kill "sharkey-12")
              (62 kill "fishermans-boat-2")
              (62 kill "evilplant-2")
              ((new 'static 'bfloat :data 62.5) kill "revcycle-8")
              ((new 'static 'bfloat :data 62.5) kill "revcycleprop-2")
              ((new 'static 'bfloat :data 62.5) kill "water-vol-8")
              (63 kill "water-vol-9")
              (63 kill "water-vol-10")
              (63 kill "reflector-middle-2")
              )
            )
          )
    (ja-play-spooled-anim
      (-> self anim)
      (the-as art-joint-anim #f)
      (the-as art-joint-anim #f)
      (the-as (function process-drawable symbol) false-func)
      )
    (remove-exit)
    (go-virtual idle)
    (none)
    )
  :post (behavior ()
    (if *progress-process*
        (logior! (-> self draw status) (draw-status skip-bones))
        (logclear! (-> self draw status) (draw-status skip-bones))
        )
    (let ((a3-0 (new-stack-vector0))
          (a1-0 (new-stack-vector0))
          )
      (let ((f0-0 (if (= (-> *setting-control* current aspect-ratio) 'aspect16x9)
                      0.87
                      1.0
                      )
                  )
            )
        (set-vector! a3-0 f0-0 f0-0 1.0 1.0)
        )
      (set-vector!
        a1-0
        (if (= (-> *setting-control* current aspect-ratio) 'aspect16x9)
            2048.0
            0.0
            )
        (if (= (-> *setting-control* current aspect-ratio) 'aspect16x9)
            -1228.8
            0.0
            )
        0.0
        1.0
        )
      (set-trs! (-> self main-joint) a1-0 (the-as quaternion #f) a3-0)
      )
    (ja-post)
    (none)
    )
  )

;; failed to figure out what this is:
(defstate idle (logo)
  :virtual #t
  :exit (-> (method-of-type logo startup) exit)
  :trans (-> (method-of-type logo startup) trans)
  :code (behavior ()
    (loop
      (set! (-> self anim) (-> self next-anim))
      (if (not (handle->process (-> self camera)))
          (set! (-> self camera)
                (ppointer->handle
                  (process-spawn
                    othercam
                    (handle->process (-> self camera-anim))
                    4
                    #t
                    'logo
                    :to (handle->process (-> self camera-anim))
                    )
                  )
                )
          )
      (set! *spawn-actors* #f)
      (ja-channel-set! 1)
      (ja-no-eval :group! (-> self draw art-group data 8) :num! (seek!) :frame-num 0.0)
      (until (ja-done? 0)
        (logior! (-> self skel status) (janim-status spool))
        (suspend)
        (ja :num! (seek!))
        )
      (set! *spawn-actors* #t)
      (ja-play-spooled-anim
        (-> self anim)
        (the-as art-joint-anim #f)
        (the-as art-joint-anim #f)
        (the-as (function process-drawable symbol) false-func)
        )
      )
    (none)
    )
  :post (-> (method-of-type logo startup) post)
  )

;; failed to figure out what this is:
(defstate hidden (logo)
  :virtual #t
  :trans (behavior ()
    (if (nonzero? (-> self next-anim))
        (spool-push *art-control* (-> self next-anim name) 0 self -1.0)
        )
    (none)
    )
  :code (behavior ()
    (ja-channel-set! 0)
    (ja-post)
    (let ((gp-0 *master-mode*))
      (set! *master-mode* 'game)
      (clear-rec *art-control*)
      (set! *master-mode* gp-0)
      )
    (loop
      (when (and (none-reserved? *art-control*) (not (paused?)))
        (let ((gp-1 (-> *display* base-frame-counter)))
          (until (>= (- (-> *display* base-frame-counter) gp-1) (seconds 0.1))
            (set! *camera-look-through-other* 2)
            (suspend)
            )
          )
        (go-virtual idle)
        )
      (set! *camera-look-through-other* 2)
      (suspend)
      )
    (none)
    )
  )

;; failed to figure out what this is:
(defstate ndi (logo)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (if (= v1-0 'blackout)
                         (set-setting! 'bg-a 'abs 1.0 0)
                         )
              )
      )
    )
  :enter (behavior ()
    (set-setting! 'bg-a 'abs 0.0 0)
    (apply-settings *setting-control*)
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (if *time-of-day-proc*
        (set! (-> *time-of-day-proc* 0 hour) 12)
        )
    (set! (-> self done?) #f)
    (none)
    )
  :exit (behavior ()
    ((-> (method-of-type logo startup) exit))
    (set-blackout-frames (seconds 0.035))
    (none)
    )
  :trans (behavior ()
    ((-> (method-of-type logo startup) trans))
    (if (and *debug-segment*
             (cpad-pressed? 0 start circle x)
             (member (level-status *level* 'village1) '(loaded active))
             )
        (set-setting! 'bg-a 'abs 1.0 0)
        )
    (when (and (= (-> *setting-control* current bg-a) 1.0)
               (and (member (level-status *level* 'village1) '(active loaded))
                    (>= (- (-> *display* base-frame-counter) (-> self state-time)) (seconds 3))
                    (let ((gp-2 (level-get *level* 'village1)))
                      (when gp-2
                        (load-state-want-levels 'title 'village1)
                        (load-state-want-display-level 'village1 'display-self)
                        (load-state-want-vis 'vi1)
                        (set-force-inside! *load-state* 'village1 #t)
                        (!= (-> gp-2 all-visible?) 'loading)
                        )
                      )
                    )
               )
      (load-state-want-display-level 'village1 'special)
      (if (not (-> self done?))
          (ja-abort-spooled-anim (-> self anim) (the-as art-joint-anim #f) -1)
          )
      (set! (-> self done?) #t)
      (deactivate self)
      )
    (if *time-of-day-proc*
        (set! (-> *time-of-day-proc* 0 hour) 12)
        )
    (none)
    )
  :code (behavior ()
    (while (!= (file-status *art-control* (-> self next-anim name) 0) 'active)
      (set-blackout-frames (seconds 0.05))
      (suspend)
      )
    (set! (-> *setting-control* current bg-a) 1.0)
    (set! (-> self camera-anim)
          (ppointer->handle (process-spawn logo-slave (-> self entity) *ndi-cam-sg* :to self))
          )
    (set! (-> self camera)
          (ppointer->handle
            (process-spawn
              othercam
              (handle->process (-> self camera-anim))
              3
              #t
              'logo
              :to (handle->process (-> self camera-anim))
              )
            )
          )
    (send-event (handle->process (-> self camera)) 'mask 0)
    (set! (-> self volumes)
          (ppointer->handle (process-spawn logo-slave (-> self entity) *ndi-volumes-sg* :to self))
          )
    (send-event (handle->process (-> self volumes)) 'origin-joint-index 3)
    (set! (-> self target) (ppointer->handle (process-spawn logo-slave #f *jchar-sg* :to self)))
    (send-event (handle->process (-> self target)) 'blend-shape #t)
    (send-event (handle->process (-> self target)) 'origin-joint-index 33)
    (set! (-> self sidekick) (ppointer->handle (process-spawn logo-slave #f *sidekick-sg* :to self)))
    (send-event (handle->process (-> self sidekick)) 'blend-shape #t)
    (send-event (handle->process (-> self sidekick)) 'origin-joint-index 6)
    (set! (-> self anim) (-> self next-anim))
    (set! (-> self next-anim) (new 'static 'spool-anim :name "logo-intro" :index 5 :parts 3 :command-list '()))
    (ja-play-spooled-anim
      (-> self anim)
      (the-as art-joint-anim #f)
      (the-as art-joint-anim #f)
      (the-as (function process-drawable symbol) false-func)
      )
    (set! (-> self done?) #t)
    (set-setting! 'bg-a 'abs 1.0 0)
    (anim-loop)
    (none)
    )
  :post (behavior ()
    (ja-post)
    (none)
    )
  )

;; definition for function logo-init-by-other
;; INFO: Used lq/sq
;; INFO: Return type mismatch object vs none.
(defbehavior logo-init-by-other logo ((arg0 entity-actor) (arg1 vector) (arg2 symbol))
  (stack-size-set! (-> self main-thread) 512)
  (logclear! (-> self mask) (process-mask progress))
  (set! (-> self entity) arg0)
  (set! (-> self root) (new 'process 'trsqv))
  (set! (-> self root trans quad) (-> arg1 quad))
  (case arg2
    (('logo)
     (set! (-> *time-of-day-context* title-light-group dir1 levels x) 0.0)
     (set! (-> *time-of-day-context* title-light-group ambi levels x) 0.444)
     (case (scf-get-territory)
       ((2)
        (initialize-skeleton self *logo-japan-sg* '())
        )
       (else
         (initialize-skeleton self *logo-sg* '())
         )
       )
     (set! (-> self main-joint) (new 'process 'joint-mod (joint-mod-handler-mode joint-set*) self 3))
     (set! (-> self main-joint max-dist) (the-as meters #t))
     (set! (-> self next-anim) (new 'static 'spool-anim
                                 :name "logo-intro"
                                 :index 5
                                 :parts 3
                                 :command-list '((0 want-levels title village1)
                                   (5 display-level village1 special)
                                   (5 want-vis vi1)
                                   (5 want-force-inside village1 #t)
                                   (115 display-level village1 display-self)
                                   (115 save)
                                   (115 kill "sage-23")
                                   (115 kill "assistant-11")
                                   (115 kill "explorer-4")
                                   (115 kill "farmer-3")
                                   (115 kill "oracle-1")
                                   (115 kill "warp-gate-switch-3")
                                   (115 kill "sharkey-12")
                                   (115 kill "fishermans-boat-2")
                                   (115 kill "evilplant-2")
                                   (115 kill "revcycle-8")
                                   (115 kill "revcycleprop-2")
                                   (115 kill "water-vol-8")
                                   (115 kill "water-vol-9")
                                   (115 kill "water-vol-10")
                                   (115 kill "reflector-middle-2")
                                   )
                                 )
           )
     (set! (-> self draw origin-joint-index) (the-as uint 3))
     )
    (('ndi)
     (set! (-> *time-of-day-context* title-light-group dir1 levels x) 1.0)
     (set! (-> *time-of-day-context* title-light-group ambi levels x) 1.0)
     (initialize-skeleton self *ndi-sg* '())
     (set! (-> self draw origin-joint-index) (the-as uint 3))
     (set! (-> self next-anim)
           (new 'static 'spool-anim :name "ndi-intro" :index 3 :parts 4 :command-list '((0 want-levels title village1)
                                                                                        (5 want-vis vi1)
                                                                                        (5 want-force-inside village1 #t)
                                                                                        (6 save)
                                                                                        (453 send-event self blackout)
                                                                                        )
                                    )
           )
     )
    )
  (logior! (-> self skel status) (janim-status inited))
  (set! (-> self draw global-effect) (draw-effect title))
  (set! (-> self draw shadow-mask) (the-as uint 255))
  (set! (-> self camera) (the-as handle #f))
  (set! (-> self volumes) (the-as handle #f))
  (set! (-> self black) (the-as handle #f))
  (cond
    ((= arg2 'logo)
     (go-virtual startup)
     )
    ((= arg2 'ndi)
     (go-virtual ndi)
     )
    )
  (none)
  )

;; failed to figure out what this is:
(defstate target-title (target)
  :event target-generic-event-handler
  :enter (behavior ()
    (set-setting! 'music-volume 'abs 0.0 0)
    (set-setting! 'sfx-volume 'abs 0.0 0)
    (set-setting! 'ambient-volume 'abs 0.0 0)
    (set-setting! 'allow-pause #f 0.0 0)
    (set-setting! 'allow-progress #f 0.0 0)
    (set-setting! 'border-mode #f 0.0 0)
    (apply-settings *setting-control*)
    (set! (-> self state-time) (-> *display* base-frame-counter))
    (set! (-> self manipy) (the-as (pointer manipy) #f))
    (ja-channel-set! 0)
    (ja-post)
    (send-event *camera* 'change-state cam-fixed 0)
    (none)
    )
  :exit (behavior ()
    (when (not (or (= (-> self next-state name) 'target-title-play) (= (-> self next-state name) 'target-title-wait)))
      (if (-> self manipy)
          (deactivate (-> self manipy 0))
          )
      )
    (none)
    )
  :trans (behavior ()
    (hide-hud-quick)
    (spool-push *art-control* "ndi-intro" 0 self -1.0)
    (sound-group-pause (sound-group music))
    (none)
    )
  :code (behavior ()
    (let ((gp-0 (the-as handle #f)))
      (case (scf-get-territory)
        ((2)
         (set! gp-0 (ppointer->handle (static-screen-spawn
                                        5
                                        (new 'static 'texture-id :page #x649)
                                        (new 'static 'texture-id :index #x1 :page #x649)
                                        (new 'static 'texture-id :index #x2 :page #x649)
                                        (seconds 3)
                                        #f
                                        self
                                        )
                                      )
               )
         )
        (else
          (while (!= (file-status *art-control* "ndi-intro" 0) 'active)
            (set-blackout-frames (seconds 0.05))
            (suspend)
            )
          (let ((s5-1 (-> *display* base-frame-counter)))
            (until (>= (- (-> *display* base-frame-counter) s5-1) (seconds 0.25))
              (set-blackout-frames (seconds 0.05))
              (suspend)
              )
            )
          )
        )
      (label cfg-8)
      (let ((s5-2 (new 'stack-no-clear 'mc-slot-info)))
        (mc-get-slot-info 0 s5-2)
        (when (or (zero? (-> s5-2 known)) (handle->process (the-as int gp-0)))
          (if (not (handle->process (the-as int gp-0)))
              (set-blackout-frames (seconds 0.05))
              )
          (suspend)
          (goto cfg-8)
          )
        (set! (-> *setting-control* default bg-a) 0.0)
        (when (or (zero? (-> s5-2 handle))
                  (and (nonzero? (-> s5-2 formatted)) (zero? (-> s5-2 inited)) (< (-> s5-2 mem-actual) (-> s5-2 mem-required)))
                  )
          (mem-copy! (the-as pointer *auto-save-info*) (the-as pointer s5-2) 300)
          (set-blackout-frames 0)
          (let ((gp-1 7))
            (if (zero? (-> s5-2 handle))
                (set! gp-1 8)
                )
            (set-setting! 'allow-progress #t 0.0 0)
            (set-blackout-frames 0)
            (set! (-> *setting-control* default bg-a-force) 0.0)
            (apply-settings *setting-control*)
            (set! (-> *setting-control* current bg-a) 0.0)
            (set! (-> *setting-control* current bg-a-force) 0.0)
            (activate-progress *dproc* (the-as progress-screen gp-1))
            )
          (set-setting! 'allow-progress #f 0.0 0)
          (apply-settings *setting-control*)
          (while *progress-process*
            (suspend)
            )
          (let ((gp-2 (-> *display* base-frame-counter)))
            (until (>= (- (-> *display* base-frame-counter) gp-2) (seconds 0.01))
              (suspend)
              )
            )
          (goto cfg-41)
          )
        )
      )
    (label cfg-41)
    (let ((gp-3 (entity-by-name "logo-1")))
      (let ((v1-53 (process-spawn logo gp-3 (-> gp-3 extra trans) 'ndi :to self)))
        (set! (-> self manipy) (the-as (pointer manipy) v1-53))
        (let ((s5-4 (ppointer->handle v1-53)))
          (while (handle->process (the-as handle s5-4))
            (suspend)
            )
          )
        )
      (set! (-> self manipy)
            (the-as (pointer manipy) (process-spawn logo gp-3 (-> gp-3 extra trans) 'logo :to self))
            )
      )
    (go target-title-play)
    (none)
    )
  :post target-no-move-post
  )

;; failed to figure out what this is:
(defstate target-title-play (target)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (cond
                       ((= v1-0 'wait)
                        (go target-title-wait)
                        )
                       ((= v1-0 'reset)
                        (deactivate (-> self manipy 0))
                        )
                       (else
                         (target-generic-event-handler arg0 arg1 arg2 arg3)
                         )
                       )
              )
      )
    )
  :enter (behavior ()
    (set-setting! 'allow-pause #f 0.0 0)
    (set-setting! 'allow-progress #f 0.0 0)
    (none)
    )
  :exit (-> target-title exit)
  :trans (the-as (function none :behavior target) hide-hud-quick)
  :code (the-as (function none :behavior target) anim-loop)
  :post target-no-move-post
  )

;; failed to figure out what this is:
(defstate target-title-wait (target)
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (let ((v1-0 arg2))
      (the-as object (cond
                       ((= v1-0 'play)
                        (go target-title-play)
                        )
                       ((= v1-0 'reset)
                        (deactivate (-> self manipy 0))
                        )
                       (else
                         (target-generic-event-handler arg0 arg1 arg2 arg3)
                         )
                       )
              )
      )
    )
  :enter (behavior ()
    (remove-setting! 'ambient-volume)
    (remove-setting! 'sfx-volume)
    (remove-setting! 'music-volume)
    (remove-setting! 'allow-progress)
    (sound-group-continue (sound-group music))
    (when *time-of-day-proc*
      (set! (-> *time-of-day-proc* 0 time-ratio) 18000.0)
      (set! *time-of-day-fast* #t)
      )
    (none)
    )
  :exit (behavior ()
    (when *time-of-day-proc*
      (set! (-> *time-of-day-proc* 0 time-ratio) 300.0)
      (set! *time-of-day-fast* #f)
      )
    (remove-setting! 'allow-pause)
    ((-> target-title exit))
    (none)
    )
  :trans (behavior ()
    (hide-hud-quick)
    (if (cpad-pressed? 0 start)
        (activate-progress *dproc* (progress-screen title))
        )
    (when (and (< (mod (-> *display* real-frame-counter) 300) 270) (not *progress-process*))
      (let ((gp-0
              (new 'stack 'font-context *font-default-matrix* 80 170 0.0 (font-color default) (font-flags shadow kerning))
              )
            )
        (let ((v1-12 gp-0))
          (set! (-> v1-12 width) (the float 352))
          )
        (let ((v1-13 gp-0))
          (set! (-> v1-13 height) (the float 40))
          )
        (set! (-> gp-0 flags) (font-flags shadow kerning middle left large))
        (print-game-text (lookup-text! *common-text* (game-text-id press-start) #f) gp-0 #f 128 22)
        )
      )
    (none)
    )
  :code (the-as (function none :behavior target) anim-loop)
  :post target-no-move-post
  )
