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

;; definition of type sunken-water
(deftype sunken-water (water-anim)
  ((use-sync?             symbol            :offset-assert 220)
   (playing-deadly-sound? symbol            :offset-assert 224)
   (deadly-time           float             :offset-assert 228)
   (deadly-fade           float             :offset-assert 232)
   (sync                  sync-info :inline :offset-assert 236)
   (safe-color-mult       vector    :inline :offset-assert 256)
   (safe-color-emissive   vector    :inline :offset-assert 272)
   (deadly-color-mult     vector    :inline :offset-assert 288)
   (deadly-color-emissive vector    :inline :offset-assert 304)
   )
  :heap-base #xd0
  :method-count-assert 31
  :size-assert         #x140
  :flag-assert         #x1f00d00140
  (:methods
    (draw-ripple (_type_) symbol 30)
    )
  )

;; definition for method 3 of type sunken-water
(defmethod inspect sunken-water ((obj sunken-water))
  (let ((t9-0 (method-of-type water-anim inspect)))
    (t9-0 obj)
    )
  (format #t "~T~Tuse-sync?: ~A~%" (-> obj use-sync?))
  (format #t "~T~Tplaying-deadly-sound?: ~A~%" (-> obj playing-deadly-sound?))
  (format #t "~T~Tdeadly-time: ~f~%" (-> obj deadly-time))
  (format #t "~T~Tdeadly-fade: ~f~%" (-> obj deadly-fade))
  (format #t "~T~Tsync: #<sync-info @ #x~X>~%" (-> obj sync))
  (format #t "~T~Tsafe-color-mult: #<vector @ #x~X>~%" (-> obj safe-color-mult))
  (format #t "~T~Tsafe-color-emissive: #<vector @ #x~X>~%" (-> obj safe-color-emissive))
  (format #t "~T~Tdeadly-color-mult: #<vector @ #x~X>~%" (-> obj deadly-color-mult))
  (format #t "~T~Tdeadly-color-emissive: #<vector @ #x~X>~%" (-> obj deadly-color-emissive))
  obj
  )

;; definition for symbol ripple-for-sunken-water, type ripple-wave-set
(define ripple-for-sunken-water (new 'static 'ripple-wave-set
                                  :count 3
                                  :converted #f
                                  :normal-scale 1.0
                                  :wave (new 'static 'inline-array ripple-wave 4
                                    (new 'static 'ripple-wave :scale 40.0 :xdiv 1 :speed 1.5)
                                    (new 'static 'ripple-wave :scale 40.0 :xdiv -1 :zdiv 1 :speed 1.5)
                                    (new 'static 'ripple-wave :scale 20.0 :xdiv 5 :zdiv 3 :speed 0.75)
                                    (new 'static 'ripple-wave)
                                    )
                                  )
        )

;; failed to figure out what this is:
(defpartgroup group-sunken-water-deadly-water
  :id 446
  :flags (use-local-clock)
  :bounds (static-bspherem 0 0 0 8)
  :parts ((sp-item 1736 :fade-after (meters 50)))
  )

;; failed to figure out what this is:
(defpart 1736
  :init-specs ((sp-tex spt-texture (new 'static 'texture-id :index #xf :page #x2))
    (sp-flt spt-num 1.0)
    (sp-flt spt-y (meters 0))
    (sp-rnd-flt spt-scale-x (meters 1) (meters 4) 1.0)
    (sp-flt spt-scale-y (meters 0.25))
    (sp-flt spt-r 255.0)
    (sp-flt spt-g 255.0)
    (sp-flt spt-b 255.0)
    (sp-rnd-flt spt-a 96.0 32.0 1.0)
    (sp-flt spt-scalevel-x (meters 0.1875))
    (sp-flt spt-scalevel-y (meters -0.001875))
    (sp-flt spt-fade-a -3.2)
    (sp-int spt-timer 81)
    (sp-cpuinfo-flags bit2 bit3 bit14)
    (sp-int spt-next-time 15)
    (sp-launcher-by-id spt-next-launcher 2022)
    )
  )

;; failed to figure out what this is:
(defpart 2022
  :init-specs ((sp-flt spt-r 255.0) (sp-rnd-flt spt-g 128.0 128.0 1.0) (sp-flt spt-b 0.0) (sp-flt spt-fade-a -1.28))
  )

;; definition for method 30 of type sunken-water
(defmethod draw-ripple sunken-water ((obj sunken-water))
  (set! (-> obj draw ripple send-query) #t)
  (let ((gp-0 (-> obj draw ripple query)))
    (let ((a0-1 (-> *display* base-frame-counter)))
      (set! (-> gp-0 start-vertex) (logand (* 13 (logand a0-1 127)) 127))
      )
    (set! (-> gp-0 vertex-skip) 128)
    (dotimes (s5-0 (-> gp-0 vertex-count))
      (sp-launch-particles-var
        *sp-particle-system-2d*
        (-> *part-id-table* 1736)
        (-> gp-0 data s5-0)
        (the-as sparticle-launch-state #f)
        (the-as sparticle-launch-control #f)
        1.0
        )
      )
    )
  #f
  )

;; failed to figure out what this is:
(defstate water-vol-idle (sunken-water)
  :virtual #t
  :event (behavior ((arg0 process) (arg1 int) (arg2 symbol) (arg3 event-message-block))
    (case arg2
      (('notify)
       (if (= (-> arg3 param 0) 'attack)
           (sound-play "get-shocked")
           )
       )
      (else
        ((-> (method-of-type water-anim water-vol-idle) event) arg0 arg1 arg2 arg3)
        )
      )
    )
  :trans (behavior ()
    (let ((t9-0 (-> (method-of-type water-anim water-vol-idle) trans)))
      (if t9-0
          (t9-0)
          )
      )
    (cond
      ((-> self use-sync?)
       (cond
         ((< (get-current-phase (-> self sync)) (-> self deadly-time))
          (when (!= (-> self deadly-fade) 1.0)
            (seek! (-> self deadly-fade) 1.0 (* 8.0 (-> *display* seconds-per-frame)))
            (let ((f30-0 (-> self deadly-fade)))
              (vector-lerp! (-> self draw color-mult) (-> self safe-color-mult) (-> self deadly-color-mult) f30-0)
              (vector-lerp!
                (-> self draw color-emissive)
                (-> self safe-color-emissive)
                (-> self deadly-color-emissive)
                f30-0
                )
              (set! (-> self draw color-mult w) 0.5)
              (if (= f30-0 1.0)
                  (set! (-> self flags) (logior (water-flags wt19) (-> self flags)))
                  )
              )
            )
          (draw-ripple self)
          (when (not (-> self playing-deadly-sound?))
            (set! (-> self playing-deadly-sound?) #t)
            (sound-play "water-on")
            )
          (update! (-> self sound))
          )
         (else
           (logclear! (-> self flags) (water-flags wt19))
           (when (!= (-> self deadly-fade) 0.0)
             (seek! (-> self deadly-fade) 0.0 (* 8.0 (-> *display* seconds-per-frame)))
             (let ((f30-1 (-> self deadly-fade)))
               (vector-lerp! (-> self draw color-mult) (-> self safe-color-mult) (-> self deadly-color-mult) f30-1)
               (vector-lerp!
                 (-> self draw color-emissive)
                 (-> self safe-color-emissive)
                 (-> self deadly-color-emissive)
                 f30-1
                 )
               (set! (-> self draw color-mult w) 0.5)
               (if (= f30-1 0.0)
                   (logclear! (-> self flags) (water-flags wt19))
                   )
               )
             )
           (when (-> self playing-deadly-sound?)
             (set! (-> self playing-deadly-sound?) #f)
             (stop! (-> self sound))
             (sound-play "water-off")
             )
           )
         )
       (set-fade!
         *palette-fade-controls*
         3
         (-> self deadly-fade)
         (vector-vector-distance (-> self root trans) (camera-pos))
         (the-as vector #f)
         )
       )
      (else
        (when (logtest? (water-flags wt19) (-> self flags))
          (draw-ripple self)
          (update! (-> self sound))
          (set! (-> self playing-deadly-sound?) #t)
          )
        )
      )
    (none)
    )
  :post (the-as (function none :behavior sunken-water) ja-post)
  )

;; definition for method 25 of type sunken-water
;; INFO: Return type mismatch uint vs none.
(defmethod TODO-RENAME-25 sunken-water ((obj sunken-water))
  (let ((t9-0 (method-of-type water-anim TODO-RENAME-25)))
    (t9-0 obj)
    )
  (set! (-> obj playing-deadly-sound?) #f)
  (set-vector! (-> obj safe-color-mult) 0.125 0.225 0.22 0.5)
  (set-vector! (-> obj safe-color-emissive) 0.0 0.0 0.0 1.0)
  (set-vector! (-> obj deadly-color-mult) 0.125 0.225 0.22 0.5)
  (set-vector! (-> obj deadly-color-emissive) 0.33 0.6 0.0 1.0)
  (let ((s5-0 (load-params! (-> obj sync) obj (the-as uint 1500) 0.0 0.15 0.15)))
    (let ((f0-16 (res-lump-float (-> obj entity) 'percent :default -1.0)))
      (if (or (< f0-16 0.0) (< 1.0 f0-16))
          (set! f0-16 (cond
                        (s5-0
                          0.5
                          )
                        ((logtest? (water-flags wt19) (-> obj flags))
                         1.0
                         )
                        (else
                          0.0
                          )
                        )
                )
          )
      (when (and s5-0 (or (= f0-16 0.0) (= f0-16 1.0)))
        (set! s5-0 #f)
        (if (= f0-16 0.0)
            (set! (-> obj flags) (logior (water-flags wt19) (-> obj flags)))
            (logclear! (-> obj flags) (water-flags wt19))
            )
        )
      (set! (-> obj use-sync?) s5-0)
      (set! (-> obj deadly-time) f0-16)
      )
    (when s5-0
      (if (< (get-current-phase (-> obj sync)) (-> obj deadly-time))
          (set! (-> obj flags) (the-as water-flags (logior (water-flags wt19) (-> obj flags))))
          (set! (-> obj flags) (the-as water-flags (logclear (-> obj flags) (water-flags wt19))))
          )
      )
    )
  (none)
  )

;; definition for method 22 of type sunken-water
;; INFO: Return type mismatch ripple-merc-query vs none.
;; Used lq/sq
(defmethod TODO-RENAME-22 sunken-water ((obj sunken-water))
  (let ((t9-0 (method-of-type water-anim TODO-RENAME-22)))
    (t9-0 obj)
    )
  (set! (-> obj play-ambient-sound?) #f)
  (cond
    ((logtest? (water-flags wt19) (-> obj flags))
     (set! (-> obj draw color-mult quad) (-> obj deadly-color-mult quad))
     (set! (-> obj draw color-emissive quad) (-> obj deadly-color-emissive quad))
     (set! (-> obj deadly-fade) 1.0)
     )
    (else
      (set! (-> obj draw color-mult quad) (-> obj safe-color-mult quad))
      (set! (-> obj draw color-emissive quad) (-> obj safe-color-emissive quad))
      (set! (-> obj deadly-fade) 0.0)
      )
    )
  (let ((s5-0 (new 'process 'ripple-control)))
    (set! (-> obj draw ripple) s5-0)
    (set! (-> s5-0 global-scale) 3072.0)
    (set! (-> s5-0 close-fade-dist) 163840.0)
    (set! (-> s5-0 far-fade-dist) 245760.0)
    (set! (-> s5-0 waveform) ripple-for-sunken-water)
    (set! (-> s5-0 query) (new 'process 'ripple-merc-query 100))
    )
  (none)
  )
