;;-*-Lisp-*-
(in-package goal)
(bundles "VI1.DGO")
(require "engine/common-obs/process-taskable.gc")
(require "engine/ui/credits.gc")
(define-extern *sidekick-human-sg* skeleton-group)

;; DECOMP BEGINS

(defpartgroup group-sequenceAV-splash
  :id 686
  :duration (seconds 3)
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 64)
  :parts ((sp-item 2854 :period (seconds 3) :length (seconds 2))))

(defpart 2854
  :init-specs
  ((:texture (lakedrop effects))
   (:num 128.0)
   (:x (meters -1) (meters 2))
   (:z (meters -1) (meters 2))
   (:scale-x (meters 0.025) (meters 0.125))
   (:rot-z (degrees 0) (degrees 360))
   (:scale-y :copy scale-x)
   (:r 128.0 32.0)
   (:g 96.0 64.0)
   (:b 64.0 32.0)
   (:a 32.0 32.0)
   (:vel-y (meters 0.026666667) (meters 0.053333335))
   (:rotvel-z (degrees -1.2) (degrees 2.4))
   (:accel-y (meters -0.00066666666) (meters -0.0016666667))
   (:timer (seconds 3))
   (:flags (bit2 bit3))
   (:conerot-x (degrees -30) (degrees 60))
   (:conerot-y (degrees 0) (degrees 5000))
   (:conerot-radius (meters 0.3) (meters -0.5))))

(defpartgroup group-sequenceAV-spit
  :id 687
  :duration (seconds 0.017)
  :linger-duration (seconds 3)
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 64)
  :parts ((sp-item 2855)))

(defpart 2855
  :init-specs
  ((:texture (lakedrop effects))
   (:num 5.0)
   (:scale-x (meters 0.025) (meters 0.025))
   (:rot-z (degrees 0) (degrees 360))
   (:scale-y :copy scale-x)
   (:r 128.0 32.0)
   (:g 128.0 32.0)
   (:b 128.0 32.0)
   (:a 32.0 32.0)
   (:vel-y (meters 0.016666668) (meters 0.0033333334))
   (:scalevel-x (meters 0.00033333333))
   (:rotvel-z (degrees -1.2) (degrees 2.4))
   (:scalevel-y :copy scalevel-x)
   (:fade-a -0.21333334)
   (:accel-y (meters -0.00033333333) (meters -0.00033333333))
   (:friction 0.95)
   (:timer (seconds 1))
   (:flags (bit2 bit3))
   (:conerot-x (degrees 106) (degrees 2))
   (:conerot-y (degrees 90) (degrees 2))
   (:conerot-radius (meters 0) (meters -0.15))))

;; og:preserve-this PAL patch here
(defpartgroup group-sequenceAV-2d-intro-mist
  :id 688
  :flags (screen-space)
  :bounds (static-bspherem 0 0 0 2)
  :parts
  ((sp-item 2858 :period (seconds 1) :length (seconds 0.017) :binding 2856)
   (sp-item 2856 :flags (start-dead launch-asap) :binding 2857)
   (sp-item 2856 :flags (start-dead launch-asap) :binding 2857)
   (sp-item 2857 :flags (start-dead))
   (sp-item 2857 :flags (start-dead))
   (sp-item 2857 :flags (start-dead))
   (sp-item 2859)
   (sp-item 2860 :flags (bit6))))

(defpart 2858
  :init-specs
  ((:texture (starflash effects))
   (:num 1.0)
   (:x (meters -2.5) (meters 5))
   (:y (meters -1.5) (meters 3))
   (:scale-x (meters 0.1))
   (:scale-y :copy scale-x)
   (:g 128.0)
   (:a 0.0)
   (:vel-y (meters 0.0033333334) (meters 0.0033333334))
   (:timer (seconds 2))
   (:flags (bit3))
   (:conerot-z (degrees 0) (degrees 360))))

(defpart 2856
  :init-specs
  ((:texture (hotdot effects))
   (:num 1.0)
   (:x (meters 0) (meters 16))
   (:y (meters 0) (meters 16))
   (:z (meters 0.5) (meters 0.2))
   (:scale-x (meters 0.5) (meters 0.5))
   (:rot-z (degrees 0) (degrees 360))
   (:scale-y :copy scale-x)
   (:r 0.0 96.0)
   (:g 0.0 32.0)
   (:b 64.0 32.0)
   (:a 128.0)
   (:omega 0.0 65536.0)
   (:vel-x (meters -0.053333335) 1 (meters 0.10666667))
   (:scalevel-x (meters -0.0012121212))
   (:rotvel-z (degrees -0.3) 1 (degrees 0.6))
   (:scalevel-y :copy scalevel-x)
   (:timer (seconds 2))
   (:flags (bit2 bit3 bit7))))

(defpart 2857
  :init-specs
  ((:texture (hotdot effects))
   (:num 1.0)
   (:scale-x (meters 0.5) (meters 0.3))
   (:scale-y :copy scale-x)
   (:r 32.0 96.0)
   (:g 0.0 32.0)
   (:b 64.0 32.0)
   (:a 64.0 64.0)
   (:scalevel-x (meters -0.0010952381))
   (:scalevel-y :copy scalevel-x)
   (:accel-y (meters 0) (meters -0.000033333334))
   (:timer (seconds 0.1) (seconds 0.997))
   (:flags (bit2 bit3))
   (:next-time (seconds 0.8))
   (:next-launcher 198)))

(defpart 2860
  :init-specs
  ((:texture (p-white effects))
   (:num 1.0)
   (:z (meters -3.90625))
   (:scale-x (meters 15))
   (:scale-y (meters 12))
   (:r 0.0)
   (:g 0.0)
   (:b 0.0)
   (:a 128.0)
   (:timer (seconds 0.017))
   (:flags (bit2 bit13 bit14))))

(defpart 2859
  :init-specs
  ((:texture (bigpuff effects))
   (:num 0.3)
   (:x (meters -4) (meters 8))
   (:y (meters -3) (meters 6))
   (:z (meters 0) (meters 3.4179688))
   (:scale-x (meters 6) (meters 8))
   (:rot-z (degrees 0) (degrees 360))
   (:scale-y (meters 6) (meters 8))
   (:r 16.0 80.0)
   (:g 0.0 16.0)
   (:b 16.0 150.0)
   (:a 0.0)
   (:scalevel-x (meters -0.0016666667) (meters 0.0033333334))
   (:rotvel-z (degrees -0.075) (degrees 0.15))
   (:scalevel-y :copy scalevel-x)
   (:fade-a 0.21333334)
   (:timer (seconds 5))
   (:flags (bit2 bit14))
   (:next-time (seconds 0.5) (seconds 0.497))
   (:next-launcher 2861)))

(defpart 2861
  :init-specs ((:fade-a 0.0) (:next-time (seconds 1) (seconds 0.997)) (:next-launcher 2862)))

(defpart 2862
  :init-specs ((:fade-a -0.21333334)))

(deftype sequenceA-village1 (process-taskable)
  ((boat handle)
   (side handle)))

(defmethod play-anim! ((this sequenceA-village1) (arg0 symbol))
  (when arg0
    (set! (-> *time-of-day-proc* 0 time-ratio) 0.0)
    (set! (-> *time-of-day-proc* 0 hour) 23)
    (send-event *camera* 'no-intro)
    (send-event *camera* 'clear-entity)
    (send-event *camera* 'force-blend 0)
    (send-event *camera* 'change-state cam-fixed 0)
    (send-event *target* 'sidekick #f)
    (set! (-> this boat)
          (ppointer->handle (manipy-spawn (-> this root trans) (-> this entity) *fishermans-boat-sg* #f :to this)))
    (send-event (handle->process (-> this boat)) 'anim-mode 'clone-anim)
    (send-event (handle->process (-> this boat)) 'origin-joint-index 3))
  ;; og:preserve-this PAL patch here
  (the-as basic
          (new 'static
               'spool-anim
               :name "sage-intro-sequence-a"
               :index 17
               :parts 22
               :command-list
               '((0 blackout 0)
                (0 want-levels village1 misty)
                (1120 send-event self pause)
                (1210 display-level misty display)
                (1210 want-vis mis)
                (1210 want-force-vis misty #t)
                (1230 send-event self sidekick-human)
                (1239 joint "cameraB")
                (1480 joint "camera")
                (1480 setting-reset ocean-off near)
                (1613 joint "cameraB")
                (1796 joint "camera")
                (1843 joint "cameraB")
                (2145 joint "camera")
                (2274 joint "cameraB")
                (2439 joint "camera")
                (2450 want-force-vis misty #f)
                (2490 joint "cameraB")
                (2570 alive "sequenceB-1")
                (2569 save)
                (2569 setting-unset ocean-off)))))

(defmethod get-art-elem ((this sequenceA-village1))
  (-> this draw art-group data 3))

(defstate play-anim (sequenceA-village1)
  :virtual #t
  :event
    (behavior ((proc process) (argc int) (message symbol) (block event-message-block))
      (case message
        (('sidekick-human)
         (format 0 "got sidekick-human~%")
         (when (= (level-status *level* 'misty) 'active)
           (let ((gp-1 (entity-by-name "sequenceA-1")))
             (when gp-1
               (format 0 "found entity for sidekick-human~%")
               (set! (-> self side) (ppointer->handle (manipy-spawn (-> self root trans) gp-1 *sidekick-human-sg* #f :to self)))
               (send-event (handle->process (-> self side)) 'anim-mode 'clone-anim)
               (send-event (handle->process (-> self side)) 'center-joint 3)
               (send-event (handle->process (-> self side)) 'blend-shape #t)))))
        ;; og:preserve-this PAL patch here
        (('pause)
         (let ((v0-2 (the-as object (logior (-> self mask) (process-mask pause)))))
           (set! (-> self mask) (the-as process-mask v0-2))
           v0-2))))
  :exit
    (behavior ()
      (send-event *target* 'sidekick #t)
      (let ((a0-2 (handle->process (-> self boat)))) (if a0-2 (deactivate a0-2)))
      (let ((a0-6 (handle->process (-> self side)))) (if a0-6 (deactivate a0-6)))
      ((-> (method-of-type process-taskable play-anim) exit))
      (#when PC_PORT
        ;; og:preserve-this extra stuff when skipping cutscene
        (when (and (= (get-response (-> self query)) 'no) (or (not (= *cheat-mode* 'debug)) (not (cpad-hold? 0 r1))))
          (close-specific-task! (game-task intro) (task-status need-resolution))
          (start 'play (get-continue-by-name *game-info* "game-start"))
          (set-blackout-frames (seconds 0.1))
          (set! (-> *time-of-day-proc* 0 time-ratio) 300.0)
          (set! (-> *time-of-day-proc* 0 hour) 7)
          (if (nonzero? (-> self part)) (kill-and-free-particles (-> self part)))
          (return #f)))
      (let ((gp-0 (entity-by-name "sequenceB-1")))
        (set-blackout-frames (seconds 20))
        (send-event *camera* 'clear-entity)
        (entity-birth-no-kill gp-0)
        (send-event (if gp-0 (-> gp-0 extra process)) 'play-anim)))
  :trans
    (behavior ()
      ;; og:preserve-this
      ;; yeah they messed up a little bit here
      (spool-push *art-control* "sage-intro-sequence-b" 0 self -1.0)
      ;; they should have put this:
      ;; (spool-push *art-control* "sidekick-human-intro-sequence-b" 0 self -1.0)
      ;; but it somehow causes a crash (maybe linux only?)
      ((-> (method-of-type process-taskable play-anim) trans))))

(defmethod should-display? ((this sequenceA-village1))
  #f)

(defbehavior sequenceA-village1-trans-hook sequenceA-village1 ()
  (when (>= 1000.0 (ja-aframe-num 0))
    (hide-hud-quick)
    (if (> (-> self part matrix) 0)
      (set-vector! (sprite-get-user-hvdf (-> self part matrix))
                   2048.0
                   2048.0
                   (+ -1024.0 (-> *math-camera* hvdf-off z))
                   (-> *math-camera* hvdf-off w)))
    (let ((f0-9 (fmin 1.0 (* 0.008333334 (- 1000.0 (ja-aframe-num 0))))))
      (set! (-> *part-id-table* 2860 init-specs 8 initial-valuef) (* 128.0 f0-9))
      (cond
        ((< f0-9 1.0)
         (set! (-> *part-id-table* 2858 init-specs 1 initial-valuef) 0.0)
         (set! (-> *part-id-table* 2859 init-specs 1 initial-valuef) 0.0))
        (else
         (set! (-> *part-id-table* 2858 init-specs 1 initial-valuef) 1.0)
         (set! (-> *part-id-table* 2859 init-specs 1 initial-valuef) 0.3))))
    (spawn (-> self part) *zero-vector*)
    (draw-title-credits (* 0.001 (ja-aframe-num 0))))
  0
  (none))

(defbehavior sequenceA-village1-init-by-other sequenceA-village1 ((arg0 entity-actor))
  (set! (-> self entity) arg0)
  (process-taskable-method-40 self arg0 *sage-sg* 3 40 (new 'static 'vector :w 4096.0) 5)
  (set! (-> self tasks) (get-task-control (game-task intro)))
  (set! (-> self boat) (the-as handle #f))
  (set! (-> self side) (the-as handle #f))
  (set! (-> self part) (create-launch-control group-sequenceAV-2d-intro-mist self))
  (set! (-> self part matrix) (sprite-allocate-user-hvdf))
  (set! (-> self cur-trans-hook) sequenceA-village1-trans-hook)
  (logclear! (-> self mask) (process-mask pause actor-pause))
  (set! (-> self been-kicked) #t)
  (set-setting! 'music-volume-movie 'abs 0.0 0)
  (set-setting! 'sfx-volume-movie 'abs 0.0 0)
  (set-setting! 'ambient-volume-movie 'abs 0.0 0)
  (go-virtual hidden)
  (none))

(defun start-sequence-a ()
  (set-blackout-frames (seconds 100))
  (process-spawn sequenceA-village1 (entity-by-name "sage-23") :to *entity-pool*)
  0
  (none))
